craffel/pretty-midi

"data byte must be int" when using hz_to_note_number

ewatts3 opened this issue · 1 comments

Hello, I am trying to use the hz_to_note_number function so that I can utilize microtonal pitches. When I run code that looks like this:

import pretty_midi

pm = pretty_midi.PrettyMIDI()
cello_program = pretty_midi.instrument_name_to_program('Cello')
cello = pretty_midi.Instrument(program=cello_program)

note = pretty_midi.Note(velocity=100, 
                        pitch=pretty_midi.hz_to_note_number(200), 
                        start=0, 
                        end=10)
cello.notes.append(note)
pm.instruments.append(cello)
pm.write('output.mid')

I get an error on the last line telling me that "data byte must be int." If I change the "pitch=" parameter to just an int within 0-127, I of course don't have this issue. I know fractional values of MIDI notes should be supported, so is there something wrong with my syntax that is causing the issue?

pretty_midi.hz_to_note_number returns values as a float (fractional note values), but the MIDI protocol doesn't support floating point whatsoever. You need to cast the output of https://github.com/craffel/pretty-midi/blob/master/pretty_midi/utilities.py#L274 to int if you plan to write it out later.