How to set the speed of mid.play() #374
-
I want to control the sending speed of mid.play, for example, 1.5x, 2x |
Beta Was this translation helpful? Give feedback.
Answered by
rdoursenaud
May 13, 2022
Replies: 1 comment
-
The def play(self, meta_messages=False):
"""Play back all tracks.
The generator will sleep between each message by
default. Messages are yielded with correct timing. The time
attribute is set to the number of seconds slept since the
previous message.
By default you will only get normal MIDI messages. Pass
meta_messages=True if you also want meta messages.
You will receive copies of the original messages, so you can
safely modify them without ruining the tracks.
"""
start_time = time.time()
input_time = 0.0
for msg in self:
input_time += msg.time
playback_time = time.time() - start_time
duration_to_next_event = input_time - playback_time
if duration_to_next_event > 0.0:
time.sleep(duration_to_next_event)
if isinstance(msg, MetaMessage) and not meta_messages:
continue
else:
yield msg One way to do it would be to override it in your own subclass: class MyMidiFile(mido.MidiFile):
def play(self, meta_messages=False, speed=1):
"""Play back all tracks.
The generator will sleep between each message by
default. Messages are yielded with correct timing. The time
attribute is set to the number of seconds slept since the
previous message.
By default you will only get normal MIDI messages. Pass
meta_messages=True if you also want meta messages.
You will receive copies of the original messages, so you can
safely modify them without ruining the tracks.
"""
start_time = time.time()
input_time = 0.0
for msg in self:
input_time += msg.time * speed
playback_time = time.time() - start_time
duration_to_next_event = input_time - playback_time
if duration_to_next_event > 0.0:
time.sleep(duration_to_next_event)
if isinstance(msg, MetaMessage) and not meta_messages:
continue
else:
yield msg |
Beta Was this translation helpful? Give feedback.
0 replies
Answer selected by
yk0n9
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The
play
method from themido.MidiFile
class is pretty straightforward: