def test_ramp_notes(): notes = [ Note(start=0, duration=2, pitch=80, velocity=80), Note(start=2, duration=2, pitch=100, velocity=100), ] (clip, res) = create_clip_with_notes(notes) def check_notes(clip): assert len(clip._prev_notes) == clip.RAMPING_STEPS + 1 assert clip._prev_notes[0].start == 0 assert clip._prev_notes[0].duration == 2 assert clip._prev_notes[3].velocity != clip._prev_notes[-1].velocity for i, note in enumerate(clip._prev_notes[2:]): assert clip._prev_notes[i + 1].velocity >= note.velocity clip._map_notes(notes) check_notes(clip) # change note velocity notes = copy(clip._prev_notes) notes[1] = copy(notes[1]) notes[1].velocity = 90 clip._map_notes(notes) check_notes(clip) assert clip._prev_notes[2].velocity <= clip._prev_notes[1].velocity
def _consolidate_notes(self, notes): # type: (List[Note]) -> List[Note] # fill with min notes if notes[0].start != self.loop_start: notes = [ Note(start=0, duration=notes[0].start - self.loop_start, pitch=0, velocity=0) ] + notes for i, next_note in enumerate(notes[1:] + [Note(start=self.length)]): current_note = notes[i] if next_note.start - current_note.end > 0: # current_note.duration = next_note.start - current_note.start notes.append( Note(start=current_note.end, duration=next_note.start - current_note.start, pitch=0, velocity=0)) # merge same velocity notes current_note = notes[0] yield current_note for next_note in notes[1:]: if next_note.velocity == current_note.velocity: current_note.duration += next_note.duration else: current_note = next_note yield current_note
def test_add_note(): notes = [ Note(start=0, duration=1, pitch=0, velocity=0), Note(start=1, duration=3, pitch=100, velocity=100), ] (clip, res) = create_clip_with_notes(notes) clip._map_notes(notes) notes = copy(clip._prev_notes) # adding a note on at 1.1 notes.append(Note(start=1, duration=0.25, pitch=50, velocity=90)) notes.sort(key=lambda x: x.start) clip._map_notes(notes) assert_note(clip._prev_notes[1], { "start": 1, "duration": 0.25, "pitch": 50, "velocity": 50 }) assert_note(clip._prev_notes[2], { "start": 1.25, "pitch": 100, "velocity": 100 }) # adding another note at 1.2 notes.append(Note(start=1.25, duration=0.25, pitch=95, velocity=50)) clip._map_notes(notes) assert_note(clip._prev_notes[2], { "start": 1.25, "pitch": 95, "velocity": 95 })
def test_consolidate_notes_3(): notes = [ Note(start=0, duration=2, pitch=0, velocity=0), Note(start=2, duration=2, pitch=127, velocity=127), ] (clip, res) = create_clip_with_notes(notes, notes) notes.append(Note(start=1, duration=0.25, pitch=80, velocity=80)) clip._map_notes(notes)
def test_clean_ramp_notes(): notes = [ Note(start=0, duration=2, pitch=100, velocity=100), Note(start=2, duration=2, pitch=80, velocity=80), ] base_notes = copy(notes) (clip, res) = create_clip_with_notes(notes) assert list(clip._clean_ramp_notes(list( chain(*clip._ramp_notes(notes))))) == base_notes
def test_consolidate_notes_2(): notes = [ Note(start=0, duration=4, pitch=127, velocity=127), ] (clip, res) = create_clip_with_notes(notes, notes) notes.append(Note(start=1, duration=1, pitch=126, velocity=126)) clip._map_notes(notes) notes = copy(clip._prev_notes) notes.append(Note(start=0, duration=1, pitch=126, velocity=126)) clip._map_notes(notes)
def test_consolidate_notes(): notes = [ Note(start=0, duration=2, pitch=100, velocity=100), Note(start=2, duration=2, pitch=100, velocity=100), ] (clip, res) = create_clip_with_notes(notes) clip._map_notes(notes) assert len(clip._prev_notes) == 1 assert clip._prev_notes[0].start == 0 assert clip._prev_notes[0].duration == clip.length
def test_clean_duplicate_notes(): notes = [ Note(start=0, duration=2, pitch=80, velocity=80), Note(start=0, duration=4, pitch=100, velocity=100), ] (clip, res) = create_clip_with_notes(notes) notes = list(clip._clean_duplicate_notes(notes)) assert len(notes) == 1 assert notes[0].duration == 4
def _fill_equal_notes(self, clip, velocities): duration = clip.length / len(velocities) return [ Note(pitch=vel, velocity=vel, start=i * duration, duration=duration, clip=clip) for i, vel in enumerate(velocities) ]
def test_modify_note_velocity(): prev_notes = [ Note(start=0, duration=2, pitch=80, velocity=80), Note(start=2, duration=2, pitch=100, velocity=100), ] def check_res(clip, res): assert len(res["set_notes"]) == 1 assert len(clip._prev_notes) == clip.RAMPING_STEPS + 1 assert res["set_notes"][0].pitch == 70 assert clip._prev_notes[0].pitch == 70 assert res["set_notes"][0].velocity == 70 assert clip._prev_notes[0].velocity == 70 notes = [copy(note) for note in prev_notes] notes[0].velocity = 70 (clip, res) = create_clip_with_notes(notes, prev_notes) clip._map_notes(notes) check_res(clip, res)
def _clean_ramp_notes(self, notes): # type: (List[Note]) -> List[Note] notes = list( filter(lambda n: n.is_quantized, notes) ) # type: (List[Note]) # keep only base notes without ramping for i, next_note in enumerate(notes[1:] + [Note(start=self.length)]): if not notes[i].is_end_quantized: notes[i].duration = next_note.start - notes[i].start return notes
def get_notes(self, startTime=0, timeRange=0, startPitch=0, pitchRange=128): # type: (Clip, int, float, float, int) -> List[Note] notes = [ Note(*note, clip=self) for note in self._clip.get_notes( startTime, startPitch, timeRange or self.length, pitchRange) ] notes.sort(key=lambda x: x.start) return notes
def test_insert_min_note(): notes = [ Note(start=2, duration=2, pitch=100, velocity=100), ] (clip, res) = create_clip_with_notes(notes) clip._map_notes(notes) assert_note(clip._prev_notes[0], { "start": 0, "duration": 2, "pitch": 0, "velocity": 0 })
def get_selected_notes(self): # type: (Clip) -> None return [ Note(*note, clip=self) for note in self._clip.get_selected_notes() ]