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
@@ -0,0 +1 @@
1
+ {"name": "midicontroller", "title": "MIDI Controller", "category": "Audio", "icon": "instant_mix", "color": "#5E35B1"}
@@ -0,0 +1,421 @@
1
+ # SPDX-FileCopyrightText: 2026 Brad Barnett
2
+ #
3
+ # SPDX-License-Identifier: MIT
4
+ """Where the controller's MIDI goes: each way out this board has.
5
+
6
+ `choices` lists what this board can do, in the order the app offers them,
7
+ and `default` picks the one it opens on:
8
+
9
+ ``usb``
10
+ The board is a USB MIDI device on its native USB port (an ESP32-S2, S3
11
+ or P4), for a computer, a phone or a host board. Needs midiports and
12
+ micropython-lib's ``usb-device-midi``.
13
+ ``host``
14
+ A synth or a sound module plugged into the board's USB host port (an
15
+ ESP32-P4, say). Every MIDI device on the port hears it, and ones
16
+ plugged in later join. Needs midiports and ``usb-core``.
17
+ ``ble``
18
+ The board advertises BLE-MIDI, for a computer, a phone or a tablet.
19
+ Needs bledev and firmware with Bluetooth.
20
+ ``out:<endpoint id>``
21
+ On a computer or in a browser page, one of the MIDI outputs the
22
+ Connections picker lists under MIDI out: a synth, a DAW's virtual port,
23
+ a loopback, or what Web MIDI offers. Choosing one here chooses it there,
24
+ for the whole device, and a choice made there is what the controller
25
+ plays, every output chosen at once. What comes back is the MIDI ins
26
+ chosen there. Needs midiports and endpointkit. In a browser the person
27
+ allows MIDI first: until then the one choice is "Allow MIDI", and
28
+ choosing it has the browser ask, then plays the first output.
29
+
30
+ `open_link` opens one and hands back a `Link`: ``write`` for the output,
31
+ ``read_into`` for what comes back (CC feedback), a status line, ``close``.
32
+ """
33
+
34
+ import sys
35
+
36
+ #: The name the board advertises over BLE, which a computer lists as its MIDI port.
37
+ BLE_NAME = "MIDI Controller"
38
+
39
+
40
+ class Link:
41
+ """One open way out. *out* is a MIDI output port, *inp* an input or None."""
42
+
43
+ def __init__(self, key, label, out, inp=None, closer=None):
44
+ self.key = key
45
+ self.label = label
46
+ self.out = out
47
+ self.inp = inp
48
+ self._closer = closer
49
+ self.closed = False
50
+
51
+ def write(self, buf, nbytes=None):
52
+ if self.out is None:
53
+ raise OSError("%s: not connected" % self.label)
54
+ return self.out.write(buf, len(buf) if nbytes is None else nbytes)
55
+
56
+ def read_into(self, buf):
57
+ """What has come in, into *buf*: how many bytes, or 0."""
58
+ if self.inp is None:
59
+ return 0
60
+ return self.inp.readinto(buf) or 0
61
+
62
+ def poll(self):
63
+ """Run anything the link needs between touches."""
64
+
65
+ def status(self):
66
+ return self.label
67
+
68
+ def close(self):
69
+ if self.closed:
70
+ return
71
+ self.closed = True
72
+ for port in (self.inp, self.out):
73
+ if port is not None:
74
+ try:
75
+ port.close()
76
+ except Exception: # noqa: BLE001 - closing the rest matters more
77
+ pass
78
+ if self._closer is not None:
79
+ self._closer()
80
+
81
+
82
+ class UsbLink(Link):
83
+ """The board as a USB MIDI device."""
84
+
85
+ def __init__(self, ports, itf, closer):
86
+ Link.__init__(self, "usb", "USB", ports[1], ports[0], closer)
87
+ self.itf = itf
88
+
89
+ def status(self):
90
+ is_open = getattr(self.itf, "is_open", None)
91
+ if is_open is not None and not is_open():
92
+ return "USB: waiting for a computer"
93
+ return "USB: connected"
94
+
95
+
96
+ class HostLink(Link):
97
+ """Every MIDI device on the board's USB host port."""
98
+
99
+ def status(self):
100
+ n = len(getattr(self.inp, "devices", ()) or ())
101
+ if not n:
102
+ return "USB host: plug a synth into the host port"
103
+ return "USB host: %d device%s" % (n, "" if n == 1 else "s")
104
+
105
+
106
+ def _sleep_ms(asyncio, ms):
107
+ """MicroPython's ``asyncio.sleep_ms``; CPython's asyncio has ``sleep`` alone."""
108
+ sleep_ms = getattr(asyncio, "sleep_ms", None)
109
+ return sleep_ms(ms) if sleep_ms is not None else asyncio.sleep(ms / 1000)
110
+
111
+
112
+ async def _yield():
113
+ import asyncio
114
+
115
+ await _sleep_ms(asyncio, 0)
116
+
117
+
118
+ class BleLink(Link):
119
+ """BLE-MIDI, served from this board; a central connects to it.
120
+
121
+ bledev runs on asyncio, and appkit runs no event loop, so `poll` runs
122
+ the loop's ready tasks for one pass each time it's called. MicroPython
123
+ has one loop for the program; on CPython the link keeps a loop of its
124
+ own, since a task needs a loop to belong to.
125
+ """
126
+
127
+ def __init__(self, bledev_midi, ble, asyncio, name=BLE_NAME):
128
+ Link.__init__(self, "ble", "BLE", None)
129
+ self._midi = bledev_midi
130
+ self._ble = ble
131
+ self._asyncio = asyncio
132
+ self.name = name
133
+ self.state = "advertising as %s" % (name or "this computer")
134
+ self._loop = None if hasattr(asyncio, "sleep_ms") else asyncio.new_event_loop()
135
+ if self._loop is None:
136
+ self._task = asyncio.create_task(self._keep())
137
+ else:
138
+ self._task = self._loop.create_task(self._keep())
139
+
140
+ def _run(self, coro):
141
+ if self._loop is None:
142
+ return self._asyncio.run(coro)
143
+ return self._loop.run_until_complete(coro)
144
+
145
+ async def _keep(self):
146
+ while not self.closed:
147
+ try:
148
+ port = await self._midi.serve(self._ble, name=self.name)
149
+ except Exception as e: # noqa: BLE001 - say so, and advertise again
150
+ self.state = str(e) or "couldn't advertise"
151
+ await _sleep_ms(self._asyncio, 1000)
152
+ continue
153
+ self.out = self.inp = port
154
+ self.state = "connected"
155
+ while not self.closed and port.connection.is_connected():
156
+ await _sleep_ms(self._asyncio, 200)
157
+ self.out = self.inp = None
158
+ if not self.closed:
159
+ self.state = "disconnected, advertising as %s" % (self.name or "this computer")
160
+
161
+ def poll(self):
162
+ self._run(_yield())
163
+
164
+ def read_into(self, buf):
165
+ try:
166
+ return Link.read_into(self, buf)
167
+ except Exception: # noqa: BLE001 - the link dropped; _keep advertises again
168
+ self.out = self.inp = None
169
+ return 0
170
+
171
+ def status(self):
172
+ return "BLE: %s" % self.state
173
+
174
+ def close(self):
175
+ port = self.out
176
+ self.closed = True
177
+ self.out = self.inp = None
178
+ if port is not None:
179
+ try:
180
+ self._run(port.close())
181
+ except Exception: # noqa: BLE001 - it's gone either way
182
+ pass
183
+ self._task.cancel()
184
+ try:
185
+ self.poll()
186
+ except Exception: # noqa: BLE001
187
+ pass
188
+ if self._loop is not None:
189
+ self._loop.close()
190
+
191
+
192
+ #: The platforms that are a browser page.
193
+ BROWSERS = ("webassembly", "emscripten")
194
+ #: The platforms whose MIDI ports the Connections picker lists: computers and browser pages.
195
+ CONNECTED_PLATFORMS = ("win32", "linux", "darwin") + BROWSERS
196
+
197
+
198
+ def _chosen():
199
+ """``(endpointkit, midiports.endpoints)`` with this device's MIDI ports
200
+ registered, or None where the picker lists none (a board, or neither
201
+ library installed)."""
202
+ if sys.platform not in CONNECTED_PLATFORMS:
203
+ return None
204
+ try:
205
+ import endpointkit
206
+ from midiports import endpoints
207
+ except ImportError:
208
+ return None
209
+ try:
210
+ endpoints.register() # the Shell's picker registers them too: whichever is first
211
+ except OSError: # no MIDI ports on this computer's system
212
+ return None
213
+ return endpointkit, endpoints
214
+
215
+
216
+ class ChosenLink(Link):
217
+ """The MIDI outputs chosen in the Connections picker, all at once, and the inputs chosen there for feedback.
218
+
219
+ The ports are the picker's: closing the link leaves them open for the
220
+ next program.
221
+ """
222
+
223
+ def __init__(self, key, kit, endpoints, first=False):
224
+ Link.__init__(self, key, "MIDI out", endpoints.fan_out(), endpoints.merged_in())
225
+ self._kit = kit
226
+ self._first = first
227
+
228
+ def poll(self):
229
+ kit = self._kit
230
+ if self._first and not kit.selection("midi.out"):
231
+ # Allowed now: play the first output, as a board plays its first way out.
232
+ for ep in kit.endpoints("midi.out"):
233
+ if ep.state in (kit.AVAILABLE, kit.CONNECTED):
234
+ kit.select("midi.out", ep.id)
235
+ self.key = "out:" + ep.id
236
+ self._first = False
237
+ break
238
+
239
+ def names(self):
240
+ """What the controller plays now, for a person: the chosen outputs' names."""
241
+ kit = self._kit
242
+ chosen = kit.selected("midi.out")
243
+ if chosen:
244
+ return ", ".join(ep.label for ep in chosen)
245
+ for ep in kit.endpoints("midi.out"):
246
+ if ep.state == kit.NEEDS_PAIRING:
247
+ return "allow MIDI when the browser asks"
248
+ if ep.state == kit.UNAVAILABLE:
249
+ return ep.caps.get("why") or ep.label
250
+ if kit.selection("midi.out"):
251
+ return "waiting for %s" % kit.selection("midi.out")[0].split(":", 1)[-1]
252
+ return "nothing chosen"
253
+
254
+ def status(self):
255
+ return "MIDI out: %s" % self.names()
256
+
257
+ def close(self):
258
+ self.closed = True
259
+
260
+
261
+ def choices():
262
+ """``[(key, label), ...]``: each way out this board has. On a computer
263
+ its own MIDI outputs come before its USB host port and Bluetooth LE."""
264
+ out = []
265
+ if sys.platform in BROWSERS:
266
+ # A page has no USB port or radio of its own: a build that carries
267
+ # the USB host's module for boards still reaches no device from here.
268
+ return _connected(out)
269
+ try:
270
+ import machine
271
+
272
+ if hasattr(machine, "USBDevice"):
273
+ import usb.device.midi # noqa: F401 - usb-device-midi is what the link needs
274
+ from midiports import usb_device # noqa: F401
275
+
276
+ out.append(("usb", "USB device"))
277
+ except ImportError:
278
+ pass
279
+ out = _connected(out)
280
+ try:
281
+ import _usbhost
282
+
283
+ if hasattr(_usbhost, "host_start"):
284
+ from midiports import usb_host # noqa: F401
285
+
286
+ out.append(("host", "USB host"))
287
+ except ImportError:
288
+ pass
289
+ try:
290
+ import bledev.auto
291
+
292
+ # A browser's and a desktop's BLE (webble, bleak) are central only,
293
+ # and the controller has to be the peripheral a computer connects to.
294
+ if bledev.auto.backend_name() not in (None, "webble", "bleak"):
295
+ import bledev.midi # noqa: F401
296
+
297
+ out.append(("ble", "Bluetooth LE"))
298
+ except ImportError:
299
+ pass
300
+ return out
301
+
302
+
303
+ def default(keys):
304
+ """The way out to open when none was chosen, from `choices`' *keys*: the
305
+ first. A computer opens on one of its own MIDI outputs only: with none
306
+ listed it opens nothing and says why (`missing`), and its USB host port
307
+ or Bluetooth LE is there to choose in Setup."""
308
+ if sys.platform in CONNECTED_PLATFORMS:
309
+ for key in keys:
310
+ if key == "usb" or key.startswith("out:"):
311
+ return key
312
+ return None
313
+ return keys[0] if keys else None
314
+
315
+
316
+ def _connected(out):
317
+ """*out* with the MIDI outputs the Connections picker lists added."""
318
+ chosen = _chosen()
319
+ if chosen is not None:
320
+ kit = chosen[0]
321
+ for ep in kit.endpoints("midi.out"):
322
+ if ep.state != kit.UNAVAILABLE:
323
+ out.append(("out:" + ep.id, ep.label))
324
+ return out
325
+
326
+
327
+ def missing():
328
+ """A line saying what this board lacks, for when `choices` is empty."""
329
+ chosen = _chosen()
330
+ if chosen is not None:
331
+ kit = chosen[0]
332
+ for ep in kit.endpoints("midi.out"):
333
+ if ep.state == kit.UNAVAILABLE and ep.caps.get("why"):
334
+ return "No MIDI out here: %s." % ep.caps["why"]
335
+ return "No MIDI out here: plug in a MIDI device or start a synth, then choose it in Connections or Setup."
336
+ if sys.platform in BROWSERS:
337
+ return "No Web MIDI here: this page needs midiports and endpointkit."
338
+ return ("No way out for MIDI here: this needs a USB device or host port with midiports, "
339
+ "Bluetooth LE with bledev, or a computer's MIDI ports with endpointkit.")
340
+
341
+
342
+ def _start_host():
343
+ import usb.core
344
+
345
+ # A synth is a full-speed device, and full speed reaches one behind a
346
+ # hub too. A host that already ran at high speed since power-up can't
347
+ # change on a board whose hub can't be power-cycled, so it stays there.
348
+ try:
349
+ usb.core.start(full_speed=True)
350
+ except OSError as e:
351
+ if e.args and e.args[0] != 95: # EOPNOTSUPP: an earlier app ran it at high speed
352
+ raise
353
+ usb.core.start()
354
+ return usb.core
355
+
356
+
357
+ def _free_port():
358
+ """Stop a USB host that holds the one USB port of an ESP32-S2 or S3.
359
+
360
+ The device has to start after the host stops: started while a host
361
+ runs, it never enumerates, and stopping the host afterwards doesn't
362
+ bring it back until the board resets.
363
+ """
364
+ if not any(chip in getattr(sys.implementation, "_machine", "") for chip in ("ESP32S2", "ESP32S3")):
365
+ return
366
+ try:
367
+ import usb.core
368
+ except ImportError:
369
+ return
370
+ if usb.core.running():
371
+ usb.core.stop()
372
+ print("midicontroller: stopped the USB host, which shares this board's USB port")
373
+
374
+
375
+ def open_link(key):
376
+ """Open the way out named *key*, one of `choices`' keys."""
377
+ if key == "usb":
378
+ import usb.device
379
+ from midiports import usb_device
380
+
381
+ _free_port()
382
+ itf = usb_device.MIDIPortsInterface()
383
+ usb.device.get().init(itf, builtin_driver=True)
384
+
385
+ def give_back():
386
+ # The port is the board's serial port alone again.
387
+ usb.device.get().init(builtin_driver=True)
388
+
389
+ return UsbLink(itf.ports, itf, give_back)
390
+ if key == "host":
391
+ from midiports import usb_host
392
+
393
+ core = _start_host()
394
+ inp, out = usb_host.all_ports()
395
+ return HostLink("host", "USB host", out, inp, core.stop)
396
+ if key == "ble":
397
+ import asyncio
398
+
399
+ import bledev.auto
400
+ import bledev.midi
401
+
402
+ # Windows advertises under the computer's own name, and no other.
403
+ name = None if bledev.auto.backend_name() == "winrt" else BLE_NAME
404
+ return BleLink(bledev.midi, bledev.auto.adapter(), asyncio, name)
405
+ if key.startswith("out:"):
406
+ chosen = _chosen()
407
+ if chosen is None:
408
+ raise OSError("no MIDI ports to choose from here")
409
+ kit, endpoints = chosen
410
+ ep_id = key[4:]
411
+ ep = kit.find(ep_id)
412
+ first = ep is not None and ep.state == kit.NEEDS_PAIRING
413
+ if first:
414
+ kit.pair(ep_id) # the browser asks the person
415
+ elif ep is not None and ep.state == kit.UNAVAILABLE:
416
+ raise OSError(ep.caps.get("why") or "%s isn't available" % ep.label)
417
+ else:
418
+ # Chosen for the whole device; one not plugged in yet is used when it is.
419
+ kit.select("midi.out", ep_id)
420
+ return ChosenLink(key, kit, endpoints, first)
421
+ raise ValueError("no MIDI link %r" % (key,))