def split_in_half(mid, verbose=True, write_to_file=False): mid = parse_mid(mid) if mid.type != 0: print("Wrong Midi file type. I did not dare to change anything.".format(mid.type)) return None ticks = 0 flat_track = MidiTrack() for msg in mid.tracks[0]: ticks += msg.time if verbose: print("Original Length in ticks".format(ticks)) half = int(ticks / 2) dur = 0 for msg in mid.tracks[0]: if dur < half: flat_track.append(msg) dur += msg.time if flat_track[-1].type == 'note_on': flat_track.pop(-1) flat_track.append(MetaMessage("end_of_track", time=0)) # replace the 'tracks' field with a single track containing all the messages. # later on we can check for duplicates in certain fields (tempo, timesignature, key) mid.tracks.clear() mid.type = 0 mid.tracks.append(flat_track) if write_to_file: mid.save(mid) if verbose: print("Overwriting midifile with changes.") return mid
velocity=90, time=round(0.96 * meta_time * length), channel=channel)) track.append( Message('note_on', note=base_note + base_num * 12 + sum(major_notes[0:2]), velocity=90, time=round(delay * meta_time), channel=channel)) track.append( Message('note_off', note=base_note + base_num * 12 + sum(major_notes[0:2]), velocity=90, time=round(0.96 * meta_time * length), channel=channel)) track.pop() track.pop() track.append( Message('note_off', note=base_note + base_num * 12 + sum(major_notes[0:2]), velocity=90, time=round(0.96 * meta_time * length), channel=channel)) track.append( Message('note_on', note=base_note + base_num * 12 + sum(major_notes[0:3]), velocity=90, time=round(delay * meta_time), channel=channel)) track.append( Message('note_off',
def output_to_midi(ml_arr, filepath, barlength, channels, file_name): channel = 0 mid = MidiFile(filepath) for i in range(15): if i not in channels: channel = i break time = 0 index = 0 output_file = MidiFile(type=0) track = MidiTrack() output_file.tracks.append(track) nextBarStart = barlength firstMessage = True output_file.ticks_per_beat = mid.ticks_per_beat for i, t in enumerate(mid.tracks): print('Track {}: {}'.format(i, track.name)) for msg in t: old_time = time time += msg.time if not msg.is_meta: if (firstMessage): firstMessage = False for note in arr_to_chord(ml_arr[index]): track.append( Message('note_on', channel=channel, note=note, time=0)) track.append(msg) continue # if (index < len(ml_arr)): while (time >= nextBarStart and index < len(ml_arr)): msgs_to_add = [] for note in arr_to_chord(ml_arr[index]): msgs_to_add.append( Message('note_off', channel=channel, note=note, time=0)) index += 1 if (index < len(ml_arr)): for note in arr_to_chord(ml_arr[index]): msgs_to_add.append( Message('note_on', channel=channel, note=note, time=0)) if (nextBarStart - old_time >= 0 and nextBarStart - old_time < barlength): msgs_to_add[0].time = nextBarStart - old_time else: msgs_to_add[0].time = barlength for m in msgs_to_add: track.append(m) msg.time = time - nextBarStart nextBarStart += barlength track.append(msg) # else: # track.append(msg) m = track.pop(-1) for note in arr_to_chord(ml_arr[-1]): track.append( Message('note_off', channel=channel, note=note, time=0)) track.append(m) output_file.save(default_storage.path('tmp/' + file_name)) file_full_path = default_storage.path('tmp/' + file_name) return file_name