omnipython-audiodsp 0.6.16.dev6__cp313-cp313-win_amd64.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 (79) hide show
  1. _audiodsp.cp313-win_amd64.pyd +0 -0
  2. audiobiquad-stubs/__init__.pyi +235 -0
  3. audiobiquad.py +319 -0
  4. audioconvolve-stubs/__init__.pyi +179 -0
  5. audioconvolve.py +295 -0
  6. audiocore-stubs/__init__.pyi +146 -0
  7. audiocore.py +275 -0
  8. audiodelays-stubs/__init__.pyi +549 -0
  9. audiodelays.py +620 -0
  10. audiodsp_util/__init__.py +84 -0
  11. audiodsp_util/__init__.pyi +22 -0
  12. audiodsp_util/py.typed +0 -0
  13. audiodynamics-stubs/__init__.pyi +281 -0
  14. audiodynamics.py +283 -0
  15. audioecho-stubs/__init__.pyi +186 -0
  16. audioecho.py +286 -0
  17. audiofeatures-stubs/__init__.pyi +141 -0
  18. audiofeatures.py +111 -0
  19. audiofilters-stubs/__init__.pyi +251 -0
  20. audiofilters.py +477 -0
  21. audiofreeverb-stubs/__init__.pyi +103 -0
  22. audiofreeverb.py +143 -0
  23. audiograph-stubs/__init__.pyi +723 -0
  24. audiograph.py +229 -0
  25. audioladder-stubs/__init__.pyi +185 -0
  26. audioladder.py +259 -0
  27. audiomath-stubs/__init__.pyi +229 -0
  28. audiomath.py +314 -0
  29. audiometer-stubs/__init__.pyi +114 -0
  30. audiometer.py +156 -0
  31. audiomixer-stubs/__init__.pyi +154 -0
  32. audiomixer.py +417 -0
  33. audiomodal-stubs/__init__.pyi +136 -0
  34. audiomodal.py +214 -0
  35. audiomp3-stubs/__init__.pyi +75 -0
  36. audiomp3.py +119 -0
  37. audiooscillator-stubs/__init__.pyi +154 -0
  38. audiooscillator.py +205 -0
  39. audiopump-stubs/__init__.pyi +633 -0
  40. audiopump.py +1325 -0
  41. audiorender/README.md +121 -0
  42. audiorender/__init__.py +66 -0
  43. audiorender/__init__.pyi +40 -0
  44. audiorender/events.py +106 -0
  45. audiorender/events.pyi +76 -0
  46. audiorender/metrics.py +99 -0
  47. audiorender/metrics.pyi +26 -0
  48. audiorender/py.typed +0 -0
  49. audiorender/render.py +167 -0
  50. audiorender/render.pyi +84 -0
  51. audiorender/tempo.py +85 -0
  52. audiorender/tempo.pyi +34 -0
  53. audiorender/voices.py +108 -0
  54. audiorender/voices.pyi +54 -0
  55. audiorender/wav.py +23 -0
  56. audiorender/wav.pyi +13 -0
  57. audioroute-stubs/__init__.pyi +203 -0
  58. audioroute.py +420 -0
  59. audiosampler-stubs/__init__.pyi +277 -0
  60. audiosampler.py +316 -0
  61. audioshaper-stubs/__init__.pyi +229 -0
  62. audioshaper.py +426 -0
  63. audiospeed-stubs/__init__.pyi +92 -0
  64. audiospeed.py +105 -0
  65. audioverb-stubs/__init__.pyi +194 -0
  66. audioverb.py +244 -0
  67. audiovorbis-stubs/__init__.pyi +81 -0
  68. audiovorbis.py +141 -0
  69. omnipython_audiodsp-0.6.16.dev6.dist-info/METADATA +74 -0
  70. omnipython_audiodsp-0.6.16.dev6.dist-info/RECORD +79 -0
  71. omnipython_audiodsp-0.6.16.dev6.dist-info/WHEEL +5 -0
  72. omnipython_audiodsp-0.6.16.dev6.dist-info/licenses/LICENSE +21 -0
  73. omnipython_audiodsp-0.6.16.dev6.dist-info/licenses/NOTICE +33 -0
  74. omnipython_audiodsp-0.6.16.dev6.dist-info/licenses/third_party/kissfft/COPYING +11 -0
  75. omnipython_audiodsp-0.6.16.dev6.dist-info/licenses/third_party/microfrontend/LICENSE +202 -0
  76. omnipython_audiodsp-0.6.16.dev6.dist-info/licenses/third_party/tremor/COPYING +28 -0
  77. omnipython_audiodsp-0.6.16.dev6.dist-info/top_level.txt +28 -0
  78. synthio-stubs/__init__.pyi +597 -0
  79. synthio.py +842 -0
Binary file
@@ -0,0 +1,235 @@
1
+ # SPDX-FileCopyrightText: 2026 Brad Barnett
2
+ #
3
+ # SPDX-License-Identifier: MIT
4
+ """Biquad and all-pass filters whose tails reach exact zero.
5
+
6
+ `Biquad` is one filter section in any of seven modes, and `AllPass` is a
7
+ cascade of first-order all-pass stages, which is what a phaser is made of.
8
+ Both keep their state in floating point, so a ringing tail decays to exactly
9
+ zero instead of sitting at one least-significant bit. `Q` is bounded to
10
+ 0.05 to 60 and `feedback` to plus or minus 0.99: a pole on the unit circle is a
11
+ filter that never stops ringing, which is the one thing this module exists to
12
+ avoid.
13
+
14
+ This module is named apart from CircuitPython's `audiofilters` on purpose. It
15
+ is not an extension of `audiofilters.Filter` or `audiofilters.Phaser` but a
16
+ second set of kernels, so the ported modules stay the same as the ones on a
17
+ stock board. For a task-shaped introduction, see "Filter a sound".
18
+
19
+ Both nodes hand out 256 frames at a time (or fewer, with ``buffer_size``), sit
20
+ in an audio chain like any other effect, add no latency, and never report
21
+ themselves finished: a chain with nothing left to read gets silence.
22
+
23
+ A level moved on a playing node walks to its new value in a straight line
24
+ over 20 ms instead of stepping, so turning ``mix`` in one call does not click.
25
+ A node that has not played yet, or has just been reset, takes its levels at
26
+ once.
27
+ """
28
+
29
+ from types import TracebackType
30
+ from typing import Protocol, Self, Union
31
+
32
+ __version__: str
33
+ """The audiodsp release this module came from."""
34
+ __revision__: str
35
+ """Which source this build was made from, from `git describe`: a release tag,
36
+ or the nearest one and the commit. "unknown" when the build had no git."""
37
+
38
+ LOW_PASS: int
39
+ """A `Biquad` mode: pass what is below ``frequency``. This is 0."""
40
+ HIGH_PASS: int
41
+ """A `Biquad` mode: pass what is above ``frequency``. This is 1."""
42
+ BAND_PASS: int
43
+ """A `Biquad` mode: pass a band around ``frequency``. This is 2."""
44
+ NOTCH: int
45
+ """A `Biquad` mode: remove a band around ``frequency``. This is 3."""
46
+ PEAKING_EQ: int
47
+ """A `Biquad` mode: boost or cut a band around ``frequency`` by ``gain_db``.
48
+ This is 4."""
49
+ LOW_SHELF: int
50
+ """A `Biquad` mode: boost or cut everything below ``frequency`` by
51
+ ``gain_db``. This is 5."""
52
+ HIGH_SHELF: int
53
+ """A `Biquad` mode: boost or cut everything above ``frequency`` by
54
+ ``gain_db``. This is 6."""
55
+ MODES: tuple[int, int, int, int, int, int, int]
56
+ """The seven mode numbers, `LOW_PASS` to `HIGH_SHELF`, in order. They are
57
+ numbered exactly as ``synthio.FilterMode`` numbers its modes, so either can be
58
+ handed to either."""
59
+ FRAMES: int
60
+ """Frames in one block when ``buffer_size`` is not given: 256."""
61
+ MAX_STAGES: int
62
+ """The most stages an `AllPass` can have: 16."""
63
+
64
+ # A number, or a synthio.LFO or synthio.Math read once a block.
65
+ _BlockInput = Union[float, object, None]
66
+
67
+ class _AudioSample(Protocol):
68
+ @property
69
+ def sample_rate(self) -> int: ...
70
+ @property
71
+ def bits_per_sample(self) -> int: ...
72
+ @property
73
+ def channel_count(self) -> int: ...
74
+
75
+ class Biquad:
76
+ """One filter section.
77
+
78
+ Every argument is a keyword. ``frequency``, ``Q``, ``gain_db`` and ``mix``
79
+ are block inputs: a number, or a ``synthio.LFO`` or ``synthio.Math`` that
80
+ is read once a block, so an LFO on ``frequency`` arrives about a block
81
+ late.
82
+
83
+ When ``frequency``, ``Q``, ``gain_db`` or ``mode`` changes while the
84
+ filter plays, it glides to the new filter over about 20 ms instead of
85
+ jumping, so a preset change that drops the corner from 20 kHz to 20 Hz does
86
+ not thump. A filter that has not played yet takes its new settings at once.
87
+
88
+ Args:
89
+ mode: One of `LOW_PASS`, `HIGH_PASS`, `BAND_PASS`, `NOTCH`,
90
+ `PEAKING_EQ`, `LOW_SHELF` and `HIGH_SHELF`. The default is
91
+ `LOW_PASS`.
92
+ frequency: The corner or centre in Hz. The default is 1000.
93
+ Q: The resonance, bounded to 0.05 to 60. The default, None, is 0.7071
94
+ (Butterworth), and reads back as that number.
95
+ gain_db: The boost or cut in dB, for `PEAKING_EQ`, `LOW_SHELF` and
96
+ `HIGH_SHELF`. The default is 0.
97
+ mix: A crossfade: 0 is a wire, 1 is the filtered signal alone. The
98
+ default is 1.
99
+ sample_rate: Frames per second. The default is 48000.
100
+ channel_count: 1 or 2. The default is 2.
101
+ buffer_size: Bytes in each block, a whole number of frames from one to
102
+ 256. The default, None, is 256 frames.
103
+
104
+ Raises:
105
+ ValueError: ``mode`` is not one of the seven, ``channel_count`` is not
106
+ 1 or 2, ``sample_rate`` is below 1, or ``buffer_size`` is not whole
107
+ frames from one to 256.
108
+ """
109
+
110
+ def __init__(
111
+ self,
112
+ *,
113
+ mode: int = 0,
114
+ frequency: _BlockInput = 1000,
115
+ Q: _BlockInput = None,
116
+ gain_db: _BlockInput = 0,
117
+ mix: _BlockInput = 1,
118
+ sample_rate: int = 48000,
119
+ channel_count: int = 2,
120
+ buffer_size: int | None = None,
121
+ ) -> None: ...
122
+ mode: int
123
+ """The filter's mode. Setting it raises `ValueError` for a number that is
124
+ not one of the seven."""
125
+ frequency: _BlockInput
126
+ """The corner or centre in Hz."""
127
+ Q: _BlockInput
128
+ """The resonance, bounded to 0.05 to 60."""
129
+ gain_db: _BlockInput
130
+ """The boost or cut in dB."""
131
+ mix: _BlockInput
132
+ """The crossfade from the dry signal (0) to the filtered one (1)."""
133
+ sample_rate: int
134
+ """Frames per second. Writable, to 1 or more."""
135
+ @property
136
+ def bits_per_sample(self) -> int:
137
+ """Size of one sample in bits: always 16."""
138
+ @property
139
+ def channel_count(self) -> int:
140
+ """Number of channels: 1 or 2."""
141
+ @property
142
+ def playing(self) -> bool:
143
+ """Whether a source is set with `play`."""
144
+ @property
145
+ def coefficients(self) -> tuple[float, float, float, float, float]:
146
+ """The filter's ``(b0, b1, b2, a1, a2)``, normalised, at the settings
147
+ in force. Reading it does not advance an LFO on a block input."""
148
+ def play(self, source: _AudioSample, /) -> None:
149
+ """Set the stream to filter, replacing the last one."""
150
+ def stop(self) -> None:
151
+ """Forget the source. The node then hands out silence."""
152
+ def clear(self) -> None:
153
+ """Empty the filter's memory, which stops a ringing tail."""
154
+ def deinit(self) -> None:
155
+ """Release the node. Later calls other than `deinit` raise
156
+ `ValueError`."""
157
+ def __enter__(self) -> Self: ...
158
+ def __exit__(self, exc_type: type[BaseException] | None, exc: BaseException | None, tb: TracebackType | None, /) -> None: ...
159
+
160
+ class AllPass:
161
+ """A cascade of first-order all-pass stages.
162
+
163
+ ``frequency`` really is the frequency at which one stage's phase passes
164
+ minus 90 degrees, so nothing has to be pre-warped. With ``mix`` at 0.5 the
165
+ cascade sums with the dry signal into the notches a script phaser makes,
166
+ and that is where they are deepest.
167
+
168
+ Every argument is a keyword. ``frequency``, ``feedback`` and ``mix`` are
169
+ block inputs, as for `Biquad`. ``stages`` sizes the node's state, so it can
170
+ only be given here.
171
+
172
+ Args:
173
+ stages: How many stages, 1 to `MAX_STAGES`. The default is 4.
174
+ frequency: The frequency in Hz at which one stage reaches minus 90
175
+ degrees. The default is 1000.
176
+ feedback: Output fed back to the input, bounded to plus or minus 0.99.
177
+ The default is 0.
178
+ mix: A crossfade: 0 is a wire, 1 is the all-pass cascade alone. The
179
+ default is 1.
180
+ sample_rate: Frames per second. The default is 48000.
181
+ channel_count: 1 or 2. The default is 2.
182
+ buffer_size: Bytes in each block, as for `Biquad`.
183
+
184
+ Raises:
185
+ ValueError: ``stages`` is out of range, ``channel_count`` is not 1 or
186
+ 2, ``sample_rate`` is below 1, or ``buffer_size`` is not whole
187
+ frames from one to 256.
188
+ """
189
+
190
+ def __init__(
191
+ self,
192
+ *,
193
+ stages: int = 4,
194
+ frequency: _BlockInput = 1000,
195
+ feedback: _BlockInput = 0,
196
+ mix: _BlockInput = 1,
197
+ sample_rate: int = 48000,
198
+ channel_count: int = 2,
199
+ buffer_size: int | None = None,
200
+ ) -> None: ...
201
+ frequency: _BlockInput
202
+ """The frequency in Hz at which one stage's phase passes minus 90 degrees."""
203
+ feedback: _BlockInput
204
+ """Output fed back to the input, bounded to plus or minus 0.99."""
205
+ mix: _BlockInput
206
+ """The crossfade from the dry signal (0) to the cascade (1)."""
207
+ sample_rate: int
208
+ """Frames per second. Writable, to 1 or more."""
209
+ @property
210
+ def bits_per_sample(self) -> int:
211
+ """Size of one sample in bits: always 16."""
212
+ @property
213
+ def channel_count(self) -> int:
214
+ """Number of channels: 1 or 2."""
215
+ @property
216
+ def playing(self) -> bool:
217
+ """Whether a source is set with `play`."""
218
+ @property
219
+ def stages(self) -> int:
220
+ """How many stages the cascade has."""
221
+ @property
222
+ def coefficient(self) -> float:
223
+ """The coefficient of each stage at the settings in force. Reading it
224
+ does not advance an LFO on a block input."""
225
+ def play(self, source: _AudioSample, /) -> None:
226
+ """Set the stream to filter, replacing the last one."""
227
+ def stop(self) -> None:
228
+ """Forget the source. The node then hands out silence."""
229
+ def clear(self) -> None:
230
+ """Empty the filter's memory, which stops a ringing tail."""
231
+ def deinit(self) -> None:
232
+ """Release the node. Later calls other than `deinit` raise
233
+ `ValueError`."""
234
+ def __enter__(self) -> Self: ...
235
+ def __exit__(self, exc_type: type[BaseException] | None, exc: BaseException | None, tb: TracebackType | None, /) -> None: ...
audiobiquad.py ADDED
@@ -0,0 +1,319 @@
1
+ # SPDX-FileCopyrightText: 2026 Brad Barnett
2
+ #
3
+ # SPDX-License-Identifier: MIT
4
+
5
+ """A biquad and a first-order all-pass cascade that reach exact zero.
6
+
7
+ Not a CircuitPython module, and not from micropython-vst3's engine either --
8
+ audiodsp adds it. `audiofilters.Filter` (over `synthio.Biquad`) and
9
+ `audiofilters.Phaser` already do these two jobs, and both are ported
10
+ CircuitPython whose arithmetic is integer:
11
+
12
+ - `synthio.Biquad`'s recursion keeps its output memory in Q12 sample units
13
+ and rounds to nearest, with no dither and no leak, so it can land on a
14
+ state that reproduces itself. Fed silence after a burst, a `LowPass` at
15
+ 100 Hz holds +1 LSB, a `LowPass` at 40 Hz q=8 holds -4 LSB and the
16
+ `Phaser` defaults hold -4 LSB -- measured to 3000 blocks, 32 seconds of
17
+ audio, and they never decay (audiodsp#23).
18
+ - `audiofilters.Phaser` has the same defect in its own kernel: its all-pass
19
+ memory is `int16_t` in plain sample units, so it settles on a non-zero
20
+ word and holds it. It also clamps `feedback` to 0.1..0.9, so the
21
+ feedback-free topology every script phaser uses is not reachable and the
22
+ notches stop about 17 dB short of a null.
23
+
24
+ Neither is modified -- audiodsp's rule for ported code is extend, never
25
+ modify, because an argument on this port's copy of a CircuitPython module
26
+ would not exist on a stock board. This module is the extension:
27
+
28
+ eq = audiobiquad.Biquad(mode=audiobiquad.PEAKING_EQ, frequency=3200,
29
+ Q=1.2, gain_db=-4.0, sample_rate=48000)
30
+ eq.play(source)
31
+
32
+ phase = audiobiquad.AllPass(stages=4, frequency=sweep, feedback=0.0,
33
+ mix=0.5, sample_rate=48000)
34
+ phase.play(eq)
35
+
36
+ `buffer_size` is the size in bytes of each block handed out, as on the
37
+ CircuitPython effects: a host running 64-frame blocks passes
38
+ `buffer_size=64 * 2 * channel_count` and the filter keeps pace with it, with
39
+ no latency and coefficients that can move every 64 frames. It defaults to 256
40
+ frames, which is also the most it can be. The filter is per sample, so the
41
+ audio is the same at any size.
42
+
43
+ `frequency`, `Q`, `gain_db`, `feedback` and `mix` all accept a synthio
44
+ `BlockInput` -- an `LFO`, a `Math`, or a plain number -- read once per
45
+ processed chunk, exactly as `audiofilters.Filter` reads its own.
46
+
47
+ A `Biquad` whose `frequency`, `Q`, `gain_db` or `mode` changes while it is
48
+ playing walks to the new filter over about 20 ms rather than stepping, so a
49
+ jump in one call does not ring its old memory through the new section at
50
+ full scale (audiocomponents#162). A `mix` move on either node walks to
51
+ the new blend in a straight line over 20 ms.
52
+
53
+ Two things this does that the ported nodes cannot:
54
+
55
+ - **Silence in, silence out.** State is `float`, and any state word below
56
+ 1e-20 is written as exact zero, so a tail decays to zero and stays there.
57
+ - **`feedback=0.0` is zero**, and negative feedback is allowed. At zero an
58
+ `AllPass` cascade at `mix=0.5` is the script phaser's equal sum, and its
59
+ notches are true nulls.
60
+
61
+ And one convenience: `AllPass`'s `frequency` really is the frequency at
62
+ which one stage's phase passes -90 degrees, because the coefficient is the
63
+ bilinear `(tan(pi f / fs) - 1) / (tan(pi f / fs) + 1)`.
64
+ `audiofilters.Phaser` uses the small-angle form of that, which lands the
65
+ break high enough to matter, so its class has to pre-warp in Python.
66
+ """
67
+
68
+ from audiocore import (
69
+ GET_BUFFER_ERROR, GET_BUFFER_MORE_DATA, _AudioSample, get_buffer,
70
+ )
71
+ import _audiodsp
72
+
73
+
74
+ #: The audiodsp this was built from, the same pair the native builds put
75
+ #: on this module (src/cp_compat/audiodsp_build.h). audiodsp#55.
76
+ __version__ = _audiodsp.__version__
77
+ __revision__ = _audiodsp.__revision__
78
+
79
+ FRAMES = _audiodsp.FILTER_F32_FRAMES
80
+ MAX_STAGES = _audiodsp.FILTER_F32_MAX_STAGES
81
+
82
+ #: Filter shapes, numbered exactly as `synthio.FilterMode` numbers them, so
83
+ #: `mode=synthio.FilterMode.NOTCH.value` and `mode=audiobiquad.NOTCH` are the
84
+ #: same request.
85
+ LOW_PASS = 0
86
+ HIGH_PASS = 1
87
+ BAND_PASS = 2
88
+ NOTCH = 3
89
+ PEAKING_EQ = 4
90
+ LOW_SHELF = 5
91
+ HIGH_SHELF = 6
92
+
93
+ MODES = (LOW_PASS, HIGH_PASS, BAND_PASS, NOTCH, PEAKING_EQ, LOW_SHELF,
94
+ HIGH_SHELF)
95
+
96
+ #: Option name -> the native configure() slot, in the order
97
+ #: shared/audiodsp_filter_f32.h declares them, which is the order the
98
+ #: MicroPython bindings list them in too.
99
+ _BIQUAD_OPTIONS = ("mode", "frequency", "Q", "gain_db", "mix")
100
+ _ALLPASS_OPTIONS = ("frequency", "feedback", "mix")
101
+
102
+
103
+ def _value(value):
104
+ """Resolve a synthio BlockInput, or pass a plain number through.
105
+
106
+ The same helper audiofilters.py uses, and for the same reason: the
107
+ MicroPython bindings call synthio_block_slot_get_limited() at exactly
108
+ this point in the loop, so both targets read a block once per chunk and
109
+ neither evaluates one inside the sample loop.
110
+ """
111
+ evaluator = getattr(value, "_evaluate", None)
112
+ if evaluator is not None:
113
+ from synthio import _CLOCK
114
+ return float(evaluator(_CLOCK.tick))
115
+ return float(value.value if hasattr(value, "value") else value)
116
+
117
+
118
+ def _frames_for(buffer_size, channel_count):
119
+ """Frames per block for a `buffer_size` in bytes, as the CircuitPython
120
+ effects take it; None is the default block."""
121
+ if buffer_size is None:
122
+ return FRAMES
123
+ width = 2 * channel_count
124
+ buffer_size = int(buffer_size)
125
+ if buffer_size < width or buffer_size % width or \
126
+ buffer_size > FRAMES * width:
127
+ raise ValueError(
128
+ "buffer_size must be whole frames, from one to %d" % FRAMES)
129
+ return buffer_size // width
130
+
131
+
132
+ class _Node(_AudioSample):
133
+ """The pull loop both classes share.
134
+
135
+ Deliberately the same shape as `audioecho.FeedbackDelay`'s: fetch a
136
+ source block, process it in runs of at most FRAMES frames, and hand a
137
+ starved chain silence rather than a short block, because this sits in
138
+ the middle of a live graph and never reports itself finished.
139
+ """
140
+
141
+ _options = ()
142
+
143
+ def _init_format(self, sample_rate, channel_count, buffer_size=None):
144
+ channel_count = int(channel_count)
145
+ if channel_count not in (1, 2):
146
+ raise ValueError("channel_count must be 1 or 2")
147
+ if int(sample_rate) < 1:
148
+ raise ValueError("sample_rate must be at least 1")
149
+ self._frames = _frames_for(buffer_size, channel_count)
150
+ self.sample_rate = int(sample_rate)
151
+ self.bits_per_sample = 16
152
+ self.channel_count = channel_count
153
+ self.samples_signed = True
154
+ self.single_buffer = False
155
+ self.max_buffer_length = self._frames * 2 * channel_count
156
+ self._deinited = False
157
+ self._source = None
158
+ self._pending = b""
159
+
160
+ @property
161
+ def playing(self):
162
+ return self._source is not None
163
+
164
+ def play(self, sample, *, loop=False):
165
+ self._check()
166
+ self._source = sample
167
+ self._pending = b""
168
+
169
+ def stop(self):
170
+ self._source = None
171
+ self._pending = b""
172
+
173
+ def _release(self):
174
+ self.stop()
175
+
176
+ def clear(self):
177
+ """Zero the recursion. A filter's state is audible: a chain restarted
178
+ with the previous take still in it plays that take's tail over the
179
+ new one."""
180
+ self._check()
181
+ self._state.reset()
182
+
183
+ def _reset_buffer(self, single_channel_output=False, audio_channel=0):
184
+ self._check()
185
+ # A host reset clears what this node holds of its own and keeps the
186
+ # source frames it has already taken, the rule CircuitPython's effects
187
+ # follow and the one clear() always had: the source was not reset, so
188
+ # dropping them would skip that much of it.
189
+ self._state.reset()
190
+
191
+ def _refresh(self):
192
+ """Read every block input once and hand the values to the kernel.
193
+
194
+ The kernel clamps, not this: doing it in one place is what keeps the
195
+ four targets identical.
196
+ """
197
+ for slot, name in enumerate(self._options):
198
+ self._state.configure(slot, _value(getattr(self, name)))
199
+ self._state.finish()
200
+
201
+ def _apply_blocks(self, frames):
202
+ """Advance the block layer by one chunk, then read it.
203
+
204
+ Same point in the loop `audiofilters/Filter.c` reads its own, and the
205
+ MicroPython twin ticks and reads in exactly this order.
206
+ """
207
+ from synthio import _advance_blocks
208
+ _advance_blocks(self.sample_rate, frames)
209
+ self._refresh()
210
+
211
+ def _get_buffer(self, single_channel_output=False, audio_channel=0):
212
+ self._check()
213
+ width = 2 * self.channel_count
214
+ output = bytearray()
215
+ produced = 0
216
+ frames = self._frames
217
+ while produced < frames:
218
+ if not self._pending:
219
+ if self._source is None:
220
+ break
221
+ result, data = get_buffer(self._source, False, 0)
222
+ data = bytes(data)
223
+ if result == GET_BUFFER_ERROR or len(data) < width:
224
+ break
225
+ self._pending = data[:len(data) // width * width]
226
+ run = min(frames - produced, len(self._pending) // width)
227
+ self._apply_blocks(run)
228
+ output += self._state.process(self._pending[:run * width])
229
+ self._pending = self._pending[run * width:]
230
+ produced += run
231
+ if produced == 0:
232
+ return GET_BUFFER_MORE_DATA, self._publish(bytes(frames * width))
233
+ return GET_BUFFER_MORE_DATA, self._publish(output)
234
+
235
+
236
+ class Biquad(_Node):
237
+ """One RBJ section, float state, exact-zero tail."""
238
+
239
+ _options = _BIQUAD_OPTIONS
240
+
241
+ def __init__(self, mode=LOW_PASS, frequency=1000.0, Q=0.7071067811865475,
242
+ gain_db=0.0, mix=1.0, sample_rate=48000, channel_count=2,
243
+ buffer_size=None):
244
+ self._init_format(sample_rate, channel_count, buffer_size)
245
+ self.mode = mode
246
+ self.frequency = frequency
247
+ self.Q = Q
248
+ self.gain_db = gain_db
249
+ self.mix = mix
250
+ self._state = _audiodsp.BiquadF32State(
251
+ sample_rate=self.sample_rate, channel_count=self.channel_count)
252
+
253
+ @property
254
+ def mode(self):
255
+ return self._mode
256
+
257
+ @mode.setter
258
+ def mode(self, value):
259
+ # Not a BlockInput: it selects the algebra, not a value in it, and a
260
+ # mode that could arrive mid-block would mean recomputing the whole
261
+ # coefficient set from a number nobody chose.
262
+ value = int(value)
263
+ if value not in MODES:
264
+ raise ValueError("mode must be one of audiobiquad.MODES")
265
+ self._mode = value
266
+
267
+ @property
268
+ def coefficients(self):
269
+ """`(b0, b1, b2, a1, a2)`, normalized, at the settings in force.
270
+
271
+ Reads the block inputs but does not advance them, so looking at this
272
+ does not move an LFO along.
273
+ """
274
+ self._refresh()
275
+ return self._state.coefficients()
276
+
277
+
278
+ class AllPass(_Node):
279
+ """A first-order all-pass cascade with unclamped feedback.
280
+
281
+ `stages` is fixed at construction, because it sizes the state. `mix` is a
282
+ crossfade: 0 is a wire, 0.5 is the equal sum of dry and cascade that the
283
+ script phasers make -- and where the notches are deepest -- and 1 is the
284
+ bare cascade, which is magnitude-flat and so sounds like nothing.
285
+ """
286
+
287
+ _options = _ALLPASS_OPTIONS
288
+
289
+ def __init__(self, stages=4, frequency=1000.0, feedback=0.0, mix=1.0,
290
+ sample_rate=48000, channel_count=2, buffer_size=None):
291
+ stages = int(stages)
292
+ if stages < 1 or stages > MAX_STAGES:
293
+ raise ValueError("stages must be between 1 and %d" % MAX_STAGES)
294
+ self._init_format(sample_rate, channel_count, buffer_size)
295
+ self._stages = stages
296
+ self.frequency = frequency
297
+ self.feedback = feedback
298
+ self.mix = mix
299
+ self._state = _audiodsp.AllPassF32State(
300
+ sample_rate=self.sample_rate, channel_count=self.channel_count,
301
+ stages=stages)
302
+
303
+ @property
304
+ def stages(self):
305
+ return self._stages
306
+
307
+ @property
308
+ def coefficient(self):
309
+ """The all-pass coefficient at the frequency in force.
310
+
311
+ Reads the block inputs but does not advance them, so looking at this
312
+ does not move an LFO along.
313
+ """
314
+ self._refresh()
315
+ return self._state.coefficient()
316
+
317
+
318
+ __all__ = ("AllPass", "Biquad", "LOW_PASS", "HIGH_PASS", "BAND_PASS", "NOTCH",
319
+ "PEAKING_EQ", "LOW_SHELF", "HIGH_SHELF", "MODES")