omnipython-apps 0.1.4.dev8__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- apps/__init__.py +5 -0
- apps/about/__init__.py +257 -0
- apps/about/app.json +1 -0
- apps/about/credits.py +12 -0
- apps/about/facts.py +398 -0
- apps/about/fonts/OFL.txt +93 -0
- apps/about/fonts/latin1_14.bin +0 -0
- apps/about/fonts/latin1_16.bin +0 -0
- apps/about/fonts/latin1_24.bin +0 -0
- apps/about/fonts/latin1_32.bin +0 -0
- apps/about/fonts/latin1_40.bin +0 -0
- apps/backline/__init__.py +58 -0
- apps/backline/app.json +1 -0
- apps/backline/app.py +475 -0
- apps/backline/art.py +717 -0
- apps/backline/ask.py +781 -0
- apps/backline/box.py +502 -0
- apps/backline/cast.py +137 -0
- apps/backline/desk/__init__.py +329 -0
- apps/backline/desk/editor.py +331 -0
- apps/backline/desk/grid.py +154 -0
- apps/backline/desk/look.py +169 -0
- apps/backline/desk/pedals.py +102 -0
- apps/backline/desk/perform.py +157 -0
- apps/backline/desk/prompt.py +210 -0
- apps/backline/desk/strip.py +159 -0
- apps/backline/desk_main.py +226 -0
- apps/backline/engine.py +622 -0
- apps/backline/floor.py +509 -0
- apps/backline/footswitch.py +119 -0
- apps/backline/glyphs.py +82 -0
- apps/backline/kits.py +428 -0
- apps/backline/midipedal.py +362 -0
- apps/backline/page_data.py +413 -0
- apps/backline/parts.py +145 -0
- apps/backline/pedal.py +107 -0
- apps/backline/remote.py +134 -0
- apps/backline/songs/ballad_68.json +1 -0
- apps/backline/songs/bossa.json +1 -0
- apps/backline/songs/country_train.json +1 -0
- apps/backline/songs/funk.json +1 -0
- apps/backline/songs/metal.json +1 -0
- apps/backline/songs/pop.json +1 -0
- apps/backline/songs/rock.json +1 -0
- apps/backline/songs/shuffle.json +1 -0
- apps/battery/__init__.py +101 -0
- apps/battery/app.json +1 -0
- apps/bit_world/act_deck.py +683 -0
- apps/bit_world/act_game.py +762 -0
- apps/bit_world/act_pet.py +1032 -0
- apps/bit_world/app.json +1 -0
- apps/bit_world/bit_output.py +45 -0
- apps/bit_world/bit_palette.py +503 -0
- apps/bit_world/bit_sound.py +431 -0
- apps/bit_world/bit_sprites.py +632 -0
- apps/bit_world/bit_world.py +534 -0
- apps/btreceiver/__init__.py +690 -0
- apps/btreceiver/app.json +1 -0
- apps/btreceiver/chime.py +155 -0
- apps/btreceiver/demo.py +529 -0
- apps/btreceiver/screen.py +633 -0
- apps/camera/__init__.py +316 -0
- apps/camera/app.json +1 -0
- apps/card/__init__.py +113 -0
- apps/card/app.json +1 -0
- apps/cast/__init__.py +631 -0
- apps/cast/app.json +1 -0
- apps/cast/receivers.py +193 -0
- apps/chess/__init__.py +18 -0
- apps/chess/app.json +1 -0
- apps/chess/app.py +922 -0
- apps/chess/art.py +323 -0
- apps/chess/ble.py +411 -0
- apps/chess/engine.py +925 -0
- apps/chess/game.py +327 -0
- apps/chess/net.py +261 -0
- apps/clock/__init__.py +38 -0
- apps/clock/app.json +1 -0
- apps/detect/__init__.py +214 -0
- apps/detect/app.json +1 -0
- apps/fmsynth/__init__.py +41 -0
- apps/fmsynth/algorithms.py +61 -0
- apps/fmsynth/app.json +1 -0
- apps/fmsynth/app.py +355 -0
- apps/fmsynth/editor.py +206 -0
- apps/fmsynth/engine.py +257 -0
- apps/fmsynth/meters.py +262 -0
- apps/fmsynth/midi.py +375 -0
- apps/fmsynth/panel_view.py +1071 -0
- apps/fmsynth/patches/fmsynth-01.syx +0 -0
- apps/fmsynth/settings.py +138 -0
- apps/fmsynth/voice.py +398 -0
- apps/gamepad/__init__.py +685 -0
- apps/gamepad/app.json +1 -0
- apps/gamepad/pad.py +94 -0
- apps/gamepad/watch.py +352 -0
- apps/hidden/__init__.py +72 -0
- apps/hidden/app.json +1 -0
- apps/home/__init__.py +483 -0
- apps/home/app.json +1 -0
- apps/home/main.py +36 -0
- apps/homeassistant/__init__.py +213 -0
- apps/homeassistant/api.py +104 -0
- apps/homeassistant/app.json +1 -0
- apps/huey/__init__.py +287 -0
- apps/huey/acting.py +184 -0
- apps/huey/actor.py +194 -0
- apps/huey/app.json +1 -0
- apps/huey/client.py +125 -0
- apps/huey/servers.py +121 -0
- apps/huey/settings.example.json +8 -0
- apps/huey/settings.py +97 -0
- apps/huey/turn.py +504 -0
- apps/huey/voice.py +217 -0
- apps/info/__init__.py +810 -0
- apps/info/app.json +1 -0
- apps/lora/__init__.py +62 -0
- apps/lora/app.json +1 -0
- apps/manifest.py +71 -0
- apps/medialink_baby_monitor/__init__.py +222 -0
- apps/medialink_baby_monitor/app.json +1 -0
- apps/medialink_doorbell/__init__.py +251 -0
- apps/medialink_doorbell/app.json +1 -0
- apps/medialink_hands_free/__init__.py +217 -0
- apps/medialink_hands_free/app.json +1 -0
- apps/medialink_surveillance_camera/__init__.py +258 -0
- apps/medialink_surveillance_camera/app.json +1 -0
- apps/medialink_video_doorbell/__init__.py +289 -0
- apps/medialink_video_doorbell/app.json +1 -0
- apps/medialink_videophone/__init__.py +273 -0
- apps/medialink_videophone/app.json +1 -0
- apps/medialink_walkie_talkie/__init__.py +228 -0
- apps/medialink_walkie_talkie/app.json +1 -0
- apps/midicontroller/__init__.py +774 -0
- apps/midicontroller/app.json +1 -0
- apps/midicontroller/links.py +421 -0
- apps/midicontroller/surface.py +343 -0
- apps/midiengine/__init__.py +977 -0
- apps/midiengine/app.json +1 -0
- apps/midiengine/banks.py +85 -0
- apps/midiengine/midiin.py +165 -0
- apps/midiengine/player.py +392 -0
- apps/midiengine/umidiparser.py +1413 -0
- apps/mp3/__init__.py +240 -0
- apps/mp3/app.json +1 -0
- apps/perch/__init__.py +119 -0
- apps/perch/app.json +1 -0
- apps/perch/huey.py +481 -0
- apps/perch/huey_props.py +386 -0
- apps/perch/huey_shift.py +42 -0
- apps/perch/huey_views.py +296 -0
- apps/perch/scene_bench.py +680 -0
- apps/perch/scene_coast.py +479 -0
- apps/perch/scene_desk.py +492 -0
- apps/perch/stage.py +1274 -0
- apps/perch/vec.py +366 -0
- apps/repl/__init__.py +325 -0
- apps/repl/app.json +1 -0
- apps/roku/__init__.py +156 -0
- apps/roku/app.json +1 -0
- apps/rokucam/__init__.py +362 -0
- apps/rokucam/app.json +1 -0
- apps/security/__init__.py +261 -0
- apps/security/app.json +1 -0
- apps/settings/__init__.py +221 -0
- apps/settings/app.json +1 -0
- apps/shell/__init__.py +33 -0
- apps/shell/app.json +1 -0
- apps/spotify/__init__.py +669 -0
- apps/spotify/app.json +1 -0
- apps/spotify/screen.py +82 -0
- apps/steps/__init__.py +58 -0
- apps/steps/app.json +1 -0
- apps/stompbox/__init__.py +31 -0
- apps/stompbox/app.json +1 -0
- apps/stompbox/app.py +668 -0
- apps/stompbox/boards.json +20 -0
- apps/stompbox/effects.py +241 -0
- apps/stompbox/engine.py +340 -0
- apps/stompbox/footswitch.py +331 -0
- apps/stompbox/inputs.py +449 -0
- apps/stompbox/meter.py +72 -0
- apps/stompbox/outputs.py +135 -0
- apps/stompbox/pedals.txt +45 -0
- apps/stompbox/presets.py +106 -0
- apps/stompbox/styles.py +135 -0
- apps/stompbox/ui.py +817 -0
- apps/stompbox/wavfile.py +73 -0
- apps/synthstation/__init__.py +20 -0
- apps/synthstation/app.json +1 -0
- apps/synthstation/audio.py +397 -0
- apps/synthstation/catalog.py +50 -0
- apps/synthstation/meters.py +136 -0
- apps/synthstation/midi.py +380 -0
- apps/synthstation/pads.py +177 -0
- apps/synthstation/parts.py +128 -0
- apps/synthstation/station.py +1069 -0
- apps/system/__init__.py +11 -0
- apps/system/app.json +1 -0
- apps/touchcal/__init__.py +324 -0
- apps/touchcal/app.json +1 -0
- apps/touchcal/fit.py +262 -0
- apps/wake/__init__.py +268 -0
- apps/wake/app.json +1 -0
- apps/wake/feed.py +155 -0
- apps/watch/__init__.py +355 -0
- apps/watch/app.json +1 -0
- apps/watch/faces.py +35 -0
- apps/watch/wheel.py +157 -0
- apps/weather/__init__.py +278 -0
- apps/weather/app.json +1 -0
- apps/weather/icons.py +172 -0
- apps/weather/sizing.py +71 -0
- apps/weather/zippad.py +172 -0
- omnipython_apps-0.1.4.dev8.dist-info/METADATA +33 -0
- omnipython_apps-0.1.4.dev8.dist-info/RECORD +218 -0
- omnipython_apps-0.1.4.dev8.dist-info/WHEEL +5 -0
- omnipython_apps-0.1.4.dev8.dist-info/top_level.txt +1 -0
|
@@ -0,0 +1,380 @@
|
|
|
1
|
+
# SPDX-FileCopyrightText: 2026 Brad Barnett
|
|
2
|
+
#
|
|
3
|
+
# SPDX-License-Identifier: MIT
|
|
4
|
+
"""MIDI in: every keyboard the program can reach, read as one stream.
|
|
5
|
+
|
|
6
|
+
On a board with a USB host port that's every MIDI device plugged into it,
|
|
7
|
+
merged by ``midiports.usb_host.all_ports()``; devices plugged in later are
|
|
8
|
+
heard too. A board without one whose ``board_peripherals`` lists a
|
|
9
|
+
``usb_midi`` role becomes a USB MIDI device the computer plays. On Windows
|
|
10
|
+
and Linux it's every MIDI input port the computer has, through
|
|
11
|
+
``midiports.windows`` or ``midiports.linux``, until inputs are chosen for
|
|
12
|
+
``midi.in`` (the connections picker, or `endpointkit.select`): then it's
|
|
13
|
+
those, read through ``midiports.endpoints``, which opened them. A browser
|
|
14
|
+
page is the same: the inputs chosen for ``midi.in`` once there are any (the
|
|
15
|
+
picker lists Web MIDI's), and until then every MIDI input the browser offers,
|
|
16
|
+
through ``midiports.webmidi``.
|
|
17
|
+
|
|
18
|
+
Bluetooth MIDI is one more input, on a board whose firmware has Bluetooth LE:
|
|
19
|
+
`Inputs.set_ble` makes the board a BLE-MIDI instrument, advertised under the
|
|
20
|
+
app's name, that a controller, a phone or a computer connects to, through
|
|
21
|
+
``midiports.ble``. It's off until the player turns it on, so nothing about
|
|
22
|
+
Bluetooth starts or takes memory before then.
|
|
23
|
+
|
|
24
|
+
`Parser` turns the bytes into calls on an object you pass, with no
|
|
25
|
+
allocation per message, so a quiet keyboard costs nothing beside the audio.
|
|
26
|
+
"""
|
|
27
|
+
|
|
28
|
+
import sys
|
|
29
|
+
|
|
30
|
+
#: What ``Inputs.why`` says when a computer has no MIDI input at all.
|
|
31
|
+
NO_INPUT = "no MIDI input on this computer"
|
|
32
|
+
#: The platforms that are a computer, with MIDI ports of their own.
|
|
33
|
+
DESKTOPS = ("win32", "linux", "darwin")
|
|
34
|
+
#: The platforms that are a browser page, whose MIDI ports are Web MIDI's.
|
|
35
|
+
BROWSERS = ("webassembly", "emscripten")
|
|
36
|
+
#: The most reads of one port in one poll.
|
|
37
|
+
READS_PER_POLL = 64
|
|
38
|
+
#: The name a board advertises its Bluetooth MIDI under.
|
|
39
|
+
BLE_NAME = "Synth Station"
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
def ble_available():
|
|
43
|
+
"""True on a board whose firmware has Bluetooth LE, as
|
|
44
|
+
``midiports.ble.available()`` says, without importing the port: that
|
|
45
|
+
waits until the player turns it on. Starts nothing."""
|
|
46
|
+
if sys.implementation.name not in ("micropython", "circuitpython"):
|
|
47
|
+
return False
|
|
48
|
+
if sys.platform in ("webassembly", "emscripten"):
|
|
49
|
+
return False
|
|
50
|
+
for name in ("bluetooth", "_bleio"):
|
|
51
|
+
try:
|
|
52
|
+
__import__(name)
|
|
53
|
+
return True
|
|
54
|
+
except ImportError:
|
|
55
|
+
pass
|
|
56
|
+
return False
|
|
57
|
+
|
|
58
|
+
|
|
59
|
+
def _name(dev):
|
|
60
|
+
"""A USB device's product string, or its vid:pid when it has none."""
|
|
61
|
+
try:
|
|
62
|
+
product = dev.product
|
|
63
|
+
except Exception: # a device that won't answer a string request
|
|
64
|
+
product = None
|
|
65
|
+
return product or "%04x:%04x" % (dev.idVendor, dev.idProduct)
|
|
66
|
+
|
|
67
|
+
|
|
68
|
+
class Inputs:
|
|
69
|
+
"""The open MIDI inputs, read together.
|
|
70
|
+
|
|
71
|
+
``kind`` is ``"usb"``, ``"device"``, ``"desktop"`` (a computer's ports,
|
|
72
|
+
or the ones chosen for ``midi.in`` anywhere), ``"web"`` (every input a
|
|
73
|
+
browser offers, and the page's other tabs) or None when there's nothing to open; ``why`` then says
|
|
74
|
+
why in a sentence. ``names()`` lists the
|
|
75
|
+
devices heard now.
|
|
76
|
+
"""
|
|
77
|
+
|
|
78
|
+
def __init__(self):
|
|
79
|
+
self.kind = None
|
|
80
|
+
self.why = None
|
|
81
|
+
self.ports = []
|
|
82
|
+
self._desktop_names = []
|
|
83
|
+
self._usb = False
|
|
84
|
+
self._buf = bytearray(64)
|
|
85
|
+
#: The Bluetooth MIDI input while it's on, and why it stopped if it did.
|
|
86
|
+
self.ble = None
|
|
87
|
+
self.ble_why = None
|
|
88
|
+
|
|
89
|
+
# ---- opening --------------------------------------------------------------
|
|
90
|
+
|
|
91
|
+
def open(self):
|
|
92
|
+
"""Open whatever this computer or board has. Never raises."""
|
|
93
|
+
# A computer reads its own MIDI ports, even where a usb.core is
|
|
94
|
+
# importable (PyUSB, or a desktop build that carries the package).
|
|
95
|
+
if getattr(sys, "platform", "") in DESKTOPS:
|
|
96
|
+
self._open_desktop()
|
|
97
|
+
return self
|
|
98
|
+
if getattr(sys, "platform", "") in BROWSERS:
|
|
99
|
+
self._open_web()
|
|
100
|
+
return self
|
|
101
|
+
try:
|
|
102
|
+
import usb.core
|
|
103
|
+
|
|
104
|
+
running = usb.core.running # PyUSB on a desktop has no host to start
|
|
105
|
+
except (ImportError, AttributeError):
|
|
106
|
+
running = None
|
|
107
|
+
if running is not None:
|
|
108
|
+
try:
|
|
109
|
+
from midiports import usb_host
|
|
110
|
+
except ImportError:
|
|
111
|
+
self.why = "midiports isn't installed"
|
|
112
|
+
return self
|
|
113
|
+
try:
|
|
114
|
+
# Take the host as it runs: a launcher that started it keeps
|
|
115
|
+
# its speed and its devices. Only a host this app started is
|
|
116
|
+
# its own to stop.
|
|
117
|
+
self._usb = not running()
|
|
118
|
+
port, out = usb_host.all_ports()
|
|
119
|
+
if out is not None:
|
|
120
|
+
out.close()
|
|
121
|
+
except Exception as e:
|
|
122
|
+
self.why = "the USB host didn't start: %s" % (e,)
|
|
123
|
+
return self
|
|
124
|
+
self.ports = [port]
|
|
125
|
+
self.kind = "usb"
|
|
126
|
+
return self
|
|
127
|
+
if self._open_device():
|
|
128
|
+
return self
|
|
129
|
+
self._open_desktop()
|
|
130
|
+
return self
|
|
131
|
+
|
|
132
|
+
def _open_device(self):
|
|
133
|
+
"""Make this board a USB MIDI device, where its board says it may
|
|
134
|
+
(a ``usb_midi`` role): starting one re-enumerates the USB port."""
|
|
135
|
+
try:
|
|
136
|
+
import board_peripherals as bp
|
|
137
|
+
except ImportError:
|
|
138
|
+
return False
|
|
139
|
+
if "usb_midi" not in getattr(bp, "PERIPHERALS", ()):
|
|
140
|
+
return False
|
|
141
|
+
try:
|
|
142
|
+
port, _out = bp.usb_midi()
|
|
143
|
+
except Exception as e:
|
|
144
|
+
self.why = "the USB MIDI device didn't start: %s" % (e,)
|
|
145
|
+
return True
|
|
146
|
+
self.ports = [port]
|
|
147
|
+
self.kind = "device"
|
|
148
|
+
return True
|
|
149
|
+
|
|
150
|
+
def _open_chosen(self):
|
|
151
|
+
"""Read the computer's inputs chosen for ``midi.in`` as one port;
|
|
152
|
+
False when none is chosen, or endpointkit isn't installed."""
|
|
153
|
+
try:
|
|
154
|
+
import endpointkit
|
|
155
|
+
from midiports import endpoints
|
|
156
|
+
except ImportError:
|
|
157
|
+
return False
|
|
158
|
+
try:
|
|
159
|
+
port = endpoints.merged_in()
|
|
160
|
+
except KeyError: # the computer's MIDI ports aren't registered
|
|
161
|
+
return False
|
|
162
|
+
# A browser's port reads the page's Web MIDI ports and the other tabs.
|
|
163
|
+
mine = getattr(port, "providers", (port.provider,))
|
|
164
|
+
chosen = [ep for ep in endpointkit.selected("midi.in") if ep.provider in mine]
|
|
165
|
+
if not chosen:
|
|
166
|
+
return False
|
|
167
|
+
self.ports = [port]
|
|
168
|
+
self._desktop_names = [ep.label for ep in chosen]
|
|
169
|
+
self.kind = "desktop"
|
|
170
|
+
return True
|
|
171
|
+
|
|
172
|
+
def _open_desktop(self):
|
|
173
|
+
if self._open_chosen():
|
|
174
|
+
return
|
|
175
|
+
platform = getattr(sys, "platform", "")
|
|
176
|
+
try:
|
|
177
|
+
import midiports
|
|
178
|
+
|
|
179
|
+
if platform.startswith("win"):
|
|
180
|
+
from midiports import windows as backend
|
|
181
|
+
elif platform.startswith("linux"):
|
|
182
|
+
from midiports import linux as backend
|
|
183
|
+
else:
|
|
184
|
+
self.why = "no MIDI ports on this platform"
|
|
185
|
+
return
|
|
186
|
+
except ImportError:
|
|
187
|
+
self.why = "midiports isn't installed"
|
|
188
|
+
return
|
|
189
|
+
try:
|
|
190
|
+
found = [p for p in backend.available() if p.direction in (midiports.IN, midiports.INOUT)]
|
|
191
|
+
except Exception as e:
|
|
192
|
+
self.why = "couldn't list MIDI ports: %s" % (e,)
|
|
193
|
+
return
|
|
194
|
+
for info in found:
|
|
195
|
+
try:
|
|
196
|
+
midi_in, midi_out = backend.ports(info.name)
|
|
197
|
+
except Exception:
|
|
198
|
+
continue
|
|
199
|
+
if midi_out is not None:
|
|
200
|
+
midi_out.close()
|
|
201
|
+
if midi_in is not None:
|
|
202
|
+
self.ports.append(midi_in)
|
|
203
|
+
self._desktop_names.append(info.name)
|
|
204
|
+
if self.ports:
|
|
205
|
+
self.kind = "desktop"
|
|
206
|
+
else:
|
|
207
|
+
self.why = NO_INPUT
|
|
208
|
+
|
|
209
|
+
def _open_web(self):
|
|
210
|
+
if self._open_chosen():
|
|
211
|
+
return
|
|
212
|
+
try:
|
|
213
|
+
from midiports import webmidi
|
|
214
|
+
except ImportError:
|
|
215
|
+
self.why = "midiports isn't installed"
|
|
216
|
+
return
|
|
217
|
+
self.ports = [webmidi.page_in()]
|
|
218
|
+
try:
|
|
219
|
+
from midiports import tabs
|
|
220
|
+
except ImportError: # a midiports from before the tabs
|
|
221
|
+
tabs = None
|
|
222
|
+
if tabs is not None:
|
|
223
|
+
self.ports.append(tabs.page_in())
|
|
224
|
+
self.kind = "web"
|
|
225
|
+
|
|
226
|
+
def set_ble(self, on, parser=None, name=BLE_NAME):
|
|
227
|
+
"""Turn the Bluetooth MIDI input on or off. Turning it on starts
|
|
228
|
+
advertising as *name*; an error starting it is raised. Turning it
|
|
229
|
+
off hands *parser* a note-off for every note it left sounding."""
|
|
230
|
+
if on and self.ble is None:
|
|
231
|
+
from midiports import ble
|
|
232
|
+
|
|
233
|
+
self.ble_why = None
|
|
234
|
+
try:
|
|
235
|
+
self.ble, _out = ble.ports(name) # the output stays unused: closing it closes the link
|
|
236
|
+
except Exception as e:
|
|
237
|
+
self.ble_why = str(e) or type(e).__name__
|
|
238
|
+
raise
|
|
239
|
+
elif not on and self.ble is not None:
|
|
240
|
+
port = self.ble
|
|
241
|
+
self.ble = None
|
|
242
|
+
port.close()
|
|
243
|
+
if parser is not None:
|
|
244
|
+
buf = self._buf
|
|
245
|
+
try:
|
|
246
|
+
for _ in range(READS_PER_POLL):
|
|
247
|
+
n = port.readinto(buf)
|
|
248
|
+
if not n:
|
|
249
|
+
break
|
|
250
|
+
parser.feed(buf, n)
|
|
251
|
+
except Exception as e: # the link had stopped: said, as in poll
|
|
252
|
+
self.ble_why = str(e) or type(e).__name__
|
|
253
|
+
|
|
254
|
+
def ble_text(self):
|
|
255
|
+
"""What the Bluetooth MIDI input is doing, for the status line, or
|
|
256
|
+
None while it's off."""
|
|
257
|
+
port = self.ble
|
|
258
|
+
if port is None:
|
|
259
|
+
return "Bluetooth MIDI stopped: %s" % (self.ble_why,) if self.ble_why else None
|
|
260
|
+
if port.state == "connected":
|
|
261
|
+
return "Bluetooth"
|
|
262
|
+
return "Bluetooth: waiting as %s" % (port.name,)
|
|
263
|
+
|
|
264
|
+
# ---- reading --------------------------------------------------------------
|
|
265
|
+
|
|
266
|
+
def names(self):
|
|
267
|
+
"""The devices heard now, as a list of strings."""
|
|
268
|
+
if self.kind == "usb":
|
|
269
|
+
found = [_name(d) for d in getattr(self.ports[0], "devices", ())]
|
|
270
|
+
elif self.kind == "web":
|
|
271
|
+
found = [name for port in self.ports for name in port.names]
|
|
272
|
+
elif self.kind == "device":
|
|
273
|
+
found = ["USB MIDI"]
|
|
274
|
+
else:
|
|
275
|
+
found = list(self._desktop_names)
|
|
276
|
+
if self.ble is not None and self.ble.state == "connected":
|
|
277
|
+
found.append("Bluetooth")
|
|
278
|
+
return found
|
|
279
|
+
|
|
280
|
+
def poll(self, parser):
|
|
281
|
+
"""Feed everything waiting on every port to *parser*."""
|
|
282
|
+
buf = self._buf
|
|
283
|
+
ble = self.ble
|
|
284
|
+
if ble is not None:
|
|
285
|
+
try:
|
|
286
|
+
for _ in range(READS_PER_POLL):
|
|
287
|
+
n = ble.readinto(buf)
|
|
288
|
+
if not n:
|
|
289
|
+
break
|
|
290
|
+
parser.feed(buf, n)
|
|
291
|
+
except Exception as e: # the link stopped: said on the status line, not retried
|
|
292
|
+
self.ble = None
|
|
293
|
+
self.ble_why = str(e) or type(e).__name__
|
|
294
|
+
ble.close()
|
|
295
|
+
for port in self.ports:
|
|
296
|
+
any_ = getattr(port, "any", None)
|
|
297
|
+
# Ask first where the port can say: reading an empty USB port
|
|
298
|
+
# still costs a transfer.
|
|
299
|
+
if any_ is not None and not any_():
|
|
300
|
+
continue
|
|
301
|
+
# A merged port hands over one message a read, so read until
|
|
302
|
+
# it's empty, but leave a flood for the next tick.
|
|
303
|
+
for _ in range(READS_PER_POLL):
|
|
304
|
+
n = port.readinto(buf)
|
|
305
|
+
if not n:
|
|
306
|
+
break
|
|
307
|
+
parser.feed(buf, n)
|
|
308
|
+
|
|
309
|
+
def close(self):
|
|
310
|
+
self.set_ble(False)
|
|
311
|
+
ports = self.ports
|
|
312
|
+
self.ports = []
|
|
313
|
+
for port in ports:
|
|
314
|
+
try:
|
|
315
|
+
port.close()
|
|
316
|
+
except Exception:
|
|
317
|
+
pass
|
|
318
|
+
if self._usb:
|
|
319
|
+
self._usb = False
|
|
320
|
+
try:
|
|
321
|
+
import usb.core
|
|
322
|
+
|
|
323
|
+
usb.core.stop()
|
|
324
|
+
except Exception:
|
|
325
|
+
pass
|
|
326
|
+
|
|
327
|
+
|
|
328
|
+
class Parser:
|
|
329
|
+
"""MIDI bytes to calls on *sink*, from every channel.
|
|
330
|
+
|
|
331
|
+
*sink* has ``note_on(note, velocity, channel)``, ``note_off(note, channel)``,
|
|
332
|
+
``control(controller, value, channel)``, ``bend(value, channel)`` (0 to
|
|
333
|
+
16383, centre 8192) and ``program(number, channel)``, with channels
|
|
334
|
+
counted from 0. Running status is followed; SysEx, real-time and system
|
|
335
|
+
common messages are skipped.
|
|
336
|
+
"""
|
|
337
|
+
|
|
338
|
+
def __init__(self, sink):
|
|
339
|
+
self.sink = sink
|
|
340
|
+
self._status = 0
|
|
341
|
+
self._first = -1
|
|
342
|
+
self._sysex = False
|
|
343
|
+
|
|
344
|
+
def feed(self, buf, n):
|
|
345
|
+
sink = self.sink
|
|
346
|
+
for i in range(n):
|
|
347
|
+
b = buf[i]
|
|
348
|
+
if b >= 0xF8: # real time: may land anywhere, even mid-message
|
|
349
|
+
continue
|
|
350
|
+
if b >= 0x80:
|
|
351
|
+
self._first = -1
|
|
352
|
+
self._sysex = b == 0xF0
|
|
353
|
+
# System common carries no running status: drop its data.
|
|
354
|
+
self._status = b if b < 0xF0 else 0
|
|
355
|
+
continue
|
|
356
|
+
status = self._status
|
|
357
|
+
if self._sysex or not status:
|
|
358
|
+
continue
|
|
359
|
+
kind = status & 0xF0
|
|
360
|
+
if kind == 0xC0:
|
|
361
|
+
sink.program(b, status & 0x0F)
|
|
362
|
+
continue
|
|
363
|
+
if kind == 0xD0:
|
|
364
|
+
continue
|
|
365
|
+
if self._first < 0:
|
|
366
|
+
self._first = b
|
|
367
|
+
continue
|
|
368
|
+
first = self._first
|
|
369
|
+
self._first = -1
|
|
370
|
+
if kind == 0x90:
|
|
371
|
+
if b:
|
|
372
|
+
sink.note_on(first, b, status & 0x0F)
|
|
373
|
+
else:
|
|
374
|
+
sink.note_off(first, status & 0x0F)
|
|
375
|
+
elif kind == 0x80:
|
|
376
|
+
sink.note_off(first, status & 0x0F)
|
|
377
|
+
elif kind == 0xB0:
|
|
378
|
+
sink.control(first, b, status & 0x0F)
|
|
379
|
+
elif kind == 0xE0:
|
|
380
|
+
sink.bend(first | (b << 7), status & 0x0F)
|
|
@@ -0,0 +1,177 @@
|
|
|
1
|
+
# SPDX-FileCopyrightText: 2026 Brad Barnett
|
|
2
|
+
#
|
|
3
|
+
# SPDX-License-Identifier: MIT
|
|
4
|
+
"""Pads that play a drum kit, and pads that play notes when there's no keyboard.
|
|
5
|
+
|
|
6
|
+
A kit's General MIDI notes leave gaps and repeats under a keyboard's pads, so
|
|
7
|
+
`KIT_PADS` lays each kit out as one pad per sound. `pad_notes` is
|
|
8
|
+
what a pad note plays on an instrument.
|
|
9
|
+
|
|
10
|
+
`NotePads` stands between the panel and the instrument's adapter.
|
|
11
|
+
Bank A's sixteen pads become notes: a drum machine's voices, or two octaves
|
|
12
|
+
of a C major scale. The instrument's patches move up to bank B onwards, so
|
|
13
|
+
they're still a press away.
|
|
14
|
+
"""
|
|
15
|
+
|
|
16
|
+
PADS = 16
|
|
17
|
+
#: Two octaves of C major from C3, for anything that isn't a drum machine.
|
|
18
|
+
SCALE = (48, 50, 52, 53, 55, 57, 59, 60, 62, 64, 65, 67, 69, 71, 72, 74)
|
|
19
|
+
NAMES = ("C", "C#", "D", "D#", "E", "F", "F#", "G", "G#", "A", "A#", "B")
|
|
20
|
+
#: The note a keyboard's first pad sends, and how many pads it has: three banks
|
|
21
|
+
#: of eight on a Donner DMK-25 PRO.
|
|
22
|
+
FIRST_PAD = 36
|
|
23
|
+
PAD_COUNT = 24
|
|
24
|
+
#: Each drum machine's sounds in pad order, so a sound stays under the same
|
|
25
|
+
#: finger from kit to kit: kick, snare, closed hat, open hat, clap, rim, toms
|
|
26
|
+
#: from low to high, cymbals, then percussion. A kit moves up over what it
|
|
27
|
+
#: doesn't have. Its other notes repeat these sounds exactly.
|
|
28
|
+
KIT_PADS = {
|
|
29
|
+
"rhythm808": (
|
|
30
|
+
(36, "Bass Drum"), (38, "Snare Drum"), (42, "Closed Hi-Hat"), (46, "Open Hi-Hat"),
|
|
31
|
+
(39, "Hand Clap"), (37, "Rim Shot"), (41, "Low Tom"), (45, "Mid Tom"),
|
|
32
|
+
(48, "High Tom"), (49, "Cymbal"), (56, "Cowbell"), (62, "High Conga"),
|
|
33
|
+
(63, "Mid Conga"), (64, "Low Conga"), (70, "Maracas"), (75, "Claves"),
|
|
34
|
+
),
|
|
35
|
+
"rhythm909": (
|
|
36
|
+
(36, "Bass Drum"), (38, "Snare Drum"), (42, "Closed Hi-Hat"), (46, "Open Hi-Hat"),
|
|
37
|
+
(39, "Hand Clap"), (37, "Rim Shot"), (41, "Low Tom"), (45, "Mid Tom"),
|
|
38
|
+
(48, "High Tom"), (49, "Crash Cymbal"), (51, "Ride Cymbal"),
|
|
39
|
+
),
|
|
40
|
+
"rhythm606": (
|
|
41
|
+
(36, "Bass Drum"), (38, "Snare Drum"), (42, "Closed Hi-Hat"), (46, "Open Hi-Hat"),
|
|
42
|
+
(41, "Low Tom"), (48, "High Tom"), (49, "Cymbal"),
|
|
43
|
+
),
|
|
44
|
+
"rhythm78": (
|
|
45
|
+
(36, "Bass Drum"), (38, "Snare Drum"), (42, "Hi-Hat"), (46, "Open Hi-Hat"),
|
|
46
|
+
(37, "Rim Shot"), (49, "Cymbal"), (54, "Tambourine"), (56, "Cowbell"),
|
|
47
|
+
(60, "High Bongo"), (61, "Low Bongo"), (62, "Low Conga"), (70, "Maracas"),
|
|
48
|
+
(73, "Guiro Short"), (74, "Guiro Long"), (75, "Claves"), (55, "Metal Beat"),
|
|
49
|
+
),
|
|
50
|
+
"electronic_drums5": (
|
|
51
|
+
(36, "Bass Drum"), (38, "Snare Drum"), (42, "Closed Hi-Hat"), (46, "Open Hi-Hat"),
|
|
52
|
+
(41, "Tom 3"), (45, "Tom 2"), (48, "Tom 1"), (49, "Cymbal"),
|
|
53
|
+
),
|
|
54
|
+
}
|
|
55
|
+
#: A SoundFont's kits differ, so its pads are the first of these General MIDI
|
|
56
|
+
#: keys that the kit playing has a sound for, in the same order.
|
|
57
|
+
SOUNDFONT_KIT = "soundfont_drums"
|
|
58
|
+
SOUNDFONT_ORDER = (
|
|
59
|
+
36, 38, 42, 46, 39, 37, 41, 43, 45, 47, 48, 50, 49, 51, 57, 59,
|
|
60
|
+
35, 40, 44, 52, 53, 55, 54, 56, 58, 60, 61, 62, 63, 64, 65, 66,
|
|
61
|
+
67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81,
|
|
62
|
+
)
|
|
63
|
+
|
|
64
|
+
|
|
65
|
+
def note_name(note):
|
|
66
|
+
return "%s%d" % (NAMES[note % 12], note // 12 - 1)
|
|
67
|
+
|
|
68
|
+
|
|
69
|
+
def pad_notes(name, instrument):
|
|
70
|
+
"""The note each pad plays on instrument *name*, or None if it isn't laid out."""
|
|
71
|
+
kit = KIT_PADS.get(name)
|
|
72
|
+
if kit is not None:
|
|
73
|
+
return tuple(note for note, _ in kit)
|
|
74
|
+
if name != SOUNDFONT_KIT:
|
|
75
|
+
return None
|
|
76
|
+
bank, program = instrument.preset
|
|
77
|
+
font = instrument.bank
|
|
78
|
+
return tuple(key for key in SOUNDFONT_ORDER if font.regions(bank, program, key, 100))[:PAD_COUNT]
|
|
79
|
+
|
|
80
|
+
|
|
81
|
+
def notes_for(face):
|
|
82
|
+
"""``((note, label), ...)`` for bank A: a kit's sounds, or the scale."""
|
|
83
|
+
kit = KIT_PADS.get(getattr(face, "NAME", None))
|
|
84
|
+
if kit is not None:
|
|
85
|
+
return kit
|
|
86
|
+
note_map = getattr(face, "NOTE_MAP", None)
|
|
87
|
+
if note_map:
|
|
88
|
+
return tuple((note, label) for note, label in tuple(note_map)[:PADS])
|
|
89
|
+
return tuple((note, note_name(note)) for note in SCALE)
|
|
90
|
+
|
|
91
|
+
|
|
92
|
+
class NotePads:
|
|
93
|
+
"""The panel's adapter, with bank A playing *notes*.
|
|
94
|
+
|
|
95
|
+
*adapter* is the instrument's own adapter. *play(note)* sounds a note
|
|
96
|
+
for a pad press; everything else passes straight through, with patch
|
|
97
|
+
numbers moved up a bank each way.
|
|
98
|
+
"""
|
|
99
|
+
|
|
100
|
+
def __init__(self, adapter, notes, play):
|
|
101
|
+
self.adapter = adapter
|
|
102
|
+
self.notes = notes
|
|
103
|
+
self._play = play
|
|
104
|
+
self._listeners = []
|
|
105
|
+
labels = [label for _, label in notes]
|
|
106
|
+
self._labels = labels + ["-"] * (PADS - len(labels))
|
|
107
|
+
adapter.on_external_change(self._relay)
|
|
108
|
+
|
|
109
|
+
# ---- what the panel reads ------------------------------------------------
|
|
110
|
+
|
|
111
|
+
@property
|
|
112
|
+
def script_name(self):
|
|
113
|
+
return self.adapter.script_name
|
|
114
|
+
|
|
115
|
+
@property
|
|
116
|
+
def macro_labels(self):
|
|
117
|
+
return self.adapter.macro_labels
|
|
118
|
+
|
|
119
|
+
@property
|
|
120
|
+
def macro_count(self):
|
|
121
|
+
return self.adapter.macro_count
|
|
122
|
+
|
|
123
|
+
@property
|
|
124
|
+
def macro_values(self):
|
|
125
|
+
return self.adapter.macro_values
|
|
126
|
+
|
|
127
|
+
@property
|
|
128
|
+
def patches(self):
|
|
129
|
+
return self._labels + list(self.adapter.patches)[: 128 - PADS]
|
|
130
|
+
|
|
131
|
+
@property
|
|
132
|
+
def patch_index(self):
|
|
133
|
+
return self.adapter.patch_index + PADS
|
|
134
|
+
|
|
135
|
+
@property
|
|
136
|
+
def bypass(self):
|
|
137
|
+
return self.adapter.bypass
|
|
138
|
+
|
|
139
|
+
@bypass.setter
|
|
140
|
+
def bypass(self, value):
|
|
141
|
+
self.adapter.bypass = value
|
|
142
|
+
|
|
143
|
+
@property
|
|
144
|
+
def engine_ready(self):
|
|
145
|
+
return self.adapter.engine_ready
|
|
146
|
+
|
|
147
|
+
@property
|
|
148
|
+
def engine_error(self):
|
|
149
|
+
return self.adapter.engine_error
|
|
150
|
+
|
|
151
|
+
# ---- what the panel calls ------------------------------------------------
|
|
152
|
+
|
|
153
|
+
def set_macro(self, index, value):
|
|
154
|
+
self.adapter.set_macro(index, value)
|
|
155
|
+
|
|
156
|
+
def set_patch(self, index):
|
|
157
|
+
if index < PADS:
|
|
158
|
+
if index < len(self.notes):
|
|
159
|
+
self._play(self.notes[index][0])
|
|
160
|
+
return
|
|
161
|
+
self.adapter.set_patch(index - PADS)
|
|
162
|
+
|
|
163
|
+
def set_bypass(self, enabled):
|
|
164
|
+
self.adapter.set_bypass(enabled)
|
|
165
|
+
|
|
166
|
+
def request_reload(self):
|
|
167
|
+
self.adapter.request_reload()
|
|
168
|
+
|
|
169
|
+
def on_external_change(self, callback):
|
|
170
|
+
self._listeners.append(callback)
|
|
171
|
+
|
|
172
|
+
def _relay(self, kind, index, value):
|
|
173
|
+
if kind == "patch":
|
|
174
|
+
index += PADS
|
|
175
|
+
value += PADS
|
|
176
|
+
for listener in self._listeners:
|
|
177
|
+
listener(kind, index, value)
|
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
# SPDX-FileCopyrightText: 2026 Brad Barnett
|
|
2
|
+
#
|
|
3
|
+
# SPDX-License-Identifier: MIT
|
|
4
|
+
"""An instrument for each MIDI channel, and their sum for the audio out.
|
|
5
|
+
|
|
6
|
+
Channel 1 always has an instrument. Any other channel can have one of its
|
|
7
|
+
own, and a channel without one plays channel 1's. A `Part` is one channel's
|
|
8
|
+
instrument with what MIDI has left waiting for it; a `Mix` is what the output
|
|
9
|
+
plays when more than one channel has an instrument.
|
|
10
|
+
"""
|
|
11
|
+
|
|
12
|
+
#: MIDI's channels, counted from 0 here and from 1 on the screen.
|
|
13
|
+
CHANNELS = 16
|
|
14
|
+
#: The mix's buffer in bytes: 512 stereo frames, about 11 ms at 48 kHz.
|
|
15
|
+
MIX_BUFFER = 2048
|
|
16
|
+
#: How long an instrument has had no voice awake before the mix stops pulling
|
|
17
|
+
#: it. Its effects run on while it's pulled, at a cost even in silence, and
|
|
18
|
+
#: every instrument's sound has died away by the time its last voice sleeps.
|
|
19
|
+
PARK_MS = 500
|
|
20
|
+
#: A parked instrument's voice plays this many frames of silence on a loop.
|
|
21
|
+
REST_FRAMES = 256
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
class Part:
|
|
25
|
+
"""One channel's instrument.
|
|
26
|
+
|
|
27
|
+
*adapter* is the instrument's panel adapter. ``knobs`` and ``program``
|
|
28
|
+
hold the last knob values and program change read from MIDI until the
|
|
29
|
+
app applies them, and ``pad_notes`` is what the pads play on a drum kit.
|
|
30
|
+
``most`` is the most voices the voice cap gives the instrument: as many
|
|
31
|
+
as it had when it was made, and 1 for an instrument it leaves alone.
|
|
32
|
+
|
|
33
|
+
A panel registers with the adapter and never takes itself off, and here
|
|
34
|
+
an instrument outlives the panel that shows it. So the adapter's
|
|
35
|
+
``on_external_change`` is replaced with one this part can `forget`.
|
|
36
|
+
"""
|
|
37
|
+
|
|
38
|
+
def __init__(self, name, adapter):
|
|
39
|
+
self.name = name
|
|
40
|
+
self.adapter = adapter
|
|
41
|
+
self.most = adapter.instrument.voices
|
|
42
|
+
self.knobs = {}
|
|
43
|
+
self.program = None
|
|
44
|
+
self.pad_notes = None
|
|
45
|
+
self.pad_patch = None
|
|
46
|
+
self.struck = {}
|
|
47
|
+
self._listeners = []
|
|
48
|
+
adapter.on_external_change(self._relay)
|
|
49
|
+
adapter.on_external_change = self._listeners.append
|
|
50
|
+
|
|
51
|
+
def _relay(self, kind, index, value):
|
|
52
|
+
for listener in self._listeners:
|
|
53
|
+
listener(kind, index, value)
|
|
54
|
+
|
|
55
|
+
def forget(self):
|
|
56
|
+
"""Stop telling the panel that was listening: it's gone."""
|
|
57
|
+
del self._listeners[:]
|
|
58
|
+
|
|
59
|
+
|
|
60
|
+
class Mix:
|
|
61
|
+
"""Several instruments summed into one ``output``.
|
|
62
|
+
|
|
63
|
+
Each plays at full level, so an instrument is as loud in a mix as it is
|
|
64
|
+
alone, and a sum past full scale is held there rather than wrapping.
|
|
65
|
+
|
|
66
|
+
An instrument that has had no voice awake for `PARK_MS` is parked: the
|
|
67
|
+
mix stops pulling it, so it costs nothing until `wake` or a voice waking
|
|
68
|
+
brings it back. Call `tick` as time passes, and `wake` before a note.
|
|
69
|
+
"""
|
|
70
|
+
|
|
71
|
+
def __init__(self, instruments, rate, channels):
|
|
72
|
+
from array import array
|
|
73
|
+
|
|
74
|
+
import audiocore
|
|
75
|
+
import audiomixer
|
|
76
|
+
|
|
77
|
+
# A parked voice plays silence rather than being stopped. Firmware
|
|
78
|
+
# whose mixer stops a voice without the audio pump's lock can hang
|
|
79
|
+
# when that lands mid-mix, and play() takes the lock.
|
|
80
|
+
self._rest = audiocore.RawSample(
|
|
81
|
+
array("h", bytes(2 * REST_FRAMES * channels)), channel_count=channels, sample_rate=rate)
|
|
82
|
+
mixer = audiomixer.Mixer(
|
|
83
|
+
voice_count=len(instruments), buffer_size=MIX_BUFFER, channel_count=channels,
|
|
84
|
+
bits_per_sample=16, samples_signed=True, sample_rate=rate)
|
|
85
|
+
for i, inst in enumerate(instruments):
|
|
86
|
+
mixer.voice[i].play(inst.output, loop=True)
|
|
87
|
+
self.output = mixer
|
|
88
|
+
self._count = len(instruments)
|
|
89
|
+
self._instruments = list(instruments)
|
|
90
|
+
self._quiet = [0] * self._count
|
|
91
|
+
self.parked = [False] * self._count
|
|
92
|
+
|
|
93
|
+
def wake(self, inst):
|
|
94
|
+
"""Pull *inst* again if it was parked, before it's played."""
|
|
95
|
+
for i, each in enumerate(self._instruments):
|
|
96
|
+
if each is inst:
|
|
97
|
+
self._quiet[i] = 0
|
|
98
|
+
if self.parked[i]:
|
|
99
|
+
self.parked[i] = False
|
|
100
|
+
self.output.voice[i].play(inst.output, loop=True)
|
|
101
|
+
return
|
|
102
|
+
|
|
103
|
+
def tick(self, ms):
|
|
104
|
+
"""*ms* more have passed: park the instruments that have stayed
|
|
105
|
+
asleep `PARK_MS`, and bring back any whose voices woke by themselves."""
|
|
106
|
+
mixer = self.output
|
|
107
|
+
if mixer is None:
|
|
108
|
+
return
|
|
109
|
+
for i, inst in enumerate(self._instruments):
|
|
110
|
+
if inst.awake:
|
|
111
|
+
self._quiet[i] = 0
|
|
112
|
+
if self.parked[i]:
|
|
113
|
+
self.parked[i] = False
|
|
114
|
+
mixer.voice[i].play(inst.output, loop=True)
|
|
115
|
+
elif not self.parked[i]:
|
|
116
|
+
self._quiet[i] += ms
|
|
117
|
+
if self._quiet[i] >= PARK_MS:
|
|
118
|
+
self.parked[i] = True
|
|
119
|
+
mixer.voice[i].play(self._rest, loop=True)
|
|
120
|
+
|
|
121
|
+
def deinit(self):
|
|
122
|
+
"""Let go of the instruments. Call it once nothing pulls the output."""
|
|
123
|
+
mixer = self.output
|
|
124
|
+
self.output = None
|
|
125
|
+
if mixer is not None:
|
|
126
|
+
for i in range(self._count):
|
|
127
|
+
mixer.voice[i].stop()
|
|
128
|
+
mixer.deinit()
|