def test_g(self): for x in range(10): # create midi object mid = MidiFile() track = MidiTrack(instrument=Piano.ElectricPiano1) mid.tracks.append(track) # get random tab and append to midi track tab = generator.get_random_tab(tempo=70) filename = "%s" % str(x) track.append(tab) # save files mid.save(join(self.mid_path, filename + ".mid")) array = tab.to_array() np.save(join(self.npy_path, filename + ".npy"), array) output.midi2wav(join(self.mid_path, filename + ".mid"), join(self.wav_path, filename + ".wav")) json = tab.to_json() output.json(join(self.json_path, filename + ".json"), json)
def test_key_shift(self): for x in range(1): tracks = [ MidiTrack(channel=0, instrument=Piano.AcousticGrandPiano), MidiTrack(channel=1, instrument=Guitar.ElectricGuitar_jazz), MidiTrack(channel=2, instrument=Strings.Cello), MidiTrack(channel=3, instrument=Brass.Trumpet), MidiTrack(channel=4, instrument=Organ.ChurchOrgan), MidiTrack(channel=5, instrument=Guitar.AcousticGuitar_steel), MidiTrack(channel=6, instrument=SynthLead.Lead2_sawtooth), MidiTrack(channel=7, instrument=Guitar.OverdrivenGuitar) ] tracks_ = [tracks[i] for i in [1, 2, 3]] for track in tracks_: track.append(generator.get_random_tab(tempo=70)) combo = ["123"] mid_ = MidiFile() mid_.tracks.extend(tracks_) mid_.set_tick_per_beat(50) for indices in combo: mid = MidiFile() mid.set_tick_per_beat(50, resample=False) for index in indices: mid.tracks.append(tracks_[int(index) - 1]) # TODO : fix 1+2+3 bug # TODO : resample bug # TODO : only piano bug event_nb = [] for key_shift in range(3): mid.key_shift(1) print(len(mid.get_events()[0])) event_nb.append(len(mid.get_events()[0])) if event_nb[0] != event_nb[1]: raise ValueError
from mimi import MidiFile, MidiTrack, generator for x in range(100): tracks = [MidiTrack(channel=x, instrument=(x + 1) * 8) for x in range(3)] for track in tracks: track.append_bar(generator.get_random_tab(tempo=70)) mid = MidiFile() mid.tracks.extend(tracks) # mid.draw_roll() mid.save_mp3("data/mp3/%03d.mp3" % x) mid.save_npz("data/npz/%03d.npz" % x) # mid.play()
from mimi import generator import matplotlib.pyplot as plt # get random tab and append to midi track tab = generator.get_random_tab(tempo=70) array = tab.to_array() print(array.shape) # piano roll plt.imshow(array[:, :, 0]) # onset event plt.imshow(array[:, :, 1])
* channel最多只有16個,track不限 * 你可以有複數個track放在同一個channel #但不建議 * 建議一個channel就配一個track * 建議一個channel只配一種樂器,配兩種會有bug # """ # 創造兩個track,放在不同channel,使用不同樂器 tracks = [ MidiTrack(channel=0, instrument=Piano.AcousticGrandPiano), MidiTrack(channel=1, instrument=Strings.Cello) ] # 對每個track生成隨機音符/和弦 for track in tracks: track.append(generator.get_random_tab(tempo=70, key="C")) # 把tracks放到MidiFile物件中 mid = MidiFile() mid.tracks.extend(tracks) # 對mid物件進行存檔/播放等操作 mid.draw_roll() # 用matplot作圖 mid.play() # 用播放器放出聲音, windows和mac預設是用timidity, ubuntu是用ffplay # roll = mid.get_roll() # np.array of pinao roll in 16 channel in shape [channel, pitch, time] # mid.save_npz("test.npz") # npz中有兩個array, instrument是標記每個channel用啥樂器, 另外一個是pinao roll(draw_roll畫出來的array) # mid.save_png("test.png") # piano roll的圖 # mid.save_mp3("test.mp3") # 這個應該不用解釋