craffel/pretty-midi

Why does the pianoroll conversion returns a pitch range of 128 ?

Natooz opened this issue · 3 comments

Hello,

First of all thank you for all your work and this great library that I've been using for several month.
My usage of pretty midi is to convert midi files to piano rolls arrays to train neural networks to reproduce music and help composers to create music.
However as deep learning models are very consuming in resources, I need to reduce its size, and so its input/output dimensions.

The get_piano_roll method has been very useful to me, but I was wondering why it returns arrays where the length of the pitch dimension is 128, although instruments like piano only have 88 keys/pitches.
Moreover having a 128 pitch length dimension increase the sparsity of the data and is another issue in my case.

Of course I can make my own "get_piano_roll" method to solve my issue, but I would also like to understand why this method return a pitch range of 128. I took a quick look at the code but didn't find any answer.

Because the MIDI spec defines 128 midi note numbers.

Most of us who work with piano rolls indeed will crop them internally to a smaller range, but a full piano roll of 128 pitches is needed to represent any possible midi file. With MIDI, you can made sounds that are out of the range of the piano keyboard.

Thank you for your quick and clear answer, I must admit that I am not aware of most of the MIDI specs.
In that case I will probably process these arrays in a way that suits my needs.

Do you think that a modified version of the get_piano_roll method with a parameter to automatically crop the pitch dimension depending on the instrument id could have an interest in this library ?

Do you think that a modified version of the get_piano_roll method with a parameter to automatically crop the pitch dimension depending on the instrument id could have an interest in this library ?

Hi Nathan, I think this is best left for user code.