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_get_events_from_roll(self): # plt.imshow(self.test_roll[3, :, :], origin="lower", interpolation='nearest', aspect='auto') # plt.show() # plt.close() l2 = self.mid.get_events_from_roll(self.test_roll) mid2 = MidiFile() mid2.tracks.extend(l2) l3 = mid2.get_roll()
def test_bar(self): mid = MidiFile() track = MidiTrack() mid.tracks.append(track) # append bar to track track.append(bar) track.append(bar2)
def test__get_events_from_roll(self): # plt.imshow(self.test_roll[3, :, :], origin="lower", interpolation='nearest', aspect='auto') # plt.show() track = self.mid._get_events_from_roll(self.test_roll[3, :, :], 3) mid2 = MidiFile() mid2.tracks.append(track) roll = mid2.get_roll(down_sample_rate=1)[3, :, :] # plt.imshow(roll, origin="lower", interpolation='nearest', aspect='auto') # plt.show() diff = ((roll > 0) * 1 - (self.test_roll[3, :, :roll.shape[1]] > 0) * 1) diff2 = roll - self.test_roll[3, :, :roll.shape[1]] ret1 = diff.sum() if ret1 != 0: raise ValueError ret2 = (diff2.sum()) if ret2 != 0: 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()
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) ] drum = MidiTrack(channel=9) choice = np.random.choice(8, 3, replace=False) tracks_ = [tracks[i] for i in choice] # tracks_.append(drum) for track in tracks_: track.append(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() print(tracks_)
from mimi import MidiFile, MidiTrack, generator from mimi.Mimi import AbsNote import mido for ins in range(128): for x in range(40, 76): with MidiFile() as mid: track = MidiTrack(channel=0, instrument=ins) mid.tracks.append(track) track.append(AbsNote(pitch=x, time=256)) mid.save_mp3("data/mp3/%03d-%03d.mp3" % (ins, x)) mid.save_npz("data/npz/%03d-%03d.npz" % (ins, x)) # mid.draw_roll() # mid.play()
from mimi import MidiFile, MidiTrack, generator from mimi.Mimi import AbsNote from mimi.instrument import Piano, Strings track = MidiTrack(channel=0, instrument=Piano.AcousticGrandPiano) for x in range(40, 76): track.append(AbsNote(pitch=x,time=256)) # 把track放到MidiFile物件中 mid = MidiFile() mid.tracks.append(track) # 對mid物件進行存檔/播放等操作 mid.save_mp3("test.mp3")
os.mkdir(mid_path) if os.path.exists(json_path) is not True: os.mkdir(json_path) if os.path.exists(npy_path) is not True: os.mkdir(npy_path) if os.path.exists(wav_path) is not True: os.mkdir(wav_path) for x in range(10): # create midi object mid = MidiFile() track = MidiTrack() 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_bar(tab) # save files mid.save(join(mid_path, filename + ".mid")) array = tab.to_array() np.save(join(npy_path, filename + ".npy"), array)
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
def setUp(self): self.mid = MidiFile("../mimi/test_file/imagine_dragons-believer.mid") self.roll = self.mid.get_roll() self.test_roll = self.roll[:, :, 3000:6000]
class TestMidiFile(unittest.TestCase): def setUp(self): self.mid = MidiFile("../mimi/test_file/imagine_dragons-believer.mid") self.roll = self.mid.get_roll() self.test_roll = self.roll[:, :, 3000:6000] def test__get_events_from_roll(self): # plt.imshow(self.test_roll[3, :, :], origin="lower", interpolation='nearest', aspect='auto') # plt.show() track = self.mid._get_events_from_roll(self.test_roll[3, :, :], 3) mid2 = MidiFile() mid2.tracks.append(track) roll = mid2.get_roll(down_sample_rate=1)[3, :, :] # plt.imshow(roll, origin="lower", interpolation='nearest', aspect='auto') # plt.show() diff = ((roll > 0) * 1 - (self.test_roll[3, :, :roll.shape[1]] > 0) * 1) diff2 = roll - self.test_roll[3, :, :roll.shape[1]] ret1 = diff.sum() if ret1 != 0: raise ValueError ret2 = (diff2.sum()) if ret2 != 0: raise ValueError def test_get_events_from_roll(self): # plt.imshow(self.test_roll[3, :, :], origin="lower", interpolation='nearest', aspect='auto') # plt.show() # plt.close() l2 = self.mid.get_events_from_roll(self.test_roll) mid2 = MidiFile() mid2.tracks.extend(l2) l3 = mid2.get_roll() # plt.imshow(l3[3, :, :], origin="lower", interpolation='nearest', aspect='auto') # plt.show() # # plt.close() 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 def test_save_npz(self): self.mid.save_npz("test.npz") os.remove('test.npz') def test_save_mp3(self): mid = self.mid directory_name = tempfile.mkdtemp() mp3_path = os.path.join(directory_name, 'test.mp3') mid.save_mp3(mp3_path) # os.remove(mp3_path) # os.removedirs(directory_name) print(directory_name) def test_play(self): # set_soundfont(r"C:\Users\cswu\Desktop\mimi\mimi\soundfont\FluidR3_GM.sf2") self.mid.play() def test_get_roll(self): mid = self.mid roll = mid.get_roll() l = roll[:, :, 3000:6000] # plt.imshow(l[3, :, :], origin="lower", interpolation='nearest', aspect='auto') # plt.show() mid.draw_roll() def tearDown(self): del self.mid del self.roll del self.test_roll return 0
* 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") # 這個應該不用解釋
from mimi import MidiFile mid = MidiFile("test.mid") mid.draw_roll()