def write_file(filename, chords): """ Writes a midi file of the given file name from the list of chords given. """ midi = MidiOutFile(open(filename, 'wb')) midi.header(format=0, nTracks=12, division=16) midi.start_of_track() time = 0 prevBass = 0 prevChord = [] for i in range(len(chords)): c = str_to_pitches(chords[i], prevBass) nextChord = [] if i + 1 >= len(chords) else str_to_pitches( chords[i + 1], c[0] if len(c) > 0 else 0) for n in c: if n not in prevChord: midi.update_time(time, relative=False) midi.note_on(0, n, V) time += 8 for n in c: if n not in nextChord: midi.update_time(time, relative=False) midi.note_off(0, n, V) prevBass = c[0] if len(c) > 0 else 0 prevChord = c midi.update_time(0) midi.end_of_track()
def write_midi_sequence(midi_file,sequence): QUARTER = 480 BAR = QUARTER*4 GRAND_PIANO = 0 out_file = open(midi_file, 'wb') midi = MidiOutFile(out_file) midi.header(format=1, nTracks=2, division=QUARTER) ########################## # track 0 # in type 1 midi files first track MUST be a timing and tempo track # and no musical events are allowed. midi.start_of_track() midi.sequence_name(b'Timing track') midi.time_signature(nn=4, dd=2, cc=24, bb=8) # optional midi.tempo_bpm(135) midi.end_of_track() ########################## # track 1 CH = 0 midi.start_of_track() midi.patch_change(CH, GRAND_PIANO) midi.sequence_name(b'piano roll %i' % CH) midi.update_time(0) # go trough all notes in 'sequence' and add them to the track for NOTE in sequence.split( ): midi.note_on(CH, int(NOTE), 0x64) midi.update_time(int(BAR/4)) # how much to sustain a note midi.note_off(CH, int(NOTE), 0x40) midi.update_time(int(QUARTER/2)) # how much time to wait between notes midi.update_time(BAR*4) midi.update_time(0) midi.end_of_track()
def make_midi(melody, file): out_file = open(f"./output/{file}.midi", 'wb') midi = MidiOutFile(out_file) midi.header(format=0, nTracks=1, division=32) midi.start_of_track() # print(f"Midi_notes: {melody}") try: for note, duration in melody: if note is 0: # if it is a pause midi.note_on(channel=0, note=0) midi.update_time(0) midi.note_off(channel=0, note=0) midi.update_time(duration) midi.note_on(channel=0, note=0) midi.update_time(0) midi.note_off(channel=0, note=0) midi.update_time(0) else: midi.update_time(0) if isinstance(note, list): # checks if note is single, or chord for n in note: midi.note_on(channel=0, note=n) else: midi.note_on(channel=0, note=note) midi.update_time(duration) if isinstance(note, list): for n in note: midi.note_off(channel=0, note=n) else: # if note is not 0: midi.note_off(channel=0, note=note) midi.update_time(0) midi.end_of_track() except: return False return True
# in type 0 midi files there is only one track. # there can be different midi channels in the same track, mo matter # the type of midi file out_file = open(exampledir('midi-out/mxm_midifile_type_0_minimal.mid'), 'wb') midi = MidiOutFile(out_file) midi.header(format=0, nTracks=1, division=QUARTER) ##################### # track 0 begin midi.start_of_track() midi.tempo_bpm(135) midi.time_signature(nn=4, dd=2, cc=24, bb=8) midi.patch_change(0, GRAND_PIANO) # note on midi.update_time(0) midi.note_on(0, NOTE, 0x64) # note off one bar later midi.update_time(BAR) midi.note_off(0, NOTE, 0x40) midi.update_time(0) midi.end_of_track() # track 0 end #####################