diff options
| author | s-ol <s+removethis@s-ol.nu> | 2023-09-22 22:45:16 +0000 |
|---|---|---|
| committer | s-ol <s+removethis@s-ol.nu> | 2023-09-24 18:08:27 +0000 |
| commit | 3d5a8a4a380c2da0b2b86c0261c0b5de4a77602d (patch) | |
| tree | 987751befde0bc39d273730a0ab487d21cb11d39 | |
| parent | add onboard synthio audio (diff) | |
| download | firmware-3d5a8a4a380c2da0b2b86c0261c0b5de4a77602d.tar.gz firmware-3d5a8a4a380c2da0b2b86c0261c0b5de4a77602d.zip | |
disable synth if synthio unavailable
| -rw-r--r-- | hex33board/__init__.py | 27 | ||||
| -rw-r--r-- | hex33board/boards/simulator.py | 52 | ||||
| -rw-r--r-- | hex33board/core.py | 19 |
3 files changed, 68 insertions, 30 deletions
diff --git a/hex33board/__init__.py b/hex33board/__init__.py index 4d89d88..458a12c 100644 --- a/hex33board/__init__.py +++ b/hex33board/__init__.py @@ -1,8 +1,6 @@ from __future__ import annotations import displayio -import audiomixer -import synthio from adafruit_ticks import ticks_ms from adafruit_midi.system_exclusive import SystemExclusive from adafruit_midi.note_on import NoteOn @@ -15,6 +13,12 @@ try: except: dev_mode = False +try: + import audiomixer + import synthio +except: + synthio = None + from .core import Mode, Note from .matrix import Matrix from .scale import Scale @@ -75,12 +79,15 @@ class Keyboard: self.i2c = None self.audio_out = board.create_audio_out(self) - self.mixer = audiomixer.Mixer( - sample_rate=41100, buffer_size=5120, channel_count=1 - ) - self.synth = synthio.Synthesizer(sample_rate=41100) - self.filter = self.synth.low_pass_filter(2000, 0.8) + if synthio: + self.synth = synthio.Synthesizer(sample_rate=41100) + self.mixer = audiomixer.Mixer( + sample_rate=41100, buffer_size=5120, channel_count=1 + ) + self.filter = self.synth.low_pass_filter(2000, 0.8) + else: + self.synth = None self.settings = Settings( { @@ -298,6 +305,9 @@ class Keyboard: self.modes["base_shift"].update_display() def on_synth_vol(self, v, last): + if not self.synth or not self.audio_out: + return + if v == 0: self.audio_out.stop() else: @@ -306,6 +316,9 @@ class Keyboard: self.mixer.voice[0].level = v * v / 10000 def on_synth_adsr(self, v, last): + if not self.synth: + return + a = self.settings.get("synth_a").value d = self.settings.get("synth_d").value s = self.settings.get("synth_s").value diff --git a/hex33board/boards/simulator.py b/hex33board/boards/simulator.py index 08d1e35..d3463eb 100644 --- a/hex33board/boards/simulator.py +++ b/hex33board/boards/simulator.py @@ -21,8 +21,6 @@ try: import rtmidi rtmidi_api = rtmidi.API_UNSPECIFIED - if rtmidi.API_UNIX_JACK in rtmidi.get_compiled_api(): - rtmidi_api = rtmidi.API_UNIX_JACK except: rtmidi = None @@ -142,6 +140,7 @@ def create_pixels(board, **kwargs): self.board = board self.brightness = 1 self.pixels = [0] * 52 + self.put_coro = None def __setitem__(self, key, value): self.pixels[key] = value @@ -163,13 +162,16 @@ def create_pixels(board, **kwargs): return colors def show(self): - return self.board.ws_queue.put( + self.put_coro = self.board.ws_queue.put( { "type": "pixels", "colors": self.hex_colors(), } ) + async def wait(self): + await self.put_coro + return DummyNeopixels(board) @@ -381,26 +383,42 @@ class Keyboard(BaseKeyboard): loop.create_task(self.run_main()) loop.run_forever() - async def run_main(self): - while True: - ticks = ticks_ms() - self.mode.tick(ticks) + def tick(self): + ticks = ticks_ms() + self.mode.tick(ticks) + + for i, pressed in self.matrix.scan_for_changes(): + if self.board.sysex_key_sync: + msg = SystemExclusive(b"\0s-", [i, pressed]) + self.broadcast(msg) + mode = self.sticky_modes.pop(i, self.mode) + should_stick = mode.key_event(i, pressed) - for i, pressed in self.matrix.scan_for_changes(): - mode = self.sticky_modes.pop(i, self.mode) - should_stick = mode.key_event(i, pressed) + if should_stick: + self.sticky_modes[i] = mode - if should_stick: - self.sticky_modes[i] = mode + if self.i2c: + self.i2c.tick() - if self.i2c: - self.i2c.tick() + msg = self.midi_usb.receive() + while msg: + if isinstance(msg, NoteOn): + note = Note(self, msg.note) + self.modes["base"].extra_notes[note.pitch] = note + elif isinstance(msg, NoteOff) and self.modes["base"].extra_notes: + del self.modes["base"].extra_notes[msg.note] - self.mode.update_pixels(self.pixels) + msg = self.midi_usb.receive() - await self.pixels.show() - self.display.refresh() + self.mode.update_pixels(self.pixels) + self.pixels.show() + self.display.refresh() + + async def run_main(self): + while True: + self.tick() + await self.pixels.wait() await asyncio.sleep(1 / 120) def run_http(self): diff --git a/hex33board/core.py b/hex33board/core.py index 23c54ce..b605427 100644 --- a/hex33board/core.py +++ b/hex33board/core.py @@ -1,13 +1,17 @@ from __future__ import annotations import displayio -import synthio from micropython import const from colorsys import hsv_to_rgb from adafruit_ticks import ticks_ms, ticks_diff from adafruit_midi.note_on import NoteOn from adafruit_midi.note_off import NoteOff +try: + import synthio +except: + synthio = None + def clamp_norm(v): return max(0, min(v, 1)) @@ -28,15 +32,17 @@ class Note: self.end_tick = None self.expiry = 1.0 - self.note = synthio.Note( - synthio.midi_to_hz(self.pitch), filter=self.keyboard.filter - ) + if self.keyboard.synth: + self.note = synthio.Note( + synthio.midi_to_hz(self.pitch), filter=self.keyboard.filter + ) def on(self, local=True): self.base.notes[self.pitch] = self self.base.notes_dirty = True - self.keyboard.synth.press(self.note) + if self.keyboard.synth: + self.keyboard.synth.press(self.note) msg = NoteOn(self.pitch, self.keyboard.velocity) self.keyboard.broadcast(msg) @@ -52,7 +58,8 @@ class Note: del self.base.notes[self.pitch] self.base.notes_dirty = True - self.keyboard.synth.release(self.note) + if self.keyboard.synth: + self.keyboard.synth.release(self.note) msg = NoteOff(self.pitch, self.keyboard.velocity) self.keyboard.broadcast(msg) |
