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/detect/__init__.py
ADDED
|
@@ -0,0 +1,214 @@
|
|
|
1
|
+
# SPDX-FileCopyrightText: 2026 Brad Barnett
|
|
2
|
+
#
|
|
3
|
+
# SPDX-License-Identifier: MIT
|
|
4
|
+
"""Object detection: the camera through YOLO11n, boxes drawn on the preview.
|
|
5
|
+
|
|
6
|
+
The model is Espressif's YOLO11n for ESP-DL, trained on COCO: its 80 classes
|
|
7
|
+
are everyday things (people, cups, bottles, chairs, phones, cats, cars). It
|
|
8
|
+
looks at a 160 x 160 copy of each frame, so a thing that fills a fair part of
|
|
9
|
+
the picture is found and a small one far away isn't.
|
|
10
|
+
|
|
11
|
+
Each loop takes a preview-sized frame into a buffer of its own, runs the
|
|
12
|
+
detector on it, draws the boxes into it and copies it to the panel's
|
|
13
|
+
framebuffer, or on a panel with none redraws the LVGL image that shows it
|
|
14
|
+
(`touchkit.cam.Preview`); the list on the right names what was found,
|
|
15
|
+
in the colour of its box. On a narrow screen the list is under the picture.
|
|
16
|
+
Flip, mirror, light and brightness are the Camera app's.
|
|
17
|
+
|
|
18
|
+
It needs a firmware with the ``espdl`` and ``image`` modules, and the model
|
|
19
|
+
file, ``yolo11n_coco.espdl``, in a ``models`` folder: ``/sd/models`` on the SD
|
|
20
|
+
card (mounted while the app is open), ``/models`` on flash or
|
|
21
|
+
``/data/models``, looked at in that order (`appkit.places`). It checks for
|
|
22
|
+
all three, and for the camera, when it opens, and says what's missing.
|
|
23
|
+
Where to download the model: https://github.com/PyDevices/apps/blob/main/docs/reference/models.md
|
|
24
|
+
|
|
25
|
+
In a browser the page brings its own model, YOLOX-nano, and fetches it the
|
|
26
|
+
first time the app opens, so its boxes can differ a little from a board's.
|
|
27
|
+
The status line names the model that runs.
|
|
28
|
+
"""
|
|
29
|
+
|
|
30
|
+
import lvgl as lv
|
|
31
|
+
from multimer import ticks_diff, ticks_ms
|
|
32
|
+
|
|
33
|
+
from appkit import places
|
|
34
|
+
from touchkit import cam, ui
|
|
35
|
+
|
|
36
|
+
MODEL = "yolo11n_coco.espdl"
|
|
37
|
+
MODELS = "models" # the folder name, on the card, flash and the data root
|
|
38
|
+
PREVIEW = 520
|
|
39
|
+
WIDE = 640 # the narrowest app area with the list beside the picture
|
|
40
|
+
SCORE = 0.15 # two clear bottles in a dim room scored 0.27 and 0.18
|
|
41
|
+
LISTED = 8
|
|
42
|
+
|
|
43
|
+
COCO = (
|
|
44
|
+
"person", "bicycle", "car", "motorcycle", "airplane", "bus", "train", "truck", "boat",
|
|
45
|
+
"traffic light", "fire hydrant", "stop sign", "parking meter", "bench", "bird", "cat",
|
|
46
|
+
"dog", "horse", "sheep", "cow", "elephant", "bear", "zebra", "giraffe", "backpack",
|
|
47
|
+
"umbrella", "handbag", "tie", "suitcase", "frisbee", "skis", "snowboard", "sports ball",
|
|
48
|
+
"kite", "baseball bat", "baseball glove", "skateboard", "surfboard", "tennis racket",
|
|
49
|
+
"bottle", "wine glass", "cup", "fork", "knife", "spoon", "bowl", "banana", "apple",
|
|
50
|
+
"sandwich", "orange", "broccoli", "carrot", "hot dog", "pizza", "donut", "cake", "chair",
|
|
51
|
+
"couch", "potted plant", "bed", "dining table", "toilet", "tv", "laptop", "mouse",
|
|
52
|
+
"remote", "keyboard", "cell phone", "microwave", "oven", "toaster", "sink",
|
|
53
|
+
"refrigerator", "book", "clock", "vase", "scissors", "teddy bear", "hair drier",
|
|
54
|
+
"toothbrush",
|
|
55
|
+
) # fmt: skip
|
|
56
|
+
|
|
57
|
+
# A colour per class, from a short wheel: the box on the picture and its line
|
|
58
|
+
# in the list share it.
|
|
59
|
+
WHEEL = (0xFF5A5A, 0x5AD15A, 0x5A9BFF, 0xFFD24A, 0xD25AFF, 0x4AE0E0, 0xFF9A3C, 0xB4FF5A)
|
|
60
|
+
|
|
61
|
+
state = None # the open app's state, for the console
|
|
62
|
+
|
|
63
|
+
|
|
64
|
+
def _rgb565(rgb):
|
|
65
|
+
return ((rgb >> 8) & 0xF800) | ((rgb >> 5) & 0x07E0) | ((rgb >> 3) & 0x001F)
|
|
66
|
+
|
|
67
|
+
|
|
68
|
+
def _model(scope):
|
|
69
|
+
places.hold_card(scope)
|
|
70
|
+
return places.find(MODELS, MODEL)
|
|
71
|
+
|
|
72
|
+
|
|
73
|
+
def main(scope):
|
|
74
|
+
global state
|
|
75
|
+
root = scope.root
|
|
76
|
+
status = ui.status(scope)
|
|
77
|
+
W, H = scope.size
|
|
78
|
+
RX, RY = scope.rect[:2]
|
|
79
|
+
st = state = {"cam": None, "det": None, "found": [], "ms": 0, "frames": 0, "t0": ticks_ms()}
|
|
80
|
+
|
|
81
|
+
def missing(text):
|
|
82
|
+
ui.missing(scope, text.split(":")[0], text)
|
|
83
|
+
|
|
84
|
+
try:
|
|
85
|
+
import espdl
|
|
86
|
+
import image
|
|
87
|
+
from pygraphics import RGB565, FrameBuffer
|
|
88
|
+
except ImportError as e:
|
|
89
|
+
missing("No vision modules: %s. Object detection needs the board's espdl, image and pygraphics modules." % (e,))
|
|
90
|
+
return
|
|
91
|
+
path = _model(scope)
|
|
92
|
+
if path is None:
|
|
93
|
+
missing("No model: put %s in %s." % (MODEL, places.where_text(MODELS)))
|
|
94
|
+
return
|
|
95
|
+
opts = cam.options(scope)
|
|
96
|
+
try:
|
|
97
|
+
c0 = cam.open_camera(opts)
|
|
98
|
+
except Exception as e:
|
|
99
|
+
missing(cam.no_camera_text(e, "Object detection"))
|
|
100
|
+
return
|
|
101
|
+
|
|
102
|
+
st["cam"] = c0
|
|
103
|
+
try:
|
|
104
|
+
det = espdl.YOLO11(path, score=SCORE)
|
|
105
|
+
except Exception as e:
|
|
106
|
+
c0.deinit()
|
|
107
|
+
st["cam"] = None
|
|
108
|
+
missing("The model didn't load: %s" % (e,))
|
|
109
|
+
return
|
|
110
|
+
st["det"] = det
|
|
111
|
+
cam.apply_controls(c0, opts)
|
|
112
|
+
|
|
113
|
+
wide = W >= WIDE
|
|
114
|
+
if wide:
|
|
115
|
+
pw, ph = cam.preview_size(c0, PREVIEW)
|
|
116
|
+
x0, y0 = 24, 8
|
|
117
|
+
else:
|
|
118
|
+
# Under the row the Home button and the status line share.
|
|
119
|
+
pw, ph = cam.preview_size(c0, W - 8)
|
|
120
|
+
x0, y0 = (W - pw) // 2, ui.small.line_height + 14
|
|
121
|
+
c0 = None
|
|
122
|
+
pv = cam.Preview(root, x0, y0, pw, ph, (RX, RY), own=True)
|
|
123
|
+
buf = pv.buf
|
|
124
|
+
canvas = FrameBuffer(buf, pw, ph, RGB565)
|
|
125
|
+
img = image.Image(pw, ph, image.RGB565, buffer=buf)
|
|
126
|
+
if pv.fb is not None:
|
|
127
|
+
from board_config import display_drv as d
|
|
128
|
+
|
|
129
|
+
panel = FrameBuffer(pv.fb, d.width, d.height, RGB565)
|
|
130
|
+
|
|
131
|
+
def show():
|
|
132
|
+
# The finished frame onto the panel. Not row by row through memoryview
|
|
133
|
+
# slices: into a buffer this big, that took 7 s a frame on an ESP32-P4.
|
|
134
|
+
panel.blit(canvas, pv.x, pv.y)
|
|
135
|
+
d.flush_rect(pv.x, pv.y, pw, ph)
|
|
136
|
+
else:
|
|
137
|
+
show = pv.show # the image redraws from the buffer
|
|
138
|
+
|
|
139
|
+
if wide:
|
|
140
|
+
rx, rw, font, step = x0 + pw + 24, W - x0 - pw - 48, ui.font, 34
|
|
141
|
+
title = ui.label(root, lv.SYMBOL.GPS + " Seeing", ui.big, ui.ACCENT)
|
|
142
|
+
title.set_pos(rx, 12)
|
|
143
|
+
# The status line goes under the header, not over it at the top right.
|
|
144
|
+
status.set_long_mode(lv.label.LONG_MODE.WRAP) # two lines: the model's name, then its classes
|
|
145
|
+
status.set_size(rw, 2 * ui.small.line_height)
|
|
146
|
+
status.set_style_text_align(lv.TEXT_ALIGN.LEFT, 0)
|
|
147
|
+
status.align(lv.ALIGN.TOP_LEFT, rx, 12 + ui.big.line_height + 2)
|
|
148
|
+
ry = 12 + ui.big.line_height + 2 + 2 * ui.small.line_height + 8
|
|
149
|
+
listed = LISTED
|
|
150
|
+
else:
|
|
151
|
+
# Under the picture: as many rows as fit, and the speed line.
|
|
152
|
+
rx, ry, rw, font = x0, y0 + ph + 4, pw, ui.small
|
|
153
|
+
step = font.line_height + 2
|
|
154
|
+
listed = max(1, min(LISTED, (H - ry - step) // step))
|
|
155
|
+
rows = []
|
|
156
|
+
for i in range(listed):
|
|
157
|
+
r = ui.label(root, "", font, ui.WHITE)
|
|
158
|
+
r.set_pos(rx, ry + i * step)
|
|
159
|
+
r.set_width(rw)
|
|
160
|
+
r.set_long_mode(lv.label.LONG_MODE.CLIP)
|
|
161
|
+
rows.append(r)
|
|
162
|
+
note = ui.label(root, "", ui.small, ui.MUTED)
|
|
163
|
+
note.set_pos(rx, ry + listed * step + (8 if wide else 0))
|
|
164
|
+
speed = "%.1f frames a second, %d ms to look at each" if wide else "%.1f fps, %d ms a look"
|
|
165
|
+
note.set_width(rw)
|
|
166
|
+
|
|
167
|
+
def loop():
|
|
168
|
+
c = st["cam"]
|
|
169
|
+
if c is None:
|
|
170
|
+
return
|
|
171
|
+
if not pv.capture(c):
|
|
172
|
+
return
|
|
173
|
+
t = ticks_ms()
|
|
174
|
+
found = st["det"].detect(img)
|
|
175
|
+
st["ms"] = ticks_diff(ticks_ms(), t)
|
|
176
|
+
for x, y, w, h, score, cat in found:
|
|
177
|
+
col = _rgb565(WHEEL[cat % len(WHEEL)])
|
|
178
|
+
canvas.rect(x, y, w, h, col)
|
|
179
|
+
canvas.rect(x + 1, y + 1, w - 2, h - 2, col)
|
|
180
|
+
canvas.rect(x + 2, y + 2, w - 4, h - 4, col)
|
|
181
|
+
st["found"] = found
|
|
182
|
+
st["frames"] += 1
|
|
183
|
+
show()
|
|
184
|
+
|
|
185
|
+
def tick(t=None):
|
|
186
|
+
found = sorted(st["found"], key=lambda f: -f[4])
|
|
187
|
+
for i, r in enumerate(rows):
|
|
188
|
+
if i < len(found):
|
|
189
|
+
cat, score = found[i][5], found[i][4]
|
|
190
|
+
name = COCO[cat] if cat < len(COCO) else "class %d" % cat
|
|
191
|
+
r.set_text("%s %d%%" % (name, int(score * 100 + 0.5)))
|
|
192
|
+
r.set_style_text_color(lv.color_hex(WHEEL[cat % len(WHEEL)]), 0)
|
|
193
|
+
else:
|
|
194
|
+
r.set_text("")
|
|
195
|
+
if not found:
|
|
196
|
+
rows[0].set_text("nothing I know")
|
|
197
|
+
rows[0].set_style_text_color(ui.MUTED, 0)
|
|
198
|
+
dt = ticks_diff(ticks_ms(), st["t0"]) or 1
|
|
199
|
+
note.set_text(speed % (st["frames"] * 1000 / dt, st["ms"]))
|
|
200
|
+
st["frames"] = 0
|
|
201
|
+
st["t0"] = ticks_ms()
|
|
202
|
+
|
|
203
|
+
def close():
|
|
204
|
+
c, st["cam"] = st["cam"], None
|
|
205
|
+
if c is not None:
|
|
206
|
+
c.deinit()
|
|
207
|
+
dd, st["det"] = st["det"], None
|
|
208
|
+
if dd is not None:
|
|
209
|
+
dd.deinit()
|
|
210
|
+
|
|
211
|
+
scope.on_close(close)
|
|
212
|
+
scope.every(500, tick)
|
|
213
|
+
status.set_text("%s, 80 COCO classes" % getattr(det, "model", "YOLO11n"))
|
|
214
|
+
scope.foreground(loop)
|
apps/detect/app.json
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"name": "detect", "title": "Object detection", "category": "Video", "icon": "image_search", "color": "#3B6E2A", "bar": "none"}
|
apps/fmsynth/__init__.py
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
# SPDX-FileCopyrightText: 2026 Brad Barnett
|
|
2
|
+
#
|
|
3
|
+
# SPDX-License-Identifier: MIT
|
|
4
|
+
"""fmsynth: a six-operator FM synthesizer app. appkit calls ``main(scope)``.
|
|
5
|
+
|
|
6
|
+
Importing this does nothing and imports nothing else: the app's modules
|
|
7
|
+
load when it opens.
|
|
8
|
+
"""
|
|
9
|
+
|
|
10
|
+
__all__ = ["main", "SCREEN"]
|
|
11
|
+
|
|
12
|
+
#: The smallest screen the panel is made for, as ``(width, height)``; the
|
|
13
|
+
#: ``"screen"`` in ``app.json`` says the same, so Home leaves the app out on a
|
|
14
|
+
#: smaller one.
|
|
15
|
+
SCREEN = (800, 480)
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
def main(scope):
|
|
19
|
+
"""Run fmsynth on *scope*; returns the running app.
|
|
20
|
+
|
|
21
|
+
On a display smaller than `SCREEN` it opens no panel: it says what it
|
|
22
|
+
needs in the middle of the screen, prints the same, and returns None.
|
|
23
|
+
"""
|
|
24
|
+
import lvgl as lv
|
|
25
|
+
|
|
26
|
+
disp = lv.display_get_default()
|
|
27
|
+
if disp is not None:
|
|
28
|
+
w, h = disp.get_horizontal_resolution(), disp.get_vertical_resolution()
|
|
29
|
+
if w < SCREEN[0] or h < SCREEN[1]:
|
|
30
|
+
text = "FM Synth needs a screen of at least %d x %d; this one is %d x %d." % (SCREEN + (w, h))
|
|
31
|
+
print("fmsynth:", text)
|
|
32
|
+
msg = lv.label(scope.root)
|
|
33
|
+
msg.set_text(text)
|
|
34
|
+
msg.set_long_mode(lv.label.LONG_MODE.WRAP)
|
|
35
|
+
msg.set_width(max(1, w - 32))
|
|
36
|
+
msg.set_style_text_align(lv.TEXT_ALIGN.CENTER, 0)
|
|
37
|
+
msg.align(lv.ALIGN.CENTER, 0, 0)
|
|
38
|
+
return None
|
|
39
|
+
from .app import App
|
|
40
|
+
|
|
41
|
+
return App(scope)
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
# SPDX-FileCopyrightText: 2026 Brad Barnett
|
|
2
|
+
#
|
|
3
|
+
# SPDX-License-Identifier: MIT
|
|
4
|
+
"""The 32 six-operator FM algorithms as graphs.
|
|
5
|
+
|
|
6
|
+
The table is copied from controlkit's ``examples/routing_algorithms.py``
|
|
7
|
+
(PyDevices/apps, MIT); see NOTICE. Each entry of ALGORITHMS is
|
|
8
|
+
``(edges, carriers, feedback)`` for operators 1 to 6. An edge ``(6, 5)``
|
|
9
|
+
means operator 6 modulates operator 5, the carriers are the operators that
|
|
10
|
+
are heard, and the feedback pair is the loop: ``(6, 6)`` is an operator that
|
|
11
|
+
feeds itself. ``graph(number)`` gives the four arguments a controlkit
|
|
12
|
+
`RoutingDiagram` takes.
|
|
13
|
+
"""
|
|
14
|
+
|
|
15
|
+
OPERATORS = (1, 2, 3, 4, 5, 6)
|
|
16
|
+
|
|
17
|
+
ALGORITHMS = (
|
|
18
|
+
(((2, 1), (6, 5), (5, 4), (4, 3)), (1, 3), (6, 6)), # 1
|
|
19
|
+
(((2, 1), (6, 5), (5, 4), (4, 3)), (1, 3), (2, 2)), # 2
|
|
20
|
+
(((3, 2), (2, 1), (6, 5), (5, 4)), (1, 4), (6, 6)), # 3
|
|
21
|
+
(((3, 2), (2, 1), (6, 5), (5, 4)), (1, 4), (4, 6)), # 4
|
|
22
|
+
(((2, 1), (4, 3), (6, 5)), (1, 3, 5), (6, 6)), # 5
|
|
23
|
+
(((2, 1), (4, 3), (6, 5)), (1, 3, 5), (5, 6)), # 6
|
|
24
|
+
(((2, 1), (4, 3), (5, 3), (6, 5)), (1, 3), (6, 6)), # 7
|
|
25
|
+
(((2, 1), (4, 3), (5, 3), (6, 5)), (1, 3), (4, 4)), # 8
|
|
26
|
+
(((2, 1), (4, 3), (5, 3), (6, 5)), (1, 3), (2, 2)), # 9
|
|
27
|
+
(((3, 2), (2, 1), (5, 4), (6, 4)), (1, 4), (3, 3)), # 10
|
|
28
|
+
(((3, 2), (2, 1), (5, 4), (6, 4)), (1, 4), (6, 6)), # 11
|
|
29
|
+
(((2, 1), (4, 3), (5, 3), (6, 3)), (1, 3), (2, 2)), # 12
|
|
30
|
+
(((2, 1), (4, 3), (5, 3), (6, 3)), (1, 3), (6, 6)), # 13
|
|
31
|
+
(((2, 1), (4, 3), (5, 4), (6, 4)), (1, 3), (6, 6)), # 14
|
|
32
|
+
(((2, 1), (4, 3), (5, 4), (6, 4)), (1, 3), (2, 2)), # 15
|
|
33
|
+
(((2, 1), (3, 1), (5, 1), (4, 3), (6, 5)), (1,), (6, 6)), # 16
|
|
34
|
+
(((2, 1), (3, 1), (5, 1), (4, 3), (6, 5)), (1,), (2, 2)), # 17
|
|
35
|
+
(((2, 1), (3, 1), (4, 1), (5, 4), (6, 5)), (1,), (3, 3)), # 18
|
|
36
|
+
(((3, 2), (2, 1), (6, 4), (6, 5)), (1, 4, 5), (6, 6)), # 19
|
|
37
|
+
(((3, 1), (3, 2), (5, 4), (6, 4)), (1, 2, 4), (3, 3)), # 20
|
|
38
|
+
(((3, 1), (3, 2), (6, 4), (6, 5)), (1, 2, 4, 5), (3, 3)), # 21
|
|
39
|
+
(((2, 1), (6, 3), (6, 4), (6, 5)), (1, 3, 4, 5), (6, 6)), # 22
|
|
40
|
+
(((3, 2), (6, 4), (6, 5)), (1, 2, 4, 5), (6, 6)), # 23
|
|
41
|
+
(((6, 3), (6, 4), (6, 5)), (1, 2, 3, 4, 5), (6, 6)), # 24
|
|
42
|
+
(((6, 4), (6, 5)), (1, 2, 3, 4, 5), (6, 6)), # 25
|
|
43
|
+
(((3, 2), (5, 4), (6, 4)), (1, 2, 4), (6, 6)), # 26
|
|
44
|
+
(((3, 2), (5, 4), (6, 4)), (1, 2, 4), (3, 3)), # 27
|
|
45
|
+
(((2, 1), (5, 4), (4, 3)), (1, 3, 6), (5, 5)), # 28
|
|
46
|
+
(((4, 3), (6, 5)), (1, 2, 3, 5), (6, 6)), # 29
|
|
47
|
+
(((5, 4), (4, 3)), (1, 2, 3, 6), (5, 5)), # 30
|
|
48
|
+
(((6, 5),), (1, 2, 3, 4, 5), (6, 6)), # 31
|
|
49
|
+
((), (1, 2, 3, 4, 5, 6), (6, 6)), # 32
|
|
50
|
+
)
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
def graph(number):
|
|
54
|
+
"""The nodes, edges, carriers and feedback of algorithm *number*, 1 to 32."""
|
|
55
|
+
edges, carriers, feedback = ALGORITHMS[number - 1]
|
|
56
|
+
return OPERATORS, edges, carriers, feedback
|
|
57
|
+
|
|
58
|
+
|
|
59
|
+
def carriers(number):
|
|
60
|
+
"""The operators that are heard in algorithm *number*, 1 to 32."""
|
|
61
|
+
return ALGORITHMS[number - 1][1]
|
apps/fmsynth/app.json
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"name": "fmsynth", "title": "FM Synth", "category": "Audio", "icon": "piano", "color": "#B5381F", "screen": [800, 480]}
|
apps/fmsynth/app.py
ADDED
|
@@ -0,0 +1,355 @@
|
|
|
1
|
+
# SPDX-FileCopyrightText: 2026 Brad Barnett
|
|
2
|
+
#
|
|
3
|
+
# SPDX-License-Identifier: MIT
|
|
4
|
+
"""The app: the panel, the engine behind it and the keyboards in front.
|
|
5
|
+
|
|
6
|
+
`App` opens the bank, builds the panel and starts the engine, then plays
|
|
7
|
+
what every MIDI keyboard the program can reach sends: notes with their
|
|
8
|
+
velocity, pitch bend, the mod wheel, the sustain pedal, program changes to
|
|
9
|
+
pick a voice, and a 32-voice bank or a single voice as system exclusive.
|
|
10
|
+
|
|
11
|
+
It listens on every MIDI channel, or on the one chosen with the header's
|
|
12
|
+
MIDI button. On a board with Bluetooth the same button turns Bluetooth MIDI
|
|
13
|
+
on: the board then advertises as a BLE-MIDI instrument a computer, a phone or
|
|
14
|
+
a controller connects to. The first device that plays it is remembered, and
|
|
15
|
+
from then on only that one may connect; BT NEW lets a different one in. The GLOBAL page's VOLUME knob sets how loud it
|
|
16
|
+
plays, with a limiter after it (see `engine`). The header also shows a light for each note that arrives, the
|
|
17
|
+
level going to the audio out, what rendering costs and the underruns.
|
|
18
|
+
|
|
19
|
+
The channel, Bluetooth, the volume and the bank and slot last played are
|
|
20
|
+
kept in the app's data folder and come back after a reboot (see `settings`).
|
|
21
|
+
"""
|
|
22
|
+
|
|
23
|
+
from . import meters as _meters
|
|
24
|
+
from . import midi as _midi
|
|
25
|
+
from . import panel_view
|
|
26
|
+
from . import settings as _settings
|
|
27
|
+
from .editor import Editor
|
|
28
|
+
from .engine import VOLUME_DEFAULT, VOLUME_MAX, VOLUME_MIN, Engine
|
|
29
|
+
|
|
30
|
+
#: How often the keyboards are read, in milliseconds.
|
|
31
|
+
MIDI_MS = 4
|
|
32
|
+
#: How often the settings are looked at, to be written once they settle.
|
|
33
|
+
SETTINGS_MS = 500
|
|
34
|
+
MOD_WHEEL_CC = 1
|
|
35
|
+
SUSTAIN_CC = 64
|
|
36
|
+
ALL_SOUND_OFF_CC = 120
|
|
37
|
+
ALL_NOTES_OFF_CC = 123
|
|
38
|
+
#: The channel choice that hears every channel.
|
|
39
|
+
OMNI = 0
|
|
40
|
+
CHANNELS = 16
|
|
41
|
+
|
|
42
|
+
#: The VOLUME knob's steps: 0 is off, then a dB a step from `VOLUME_MIN` up
|
|
43
|
+
#: to `VOLUME_MAX`.
|
|
44
|
+
VOLUME_STEPS = VOLUME_MAX - VOLUME_MIN + 1
|
|
45
|
+
VOLUME_STEP = VOLUME_DEFAULT - VOLUME_MIN + 1
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
def volume_db(step):
|
|
49
|
+
"""The volume in dB at the knob's *step*, or None for off."""
|
|
50
|
+
return None if step <= 0 else VOLUME_MIN + step - 1
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
def volume_text(step):
|
|
54
|
+
"""What the knob's legend and the display say at *step*: "+12dB", "OFF"."""
|
|
55
|
+
db = volume_db(step)
|
|
56
|
+
if db is None:
|
|
57
|
+
return "OFF"
|
|
58
|
+
return ("+%ddB" if db > 0 else "%ddB") % db
|
|
59
|
+
|
|
60
|
+
|
|
61
|
+
#: The running app, for a console or a bench script: ``current.editor`` is
|
|
62
|
+
#: what the panel edits, ``current.engine`` the sound, and
|
|
63
|
+
#: ``current.parser.feed()`` plays MIDI bytes. None while the app isn't open.
|
|
64
|
+
current = None
|
|
65
|
+
|
|
66
|
+
|
|
67
|
+
def _folder(path):
|
|
68
|
+
path = path.replace("\\", "/")
|
|
69
|
+
return path.rsplit("/", 1)[0] if "/" in path else "."
|
|
70
|
+
|
|
71
|
+
|
|
72
|
+
class App:
|
|
73
|
+
"""The whole app on *scope*. ``main(scope)`` makes one."""
|
|
74
|
+
|
|
75
|
+
def __init__(self, scope):
|
|
76
|
+
global current
|
|
77
|
+
self.scope = scope
|
|
78
|
+
self.inputs = None
|
|
79
|
+
#: Kept while the program runs, so the choice is still there when the
|
|
80
|
+
#: app is opened again; the first time it opens, from the settings file.
|
|
81
|
+
store = getattr(scope, "store", None)
|
|
82
|
+
self.prefs = store() if store is not None else {}
|
|
83
|
+
save_folder = getattr(scope, "data_dir", None)
|
|
84
|
+
self.settings = _settings.Settings(save_folder)
|
|
85
|
+
if not self.prefs.get("loaded"):
|
|
86
|
+
kept = self.settings.load()
|
|
87
|
+
self.prefs.update(kept)
|
|
88
|
+
if kept["volume"] is None:
|
|
89
|
+
del self.prefs["volume"]
|
|
90
|
+
self.prefs["loaded"] = True
|
|
91
|
+
#: The MIDI channel listened to, 1 to 16, or `OMNI` for all of them.
|
|
92
|
+
self.channel = self.prefs.get("channel", OMNI)
|
|
93
|
+
if not isinstance(self.channel, int) or not 0 <= self.channel <= CHANNELS:
|
|
94
|
+
self.channel = OMNI
|
|
95
|
+
#: The VOLUME knob's step, 0 to `VOLUME_STEPS`.
|
|
96
|
+
self.volume = self.prefs.get("volume", VOLUME_STEP)
|
|
97
|
+
if not isinstance(self.volume, int) or not 0 <= self.volume <= VOLUME_STEPS:
|
|
98
|
+
self.volume = VOLUME_STEP
|
|
99
|
+
#: Whether this board can take MIDI over Bluetooth, and whether the
|
|
100
|
+
#: player has turned that on.
|
|
101
|
+
self.ble_ok = _midi.ble_available()
|
|
102
|
+
self._ble_started = False
|
|
103
|
+
#: ``(address,)`` of the device BT NEW dropped, turned away until
|
|
104
|
+
#: another is remembered, or None.
|
|
105
|
+
self._ble_turned_away = None
|
|
106
|
+
self._ble_refused = 0
|
|
107
|
+
self._light = _meters.Light()
|
|
108
|
+
self._bar = _meters.Bar()
|
|
109
|
+
self._ignored = False
|
|
110
|
+
self._status = None
|
|
111
|
+
here = _folder(__file__)
|
|
112
|
+
# Banks are looked for in the app's data folder, in a patches folder
|
|
113
|
+
# inside the package, and in the one beside it in a checkout.
|
|
114
|
+
folders = [here + "/patches", _folder(here) + "/patches"]
|
|
115
|
+
self.engine = Engine(scope)
|
|
116
|
+
self.engine.set_volume(volume_db(self.volume))
|
|
117
|
+
self.editor = Editor(self.engine, folders, save_folder)
|
|
118
|
+
self._open_bank()
|
|
119
|
+
self.engine.start()
|
|
120
|
+
if self.engine.why:
|
|
121
|
+
print("fmsynth: %s" % (self.engine.why,))
|
|
122
|
+
text = self.engine.why or _meters.status_text(None, 0)
|
|
123
|
+
self.panel = panel_view.build(self.editor, screen=scope.root, engine_text=text,
|
|
124
|
+
engine_ok=self.engine.why is None, channel=self.channel,
|
|
125
|
+
on_channel=self.set_channel,
|
|
126
|
+
volume=self.volume, volume_steps=VOLUME_STEPS,
|
|
127
|
+
volume_default=VOLUME_STEP, volume_text=volume_text,
|
|
128
|
+
on_volume=self.set_volume,
|
|
129
|
+
bluetooth=bool(self.prefs.get("ble")) if self.ble_ok else None,
|
|
130
|
+
on_bluetooth=self.set_ble, on_bluetooth_new=self.new_ble_device)
|
|
131
|
+
self.parser = _midi.Parser(self)
|
|
132
|
+
self._timer = scope.every(MIDI_MS, self._midi_tick)
|
|
133
|
+
self._meter_timer = scope.every(_meters.TICK_MS, self._meter_tick)
|
|
134
|
+
self._status_timer = scope.every(_meters.STATUS_MS, self._status_tick)
|
|
135
|
+
self._settings_timer = scope.every(SETTINGS_MS, self._settings_tick)
|
|
136
|
+
scope.after(50, self._open_midi)
|
|
137
|
+
scope.on_close(self.close)
|
|
138
|
+
current = self
|
|
139
|
+
|
|
140
|
+
# ---- what is kept ---------------------------------------------------------------
|
|
141
|
+
|
|
142
|
+
def _open_bank(self):
|
|
143
|
+
"""The bank and slot last played, else the default bank's first voice."""
|
|
144
|
+
editor = self.editor
|
|
145
|
+
name = self.prefs.get("bank")
|
|
146
|
+
path = None
|
|
147
|
+
if isinstance(name, str):
|
|
148
|
+
path = dict(editor.files()).get(name)
|
|
149
|
+
if path is None:
|
|
150
|
+
print("fmsynth: the bank %s isn't here any more" % (name,))
|
|
151
|
+
try:
|
|
152
|
+
if path is not None:
|
|
153
|
+
editor.load(path)
|
|
154
|
+
else:
|
|
155
|
+
editor.open_default()
|
|
156
|
+
except (OSError, ValueError) as e:
|
|
157
|
+
print("fmsynth: no bank opened: %s" % (e,))
|
|
158
|
+
slot = self.prefs.get("slot")
|
|
159
|
+
if path is not None and isinstance(slot, int) and 0 < slot < 32:
|
|
160
|
+
editor.select(slot)
|
|
161
|
+
|
|
162
|
+
def kept(self):
|
|
163
|
+
"""What `settings` keeps, as it is now."""
|
|
164
|
+
return {"channel": self.channel, "ble": bool(self.prefs.get("ble")), "ble_peer": self.prefs.get("ble_peer"),
|
|
165
|
+
"volume": self.volume,
|
|
166
|
+
"bank": self.editor.file, "slot": self.editor.slot}
|
|
167
|
+
|
|
168
|
+
def _settings_tick(self, _timer=None):
|
|
169
|
+
self.prefs["bank"] = self.editor.file
|
|
170
|
+
self.prefs["slot"] = self.editor.slot
|
|
171
|
+
self.settings.update(self.kept())
|
|
172
|
+
|
|
173
|
+
# ---- MIDI ---------------------------------------------------------------------
|
|
174
|
+
|
|
175
|
+
def _open_midi(self, _timer=None):
|
|
176
|
+
self.inputs = _midi.Inputs().open()
|
|
177
|
+
if self.inputs.why:
|
|
178
|
+
print("fmsynth: %s" % (self.inputs.why,))
|
|
179
|
+
else:
|
|
180
|
+
print("fmsynth: MIDI in from", ", ".join(self.inputs.names()) or self.inputs.kind)
|
|
181
|
+
if self.ble_ok and self.prefs.get("ble") and not self._ble_started:
|
|
182
|
+
# Left on the last time the app was open.
|
|
183
|
+
self.set_ble(True)
|
|
184
|
+
|
|
185
|
+
def set_ble(self, on):
|
|
186
|
+
"""Turn Bluetooth MIDI on or off, and remember it for the next time
|
|
187
|
+
the app opens. The board advertises as `midi.BLE_NAME`; what arrives
|
|
188
|
+
is played exactly as from a USB keyboard. A failure to start is
|
|
189
|
+
printed and shown on the display."""
|
|
190
|
+
inputs = self.inputs
|
|
191
|
+
if inputs is None or not self.ble_ok:
|
|
192
|
+
return
|
|
193
|
+
self.prefs["ble"] = bool(on)
|
|
194
|
+
self._ble_started = True
|
|
195
|
+
text = "BLUETOOTH ON" if on else "BLUETOOTH OFF"
|
|
196
|
+
try:
|
|
197
|
+
inputs.set_ble(on, self.parser, accept=self.ble_accept)
|
|
198
|
+
except Exception as e:
|
|
199
|
+
print("fmsynth: Bluetooth MIDI didn't start:", e)
|
|
200
|
+
self.prefs["ble"] = False
|
|
201
|
+
text = "NO BLUETOOTH"
|
|
202
|
+
self.panel.set_bluetooth(inputs.ble is not None, text)
|
|
203
|
+
self._status = None
|
|
204
|
+
self._status_tick()
|
|
205
|
+
|
|
206
|
+
def ble_accept(self, address):
|
|
207
|
+
"""Whether the device at *address* may play over Bluetooth: the one
|
|
208
|
+
remembered, or while none is, the first that connects, which is then
|
|
209
|
+
remembered. Some controllers connect by themselves to any BLE-MIDI
|
|
210
|
+
instrument in range; this keeps them from taking the link.
|
|
211
|
+
|
|
212
|
+
A device whose address the radio didn't give (*address* None) can't be
|
|
213
|
+
remembered, so it's always turned away. The ones that connect by
|
|
214
|
+
themselves often arrive that way.
|
|
215
|
+
"""
|
|
216
|
+
if address is None:
|
|
217
|
+
return False
|
|
218
|
+
peer = self.prefs.get("ble_peer")
|
|
219
|
+
if peer is not None:
|
|
220
|
+
return address == peer
|
|
221
|
+
if self._ble_turned_away is not None and address == self._ble_turned_away[0]:
|
|
222
|
+
return False
|
|
223
|
+
self.prefs["ble_peer"] = address
|
|
224
|
+
self._ble_turned_away = None
|
|
225
|
+
return True
|
|
226
|
+
|
|
227
|
+
def new_ble_device(self):
|
|
228
|
+
"""Let a different controller in: forget the one remembered, drop the
|
|
229
|
+
one connected and turn it away, and remember the next that connects."""
|
|
230
|
+
inputs = self.inputs
|
|
231
|
+
if inputs is None or not self.ble_ok:
|
|
232
|
+
return
|
|
233
|
+
link = inputs.ble
|
|
234
|
+
if link is not None and link.state == "connected":
|
|
235
|
+
self._ble_turned_away = (link.peer,)
|
|
236
|
+
self.prefs["ble_peer"] = None
|
|
237
|
+
if link is not None:
|
|
238
|
+
inputs.set_ble(False, self.parser)
|
|
239
|
+
self.set_ble(True)
|
|
240
|
+
if inputs.ble is not None:
|
|
241
|
+
self.panel.flash("BT NEW DEVICE")
|
|
242
|
+
|
|
243
|
+
def _midi_tick(self, _timer=None):
|
|
244
|
+
if self.inputs is not None:
|
|
245
|
+
self.inputs.poll(self.parser)
|
|
246
|
+
|
|
247
|
+
def hears(self, channel):
|
|
248
|
+
"""True when MIDI channel *channel*, counted from 0, is listened to."""
|
|
249
|
+
return self.channel == OMNI or channel == self.channel - 1
|
|
250
|
+
|
|
251
|
+
def set_channel(self, channel):
|
|
252
|
+
"""Listen on MIDI channel *channel*, 1 to 16, or on all with `OMNI`.
|
|
253
|
+
|
|
254
|
+
Whatever sounds is released: its note off may come on a channel no
|
|
255
|
+
longer heard.
|
|
256
|
+
"""
|
|
257
|
+
if not 0 <= channel <= CHANNELS:
|
|
258
|
+
raise ValueError("a MIDI channel is 1 to 16, or 0 for all")
|
|
259
|
+
if channel == self.channel:
|
|
260
|
+
return
|
|
261
|
+
self.channel = channel
|
|
262
|
+
self.prefs["channel"] = channel
|
|
263
|
+
self.engine.sustain(False)
|
|
264
|
+
self.engine.release_all()
|
|
265
|
+
self.panel.set_channel(channel)
|
|
266
|
+
|
|
267
|
+
def set_volume(self, step):
|
|
268
|
+
"""Set the volume to the knob's *step*: 0 is off, `VOLUME_STEPS` the
|
|
269
|
+
most. It is remembered like the channel."""
|
|
270
|
+
if not 0 <= step <= VOLUME_STEPS:
|
|
271
|
+
raise ValueError("the volume's steps are 0 to %d" % VOLUME_STEPS)
|
|
272
|
+
self.volume = step
|
|
273
|
+
self.prefs["volume"] = step
|
|
274
|
+
self.engine.set_volume(volume_db(step))
|
|
275
|
+
self.panel.set_volume(step)
|
|
276
|
+
|
|
277
|
+
def note_on(self, note, velocity, channel=0):
|
|
278
|
+
# The light shows every note that arrives, in another colour when it
|
|
279
|
+
# came on a channel that isn't listened to.
|
|
280
|
+
heard = self.hears(channel)
|
|
281
|
+
self._light.strike(velocity)
|
|
282
|
+
self._ignored = not heard
|
|
283
|
+
if heard:
|
|
284
|
+
self.engine.press(note, velocity)
|
|
285
|
+
|
|
286
|
+
def note_off(self, note, channel=0):
|
|
287
|
+
if self.hears(channel):
|
|
288
|
+
self.engine.release(note)
|
|
289
|
+
|
|
290
|
+
def control(self, controller, value, channel=0):
|
|
291
|
+
if not self.hears(channel):
|
|
292
|
+
return
|
|
293
|
+
if controller == MOD_WHEEL_CC:
|
|
294
|
+
self.engine.mod_wheel(value / 127)
|
|
295
|
+
elif controller == SUSTAIN_CC:
|
|
296
|
+
self.engine.sustain(value >= 64)
|
|
297
|
+
elif controller in (ALL_SOUND_OFF_CC, ALL_NOTES_OFF_CC):
|
|
298
|
+
self.engine.release_all()
|
|
299
|
+
|
|
300
|
+
def bend(self, value, channel=0):
|
|
301
|
+
if not self.hears(channel):
|
|
302
|
+
return
|
|
303
|
+
self.engine.pitch_bend((value - 8192) / 8192)
|
|
304
|
+
|
|
305
|
+
def program(self, number, channel=0):
|
|
306
|
+
if not self.hears(channel):
|
|
307
|
+
return
|
|
308
|
+
self.editor.select(number % 32, tell=True)
|
|
309
|
+
|
|
310
|
+
def sysex(self, message):
|
|
311
|
+
try:
|
|
312
|
+
self.editor.load_sysex(message)
|
|
313
|
+
except ValueError as e:
|
|
314
|
+
print("fmsynth: %s" % (e,))
|
|
315
|
+
self.panel.flash("BAD CHECKSUM")
|
|
316
|
+
|
|
317
|
+
# ---- the meter, the light and the readout ------------------------------------------
|
|
318
|
+
|
|
319
|
+
def _meter_tick(self, _timer=None):
|
|
320
|
+
panel = self.panel
|
|
321
|
+
panel.set_meter(self._bar.tick(self.engine.level()) / _meters.BAR_TOP)
|
|
322
|
+
panel.set_midi_light(self._light.tick() > 0, not self._ignored)
|
|
323
|
+
|
|
324
|
+
def _status_tick(self, _timer=None):
|
|
325
|
+
if self.engine.why:
|
|
326
|
+
return
|
|
327
|
+
load, _worst, underruns = self.engine.load()
|
|
328
|
+
text = _meters.status_text(load, underruns)
|
|
329
|
+
ble = self.inputs.ble_text() if self.inputs is not None else None
|
|
330
|
+
refused = self.inputs.ble_refused() if self.inputs is not None else 0
|
|
331
|
+
if refused > self._ble_refused:
|
|
332
|
+
self.panel.flash("BT REFUSED")
|
|
333
|
+
self._ble_refused = refused
|
|
334
|
+
if ble and text.startswith("LOAD "):
|
|
335
|
+
# The small print has room for a dozen letters: with Bluetooth on,
|
|
336
|
+
# its state stands where the word LOAD was.
|
|
337
|
+
text = ble + text[4:]
|
|
338
|
+
if text != self._status:
|
|
339
|
+
self._status = text
|
|
340
|
+
self.panel.set_status(text)
|
|
341
|
+
|
|
342
|
+
# ---- closing ------------------------------------------------------------------
|
|
343
|
+
|
|
344
|
+
def close(self):
|
|
345
|
+
global current
|
|
346
|
+
if current is self:
|
|
347
|
+
current = None
|
|
348
|
+
if self.inputs is not None:
|
|
349
|
+
self.inputs.close()
|
|
350
|
+
self.inputs = None
|
|
351
|
+
self.engine.release_all()
|
|
352
|
+
self.engine.close()
|
|
353
|
+
self._settings_tick()
|
|
354
|
+
self.settings.flush(self.kept())
|
|
355
|
+
self.editor.on_change = None
|