def setUp(self): self.seq = Seq() # set the velocity of all steps to 100 for i in range(16): self.seq.select_step(i) self.seq.set_velocity(100) for i in range(16): self.seq.deselect_step(i) # now go through and give each a different note for i in range(16): self.seq.select_step(i) self.seq.set_note(60 + i) self.seq.deselect_step(i)
def __init__(self): # TODO: get rid of current_step attribute in favor of querying seq self._manta = Manta() self._midi_source = MIDISource("MantaSeq") self._seq = Seq() self._manta.set_led_enable(PAD_AND_BUTTON, True) self.step_duration = 0.125 # the first step should get executed on the first process() call self.next_step_timestamp = time.time() self.current_step = 0 self.note_offs = {} self.running = False self.start_stop_button = 0 self.shift_button = 1 self._state = MantaSeqIdleState(self) self.pad_leds = [MantaSeqPadLED(i, self._manta) for i in range(48)] self._global_cc1 = 0 self._global_cc2 = 0 # store the notenum and value of the last selected note, which will # be assigned to new steps self._selected_note = None self._selected_cc1 = None self._selected_cc2 = None
class TestStepping(unittest.TestCase): def setUp(self): self.seq = Seq() # set the velocity of all steps to 100 for i in range(16): self.seq.select_step(i) self.seq.set_velocity(100) for i in range(16): self.seq.deselect_step(i) # now go through and give each a different note for i in range(16): self.seq.select_step(i) self.seq.set_note(60 + i) self.seq.deselect_step(i) def test_each_step_calls_next_step(self): for i in range(16): step = self.seq.step() self.assertEqual(step.note, 60 + i) def test_steps_wrap(self): for i in range(50): step = self.seq.step() self.assertEqual(step.note, 60 + i % 16) def test_step_count_can_change(self): self.seq.step_count = 7 for i in range(50): step = self.seq.step() self.assertEqual(step.note, 60 + i % 7) def test_step_resets_if_current_step_is_greater_than_step_count(self): self.seq.step_count = 16 for i in range(12): self.seq.step() self.seq.step_count = 10 step = self.seq.step() self.assertEqual(step.note, 60)
class MantaSeq(object): def __init__(self): # TODO: get rid of current_step attribute in favor of querying seq self._manta = Manta() self._midi_source = MIDISource("MantaSeq") self._seq = Seq() self._manta.set_led_enable(PAD_AND_BUTTON, True) self.step_duration = 0.125 # the first step should get executed on the first process() call self.next_step_timestamp = time.time() self.current_step = 0 self.note_offs = {} self.running = False self.start_stop_button = 0 self.shift_button = 1 self._state = MantaSeqIdleState(self) self.pad_leds = [MantaSeqPadLED(i, self._manta) for i in range(48)] self._global_cc1 = 0 self._global_cc2 = 0 # store the notenum and value of the last selected note, which will # be assigned to new steps self._selected_note = None self._selected_cc1 = None self._selected_cc2 = None def cleanup(self): self._manta.set_led_enable(PAD_AND_BUTTON, False) def start(self): self.running = True self.next_step_timestamp = time.time() def stop(self): self.running = False def _get_step_color(self, step_num): if step_num == self.current_step: return RED elif self._seq.steps[step_num].velocity > 0: return AMBER else: return OFF def _send_midi_cc(self, cc_num, value): self._midi_source.send(make_cc(cc_num, value)) def _send_midi_note(self, note_num, velocity): # remove from note_offs dict if present self.note_offs.pop(note_num, None) midi_note = make_note(note_num, velocity) self._midi_source.send(midi_note) def _schedule_note_off(self, note_num, timestamp): self.note_offs[note_num] = timestamp def set_pad_highlight(self, pad_num, highlight): self.pad_leds[pad_num].highlight(highlight) def set_pad_active(self, pad_num, active): self.pad_leds[pad_num].active(active) def set_pad_intensity(self, pad_num, intensity): self.pad_leds[pad_num].intensity(intensity) def process(self): now = time.time() events = self._manta.process() for event in events: if isinstance(event, PadVelocityEvent): self._process_pad_velocity_event(event) if isinstance(event, ButtonVelocityEvent): self._process_button_velocity_event(event) elif isinstance(event, PadValueEvent): self._process_pad_value_event(event) elif isinstance(event, SliderValueEvent): self._process_slider_value_event(event) # send any pending note_offs for note_num, timestamp in self.note_offs.items(): if now >= timestamp: # send_midi_note will take care of removing the note # from the list self._send_midi_note(note_num, 0) self.set_pad_intensity(pad_from_note(note_num), 0) # if it's time for another step, do it if self.running and now >= self.next_step_timestamp: last_step = self.current_step self.current_step = self._seq.current_step_index step_obj = self._seq.step() if step_obj.velocity > 0: self._send_midi_note(step_obj.note, step_obj.velocity) note_off_timestamp = self.next_step_timestamp + (step_obj.duration * self.step_duration) self._schedule_note_off(step_obj.note, note_off_timestamp) self.set_pad_intensity(pad_from_note(step_obj.note), step_obj.velocity) self._send_midi_cc(1, self._combine_cc(self._global_cc1, step_obj.cc1)) self._send_midi_cc(2, self._combine_cc(self._global_cc2, step_obj.cc2)) # update the step LEDs (previous and current) self.set_pad_highlight(last_step, False) self.set_pad_highlight(self.current_step, True) # remember which step we turned on so we can turn it off next time # around self.next_step_timestamp += self.step_duration def _combine_cc(self, glob, step): """ combines the global cc (glob) with the per-step cc (step) """ return int(glob + step / 127.0 * (127 - glob)) # most of the events get deferred to the state, as they're state-dependent def _process_pad_velocity_event(self, event): if row_from_pad(event.pad_num) < 2: if event.velocity > 0: self._state.process_step_press(event.pad_num) else: self._state.process_step_release(event.pad_num) else: self._state.process_note_velocity(event.pad_num, event.velocity) def _process_button_velocity_event(self, event): if event.button_num == self.start_stop_button: if event.velocity > 0: self.stop() if self.running else self.start() elif event.button_num == self.shift_button: if event.velocity > 0: self._state.process_shift_press() else: self._state.process_shift_release() def _process_pad_value_event(self, event): # pad value messages are ignored for step selection pads if event.pad_num > 15: self._state.process_note_value(event.pad_num, event.value) def _process_slider_value_event(self, event): if event.touched: self._state.process_slider_value(event.slider_num, event.value) else: self._state.process_slider_release(event.slider_num)