Skip to content

Commit

Permalink
[WORKING] Small output refactor + output for FORCE ON
Browse files Browse the repository at this point in the history
  • Loading branch information
16pierre committed Apr 5, 2020
1 parent 14bc76e commit f2af2ce
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 24 deletions.
3 changes: 2 additions & 1 deletion main.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@
input_bpm = MidiInputBpm(timing, generic_midi_input)
midi_light_controller = MidiLightWriterController(
priority_light_writer_factory.high(),
generic_midi_input
generic_midi_input,
midi_binding
)

timing.start_ticking()
Expand Down
15 changes: 14 additions & 1 deletion midi/midi_input.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from midi.midi_bindings import MidiBindings
from observer import Observed, Observer
from datetime import datetime
from midi.midi_output import MidiOutputGeneric

class MidiInputSteps:

Expand Down Expand Up @@ -93,15 +94,27 @@ def _change_bpm(self):


class MidiLightWriterController(Observer):
def __init__(self, light_writer, generic_listener):
def __init__(self, light_writer, generic_listener, bindings):
super().__init__()
self.light_writer = light_writer
self.generic_output = MidiOutputGeneric(bindings)
self.bindings = bindings
generic_listener.register_observer(self, MidiGenericInputListener.EVENT_NOTE_OFF)
generic_listener.register_observer(self, MidiGenericInputListener.EVENT_NOTE_ON)

self.generic_output.green(
bindings.generic_midi[MidiBindings.BUTTON_FORCE_ON],
blink=True)

def notify(self, source, event_type, value = None):
if value == MidiBindings.BUTTON_FORCE_ON:
if event_type == MidiGenericInputListener.EVENT_NOTE_OFF:
self.light_writer.neutral()
self.generic_output.green(
self.bindings.generic_midi[MidiBindings.BUTTON_FORCE_ON],
blink=True)
if event_type == MidiGenericInputListener.EVENT_NOTE_ON:
self.light_writer.on()
self.generic_output.green(
self.bindings.generic_midi[MidiBindings.BUTTON_FORCE_ON],
blink=False)
46 changes: 24 additions & 22 deletions midi/midi_output.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,33 +11,35 @@
COLOR_YELLOW = 5
# Note: +1 => blink

class MidiOutputGeneric:
def __init__(self, binding):
self.port = mido.open_output(binding.midi_port_name)

def red(port, note):
msg = mido.Message('note_on', note=note, velocity=COLOR_RED)
port.send(msg)


def green(port, note):
msg = mido.Message('note_on', note=note, velocity=COLOR_GREEN)
port.send(msg)
def _send_velocity(self, note, velocity, blink):
if blink:
velocity = velocity + 1
msg = mido.Message('note_on', note=note, velocity=velocity)
self.port.send(msg)

def red(self, note, blink=False):
self._send_velocity(note, COLOR_RED, blink)

def black(port, note):
msg = mido.Message('note_on', note=note, velocity=COLOR_BLACK)
port.send(msg)
def green(self, note, blink=False):
self._send_velocity(note, COLOR_GREEN, blink)

def black(self, note):
self._send_velocity(note, COLOR_BLACK, False)

def yellow(port, note):
msg = mido.Message('note_on', note=note, velocity=COLOR_YELLOW)
port.send(msg)
def yellow(self, note, blink=False):
self._send_velocity(note, COLOR_YELLOW, blink)


class MidiOutputBpm:

def __init__(self, timing, binding):
self.binding = binding
self.timing = timing
self.port = mido.open_output(binding.midi_port_name)
self.generic_output = MidiOutputGeneric(binding)

def start_bpm_thread(self):
t = threading.Thread(target=self._tick_bpm, daemon=True, args=()).start()
Expand All @@ -46,9 +48,9 @@ def _tick_bpm(self):
while(True):
# TODO: Replace this with a StepListener
bpm = self.timing.get_bpm()
green(self.port, self.binding.generic_midi[MidiBindings.BUTTON_BPM])
self.generic_output.green(self.binding.generic_midi[MidiBindings.BUTTON_BPM])
time.sleep(60.0 / bpm / 4.0)
black(self.port, self.binding.generic_midi[MidiBindings.BUTTON_BPM])
self.generic_output.black(self.binding.generic_midi[MidiBindings.BUTTON_BPM])
time.sleep(60.0 / bpm / 4.0 * 3.0)


Expand All @@ -58,11 +60,11 @@ def __init__(self, timing, binding):
super().__init__()
self.binding = binding
self.timing = timing
self.port = mido.open_output(binding.midi_port_name)
self.generic_output = MidiOutputGeneric(binding)
self.last_step = 0

for note in self.binding.notes_for_time:
black(self.port, note)
self.generic_output.black(note)

timing.register_observer(self, Timing.EVENT_TYPE_STEP_CHANGED)

Expand All @@ -73,10 +75,10 @@ def _refresh_output(self):
current_step = self.timing.get_current_step()
for step in range(len(self.binding.notes_for_time)):
if step == current_step:
yellow(self.port, self.binding.notes_for_time[step])
self.generic_output.yellow(self.binding.notes_for_time[step])
else:
if self.timing.get_step_status(step):
green(self.port, self.binding.notes_for_time[step])
self.generic_output.green(self.binding.notes_for_time[step])
else:
black(self.port, self.binding.notes_for_time[step])
self.generic_output.black(self.binding.notes_for_time[step])

0 comments on commit f2af2ce

Please sign in to comment.