I am trying to extract data from MIDI files using the music21 python module. The issue I am having is when I try to get data for note duration from a note that is an n-tuple (e.g. a 32nd note Triplet). The module usually returns the note length as a fraction of quarter lengths (e.g. a quarter note is 1.0, an eight is 0.5, etc.). However, for triplets it returns a python Fraction object (e.g. Fraction(1, 12)). I have the following loop:
for note in notes_from_stream:
temp_arr = []
temp_arr.append(get_midi_representation(note))
temp_arr.append(note.duration.dots)
temp_arr.append(note.duration.quarterLength)
tup = float(note.duration.quarterLength.numerator) / (note.duration.quarterLength.denominator)
temp_arr.append(tup)
note_list_arr.append(temp_arr)
The temp_arr is added to note_list_arr at the end of each iteration. After the loop finishes, I create a new 2x2 numpy array from note_list_arr with the numpy.asarray() function. So, the actual problem is after all the data is in the numpy array I get the following contents in it:
[[128 0 Fraction(1, 12) 0.08333333333333333]
[128 0 Fraction(1, 24) 0.041666666666666664]]
The problem with this is that it contains the Fraction object, but if I remove the line which puts it there (temp_arr.append(note.duration.quarterLength) and leave only the one which calculates the real number value of the fraction, I get the following:
[[ 1.28000000e+02 0.00000000e+00 8.33333333e-02]
[ 1.28000000e+02 0.00000000e+00 4.16666667e-02]]
All the values in the array get converted to floats with exponent notation. How can I avoid this?