omnipython-applibs 0.1.2.dev16__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.
- appkit/__init__.py +52 -0
- appkit/__init__.pyi +17 -0
- appkit/data.py +149 -0
- appkit/data.pyi +25 -0
- appkit/foreground.py +357 -0
- appkit/foreground.pyi +45 -0
- appkit/home.py +19 -0
- appkit/home.pyi +8 -0
- appkit/hover.py +59 -0
- appkit/hover.pyi +13 -0
- appkit/index.py +199 -0
- appkit/index.pyi +30 -0
- appkit/launcher.py +1075 -0
- appkit/launcher.pyi +96 -0
- appkit/manifest.py +75 -0
- appkit/manifest.pyi +8 -0
- appkit/net.py +167 -0
- appkit/net.pyi +34 -0
- appkit/places.py +202 -0
- appkit/places.pyi +39 -0
- appkit/py.typed +0 -0
- appkit/scope.py +636 -0
- appkit/scope.pyi +110 -0
- appkit/shell.py +121 -0
- appkit/shell.pyi +32 -0
- appkit/worker.py +409 -0
- appkit/worker.pyi +83 -0
- controlkit/__init__.py +19 -0
- controlkit/__init__.pyi +10 -0
- controlkit/_draw.py +88 -0
- controlkit/_safe.py +29 -0
- controlkit/dotmatrix.py +101 -0
- controlkit/dotmatrix.pyi +23 -0
- controlkit/encoder.py +297 -0
- controlkit/encoder.pyi +55 -0
- controlkit/font5x7.py +22 -0
- controlkit/font5x7.pyi +11 -0
- controlkit/lamp_button.py +92 -0
- controlkit/lamp_button.pyi +40 -0
- controlkit/meter.py +86 -0
- controlkit/meter.pyi +30 -0
- controlkit/mock.py +116 -0
- controlkit/mock.pyi +35 -0
- controlkit/pad_grid.py +113 -0
- controlkit/pad_grid.pyi +41 -0
- controlkit/py.typed +0 -0
- controlkit/slider.py +185 -0
- controlkit/slider.pyi +49 -0
- controlkit/sprites.py +279 -0
- controlkit/sprites.pyi +22 -0
- controlkit/theme.py +120 -0
- controlkit/theme.pyi +15 -0
- deskkit/__init__.py +74 -0
- deskkit/__init__.pyi +18 -0
- deskkit/forecast.py +261 -0
- deskkit/forecast.pyi +40 -0
- deskkit/localtime.py +215 -0
- deskkit/localtime.pyi +33 -0
- deskkit/py.typed +0 -0
- deskkit/shell.py +113 -0
- deskkit/shell.pyi +33 -0
- omnipython_applibs-0.1.2.dev16.dist-info/METADATA +90 -0
- omnipython_applibs-0.1.2.dev16.dist-info/RECORD +143 -0
- omnipython_applibs-0.1.2.dev16.dist-info/WHEEL +5 -0
- omnipython_applibs-0.1.2.dev16.dist-info/licenses/LICENSE +21 -0
- omnipython_applibs-0.1.2.dev16.dist-info/top_level.txt +9 -0
- programkit/__init__.py +27 -0
- programkit/__init__.pyi +8 -0
- programkit/board.py +56 -0
- programkit/board.pyi +20 -0
- programkit/press.py +63 -0
- programkit/press.pyi +17 -0
- programkit/py.typed +0 -0
- programkit/rows.py +149 -0
- programkit/rows.pyi +34 -0
- sayso/__init__.py +109 -0
- sayso/__init__.pyi +124 -0
- sayso/auth.py +811 -0
- sayso/auth.pyi +102 -0
- sayso/client.py +1286 -0
- sayso/client.pyi +138 -0
- sayso/keepalive.py +371 -0
- sayso/keepalive.pyi +53 -0
- sayso/local.py +85 -0
- sayso/local.pyi +28 -0
- sayso/object_specs.py +706 -0
- sayso/object_specs.pyi +8 -0
- sayso/objects.py +411 -0
- sayso/objects.pyi +854 -0
- sayso/py.typed +0 -0
- sayso/schema_coverage.py +210 -0
- sayso/schema_coverage.pyi +13 -0
- sayso/transport.py +426 -0
- sayso/transport.pyi +47 -0
- songmodel/__init__.py +30 -0
- songmodel/__init__.pyi +9 -0
- songmodel/link.py +189 -0
- songmodel/link.pyi +53 -0
- songmodel/model.py +238 -0
- songmodel/model.pyi +40 -0
- songmodel/page.html +225 -0
- songmodel/page_data.py +236 -0
- songmodel/page_data.pyi +6 -0
- songmodel/py.typed +0 -0
- soundmodule/__init__.py +7 -0
- soundmodule/__init__.pyi +6 -0
- soundmodule/engine.py +1001 -0
- soundmodule/engine.pyi +196 -0
- soundmodule/names.py +298 -0
- soundmodule/names.pyi +30 -0
- soundmodule/pipes.py +105 -0
- soundmodule/pipes.pyi +31 -0
- soundmodule/py.typed +0 -0
- soundmodule/run.py +249 -0
- soundmodule/run.pyi +49 -0
- spectrum/__init__.py +72 -0
- spectrum/__init__.pyi +30 -0
- spectrum/analyzer.py +231 -0
- spectrum/analyzer.pyi +67 -0
- spectrum/fake_music.py +201 -0
- spectrum/fake_music.pyi +36 -0
- spectrum/pump_levels.py +126 -0
- spectrum/pump_levels.pyi +46 -0
- spectrum/py.typed +0 -0
- spectrum/spectrum_view.py +403 -0
- spectrum/spectrum_view.pyi +80 -0
- touchkit/__init__.py +4 -0
- touchkit/__init__.pyi +4 -0
- touchkit/cam.py +146 -0
- touchkit/cam.pyi +35 -0
- touchkit/clock.py +18 -0
- touchkit/clock.pyi +6 -0
- touchkit/ecp.py +134 -0
- touchkit/ecp.pyi +28 -0
- touchkit/net.py +347 -0
- touchkit/net.pyi +27 -0
- touchkit/py.typed +0 -0
- touchkit/settings.py +85 -0
- touchkit/settings.pyi +17 -0
- touchkit/shell.py +268 -0
- touchkit/shell.pyi +42 -0
- touchkit/ui.py +167 -0
- touchkit/ui.pyi +53 -0
soundmodule/engine.py
ADDED
|
@@ -0,0 +1,1001 @@
|
|
|
1
|
+
# SPDX-FileCopyrightText: 2026 PyDevices
|
|
2
|
+
#
|
|
3
|
+
# SPDX-License-Identifier: MIT
|
|
4
|
+
"""The sound module's engine: sixteen parts, one per MIDI channel, mixed
|
|
5
|
+
through chorus and reverb. It has no face of its own: feed it MIDI bytes
|
|
6
|
+
and pull its ``output``, from a UI (midiEngine), a headless loop
|
|
7
|
+
(``soundmodule.run``) or a test.
|
|
8
|
+
|
|
9
|
+
What a part plays
|
|
10
|
+
-----------------
|
|
11
|
+
Bank Select (CC0, the MSB, and CC32, the LSB) then Program Change choose:
|
|
12
|
+
|
|
13
|
+
- **MSB 0-79: General MIDI**, from a SoundFont. ``soundfont`` plays the
|
|
14
|
+
melodic channels and ``soundfont_drums`` channel 10, and the bank select
|
|
15
|
+
reaches the SoundFont as it is, so a GS or XG variation bank plays the
|
|
16
|
+
variation the bank has. Program Change is the GM program.
|
|
17
|
+
- **MSB 80: audioinstruments' instruments** (``INSTRUMENTS``). Program
|
|
18
|
+
Change *n* plays the *n*-th of them, and the LSB picks its patch.
|
|
19
|
+
- **MSB 81: audioinstruments' drum machines** (``DRUM_MACHINES``), the same
|
|
20
|
+
way.
|
|
21
|
+
|
|
22
|
+
A part's instrument is made the first time its channel is played or given
|
|
23
|
+
a program, and made again when a Program Change asks for a different one.
|
|
24
|
+
Nothing has to be set up first: a part that has had no Bank Select plays
|
|
25
|
+
General MIDI, as a GM module's do.
|
|
26
|
+
|
|
27
|
+
Controllers
|
|
28
|
+
-----------
|
|
29
|
+
Each macro of the part's instrument is a CC: macro *i* is CC ``MACRO_CC +
|
|
30
|
+
i`` (102 to 117, controllers MIDI leaves undefined), at the instrument's
|
|
31
|
+
own 0-127 scale. Volume (CC7) reaches a SoundFont part as CC7, which plays
|
|
32
|
+
it as the square law GM modules use (40 log10 of CC7/127 in dB, as
|
|
33
|
+
Expression), and pan (CC10) its Pan macro; on an instrument part both set
|
|
34
|
+
its level and pan in the mixer. Expression (CC11) is the same square law on
|
|
35
|
+
every part, multiplied with volume, and a part keeps it when its instrument is
|
|
36
|
+
made again (a bank or program change to another instrument, or All Sound Off).
|
|
37
|
+
The sustain pedal (CC64) holds notes for every part, and Reset All
|
|
38
|
+
Controllers (121) also puts Expression (CC11) back to 127 and the mod wheel
|
|
39
|
+
and channel pressure to 0, as GM's RP-015 asks.
|
|
40
|
+
|
|
41
|
+
Registered parameters (RPN, CC101 and CC100, then Data Entry on CC6 and
|
|
42
|
+
CC38 or Increment and Decrement on CC96 and CC97) set a part's Pitch Bend
|
|
43
|
+
Sensitivity (RPN 0, two semitones at power-on, up to 24), Channel Fine
|
|
44
|
+
Tuning (RPN 1, a hundred cents either way) and Channel Coarse Tuning (RPN 2,
|
|
45
|
+
whole semitones), and RPN 127/127 (null) or any NRPN stops Data Entry
|
|
46
|
+
reaching them. Increment and Decrement step the bend range and the coarse
|
|
47
|
+
tuning a semitone and the fine tuning one 14-bit step. A SoundFont part
|
|
48
|
+
plays them (``soundfont``'s ``bend_range`` and ``tuning``); an
|
|
49
|
+
audioinstruments from before those properties gets the bend scaled into its
|
|
50
|
+
fixed two semitones, which a range under two plays right and a wider one
|
|
51
|
+
clips. audioinstruments' synths and drum machines don't bend, so nothing
|
|
52
|
+
reaches them. Reset All Controllers sets the RPN back to null and keeps the
|
|
53
|
+
values; GM System On puts the values back too. Reverb (CC91) and chorus
|
|
54
|
+
(CC93) are each part's send, but there is one reverb and one chorus on the
|
|
55
|
+
master, so the master's level is the mean of the sends of the parts in use
|
|
56
|
+
(the ones with an instrument), whatever order they came in. A true send per
|
|
57
|
+
part needs a bus in the sampler's kernel, which it doesn't have; rendering a
|
|
58
|
+
part's voices twice to make one would cost more than a board's block.
|
|
59
|
+
All Sound Off, Reset All Controllers and All Notes Off (120, 121, 123) do
|
|
60
|
+
what GM says. Any other controller reaches the instrument's
|
|
61
|
+
``control_change`` as it is.
|
|
62
|
+
|
|
63
|
+
System Exclusive
|
|
64
|
+
----------------
|
|
65
|
+
GM System On (and GM2 On, GS Reset and XG System On, which ask the same)
|
|
66
|
+
put every part back to its power-on state. The Identity Request (``F0 7E
|
|
67
|
+
<id> 06 01 F7``) is answered with an Identity Reply through ``send``, the
|
|
68
|
+
callable the module writes MIDI out with: manufacturer ``0x7D`` (the ID
|
|
69
|
+
MIDI sets aside for non-commercial use), family ``FAMILY``, model ``MODEL``
|
|
70
|
+
and ``VERSION``.
|
|
71
|
+
|
|
72
|
+
The voices
|
|
73
|
+
----------
|
|
74
|
+
Every SoundFont part reads the same open SoundFont and plays on the same
|
|
75
|
+
pool of voices (``VOICES`` of them, one sampler), as a hardware GM module's
|
|
76
|
+
parts do: a busy part takes voices an idle one isn't using, and the oldest
|
|
77
|
+
releasing voices in the module are faded out to keep a few ready, so a new
|
|
78
|
+
note never has to cut one off with a click. Every voice plays at ``GAIN``,
|
|
79
|
+
the headroom sixteen parts summed need. An instrument part has its own
|
|
80
|
+
voices, and its output is one voice of a mixer the pool plays into too.
|
|
81
|
+
|
|
82
|
+
``prepare()`` opens the SoundFont before anything plays. Reading a big
|
|
83
|
+
bank's presets takes a moment and some megabytes, and done on the first
|
|
84
|
+
program change it would land inside whatever sent it. Once it is open a
|
|
85
|
+
part costs only its patch list, and its samples are read on its notes, the
|
|
86
|
+
bank keeping a bounded number of them: on a board, half the memory free
|
|
87
|
+
once the bank is open (``CACHE_SHARE``); on a desktop, soundfont's own
|
|
88
|
+
bound. An audioinstruments from before that bound keeps every sample it
|
|
89
|
+
reads; then the module lets them all go whenever they pass the bound, or
|
|
90
|
+
``FALLBACK_CACHE_BYTES`` if that is less.
|
|
91
|
+
|
|
92
|
+
No UI and no sound card in this module, so a test can drive it on CPython,
|
|
93
|
+
MicroPython or CircuitPython with stand-in instruments (``create=``).
|
|
94
|
+
"""
|
|
95
|
+
|
|
96
|
+
import sys
|
|
97
|
+
|
|
98
|
+
DRUM_CHANNEL = 9 # MIDI channel 10
|
|
99
|
+
MELODIC = "soundfont"
|
|
100
|
+
DRUMS = "soundfont_drums"
|
|
101
|
+
PERCUSSION_BANK = 128
|
|
102
|
+
|
|
103
|
+
#: Bank Select MSBs: below ``BANK_INSTRUMENTS`` is General MIDI.
|
|
104
|
+
BANK_INSTRUMENTS = 80
|
|
105
|
+
BANK_DRUM_MACHINES = 81
|
|
106
|
+
|
|
107
|
+
#: audioinstruments' melodic instruments, by program number on bank
|
|
108
|
+
#: ``BANK_INSTRUMENTS``. New ones go on the end, so a song's program numbers
|
|
109
|
+
#: keep their sound. ``None`` is a program an earlier instrument set had and
|
|
110
|
+
#: this one doesn't: it plays nothing, and keeps the numbers after it where
|
|
111
|
+
#: they were.
|
|
112
|
+
INSTRUMENTS = (
|
|
113
|
+
"polya6", "patch2600", "tonewheel3", "clavichord", "electric_grand70",
|
|
114
|
+
"poly80", None, None, None, None, None, "compact_organ", None, None,
|
|
115
|
+
"poly106", "poly8", None, None, None, None, "mono_synth", "duo20", None,
|
|
116
|
+
"west_coast_synth", None, "polyxa", "duosynth", "reed_piano", "poly6",
|
|
117
|
+
None, "poly5", None, "electric_piano", "mono101", "string_ensemble",
|
|
118
|
+
"bass_pedals", "acid303", None, None, "combo_organ", "vocoder330",
|
|
119
|
+
"hybrid_mono", "reed_piano200a", "mono_one", "state_variable_mono",
|
|
120
|
+
)
|
|
121
|
+
|
|
122
|
+
#: audioinstruments' drum machines, by program number on bank
|
|
123
|
+
#: ``BANK_DRUM_MACHINES``, with ``None`` as above.
|
|
124
|
+
DRUM_MACHINES = (
|
|
125
|
+
None, "rhythm78", None, None, None, None, "electronic_drums5", None,
|
|
126
|
+
"rhythm606", None, "rhythm808", "rhythm909",
|
|
127
|
+
)
|
|
128
|
+
|
|
129
|
+
CC_BANK_MSB, CC_BANK_LSB = 0, 32
|
|
130
|
+
CC_VOLUME, CC_PAN, CC_SUSTAIN = 7, 10, 64
|
|
131
|
+
CC_REVERB, CC_CHORUS = 91, 93
|
|
132
|
+
CC_ALL_SOUND_OFF, CC_RESET_CONTROLLERS, CC_ALL_NOTES_OFF = 120, 121, 123
|
|
133
|
+
CC_MOD_WHEEL, CC_EXPRESSION = 1, 11
|
|
134
|
+
CC_DATA_MSB, CC_DATA_LSB, CC_DATA_INC, CC_DATA_DEC = 6, 38, 96, 97
|
|
135
|
+
CC_NRPN_LSB, CC_NRPN_MSB, CC_RPN_LSB, CC_RPN_MSB = 98, 99, 100, 101
|
|
136
|
+
#: The registered parameters a part keeps, by (MSB, LSB): MIDI's numbers.
|
|
137
|
+
RPN_BEND_RANGE, RPN_FINE_TUNING, RPN_COARSE_TUNING = (0, 0), (0, 1), (0, 2)
|
|
138
|
+
RPN_NULL = (127, 127)
|
|
139
|
+
#: Their power-on values, as 14-bit Data Entry (MSB << 7 | LSB): two
|
|
140
|
+
#: semitones of bend, and both tunings centred.
|
|
141
|
+
RPN_DEFAULTS = {RPN_BEND_RANGE: 2 << 7, RPN_FINE_TUNING: 8192, RPN_COARSE_TUNING: 64 << 7}
|
|
142
|
+
MAX_BEND_RANGE = 24 # semitones, as GM2 and most modules cap it
|
|
143
|
+
#: The bend an audioinstruments without ``bend_range`` plays at full deflection.
|
|
144
|
+
NATIVE_BEND_RANGE = 2.0
|
|
145
|
+
#: Macro *i* of a part's instrument is CC ``MACRO_CC + i``.
|
|
146
|
+
MACRO_CC = 102
|
|
147
|
+
MACRO_CCS = 16
|
|
148
|
+
|
|
149
|
+
#: The Identity Reply's fields. 0x7D is MIDI's ID for non-commercial use.
|
|
150
|
+
MANUFACTURER = 0x7D
|
|
151
|
+
FAMILY = 0x0001
|
|
152
|
+
MODEL = 0x0001
|
|
153
|
+
VERSION = (0, 1, 0, 0)
|
|
154
|
+
#: The SysEx device ID this module answers to, as well as 0x7F (everyone).
|
|
155
|
+
DEVICE_ID = 0x7F
|
|
156
|
+
|
|
157
|
+
DEFAULT_VOLUME = 100
|
|
158
|
+
DEFAULT_PAN = 64
|
|
159
|
+
DEFAULT_EXPRESSION = 127
|
|
160
|
+
# The soundfont instruments' macros that CC7 and CC10 drive.
|
|
161
|
+
VOLUME_MACRO, PAN_MACRO = 0, 4
|
|
162
|
+
DEFAULT_REVERB = 40 # GM Level 1's power-on reverb send; chorus is 0
|
|
163
|
+
DEFAULT_CHORUS = 0
|
|
164
|
+
DEFAULT_MASTER = 80 # the output's level, 0-100
|
|
165
|
+
VOICES = 64 # the module's polyphony, shared by the sixteen parts
|
|
166
|
+
#: A board: MicroPython on a microcontroller, where one core renders the
|
|
167
|
+
#: audio. On an ESP32-P4 a dense orchestral file at 64 voices with chorus
|
|
168
|
+
#: and reverb takes about three quarters of each 5.3 ms block, which a
|
|
169
|
+
#: denser file or a slower chip would overrun; a board gets 48 voices and no
|
|
170
|
+
#: chorus (its delay line costs 0.4 ms a block even at a level of 0), about
|
|
171
|
+
#: 3 ms a block. ``voice_count`` and ``chorus=True`` take them back.
|
|
172
|
+
BOARD = sys.implementation.name in ("micropython", "circuitpython") and sys.platform not in (
|
|
173
|
+
"linux", "darwin", "win32", "windows", "webassembly")
|
|
174
|
+
BOARD_VOICES = 48
|
|
175
|
+
RESERVE = 8 # voices kept ready by fading the oldest releases, so a steal is silent rather than a click
|
|
176
|
+
GAIN = 0.2 # every voice's level: headroom for sixteen parts summed, as GM players run these banks
|
|
177
|
+
#: An instrument part's mixer level at full volume: about where one of
|
|
178
|
+
#: them sits beside a SoundFont part at the same CC7.
|
|
179
|
+
PART_LEVEL = 0.35
|
|
180
|
+
BEND_CENTER = 8192
|
|
181
|
+
CACHE_SHARE = 2 # on a board, the bank keeps samples up to the free memory over this, once it is open
|
|
182
|
+
# The most sample bytes a bank may keep when audioinstruments' soundfont has
|
|
183
|
+
# no bound of its own: soundfont's own defaults, a board's and a desktop's.
|
|
184
|
+
FALLBACK_CACHE_BYTES = (64 if sys.implementation.name == "cpython" else 8) * 1048576
|
|
185
|
+
MIXER_BUFFER = 2048 # bytes: 512 stereo frames, about 11 ms at 48 kHz, so a live note waits little
|
|
186
|
+
PARTS_MIXER_BUFFER = 4096 # the stand-ins' mixer, as it always was
|
|
187
|
+
|
|
188
|
+
#: The 128 General MIDI programs, by number.
|
|
189
|
+
GM_NAMES = (
|
|
190
|
+
"Acoustic Grand Piano", "Bright Acoustic Piano", "Electric Grand Piano", "Honky-tonk Piano",
|
|
191
|
+
"Electric Piano 1", "Electric Piano 2", "Harpsichord", "Clavinet",
|
|
192
|
+
"Celesta", "Glockenspiel", "Music Box", "Vibraphone", "Marimba", "Xylophone", "Tubular Bells", "Dulcimer",
|
|
193
|
+
"Drawbar Organ", "Percussive Organ", "Rock Organ", "Church Organ",
|
|
194
|
+
"Reed Organ", "Accordion", "Harmonica", "Tango Accordion",
|
|
195
|
+
"Acoustic Guitar (nylon)", "Acoustic Guitar (steel)", "Electric Guitar (jazz)", "Electric Guitar (clean)",
|
|
196
|
+
"Electric Guitar (muted)", "Overdriven Guitar", "Distortion Guitar", "Guitar Harmonics",
|
|
197
|
+
"Acoustic Bass", "Electric Bass (finger)", "Electric Bass (pick)", "Fretless Bass",
|
|
198
|
+
"Slap Bass 1", "Slap Bass 2", "Synth Bass 1", "Synth Bass 2",
|
|
199
|
+
"Violin", "Viola", "Cello", "Contrabass", "Tremolo Strings", "Pizzicato Strings", "Orchestral Harp", "Timpani",
|
|
200
|
+
"String Ensemble 1", "String Ensemble 2", "Synth Strings 1", "Synth Strings 2",
|
|
201
|
+
"Choir Aahs", "Voice Oohs", "Synth Voice", "Orchestra Hit",
|
|
202
|
+
"Trumpet", "Trombone", "Tuba", "Muted Trumpet", "French Horn", "Brass Section", "Synth Brass 1", "Synth Brass 2",
|
|
203
|
+
"Soprano Sax", "Alto Sax", "Tenor Sax", "Baritone Sax", "Oboe", "English Horn", "Bassoon", "Clarinet",
|
|
204
|
+
"Piccolo", "Flute", "Recorder", "Pan Flute", "Blown Bottle", "Shakuhachi", "Whistle", "Ocarina",
|
|
205
|
+
"Lead 1 (square)", "Lead 2 (sawtooth)", "Lead 3 (calliope)", "Lead 4 (chiff)",
|
|
206
|
+
"Lead 5 (charang)", "Lead 6 (voice)", "Lead 7 (fifths)", "Lead 8 (bass + lead)",
|
|
207
|
+
"Pad 1 (new age)", "Pad 2 (warm)", "Pad 3 (polysynth)", "Pad 4 (choir)",
|
|
208
|
+
"Pad 5 (bowed)", "Pad 6 (metallic)", "Pad 7 (halo)", "Pad 8 (sweep)",
|
|
209
|
+
"FX 1 (rain)", "FX 2 (soundtrack)", "FX 3 (crystal)", "FX 4 (atmosphere)",
|
|
210
|
+
"FX 5 (brightness)", "FX 6 (goblins)", "FX 7 (echoes)", "FX 8 (sci-fi)",
|
|
211
|
+
"Sitar", "Banjo", "Shamisen", "Koto", "Kalimba", "Bag pipe", "Fiddle", "Shanai",
|
|
212
|
+
"Tinkle Bell", "Agogo", "Steel Drums", "Woodblock", "Taiko Drum", "Melodic Tom", "Synth Drum", "Reverse Cymbal",
|
|
213
|
+
"Guitar Fret Noise", "Breath Noise", "Seashore", "Bird Tweet", "Telephone Ring", "Helicopter", "Applause", "Gunshot",
|
|
214
|
+
)
|
|
215
|
+
|
|
216
|
+
#: GM's standard drum kits, by program number, for a bank that has no kit
|
|
217
|
+
#: at the number asked for (the nearest lower one plays, as on a GS module).
|
|
218
|
+
GM_KITS = ((0, "Standard"), (8, "Room"), (16, "Power"), (24, "Electronic"), (25, "TR-808"),
|
|
219
|
+
(32, "Jazz"), (40, "Brush"), (48, "Orchestra"), (56, "SFX"))
|
|
220
|
+
|
|
221
|
+
|
|
222
|
+
def board_cache_bytes():
|
|
223
|
+
"""The bound for the open bank's samples on MicroPython and
|
|
224
|
+
CircuitPython: the memory free now over ``CACHE_SHARE``. None on
|
|
225
|
+
CPython, which keeps soundfont's."""
|
|
226
|
+
if sys.implementation.name == "cpython":
|
|
227
|
+
return None
|
|
228
|
+
try:
|
|
229
|
+
import gc
|
|
230
|
+
|
|
231
|
+
gc.collect()
|
|
232
|
+
return gc.mem_free() // CACHE_SHARE
|
|
233
|
+
except (ImportError, AttributeError):
|
|
234
|
+
return None
|
|
235
|
+
|
|
236
|
+
|
|
237
|
+
def _shared_voices():
|
|
238
|
+
"""``soundfont.Voices``, or None from an audioinstruments that predates
|
|
239
|
+
it: then each part has its own voices, summed by a mixer."""
|
|
240
|
+
try:
|
|
241
|
+
from audioinstruments._rack.soundfont import Voices
|
|
242
|
+
except ImportError:
|
|
243
|
+
return None
|
|
244
|
+
return Voices
|
|
245
|
+
|
|
246
|
+
|
|
247
|
+
def _default_create(name, rate, channels, bank, voice_pool=None):
|
|
248
|
+
import audioinstruments
|
|
249
|
+
if name not in (MELODIC, DRUMS):
|
|
250
|
+
return audioinstruments.create(name, rate, channel_count=channels)
|
|
251
|
+
if voice_pool is None:
|
|
252
|
+
return audioinstruments.create(name, rate, channel_count=channels, bank=bank)
|
|
253
|
+
try:
|
|
254
|
+
return audioinstruments.create(name, rate, channel_count=channels, bank=bank, voice_pool=voice_pool)
|
|
255
|
+
except TypeError: # an audioinstruments from before the pool was voice_pool=
|
|
256
|
+
return audioinstruments.create(name, rate, channel_count=channels, bank=bank, voices=voice_pool)
|
|
257
|
+
|
|
258
|
+
|
|
259
|
+
def identity_reply(device_id=DEVICE_ID):
|
|
260
|
+
"""The Identity Reply this module sends, F0 to F7."""
|
|
261
|
+
return bytes((0xF0, 0x7E, device_id, 0x06, 0x02, MANUFACTURER,
|
|
262
|
+
FAMILY & 0x7F, FAMILY >> 7, MODEL & 0x7F, MODEL >> 7) + tuple(VERSION) + (0xF7,))
|
|
263
|
+
|
|
264
|
+
|
|
265
|
+
class _Now:
|
|
266
|
+
"""A context that changes nothing: writes go straight to the voices."""
|
|
267
|
+
|
|
268
|
+
def __enter__(self):
|
|
269
|
+
return self
|
|
270
|
+
|
|
271
|
+
def __exit__(self, *exc):
|
|
272
|
+
return False
|
|
273
|
+
|
|
274
|
+
|
|
275
|
+
_NOW = _Now()
|
|
276
|
+
|
|
277
|
+
|
|
278
|
+
def deferred(module):
|
|
279
|
+
"""``with engine.deferred(module):`` queues the voices' writes and sends
|
|
280
|
+
them together at the end, the audio pump parked once for all of them
|
|
281
|
+
(``soundfont.Voices.deferred``). Each write alone waits for the block
|
|
282
|
+
the pump is pulling, which on a busy board is most of a block. Changes
|
|
283
|
+
nothing for a module without shared voices, or no module."""
|
|
284
|
+
pool = getattr(module, "voice_pool", None)
|
|
285
|
+
if pool is not None and hasattr(pool, "deferred"):
|
|
286
|
+
return pool.deferred()
|
|
287
|
+
return _NOW
|
|
288
|
+
|
|
289
|
+
|
|
290
|
+
class Part:
|
|
291
|
+
"""One MIDI channel's state. ``inst`` is None until the channel is used,
|
|
292
|
+
and ``inst_name`` is what it is (``MELODIC``, ``DRUMS`` or one of
|
|
293
|
+
``INSTRUMENTS`` and ``DRUM_MACHINES``)."""
|
|
294
|
+
|
|
295
|
+
def __init__(self, channel):
|
|
296
|
+
self.channel = channel
|
|
297
|
+
self.inst = None
|
|
298
|
+
self.inst_name = None
|
|
299
|
+
self.notes = 0 # note-ons so far, for an activity light
|
|
300
|
+
self.last = None # (note, velocity) of the last note-on
|
|
301
|
+
# What the part has played: (bank MSB, bank LSB, program) to
|
|
302
|
+
# [note-ons, the name of what sounded]. A sequencer may move the
|
|
303
|
+
# bank back when it stops, so the selection at the end can differ.
|
|
304
|
+
self.played = {}
|
|
305
|
+
self.reset()
|
|
306
|
+
|
|
307
|
+
def reset(self):
|
|
308
|
+
self.program = 0
|
|
309
|
+
self.bank_msb = 0
|
|
310
|
+
self.bank_lsb = 0
|
|
311
|
+
self.volume = DEFAULT_VOLUME
|
|
312
|
+
self.expression = DEFAULT_EXPRESSION
|
|
313
|
+
self.pan = DEFAULT_PAN
|
|
314
|
+
self.sustain = False
|
|
315
|
+
self.held = set()
|
|
316
|
+
self.sustained = set()
|
|
317
|
+
self.bend = BEND_CENTER
|
|
318
|
+
self.rpn = RPN_NULL # the parameter Data Entry sets; None for an NRPN
|
|
319
|
+
self.params = dict(RPN_DEFAULTS)
|
|
320
|
+
self.reverb = DEFAULT_REVERB # CC91, the part's send
|
|
321
|
+
self.chorus = DEFAULT_CHORUS # CC93
|
|
322
|
+
|
|
323
|
+
@property
|
|
324
|
+
def bend_range(self):
|
|
325
|
+
"""Pitch Bend Sensitivity in semitones, cents included."""
|
|
326
|
+
v = self.params[RPN_BEND_RANGE]
|
|
327
|
+
return min(MAX_BEND_RANGE, (v >> 7) + (v & 0x7F) / 100.0)
|
|
328
|
+
|
|
329
|
+
@property
|
|
330
|
+
def tuning(self):
|
|
331
|
+
"""Fine and coarse tuning together, in semitones."""
|
|
332
|
+
return ((self.params[RPN_COARSE_TUNING] >> 7) - 64) + (self.params[RPN_FINE_TUNING] - 8192) / 8192.0
|
|
333
|
+
|
|
334
|
+
@property
|
|
335
|
+
def drums(self):
|
|
336
|
+
return self.channel == DRUM_CHANNEL
|
|
337
|
+
|
|
338
|
+
@property
|
|
339
|
+
def gm(self):
|
|
340
|
+
"""True when the part's bank is General MIDI's (the SoundFont's)."""
|
|
341
|
+
return self.bank_msb < BANK_INSTRUMENTS
|
|
342
|
+
|
|
343
|
+
|
|
344
|
+
class Module:
|
|
345
|
+
"""Sixteen parts, mixed, with chorus and reverb on the master.
|
|
346
|
+
|
|
347
|
+
With the default instruments every SoundFont part plays on one shared
|
|
348
|
+
``soundfont.Voices`` of ``voices`` voices. ``instruments`` (True on a
|
|
349
|
+
desktop, False on a board until its cost is measured there) lets banks
|
|
350
|
+
80 and 81 play audioinstruments' instruments: each one's output is a
|
|
351
|
+
voice of ``mixer``, an ``audiomixer.Mixer`` the pool plays into as its
|
|
352
|
+
last voice. Without them the pool is the source, as it always was.
|
|
353
|
+
A ``create(name, rate, channels, bank)`` of your own (stand-ins in a
|
|
354
|
+
test) gives each part its own output instead, summed by ``mixer``, made
|
|
355
|
+
here when None. ``effects`` False leaves the source as the output
|
|
356
|
+
(tests, or a board with no cycles to spare). ``voices`` defaults to
|
|
357
|
+
``VOICES``, or ``BOARD_VOICES`` on a board, and ``voice_count``
|
|
358
|
+
changes it while the module plays; ``chorus`` False (a board's default)
|
|
359
|
+
leaves the chorus out, and CC93 then sets a level nothing hears.
|
|
360
|
+
``send`` is called with the bytes of each reply the module sends (the
|
|
361
|
+
Identity Reply); None drops them.
|
|
362
|
+
"""
|
|
363
|
+
|
|
364
|
+
def __init__(self, rate=48000, channels=2, bank=None, create=None, mixer=None, effects=True, kits=None,
|
|
365
|
+
voices=None, chorus=None, instruments=None, send=None):
|
|
366
|
+
self.rate = rate
|
|
367
|
+
self.channels = channels
|
|
368
|
+
self.bank = bank
|
|
369
|
+
self.send = send
|
|
370
|
+
self._create = create or _default_create
|
|
371
|
+
self.parts = [Part(c) for c in range(16)]
|
|
372
|
+
if voices is None:
|
|
373
|
+
voices = BOARD_VOICES if BOARD else VOICES
|
|
374
|
+
if chorus is None:
|
|
375
|
+
chorus = not BOARD
|
|
376
|
+
if instruments is None:
|
|
377
|
+
instruments = not BOARD
|
|
378
|
+
self.instruments = instruments or create is not None
|
|
379
|
+
self.voice_pool = self.mixer = None
|
|
380
|
+
shared = _shared_voices() if create is None and mixer is None else None
|
|
381
|
+
if shared is not None:
|
|
382
|
+
# Leave room for ``voice_count`` to rise to VOICES later; an
|
|
383
|
+
# audioinstruments without ``max_voice_count`` sizes to the ceiling.
|
|
384
|
+
try:
|
|
385
|
+
self.voice_pool = shared(rate, channels, voice_count=voices, reserve=RESERVE,
|
|
386
|
+
gain=GAIN, max_voice_count=max(voices, VOICES))
|
|
387
|
+
except TypeError:
|
|
388
|
+
self.voice_pool = shared(rate, channels, voice_count=voices, reserve=RESERVE, gain=GAIN)
|
|
389
|
+
source = self.voice_pool.output
|
|
390
|
+
if instruments:
|
|
391
|
+
self.mixer = self._make_mixer(17)
|
|
392
|
+
self.mixer.voice[16].play(source, loop=True)
|
|
393
|
+
source = self.mixer
|
|
394
|
+
else:
|
|
395
|
+
self.mixer = mixer if mixer is not None else self._make_mixer(16)
|
|
396
|
+
source = self.mixer
|
|
397
|
+
self.chorus = self.reverb = None
|
|
398
|
+
self.output = source
|
|
399
|
+
if effects:
|
|
400
|
+
import audioeffects
|
|
401
|
+
if chorus:
|
|
402
|
+
self.chorus = audioeffects.create("Chorus", source, rate, mix=0.0)
|
|
403
|
+
source = self.chorus.output
|
|
404
|
+
self.reverb = audioeffects.create("Reverb", source, rate, character="hall", mix=0.0)
|
|
405
|
+
self.output = self.reverb.output
|
|
406
|
+
self.reverb_level = self.chorus_level = 0
|
|
407
|
+
self.set_reverb(DEFAULT_REVERB)
|
|
408
|
+
self.set_chorus(DEFAULT_CHORUS)
|
|
409
|
+
self.master_level = DEFAULT_MASTER
|
|
410
|
+
self.on_master = None # the output's volume setter (0-100): a UI's MASTER slider
|
|
411
|
+
self._kits = kits # the bank's drum programs, when known; else asked of the bank
|
|
412
|
+
self._status = 0
|
|
413
|
+
self._need = 0
|
|
414
|
+
self._data = []
|
|
415
|
+
self._sysex = None
|
|
416
|
+
self._skip = 0
|
|
417
|
+
self.error = None # why the last instrument couldn't be made (no bank)
|
|
418
|
+
self.loaded = None # the open bank, once prepare() has opened it
|
|
419
|
+
self.cache_bytes = None # the bound prepare() gave its samples; None for soundfont's own
|
|
420
|
+
self._unbounded = None # that bank, when its soundfont keeps every sample
|
|
421
|
+
self._kept = 0 # how many samples it held when last looked at
|
|
422
|
+
self.identities = 0 # Identity Requests answered
|
|
423
|
+
|
|
424
|
+
@property
|
|
425
|
+
def voice_count(self):
|
|
426
|
+
"""How many engine voices the parts share, settable while it plays
|
|
427
|
+
(``soundfont.Voices.voice_count``); None without a shared pool."""
|
|
428
|
+
pool = self.voice_pool
|
|
429
|
+
return None if pool is None else pool.voice_count
|
|
430
|
+
|
|
431
|
+
@voice_count.setter
|
|
432
|
+
def voice_count(self, count):
|
|
433
|
+
if self.voice_pool is None:
|
|
434
|
+
raise ValueError("no shared voices to count")
|
|
435
|
+
self.voice_pool.voice_count = count
|
|
436
|
+
|
|
437
|
+
def _make_mixer(self, count):
|
|
438
|
+
import audiomixer
|
|
439
|
+
size = MIXER_BUFFER if count > 16 else PARTS_MIXER_BUFFER
|
|
440
|
+
return audiomixer.Mixer(voice_count=count, buffer_size=size, channel_count=self.channels,
|
|
441
|
+
bits_per_sample=16, samples_signed=True, sample_rate=self.rate)
|
|
442
|
+
|
|
443
|
+
# -- the bank -------------------------------------------------------------------
|
|
444
|
+
|
|
445
|
+
def prepare(self):
|
|
446
|
+
"""Open the bank now, so no part has to: the open bank, or None
|
|
447
|
+
with the reason in ``error``. Nothing to do with instruments of your
|
|
448
|
+
own (``create=``)."""
|
|
449
|
+
if self._create is not _default_create:
|
|
450
|
+
return None
|
|
451
|
+
if self.loaded is None:
|
|
452
|
+
try:
|
|
453
|
+
import audioinstruments._rack.soundfont # dotted: a lazy package's from-import fails on MicroPython
|
|
454
|
+
|
|
455
|
+
self.loaded = audioinstruments._rack.soundfont.open_bank(self.bank)
|
|
456
|
+
except Exception as e:
|
|
457
|
+
self.error = e
|
|
458
|
+
return None
|
|
459
|
+
self.cache_bytes = board_cache_bytes()
|
|
460
|
+
if not hasattr(self.loaded, "cache_bytes"):
|
|
461
|
+
self._unbounded = self.loaded
|
|
462
|
+
elif self.cache_bytes is not None:
|
|
463
|
+
self.loaded.cache_bytes = self.cache_bytes
|
|
464
|
+
self.kit_programs()
|
|
465
|
+
return self.loaded
|
|
466
|
+
|
|
467
|
+
def _trim(self):
|
|
468
|
+
"""Let every sample go once an unbounded bank holds more than
|
|
469
|
+
``FALLBACK_CACHE_BYTES``; voices playing one keep theirs."""
|
|
470
|
+
samples = self._unbounded.samples
|
|
471
|
+
if len(samples) == self._kept:
|
|
472
|
+
return
|
|
473
|
+
held = 0
|
|
474
|
+
for pcm in samples.values():
|
|
475
|
+
held += len(pcm) * 2
|
|
476
|
+
limit = FALLBACK_CACHE_BYTES
|
|
477
|
+
if self.cache_bytes is not None and self.cache_bytes < limit:
|
|
478
|
+
limit = self.cache_bytes
|
|
479
|
+
if held > limit:
|
|
480
|
+
samples.clear()
|
|
481
|
+
self._kept = len(samples)
|
|
482
|
+
|
|
483
|
+
# -- the parts' instruments -----------------------------------------------------
|
|
484
|
+
|
|
485
|
+
def wanted(self, part):
|
|
486
|
+
"""The instrument the part's bank and program ask for: one of
|
|
487
|
+
``INSTRUMENTS`` or ``DRUM_MACHINES`` on banks 80 and 81 (None for a
|
|
488
|
+
program past the end), else the SoundFont's."""
|
|
489
|
+
if self.instruments and not part.gm:
|
|
490
|
+
if part.bank_msb == BANK_INSTRUMENTS:
|
|
491
|
+
table = INSTRUMENTS
|
|
492
|
+
elif part.bank_msb == BANK_DRUM_MACHINES:
|
|
493
|
+
table = DRUM_MACHINES
|
|
494
|
+
else:
|
|
495
|
+
return None
|
|
496
|
+
return table[part.program] if part.program < len(table) else None
|
|
497
|
+
return DRUMS if part.drums else MELODIC
|
|
498
|
+
|
|
499
|
+
def instrument(self, part):
|
|
500
|
+
"""The part's instrument, made on its first use or when its bank and
|
|
501
|
+
program ask for another; None without one (no SoundFont, or a
|
|
502
|
+
program no instrument has)."""
|
|
503
|
+
name = self.wanted(part)
|
|
504
|
+
inst = part.inst
|
|
505
|
+
if inst is not None and part.inst_name == name:
|
|
506
|
+
return inst
|
|
507
|
+
if inst is not None:
|
|
508
|
+
self._drop(part)
|
|
509
|
+
if name is None:
|
|
510
|
+
return None
|
|
511
|
+
try:
|
|
512
|
+
if self.voice_pool is not None and name in (MELODIC, DRUMS):
|
|
513
|
+
inst = self._create(name, self.rate, self.channels, self.bank, self.voice_pool)
|
|
514
|
+
else:
|
|
515
|
+
inst = self._create(name, self.rate, self.channels, self.bank)
|
|
516
|
+
except Exception as e:
|
|
517
|
+
self.error = e
|
|
518
|
+
return None
|
|
519
|
+
part.inst = inst
|
|
520
|
+
part.inst_name = name
|
|
521
|
+
self._sends()
|
|
522
|
+
if self.mixer is not None and not (self.voice_pool is not None and name in (MELODIC, DRUMS)):
|
|
523
|
+
voice = self.mixer.voice[part.channel]
|
|
524
|
+
voice.play(inst.output, loop=True)
|
|
525
|
+
self._apply(part)
|
|
526
|
+
return inst
|
|
527
|
+
|
|
528
|
+
def _drop(self, part):
|
|
529
|
+
"""Let the part's instrument go: its notes released, its mixer voice
|
|
530
|
+
stopped, its voices given back."""
|
|
531
|
+
inst = part.inst
|
|
532
|
+
part.inst = None
|
|
533
|
+
part.inst_name = None
|
|
534
|
+
self._sends()
|
|
535
|
+
if inst is None:
|
|
536
|
+
return
|
|
537
|
+
try:
|
|
538
|
+
inst.all_notes_off()
|
|
539
|
+
except Exception:
|
|
540
|
+
pass
|
|
541
|
+
if self.mixer is not None and part.channel < 16:
|
|
542
|
+
try:
|
|
543
|
+
self.mixer.voice[part.channel].stop()
|
|
544
|
+
except Exception:
|
|
545
|
+
pass
|
|
546
|
+
inst.deinit()
|
|
547
|
+
|
|
548
|
+
def _own_voice(self, part):
|
|
549
|
+
"""True when the part's instrument plays through its own mixer voice
|
|
550
|
+
rather than the shared pool."""
|
|
551
|
+
return part.inst is not None and self.mixer is not None and not (
|
|
552
|
+
self.voice_pool is not None and part.inst_name in (MELODIC, DRUMS))
|
|
553
|
+
|
|
554
|
+
def _apply(self, part):
|
|
555
|
+
"""Send a part's remembered state to its instrument."""
|
|
556
|
+
inst = part.inst
|
|
557
|
+
if inst is None:
|
|
558
|
+
return
|
|
559
|
+
if part.inst_name == MELODIC:
|
|
560
|
+
inst.control_change(CC_BANK_MSB, part.bank_msb)
|
|
561
|
+
inst.control_change(CC_BANK_LSB, part.bank_lsb)
|
|
562
|
+
self._select(part)
|
|
563
|
+
self._tune(part)
|
|
564
|
+
inst.pitch_bend(self._wheel(part)) # centred too: a reset may not centre it
|
|
565
|
+
|
|
566
|
+
def _tune(self, part):
|
|
567
|
+
"""Give a SoundFont part its bend range and tuning."""
|
|
568
|
+
inst = part.inst
|
|
569
|
+
if inst is not None and part.inst_name in (MELODIC, DRUMS) and hasattr(inst, "bend_range"):
|
|
570
|
+
inst.bend_range = part.bend_range
|
|
571
|
+
inst.tuning = part.tuning
|
|
572
|
+
|
|
573
|
+
def _wheel(self, part):
|
|
574
|
+
"""The bend to send the part's instrument: as it came, unless the
|
|
575
|
+
instrument's range is fixed, when it is scaled into that range."""
|
|
576
|
+
inst = part.inst
|
|
577
|
+
if part.inst_name not in (MELODIC, DRUMS) or hasattr(inst, "bend_range"):
|
|
578
|
+
return part.bend
|
|
579
|
+
v = BEND_CENTER + int((part.bend - BEND_CENTER) * part.bend_range / NATIVE_BEND_RANGE)
|
|
580
|
+
return 0 if v < 0 else 16383 if v > 16383 else v
|
|
581
|
+
|
|
582
|
+
def _select(self, part):
|
|
583
|
+
"""Pick the part's program on its instrument, then its volume and
|
|
584
|
+
pan: a program change resets the patch's macros."""
|
|
585
|
+
inst = part.inst
|
|
586
|
+
if part.inst_name in (MELODIC, DRUMS):
|
|
587
|
+
index = self.kit_index(part.program) if part.inst_name == DRUMS else part.program
|
|
588
|
+
if index in inst.patches:
|
|
589
|
+
inst.program_change(index)
|
|
590
|
+
inst.control_change(CC_VOLUME, part.volume)
|
|
591
|
+
inst.control_change(CC_EXPRESSION, part.expression)
|
|
592
|
+
inst.set_macro(PAN_MACRO, part.pan)
|
|
593
|
+
else:
|
|
594
|
+
if part.bank_lsb in inst.patches:
|
|
595
|
+
inst.program_change(part.bank_lsb)
|
|
596
|
+
self._level(part)
|
|
597
|
+
|
|
598
|
+
def _level(self, part):
|
|
599
|
+
"""An instrument part's volume, expression and pan, on its mixer
|
|
600
|
+
voice: volume and expression multiply, and the product is squared,
|
|
601
|
+
as a SoundFont part plays them."""
|
|
602
|
+
if not self._own_voice(part):
|
|
603
|
+
return
|
|
604
|
+
voice = self.mixer.voice[part.channel]
|
|
605
|
+
v = part.volume / 127.0 * part.expression / 127.0
|
|
606
|
+
voice.level = PART_LEVEL * v * v
|
|
607
|
+
try:
|
|
608
|
+
voice.panning = max(-1.0, min(1.0, (part.pan - 64) / 63.0))
|
|
609
|
+
except AttributeError: # an audiomixer without panning: centred
|
|
610
|
+
pass
|
|
611
|
+
|
|
612
|
+
def kit_index(self, program):
|
|
613
|
+
"""The drum instrument's patch for GM drum program *program*: the
|
|
614
|
+
kit at that number, else the nearest lower one (the standard kit at
|
|
615
|
+
worst), as GS modules fall back."""
|
|
616
|
+
kits = self.kit_programs()
|
|
617
|
+
best = 0
|
|
618
|
+
for i, p in enumerate(kits):
|
|
619
|
+
if p <= program:
|
|
620
|
+
best = i
|
|
621
|
+
return best
|
|
622
|
+
|
|
623
|
+
def kit_programs(self):
|
|
624
|
+
"""The GM drum program numbers the bank has kits for, in order."""
|
|
625
|
+
if self._kits is None:
|
|
626
|
+
try:
|
|
627
|
+
import audioinstruments._rack.soundfont # dotted: a lazy package's from-import fails on MicroPython
|
|
628
|
+
|
|
629
|
+
soundfont = audioinstruments._rack.soundfont
|
|
630
|
+
self._kits = [p for p, _ in soundfont.open_bank(self.bank).programs(PERCUSSION_BANK)]
|
|
631
|
+
except Exception:
|
|
632
|
+
self._kits = [p for p, _ in GM_KITS]
|
|
633
|
+
return self._kits
|
|
634
|
+
|
|
635
|
+
def name(self, channel):
|
|
636
|
+
"""What channel *channel* plays: the GM program's name, the kit's,
|
|
637
|
+
or the instrument's with its patch."""
|
|
638
|
+
part = self.parts[channel]
|
|
639
|
+
name = self.wanted(part)
|
|
640
|
+
if name is None:
|
|
641
|
+
return "(none)"
|
|
642
|
+
if name not in (MELODIC, DRUMS):
|
|
643
|
+
inst = part.inst
|
|
644
|
+
if inst is not None and part.inst_name == name and part.bank_lsb in inst.patches:
|
|
645
|
+
return "%s: %s" % (name, inst.patches[part.bank_lsb][0])
|
|
646
|
+
return name
|
|
647
|
+
if part.drums:
|
|
648
|
+
inst = part.inst
|
|
649
|
+
if inst is not None and part.inst_name == DRUMS:
|
|
650
|
+
return inst.patches[self.kit_index(part.program)][0]
|
|
651
|
+
kits = self.kit_programs()
|
|
652
|
+
best = GM_KITS[0][1]
|
|
653
|
+
for p, n in GM_KITS:
|
|
654
|
+
if p <= part.program and p in kits:
|
|
655
|
+
best = n
|
|
656
|
+
return best + " Kit"
|
|
657
|
+
return GM_NAMES[part.program]
|
|
658
|
+
|
|
659
|
+
# -- MIDI in --------------------------------------------------------------------
|
|
660
|
+
|
|
661
|
+
def feed(self, data, nbytes=None):
|
|
662
|
+
"""Parse raw MIDI 1.0 bytes (the first ``nbytes`` when given):
|
|
663
|
+
running status, realtime bytes inside a message, and SysEx."""
|
|
664
|
+
n = len(data) if nbytes is None else nbytes
|
|
665
|
+
for i in range(n):
|
|
666
|
+
b = data[i]
|
|
667
|
+
if b >= 0xF8: # realtime: clock, start, stop, sensing
|
|
668
|
+
continue
|
|
669
|
+
if self._sysex is not None:
|
|
670
|
+
if b == 0xF7:
|
|
671
|
+
self.sysex(bytes(self._sysex))
|
|
672
|
+
self._sysex = None
|
|
673
|
+
elif b < 0x80:
|
|
674
|
+
self._sysex.append(b)
|
|
675
|
+
else: # a status byte ends an unterminated SysEx
|
|
676
|
+
self._sysex = None
|
|
677
|
+
self._status_byte(b)
|
|
678
|
+
continue
|
|
679
|
+
if b >= 0x80:
|
|
680
|
+
self._status_byte(b)
|
|
681
|
+
elif self._skip:
|
|
682
|
+
self._skip -= 1
|
|
683
|
+
elif self._status:
|
|
684
|
+
self._data.append(b)
|
|
685
|
+
if len(self._data) == self._need:
|
|
686
|
+
d = self._data
|
|
687
|
+
self._data = []
|
|
688
|
+
self.message(self._status, d[0], d[1] if self._need == 2 else 0)
|
|
689
|
+
|
|
690
|
+
def _status_byte(self, b):
|
|
691
|
+
if b == 0xF0:
|
|
692
|
+
self._sysex = bytearray()
|
|
693
|
+
self._status = 0
|
|
694
|
+
elif b >= 0xF0: # system common cancels running status
|
|
695
|
+
self._status = 0
|
|
696
|
+
self._skip = {0xF1: 1, 0xF2: 2, 0xF3: 1}.get(b, 0)
|
|
697
|
+
else:
|
|
698
|
+
self._status = b
|
|
699
|
+
self._data = []
|
|
700
|
+
self._need = 1 if (b & 0xF0) in (0xC0, 0xD0) else 2
|
|
701
|
+
|
|
702
|
+
def message(self, status, d1, d2=0):
|
|
703
|
+
"""One channel message: ``status`` with its channel, and its data."""
|
|
704
|
+
kind = status & 0xF0
|
|
705
|
+
part = self.parts[status & 0x0F]
|
|
706
|
+
if kind == 0x90 and d2:
|
|
707
|
+
self.note_on(part, d1, d2)
|
|
708
|
+
elif kind in (0x80, 0x90):
|
|
709
|
+
self.note_off(part, d1)
|
|
710
|
+
elif kind == 0xB0:
|
|
711
|
+
self.control(part, d1, d2)
|
|
712
|
+
elif kind == 0xC0:
|
|
713
|
+
self.program(part, d1)
|
|
714
|
+
elif kind == 0xD0:
|
|
715
|
+
if part.inst is not None and hasattr(part.inst, "channel_pressure"):
|
|
716
|
+
part.inst.channel_pressure(d1)
|
|
717
|
+
elif kind == 0xE0:
|
|
718
|
+
self.bend(part, d1 | (d2 << 7))
|
|
719
|
+
|
|
720
|
+
def sysex(self, data):
|
|
721
|
+
"""``data`` is the bytes between F0 and F7. GM System On (and GM2
|
|
722
|
+
On, GS Reset and XG System On, which ask the same) reset every part;
|
|
723
|
+
the Identity Request is answered."""
|
|
724
|
+
if len(data) >= 4 and data[0] == 0x7E and data[2] == 0x06 and data[3] == 0x01:
|
|
725
|
+
if data[1] in (0x7F, DEVICE_ID):
|
|
726
|
+
self.identities += 1
|
|
727
|
+
if self.send is not None:
|
|
728
|
+
self.send(identity_reply())
|
|
729
|
+
elif len(data) >= 4 and data[0] == 0x7E and data[2] == 0x09 and data[3] in (0x01, 0x03):
|
|
730
|
+
self.gm_reset()
|
|
731
|
+
elif len(data) >= 9 and data[0] == 0x41 and data[2:4] == b"\x42\x12" and data[4:7] == b"\x40\x00\x7f":
|
|
732
|
+
self.gm_reset()
|
|
733
|
+
elif len(data) >= 7 and data[0] == 0x43 and data[2] == 0x4C and data[3:6] == b"\x00\x00\x7e":
|
|
734
|
+
self.gm_reset()
|
|
735
|
+
|
|
736
|
+
# -- the events ----------------------------------------------------------------
|
|
737
|
+
|
|
738
|
+
def note_on(self, part, note, velocity):
|
|
739
|
+
inst = self.instrument(part)
|
|
740
|
+
if inst is None:
|
|
741
|
+
return
|
|
742
|
+
inst.note_on(note, velocity)
|
|
743
|
+
if self._unbounded is not None:
|
|
744
|
+
self._trim()
|
|
745
|
+
part.held.add(note)
|
|
746
|
+
part.sustained.discard(note)
|
|
747
|
+
part.notes += 1
|
|
748
|
+
part.last = (note, velocity)
|
|
749
|
+
key = (part.bank_msb, part.bank_lsb, part.program)
|
|
750
|
+
tally = part.played.get(key)
|
|
751
|
+
if tally is None:
|
|
752
|
+
part.played[key] = [1, self.name(part.channel)]
|
|
753
|
+
else:
|
|
754
|
+
tally[0] += 1
|
|
755
|
+
|
|
756
|
+
def note_off(self, part, note):
|
|
757
|
+
part.held.discard(note)
|
|
758
|
+
if part.sustain:
|
|
759
|
+
part.sustained.add(note)
|
|
760
|
+
elif part.inst is not None:
|
|
761
|
+
part.inst.note_off(note)
|
|
762
|
+
|
|
763
|
+
def control(self, part, cc, value):
|
|
764
|
+
inst = part.inst
|
|
765
|
+
if cc in (CC_BANK_MSB, CC_BANK_LSB):
|
|
766
|
+
if cc == CC_BANK_MSB:
|
|
767
|
+
part.bank_msb = value
|
|
768
|
+
else:
|
|
769
|
+
part.bank_lsb = value
|
|
770
|
+
# A SoundFont part hears its bank at once, as it always did;
|
|
771
|
+
# a change of instrument waits for the Program Change.
|
|
772
|
+
if inst is not None and part.inst_name == MELODIC and part.gm:
|
|
773
|
+
inst.control_change(cc, value)
|
|
774
|
+
elif cc == CC_VOLUME:
|
|
775
|
+
part.volume = value
|
|
776
|
+
if inst is not None:
|
|
777
|
+
if part.inst_name in (MELODIC, DRUMS):
|
|
778
|
+
inst.control_change(CC_VOLUME, value)
|
|
779
|
+
else:
|
|
780
|
+
self._level(part)
|
|
781
|
+
elif cc == CC_EXPRESSION:
|
|
782
|
+
part.expression = value
|
|
783
|
+
if inst is not None:
|
|
784
|
+
if part.inst_name in (MELODIC, DRUMS):
|
|
785
|
+
inst.control_change(CC_EXPRESSION, value)
|
|
786
|
+
else:
|
|
787
|
+
self._level(part)
|
|
788
|
+
elif cc == CC_PAN:
|
|
789
|
+
part.pan = value
|
|
790
|
+
if inst is not None:
|
|
791
|
+
if part.inst_name in (MELODIC, DRUMS):
|
|
792
|
+
inst.set_macro(PAN_MACRO, value)
|
|
793
|
+
else:
|
|
794
|
+
self._level(part)
|
|
795
|
+
elif cc == CC_SUSTAIN:
|
|
796
|
+
on = value >= 64
|
|
797
|
+
part.sustain = on
|
|
798
|
+
if not on:
|
|
799
|
+
self._release_sustained(part)
|
|
800
|
+
elif cc == CC_REVERB:
|
|
801
|
+
part.reverb = value
|
|
802
|
+
self._sends()
|
|
803
|
+
elif cc == CC_CHORUS:
|
|
804
|
+
part.chorus = value
|
|
805
|
+
self._sends()
|
|
806
|
+
elif cc in (CC_RPN_MSB, CC_RPN_LSB):
|
|
807
|
+
msb, lsb = part.rpn if part.rpn is not None else RPN_NULL
|
|
808
|
+
part.rpn = (value, lsb) if cc == CC_RPN_MSB else (msb, value)
|
|
809
|
+
elif cc in (CC_NRPN_MSB, CC_NRPN_LSB):
|
|
810
|
+
part.rpn = None # no NRPNs here: Data Entry goes nowhere
|
|
811
|
+
elif cc in (CC_DATA_MSB, CC_DATA_LSB, CC_DATA_INC, CC_DATA_DEC):
|
|
812
|
+
self.data_entry(part, cc, value)
|
|
813
|
+
elif cc == CC_ALL_SOUND_OFF:
|
|
814
|
+
self.all_sound_off(part)
|
|
815
|
+
elif cc == CC_RESET_CONTROLLERS:
|
|
816
|
+
self.reset_controllers(part)
|
|
817
|
+
elif cc == CC_ALL_NOTES_OFF:
|
|
818
|
+
self.all_notes_off(part)
|
|
819
|
+
elif MACRO_CC <= cc < MACRO_CC + MACRO_CCS:
|
|
820
|
+
if inst is not None and cc - MACRO_CC < len(inst.macro_labels):
|
|
821
|
+
inst.set_macro(cc - MACRO_CC, value)
|
|
822
|
+
elif inst is not None:
|
|
823
|
+
inst.control_change(cc, value)
|
|
824
|
+
|
|
825
|
+
def program(self, part, number):
|
|
826
|
+
part.program = number
|
|
827
|
+
if part.inst is None or part.inst_name != self.wanted(part):
|
|
828
|
+
self.instrument(part) # made now, with the program applied
|
|
829
|
+
else:
|
|
830
|
+
self._select(part)
|
|
831
|
+
|
|
832
|
+
def bend(self, part, value):
|
|
833
|
+
part.bend = value
|
|
834
|
+
if part.inst is not None:
|
|
835
|
+
part.inst.pitch_bend(self._wheel(part))
|
|
836
|
+
|
|
837
|
+
def data_entry(self, part, cc, value):
|
|
838
|
+
"""Data Entry (CC6, CC38), Increment (CC96) or Decrement (CC97) for
|
|
839
|
+
the part's selected RPN: an MSB alone zeroes the LSB."""
|
|
840
|
+
rpn = part.rpn
|
|
841
|
+
if rpn not in part.params:
|
|
842
|
+
return
|
|
843
|
+
v = part.params[rpn]
|
|
844
|
+
if cc == CC_DATA_MSB:
|
|
845
|
+
v = value << 7
|
|
846
|
+
elif cc == CC_DATA_LSB:
|
|
847
|
+
v = (v & 0x3F80) | value
|
|
848
|
+
else:
|
|
849
|
+
step = 1 if rpn == RPN_FINE_TUNING else 128
|
|
850
|
+
v += step if cc == CC_DATA_INC else -step
|
|
851
|
+
v = 0 if v < 0 else 16383 if v > 16383 else v
|
|
852
|
+
if rpn == RPN_BEND_RANGE and v > MAX_BEND_RANGE << 7:
|
|
853
|
+
v = MAX_BEND_RANGE << 7
|
|
854
|
+
part.params[rpn] = v
|
|
855
|
+
if part.inst is not None:
|
|
856
|
+
self._tune(part)
|
|
857
|
+
if rpn == RPN_BEND_RANGE and part.bend != BEND_CENTER:
|
|
858
|
+
part.inst.pitch_bend(self._wheel(part))
|
|
859
|
+
|
|
860
|
+
def _release_sustained(self, part):
|
|
861
|
+
notes = part.sustained
|
|
862
|
+
part.sustained = set()
|
|
863
|
+
if part.inst is not None:
|
|
864
|
+
for n in notes:
|
|
865
|
+
part.inst.note_off(n)
|
|
866
|
+
|
|
867
|
+
def all_notes_off(self, part):
|
|
868
|
+
"""CC123: release every note the part holds."""
|
|
869
|
+
part.held = set()
|
|
870
|
+
part.sustained = set()
|
|
871
|
+
if part.inst is not None:
|
|
872
|
+
part.inst.all_notes_off()
|
|
873
|
+
|
|
874
|
+
def all_sound_off(self, part):
|
|
875
|
+
"""CC120: stop the part's sound at once, keeping its program."""
|
|
876
|
+
part.held = set()
|
|
877
|
+
part.sustained = set()
|
|
878
|
+
inst = part.inst
|
|
879
|
+
if inst is not None:
|
|
880
|
+
inst.reset() # stops every voice, and forgets the program and bank
|
|
881
|
+
self._apply(part)
|
|
882
|
+
|
|
883
|
+
def reset_controllers(self, part):
|
|
884
|
+
"""CC121, as RP-015 has it: pedal up, bend centred, Expression 127,
|
|
885
|
+
the mod wheel and channel pressure 0, and the RPN null; program,
|
|
886
|
+
volume, pan and the RPN values stay."""
|
|
887
|
+
part.sustain = False
|
|
888
|
+
self._release_sustained(part)
|
|
889
|
+
part.bend = BEND_CENTER
|
|
890
|
+
part.rpn = RPN_NULL
|
|
891
|
+
part.expression = DEFAULT_EXPRESSION
|
|
892
|
+
inst = part.inst
|
|
893
|
+
if inst is not None:
|
|
894
|
+
inst.pitch_bend(BEND_CENTER)
|
|
895
|
+
if part.inst_name not in (MELODIC, DRUMS):
|
|
896
|
+
self._level(part)
|
|
897
|
+
else:
|
|
898
|
+
inst.control_change(CC_EXPRESSION, DEFAULT_EXPRESSION)
|
|
899
|
+
inst.control_change(CC_MOD_WHEEL, 0)
|
|
900
|
+
if hasattr(inst, "channel_pressure"):
|
|
901
|
+
inst.channel_pressure(0)
|
|
902
|
+
|
|
903
|
+
def panic(self):
|
|
904
|
+
"""Every part silent now; programs and levels stay."""
|
|
905
|
+
for part in self.parts:
|
|
906
|
+
self.all_sound_off(part)
|
|
907
|
+
self.reset_controllers(part)
|
|
908
|
+
|
|
909
|
+
def gm_reset(self):
|
|
910
|
+
"""GM System On: every part back to bank 0, program 0, volume 100,
|
|
911
|
+
pan centre, pedal up, bend centred, two semitones of bend and no
|
|
912
|
+
tuning; reverb 40 and chorus 0. A part
|
|
913
|
+
playing one of audioinstruments' instruments lets it go, and plays
|
|
914
|
+
the SoundFont again from its next note."""
|
|
915
|
+
for part in self.parts:
|
|
916
|
+
part.reset()
|
|
917
|
+
if part.inst is not None:
|
|
918
|
+
if part.inst_name != self.wanted(part):
|
|
919
|
+
self._drop(part)
|
|
920
|
+
else:
|
|
921
|
+
part.inst.reset()
|
|
922
|
+
self._apply(part)
|
|
923
|
+
self._sends()
|
|
924
|
+
|
|
925
|
+
# -- a UI's channel sliders -------------------------------------------------------
|
|
926
|
+
|
|
927
|
+
def set_volume(self, part, value):
|
|
928
|
+
"""A UI's volume slider for one channel: the path CC7 takes, so
|
|
929
|
+
``part.volume`` is what the UI shows."""
|
|
930
|
+
self.control(part, CC_VOLUME, value)
|
|
931
|
+
|
|
932
|
+
def set_pan(self, part, value):
|
|
933
|
+
"""A UI's pan slider for one channel: the path CC10 takes."""
|
|
934
|
+
self.control(part, CC_PAN, value)
|
|
935
|
+
|
|
936
|
+
# -- the master -----------------------------------------------------------------
|
|
937
|
+
|
|
938
|
+
def set_master(self, level):
|
|
939
|
+
"""The MASTER level, 0-100: the output's volume, not any part's.
|
|
940
|
+
``on_master`` (the AudioOut's ``set_volume``) gets it, so a part's
|
|
941
|
+
CC7 and the master never fight over one control."""
|
|
942
|
+
level = max(0, min(100, int(level)))
|
|
943
|
+
self.master_level = level
|
|
944
|
+
if self.on_master is not None:
|
|
945
|
+
self.on_master(level)
|
|
946
|
+
|
|
947
|
+
def _sends(self):
|
|
948
|
+
"""The master reverb and chorus: the mean of the parts' sends,
|
|
949
|
+
over the parts in use, or every part before any is."""
|
|
950
|
+
parts = [p for p in self.parts if p.inst is not None] or self.parts
|
|
951
|
+
self.set_reverb((sum(p.reverb for p in parts) + len(parts) // 2) // len(parts))
|
|
952
|
+
self.set_chorus((sum(p.chorus for p in parts) + len(parts) // 2) // len(parts))
|
|
953
|
+
|
|
954
|
+
def set_reverb(self, level):
|
|
955
|
+
self.reverb_level = level
|
|
956
|
+
_set_mix(self.reverb, level)
|
|
957
|
+
|
|
958
|
+
def set_chorus(self, level):
|
|
959
|
+
self.chorus_level = level
|
|
960
|
+
_set_mix(self.chorus, level)
|
|
961
|
+
|
|
962
|
+
def fill(self):
|
|
963
|
+
"""Let the parts' instruments move their notes and knobs on: call it
|
|
964
|
+
before each pull of ``output``, or every 10 to 20 ms where something
|
|
965
|
+
else pulls it. An audioinstruments instrument on MicroPython takes
|
|
966
|
+
notes and knobs into the sound in ``fill()``; on CPython, and for a
|
|
967
|
+
SoundFont part, it does nothing."""
|
|
968
|
+
for part in self.parts:
|
|
969
|
+
inst = part.inst
|
|
970
|
+
if inst is not None and part.inst_name not in (MELODIC, DRUMS):
|
|
971
|
+
fill = getattr(inst, "fill", None)
|
|
972
|
+
if fill is not None:
|
|
973
|
+
fill()
|
|
974
|
+
|
|
975
|
+
def close(self):
|
|
976
|
+
for part in self.parts:
|
|
977
|
+
self._drop(part)
|
|
978
|
+
if self.mixer is not None and self.voice_pool is not None and self.instruments:
|
|
979
|
+
try:
|
|
980
|
+
self.mixer.voice[16].stop()
|
|
981
|
+
except Exception:
|
|
982
|
+
pass
|
|
983
|
+
for fx in (self.reverb, self.chorus):
|
|
984
|
+
if fx is not None:
|
|
985
|
+
fx.deinit()
|
|
986
|
+
self.reverb = self.chorus = None
|
|
987
|
+
if self.voice_pool is not None:
|
|
988
|
+
self.voice_pool.deinit()
|
|
989
|
+
self.voice_pool = None
|
|
990
|
+
|
|
991
|
+
|
|
992
|
+
def _mix_index(fx):
|
|
993
|
+
for i, label in enumerate(fx.MACRO_LABELS):
|
|
994
|
+
if label.lower().startswith("mix"):
|
|
995
|
+
return i
|
|
996
|
+
raise ValueError("%s has no Mix macro" % fx.NAME)
|
|
997
|
+
|
|
998
|
+
|
|
999
|
+
def _set_mix(fx, level):
|
|
1000
|
+
if fx is not None:
|
|
1001
|
+
fx.set_macro(_mix_index(fx), level)
|