def generate_music(track_dists, starts, time=(0.5 * 60 * 1000)): ''' produce new midi data from the distributions this should probably be re-written browser-side ''' starts.reverse() mid = MidiFile() json_data = [] for dist in track_dists: # create a new track for the generated music track = MidiTrack() mid.tracks.append(track) # create a text version json_track = [] # select the same first note as the original piece start = starts.pop() token = start count = 0 while count < time: for note in token.split('|'): note = Message.from_str(note) count += note.time track.append(note) json_track.append({ 'note': note.note, 'velocity': note.velocity, 'time': note.time, }) options = dist[token] try: token = weighted_choice(options) except IndexError: print('no followup found for token: %s' % token) token = start json_data.append(json_track) mid.save('new.mid') json.dump(json_data, open('new.json', 'w'))
track = MidiTrack() # fname = "20th-century-fox.txt" fname = "glich.txt" with open(fname) as f: lines = f.readlines() for line in lines: line = line.strip() # print(line) if line.startswith("Track"): print(line) track = MidiTrack() mid.tracks.append(track) msg = Message.from_str( "control_change channel=0 control=7 value=100 time=5") track.append(msg) print msg # elif line.startswith("program_change"): # continue # elif line.startswith("control_change"): # continue elif line.startswith("<meta"): continue else: print(line) msg = Message.from_str(line) track.append(msg) mid.save("glich.mid")