craffel / pretty-midi

Utility functions for handling MIDI data in a nice/intuitive way.
MIT License
856 stars 151 forks source link

get piano roll with the right sampling rate #210

Closed BaptisteLoquette closed 2 years ago

BaptisteLoquette commented 2 years ago

Hello, hope you're doing well. When using the get_piano_roll() function, the sampling rate it set by default to 100, but for many midi files it doesn't fit good. When using the piano_roll_to_pretty_midi() function we can see a lot of changes in comparison with the original MIDI file.

How to get the right sampling rate ? I can't find a function to get the sampling rate of the initial MIDI file through pretty midi or Mido. Can you help me please ?

Thanks

craffel commented 2 years ago

MIDI files do not have a sampling rate per se. You could probably compute an optimal sampling rate based on the pulses per quarter note value of the MIDI file, but there would not be a fixed sampling rate - it would depend on the tempo, which can change over the course of the file.

BaptisteLoquette commented 2 years ago

Okay I see, but from the tempos could I determine the best sampling rate ?

craffel commented 2 years ago

You could, but it would vary over the course of the MIDI file.

BaptisteLoquette commented 2 years ago

But a software like MuseScore3 seems to find the appropriate sampling rate to render from the initial MIDI file, though with the combination of get_piano_roll() then piano_roll_to_pretty_midi() the rendered MIDI file in MuseScore seems either too slow either too fast you see ?

craffel commented 2 years ago

piano_roll_to_pretty_midi is an approximate/example function - you should not use it for anything except for fun.

BaptisteLoquette commented 2 years ago

Okay I see, but get_piano_roll() can be used no just for fun right ?

craffel commented 2 years ago

Depends on your definition of fun, I guess. It should only be used in settings where using a fixed sampling rate for the entire MIDI file is appropriate/workable.

BaptisteLoquette commented 2 years ago

I see, thanks for all your answers, I really appreciate that. Supposing a constant known tempo how could I determine the best sampling rate pls ? If you don't have time to answer could you give me some references on the subject pls ?

craffel commented 2 years ago

You'll need to set it according to the pulses per quarter note and the tempo - you can start with https://en.wikipedia.org/wiki/Pulses_per_quarter_note. I will leave derivation of the exact formula up to the reader :)

BaptisteLoquette commented 2 years ago

Thx !