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/wake/__init__.py
ADDED
|
@@ -0,0 +1,268 @@
|
|
|
1
|
+
# SPDX-FileCopyrightText: 2026 Brad Barnett
|
|
2
|
+
#
|
|
3
|
+
# SPDX-License-Identifier: MIT
|
|
4
|
+
"""Wake word: listens for "Hey Jarvis" and flashes on each detection.
|
|
5
|
+
|
|
6
|
+
It listens on whatever microphone the device is set to, the one chosen
|
|
7
|
+
under Audio in, or the board's own microphones when nothing is chosen. A
|
|
8
|
+
USB mic chosen there is heard as soon as it's plugged in. The input is
|
|
9
|
+
asked for the model's own 16 kHz, mono; one without 16 kHz is taken at 48,
|
|
10
|
+
32 or 24 kHz and filtered down (``feed.py``). It needs firmware built with
|
|
11
|
+
``tflite`` and audiodsp's ``audiofeatures``, and omnipython's
|
|
12
|
+
``medialink``; a USB mic also needs ``usb-host-audio``.
|
|
13
|
+
The model, ``hey_jarvis.json``, loads from a ``models`` folder: the SD card's
|
|
14
|
+
``/sd/models``, then ``/models`` on flash, then ``/data/models``
|
|
15
|
+
(`appkit.places`). The card is mounted read-only and unmounted again at
|
|
16
|
+
once: the model is read in before that. ``hey_jarvis.json`` and
|
|
17
|
+
``hey_jarvis.tflite`` are downloaded from ESPHome's micro-wake-word-models; the
|
|
18
|
+
links are at https://github.com/PyDevices/apps/blob/main/docs/reference/models.md
|
|
19
|
+
"""
|
|
20
|
+
|
|
21
|
+
import lvgl as lv
|
|
22
|
+
from multimer import ticks_diff, ticks_ms
|
|
23
|
+
from appkit import places
|
|
24
|
+
from touchkit import ui
|
|
25
|
+
|
|
26
|
+
try:
|
|
27
|
+
from . import feed
|
|
28
|
+
except ImportError as e: # CPython without scipy: main() says so
|
|
29
|
+
feed = None
|
|
30
|
+
_feed_error = e
|
|
31
|
+
|
|
32
|
+
MODEL = "hey_jarvis.json"
|
|
33
|
+
MODELS = "models" # the folder name, on the card, flash and the data root
|
|
34
|
+
PHRASE = "Hey Jarvis"
|
|
35
|
+
FLASH_MS = 1500
|
|
36
|
+
TICK_MS = 20
|
|
37
|
+
READ_MS = 10 # audio per read
|
|
38
|
+
BUSY_MS = 12 # reading per tick, so a backlog can't hold the screen up
|
|
39
|
+
|
|
40
|
+
state = None # for the console
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
def _load_model():
|
|
44
|
+
"""The detector. OSError when no ``models`` folder has the model."""
|
|
45
|
+
from medialink.wakeword import MicroWakeWord
|
|
46
|
+
|
|
47
|
+
card = places.mount_card()
|
|
48
|
+
try:
|
|
49
|
+
path = places.find(MODELS, MODEL)
|
|
50
|
+
if path is None:
|
|
51
|
+
raise OSError("%s is in none of %s" % (MODEL, places.where_text(MODELS)))
|
|
52
|
+
return MicroWakeWord(path)
|
|
53
|
+
finally:
|
|
54
|
+
places.unmount_card(card)
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
def _input_name(chosen):
|
|
58
|
+
"""What the status line calls the input in use."""
|
|
59
|
+
for ep in chosen:
|
|
60
|
+
return ep.label
|
|
61
|
+
return "the built-in microphones"
|
|
62
|
+
|
|
63
|
+
|
|
64
|
+
def main(scope):
|
|
65
|
+
global state
|
|
66
|
+
try:
|
|
67
|
+
import audiofeatures # noqa: F401
|
|
68
|
+
import tflite # noqa: F401
|
|
69
|
+
from medialink.wakeword import MicroWakeWord # noqa: F401
|
|
70
|
+
|
|
71
|
+
if feed is None:
|
|
72
|
+
raise _feed_error
|
|
73
|
+
except ImportError as e:
|
|
74
|
+
ui.missing(
|
|
75
|
+
scope,
|
|
76
|
+
"missing: tflite, audiofeatures or medialink",
|
|
77
|
+
"Wake word needs firmware with tflite and audiofeatures, and the medialink package: %s." % (e,),
|
|
78
|
+
)
|
|
79
|
+
return
|
|
80
|
+
scr = scope.root
|
|
81
|
+
status = ui.status(scope) # first: on a narrow screen it takes a row from scope.size
|
|
82
|
+
status.set_text("starting")
|
|
83
|
+
W, H = scope.size
|
|
84
|
+
|
|
85
|
+
# The ring beside a column of text on a wide screen, above it on a tall
|
|
86
|
+
# or square one, both centred in the app's area.
|
|
87
|
+
wide = W * 3 >= H * 4
|
|
88
|
+
cramped = H < 110 # a 160x128 screen under the title row: a small ring, small type, fewer lines
|
|
89
|
+
if cramped:
|
|
90
|
+
d = max(36, min(H - 12, W // 4))
|
|
91
|
+
col_w = W - d - 20
|
|
92
|
+
elif wide:
|
|
93
|
+
d = max(80, min(260, H - 48, W * 2 // 5))
|
|
94
|
+
col_w = min(340, W - d - 64)
|
|
95
|
+
else:
|
|
96
|
+
col_w = W - 24
|
|
97
|
+
d = max(80, min(260, W - 48, H * 2 // 5))
|
|
98
|
+
big = ui.big if col_w >= 300 else ui.tiny if cramped else ui.font
|
|
99
|
+
body = ui.plain(lv.obj(scr))
|
|
100
|
+
body.set_size(W, H)
|
|
101
|
+
body.set_flex_flow(lv.FLEX_FLOW.ROW if wide else lv.FLEX_FLOW.COLUMN)
|
|
102
|
+
body.set_flex_align(lv.FLEX_ALIGN.CENTER, lv.FLEX_ALIGN.CENTER, lv.FLEX_ALIGN.CENTER)
|
|
103
|
+
body.set_style_pad_column(8 if cramped else 40, 0)
|
|
104
|
+
body.set_style_pad_row(16 if d >= 160 else 8, 0)
|
|
105
|
+
|
|
106
|
+
ring = lv.obj(body)
|
|
107
|
+
ring.set_size(d, d)
|
|
108
|
+
ring.set_style_radius(d // 2, 0)
|
|
109
|
+
ring.set_style_bg_color(ui.PANEL, 0)
|
|
110
|
+
ring.set_style_border_color(ui.ACCENT, 0)
|
|
111
|
+
ring.set_style_border_width(6 if d >= 160 else 2 if cramped else 4, 0)
|
|
112
|
+
ring.remove_flag(lv.obj.FLAG.SCROLLABLE)
|
|
113
|
+
icon = ui.label(ring, lv.SYMBOL.AUDIO, ui.huge if d >= 160 else ui.small if cramped else ui.big, ui.ACCENT)
|
|
114
|
+
icon.center()
|
|
115
|
+
|
|
116
|
+
col = ui.plain(lv.obj(body))
|
|
117
|
+
col.set_size(col_w, lv.SIZE_CONTENT)
|
|
118
|
+
col.set_flex_flow(lv.FLEX_FLOW.COLUMN)
|
|
119
|
+
col.set_flex_align(lv.FLEX_ALIGN.START, lv.FLEX_ALIGN.START if wide else lv.FLEX_ALIGN.CENTER, lv.FLEX_ALIGN.START)
|
|
120
|
+
col.set_style_pad_row(12 if big is ui.big else 2 if cramped else 4, 0)
|
|
121
|
+
align = lv.TEXT_ALIGN.LEFT if wide else lv.TEXT_ALIGN.CENTER
|
|
122
|
+
|
|
123
|
+
def line(text, font, color):
|
|
124
|
+
lbl = ui.label(col, text, font, color)
|
|
125
|
+
lbl.set_width(col_w)
|
|
126
|
+
lbl.set_style_text_align(align, 0)
|
|
127
|
+
return lbl
|
|
128
|
+
|
|
129
|
+
prompt = PHRASE if cramped else 'Say "%s"' % PHRASE # no room for the word Say
|
|
130
|
+
say = line(prompt, big, ui.WHITE)
|
|
131
|
+
count = line("Detections: 0", big, ui.WHITE)
|
|
132
|
+
last = line("", ui.font if big is ui.big else ui.tiny if cramped else ui.small, ui.MUTED)
|
|
133
|
+
bar = lv.bar(col)
|
|
134
|
+
bar.set_size(col_w, 24 if big is ui.big else 8 if cramped else 12)
|
|
135
|
+
bar.set_range(0, 100)
|
|
136
|
+
caption = line("model score", ui.small if big is ui.big else ui.tiny, ui.MUTED)
|
|
137
|
+
if cramped:
|
|
138
|
+
# No room for the caption, and the last line only when it has something to say.
|
|
139
|
+
caption.add_flag(lv.obj.FLAG.HIDDEN)
|
|
140
|
+
|
|
141
|
+
def tell(short, text, under=True):
|
|
142
|
+
"""The status line says *text*; on a cramped screen it says *short*,
|
|
143
|
+
which fits beside the title, and *text* goes under the ring, cut
|
|
144
|
+
to one line, unless *under* is False."""
|
|
145
|
+
if not cramped:
|
|
146
|
+
status.set_text(text)
|
|
147
|
+
return
|
|
148
|
+
status.set_text(short)
|
|
149
|
+
if under:
|
|
150
|
+
last.set_text(text)
|
|
151
|
+
last.remove_flag(lv.obj.FLAG.HIDDEN)
|
|
152
|
+
|
|
153
|
+
if cramped:
|
|
154
|
+
last.add_flag(lv.obj.FLAG.HIDDEN)
|
|
155
|
+
last.set_long_mode(lv.label.LONG_MODE.DOTS)
|
|
156
|
+
last.set_height(last.get_style_text_font(0).line_height)
|
|
157
|
+
|
|
158
|
+
st = state = {"mic": None, "feed": None, "ww": None, "buf": None, "hits": 0, "flash": None,
|
|
159
|
+
"t0": ticks_ms(), "best": 0.0, "chosen": [], "error": None}
|
|
160
|
+
|
|
161
|
+
def close_mic():
|
|
162
|
+
mic = st["mic"]
|
|
163
|
+
st["mic"] = None
|
|
164
|
+
if mic is not None:
|
|
165
|
+
try:
|
|
166
|
+
mic.close()
|
|
167
|
+
except Exception as e:
|
|
168
|
+
print("wake: mic close:", e)
|
|
169
|
+
|
|
170
|
+
def close():
|
|
171
|
+
close_mic()
|
|
172
|
+
st["ww"] = None
|
|
173
|
+
|
|
174
|
+
scope.on_close(close)
|
|
175
|
+
|
|
176
|
+
def open_mic():
|
|
177
|
+
# The input the device is set to: boarddev.pcm_in follows the
|
|
178
|
+
# Audio in choice, and is the board's own microphones without one.
|
|
179
|
+
import boarddev
|
|
180
|
+
from audiodev import AudioFormat
|
|
181
|
+
|
|
182
|
+
close_mic()
|
|
183
|
+
name = _input_name(st["chosen"])
|
|
184
|
+
try:
|
|
185
|
+
mic, fd = feed.open_input(boarddev.pcm_in, lambda rate: AudioFormat(rate, 1, 16))
|
|
186
|
+
mic.open()
|
|
187
|
+
except Exception as e: # noqa: BLE001 - say why on the screen, keep the app up
|
|
188
|
+
tell("can't listen", "can't listen on %s: %s" % (name, e))
|
|
189
|
+
print("wake: input:", repr(e))
|
|
190
|
+
return
|
|
191
|
+
st["mic"], st["feed"] = mic, fd
|
|
192
|
+
st["buf"] = bytearray(fd.rate // 1000 * READ_MS * 2)
|
|
193
|
+
st["t0"] = ticks_ms()
|
|
194
|
+
st["ww"].reset()
|
|
195
|
+
tell("listening", "listening on %s, %d kHz" % (name, fd.rate // 1000), under=False)
|
|
196
|
+
print("wake: listening on %s, %d kHz" % (name, fd.rate // 1000))
|
|
197
|
+
|
|
198
|
+
def moved(chosen):
|
|
199
|
+
st["chosen"] = chosen
|
|
200
|
+
if st["ww"] is not None:
|
|
201
|
+
open_mic()
|
|
202
|
+
|
|
203
|
+
def setup(t):
|
|
204
|
+
# One step per tick, so the screen draws between them.
|
|
205
|
+
t.deinit()
|
|
206
|
+
status.set_text("loading the model")
|
|
207
|
+
try:
|
|
208
|
+
st["ww"] = _load_model()
|
|
209
|
+
except Exception as e:
|
|
210
|
+
tell("no model", "no model: put %s in %s" % (MODEL, places.where_text(MODELS)))
|
|
211
|
+
# Again under the ring, where it wraps: a narrow status line cuts it short.
|
|
212
|
+
last.set_text("No model: put %s in %s." % (MODEL, places.where_text(MODELS)))
|
|
213
|
+
last.remove_flag(lv.obj.FLAG.HIDDEN)
|
|
214
|
+
print("wake: model:", e)
|
|
215
|
+
return
|
|
216
|
+
st["chosen"] = scope.follow("audio.in", moved)
|
|
217
|
+
open_mic()
|
|
218
|
+
scope.every(TICK_MS, listen)
|
|
219
|
+
scope.every(250, show)
|
|
220
|
+
|
|
221
|
+
def listen(t):
|
|
222
|
+
mic, ww, fd, buf = st["mic"], st["ww"], st["feed"], st["buf"]
|
|
223
|
+
if mic is None:
|
|
224
|
+
return
|
|
225
|
+
began = ticks_ms()
|
|
226
|
+
while ticks_diff(ticks_ms(), began) < BUSY_MS:
|
|
227
|
+
try:
|
|
228
|
+
n = mic.readinto(buf)
|
|
229
|
+
except OSError as e: # a USB mic pulled out, a stalled codec
|
|
230
|
+
tell("stopped", "%s stopped: %s" % (_input_name(st["chosen"]), e))
|
|
231
|
+
print("wake: read:", e)
|
|
232
|
+
close_mic()
|
|
233
|
+
return
|
|
234
|
+
if not n:
|
|
235
|
+
return
|
|
236
|
+
for pcm in fd.push(memoryview(buf)[:n]):
|
|
237
|
+
if ww.feed(pcm, feed.MODEL_RATE, 1):
|
|
238
|
+
hit()
|
|
239
|
+
if ww.probability > st["best"]:
|
|
240
|
+
st["best"] = ww.probability
|
|
241
|
+
|
|
242
|
+
def hit():
|
|
243
|
+
st["hits"] += 1
|
|
244
|
+
st["flash"] = ticks_ms()
|
|
245
|
+
secs = ticks_diff(ticks_ms(), st["t0"]) // 1000
|
|
246
|
+
print("wake: %s (#%d at %d s)" % (PHRASE, st["hits"], secs))
|
|
247
|
+
count.set_text("Detections: %d" % st["hits"])
|
|
248
|
+
last.set_text(("last at %d:%02d" if cramped else "last at %d:%02d after it started listening") % (secs // 60, secs % 60))
|
|
249
|
+
last.remove_flag(lv.obj.FLAG.HIDDEN)
|
|
250
|
+
ring.set_style_bg_color(ui.GOOD, 0)
|
|
251
|
+
ring.set_style_border_color(ui.WHITE, 0)
|
|
252
|
+
icon.set_text(lv.SYMBOL.OK)
|
|
253
|
+
icon.set_style_text_color(ui.WHITE, 0)
|
|
254
|
+
say.set_text("%s!" % PHRASE)
|
|
255
|
+
|
|
256
|
+
def show(t):
|
|
257
|
+
bar.set_value(int(st["best"] * 100), 0)
|
|
258
|
+
st["best"] = 0.0
|
|
259
|
+
f = st["flash"]
|
|
260
|
+
if f is not None and ticks_diff(ticks_ms(), f) > FLASH_MS:
|
|
261
|
+
st["flash"] = None
|
|
262
|
+
ring.set_style_bg_color(ui.PANEL, 0)
|
|
263
|
+
ring.set_style_border_color(ui.ACCENT, 0)
|
|
264
|
+
icon.set_text(lv.SYMBOL.AUDIO)
|
|
265
|
+
icon.set_style_text_color(ui.ACCENT, 0)
|
|
266
|
+
say.set_text(prompt)
|
|
267
|
+
|
|
268
|
+
scope.every(200, setup)
|
apps/wake/app.json
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"name": "wake", "title": "Wake word", "category": "Audio", "icon": "mic", "color": "#8C7A1E"}
|
apps/wake/feed.py
ADDED
|
@@ -0,0 +1,155 @@
|
|
|
1
|
+
# SPDX-FileCopyrightText: 2026 Brad Barnett
|
|
2
|
+
#
|
|
3
|
+
# SPDX-License-Identifier: MIT
|
|
4
|
+
"""A microphone's audio, brought to what the wake-word model takes:
|
|
5
|
+
16 kHz mono 16-bit.
|
|
6
|
+
|
|
7
|
+
``open_input`` asks the input it's given for the model's own rate first.
|
|
8
|
+
16 kHz goes straight in; a board's microphones and most USB mics have it,
|
|
9
|
+
and a stereo-only USB mic (a webcam's, often) is downmixed in C as the
|
|
10
|
+
packets land. An input without 16 kHz is opened at 48, 32 or 24 kHz, and
|
|
11
|
+
``Feed`` brings it down through a 6th-order elliptic low-pass at 7 kHz:
|
|
12
|
+
every third sample of 48 kHz, every second of 32 kHz, and 24 kHz with a
|
|
13
|
+
zero after each sample (48 kHz) and then every third. An input with none
|
|
14
|
+
of those (44.1 kHz, 8 kHz, 24-bit only) raises ``Unsupported``, which
|
|
15
|
+
names what it does offer when it says.
|
|
16
|
+
|
|
17
|
+
No LVGL or USB here, so a test drives it on CPython or MicroPython with a
|
|
18
|
+
stand-in input.
|
|
19
|
+
"""
|
|
20
|
+
|
|
21
|
+
try:
|
|
22
|
+
from ulab import numpy as np
|
|
23
|
+
from ulab import scipy as _spy
|
|
24
|
+
|
|
25
|
+
_sosfilt = _spy.signal.sosfilt
|
|
26
|
+
except ImportError: # CPython, for the tests
|
|
27
|
+
import numpy as np
|
|
28
|
+
from scipy.signal import sosfilt as _sosfilt
|
|
29
|
+
|
|
30
|
+
_FLOAT = getattr(np, "float64", None) or np.float # ulab has only np.float
|
|
31
|
+
|
|
32
|
+
MODEL_RATE = 16000
|
|
33
|
+
#: The rates the app takes, in the order it asks for them, with the
|
|
34
|
+
#: (up, down) that brings each to 16 kHz.
|
|
35
|
+
PLANS = ((16000, 1, 1), (48000, 1, 3), (32000, 1, 2), (24000, 2, 3))
|
|
36
|
+
|
|
37
|
+
# scipy.signal.ellip(6, 0.5, 60, 7000, fs=FS, output="sos"): passband to
|
|
38
|
+
# 7 kHz within 0.5 dB, 60 dB down above it, at the rate after any zeros.
|
|
39
|
+
_SOS = {
|
|
40
|
+
48000: (
|
|
41
|
+
(0.006933717795, 0.00939545639, 0.006933717795, 1.0, -1.367576716, 0.5231455731),
|
|
42
|
+
(1.0, -0.2072824888, 1.0, 1.0, -1.229819789, 0.7273085523),
|
|
43
|
+
(1.0, -0.6643733109, 1.0, 1.0, -1.157143099, 0.9194936469)),
|
|
44
|
+
32000: (
|
|
45
|
+
(0.02239589618, 0.03897715383, 0.02239589618, 1.0, -1.002252672, 0.3533378446),
|
|
46
|
+
(1.0, 0.7689714327, 1.0, 1.0, -0.6025153495, 0.6537500441),
|
|
47
|
+
(1.0, 0.3251754601, 1.0, 1.0, -0.3538671505, 0.9017749359)),
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
class Unsupported(ValueError):
|
|
52
|
+
"""The mic has no stream the app can use; the message says what it
|
|
53
|
+
offers."""
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+
def offers(text):
|
|
57
|
+
"""What a mic offers, in words, from usb-host-audio's ``ValueError``
|
|
58
|
+
("... it offers: itf 1 alt 1: in 16000/32000 Hz x 2ch x 16bit, ep ...").
|
|
59
|
+
Only its inputs; an empty string when it lists none."""
|
|
60
|
+
seen = []
|
|
61
|
+
for chunk in text.split("itf ")[1:]:
|
|
62
|
+
if ": " not in chunk:
|
|
63
|
+
continue
|
|
64
|
+
what = chunk.split(": ", 1)[1].split(", ep", 1)[0].strip()
|
|
65
|
+
if not what.startswith("in "):
|
|
66
|
+
continue
|
|
67
|
+
parts = what[3:].split(" x ")
|
|
68
|
+
if len(parts) != 3:
|
|
69
|
+
continue
|
|
70
|
+
rates = []
|
|
71
|
+
for r in parts[0].replace(" Hz", "").split("/"):
|
|
72
|
+
try:
|
|
73
|
+
hz = int(r)
|
|
74
|
+
except ValueError:
|
|
75
|
+
rates.append(r)
|
|
76
|
+
continue
|
|
77
|
+
rates.append(("%d" % (hz // 1000)) if hz % 1000 == 0 else ("%.1f" % (hz / 1000)))
|
|
78
|
+
ch = parts[1].replace("ch", "")
|
|
79
|
+
ch = {"1": "mono", "2": "stereo"}.get(ch, ch + " channels")
|
|
80
|
+
said = "%s kHz %s %s-bit" % ("/".join(rates), ch, parts[2].replace("bit", ""))
|
|
81
|
+
if said not in seen:
|
|
82
|
+
seen.append(said)
|
|
83
|
+
return ", ".join(seen)
|
|
84
|
+
|
|
85
|
+
|
|
86
|
+
def open_input(pcm_in, format_of):
|
|
87
|
+
"""``(mic, feed)``: the input ``pcm_in(format)`` opens at the first rate
|
|
88
|
+
of ``PLANS`` it takes, 16-bit mono, and the ``Feed`` for that rate.
|
|
89
|
+
``format_of(rate)`` makes the format (audiodev's ``AudioFormat(rate, 1,
|
|
90
|
+
16)``). Raises ``Unsupported`` when it takes none of them."""
|
|
91
|
+
last = ""
|
|
92
|
+
for rate, _, _ in PLANS:
|
|
93
|
+
try:
|
|
94
|
+
mic = pcm_in(format_of(rate))
|
|
95
|
+
except ValueError as e:
|
|
96
|
+
last = str(e)
|
|
97
|
+
continue
|
|
98
|
+
return mic, Feed(rate)
|
|
99
|
+
said = offers(last)
|
|
100
|
+
raise Unsupported("this microphone offers %s; the wake word takes 16, 24, 32 or 48 kHz 16-bit"
|
|
101
|
+
% (said or "none of those"))
|
|
102
|
+
|
|
103
|
+
|
|
104
|
+
class Feed:
|
|
105
|
+
"""Bytes from the mic in (``push``), 16 kHz int16 out, as a list of
|
|
106
|
+
bytes objects for ``ww.process``. At 16 kHz what goes in comes out,
|
|
107
|
+
whole samples at a time; otherwise in 10 ms blocks, the filter's state
|
|
108
|
+
kept from one to the next."""
|
|
109
|
+
|
|
110
|
+
def __init__(self, rate):
|
|
111
|
+
for r, up, down in PLANS:
|
|
112
|
+
if r == rate:
|
|
113
|
+
break
|
|
114
|
+
else:
|
|
115
|
+
raise ValueError("no plan for %d Hz" % rate)
|
|
116
|
+
self.rate, self.up, self.down = rate, up, down
|
|
117
|
+
self.block = rate // 100 * 2 # 10 ms of input, in bytes
|
|
118
|
+
self._pending = bytearray()
|
|
119
|
+
if up == down:
|
|
120
|
+
self._sos = None
|
|
121
|
+
else:
|
|
122
|
+
self._sos = np.array(_SOS[rate * up])
|
|
123
|
+
self._zi = np.zeros((3, 2))
|
|
124
|
+
|
|
125
|
+
def push(self, data):
|
|
126
|
+
out = []
|
|
127
|
+
if self._sos is None:
|
|
128
|
+
p = self._pending
|
|
129
|
+
if p: # half a sample left from the last push
|
|
130
|
+
p += data
|
|
131
|
+
data = p
|
|
132
|
+
n = len(data) & ~1
|
|
133
|
+
if n:
|
|
134
|
+
out.append(bytes(data[:n]))
|
|
135
|
+
self._pending = bytearray(data[n:])
|
|
136
|
+
return out
|
|
137
|
+
p = self._pending
|
|
138
|
+
p += data
|
|
139
|
+
block = self.block
|
|
140
|
+
at = 0
|
|
141
|
+
while len(p) - at >= block:
|
|
142
|
+
out.append(self._convert(p[at:at + block]))
|
|
143
|
+
at += block
|
|
144
|
+
self._pending = p[at:] if at else p
|
|
145
|
+
return out
|
|
146
|
+
|
|
147
|
+
def _convert(self, raw):
|
|
148
|
+
x = np.array(np.frombuffer(raw, dtype=np.int16), dtype=_FLOAT)
|
|
149
|
+
if self.up > 1:
|
|
150
|
+
z = np.zeros(len(x) * self.up, dtype=_FLOAT)
|
|
151
|
+
z[::self.up] = x * self.up # keeps the level after the zeros
|
|
152
|
+
x = z
|
|
153
|
+
y, self._zi = _sosfilt(self._sos, x, zi=self._zi)
|
|
154
|
+
y = np.clip(y[::self.down], -32768, 32767)
|
|
155
|
+
return np.array(y, dtype=np.int16).tobytes()
|