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
apps/fmsynth/meters.py
ADDED
|
@@ -0,0 +1,262 @@
|
|
|
1
|
+
# SPDX-FileCopyrightText: 2026 Brad Barnett
|
|
2
|
+
#
|
|
3
|
+
# SPDX-License-Identifier: MIT
|
|
4
|
+
"""What the header's meter, MIDI light and load readout show, and where it
|
|
5
|
+
comes from.
|
|
6
|
+
|
|
7
|
+
Adapted from Synth Station's ``audio.py`` and ``meters.py`` (PyDevices/apps,
|
|
8
|
+
MIT; see NOTICE): how it reads the level from the pump's tap, how it works
|
|
9
|
+
out the render load and counts underruns from the pump's status, and how its
|
|
10
|
+
bar falls and its light fades. The drawing isn't here: the panel shows these
|
|
11
|
+
on controlkit's controls.
|
|
12
|
+
|
|
13
|
+
Together the meter and the light say which side a silence is on: the light
|
|
14
|
+
with no meter is a note the engine didn't sound, and neither means nothing
|
|
15
|
+
arrived. The meter shows what goes to the audio out: after fmsynth's own
|
|
16
|
+
volume and limiter, and before the codec's or the computer's volume, which
|
|
17
|
+
doesn't move it.
|
|
18
|
+
"""
|
|
19
|
+
|
|
20
|
+
import math
|
|
21
|
+
import struct
|
|
22
|
+
import time
|
|
23
|
+
from array import array
|
|
24
|
+
|
|
25
|
+
#: The peaks an empty and a full meter stand for.
|
|
26
|
+
FLOOR_DB = -60.0
|
|
27
|
+
TOP_DB = 0.0
|
|
28
|
+
#: A meter's steps, 3 dB each. Every redraw of the panel slows the pump a
|
|
29
|
+
#: little, so the meter draws ten times a second and a held note stays still
|
|
30
|
+
#: within a step.
|
|
31
|
+
BAR_TOP = 20
|
|
32
|
+
TICK_MS = 100
|
|
33
|
+
#: How often the load and the underruns are read, in milliseconds.
|
|
34
|
+
STATUS_MS = 500
|
|
35
|
+
#: The shortest stretch a load is measured over, in microseconds.
|
|
36
|
+
MEASURE_US = 200000
|
|
37
|
+
#: Taken off per tick: the meter falls in a second, and the light goes out in
|
|
38
|
+
#: 200 ms, so that hits at 120 bpm stay apart.
|
|
39
|
+
BAR_DECAY = 2
|
|
40
|
+
LIGHT_DECAY = 64
|
|
41
|
+
#: The softest note's flash, of 127, so that it can still be seen.
|
|
42
|
+
LIGHT_FLOOR = 64
|
|
43
|
+
|
|
44
|
+
#: What `Monitor.levels` looks at: the newest 100 ms, every 40th frame, which
|
|
45
|
+
#: is enough for a meter and costs the app about a millisecond.
|
|
46
|
+
LEVEL_FRAMES = 4800
|
|
47
|
+
LEVEL_STEP = 40
|
|
48
|
+
TAP_FRAMES = 8192
|
|
49
|
+
|
|
50
|
+
# Where the pump's status block keeps what `Monitor.load` reads.
|
|
51
|
+
_BLOCKS, _BYTES, _PULL_US, _SINK_BYTES, _MAX_PULL_US, _DMA_BYTES = 0, 1, 12, 15, 20, 21
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
if hasattr(time, "ticks_us"):
|
|
55
|
+
_now_us = time.ticks_us
|
|
56
|
+
_us_since = time.ticks_diff
|
|
57
|
+
else:
|
|
58
|
+
|
|
59
|
+
def _now_us():
|
|
60
|
+
return time.perf_counter_ns() // 1000
|
|
61
|
+
|
|
62
|
+
def _us_since(end, start):
|
|
63
|
+
return end - start
|
|
64
|
+
|
|
65
|
+
|
|
66
|
+
def bar_height(peak):
|
|
67
|
+
"""A peak in 0.0 to 1.0 as a meter's height in 0 to `BAR_TOP`."""
|
|
68
|
+
if peak <= 0:
|
|
69
|
+
return 0
|
|
70
|
+
db = 20 * math.log10(peak)
|
|
71
|
+
return max(0, min(BAR_TOP, int((db - FLOOR_DB) * BAR_TOP / (TOP_DB - FLOOR_DB))))
|
|
72
|
+
|
|
73
|
+
|
|
74
|
+
def status_text(load, underruns):
|
|
75
|
+
"""The readout: render load as a percentage, and the underruns counted."""
|
|
76
|
+
return "LOAD --" if load is None else "LOAD %d%% U%d" % (load, underruns)
|
|
77
|
+
|
|
78
|
+
|
|
79
|
+
class Bar:
|
|
80
|
+
"""A meter's height from the newest peak: it jumps up and falls slowly."""
|
|
81
|
+
|
|
82
|
+
def __init__(self):
|
|
83
|
+
self.shown = 0
|
|
84
|
+
|
|
85
|
+
def tick(self, peak):
|
|
86
|
+
"""Take *peak*, 0.0 to 1.0; return the height to show, 0 to `BAR_TOP`."""
|
|
87
|
+
shown = self.shown
|
|
88
|
+
height = bar_height(peak)
|
|
89
|
+
if height < shown:
|
|
90
|
+
# A step's wobble isn't worth a redraw, but silence is empty.
|
|
91
|
+
if height == shown - 1 and height:
|
|
92
|
+
height = shown
|
|
93
|
+
else:
|
|
94
|
+
height = max(height, shown - BAR_DECAY)
|
|
95
|
+
self.shown = height
|
|
96
|
+
return height
|
|
97
|
+
|
|
98
|
+
|
|
99
|
+
class Light:
|
|
100
|
+
"""The MIDI light: a note strikes it, and it fades over two ticks."""
|
|
101
|
+
|
|
102
|
+
def __init__(self):
|
|
103
|
+
self.level = 0
|
|
104
|
+
|
|
105
|
+
def strike(self, velocity):
|
|
106
|
+
"""A note came in."""
|
|
107
|
+
self.level = max(self.level, LIGHT_FLOOR + velocity * (127 - LIGHT_FLOOR) // 127)
|
|
108
|
+
|
|
109
|
+
def tick(self):
|
|
110
|
+
"""Return how bright it is now, 0 to 127, and fade it a step."""
|
|
111
|
+
level = self.level
|
|
112
|
+
self.level = max(0, level - LIGHT_DECAY)
|
|
113
|
+
return level
|
|
114
|
+
|
|
115
|
+
|
|
116
|
+
class Monitor:
|
|
117
|
+
"""Watches what an ``audio_out`` plays: its level, load and underruns.
|
|
118
|
+
|
|
119
|
+
``attach(out)`` after ``out.play()``, ``detach()`` before it closes.
|
|
120
|
+
Everything reads as silence and "not measured" where the audio doesn't
|
|
121
|
+
go through the pump, or the pump has no tap.
|
|
122
|
+
"""
|
|
123
|
+
|
|
124
|
+
def __init__(self, rate, channels):
|
|
125
|
+
self.rate = rate
|
|
126
|
+
self.channels = channels
|
|
127
|
+
self._out = None
|
|
128
|
+
self._owner = None
|
|
129
|
+
self._tap = None
|
|
130
|
+
self._level_buf = None
|
|
131
|
+
self._last = None
|
|
132
|
+
self._starved = 0
|
|
133
|
+
self.underruns = 0
|
|
134
|
+
self.load_pct = None
|
|
135
|
+
self.worst_pct = None
|
|
136
|
+
try:
|
|
137
|
+
import audiopump
|
|
138
|
+
except ImportError:
|
|
139
|
+
audiopump = None
|
|
140
|
+
self._pump = audiopump
|
|
141
|
+
#: The clock the load is measured against, in microseconds.
|
|
142
|
+
self.clock = _now_us
|
|
143
|
+
|
|
144
|
+
def attach(self, out):
|
|
145
|
+
self._out = out
|
|
146
|
+
self._owner = None
|
|
147
|
+
self._last = None
|
|
148
|
+
self._starved = 0
|
|
149
|
+
self.underruns = 0
|
|
150
|
+
self.load_pct = None
|
|
151
|
+
self.worst_pct = None
|
|
152
|
+
pump = self._pump
|
|
153
|
+
if pump is None or not hasattr(pump, "Tap"):
|
|
154
|
+
return
|
|
155
|
+
if self._tap is None:
|
|
156
|
+
self._tap = pump.Tap(frames=TAP_FRAMES, channel_count=self.channels)
|
|
157
|
+
self._level_buf = array("h", bytes(LEVEL_FRAMES * self.channels * 2))
|
|
158
|
+
pump.tap(self._tap)
|
|
159
|
+
|
|
160
|
+
def detach(self):
|
|
161
|
+
self._out = None
|
|
162
|
+
self._owner = None
|
|
163
|
+
if self._tap is not None:
|
|
164
|
+
self._tap = None
|
|
165
|
+
self._pump.tap(None)
|
|
166
|
+
|
|
167
|
+
def levels(self):
|
|
168
|
+
"""The newest audio's left and right peaks, 0.0 to 1.0.
|
|
169
|
+
|
|
170
|
+
Both 0.0 when nothing new has gone out, or where there's no tap.
|
|
171
|
+
"""
|
|
172
|
+
if self._tap is None:
|
|
173
|
+
return 0.0, 0.0
|
|
174
|
+
buf = self._level_buf
|
|
175
|
+
channels = self.channels
|
|
176
|
+
samples = self._tap.readinto(buf) // 2
|
|
177
|
+
left = right = 0
|
|
178
|
+
for i in range(0, samples - channels + 1, LEVEL_STEP * channels):
|
|
179
|
+
v = buf[i]
|
|
180
|
+
if v < 0:
|
|
181
|
+
v = -v
|
|
182
|
+
if v > left:
|
|
183
|
+
left = v
|
|
184
|
+
v = buf[i + channels - 1]
|
|
185
|
+
if v < 0:
|
|
186
|
+
v = -v
|
|
187
|
+
if v > right:
|
|
188
|
+
right = v
|
|
189
|
+
return left / 32768, right / 32768
|
|
190
|
+
|
|
191
|
+
def _pump_owner(self):
|
|
192
|
+
"""audiodev's pump owner, once ``audio_out`` plays through the pump."""
|
|
193
|
+
out = self._out
|
|
194
|
+
if out is None or not getattr(out, "pumped", False):
|
|
195
|
+
return None
|
|
196
|
+
if self._owner is None:
|
|
197
|
+
try:
|
|
198
|
+
from audiodev import pump
|
|
199
|
+
|
|
200
|
+
self._owner = pump.owner()
|
|
201
|
+
except Exception:
|
|
202
|
+
return None
|
|
203
|
+
return self._owner
|
|
204
|
+
|
|
205
|
+
def load(self):
|
|
206
|
+
"""Update ``load_pct``, ``worst_pct`` and ``underruns``; return them.
|
|
207
|
+
|
|
208
|
+
``load_pct`` is the render time of the blocks since the last call as
|
|
209
|
+
a percentage of the time that passed: over 100 can't keep up.
|
|
210
|
+
``worst_pct`` is the slowest single block since `attach`, against
|
|
211
|
+
the time a block had. Both are measured against the clock, not
|
|
212
|
+
worked out from the bytes played: the pump counts bytes as the
|
|
213
|
+
output takes them, and a board's I2S takes two channels for a synth
|
|
214
|
+
that makes one. ``underruns`` counts the calls between which the
|
|
215
|
+
speaker fell further behind than ever before, and more than a block
|
|
216
|
+
behind (on a board), or blocks the output had no room for (on a
|
|
217
|
+
desktop). Both loads are None until there's something to measure.
|
|
218
|
+
"""
|
|
219
|
+
owner = self._pump_owner()
|
|
220
|
+
if owner is None or self._pump is None:
|
|
221
|
+
return self.load_pct, self.worst_pct, self.underruns
|
|
222
|
+
try:
|
|
223
|
+
status = owner.status()
|
|
224
|
+
except Exception:
|
|
225
|
+
status = None
|
|
226
|
+
if status is None:
|
|
227
|
+
return self.load_pct, self.worst_pct, self.underruns
|
|
228
|
+
w = struct.unpack_from("<%dQ" % self._pump.STATUS_WORDS, status)
|
|
229
|
+
blocks, nbytes, pull_us = w[_BLOCKS], w[_BYTES], w[_PULL_US]
|
|
230
|
+
now = self.clock()
|
|
231
|
+
last = self._last
|
|
232
|
+
if last is None:
|
|
233
|
+
self._last = (blocks, now, pull_us)
|
|
234
|
+
elif _us_since(now, last[1]) >= MEASURE_US:
|
|
235
|
+
# Over less time than this the count of blocks is too coarse to
|
|
236
|
+
# say how long one lasts.
|
|
237
|
+
self._last = (blocks, now, pull_us)
|
|
238
|
+
if blocks > last[0]:
|
|
239
|
+
elapsed = _us_since(now, last[1])
|
|
240
|
+
self.load_pct = (pull_us - last[2]) * 100 // elapsed
|
|
241
|
+
self.worst_pct = w[_MAX_PULL_US] * (blocks - last[0]) * 100 // elapsed
|
|
242
|
+
# Through audio_out the pump counts what its own I2S clocked out; a
|
|
243
|
+
# desktop's pump never touches I2S and leaves this at 0.
|
|
244
|
+
dma = w[_DMA_BYTES] if self._pump.STATUS_WORDS > _DMA_BYTES else 0
|
|
245
|
+
if dma:
|
|
246
|
+
# On a board the DMA clocks out silence when the pump is late:
|
|
247
|
+
# bytes it sent that the pump never wrote are a gap you heard.
|
|
248
|
+
# It always runs up to a block ahead of the pump, which hands
|
|
249
|
+
# over a block at a time, so less than a block is no gap.
|
|
250
|
+
gap = dma - w[_SINK_BYTES]
|
|
251
|
+
if gap > self._starved:
|
|
252
|
+
self._starved = gap
|
|
253
|
+
# The ring clocks silence before the first block lands:
|
|
254
|
+
# that's the start, not a gap.
|
|
255
|
+
if last is not None and blocks and gap > nbytes // blocks:
|
|
256
|
+
self.underruns += 1
|
|
257
|
+
else:
|
|
258
|
+
try:
|
|
259
|
+
self.underruns = owner.health().get("dropped", 0)
|
|
260
|
+
except Exception:
|
|
261
|
+
pass
|
|
262
|
+
return self.load_pct, self.worst_pct, self.underruns
|
apps/fmsynth/midi.py
ADDED
|
@@ -0,0 +1,375 @@
|
|
|
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
|
+
`Inputs` is adapted from Synth Station's ``midi.py`` (PyDevices/apps, MIT;
|
|
7
|
+
see NOTICE). On a board with a USB host port it reads every MIDI device
|
|
8
|
+
plugged into it; 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, and in a browser
|
|
11
|
+
every input the browser offers.
|
|
12
|
+
|
|
13
|
+
Bluetooth MIDI is one more input, on a board whose firmware has Bluetooth LE:
|
|
14
|
+
`Inputs.set_ble` makes the board a BLE-MIDI instrument, advertised under the
|
|
15
|
+
app's name, that a controller, a phone or a computer connects to, through
|
|
16
|
+
``midiports.ble``. It's off until the player turns it on, so nothing about
|
|
17
|
+
Bluetooth starts or takes memory before then.
|
|
18
|
+
|
|
19
|
+
`Parser` turns the bytes into calls on an object you pass: notes, controllers,
|
|
20
|
+
pitch bend and program changes, and each whole system exclusive message, so a
|
|
21
|
+
32-voice bank or a single voice sent from an editor arrives in one piece.
|
|
22
|
+
"""
|
|
23
|
+
|
|
24
|
+
import sys
|
|
25
|
+
|
|
26
|
+
#: What ``Inputs.why`` says when a computer has no MIDI input at all.
|
|
27
|
+
NO_INPUT = "no MIDI input on this computer"
|
|
28
|
+
#: The platforms that are a computer, with MIDI ports of their own.
|
|
29
|
+
DESKTOPS = ("win32", "linux", "darwin")
|
|
30
|
+
#: The platforms that are a browser page, whose MIDI inputs are Web MIDI's:
|
|
31
|
+
#: MicroPython's WebAssembly port and Pyodide.
|
|
32
|
+
BROWSERS = ("webassembly", "emscripten")
|
|
33
|
+
#: The most reads of one port in one poll.
|
|
34
|
+
READS_PER_POLL = 64
|
|
35
|
+
#: The longest system exclusive message kept: a 32-voice bank.
|
|
36
|
+
SYSEX_MAX = 4104
|
|
37
|
+
#: The name a board advertises its Bluetooth MIDI under.
|
|
38
|
+
BLE_NAME = "fmsynth"
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
def ble_available():
|
|
42
|
+
"""True on a board whose firmware has Bluetooth LE, as
|
|
43
|
+
``midiports.ble.available()`` says, without importing the port: that
|
|
44
|
+
waits until the player turns it on. Starts nothing."""
|
|
45
|
+
if sys.implementation.name not in ("micropython", "circuitpython"):
|
|
46
|
+
return False
|
|
47
|
+
if sys.platform in BROWSERS:
|
|
48
|
+
return False
|
|
49
|
+
for name in ("bluetooth", "_bleio"):
|
|
50
|
+
try:
|
|
51
|
+
__import__(name)
|
|
52
|
+
return True
|
|
53
|
+
except ImportError:
|
|
54
|
+
pass
|
|
55
|
+
return False
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
def _name(dev):
|
|
59
|
+
"""A USB device's product string, or its vid:pid when it has none."""
|
|
60
|
+
try:
|
|
61
|
+
product = dev.product
|
|
62
|
+
except Exception: # a device that won't answer a string request
|
|
63
|
+
product = None
|
|
64
|
+
return product or "%04x:%04x" % (dev.idVendor, dev.idProduct)
|
|
65
|
+
|
|
66
|
+
|
|
67
|
+
class Inputs:
|
|
68
|
+
"""The open MIDI inputs, read together.
|
|
69
|
+
|
|
70
|
+
``kind`` is ``"usb"``, ``"device"``, ``"desktop"``, ``"web"`` or None when there's nothing to
|
|
71
|
+
open; ``why`` then says why in a sentence. ``names()`` lists the
|
|
72
|
+
devices heard now.
|
|
73
|
+
"""
|
|
74
|
+
|
|
75
|
+
def __init__(self):
|
|
76
|
+
self.kind = None
|
|
77
|
+
self.why = None
|
|
78
|
+
self.ports = []
|
|
79
|
+
self._desktop_names = []
|
|
80
|
+
self._usb = False
|
|
81
|
+
self._buf = bytearray(64)
|
|
82
|
+
#: The Bluetooth MIDI input while it's on, and why it stopped if it did.
|
|
83
|
+
self.ble = None
|
|
84
|
+
self.ble_why = None
|
|
85
|
+
|
|
86
|
+
# ---- opening --------------------------------------------------------------
|
|
87
|
+
|
|
88
|
+
def open(self):
|
|
89
|
+
"""Open whatever this computer or board has. Never raises."""
|
|
90
|
+
# A computer reads its own MIDI ports, even where a usb.core is
|
|
91
|
+
# importable (PyUSB, or a desktop build that carries the package).
|
|
92
|
+
if getattr(sys, "platform", "") in DESKTOPS:
|
|
93
|
+
self._open_desktop()
|
|
94
|
+
return self
|
|
95
|
+
if getattr(sys, "platform", "") in BROWSERS:
|
|
96
|
+
self._open_web()
|
|
97
|
+
return self
|
|
98
|
+
try:
|
|
99
|
+
import usb.core
|
|
100
|
+
|
|
101
|
+
running = usb.core.running # PyUSB on a desktop has no host to start
|
|
102
|
+
except (ImportError, AttributeError):
|
|
103
|
+
running = None
|
|
104
|
+
if running is not None:
|
|
105
|
+
try:
|
|
106
|
+
from midiports import usb_host
|
|
107
|
+
except ImportError:
|
|
108
|
+
self.why = "midiports isn't installed"
|
|
109
|
+
return self
|
|
110
|
+
try:
|
|
111
|
+
# Take the host as it runs: a launcher that started it keeps
|
|
112
|
+
# its speed and its devices. Only a host this app started is
|
|
113
|
+
# its own to stop.
|
|
114
|
+
self._usb = not running()
|
|
115
|
+
port, out = usb_host.all_ports()
|
|
116
|
+
if out is not None:
|
|
117
|
+
out.close()
|
|
118
|
+
except Exception as e:
|
|
119
|
+
self.why = "the USB host didn't start: %s" % (e,)
|
|
120
|
+
return self
|
|
121
|
+
self.ports = [port]
|
|
122
|
+
self.kind = "usb"
|
|
123
|
+
return self
|
|
124
|
+
if self._open_device():
|
|
125
|
+
return self
|
|
126
|
+
self._open_desktop()
|
|
127
|
+
return self
|
|
128
|
+
|
|
129
|
+
def _open_device(self):
|
|
130
|
+
"""Make this board a USB MIDI device, where its board says it may
|
|
131
|
+
(a ``usb_midi`` role): starting one re-enumerates the USB port."""
|
|
132
|
+
try:
|
|
133
|
+
import board_peripherals as bp
|
|
134
|
+
except ImportError:
|
|
135
|
+
return False
|
|
136
|
+
if "usb_midi" not in getattr(bp, "PERIPHERALS", ()):
|
|
137
|
+
return False
|
|
138
|
+
try:
|
|
139
|
+
port, _out = bp.usb_midi()
|
|
140
|
+
except Exception as e:
|
|
141
|
+
self.why = "the USB MIDI device didn't start: %s" % (e,)
|
|
142
|
+
return True
|
|
143
|
+
self.ports = [port]
|
|
144
|
+
self.kind = "device"
|
|
145
|
+
return True
|
|
146
|
+
|
|
147
|
+
def _open_desktop(self):
|
|
148
|
+
platform = getattr(sys, "platform", "")
|
|
149
|
+
try:
|
|
150
|
+
import midiports
|
|
151
|
+
|
|
152
|
+
if platform.startswith("win"):
|
|
153
|
+
from midiports import windows as backend
|
|
154
|
+
elif platform.startswith("linux"):
|
|
155
|
+
from midiports import linux as backend
|
|
156
|
+
else:
|
|
157
|
+
self.why = "no MIDI ports on this platform"
|
|
158
|
+
return
|
|
159
|
+
except ImportError:
|
|
160
|
+
self.why = "midiports isn't installed"
|
|
161
|
+
return
|
|
162
|
+
try:
|
|
163
|
+
found = [p for p in backend.available() if p.direction in (midiports.IN, midiports.INOUT)]
|
|
164
|
+
except Exception as e:
|
|
165
|
+
self.why = "couldn't list MIDI ports: %s" % (e,)
|
|
166
|
+
return
|
|
167
|
+
for info in found:
|
|
168
|
+
try:
|
|
169
|
+
midi_in, midi_out = backend.ports(info.name)
|
|
170
|
+
except Exception:
|
|
171
|
+
continue
|
|
172
|
+
if midi_out is not None:
|
|
173
|
+
midi_out.close()
|
|
174
|
+
if midi_in is not None:
|
|
175
|
+
self.ports.append(midi_in)
|
|
176
|
+
self._desktop_names.append(info.name)
|
|
177
|
+
if self.ports:
|
|
178
|
+
self.kind = "desktop"
|
|
179
|
+
else:
|
|
180
|
+
self.why = NO_INPUT
|
|
181
|
+
|
|
182
|
+
def _open_web(self):
|
|
183
|
+
try:
|
|
184
|
+
from midiports import webmidi
|
|
185
|
+
except ImportError:
|
|
186
|
+
self.why = "midiports isn't installed"
|
|
187
|
+
return
|
|
188
|
+
self.ports = [webmidi.page_in()]
|
|
189
|
+
self.kind = "web"
|
|
190
|
+
|
|
191
|
+
def set_ble(self, on, parser=None, name=BLE_NAME, accept=None):
|
|
192
|
+
"""Turn the Bluetooth MIDI input on or off. Turning it on starts
|
|
193
|
+
advertising as *name*; an error starting it is raised. *accept* is
|
|
194
|
+
asked about each device that connects, with its address, and one it
|
|
195
|
+
refuses is dropped (see ``midiports.ble.ports``); a midiports too old
|
|
196
|
+
to ask lets every device in. Turning it off hands
|
|
197
|
+
*parser* a note-off for every note it left sounding."""
|
|
198
|
+
if on and self.ble is None:
|
|
199
|
+
from midiports import ble
|
|
200
|
+
|
|
201
|
+
self.ble_why = None
|
|
202
|
+
try:
|
|
203
|
+
# The output stays unused: closing it closes the link.
|
|
204
|
+
try:
|
|
205
|
+
self.ble, _out = ble.ports(name, accept=accept)
|
|
206
|
+
except TypeError:
|
|
207
|
+
# A midiports older than accept= lets every device in.
|
|
208
|
+
self.ble, _out = ble.ports(name)
|
|
209
|
+
except Exception as e:
|
|
210
|
+
self.ble_why = str(e) or type(e).__name__
|
|
211
|
+
raise
|
|
212
|
+
elif not on and self.ble is not None:
|
|
213
|
+
port = self.ble
|
|
214
|
+
self.ble = None
|
|
215
|
+
port.close()
|
|
216
|
+
if parser is not None:
|
|
217
|
+
buf = self._buf
|
|
218
|
+
try:
|
|
219
|
+
for _ in range(READS_PER_POLL):
|
|
220
|
+
n = port.readinto(buf)
|
|
221
|
+
if not n:
|
|
222
|
+
break
|
|
223
|
+
parser.feed(buf, n)
|
|
224
|
+
except Exception as e: # the link had stopped: said, as in poll
|
|
225
|
+
self.ble_why = str(e) or type(e).__name__
|
|
226
|
+
|
|
227
|
+
def ble_refused(self):
|
|
228
|
+
"""How many devices the Bluetooth MIDI input has turned away."""
|
|
229
|
+
return getattr(self.ble, "refused", 0) if self.ble is not None else 0
|
|
230
|
+
|
|
231
|
+
def ble_text(self):
|
|
232
|
+
"""What the Bluetooth MIDI input is doing, in a few letters for the
|
|
233
|
+
readout: ``"BT"`` while something is connected, ``"BT?"`` while
|
|
234
|
+
it waits for a connection, ``"BT ERR"`` when it stopped, or None
|
|
235
|
+
while it's off."""
|
|
236
|
+
port = self.ble
|
|
237
|
+
if port is None:
|
|
238
|
+
return "BT ERR" if self.ble_why else None
|
|
239
|
+
return "BT" if port.state == "connected" else "BT?"
|
|
240
|
+
|
|
241
|
+
# ---- reading --------------------------------------------------------------
|
|
242
|
+
|
|
243
|
+
def names(self):
|
|
244
|
+
"""The devices heard now, as a list of strings."""
|
|
245
|
+
if self.kind == "usb":
|
|
246
|
+
found = [_name(d) for d in getattr(self.ports[0], "devices", ())]
|
|
247
|
+
elif self.kind == "web":
|
|
248
|
+
found = list(self.ports[0].names)
|
|
249
|
+
elif self.kind == "device":
|
|
250
|
+
found = ["USB MIDI"]
|
|
251
|
+
else:
|
|
252
|
+
found = list(self._desktop_names)
|
|
253
|
+
if self.ble is not None and self.ble.state == "connected":
|
|
254
|
+
found.append("Bluetooth")
|
|
255
|
+
return found
|
|
256
|
+
|
|
257
|
+
def poll(self, parser):
|
|
258
|
+
"""Feed everything waiting on every port to *parser*."""
|
|
259
|
+
buf = self._buf
|
|
260
|
+
ble = self.ble
|
|
261
|
+
if ble is not None:
|
|
262
|
+
try:
|
|
263
|
+
for _ in range(READS_PER_POLL):
|
|
264
|
+
n = ble.readinto(buf)
|
|
265
|
+
if not n:
|
|
266
|
+
break
|
|
267
|
+
parser.feed(buf, n)
|
|
268
|
+
except Exception as e: # the link stopped: said in the readout, not retried
|
|
269
|
+
self.ble = None
|
|
270
|
+
self.ble_why = str(e) or type(e).__name__
|
|
271
|
+
ble.close()
|
|
272
|
+
for port in self.ports:
|
|
273
|
+
any_ = getattr(port, "any", None)
|
|
274
|
+
# Ask first where the port can say: reading an empty USB port
|
|
275
|
+
# still costs a transfer.
|
|
276
|
+
if any_ is not None and not any_():
|
|
277
|
+
continue
|
|
278
|
+
# A merged port hands over one message a read, so read until
|
|
279
|
+
# it's empty, but leave a flood for the next tick.
|
|
280
|
+
for _ in range(READS_PER_POLL):
|
|
281
|
+
n = port.readinto(buf)
|
|
282
|
+
if not n:
|
|
283
|
+
break
|
|
284
|
+
parser.feed(buf, n)
|
|
285
|
+
|
|
286
|
+
def close(self):
|
|
287
|
+
self.set_ble(False)
|
|
288
|
+
ports = self.ports
|
|
289
|
+
self.ports = []
|
|
290
|
+
for port in ports:
|
|
291
|
+
try:
|
|
292
|
+
port.close()
|
|
293
|
+
except Exception:
|
|
294
|
+
pass
|
|
295
|
+
if self._usb:
|
|
296
|
+
self._usb = False
|
|
297
|
+
try:
|
|
298
|
+
import usb.core
|
|
299
|
+
|
|
300
|
+
usb.core.stop()
|
|
301
|
+
except Exception:
|
|
302
|
+
pass
|
|
303
|
+
|
|
304
|
+
|
|
305
|
+
class Parser:
|
|
306
|
+
"""MIDI bytes to calls on *sink*, from every channel.
|
|
307
|
+
|
|
308
|
+
*sink* has ``note_on(note, velocity, channel)``, ``note_off(note, channel)``,
|
|
309
|
+
``control(controller, value, channel)``, ``bend(value, channel)`` (0 to
|
|
310
|
+
16383, centre 8192), ``program(number, channel)`` and ``sysex(message)``,
|
|
311
|
+
with channels counted from 0. *message* is the whole message, F0 to F7,
|
|
312
|
+
in a buffer the parser uses again: copy what you keep. Running status is
|
|
313
|
+
followed; real-time and system common messages are skipped, and so is a
|
|
314
|
+
system exclusive message longer than `SYSEX_MAX`.
|
|
315
|
+
"""
|
|
316
|
+
|
|
317
|
+
def __init__(self, sink):
|
|
318
|
+
self.sink = sink
|
|
319
|
+
self._status = 0
|
|
320
|
+
self._first = -1
|
|
321
|
+
self._sysex = None
|
|
322
|
+
self._length = 0
|
|
323
|
+
self._buf = bytearray(SYSEX_MAX)
|
|
324
|
+
|
|
325
|
+
def feed(self, buf, n):
|
|
326
|
+
sink = self.sink
|
|
327
|
+
for i in range(n):
|
|
328
|
+
b = buf[i]
|
|
329
|
+
if b >= 0xF8: # real time: may land anywhere, even mid-message
|
|
330
|
+
continue
|
|
331
|
+
if b >= 0x80:
|
|
332
|
+
if b == 0xF7 and self._sysex:
|
|
333
|
+
length = self._length
|
|
334
|
+
if length < SYSEX_MAX:
|
|
335
|
+
self._buf[length] = 0xF7
|
|
336
|
+
sink.sysex(memoryview(self._buf)[:length + 1])
|
|
337
|
+
self._first = -1
|
|
338
|
+
self._sysex = b == 0xF0
|
|
339
|
+
if self._sysex:
|
|
340
|
+
self._buf[0] = 0xF0
|
|
341
|
+
self._length = 1
|
|
342
|
+
# System common carries no running status: drop its data.
|
|
343
|
+
self._status = b if b < 0xF0 else 0
|
|
344
|
+
continue
|
|
345
|
+
if self._sysex:
|
|
346
|
+
# One too long is counted past the end, and dropped at its F7.
|
|
347
|
+
if self._length < SYSEX_MAX:
|
|
348
|
+
self._buf[self._length] = b
|
|
349
|
+
self._length += 1
|
|
350
|
+
continue
|
|
351
|
+
status = self._status
|
|
352
|
+
if not status:
|
|
353
|
+
continue
|
|
354
|
+
kind = status & 0xF0
|
|
355
|
+
if kind == 0xC0:
|
|
356
|
+
sink.program(b, status & 0x0F)
|
|
357
|
+
continue
|
|
358
|
+
if kind == 0xD0:
|
|
359
|
+
continue
|
|
360
|
+
if self._first < 0:
|
|
361
|
+
self._first = b
|
|
362
|
+
continue
|
|
363
|
+
first = self._first
|
|
364
|
+
self._first = -1
|
|
365
|
+
if kind == 0x90:
|
|
366
|
+
if b:
|
|
367
|
+
sink.note_on(first, b, status & 0x0F)
|
|
368
|
+
else:
|
|
369
|
+
sink.note_off(first, status & 0x0F)
|
|
370
|
+
elif kind == 0x80:
|
|
371
|
+
sink.note_off(first, status & 0x0F)
|
|
372
|
+
elif kind == 0xB0:
|
|
373
|
+
sink.control(first, b, status & 0x0F)
|
|
374
|
+
elif kind == 0xE0:
|
|
375
|
+
sink.bend(first | (b << 7), status & 0x0F)
|