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
Binary file
@@ -0,0 +1,138 @@
1
+ # SPDX-FileCopyrightText: 2026 Brad Barnett
2
+ #
3
+ # SPDX-License-Identifier: MIT
4
+ """What the app keeps across a reboot, in ``settings.json`` in its data folder.
5
+
6
+ The MIDI channel, Bluetooth on or off and the device let in over it, the
7
+ VOLUME knob, and the bank file and slot last played. appkit gives each app a
8
+ data folder (``/data/fmsynth`` on a board, appkit's data root on a computer)
9
+ but no settings file, so this is a small JSON one there. A missing or
10
+ unreadable file means the defaults, with a line printed to say so.
11
+
12
+ Writes are held back: a change is written once it has stood for `SETTLE_MS`,
13
+ so turning the VOLUME knob through twenty notches writes the flash once, and
14
+ `flush` writes whatever is waiting when the app closes.
15
+ """
16
+
17
+ import json
18
+
19
+ import time
20
+
21
+ if hasattr(time, "ticks_ms"):
22
+ _ticks = time.ticks_ms
23
+ _diff = time.ticks_diff
24
+ else: # CPython
25
+
26
+ def _ticks():
27
+ return int(time.monotonic() * 1000)
28
+
29
+ def _diff(a, b):
30
+ return a - b
31
+
32
+
33
+ #: The file's name in the app's data folder.
34
+ FILE = "settings.json"
35
+ #: How long a change has to stand before it is written.
36
+ SETTLE_MS = 2000
37
+ #: What is kept, and what it is when nothing was.
38
+ DEFAULTS = {"channel": 0, "ble": False, "ble_peer": None, "volume": None, "bank": None, "slot": 0}
39
+
40
+
41
+ def _join(folder, name):
42
+ return folder + name if folder.endswith("/") or folder.endswith("\\") else folder + "/" + name
43
+
44
+
45
+ class Settings:
46
+ """``settings.json`` in *folder*, or nothing kept when *folder* is None.
47
+
48
+ *ticks* stands in for the millisecond clock, for a test.
49
+ """
50
+
51
+ def __init__(self, folder, ticks=None):
52
+ self.path = _join(folder, FILE) if folder else None
53
+ self._ticks = ticks or _ticks
54
+ self._saved = None
55
+ self._pending = None
56
+ self._since = 0
57
+ #: How many times the file was written, for a test or a bench script.
58
+ self.writes = 0
59
+
60
+ def load(self):
61
+ """The kept settings over the defaults: a new dict every call."""
62
+ values = dict(DEFAULTS)
63
+ if self.path is None:
64
+ return values
65
+ try:
66
+ saved = self._read(self.path)
67
+ except OSError:
68
+ try:
69
+ # A reset between the two steps of a write leaves only this.
70
+ saved = self._read(self.path + ".new")
71
+ except (OSError, ValueError):
72
+ print("fmsynth: no %s yet, starting from the defaults" % FILE)
73
+ return values
74
+ except ValueError as e:
75
+ print("fmsynth: %s can't be read (%s), starting from the defaults" % (self.path, e))
76
+ return values
77
+ if not isinstance(saved, dict):
78
+ print("fmsynth: %s holds no settings, starting from the defaults" % (self.path,))
79
+ return values
80
+ for key in DEFAULTS:
81
+ if key in saved:
82
+ values[key] = saved[key]
83
+ self._saved = dict(values)
84
+ return values
85
+
86
+ @staticmethod
87
+ def _read(path):
88
+ with open(path, "rb") as f:
89
+ return json.loads(f.read().decode("utf-8"))
90
+
91
+ def update(self, values):
92
+ """Note the settings as they are now; write them once they've stood
93
+ for `SETTLE_MS`. Call it every few hundred milliseconds."""
94
+ if self.path is None:
95
+ return
96
+ values = dict(values)
97
+ if values == self._saved:
98
+ self._pending = None
99
+ return
100
+ now = self._ticks()
101
+ if values != self._pending:
102
+ self._pending = values
103
+ self._since = now
104
+ return
105
+ if _diff(now, self._since) >= SETTLE_MS:
106
+ self._write(values)
107
+
108
+ def flush(self, values=None):
109
+ """Write what is waiting now, or *values* if they differ from the file."""
110
+ if self.path is None:
111
+ return
112
+ if values is not None:
113
+ values = dict(values)
114
+ if values != self._saved:
115
+ self._write(values)
116
+ elif self._pending is not None:
117
+ self._write(self._pending)
118
+
119
+ def _write(self, values):
120
+ import os
121
+
122
+ # Written beside it first, so a reset mid-write leaves the old file.
123
+ tmp = self.path + ".new"
124
+ try:
125
+ with open(tmp, "wb") as f:
126
+ f.write(json.dumps(values).encode("utf-8"))
127
+ try:
128
+ os.remove(self.path) # rename won't replace a file on Windows
129
+ except OSError:
130
+ pass
131
+ os.rename(tmp, self.path)
132
+ except OSError as e:
133
+ print("fmsynth: settings not saved: %s" % (e,))
134
+ self._pending = None
135
+ return
136
+ self._saved = values
137
+ self._pending = None
138
+ self.writes += 1
apps/fmsynth/voice.py ADDED
@@ -0,0 +1,398 @@
1
+ # SPDX-FileCopyrightText: 2026 Brad Barnett
2
+ #
3
+ # SPDX-License-Identifier: MIT
4
+ """The voice and the bank: six operators and the rest, as the classic bytes.
5
+
6
+ A `Voice` is 155 bytes in the single-voice parameter order, and every
7
+ parameter is read and written by name:
8
+
9
+ voice = Voice()
10
+ voice.set("algorithm", 4) # stored 0 to 31, shown 1 to 32
11
+ voice.ops[0].set("level", 99) # ops[0] is operator 1
12
+ voice.to_bytes() # the 155 bytes
13
+
14
+ A `Bank` is 32 voices. It reads and writes the 4104-byte bulk dump that the
15
+ format's instruments and editors exchange, checksum included.
16
+
17
+ This is plain Python with no imports, so it runs anywhere and never needs
18
+ the sound engine.
19
+ """
20
+
21
+ VOICE_SIZE = 155
22
+ PACKED_SIZE = 128
23
+ VOICES = 32
24
+ BANK_SYSEX_SIZE = 4104
25
+ SINGLE_SYSEX_SIZE = 163
26
+ BANK_HEADER = b"\xf0\x43\x00\x09\x20\x00"
27
+ SINGLE_HEADER = b"\xf0\x43\x00\x00\x01\x1b"
28
+ OPERATORS = 6
29
+ OP_SIZE = 21
30
+ NAME_AT = 145
31
+ NAME_SIZE = 10
32
+
33
+ #: One operator's parameters: name, offset inside its 21 bytes, highest value.
34
+ OP_PARAMS = (
35
+ ("rate1", 0, 99),
36
+ ("rate2", 1, 99),
37
+ ("rate3", 2, 99),
38
+ ("rate4", 3, 99),
39
+ ("level1", 4, 99),
40
+ ("level2", 5, 99),
41
+ ("level3", 6, 99),
42
+ ("level4", 7, 99),
43
+ ("break_point", 8, 99),
44
+ ("left_depth", 9, 99),
45
+ ("right_depth", 10, 99),
46
+ ("left_curve", 11, 3),
47
+ ("right_curve", 12, 3),
48
+ ("rate_scale", 13, 7),
49
+ ("amp_mod", 14, 3),
50
+ ("velocity", 15, 7),
51
+ ("level", 16, 99),
52
+ ("fixed", 17, 1),
53
+ ("coarse", 18, 31),
54
+ ("fine", 19, 99),
55
+ ("detune", 20, 14),
56
+ )
57
+
58
+ #: The voice's own parameters: name, offset in the 155 bytes, highest value.
59
+ VOICE_PARAMS = (
60
+ ("pitch_rate1", 126, 99),
61
+ ("pitch_rate2", 127, 99),
62
+ ("pitch_rate3", 128, 99),
63
+ ("pitch_rate4", 129, 99),
64
+ ("pitch_level1", 130, 99),
65
+ ("pitch_level2", 131, 99),
66
+ ("pitch_level3", 132, 99),
67
+ ("pitch_level4", 133, 99),
68
+ ("algorithm", 134, 31),
69
+ ("feedback", 135, 7),
70
+ ("key_sync", 136, 1),
71
+ ("lfo_speed", 137, 99),
72
+ ("lfo_delay", 138, 99),
73
+ ("lfo_pitch_depth", 139, 99),
74
+ ("lfo_amp_depth", 140, 99),
75
+ ("lfo_sync", 141, 1),
76
+ ("lfo_wave", 142, 5),
77
+ ("lfo_pitch_sens", 143, 7),
78
+ ("transpose", 144, 48),
79
+ )
80
+
81
+ _OP = {name: (offset, top) for name, offset, top in OP_PARAMS}
82
+ _VOICE = {name: (offset, top) for name, offset, top in VOICE_PARAMS}
83
+
84
+ CURVES = ("-LIN", "-EXP", "+EXP", "+LIN")
85
+ LFO_WAVES = ("TRIANGLE", "SAW DOWN", "SAW UP", "SQUARE", "SINE", "S+HOLD")
86
+ NOTES = ("C", "C#", "D", "D#", "E", "F", "F#", "G", "G#", "A", "A#", "B")
87
+
88
+
89
+ def _init_bytes():
90
+ data = bytearray(VOICE_SIZE)
91
+ for number in range(1, OPERATORS + 1):
92
+ base = op_offset(number)
93
+ data[base:base + 8] = bytes((99, 99, 99, 99, 99, 99, 99, 0))
94
+ data[base + 8] = 39 # the break point, C3
95
+ data[base + 16] = 99 if number == 1 else 0
96
+ data[base + 18] = 1 # coarse: the note's own frequency
97
+ data[base + 20] = 7 # detune, centred
98
+ data[126:134] = bytes((99, 99, 99, 99, 50, 50, 50, 50))
99
+ data[136] = 1 # oscillators restart with each note
100
+ data[137] = 35
101
+ data[141] = 1
102
+ data[143] = 3
103
+ data[144] = 24 # transpose, centred
104
+ data[NAME_AT:NAME_AT + NAME_SIZE] = b"INIT VOICE"
105
+ return data
106
+
107
+
108
+ def param_max(name):
109
+ """The highest value parameter *name* takes, for an operator's or the voice's own."""
110
+ return (_OP[name] if name in _OP else _VOICE[name])[1]
111
+
112
+
113
+ def op_offset(number):
114
+ """Where operator *number* (1 to 6) starts in the 155 bytes. Operator 6 comes first."""
115
+ if not 1 <= number <= OPERATORS:
116
+ raise ValueError("operator must be 1 to 6, not %r" % (number,))
117
+ return (OPERATORS - number) * OP_SIZE
118
+
119
+
120
+ def checksum(data):
121
+ """The format's checksum of *data*: what makes the low seven bits of the sum zero."""
122
+ return (-sum(data)) & 0x7F
123
+
124
+
125
+ def _write(data, offset, top, name, value):
126
+ value = int(value)
127
+ if not 0 <= value <= top:
128
+ raise ValueError("%s must be 0 to %d, not %d" % (name, top, value))
129
+ data[offset] = value
130
+
131
+
132
+ class Operator:
133
+ """One of a voice's six operators, read and written in the voice's own bytes."""
134
+
135
+ def __init__(self, voice, number):
136
+ self.voice = voice
137
+ self.number = number
138
+ self.base = op_offset(number)
139
+
140
+ def offset(self, name):
141
+ """Where parameter *name* of this operator is in the 155 bytes."""
142
+ return self.base + _OP[name][0]
143
+
144
+ def get(self, name):
145
+ return self.voice.data[self.base + _OP[name][0]]
146
+
147
+ def set(self, name, value):
148
+ offset, top = _OP[name]
149
+ _write(self.voice.data, self.base + offset, top, name, value)
150
+
151
+ def rates(self):
152
+ return list(self.voice.data[self.base:self.base + 4])
153
+
154
+ def levels(self):
155
+ return list(self.voice.data[self.base + 4:self.base + 8])
156
+
157
+ def frequency_text(self):
158
+ """The operator's pitch as a musician reads it: a ratio, or a fixed frequency in Hz."""
159
+ coarse = self.get("coarse")
160
+ fine = self.get("fine")
161
+ if self.get("fixed"):
162
+ hz = (10.0 ** (coarse & 3)) * (10.0 ** (fine / 100.0))
163
+ return ("%.1fHz" if hz >= 100 else "%.2fHz" if hz >= 10 else "%.3fHz") % hz
164
+ ratio = (0.5 if coarse == 0 else float(coarse)) * (1 + fine / 100.0)
165
+ return "x%.2f" % ratio
166
+
167
+
168
+ class Voice:
169
+ """One voice: 155 bytes, read and written by parameter name."""
170
+
171
+ def __init__(self, data=None):
172
+ self.data = _init_bytes()
173
+ self.ops = tuple(Operator(self, number) for number in range(1, OPERATORS + 1))
174
+ if data is not None:
175
+ self.load_bytes(data)
176
+
177
+ # ---- parameters ---------------------------------------------------------
178
+
179
+ def op(self, number):
180
+ """Operator *number*, 1 to 6."""
181
+ if not 1 <= number <= OPERATORS:
182
+ raise ValueError("operator must be 1 to 6, not %r" % (number,))
183
+ return self.ops[number - 1]
184
+
185
+ @staticmethod
186
+ def offset(name):
187
+ """Where the voice parameter *name* is in the 155 bytes."""
188
+ return _VOICE[name][0]
189
+
190
+ def get(self, name):
191
+ return self.data[_VOICE[name][0]]
192
+
193
+ def set(self, name, value):
194
+ offset, top = _VOICE[name]
195
+ _write(self.data, offset, top, name, value)
196
+
197
+ def pitch_rates(self):
198
+ return list(self.data[126:130])
199
+
200
+ def pitch_levels(self):
201
+ return list(self.data[130:134])
202
+
203
+ @property
204
+ def name(self):
205
+ """Up to ten characters."""
206
+ raw = self.data[NAME_AT:NAME_AT + NAME_SIZE]
207
+ return "".join(chr(c) if 32 <= c < 127 else "?" for c in raw).rstrip()
208
+
209
+ @name.setter
210
+ def name(self, text):
211
+ raw = [ord(c) for c in str(text)]
212
+ if len(raw) > NAME_SIZE:
213
+ raise ValueError("a name is at most 10 characters, not %d" % len(raw))
214
+ for c in raw:
215
+ if not 32 <= c < 127:
216
+ raise ValueError("a name is plain ASCII")
217
+ raw += [32] * (NAME_SIZE - len(raw))
218
+ self.data[NAME_AT:NAME_AT + NAME_SIZE] = bytes(raw)
219
+
220
+ # ---- bytes ----------------------------------------------------------------
221
+
222
+ def to_bytes(self):
223
+ """The voice as 155 bytes, in the single-voice parameter order."""
224
+ return bytes(self.data)
225
+
226
+ def load_bytes(self, data):
227
+ """Take 155 bytes in place. A value past its parameter's range is brought down to it."""
228
+ if len(data) != VOICE_SIZE:
229
+ raise ValueError("a voice is 155 bytes, not %d" % len(data))
230
+ fresh = bytearray(data)
231
+ for number in range(1, OPERATORS + 1):
232
+ base = op_offset(number)
233
+ for _name, offset, top in OP_PARAMS:
234
+ if fresh[base + offset] > top:
235
+ fresh[base + offset] = top
236
+ for _name, offset, top in VOICE_PARAMS:
237
+ if fresh[offset] > top:
238
+ fresh[offset] = top
239
+ for i in range(NAME_AT, NAME_AT + NAME_SIZE):
240
+ fresh[i] &= 0x7F
241
+ self.data[:] = fresh
242
+
243
+ @classmethod
244
+ def from_bytes(cls, data):
245
+ """A voice from 155 bytes."""
246
+ return cls(data)
247
+
248
+ def copy(self):
249
+ return Voice(self.data)
250
+
251
+ def __eq__(self, other):
252
+ return isinstance(other, Voice) and self.data == other.data
253
+
254
+ def __ne__(self, other):
255
+ return not self.__eq__(other)
256
+
257
+ # ---- the single-voice message ---------------------------------------------
258
+
259
+ def to_sysex(self):
260
+ """The voice as a 163-byte single-voice system exclusive message."""
261
+ body = bytes(self.data)
262
+ return SINGLE_HEADER + body + bytes((checksum(body), 0xF7))
263
+
264
+ @classmethod
265
+ def from_sysex(cls, message):
266
+ """A voice from a 163-byte single-voice message, checked against its checksum."""
267
+ if len(message) != SINGLE_SYSEX_SIZE:
268
+ raise ValueError("a single-voice message is 163 bytes, not %d" % len(message))
269
+ if message[0] != 0xF0 or message[1] != 0x43 or bytes(message[3:6]) != SINGLE_HEADER[3:] \
270
+ or message[-1] != 0xF7:
271
+ raise ValueError("not a single-voice message")
272
+ body = bytes(message[6:6 + VOICE_SIZE])
273
+ if checksum(body) != message[-2]:
274
+ raise ValueError("the voice's checksum does not match")
275
+ return cls(body)
276
+
277
+ # ---- the packed form a bank holds --------------------------------------------
278
+
279
+ def pack(self):
280
+ """The voice as the 128 bytes a bank stores."""
281
+ data = self.data
282
+ out = bytearray(PACKED_SIZE)
283
+ for slot in range(OPERATORS):
284
+ src = slot * OP_SIZE
285
+ dst = slot * 17
286
+ out[dst:dst + 11] = data[src:src + 11]
287
+ out[dst + 11] = data[src + 11] | (data[src + 12] << 2)
288
+ out[dst + 12] = data[src + 13] | (data[src + 20] << 3)
289
+ out[dst + 13] = data[src + 14] | (data[src + 15] << 2)
290
+ out[dst + 14] = data[src + 16]
291
+ out[dst + 15] = data[src + 17] | (data[src + 18] << 1)
292
+ out[dst + 16] = data[src + 19]
293
+ out[102:110] = data[126:134]
294
+ out[110] = data[134]
295
+ out[111] = data[135] | (data[136] << 3)
296
+ out[112:116] = data[137:141]
297
+ out[116] = data[141] | (data[142] << 1) | (data[143] << 4)
298
+ out[117] = data[144]
299
+ out[118:128] = data[NAME_AT:NAME_AT + NAME_SIZE]
300
+ return bytes(out)
301
+
302
+ @classmethod
303
+ def unpack(cls, packed):
304
+ """A voice from the 128 bytes a bank stores."""
305
+ if len(packed) != PACKED_SIZE:
306
+ raise ValueError("a packed voice is 128 bytes, not %d" % len(packed))
307
+ data = bytearray(VOICE_SIZE)
308
+ for slot in range(OPERATORS):
309
+ src = slot * 17
310
+ dst = slot * OP_SIZE
311
+ data[dst:dst + 11] = packed[src:src + 11]
312
+ data[dst + 11] = packed[src + 11] & 3
313
+ data[dst + 12] = (packed[src + 11] >> 2) & 3
314
+ data[dst + 13] = packed[src + 12] & 7
315
+ data[dst + 20] = (packed[src + 12] >> 3) & 15
316
+ data[dst + 14] = packed[src + 13] & 3
317
+ data[dst + 15] = (packed[src + 13] >> 2) & 7
318
+ data[dst + 16] = packed[src + 14]
319
+ data[dst + 17] = packed[src + 15] & 1
320
+ data[dst + 18] = (packed[src + 15] >> 1) & 31
321
+ data[dst + 19] = packed[src + 16]
322
+ data[126:134] = packed[102:110]
323
+ data[134] = packed[110] & 31
324
+ data[135] = packed[111] & 7
325
+ data[136] = (packed[111] >> 3) & 1
326
+ data[137:141] = packed[112:116]
327
+ data[141] = packed[116] & 1
328
+ data[142] = (packed[116] >> 1) & 7
329
+ data[143] = (packed[116] >> 4) & 7
330
+ data[144] = packed[117]
331
+ data[NAME_AT:NAME_AT + NAME_SIZE] = packed[118:128]
332
+ return cls(data)
333
+
334
+
335
+ def note_name(value):
336
+ """A break point (0 to 99) as a note: 0 is A-1 and 39 is C3."""
337
+ note = value + 9
338
+ return "%s%d" % (NOTES[note % 12], note // 12 - 1)
339
+
340
+
341
+ class Bank:
342
+ """Thirty-two voices, and the 4104-byte bulk dump that carries them."""
343
+
344
+ def __init__(self, voices=None):
345
+ self.voices = [Voice() for _ in range(VOICES)]
346
+ if voices is not None:
347
+ voices = list(voices)
348
+ if len(voices) > VOICES:
349
+ raise ValueError("a bank holds 32 voices, not %d" % len(voices))
350
+ for i, voice in enumerate(voices):
351
+ self.voices[i] = voice.copy()
352
+
353
+ def __len__(self):
354
+ return VOICES
355
+
356
+ def __getitem__(self, index):
357
+ return self.voices[index]
358
+
359
+ def __setitem__(self, index, voice):
360
+ self.voices[index] = voice.copy()
361
+
362
+ def names(self):
363
+ return [voice.name for voice in self.voices]
364
+
365
+ def to_sysex(self):
366
+ """The bank as a 4104-byte bulk dump."""
367
+ body = bytearray()
368
+ for voice in self.voices:
369
+ body += voice.pack()
370
+ return BANK_HEADER + bytes(body) + bytes((checksum(body), 0xF7))
371
+
372
+ @classmethod
373
+ def from_sysex(cls, message):
374
+ """A bank from a 4104-byte bulk dump, checked against its checksum."""
375
+ if len(message) != BANK_SYSEX_SIZE:
376
+ raise ValueError("a bank is 4104 bytes, not %d" % len(message))
377
+ # The third byte carries the sender's MIDI channel, so it is not compared.
378
+ if message[0] != 0xF0 or message[1] != 0x43 or bytes(message[3:6]) != BANK_HEADER[3:] \
379
+ or message[-1] != 0xF7:
380
+ raise ValueError("not a 32-voice bank")
381
+ body = bytes(message[6:6 + VOICES * PACKED_SIZE])
382
+ if checksum(body) != message[-2]:
383
+ raise ValueError("the bank's checksum does not match")
384
+ bank = cls()
385
+ for i in range(VOICES):
386
+ bank.voices[i] = Voice.unpack(body[i * PACKED_SIZE:(i + 1) * PACKED_SIZE])
387
+ return bank
388
+
389
+ @classmethod
390
+ def load(cls, path):
391
+ """The bank in the file at *path*."""
392
+ with open(path, "rb") as f:
393
+ return cls.from_sysex(f.read())
394
+
395
+ def save(self, path):
396
+ """Write the bank to *path* as a bulk dump."""
397
+ with open(path, "wb") as f:
398
+ f.write(self.to_sysex())