from midiwrapper import Song from keras.models import load_model import numpy as np if __name__ == '__main__': m = load_model('./temp/gan2.h5') m.summary() bs, seq_len, note_dim, _ = m.output_shape code_dim = m.input_shape[-1] def code_generator(bs): Z = np.random.uniform(-1., 1., size=(bs, code_dim)) return Z mid = Song() track = mid.add_track() notes = m.predict(code_generator(1))[0, :, :, 0] mid._compose(track, notes, deltat=100, threshold=1.) mid.save_as('gan2.mid')
seq = get_openning(LEN, mode='borrow') notes = [] # deepcopy(seq) accumulate = np.zeros((dim, )).astype('int') for _ in range(SONG_LEN): note = model.predict(np.array([seq]))[0][-1] seq.pop(0) # sustain too long accumulate = accumulate * (note >= THRESHOLD) + (note >= THRESHOLD) note[accumulate >= MAX_SUSTAIN] = 0. accumulate[accumulate >= MAX_SUSTAIN] = 0. print ''.join([('x' if char >= THRESHOLD else '_') for char in note]) notes.append(note) if (note >= THRESHOLD).sum() == 0: # too less notes print 'no note, max =', note.max() # note = np.random.rand(dim,) # seq = get_openning(LEN) if note.max() > 0: note /= note.max() seq.append(note) else: # use output as input note = note * np.random.uniform(0.5, 2.0, size=(dim, )) seq.append(note) mid.compose(track, np.array(notes), deltat=200, threshold=THRESHOLD) mid.save_as('simple_rnn.mid')
THRESHOLD = 0.85 hots = Song(FILE).\ encode_onehot( {'filter_f': lambda x: x.type in ['note_on', 'note_off'], 'unit': 'beat'}, {'resolution': 0.25}) print hots.shape model = load_model('temp/memorize.h5') mid = Song() track = mid.add_track() seq = [hots[i] for i in range(20)] # seq = [np.random.binomial(1, 0.3, (dim,)) for _ in range(LEN)] notes = [] # deepcopy(seq) accumulate = np.zeros((dim,)).astype('int') for _ in range(SONG_LEN): note = model.predict(np.array([seq]))[0] seq.pop(0) print ''.join([('x' if char >= THRESHOLD else '_') for char in note]) notes.append(note) # use output as input # note = note * np.random.uniform(0.75, 1.2, size=(dim, )) seq.append(note) mid._compose(track, np.array(notes), deltat=100, threshold=THRESHOLD) mid.save_as('memorize.mid')
from mido import MidiFile midi = MidiFile('./datasets/e-comp/2002chan01.mid') encoder = AllInOneEncoder() code = encoder.encode(midi) msg = encoder.decode(code) print code.dtype print code.shape # print msg[-10:] from midiwrapper import Song s = Song() track = s.add_track() for msgi in msg: track.append(msgi) s.save_as("decode.mid") # import os # from tqdm import tqdm # dirpath = './datasets/e-comp/' # tmppath = './datasets/e-comp-allinone/' # encoder = AllInOneEncoder() # filelist = [] # for root, _, files in os.walk(dirpath): # for name in files: # filelist.append(os.path.join(root, name)) # for filename in tqdm(filelist): # midi = Song(filename) # hots = encoder(midi.midi).astype('bool') # np.savez_compressed # (os.path.join(tmppath, filename.split('/')[-1]+'.npz'),