Here the code and the results:
Code to be copied inside python: (Taken from צור מוזיקה עם RNN | TensorFlow Core )
def midi_to_notes(midi_file: str) -> pd.DataFrame:
pm = pretty_midi.PrettyMIDI(midi_file)
instrument = pm.instruments[0]
notes = collections.defaultdict(list)
# Sort the notes by start time
sorted_notes = sorted(instrument.notes, key=lambda note: note.start)
prev_start = sorted_notes[0].start
for note in sorted_notes:
start = note.start
end = note.end
notes['pitch'].append(note.pitch)
notes['start'].append(start)
notes['end'].append(end)
notes['step'].append(start - prev_start)
notes['duration'].append(end - start)
prev_start = start
return pd.DataFrame({name: np.array(value) for name, value in notes.items()})
Errors in python-konsole:
>>> def midi_to_notes(midi_file: str) -> pd.DataFrame:
... pm = pretty_midi.PrettyMIDI(midi_file)
... instrument = pm.instruments[0]
... notes = collections.defaultdict(list)
...
>>> # Sort the notes by start time
>>> sorted_notes = sorted(instrument.notes, key=lambda note: note.start)
File "<stdin>", line 1
sorted_notes = sorted(instrument.notes, key=lambda note: note.start)
IndentationError: unexpected indent
>>> prev_start = sorted_notes[0].start
File "<stdin>", line 1
prev_start = sorted_notes[0].start
IndentationError: unexpected indent
>>>
>>> for note in sorted_notes:
File "<stdin>", line 1
for note in sorted_notes:
IndentationError: unexpected indent
>>> start = note.start
File "<stdin>", line 1
start = note.start
IndentationError: unexpected indent
>>> end = note.end
File "<stdin>", line 1
end = note.end