disable synth if synthio unavailable
s-ol
8 days ago
0 | 0 | from __future__ import annotations |
1 | 1 | |
2 | 2 | import displayio |
3 | import audiomixer | |
4 | import synthio | |
5 | 3 | from adafruit_ticks import ticks_ms |
6 | 4 | from adafruit_midi.system_exclusive import SystemExclusive |
7 | 5 | from adafruit_midi.note_on import NoteOn |
13 | 11 | dev_mode = storage.getmount("/").readonly |
14 | 12 | except: |
15 | 13 | dev_mode = False |
14 | ||
15 | try: | |
16 | import audiomixer | |
17 | import synthio | |
18 | except: | |
19 | synthio = None | |
16 | 20 | |
17 | 21 | from .core import Mode, Note |
18 | 22 | from .matrix import Matrix |
74 | 78 | self.i2c = None |
75 | 79 | |
76 | 80 | self.audio_out = board.create_audio_out(self) |
77 | self.mixer = audiomixer.Mixer( | |
78 | sample_rate=41100, buffer_size=5120, channel_count=1 | |
79 | ) | |
80 | ||
81 | self.synth = synthio.Synthesizer(sample_rate=41100) | |
82 | self.filter = self.synth.low_pass_filter(2000, 0.8) | |
81 | ||
82 | if synthio: | |
83 | self.synth = synthio.Synthesizer(sample_rate=41100) | |
84 | self.mixer = audiomixer.Mixer( | |
85 | sample_rate=41100, buffer_size=5120, channel_count=1 | |
86 | ) | |
87 | self.filter = self.synth.low_pass_filter(2000, 0.8) | |
88 | else: | |
89 | self.synth = None | |
83 | 90 | |
84 | 91 | self.settings = Settings( |
85 | 92 | { |
297 | 304 | self.modes["base_shift"].update_display() |
298 | 305 | |
299 | 306 | def on_synth_vol(self, v, last): |
307 | if not self.synth or not self.audio_out: | |
308 | return | |
309 | ||
300 | 310 | if v == 0: |
301 | 311 | self.audio_out.stop() |
302 | 312 | else: |
305 | 315 | self.mixer.voice[0].level = v * v / 10000 |
306 | 316 | |
307 | 317 | def on_synth_adsr(self, v, last): |
318 | if not self.synth: | |
319 | return | |
320 | ||
308 | 321 | a = self.settings.get("synth_a").value |
309 | 322 | d = self.settings.get("synth_d").value |
310 | 323 | s = self.settings.get("synth_s").value |
20 | 20 | import rtmidi |
21 | 21 | |
22 | 22 | rtmidi_api = rtmidi.API_UNSPECIFIED |
23 | if rtmidi.API_UNIX_JACK in rtmidi.get_compiled_api(): | |
24 | rtmidi_api = rtmidi.API_UNIX_JACK | |
25 | 23 | except: |
26 | 24 | rtmidi = None |
27 | 25 | |
141 | 139 | self.board = board |
142 | 140 | self.brightness = 1 |
143 | 141 | self.pixels = [0] * 52 |
142 | self.put_coro = None | |
144 | 143 | |
145 | 144 | def __setitem__(self, key, value): |
146 | 145 | self.pixels[key] = value |
162 | 161 | return colors |
163 | 162 | |
164 | 163 | def show(self): |
165 | return self.board.ws_queue.put( | |
164 | self.put_coro = self.board.ws_queue.put( | |
166 | 165 | { |
167 | 166 | "type": "pixels", |
168 | 167 | "colors": self.hex_colors(), |
169 | 168 | } |
170 | 169 | ) |
170 | ||
171 | async def wait(self): | |
172 | await self.put_coro | |
171 | 173 | |
172 | 174 | return DummyNeopixels(board) |
173 | 175 | |
380 | 382 | loop.create_task(self.run_main()) |
381 | 383 | loop.run_forever() |
382 | 384 | |
385 | def tick(self): | |
386 | ticks = ticks_ms() | |
387 | self.mode.tick(ticks) | |
388 | ||
389 | for i, pressed in self.matrix.scan_for_changes(): | |
390 | if self.board.sysex_key_sync: | |
391 | msg = SystemExclusive(b"\0s-", [i, pressed]) | |
392 | self.broadcast(msg) | |
393 | mode = self.sticky_modes.pop(i, self.mode) | |
394 | should_stick = mode.key_event(i, pressed) | |
395 | ||
396 | if should_stick: | |
397 | self.sticky_modes[i] = mode | |
398 | ||
399 | if self.i2c: | |
400 | self.i2c.tick() | |
401 | ||
402 | msg = self.midi_usb.receive() | |
403 | while msg: | |
404 | if isinstance(msg, NoteOn): | |
405 | note = Note(self, msg.note) | |
406 | self.modes["base"].extra_notes[note.pitch] = note | |
407 | elif isinstance(msg, NoteOff) and self.modes["base"].extra_notes: | |
408 | del self.modes["base"].extra_notes[msg.note] | |
409 | ||
410 | msg = self.midi_usb.receive() | |
411 | ||
412 | self.mode.update_pixels(self.pixels) | |
413 | ||
414 | self.pixels.show() | |
415 | self.display.refresh() | |
416 | ||
383 | 417 | async def run_main(self): |
384 | 418 | while True: |
385 | ticks = ticks_ms() | |
386 | self.mode.tick(ticks) | |
387 | ||
388 | for i, pressed in self.matrix.scan_for_changes(): | |
389 | mode = self.sticky_modes.pop(i, self.mode) | |
390 | should_stick = mode.key_event(i, pressed) | |
391 | ||
392 | if should_stick: | |
393 | self.sticky_modes[i] = mode | |
394 | ||
395 | if self.i2c: | |
396 | self.i2c.tick() | |
397 | ||
398 | self.mode.update_pixels(self.pixels) | |
399 | ||
400 | await self.pixels.show() | |
401 | self.display.refresh() | |
402 | ||
419 | self.tick() | |
420 | await self.pixels.wait() | |
403 | 421 | await asyncio.sleep(1 / 120) |
404 | 422 | |
405 | 423 | def run_http(self): |
0 | 0 | from __future__ import annotations |
1 | 1 | |
2 | 2 | import displayio |
3 | import synthio | |
4 | 3 | from micropython import const |
5 | 4 | from colorsys import hsv_to_rgb |
6 | 5 | from adafruit_ticks import ticks_ms, ticks_diff |
7 | 6 | from adafruit_midi.note_on import NoteOn |
8 | 7 | from adafruit_midi.note_off import NoteOff |
8 | ||
9 | try: | |
10 | import synthio | |
11 | except: | |
12 | synthio = None | |
9 | 13 | |
10 | 14 | |
11 | 15 | def clamp_norm(v): |
27 | 31 | self.end_tick = None |
28 | 32 | self.expiry = 1.0 |
29 | 33 | |
30 | self.note = synthio.Note( | |
31 | synthio.midi_to_hz(self.pitch), filter=self.keyboard.filter | |
32 | ) | |
34 | if self.keyboard.synth: | |
35 | self.note = synthio.Note( | |
36 | synthio.midi_to_hz(self.pitch), filter=self.keyboard.filter | |
37 | ) | |
33 | 38 | |
34 | 39 | def on(self, local=True): |
35 | 40 | self.base.notes[self.pitch] = self |
36 | 41 | self.base.notes_dirty = True |
37 | 42 | |
38 | self.keyboard.synth.press(self.note) | |
43 | if self.keyboard.synth: | |
44 | self.keyboard.synth.press(self.note) | |
39 | 45 | |
40 | 46 | msg = NoteOn(self.pitch, self.keyboard.velocity) |
41 | 47 | self.keyboard.broadcast(msg) |
51 | 57 | del self.base.notes[self.pitch] |
52 | 58 | self.base.notes_dirty = True |
53 | 59 | |
54 | self.keyboard.synth.release(self.note) | |
60 | if self.keyboard.synth: | |
61 | self.keyboard.synth.release(self.note) | |
55 | 62 | |
56 | 63 | msg = NoteOff(self.pitch, self.keyboard.velocity) |
57 | 64 | self.keyboard.broadcast(msg) |