def midiToMatrix(midi_file, steps_per_quarter): """ Args: midi_file: Ruta donde se encuentra el archivo midi. steps_per_quarter: Division del primer tiempo en 4 semicorcheas, 8 en fusas y 16 en semifusas. Returns: aa: Matriz binaria, jump: Duracion en tiempo de cada fila, steps_per_bar: Numero de filas en el primer tiempo, steps_per_quarter: Numero de filas del primer compas. """ print(midi_file) sequence = mm.midi_file_to_sequence_proto(midi_file) quantized_sequence = sequences_lib.quantize_note_sequence( sequence, steps_per_quarter=steps_per_quarter) drum_song = mm.DrumTrack() drum_song.from_quantized_sequence(quantized_sequence, gap_bars=50) steps_per_bar = drum_song.steps_per_bar steps_per_quarter = drum_song.steps_per_quarter print('steps_per_bar: ', drum_song.steps_per_bar) print('steps_per_quarter: ', drum_song.steps_per_quarter) drum_song = drum_song.to_sequence() print("total_time", drum_song.total_time) count = 0 fila = 0 jump = drum_song.notes[0].end_time - drum_song.notes[0].start_time print("step_duration", jump) active = collections.defaultdict(list) while count + jump <= drum_song.total_time: active['fila'].append(fila) active['start_time'].append(count) active['end_time'].append(count + jump) count += jump fila += 1 df_1 = pd.DataFrame(active) #print(df_1) aa = np.zeros((fila, PITCH_LIMIT)) print() print("WaitMidiToMatrix...........:(") for note in drum_song.notes: for indice_fila, fila in df_1.iterrows(): if fila.start_time == note.start_time and note.end_time == fila.end_time: aa[int(fila.fila), note.pitch] = 1 print("Done.......................:)") print() return aa, jump, steps_per_bar, steps_per_quarter
def load_midi(midi_path): """Load midi as a notesequence.""" midi_path = util.expand_path(midi_path) ns = mm.midi_file_to_sequence_proto(midi_path) pitches = np.array([n.pitch for n in ns.notes]) start_times = np.array([n.start_time for n in ns.notes]) end_times = np.array([n.end_time for n in ns.notes]) notes = {'pitches': pitches, 'start_times': start_times, 'end_times': end_times} # print(ns) # print(notes) return ns, notes
def load_midi(midi_path, min_pitch=36, max_pitch=84): """Load midi as a notesequence.""" midi_path = util.expand_path(midi_path) ns = mm.midi_file_to_sequence_proto(midi_path) pitches = np.array([n.pitch for n in ns.notes]) velocities = np.array([n.velocity for n in ns.notes]) start_times = np.array([n.start_time for n in ns.notes]) end_times = np.array([n.end_time for n in ns.notes]) valid = np.logical_and(pitches >= min_pitch, pitches <= max_pitch) notes = {'pitches': pitches[valid], 'velocities': velocities[valid], 'start_times': start_times[valid], 'end_times': end_times[valid]} return ns, notes
def make_notes_sequence(pitches, start_times, durations, qpm): TEMP_MIDI = "temp.mid" make_midi(pitches, start_times, durations, qpm, TEMP_MIDI) return mm.midi_file_to_sequence_proto(TEMP_MIDI)