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,774 @@
1
+ # SPDX-FileCopyrightText: 2026 Brad Barnett
2
+ #
3
+ # SPDX-License-Identifier: MIT
4
+ """MIDI Controller: pads, knobs, faders and keys that play a synth or a DAW.
5
+
6
+ The tabs along the top switch between the views a hardware controller
7
+ would have side by side:
8
+
9
+ - Pads: sixteen pads in a 4 x 4 grid that play notes (a drum kit's from
10
+ 36, at the bottom left), with four banks, A to D, each sixteen notes up.
11
+ - Knobs: sixteen endless knobs, 4 x 4, or 8 x 2 on a wide, short screen,
12
+ on CC 70 to 85 (a synth's sound controllers: 74 is the filter cutoff).
13
+ - Faders: sixteen faders in a row, on CC 102 to 117.
14
+ - Mixer: eight strips, each a volume fader (CC 7) with mute and solo,
15
+ strip 1 on the channel and each next strip one channel up, so they are
16
+ the volumes of a synth's first eight parts.
17
+ - XY: a pad that sends two CCs at once (16 across, 17 up), and the
18
+ transport: rewind, forward, stop, play, record and loop (CC 20 to 25).
19
+ - Keys: two octaves of keys with octave shift, and a modulation wheel.
20
+ - Setup: where it sends, the channel, the velocity, and the mapping.
21
+
22
+ A touch panel reads no pressure, so a pad's velocity comes from where the
23
+ finger lands: nearer the top is harder. A key is harder nearer its front
24
+ edge. Setup can fix the velocity instead.
25
+
26
+ MIDI goes out over whichever way this board has: USB as a MIDI device
27
+ (``usb-device-midi``), a synth on the USB host port, Bluetooth LE MIDI
28
+ (bledev), or, on a computer or in a browser page, the MIDI outputs the
29
+ Connections picker lists. Setup picks one when there are several, and an
30
+ output picked there is the one Connections uses too. What comes back moves
31
+ the controls to match, so a DAW's automation turns the knobs, and a note
32
+ played into the board lights its pad.
33
+
34
+ Edit in Setup shows each control's note or CC in its place; tap one to
35
+ change it. The mapping and the settings are kept in ``config.json`` in
36
+ the app's data folder (``/data/midicontroller`` on a board), holding only
37
+ what differs from the defaults. Fits a screen of 480 x 320 or larger.
38
+ """
39
+
40
+ import lvgl as lv
41
+ from controlkit import LampButton, PadGrid, Slider
42
+ from controlkit import encoder as _encoder
43
+ from controlkit.theme import BLACK
44
+ from multimer import ticks_diff, ticks_ms
45
+ from touchkit import settings, ui
46
+
47
+ from . import links
48
+ from .surface import (
49
+ BANKS,
50
+ CC,
51
+ NOTE,
52
+ TRANSPORT,
53
+ Surface,
54
+ family,
55
+ index,
56
+ is_button,
57
+ note_name,
58
+ )
59
+
60
+ #: How often what comes in is read and the status line caught up, in ms.
61
+ TICK_MS = 20
62
+ #: How long after a change the settings are saved, in ms: one write for a
63
+ #: run of taps, never one in the middle of playing.
64
+ SAVE_MS = 1500
65
+ #: The tabs: key and label.
66
+ VIEWS = (("pads", "Pads"), ("knobs", "Knobs"), ("faders", "Faders"), ("mixer", "Mixer"),
67
+ ("xy", "XY"), ("keys", "Keys"), ("setup", lv.SYMBOL.SETTINGS))
68
+ #: Each bank's colour, for the pads it lights.
69
+ BANK_COLORS = (0x26C6DA, 0xFFB300, 0xEC407A, 0x7CB342)
70
+ #: The velocities Setup steps through: 0 is from the touch.
71
+ VELOCITIES = (0, 32, 48, 64, 80, 96, 100, 112, 127)
72
+ #: The lamps of the transport buttons.
73
+ TRANSPORT_LAMPS = (0xFFB300, 0xFFB300, 0xC9CCD1, 0x3BA55C, 0xE53935, 0x26C6DA)
74
+ #: A key of the keyboard strip: white keys in an octave, and where each black one sits.
75
+ WHITE = (0, 2, 4, 5, 7, 9, 11)
76
+ BLACK_KEYS = ((1, 1), (3, 2), (6, 4), (8, 5), (10, 6)) # (semitone, after which white key)
77
+
78
+
79
+ def _margin(W):
80
+ return max(6, W // 80)
81
+
82
+
83
+ def _point():
84
+ p = lv.point_t()
85
+ indev = lv.indev_active()
86
+ if indev is None:
87
+ return None
88
+ indev.get_point(p)
89
+ return p.x, p.y
90
+
91
+
92
+ def _area(obj):
93
+ a = lv.area_t()
94
+ obj.get_coords(a)
95
+ return a
96
+
97
+
98
+ def _frac_up(obj):
99
+ """How far up *obj* the finger is: 0 at its bottom edge, 1 at its top."""
100
+ p = _point()
101
+ if p is None:
102
+ return None
103
+ a = _area(obj)
104
+ h = max(1, a.y2 - a.y1)
105
+ return (a.y2 - p[1]) / h
106
+
107
+
108
+ def _frac_down(obj):
109
+ f = _frac_up(obj)
110
+ return None if f is None else 1 - f
111
+
112
+
113
+ def _button(parent, text, cb, x, y, w, h, font=None, color=None):
114
+ b, lbl = ui.button(parent, text, cb, w=w, h=h, font=font or ui.small, color=color or ui.KEY)
115
+ b.set_pos(x, y)
116
+ b.set_style_radius(8, 0)
117
+ b.set_style_pad_all(2, 0)
118
+ return b, lbl
119
+
120
+
121
+ def _on_edges(obj, down, up):
122
+ """``down()`` when a finger lands on *obj*, ``up()`` when it lifts or slides off."""
123
+ obj.add_event_cb(lambda e: down(), lv.EVENT.PRESSED, None)
124
+ obj.add_event_cb(lambda e: up(), lv.EVENT.RELEASED, None)
125
+ obj.add_event_cb(lambda e: up(), lv.EVENT.PRESS_LOST, None)
126
+
127
+
128
+ class _KnobPanel:
129
+ """What controlkit's encoders ask of the panel they sit on."""
130
+
131
+ fine = False
132
+
133
+ def __init__(self, app, scale):
134
+ self.app = app
135
+ self.travel = max(80, int(200 * scale))
136
+
137
+ def default_value(self, i):
138
+ return 0
139
+
140
+ def _turned(self, enc, value):
141
+ self.app.moved("knob%d" % enc.index, value)
142
+
143
+ def _focused(self, enc):
144
+ pass
145
+
146
+
147
+ def _name(cid):
148
+ """A control's short name: ``Pad 1``, ``Vol 3``, ``Play``."""
149
+ f, i = family(cid), index(cid)
150
+ if f == "tr":
151
+ return TRANSPORT[i]
152
+ if cid in ("x", "y"):
153
+ return "XY " + cid.upper()
154
+ if cid == "mod":
155
+ return "Mod"
156
+ return "%s %d" % ({"pad": "Pad", "knob": "Knob", "fader": "Fader", "mix": "Vol", "mute": "Mute",
157
+ "solo": "Solo"}[f], i + 1)
158
+
159
+
160
+ class Controller:
161
+ """The app on one screen: the tabs, the view under them, the link out."""
162
+
163
+ def __init__(self, scope, link_opener=None):
164
+ self.scope = scope
165
+ self.root = scope.root
166
+ self.W, self.H = scope.size
167
+ self._open = link_opener or links.open_link
168
+ try:
169
+ config = settings.config(scope, {})
170
+ except Exception: # noqa: BLE001 - a scope with no data folder starts from the defaults
171
+ config = {}
172
+ self.surface = Surface(config)
173
+ self.link = None
174
+ self.link_error = None
175
+ self.choices = links.choices()
176
+ self.view = None
177
+ self.edit = False
178
+ self.editing = None # the control the editor is open on
179
+ self.widgets = {} # control id -> fn(value) that shows a value without sending
180
+ self.parts = {} # control id -> the widget it is on this view
181
+ self.shown = None
182
+ self._changed_at = None
183
+ self._buf = bytearray(64)
184
+
185
+ W, H = self.W, self.H
186
+ m = _margin(W)
187
+ self.m = m
188
+ self.status = ui.label(self.root, "", ui.small, ui.MUTED)
189
+ self.status.set_long_mode(lv.label.LONG_MODE.DOTS) # a long one ends in dots, inside the row
190
+ self.status.set_size(W - 2 * m, ui.small.line_height)
191
+ self.status.set_pos(m, 4)
192
+ row = ui.small.line_height + 8
193
+ tab_h = max(34, min(48, H // 9))
194
+ gap = 4
195
+ tab_w = (W - 2 * m - gap * (len(VIEWS) - 1)) // len(VIEWS)
196
+ self.tabs = {}
197
+ for i, (key, text) in enumerate(VIEWS):
198
+ b, _ = _button(self.root, text, lambda key=key: self.show(key), m + i * (tab_w + gap), row, tab_w, tab_h)
199
+ b.set_style_bg_color(ui.ACCENT, lv.STATE.CHECKED)
200
+ self.tabs[key] = b
201
+ top = row + tab_h + m
202
+ self.body = ui.plain(lv.obj(self.root))
203
+ self.body.set_pos(0, top)
204
+ self.body.set_size(W, H - top)
205
+ self.BW, self.BH = W, H - top - m
206
+
207
+ self.choose(self.surface.settings["output"], save=False)
208
+ scope.on_close(self.close)
209
+ self.show(self.surface.settings["view"], save=False)
210
+ self.tick()
211
+ scope.every(TICK_MS, self.tick)
212
+
213
+ # -- the link out ---------------------------------------------------------
214
+
215
+ def choose(self, key, save=True):
216
+ """Send through the way out named *key*, or the first this board has."""
217
+ keys = [k for k, _ in self.choices]
218
+ if key is not None and (key.startswith("pc:") or key.startswith("web:")):
219
+ key = self._chosen_now(key[key.index(":") + 1:]) # saved before the picker chose
220
+ # An output saved last time may not be listed yet (a browser lists
221
+ # none until the person allows MIDI again, a synth not started yet):
222
+ # it stays chosen, and plays once it's there.
223
+ kept = key is not None and key.startswith("out:") and any(k.startswith("out:") for k in keys)
224
+ if key not in keys and not kept:
225
+ key = self._chosen_now() or links.default(keys)
226
+ if self.link is not None:
227
+ self.link.close()
228
+ self.link = None
229
+ self.surface.out = None
230
+ self.link_error = None
231
+ if key is not None:
232
+ try:
233
+ self.link = self._open(key)
234
+ self.surface.out = self.link
235
+ except Exception as e: # noqa: BLE001 - said on the status line; Setup can pick another
236
+ print("midicontroller:", key, e)
237
+ self.link_error = "%s: %s" % (dict(self.choices).get(key, key), e)
238
+ if save:
239
+ self.surface.set("output", key)
240
+ self._changed_at = ticks_ms()
241
+
242
+ def close(self):
243
+ if self.link is not None:
244
+ self.link.close()
245
+ self.link = None
246
+ self.save()
247
+
248
+ def save(self):
249
+ if not self.surface.changed:
250
+ return
251
+ import json
252
+
253
+ from appkit import data
254
+
255
+ self.surface.changed = False
256
+ self._changed_at = None
257
+ try:
258
+ with open(data.join(self.scope.data_dir, "config.json"), "w") as f:
259
+ json.dump(self.surface.config(), f)
260
+ except (OSError, AttributeError, ValueError) as e:
261
+ print("midicontroller: couldn't save the settings:", e)
262
+
263
+ # -- each tick --------------------------------------------------------------
264
+
265
+ def tick(self, _t=None):
266
+ link = self.link
267
+ now = ticks_ms()
268
+ if link is not None:
269
+ try:
270
+ link.poll()
271
+ except Exception as e: # noqa: BLE001 - the link says what's wrong on its status
272
+ print("midicontroller:", e)
273
+ for _ in range(8): # a burst at most this many reads a tick, so the screen keeps up
274
+ n = link.read_into(self._buf)
275
+ if not n:
276
+ break
277
+ for cid, value in self.surface.feed(memoryview(self._buf)[:n], now, ticks_diff):
278
+ show = self.widgets.get(cid)
279
+ if show is not None:
280
+ show(value)
281
+ if self.surface.changed:
282
+ if self._changed_at is None:
283
+ self._changed_at = now
284
+ elif ticks_diff(now, self._changed_at) >= SAVE_MS:
285
+ self.save()
286
+ self._status()
287
+
288
+ def _status(self):
289
+ s = self.surface
290
+ if self.link_error:
291
+ where = self.link_error
292
+ elif self.link is not None:
293
+ where = self.link.status()
294
+ elif not self.choices:
295
+ where = "no MIDI out on this board"
296
+ else:
297
+ where = links.missing() # a computer with no MIDI out of its own: Setup offers the rest
298
+ text = "%s ch %d" % (where, s.settings["channel"])
299
+ if self.edit and self.view != "setup":
300
+ text = "Editing: tap a control to change what it sends"
301
+ elif s.error:
302
+ text += " " + s.error
303
+ elif s.last is not None:
304
+ text += " " + describe(s.last)
305
+ if text != self.shown:
306
+ self.shown = text
307
+ self.status.set_text(text)
308
+
309
+ # -- what the controls call -------------------------------------------------
310
+
311
+ def moved(self, cid, value):
312
+ self.surface.move(cid, value, ticks_ms())
313
+
314
+ # -- views --------------------------------------------------------------------
315
+
316
+ def show(self, key, save=True):
317
+ """Open the view *key*, from its tab."""
318
+ if key not in self.tabs:
319
+ key = "pads"
320
+ for k, b in self.tabs.items():
321
+ if k == key:
322
+ b.add_state(lv.STATE.CHECKED)
323
+ else:
324
+ b.remove_state(lv.STATE.CHECKED)
325
+ self.view = key
326
+ self.editing = None
327
+ if save and self.surface.settings["view"] != key:
328
+ self.surface.set("view", key)
329
+ self._build()
330
+
331
+ def later(self, fn):
332
+ """Run *fn* after this event: a button can't clean the view it's in."""
333
+ self.body.add_flag(lv.obj.FLAG.HIDDEN)
334
+
335
+ def run(_t=None):
336
+ self.body.remove_flag(lv.obj.FLAG.HIDDEN)
337
+ fn()
338
+
339
+ self.scope.after(10, run)
340
+
341
+ def _build(self):
342
+ self.body.clean()
343
+ self.widgets = {}
344
+ self.parts = {}
345
+ if self.editing is not None:
346
+ self._editor(self.editing)
347
+ elif self.view == "setup":
348
+ self._setup()
349
+ elif self.edit:
350
+ self._map()
351
+ else:
352
+ getattr(self, "_" + self.view)()
353
+
354
+ def _pads(self):
355
+ W, BH, m = self.BW, self.BH, self.m
356
+ bank_w = max(52, W // 12)
357
+ s = min(BH, W - 3 * m - bank_w)
358
+ x0 = (W - s - m - bank_w) // 2
359
+ grid = PadGrid(self.body, x0, 0, s, s, BLACK, cols=4, rows=4, gap=max(4, s // 60),
360
+ on_down=self._pad_down, on_up=self._pad_up)
361
+ self.grid = grid
362
+ color = BANK_COLORS[self.surface.settings["bank"]]
363
+ for g in range(16):
364
+ cid = "pad%d" % self._pad_of(g)
365
+ grid.set_label(g, self.surface.describe(cid))
366
+
367
+ def show(value, g=g):
368
+ grid.set_color(g, color if value else None)
369
+
370
+ self.widgets[cid] = show
371
+ if self.surface.values.get(cid):
372
+ show(1)
373
+ bh = (s - 3 * 6) // 4
374
+ for i, name in enumerate(BANKS):
375
+ b, _ = _button(self.body, name, lambda i=i: self._bank(i), x0 + s + m, i * (bh + 6), bank_w, bh,
376
+ font=ui.font)
377
+ if i == self.surface.settings["bank"]:
378
+ b.set_style_bg_color(lv.color_hex(BANK_COLORS[i]), 0)
379
+
380
+ @staticmethod
381
+ def _pad_of(g):
382
+ """The pad number of grid cell *g*: pad 0 at the bottom left, as pads count."""
383
+ row, col = divmod(g, 4)
384
+ return (3 - row) * 4 + col
385
+
386
+ def _pad_down(self, grid, g):
387
+ self.surface.press("pad%d" % self._pad_of(g), _frac_up(grid.pad(g)))
388
+ grid.set_color(g, BANK_COLORS[self.surface.settings["bank"]])
389
+
390
+ def _pad_up(self, grid, g):
391
+ self.surface.release("pad%d" % self._pad_of(g))
392
+ grid.set_color(g, None)
393
+
394
+ def _bank(self, i):
395
+ self.surface.set("bank", i)
396
+ self.later(self._build)
397
+
398
+ def _knobs(self):
399
+ W, BH, m = self.BW, self.BH, self.m
400
+ # 4 x 4, or 8 x 2 where that makes each knob bigger.
401
+ best = None
402
+ for cols, rows in ((4, 4), (8, 2)):
403
+ cw, ch = (W - 2 * m) // cols, BH // rows
404
+ size = min(cw, ch - 16)
405
+ if best is None or size > best[0]:
406
+ best = (size, cols, rows, cw, ch)
407
+ _, cols, rows, cw, ch = best
408
+ scale = min(self.W / 800, self.H / 480)
409
+ geom = _encoder.geometry(cw, ch, scale)
410
+ panel = _KnobPanel(self, scale)
411
+ x0 = (W - cols * cw) // 2
412
+ for i in range(16):
413
+ r, c = divmod(i, cols)
414
+ cid = "knob%d" % i
415
+ enc = _encoder.Encoder(panel, self.body, i, self.surface.describe(cid), x0 + c * cw, r * ch, cw, ch,
416
+ geom, BLACK)
417
+ enc.obj.remove_flag(lv.obj.FLAG.GESTURE_BUBBLE)
418
+ enc.set_value(self.surface.values.get(cid, 0))
419
+ self.widgets[cid] = enc.set_value
420
+ self.parts[cid] = enc
421
+
422
+ def _slider(self, cid, x, y, w, h, legend):
423
+ def changed(s, value):
424
+ self.moved(cid, value)
425
+
426
+ sl = Slider(self.body, x, y, w, h, BLACK, legend=legend, value=self.surface.values.get(cid, 0),
427
+ on_change=changed)
428
+ sl.obj.remove_flag(lv.obj.FLAG.GESTURE_BUBBLE)
429
+ self.widgets[cid] = sl.set_value
430
+ self.parts[cid] = sl
431
+ return sl
432
+
433
+ def _faders(self):
434
+ W, BH, m = self.BW, self.BH, self.m
435
+ cw = (W - 2 * m) // 16
436
+ x0 = (W - 16 * cw) // 2
437
+ for i in range(16):
438
+ self._slider("fader%d" % i, x0 + i * cw + 1, 0, cw - 2, BH, str(i + 1))
439
+
440
+ def _mixer(self):
441
+ W, BH, m = self.BW, self.BH, self.m
442
+ sw = (W - 2 * m) // 8
443
+ x0 = (W - 8 * sw) // 2
444
+ bh = max(18, min(34, BH // 10))
445
+ lamp_h = bh + 16 # a lamp button's legend sits over its cap
446
+ fader_h = BH - 2 * (lamp_h + 6)
447
+ for i in range(8):
448
+ x = x0 + i * sw
449
+ self._slider("mix%d" % i, x + 2, 0, sw - 4, fader_h, str(i + 1))
450
+ for j, (fam, lamp) in enumerate((("mute", 0xFFB300), ("solo", 0x3BA55C))):
451
+ cid = "%s%d" % (fam, i)
452
+
453
+ def pressed(b, on, cid=cid):
454
+ self.surface.press(cid)
455
+
456
+ b = LampButton(self.body, x + 4, fader_h + 6 + j * (lamp_h + 6), sw - 8, bh, BLACK,
457
+ legend=fam, toggle=True, lamp=lamp, on_press=pressed)
458
+ b.set_on(bool(self.surface.values.get(cid)))
459
+ self.widgets[cid] = lambda v, b=b: b.set_on(v >= 64)
460
+ self.parts[cid] = b
461
+
462
+ def _xy(self):
463
+ W, BH, m = self.BW, self.BH, self.m
464
+ s = min(BH, (W - 3 * m) // 2)
465
+ pad = ui.plain(lv.obj(self.body))
466
+ pad.set_pos(m, 0)
467
+ pad.set_size(s, s)
468
+ pad.set_style_bg_color(ui.PANEL, 0)
469
+ pad.set_style_bg_opa(lv.OPA.COVER, 0)
470
+ pad.set_style_radius(12, 0)
471
+ pad.add_flag(lv.obj.FLAG.CLICKABLE)
472
+ pad.remove_flag(lv.obj.FLAG.GESTURE_BUBBLE)
473
+ for w, h in ((s - 24, 1), (1, s - 24)):
474
+ line = ui.plain(lv.obj(pad))
475
+ line.set_size(w, h)
476
+ line.set_style_bg_color(ui.KEY, 0)
477
+ line.set_style_bg_opa(lv.OPA.COVER, 0)
478
+ line.center()
479
+ d = max(16, s // 10)
480
+ dot = ui.plain(lv.obj(pad))
481
+ dot.set_size(d, d)
482
+ dot.set_style_radius(lv.RADIUS_CIRCLE, 0)
483
+ dot.set_style_bg_color(ui.ACCENT, 0)
484
+ dot.set_style_bg_opa(lv.OPA.COVER, 0)
485
+ dot.remove_flag(lv.obj.FLAG.CLICKABLE)
486
+ reach = s - d
487
+ vals = self.surface.values
488
+
489
+ def place():
490
+ dot.set_pos(vals.get("x", 64) * reach // 127, reach - vals.get("y", 64) * reach // 127)
491
+
492
+ def touched(e):
493
+ p = _point()
494
+ if p is None:
495
+ return
496
+ a = _area(pad)
497
+ x = min(127, max(0, (p[0] - a.x1 - d // 2) * 127 // max(1, reach)))
498
+ y = min(127, max(0, (a.y2 - p[1] - d // 2) * 127 // max(1, reach)))
499
+ if x != vals.get("x"):
500
+ self.moved("x", x)
501
+ if y != vals.get("y"):
502
+ self.moved("y", y)
503
+ place()
504
+
505
+ pad.add_event_cb(touched, lv.EVENT.PRESSED, None)
506
+ pad.add_event_cb(touched, lv.EVENT.PRESSING, None)
507
+ place()
508
+ self.xy = pad
509
+ self.widgets["x"] = self.widgets["y"] = lambda v: place()
510
+
511
+ # The transport, three by two beside it.
512
+ x0 = m + s + m
513
+ cw = (W - x0 - m) // 3
514
+ bh = max(24, min(56, (BH - 2 * 16 - m) // 2))
515
+ y0 = (BH - 2 * (bh + 16) - m) // 2
516
+ for i, name in enumerate(TRANSPORT):
517
+ r, c = divmod(i, 3)
518
+ cid = "tr%d" % i
519
+
520
+ def pressed(b, on, cid=cid):
521
+ self.surface.press(cid)
522
+ self.surface.release(cid)
523
+
524
+ self.parts[cid] = LampButton(self.body, x0 + c * cw + 3, y0 + r * (bh + 16 + m), cw - 6, bh, BLACK,
525
+ legend=name, lamp=TRANSPORT_LAMPS[i], on_press=pressed)
526
+
527
+ def _keys(self):
528
+ W, BH, m = self.BW, self.BH, self.m
529
+ mod_w = max(40, W // 16)
530
+ self._slider("mod", m, 0, mod_w, BH, "Mod")
531
+ x0 = m + mod_w + m
532
+ row_h = max(30, min(44, BH // 6))
533
+ kw_all = W - x0 - m
534
+ oct_w = max(56, kw_all // 6)
535
+ _button(self.body, lv.SYMBOL.LEFT + " Oct", lambda: self._octave(-1), x0, 0, oct_w, row_h)
536
+ _button(self.body, "Oct " + lv.SYMBOL.RIGHT, lambda: self._octave(1), x0 + kw_all - oct_w, 0, oct_w, row_h)
537
+ lo = self.surface.key_note(0)
538
+ rng = ui.label(self.body, "%s to %s" % (note_name(max(0, lo)), note_name(min(127, lo + 23))), ui.small,
539
+ ui.MUTED)
540
+ rng.align_to(self.body, lv.ALIGN.TOP_LEFT, x0 + kw_all // 2 - 40, (row_h - ui.small.line_height) // 2)
541
+ rng.set_width(80)
542
+ rng.set_style_text_align(lv.TEXT_ALIGN.CENTER, 0)
543
+ top = row_h + m
544
+ kh = BH - top
545
+ whites = ui.plain(lv.obj(self.body))
546
+ whites.set_pos(x0, top)
547
+ whites.set_size(kw_all, kh)
548
+ blacks = ui.plain(lv.obj(self.body)) # over the whites, in a layer of its own
549
+ blacks.set_pos(x0, top)
550
+ blacks.set_size(kw_all, kh * 3 // 5)
551
+ blacks.remove_flag(lv.obj.FLAG.CLICKABLE) # a touch between black keys reaches the white key under it
552
+ ww = kw_all // 14
553
+ self.keys = {}
554
+ for octave in range(2):
555
+ for j, semi in enumerate(WHITE):
556
+ k = octave * 12 + semi
557
+ key = self._key(whites, k, (octave * 7 + j) * ww + 1, 0, ww - 2, kh, ui.WHITE)
558
+ if semi == 0:
559
+ lbl = ui.label(key, note_name(self.surface.key_note(k)) if lo + k <= 127 else "", ui.tiny,
560
+ lv.color_hex(0x404040))
561
+ lbl.align(lv.ALIGN.BOTTOM_MID, 0, -4)
562
+ for semi, after in BLACK_KEYS:
563
+ k = octave * 12 + semi
564
+ bw = ww * 3 // 5
565
+ self._key(blacks, k, (octave * 7 + after) * ww - bw // 2, 0, bw, kh * 3 // 5, lv.color_hex(0x15171C))
566
+
567
+ def _key(self, parent, k, x, y, w, h, color):
568
+ key = ui.plain(lv.obj(parent))
569
+ key.set_pos(x, y)
570
+ key.set_size(w, h)
571
+ key.set_style_bg_color(color, 0)
572
+ key.set_style_bg_opa(lv.OPA.COVER, 0)
573
+ key.set_style_bg_color(ui.ACCENT, lv.STATE.PRESSED)
574
+ key.set_style_radius(4, 0)
575
+ key.add_flag(lv.obj.FLAG.CLICKABLE)
576
+ key.remove_flag(lv.obj.FLAG.GESTURE_BUBBLE)
577
+ _on_edges(key, lambda: self.surface.key(k, True, _frac_down(key)), lambda: self.surface.key(k, False))
578
+ self.keys[k] = key
579
+ return key
580
+
581
+ def _octave(self, step):
582
+ self.surface.set("octave", self.surface.settings["octave"] + step)
583
+ self.later(self._build)
584
+
585
+ # -- setup, and editing the mapping ----------------------------------------------
586
+
587
+ def _setup(self):
588
+ W, BH, m = self.BW, self.BH, self.m
589
+ s = self.surface
590
+ rows = 4
591
+ rh = min(52, (BH - (rows - 1) * m) // rows)
592
+ name_w = max(96, W // 5)
593
+ bw = max(44, rh)
594
+ val_w = min(320, W - 2 * m - name_w - 2 * bw - 2 * m)
595
+
596
+ def row(i, name, value, minus, plus):
597
+ y = i * (rh + m)
598
+ ui.label(self.body, name, ui.small, ui.MUTED).set_pos(m, y + (rh - ui.small.line_height) // 2)
599
+ x = m + name_w
600
+ _button(self.body, lv.SYMBOL.LEFT, minus, x, y, bw, rh, font=ui.font)
601
+ v = ui.label(self.body, value, ui.small, ui.WHITE)
602
+ v.set_long_mode(lv.label.LONG_MODE.DOTS)
603
+ v.set_size(val_w, ui.small.line_height)
604
+ v.set_style_text_align(lv.TEXT_ALIGN.CENTER, 0)
605
+ v.set_pos(x + bw + m, y + (rh - ui.small.line_height) // 2)
606
+ _button(self.body, lv.SYMBOL.RIGHT, plus, x + bw + 2 * m + val_w, y, bw, rh, font=ui.font)
607
+
608
+ self.choices = links.choices()
609
+ names = dict(self.choices)
610
+ key = self.link.key if self.link is not None else s.settings["output"]
611
+ row(0, "Send to", self._out_name(key) or names.get(key) or "nowhere", lambda: self._output(-1), lambda: self._output(1))
612
+ row(1, "Channel", str(s.settings["channel"]), lambda: self._set("channel", -1),
613
+ lambda: self._set("channel", 1))
614
+ vel = s.settings["velocity"]
615
+ row(2, "Velocity", "from the touch" if not vel else "always %d" % vel, lambda: self._set("velocity", -1),
616
+ lambda: self._set("velocity", 1))
617
+ y = 3 * (rh + m)
618
+ n = 3
619
+ w = (W - 2 * m - (n - 1) * m) // n
620
+ b, _ = _button(self.body, "Edit mapping", self._toggle_edit, m, y, w, rh)
621
+ if self.edit:
622
+ b.set_style_bg_color(ui.ACCENT, 0)
623
+ _button(self.body, "Reset mapping", self._reset_all, m + (w + m), y, w, rh)
624
+ _button(self.body, "All notes off", s.panic, m + 2 * (w + m), y, w, rh)
625
+
626
+ def _chosen_now(self, label=None):
627
+ """The key of the output chosen in Connections now (or the listed one
628
+ called *label*), when there is one, else None."""
629
+ for key, name in self.choices:
630
+ if key.startswith("out:") and (name == label if label is not None else self._is_chosen(key[4:])):
631
+ return key
632
+ return None
633
+
634
+ @staticmethod
635
+ def _is_chosen(ep_id):
636
+ try:
637
+ import endpointkit
638
+ except ImportError:
639
+ return False
640
+ return ep_id in endpointkit.selection("midi.out")
641
+
642
+ def _out_name(self, key):
643
+ """What the controller plays when that's the Connections choice, or None."""
644
+ names = getattr(self.link, "names", None)
645
+ return names() if names is not None and key and key.startswith("out:") else None
646
+
647
+ def _output(self, step):
648
+ self.choices = links.choices() # a browser lists its outputs once the person says yes
649
+ keys = [k for k, _ in self.choices]
650
+ if not keys:
651
+ return
652
+ cur = self.link.key if self.link is not None else self.surface.settings["output"]
653
+ if cur not in keys or (cur.startswith("out:") and not self._is_chosen(cur[4:])):
654
+ cur = self._chosen_now() or cur # what Connections chose since
655
+ i = keys.index(cur) if cur in keys else -step
656
+ self.choose(keys[(i + step) % len(keys)])
657
+ self.later(self._build)
658
+
659
+ def _set(self, key, step):
660
+ v = self.surface.settings[key]
661
+ if key == "velocity":
662
+ # Along the ladder, from where v sits on it.
663
+ i = 0
664
+ while i + 1 < len(VELOCITIES) and VELOCITIES[i + 1] <= v:
665
+ i += 1
666
+ v = VELOCITIES[min(len(VELOCITIES) - 1, max(0, i + step))]
667
+ else:
668
+ v += step
669
+ self.surface.set(key, v)
670
+ self.later(self._build)
671
+
672
+ def _toggle_edit(self):
673
+ self.edit = not self.edit
674
+ self.later(self._build)
675
+
676
+ def _reset_all(self):
677
+ self.surface.reset()
678
+ self.later(self._build)
679
+
680
+ def _map(self):
681
+ """The view's controls as buttons in their places, each with what it sends."""
682
+ v = self.view
683
+ if v == "pads":
684
+ cids, cols = ["pad%d" % self._pad_of(g) for g in range(16)], 4
685
+ elif v == "knobs":
686
+ cids, cols = ["knob%d" % i for i in range(16)], 4
687
+ elif v == "faders":
688
+ cids, cols = ["fader%d" % i for i in range(16)], 4
689
+ elif v == "mixer":
690
+ cids, cols = ["%s%d" % (f, i) for f in ("mix", "mute", "solo") for i in range(8)], 8
691
+ elif v == "xy":
692
+ cids, cols = ["x", "y"] + ["tr%d" % i for i in range(len(TRANSPORT))], 4
693
+ else:
694
+ cids, cols = ["mod"], 1
695
+ W, BH, m = self.BW, self.BH, self.m
696
+ rows = (len(cids) + cols - 1) // cols
697
+ cw = (W - 2 * m) // cols
698
+ ch = min(72, BH // rows)
699
+ font = ui.small if cw >= 96 else ui.tiny
700
+ for i, cid in enumerate(cids):
701
+ r, c = divmod(i, cols)
702
+ b, lbl = _button(self.body, "%s\n%s" % (_name(cid), self.surface.describe(cid)),
703
+ lambda cid=cid: self._open_editor(cid), m + c * cw + 2, r * ch + 2, cw - 4, ch - 4,
704
+ font=font, color=ui.PANEL)
705
+ lbl.set_style_text_align(lv.TEXT_ALIGN.CENTER, 0)
706
+ self.parts["map:" + cid] = b
707
+ if v == "keys":
708
+ hint = ui.label(self.body, "The keys play the channel; Oct moves them.", ui.small, ui.MUTED)
709
+ hint.set_pos(m, ch + m)
710
+
711
+ def _open_editor(self, cid):
712
+ self.editing = cid
713
+ self.later(self._build)
714
+
715
+ def _editor(self, cid):
716
+ W, BH, m = self.BW, self.BH, self.m
717
+ s = self.surface
718
+ kind, num = s.map[cid]
719
+ title = ui.label(self.body, "%s sends %s" % (_name(cid), s.describe(cid)), ui.font, ui.WHITE)
720
+ title.set_pos(m, 0)
721
+ rh = min(52, (BH - ui.font.line_height - 3 * m) // 3)
722
+ y = ui.font.line_height + m
723
+ if is_button(cid):
724
+ w = (W - 3 * m) // 2
725
+ for i, (k, text) in enumerate(((NOTE, "Note"), (CC, "CC"))):
726
+ b, _ = _button(self.body, text, lambda k=k: self._remap(cid, kind=k), m + i * (w + m), y, w, rh)
727
+ if k == kind:
728
+ b.set_style_bg_color(ui.ACCENT, 0)
729
+ y += rh + m
730
+ steps = (-10, -1, 1, 10)
731
+ val_w = max(80, W // 5)
732
+ bw = (W - 2 * m - val_w - 4 * m) // 4
733
+ x = m
734
+ for i, step in enumerate(steps):
735
+ _button(self.body, "%+d" % step, lambda step=step: self._remap(cid, number=num + step), x, y, bw, rh,
736
+ font=ui.font)
737
+ x += bw + m
738
+ if i == 1:
739
+ v = ui.label(self.body, str(num), ui.big, ui.WHITE)
740
+ v.set_width(val_w)
741
+ v.set_style_text_align(lv.TEXT_ALIGN.CENTER, 0)
742
+ v.set_pos(x, y + (rh - ui.big.line_height) // 2)
743
+ x += val_w + m
744
+ y += rh + m
745
+ w = (W - 3 * m) // 2
746
+ _button(self.body, "Default", lambda: self._remap(cid, default=True), m, y, w, rh)
747
+ _button(self.body, "Done", self._close_editor, 2 * m + w, y, w, rh, color=ui.ACCENT)
748
+
749
+ def _remap(self, cid, kind=None, number=None, default=False):
750
+ if default:
751
+ self.surface.reset(cid)
752
+ else:
753
+ self.surface.remap(cid, kind=kind, number=number)
754
+ self.later(self._build)
755
+
756
+ def _close_editor(self):
757
+ self.editing = None
758
+ self.later(self._build)
759
+
760
+
761
+ def describe(msg):
762
+ """A sent message for the status line: ``Note C1 100``, ``CC 74 = 64``."""
763
+ status = msg[0] & 0xF0
764
+ if status == 0x90 and msg[2]:
765
+ return "Note %s %d" % (note_name(msg[1]), msg[2])
766
+ if status in (0x80, 0x90):
767
+ return "Note %s off" % note_name(msg[1])
768
+ if status == 0xB0:
769
+ return "CC %d = %d" % (msg[1], msg[2])
770
+ return " ".join("%02X" % b for b in msg)
771
+
772
+
773
+ def main(scope):
774
+ return Controller(scope)