0

I am writing the script simply play the midi file

import pretty_midi
import mido

port = mido.open_output(mido.get_output_names()[0]) # open port of software synthesizer.

midi_file = '2021-01-02_101128_03.mid'
midi_data = pretty_midi.PrettyMIDI(midi_file)

notes = midi_data.instruments[0].notes # get notes from trackone.

for note in notes:
    print(note)
    msg = Message('note_on', note=note.pitch)
    port.send(msg) # send message to synthesizer

It plays every notes at the same (for loop is too fast for us!)

I need to control the timing of sending message.

How can I make it ? how can I control the timing of sending message?

in Midi file there are message like this.

Note(start=0.000000, end=0.125000, pitch=60, velocity=100)
Note(start=0.375000, end=0.500000, pitch=60, velocity=100)
Note(start=0.500000, end=0.625000, pitch=64, velocity=100)
Note(start=0.750000, end=0.875000, pitch=67, velocity=100)
Note(start=1.000000, end=1.125000, pitch=68, velocity=100)
Note(start=1.375000, end=1.500000, pitch=67, velocity=100)
Note(start=1.500000, end=1.625000, pitch=65, velocity=100)
Note(start=1.750000, end=1.875000, pitch=62, velocity=100)
Note(start=1.875000, end=2.000000, pitch=62, velocity=100)
whitebear
  • 11,200
  • 24
  • 114
  • 237
  • Your data appears to have `start` and `end` attributes from which you can calculate the note duration. Maybe just sleep that amount of time and then send a `note_off` message? – larsks Jan 02 '21 at 13:50
  • yes, I am thinking to do so, but it becomes too complex. For example note on every 0.1sec, like `playstart` + wait(0.1) +`playstart` + wait(0.1) + `playstart` + wait(0.1) + `playstart` (ignore playstop). I guess it's not correct because period time become 0.1sec plus python exec `playstart` time(maybe very small 0.001 though) – whitebear Jan 02 '21 at 14:24
  • You guess? Or you tried that and it didn't work? It would be great if you could update your question to show specifically what you've tried, and explain in the question why the solution didn't work out as desired. That will make it easier for folks to find than having it here in the comments. – larsks Jan 02 '21 at 15:25
  • Yes you are right. I made code and tried, but I can't check this is correct timing or not. i need to record it to check.... That's another problem. – whitebear Jan 03 '21 at 03:39

0 Answers0