0

I am trying to create a midifile using mido but I didn't find a clear example of how to create a midifile using mido. what I am looking for is the equivilent of this code using midiutil:


degrees  = [60, 62, 64, 65, 67, 69, 71, 72]  # MIDI note number
track    = 0
channel  = 0
time     = 0    # In beats
duration = 1    # In beats
tempo    = 60   # In BPM
volume   = 100  # 0-127, as per the MIDI standard

MyMIDI = MIDIFile(1)  # One track, defaults to format 1 (tempo track is created
                      # automatically)
MyMIDI.addTempo(track, time, tempo)
MyMIDI.addTimeSignature(0, time, 4, 2, 24)

for i, pitch in enumerate(degrees):
    MyMIDI.addNote(track, channel, pitch, time + i, duration, volume)

with open("major-scale.mid", "wb") as output_file:
    MyMIDI.writeFile(output_file)

I hope someone wants to explain how to(using mido):

  • create midiobject
  • set tempo
  • set timesignature
  • add note
  • add keysignature
  • save the file as midifile.mid.

Thanks in advance!

1 Answers1

1

No one's helping you? Well, the answers are all in the docs anyway. See p.37 in the official mido-readthedocs-io-en-latest.pdf ...

from mido import Message, MetaMessage, MidiFile, MidiTrack, bpm2tempo, second2tick

mid = MidiFile()
track = MidiTrack()
mid.tracks.append(track)

track.append(MetaMessage('key_signature', key='Dm'))
track.append(MetaMessage('set_tempo', tempo=bpm2tempo(120)))
track.append(MetaMessage('time_signature', numerator=6, denominator=8))

track.append(Message('program_change', program=12, time=10))
track.append(Message('note_on', channel=2, note=60, velocity=64, time=1))
track.append(Message('note_off', channel=2, note=60, velocity=100, time=2))

track.append(MetaMessage('end_of_track'))

mid.save('new_song.mid')

And here's what it looks like:

$ midicsvpy new_song.mid  newsong.csv
$ cat newsong.csv 
0, 0, Header, 1, 1, 480
1, 0, Start_track
1, 0, Key_signature, -1, "minor"
1, 0, Tempo, 500000
1, 0, Time_signature, 6, 3, 24, 8
1, 10, Program_c, 0, 12
1, 11, Note_on_c, 2, 60, 64
1, 13, Note_off_c, 2, 60, 100
1, 13, End_track
0, 0, End_of_file
aMike
  • 852
  • 5
  • 14