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.
Files changed (218) hide show
  1. apps/__init__.py +5 -0
  2. apps/about/__init__.py +257 -0
  3. apps/about/app.json +1 -0
  4. apps/about/credits.py +12 -0
  5. apps/about/facts.py +398 -0
  6. apps/about/fonts/OFL.txt +93 -0
  7. apps/about/fonts/latin1_14.bin +0 -0
  8. apps/about/fonts/latin1_16.bin +0 -0
  9. apps/about/fonts/latin1_24.bin +0 -0
  10. apps/about/fonts/latin1_32.bin +0 -0
  11. apps/about/fonts/latin1_40.bin +0 -0
  12. apps/backline/__init__.py +58 -0
  13. apps/backline/app.json +1 -0
  14. apps/backline/app.py +475 -0
  15. apps/backline/art.py +717 -0
  16. apps/backline/ask.py +781 -0
  17. apps/backline/box.py +502 -0
  18. apps/backline/cast.py +137 -0
  19. apps/backline/desk/__init__.py +329 -0
  20. apps/backline/desk/editor.py +331 -0
  21. apps/backline/desk/grid.py +154 -0
  22. apps/backline/desk/look.py +169 -0
  23. apps/backline/desk/pedals.py +102 -0
  24. apps/backline/desk/perform.py +157 -0
  25. apps/backline/desk/prompt.py +210 -0
  26. apps/backline/desk/strip.py +159 -0
  27. apps/backline/desk_main.py +226 -0
  28. apps/backline/engine.py +622 -0
  29. apps/backline/floor.py +509 -0
  30. apps/backline/footswitch.py +119 -0
  31. apps/backline/glyphs.py +82 -0
  32. apps/backline/kits.py +428 -0
  33. apps/backline/midipedal.py +362 -0
  34. apps/backline/page_data.py +413 -0
  35. apps/backline/parts.py +145 -0
  36. apps/backline/pedal.py +107 -0
  37. apps/backline/remote.py +134 -0
  38. apps/backline/songs/ballad_68.json +1 -0
  39. apps/backline/songs/bossa.json +1 -0
  40. apps/backline/songs/country_train.json +1 -0
  41. apps/backline/songs/funk.json +1 -0
  42. apps/backline/songs/metal.json +1 -0
  43. apps/backline/songs/pop.json +1 -0
  44. apps/backline/songs/rock.json +1 -0
  45. apps/backline/songs/shuffle.json +1 -0
  46. apps/battery/__init__.py +101 -0
  47. apps/battery/app.json +1 -0
  48. apps/bit_world/act_deck.py +683 -0
  49. apps/bit_world/act_game.py +762 -0
  50. apps/bit_world/act_pet.py +1032 -0
  51. apps/bit_world/app.json +1 -0
  52. apps/bit_world/bit_output.py +45 -0
  53. apps/bit_world/bit_palette.py +503 -0
  54. apps/bit_world/bit_sound.py +431 -0
  55. apps/bit_world/bit_sprites.py +632 -0
  56. apps/bit_world/bit_world.py +534 -0
  57. apps/btreceiver/__init__.py +690 -0
  58. apps/btreceiver/app.json +1 -0
  59. apps/btreceiver/chime.py +155 -0
  60. apps/btreceiver/demo.py +529 -0
  61. apps/btreceiver/screen.py +633 -0
  62. apps/camera/__init__.py +316 -0
  63. apps/camera/app.json +1 -0
  64. apps/card/__init__.py +113 -0
  65. apps/card/app.json +1 -0
  66. apps/cast/__init__.py +631 -0
  67. apps/cast/app.json +1 -0
  68. apps/cast/receivers.py +193 -0
  69. apps/chess/__init__.py +18 -0
  70. apps/chess/app.json +1 -0
  71. apps/chess/app.py +922 -0
  72. apps/chess/art.py +323 -0
  73. apps/chess/ble.py +411 -0
  74. apps/chess/engine.py +925 -0
  75. apps/chess/game.py +327 -0
  76. apps/chess/net.py +261 -0
  77. apps/clock/__init__.py +38 -0
  78. apps/clock/app.json +1 -0
  79. apps/detect/__init__.py +214 -0
  80. apps/detect/app.json +1 -0
  81. apps/fmsynth/__init__.py +41 -0
  82. apps/fmsynth/algorithms.py +61 -0
  83. apps/fmsynth/app.json +1 -0
  84. apps/fmsynth/app.py +355 -0
  85. apps/fmsynth/editor.py +206 -0
  86. apps/fmsynth/engine.py +257 -0
  87. apps/fmsynth/meters.py +262 -0
  88. apps/fmsynth/midi.py +375 -0
  89. apps/fmsynth/panel_view.py +1071 -0
  90. apps/fmsynth/patches/fmsynth-01.syx +0 -0
  91. apps/fmsynth/settings.py +138 -0
  92. apps/fmsynth/voice.py +398 -0
  93. apps/gamepad/__init__.py +685 -0
  94. apps/gamepad/app.json +1 -0
  95. apps/gamepad/pad.py +94 -0
  96. apps/gamepad/watch.py +352 -0
  97. apps/hidden/__init__.py +72 -0
  98. apps/hidden/app.json +1 -0
  99. apps/home/__init__.py +483 -0
  100. apps/home/app.json +1 -0
  101. apps/home/main.py +36 -0
  102. apps/homeassistant/__init__.py +213 -0
  103. apps/homeassistant/api.py +104 -0
  104. apps/homeassistant/app.json +1 -0
  105. apps/huey/__init__.py +287 -0
  106. apps/huey/acting.py +184 -0
  107. apps/huey/actor.py +194 -0
  108. apps/huey/app.json +1 -0
  109. apps/huey/client.py +125 -0
  110. apps/huey/servers.py +121 -0
  111. apps/huey/settings.example.json +8 -0
  112. apps/huey/settings.py +97 -0
  113. apps/huey/turn.py +504 -0
  114. apps/huey/voice.py +217 -0
  115. apps/info/__init__.py +810 -0
  116. apps/info/app.json +1 -0
  117. apps/lora/__init__.py +62 -0
  118. apps/lora/app.json +1 -0
  119. apps/manifest.py +71 -0
  120. apps/medialink_baby_monitor/__init__.py +222 -0
  121. apps/medialink_baby_monitor/app.json +1 -0
  122. apps/medialink_doorbell/__init__.py +251 -0
  123. apps/medialink_doorbell/app.json +1 -0
  124. apps/medialink_hands_free/__init__.py +217 -0
  125. apps/medialink_hands_free/app.json +1 -0
  126. apps/medialink_surveillance_camera/__init__.py +258 -0
  127. apps/medialink_surveillance_camera/app.json +1 -0
  128. apps/medialink_video_doorbell/__init__.py +289 -0
  129. apps/medialink_video_doorbell/app.json +1 -0
  130. apps/medialink_videophone/__init__.py +273 -0
  131. apps/medialink_videophone/app.json +1 -0
  132. apps/medialink_walkie_talkie/__init__.py +228 -0
  133. apps/medialink_walkie_talkie/app.json +1 -0
  134. apps/midicontroller/__init__.py +774 -0
  135. apps/midicontroller/app.json +1 -0
  136. apps/midicontroller/links.py +421 -0
  137. apps/midicontroller/surface.py +343 -0
  138. apps/midiengine/__init__.py +977 -0
  139. apps/midiengine/app.json +1 -0
  140. apps/midiengine/banks.py +85 -0
  141. apps/midiengine/midiin.py +165 -0
  142. apps/midiengine/player.py +392 -0
  143. apps/midiengine/umidiparser.py +1413 -0
  144. apps/mp3/__init__.py +240 -0
  145. apps/mp3/app.json +1 -0
  146. apps/perch/__init__.py +119 -0
  147. apps/perch/app.json +1 -0
  148. apps/perch/huey.py +481 -0
  149. apps/perch/huey_props.py +386 -0
  150. apps/perch/huey_shift.py +42 -0
  151. apps/perch/huey_views.py +296 -0
  152. apps/perch/scene_bench.py +680 -0
  153. apps/perch/scene_coast.py +479 -0
  154. apps/perch/scene_desk.py +492 -0
  155. apps/perch/stage.py +1274 -0
  156. apps/perch/vec.py +366 -0
  157. apps/repl/__init__.py +325 -0
  158. apps/repl/app.json +1 -0
  159. apps/roku/__init__.py +156 -0
  160. apps/roku/app.json +1 -0
  161. apps/rokucam/__init__.py +362 -0
  162. apps/rokucam/app.json +1 -0
  163. apps/security/__init__.py +261 -0
  164. apps/security/app.json +1 -0
  165. apps/settings/__init__.py +221 -0
  166. apps/settings/app.json +1 -0
  167. apps/shell/__init__.py +33 -0
  168. apps/shell/app.json +1 -0
  169. apps/spotify/__init__.py +669 -0
  170. apps/spotify/app.json +1 -0
  171. apps/spotify/screen.py +82 -0
  172. apps/steps/__init__.py +58 -0
  173. apps/steps/app.json +1 -0
  174. apps/stompbox/__init__.py +31 -0
  175. apps/stompbox/app.json +1 -0
  176. apps/stompbox/app.py +668 -0
  177. apps/stompbox/boards.json +20 -0
  178. apps/stompbox/effects.py +241 -0
  179. apps/stompbox/engine.py +340 -0
  180. apps/stompbox/footswitch.py +331 -0
  181. apps/stompbox/inputs.py +449 -0
  182. apps/stompbox/meter.py +72 -0
  183. apps/stompbox/outputs.py +135 -0
  184. apps/stompbox/pedals.txt +45 -0
  185. apps/stompbox/presets.py +106 -0
  186. apps/stompbox/styles.py +135 -0
  187. apps/stompbox/ui.py +817 -0
  188. apps/stompbox/wavfile.py +73 -0
  189. apps/synthstation/__init__.py +20 -0
  190. apps/synthstation/app.json +1 -0
  191. apps/synthstation/audio.py +397 -0
  192. apps/synthstation/catalog.py +50 -0
  193. apps/synthstation/meters.py +136 -0
  194. apps/synthstation/midi.py +380 -0
  195. apps/synthstation/pads.py +177 -0
  196. apps/synthstation/parts.py +128 -0
  197. apps/synthstation/station.py +1069 -0
  198. apps/system/__init__.py +11 -0
  199. apps/system/app.json +1 -0
  200. apps/touchcal/__init__.py +324 -0
  201. apps/touchcal/app.json +1 -0
  202. apps/touchcal/fit.py +262 -0
  203. apps/wake/__init__.py +268 -0
  204. apps/wake/app.json +1 -0
  205. apps/wake/feed.py +155 -0
  206. apps/watch/__init__.py +355 -0
  207. apps/watch/app.json +1 -0
  208. apps/watch/faces.py +35 -0
  209. apps/watch/wheel.py +157 -0
  210. apps/weather/__init__.py +278 -0
  211. apps/weather/app.json +1 -0
  212. apps/weather/icons.py +172 -0
  213. apps/weather/sizing.py +71 -0
  214. apps/weather/zippad.py +172 -0
  215. omnipython_apps-0.1.4.dev8.dist-info/METADATA +33 -0
  216. omnipython_apps-0.1.4.dev8.dist-info/RECORD +218 -0
  217. omnipython_apps-0.1.4.dev8.dist-info/WHEEL +5 -0
  218. omnipython_apps-0.1.4.dev8.dist-info/top_level.txt +1 -0
apps/fmsynth/editor.py ADDED
@@ -0,0 +1,206 @@
1
+ # SPDX-FileCopyrightText: 2026 Brad Barnett
2
+ #
3
+ # SPDX-License-Identifier: MIT
4
+ """What the panel edits: a bank, the slot chosen from it, and the voice being changed.
5
+
6
+ The `Editor` keeps the 32 voices of a bank and an edit copy of one of them.
7
+ Every change goes to the edit copy and on to the engine, and the bank keeps
8
+ what it had until ``store()``. ``compare(True)`` plays and shows the stored
9
+ voice until it is turned off again. Banks are read from and written to
10
+ ``.syx`` files, and arrive over MIDI as system exclusive messages.
11
+
12
+ It draws nothing and imports no LVGL, so the panel, the MIDI input and a
13
+ test all drive the same object.
14
+ """
15
+
16
+ from appkit import assets
17
+
18
+ from .voice import BANK_SYSEX_SIZE, SINGLE_SYSEX_SIZE, VOICES, Bank, Voice
19
+
20
+ #: The bank opened at the start, when a folder has it.
21
+ DEFAULT_BANK = "fmsynth-01.syx"
22
+ #: The name a bank is saved under when it came from nowhere.
23
+ UNTITLED = "user-01.syx"
24
+
25
+
26
+ def _join(folder, name):
27
+ return name if not folder else (folder + name if folder.endswith("/") else folder + "/" + name)
28
+
29
+
30
+ class Editor:
31
+ """The bank, the slot and the edit copy, over *engine*.
32
+
33
+ *folders* are searched for ``.syx`` banks, in order. *save_folder* is
34
+ where ``save()`` writes; it is searched first.
35
+ """
36
+
37
+ def __init__(self, engine, folders=(), save_folder=None):
38
+ self.engine = engine
39
+ self.save_folder = save_folder
40
+ self.folders = ([save_folder] if save_folder else []) + [f for f in folders if f != save_folder]
41
+ self.bank = Bank()
42
+ self.slot = 0
43
+ self.edit = self.bank[0].copy()
44
+ self.comparing = False
45
+ #: The bank's file name, and None until one is loaded or saved.
46
+ self.file = None
47
+ #: Called with "voice", "slot" or "bank" after a change the panel didn't make.
48
+ self.on_change = None
49
+ self._send()
50
+
51
+ # ---- what is shown and played ----------------------------------------------------
52
+
53
+ @property
54
+ def voice(self):
55
+ """The voice shown and played: the edit copy, or the stored one while comparing."""
56
+ return self.bank[self.slot] if self.comparing else self.edit
57
+
58
+ @property
59
+ def stored(self):
60
+ return self.bank[self.slot]
61
+
62
+ @property
63
+ def edited(self):
64
+ """True when the edit copy is no longer what the bank holds."""
65
+ return self.edit != self.bank[self.slot]
66
+
67
+ def _send(self):
68
+ self.engine.patch(self.voice.to_bytes())
69
+
70
+ def _tell(self, what):
71
+ if self.on_change is not None:
72
+ self.on_change(what)
73
+
74
+ # ---- edits --------------------------------------------------------------------------
75
+
76
+ def set_op(self, number, name, value):
77
+ """Give operator *number* (1 to 6) of the edit copy *value* for *name*."""
78
+ if self.comparing:
79
+ return False
80
+ self.edit.op(number).set(name, value)
81
+ self._send()
82
+ return True
83
+
84
+ def set_voice(self, name, value):
85
+ """Give the edit copy *value* for its own parameter *name*."""
86
+ if self.comparing:
87
+ return False
88
+ self.edit.set(name, value)
89
+ self._send()
90
+ return True
91
+
92
+ def set_name(self, text):
93
+ if self.comparing:
94
+ return False
95
+ self.edit.name = text
96
+ return True
97
+
98
+ def compare(self, on):
99
+ """Show and play the stored voice (True) or the edit copy again (False)."""
100
+ self.comparing = bool(on)
101
+ self._send()
102
+
103
+ def select(self, slot, tell=False):
104
+ """Take slot *slot* (0 to 31) of the bank as the voice to play and edit."""
105
+ if not 0 <= slot < VOICES:
106
+ raise ValueError("a bank has slots 0 to 31, not %d" % slot)
107
+ self.slot = slot
108
+ self.edit = self.bank[slot].copy()
109
+ self.comparing = False
110
+ self._send()
111
+ if tell:
112
+ self._tell("slot")
113
+
114
+ def store(self):
115
+ """Put the edit copy into the bank at the slot. The file is not written."""
116
+ self.bank[self.slot] = self.edit
117
+ self.comparing = False
118
+ self._send()
119
+
120
+ def init_voice(self):
121
+ """Make the edit copy the plain starting voice."""
122
+ self.edit = Voice()
123
+ self.comparing = False
124
+ self._send()
125
+
126
+ # ---- banks ----------------------------------------------------------------------------
127
+
128
+ def use_bank(self, bank, file=None, tell=False):
129
+ self.bank = bank
130
+ if file is not None:
131
+ self.file = file
132
+ self.select(min(self.slot, VOICES - 1))
133
+ if tell:
134
+ self._tell("bank")
135
+
136
+ def files(self):
137
+ """The banks found, as (name, path), sorted by name. The save folder's copy wins."""
138
+ found = {}
139
+ # appkit.assets reads a folder on the filesystem, or the app's own
140
+ # files where the app is frozen into the firmware.
141
+ for folder in self.folders:
142
+ try:
143
+ names = assets.listdir(folder)
144
+ except OSError:
145
+ continue
146
+ for name in names:
147
+ if not name.lower().endswith(".syx") or name in found:
148
+ continue
149
+ path = _join(folder, name)
150
+ try:
151
+ size = len(assets.read(path, binary=True))
152
+ except OSError:
153
+ continue
154
+ if size == BANK_SYSEX_SIZE:
155
+ found[name] = path
156
+ return sorted(found.items())
157
+
158
+ def load(self, path):
159
+ """Open the bank at *path*. Raises OSError or ValueError, and then nothing changes."""
160
+ bank = Bank.from_sysex(assets.read(path, binary=True))
161
+ name = path.replace("\\", "/").split("/")[-1]
162
+ self.use_bank(bank, name)
163
+
164
+ def open_default(self):
165
+ """Open `DEFAULT_BANK`, or the first bank found. Without one the bank stays as it is."""
166
+ files = self.files()
167
+ for name, path in files:
168
+ if name == DEFAULT_BANK:
169
+ self.load(path)
170
+ return name
171
+ if files:
172
+ self.load(files[0][1])
173
+ return files[0][0]
174
+ return None
175
+
176
+ def save(self):
177
+ """Write the bank to the save folder under its name; returns the path."""
178
+ if not self.save_folder:
179
+ raise OSError("no folder to save to")
180
+ name = self.file or UNTITLED
181
+ path = _join(self.save_folder, name)
182
+ self.bank.save(path)
183
+ self.file = name
184
+ return path
185
+
186
+ def load_sysex(self, message):
187
+ """Take a system exclusive message: a 32-voice bank, or one voice for the edit copy.
188
+
189
+ Returns "bank", "voice", or None for any other message. A bank or a
190
+ voice whose checksum is wrong raises ValueError.
191
+ """
192
+ size = len(message)
193
+ if size == BANK_SYSEX_SIZE and message[3] == 0x09:
194
+ bank = Bank.from_sysex(message)
195
+ # It came from no file: the display stops naming the one that
196
+ # was open, and SAVE writes a new file, not over that one.
197
+ self.file = None
198
+ self.use_bank(bank, tell=True)
199
+ return "bank"
200
+ if size == SINGLE_SYSEX_SIZE and message[3] == 0x00:
201
+ self.edit = Voice.from_sysex(message)
202
+ self.comparing = False
203
+ self._send()
204
+ self._tell("voice")
205
+ return "voice"
206
+ return None
apps/fmsynth/engine.py ADDED
@@ -0,0 +1,257 @@
1
+ # SPDX-FileCopyrightText: 2026 Brad Barnett
2
+ #
3
+ # SPDX-License-Identifier: MIT
4
+ """The one place fmsynth meets the sound engine.
5
+
6
+ `Engine` hands voices and notes to ``audiofm`` (audiodsp). Everything the
7
+ app says to the engine goes through the calls here, and the engine is
8
+ given a voice only as its 155 bytes, so the app's own model never depends on
9
+ the engine's.
10
+
11
+ Where ``audiofm`` isn't installed the engine is `Silent`: it has the same
12
+ calls, makes no sound and keeps a log of what it was asked, so the editor
13
+ runs and can be checked without the engine. `Engine.name` says which one is
14
+ in use.
15
+
16
+ Between ``audiofm`` and the audio out stands the volume: one
17
+ ``audiodynamics.Dynamics`` (audiodsp) as a limiter with make-up gain. Each
18
+ voice comes out of ``audiofm`` at a sixteenth of full scale so that sixteen
19
+ can't clip, which leaves one note quiet. The volume turns everything up, 12
20
+ dB to begin with, and the limiter holds what that would push over the top
21
+ just under full scale: a big chord is turned down smoothly instead of
22
+ clipping. `Engine.output` is what goes to the audio out.
23
+ """
24
+
25
+ VOICES = 16
26
+ RATE = 48000
27
+ #: How many calls the silent engine remembers.
28
+ LOG_SIZE = 64
29
+
30
+ #: The volume in dB: as far up and down as it goes, and where it starts.
31
+ #: None is off.
32
+ VOLUME_MAX = 18
33
+ VOLUME_MIN = -41
34
+ VOLUME_DEFAULT = 12
35
+ #: The highest peak the limiter lets out, in dBFS.
36
+ CEILING_DB = -1.0
37
+ #: The limiter. It starts 3 dB under the ceiling and bends into it (the
38
+ #: knee); it catches a peak at once, from 1 ms before it arrives, so nothing
39
+ #: gets past; and it lets go at the pace of 2.5 s, so that a held chord whose
40
+ #: notes beat against each other isn't turned up and down with the beats.
41
+ _LIMITER = {"knee_db": 6, "attack_ms": 0, "release_ms": 2500, "lookahead_ms": 1}
42
+ #: Make-up gain that leaves nothing: 16-bit sound ends 96 dB down.
43
+ _OFF_DB = -120
44
+ #: After the volume is turned down the limiter's threshold goes up, this many
45
+ #: milliseconds later: the gain takes 20 ms to walk down, and a threshold
46
+ #: raised before it has would let a held chord over the ceiling.
47
+ _SETTLE_MS = 40
48
+ #: On a computer, the output to play to by its name, such as ``"Speakers
49
+ #: (USB Audio)"``; None plays to the default. Set it before the app starts.
50
+ DEVICE = None
51
+
52
+
53
+ class Silent:
54
+ """Stands in for ``audiofm.Synth``: the same calls, no sound, and a log."""
55
+
56
+ def __init__(self):
57
+ self.log = []
58
+ self.patch_bytes = None
59
+ self.pitch_bend = 0.0
60
+ self.mod_wheel = 0.0
61
+ self.sustain = False
62
+
63
+ def _note(self, *call):
64
+ self.log.append(call)
65
+ if len(self.log) > LOG_SIZE:
66
+ del self.log[0]
67
+
68
+ def press(self, note, velocity=100):
69
+ self._note("press", note, velocity)
70
+
71
+ def release(self, note):
72
+ self._note("release", note)
73
+
74
+ def release_all(self):
75
+ self._note("release_all")
76
+
77
+
78
+ class Engine:
79
+ """The sound engine, or its silent stand-in.
80
+
81
+ *scope* is the app's scope: a desktop audio output runs its service on
82
+ the app's timers. ``start()`` opens the audio out, and may be left out
83
+ to run the engine with no sound (a test pulls the audio itself from
84
+ ``output``, or from ``synth`` for the sound before the volume).
85
+ """
86
+
87
+ def __init__(self, scope=None, voices=VOICES, sample_rate=RATE):
88
+ self.scope = scope
89
+ self.sample_rate = sample_rate
90
+ self.voices = voices
91
+ self._out = None
92
+ self._audiofm = None
93
+ #: Reads the level, the load and the underruns off the audio out.
94
+ self.monitor = None
95
+ #: Why there is no sound, in a sentence, or None when there is.
96
+ self.why = None
97
+ #: The volume in dB, `VOLUME_MIN` to `VOLUME_MAX`, or None for off.
98
+ self.volume = VOLUME_DEFAULT
99
+ self._limiter = None
100
+ self._threshold = CEILING_DB - VOLUME_DEFAULT
101
+ self._settle = None
102
+ try:
103
+ import audiofm
104
+ except ImportError:
105
+ audiofm = None
106
+ if audiofm is None:
107
+ self.name = "silent"
108
+ self.why = "audiofm isn't installed"
109
+ self.synth = Silent()
110
+ self.output = None
111
+ else:
112
+ import audiodynamics
113
+
114
+ self.name = "audiofm"
115
+ self._audiofm = audiofm
116
+ self.synth = audiofm.Synth(voices=voices, sample_rate=sample_rate, channel_count=1)
117
+ limiter = audiodynamics.Dynamics(audiodynamics.DYN_LIMIT, sample_rate=sample_rate, channel_count=1,
118
+ threshold_db=CEILING_DB - self.volume, makeup_db=self.volume,
119
+ **_LIMITER)
120
+ limiter.play(self.synth)
121
+ self._limiter = limiter
122
+ #: What goes to the audio out: the synth through the volume.
123
+ self.output = limiter
124
+
125
+ @property
126
+ def silent(self):
127
+ return self._audiofm is None
128
+
129
+ # ---- the voice ------------------------------------------------------------
130
+
131
+ def patch(self, data):
132
+ """Play *data*, a voice's 155 bytes, from the next note on."""
133
+ if self._audiofm is None:
134
+ self.synth.patch_bytes = bytes(data)
135
+ self.synth._note("patch", len(data))
136
+ else:
137
+ self.synth.patch = self._audiofm.Patch.unpack(bytes(data))
138
+
139
+ # ---- playing ----------------------------------------------------------------
140
+
141
+ def press(self, note, velocity=100):
142
+ self.synth.press(note, velocity)
143
+
144
+ def release(self, note):
145
+ self.synth.release(note)
146
+
147
+ def release_all(self):
148
+ self.synth.release_all()
149
+
150
+ def pitch_bend(self, value):
151
+ """*value* is -1.0 to 1.0."""
152
+ self.synth.pitch_bend = value
153
+
154
+ def mod_wheel(self, value):
155
+ """*value* is 0.0 to 1.0."""
156
+ self.synth.mod_wheel = value
157
+
158
+ def sustain(self, down):
159
+ self.synth.sustain = bool(down)
160
+
161
+ # ---- the volume ------------------------------------------------------------------
162
+
163
+ def set_volume(self, db):
164
+ """Turn the sound up or down: *db* is `VOLUME_MIN` to `VOLUME_MAX`,
165
+ or None for off. One note comes out *db* louder than ``audiofm``
166
+ makes it; whatever that would push past `CEILING_DB` is held there.
167
+ """
168
+ if db is not None and not VOLUME_MIN <= db <= VOLUME_MAX:
169
+ raise ValueError("the volume is %d to %d dB, or None for off" % (VOLUME_MIN, VOLUME_MAX))
170
+ self.volume = db
171
+ limiter = self._limiter
172
+ if limiter is None:
173
+ return
174
+ if self._settle is not None:
175
+ self._settle.deinit()
176
+ self._settle = None
177
+ if db is None:
178
+ limiter.set(makeup_db=_OFF_DB)
179
+ return
180
+ threshold = CEILING_DB - db
181
+ if threshold > self._threshold and self.scope is not None and self._out is not None:
182
+ # Turned down while it plays: the gain walks down first, and the
183
+ # threshold follows it up.
184
+ limiter.set(makeup_db=db)
185
+ self._settle = self.scope.after(_SETTLE_MS, self._settled)
186
+ else:
187
+ self._threshold = threshold
188
+ limiter.set(threshold_db=threshold, makeup_db=db)
189
+
190
+ def _settled(self, _timer=None):
191
+ self._settle = None
192
+ if self._limiter is not None and self.volume is not None:
193
+ self._threshold = CEILING_DB - self.volume
194
+ self._limiter.set(threshold_db=self._threshold)
195
+
196
+ # ---- the audio out ------------------------------------------------------------
197
+
198
+ def start(self):
199
+ """Send the engine's sound to the board's or the computer's audio out."""
200
+ if self._audiofm is None or self._out is not None:
201
+ return
202
+ try:
203
+ import board_peripherals as bp
204
+ from audiodev import AudioFormat
205
+
206
+ named = {} if DEVICE is None else {"device": DEVICE}
207
+ out = bp.audio_out(AudioFormat(self.sample_rate, 1, 16), latency="low", **named)
208
+ if self.scope is not None:
209
+ out.attach(self.scope)
210
+ out.play(self.output)
211
+ except Exception as e: # no audio out here: the editor still runs, and says why
212
+ self.why = "no audio out: %s" % (str(e) or type(e).__name__,)
213
+ return
214
+ self._out = out
215
+ try:
216
+ from .meters import Monitor
217
+
218
+ self.monitor = Monitor(self.sample_rate, 1)
219
+ self.monitor.attach(out)
220
+ except Exception as e: # the sound doesn't depend on its meter
221
+ self.monitor = None
222
+ print("fmsynth: no meter: %s" % (str(e) or type(e).__name__,))
223
+
224
+ # ---- what the meter and the readout show ----------------------------------------
225
+
226
+ def level(self):
227
+ """The newest sound's peak, 0.0 to 1.0, as it goes to the audio out."""
228
+ if self.monitor is None:
229
+ return 0.0
230
+ return max(self.monitor.levels())
231
+
232
+ def load(self):
233
+ """``(load, worst, underruns)``: what rendering costs as a percentage of
234
+ the time it has, the slowest block's, and how often the audio out ran
235
+ dry. The loads are None while there's nothing to measure."""
236
+ if self.monitor is None:
237
+ return None, None, 0
238
+ return self.monitor.load()
239
+
240
+ def close(self):
241
+ out = self._out
242
+ self._out = None
243
+ if self._settle is not None:
244
+ self._settle.deinit()
245
+ self._settle = None
246
+ if self.monitor is not None:
247
+ try:
248
+ self.monitor.detach()
249
+ except Exception:
250
+ pass
251
+ self.monitor = None
252
+ if out is not None:
253
+ try:
254
+ out.stop()
255
+ out.close()
256
+ except Exception:
257
+ pass