def array_to_pm(array, fs=DEFAULT_FS, velocity=DEFAULT_VELOCITY, pitch_range=DEFAULT_PITCH_RANGE): pm = PrettyMIDI() inst = Instrument(1) pm.instruments.append(inst) last_notes = {} last_state = np.zeros(array.shape[1]) > 0.5 for i, step in enumerate(array): now = i / fs step = step > 0.5 changed = step != last_state for pitch in np.where(changed)[0]: if step[pitch]: last_notes[pitch] = Note(velocity, pitch + pitch_range.start, now, None) inst.notes.append(last_notes[pitch]) else: last_notes[pitch].end = now del last_notes[pitch] last_state = step now = (i + 1) / fs for note in last_notes.values(): note.end = now return pm
def to_note_seq(self): time = 0 notes = [] velocity = DEFAULT_VELOCITY velocity_bins = EventSeq.get_velocity_bins() last_notes = {} for event in self.events: if event.type == 'note_on': pitch = event.value + EventSeq.pitch_range.start note = Note(velocity, pitch, time, None) notes.append(note) last_notes[pitch] = note elif event.type == 'note_off': pitch = event.value + EventSeq.pitch_range.start if pitch in last_notes: note = last_notes[pitch] note.end = max(time, note.start + MIN_NOTE_LENGTH) del last_notes[pitch] elif event.type == 'velocity': index = min(event.value, velocity_bins.size - 1) velocity = velocity_bins[index] # velocity = velocity_bins[24] #100 elif event.type == 'time_shift': time += EventSeq.time_shift_bins[event.value] for note in notes: if note.end is None: note.end = note.start + DEFAULT_NOTE_LENGTH note.velocity = int(note.velocity) return NoteSeq(notes)
def conv2note_seq(self): time = 0 notes = [] velocity = STATE_VELOCITY velocity_bins = Event_Seqce.getting_veloc_basket() last_notes = {} for event in self.events: if event.type == 'note_on': pitch = event.value + Event_Seqce.pitch_range.start note = Note(velocity, pitch, time, None) notes.append(note) last_notes[pitch] = note elif event.type == 'note_off': pitch = event.value + Event_Seqce.pitch_range.start if pitch in last_notes: note = last_notes[pitch] note.end = max(time, note.start + MIN_NOTE_LENGTH) del last_notes[pitch] elif event.type == 'velocity': index = min(event.value, velocity_bins.size - 1) velocity = velocity_bins[index] elif event.type == 'time_shift': time += Event_Seqce.time_shift_bins[event.value] for note in notes: if note.end is None: note.end = note.start + STATE_NOTE_LENGTH note.velocity = int(note.velocity) return Note_Seqce(notes)