react-native-audio-api 0.5.5 → 0.6.0-rc.0

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 (96) hide show
  1. package/RNAudioAPI.podspec +1 -1
  2. package/android/src/main/cpp/audioapi/android/core/AudioPlayer.cpp +0 -20
  3. package/android/src/main/cpp/audioapi/android/core/AudioPlayer.h +0 -2
  4. package/android/src/main/java/com/swmansion/audioapi/AudioAPIPackage.kt +13 -0
  5. package/android/src/main/java/com/swmansion/audioapi/AudioManagerModule.kt +59 -0
  6. package/android/src/oldarch/NativeAudioManagerModuleSpec.java +99 -0
  7. package/common/cpp/audioapi/AudioAPIModuleInstaller.h +30 -6
  8. package/common/cpp/audioapi/HostObjects/OfflineAudioContextHostObject.h +70 -0
  9. package/common/cpp/audioapi/core/AudioContext.cpp +1 -12
  10. package/common/cpp/audioapi/core/AudioContext.h +0 -1
  11. package/common/cpp/audioapi/core/OfflineAudioContext.cpp +117 -0
  12. package/common/cpp/audioapi/core/OfflineAudioContext.h +40 -0
  13. package/common/cpp/audioapi/core/sources/AudioBufferSourceNode.cpp +3 -3
  14. package/common/cpp/audioapi/core/sources/AudioScheduledSourceNode.cpp +28 -2
  15. package/common/cpp/audioapi/core/utils/AudioNodeDestructor.cpp +53 -0
  16. package/common/cpp/audioapi/core/utils/AudioNodeDestructor.h +33 -0
  17. package/common/cpp/audioapi/core/utils/AudioNodeManager.cpp +13 -10
  18. package/common/cpp/audioapi/core/utils/AudioNodeManager.h +3 -0
  19. package/common/cpp/audioapi/libs/signalsmith-stretch/fft-accelerate.h +326 -0
  20. package/common/cpp/audioapi/libs/signalsmith-stretch/fft.h +1257 -413
  21. package/common/cpp/audioapi/libs/signalsmith-stretch/signalsmith-stretch.h +398 -232
  22. package/common/cpp/audioapi/libs/signalsmith-stretch/stft.h +625 -0
  23. package/ios/audioapi/ios/AudioAPIModule.mm +2 -3
  24. package/ios/audioapi/ios/AudioManagerModule.h +18 -0
  25. package/ios/audioapi/ios/AudioManagerModule.mm +92 -0
  26. package/ios/audioapi/ios/core/AudioPlayer.h +4 -12
  27. package/ios/audioapi/ios/core/AudioPlayer.m +26 -108
  28. package/ios/audioapi/ios/core/IOSAudioPlayer.h +1 -3
  29. package/ios/audioapi/ios/core/IOSAudioPlayer.mm +4 -28
  30. package/ios/audioapi/ios/system/AudioEngine.h +23 -0
  31. package/ios/audioapi/ios/system/AudioEngine.mm +137 -0
  32. package/ios/audioapi/ios/system/AudioSessionManager.h +22 -0
  33. package/ios/audioapi/ios/system/AudioSessionManager.mm +183 -0
  34. package/ios/audioapi/ios/system/LockScreenManager.h +23 -0
  35. package/ios/audioapi/ios/system/LockScreenManager.mm +299 -0
  36. package/ios/audioapi/ios/system/NotificationManager.h +16 -0
  37. package/ios/audioapi/ios/system/NotificationManager.mm +151 -0
  38. package/lib/module/api.js +3 -1
  39. package/lib/module/api.js.map +1 -1
  40. package/lib/module/api.web.js +1 -0
  41. package/lib/module/api.web.js.map +1 -1
  42. package/lib/module/core/AudioContext.js +2 -1
  43. package/lib/module/core/AudioContext.js.map +1 -1
  44. package/lib/module/core/OfflineAudioContext.js +57 -0
  45. package/lib/module/core/OfflineAudioContext.js.map +1 -0
  46. package/lib/module/specs/NativeAudioManagerModule.js +31 -0
  47. package/lib/module/specs/NativeAudioManagerModule.js.map +1 -0
  48. package/lib/module/specs/index.js +6 -0
  49. package/lib/module/specs/index.js.map +1 -0
  50. package/lib/module/system/AudioManager.js +66 -0
  51. package/lib/module/system/AudioManager.js.map +1 -0
  52. package/lib/module/system/index.js +4 -0
  53. package/lib/module/system/index.js.map +1 -0
  54. package/lib/module/system/types.js +2 -0
  55. package/lib/module/system/types.js.map +1 -0
  56. package/lib/module/web-core/OfflineAudioContext.js +90 -0
  57. package/lib/module/web-core/OfflineAudioContext.js.map +1 -0
  58. package/lib/typescript/api.d.ts +4 -1
  59. package/lib/typescript/api.d.ts.map +1 -1
  60. package/lib/typescript/api.web.d.ts +1 -0
  61. package/lib/typescript/api.web.d.ts.map +1 -1
  62. package/lib/typescript/core/AudioContext.d.ts.map +1 -1
  63. package/lib/typescript/core/OfflineAudioContext.d.ts +14 -0
  64. package/lib/typescript/core/OfflineAudioContext.d.ts.map +1 -0
  65. package/lib/typescript/interfaces.d.ts +6 -0
  66. package/lib/typescript/interfaces.d.ts.map +1 -1
  67. package/lib/typescript/specs/NativeAudioManagerModule.d.ts +13 -0
  68. package/lib/typescript/specs/NativeAudioManagerModule.d.ts.map +1 -0
  69. package/lib/typescript/specs/index.d.ts +4 -0
  70. package/lib/typescript/specs/index.d.ts.map +1 -0
  71. package/lib/typescript/system/AudioManager.d.ts +12 -0
  72. package/lib/typescript/system/AudioManager.d.ts.map +1 -0
  73. package/lib/typescript/system/index.d.ts +2 -0
  74. package/lib/typescript/system/index.d.ts.map +1 -0
  75. package/lib/typescript/system/types.d.ts +28 -0
  76. package/lib/typescript/system/types.d.ts.map +1 -0
  77. package/lib/typescript/types.d.ts +5 -0
  78. package/lib/typescript/types.d.ts.map +1 -1
  79. package/lib/typescript/web-core/OfflineAudioContext.d.ts +34 -0
  80. package/lib/typescript/web-core/OfflineAudioContext.d.ts.map +1 -0
  81. package/package.json +2 -2
  82. package/src/api.ts +12 -2
  83. package/src/api.web.ts +1 -0
  84. package/src/core/AudioContext.ts +6 -1
  85. package/src/core/OfflineAudioContext.ts +94 -0
  86. package/src/interfaces.ts +11 -0
  87. package/src/specs/NativeAudioManagerModule.ts +51 -0
  88. package/src/specs/index.ts +6 -0
  89. package/src/system/AudioManager.ts +122 -0
  90. package/src/system/index.ts +1 -0
  91. package/src/system/types.ts +68 -0
  92. package/src/types.ts +6 -0
  93. package/src/web-core/OfflineAudioContext.tsx +163 -0
  94. package/common/cpp/audioapi/libs/signalsmith-stretch/delay.h +0 -715
  95. package/common/cpp/audioapi/libs/signalsmith-stretch/perf.h +0 -82
  96. package/common/cpp/audioapi/libs/signalsmith-stretch/spectral.h +0 -493
@@ -1,82 +0,0 @@
1
- #ifndef SIGNALSMITH_DSP_PERF_H
2
- #define SIGNALSMITH_DSP_PERF_H
3
-
4
- #include <complex>
5
-
6
- #if defined(__SSE__) || defined(_M_X64)
7
- # include <xmmintrin.h>
8
- #else
9
- # include <cstdint> // for uintptr_t
10
- #endif
11
-
12
- namespace signalsmith {
13
- namespace perf {
14
- /** @defgroup Performance Performance helpers
15
- @brief Nothing serious, just some `#defines` and helpers
16
-
17
- @{
18
- @file
19
- */
20
-
21
- /// *Really* insist that a function/method is inlined (mostly for performance in DEBUG builds)
22
- #ifndef SIGNALSMITH_INLINE
23
- #ifdef __GNUC__
24
- #define SIGNALSMITH_INLINE __attribute__((always_inline)) inline
25
- #elif defined(__MSVC__)
26
- #define SIGNALSMITH_INLINE __forceinline inline
27
- #else
28
- #define SIGNALSMITH_INLINE inline
29
- #endif
30
- #endif
31
-
32
- /** @brief Complex-multiplication (with optional conjugate second-arg), without handling NaN/Infinity
33
- The `std::complex` multiplication has edge-cases around NaNs which slow things down and prevent auto-vectorisation. Flags like `-ffast-math` sort this out anyway, but this helps with Debug builds.
34
- */
35
- template <bool conjugateSecond=false, typename V>
36
- SIGNALSMITH_INLINE static std::complex<V> mul(const std::complex<V> &a, const std::complex<V> &b) {
37
- return conjugateSecond ? std::complex<V>{
38
- b.real()*a.real() + b.imag()*a.imag(),
39
- b.real()*a.imag() - b.imag()*a.real()
40
- } : std::complex<V>{
41
- a.real()*b.real() - a.imag()*b.imag(),
42
- a.real()*b.imag() + a.imag()*b.real()
43
- };
44
- }
45
-
46
- #if defined(__SSE__) || defined(_M_X64)
47
- class StopDenormals {
48
- unsigned int controlStatusRegister;
49
- public:
50
- StopDenormals() : controlStatusRegister(_mm_getcsr()) {
51
- _mm_setcsr(controlStatusRegister|0x8040); // Flush-to-Zero and Denormals-Are-Zero
52
- }
53
- ~StopDenormals() {
54
- _mm_setcsr(controlStatusRegister);
55
- }
56
- };
57
- #elif (defined (__ARM_NEON) || defined (__ARM_NEON__))
58
- class StopDenormals {
59
- uintptr_t status;
60
- public:
61
- StopDenormals() {
62
- uintptr_t asmStatus;
63
- asm volatile("mrs %0, fpcr" : "=r"(asmStatus));
64
- status = asmStatus = asmStatus|0x01000000U; // Flush to Zero
65
- asm volatile("msr fpcr, %0" : : "ri"(asmStatus));
66
- }
67
- ~StopDenormals() {
68
- uintptr_t asmStatus = status;
69
- asm volatile("msr fpcr, %0" : : "ri"(asmStatus));
70
- }
71
- };
72
- #else
73
- # if __cplusplus >= 202302L
74
- # warning "The `StopDenormals` class doesn't do anything for this architecture"
75
- # endif
76
- class StopDenormals {}; // FIXME: add for other architectures
77
- #endif
78
-
79
- /** @} */
80
- }} // signalsmith::perf::
81
-
82
- #endif // include guard
@@ -1,493 +0,0 @@
1
- #include <audioapi/core/Constants.h>
2
-
3
- #ifndef SIGNALSMITH_DSP_SPECTRAL_H
4
- #define SIGNALSMITH_DSP_SPECTRAL_H
5
-
6
- #include <audioapi/libs/signalsmith-stretch/perf.h>
7
- #include <audioapi/libs/signalsmith-stretch/fft.h>
8
- #include <audioapi/libs/signalsmith-stretch/delay.h>
9
-
10
- #include <audioapi/dsp/Windows.h>
11
-
12
- #include <cmath>
13
-
14
- namespace signalsmith {
15
- namespace spectral {
16
- /** @defgroup Spectral Spectral Processing
17
- @brief Tools for frequency-domain manipulation of audio signals
18
-
19
- @{
20
- @file
21
- */
22
-
23
- /** @brief An FFT with built-in windowing and round-trip scaling
24
-
25
- This uses a Modified Real FFT, which applies half-bin shift before the transform. The result therefore has `N/2` bins, centred at the frequencies: `(i + 0.5)/N`.
26
-
27
- This avoids the awkward (real-valued) bands for DC-offset and Nyquist.
28
- */
29
- template<typename Sample>
30
- class WindowedFFT {
31
- using MRFFT = signalsmith::fft::ModifiedRealFFT<Sample>;
32
- using Complex = std::complex<Sample>;
33
- MRFFT mrfft{2};
34
-
35
- std::vector<Sample> fftWindow;
36
- std::vector<Sample> timeBuffer;
37
- int offsetSamples = 0;
38
- public:
39
- /// Returns a fast FFT size <= `size`
40
- static int fastSizeAbove(int size, int divisor=1) {
41
- return MRFFT::fastSizeAbove(size/divisor)*divisor;
42
- }
43
- /// Returns a fast FFT size >= `size`
44
- static int fastSizeBelow(int size, int divisor=1) {
45
- return MRFFT::fastSizeBelow(1 + (size - 1)/divisor)*divisor;
46
- }
47
-
48
- WindowedFFT() {}
49
- WindowedFFT(int size, int rotateSamples=0) {
50
- setSize(size, rotateSamples);
51
- }
52
- template<class WindowFn>
53
- WindowedFFT(int size, WindowFn fn, Sample windowOffset=0.5, int rotateSamples=0) {
54
- setSize(size, fn, windowOffset, rotateSamples);
55
- }
56
-
57
- /// Sets the size, returning the window for modification (initially all 1s)
58
- std::vector<Sample> & setSizeWindow(int size, int rotateSamples=0) {
59
- mrfft.setSize(size);
60
- fftWindow.assign(size, 1);
61
- timeBuffer.resize(size);
62
- offsetSamples = rotateSamples;
63
- if (offsetSamples < 0) offsetSamples += size; // TODO: for a negative rotation, the other half of the result is inverted
64
- return fftWindow;
65
- }
66
- /// Sets the FFT size, with a user-defined functor for the window
67
- template<class WindowFn>
68
- void setSize(int size, WindowFn fn, Sample windowOffset=0.5, int rotateSamples=0) {
69
- setSizeWindow(size, rotateSamples);
70
-
71
- Sample invSize = 1/(Sample)size;
72
- for (int i = 0; i < size; ++i) {
73
- Sample r = (i + windowOffset)*invSize;
74
- fftWindow[i] = fn(r);
75
- }
76
- }
77
- /// Sets the size (using the default Blackman-Harris window)
78
- void setSize(int size, int rotateSamples=0) {
79
- setSize(size, [](double x) {
80
- double phase = 2 * audioapi::PI * x;
81
- // Blackman-Harris
82
- return 0.35875 - 0.48829*std::cos(phase) + 0.14128*std::cos(phase*2) - 0.01168*std::cos(phase*3);
83
- }, Sample(0.5), rotateSamples);
84
- }
85
-
86
- const std::vector<Sample> & window() const {
87
- return this->fftWindow;
88
- }
89
- int size() const {
90
- return mrfft.size();
91
- }
92
-
93
- /// Performs an FFT, with windowing and rotation (if enabled)
94
- template<bool withWindow=true, bool withScaling=false, class Input, class Output>
95
- void fft(Input &&input, Output &&output) {
96
- int fftSize = size();
97
- const Sample norm = (withScaling ? 1/(Sample)fftSize : 1);
98
- for (int i = 0; i < offsetSamples; ++i) {
99
- // Inverted polarity since we're using the MRFFT
100
- timeBuffer[i + fftSize - offsetSamples] = -input[i]*norm*(withWindow ? fftWindow[i] : Sample(1));
101
- }
102
- for (int i = offsetSamples; i < fftSize; ++i) {
103
- timeBuffer[i - offsetSamples] = input[i]*norm*(withWindow ? fftWindow[i] : Sample(1));
104
- }
105
- mrfft.fft(timeBuffer, output);
106
- }
107
- /// Performs an FFT (no windowing or rotation)
108
- template<class Input, class Output>
109
- void fftRaw(Input &&input, Output &&output) {
110
- mrfft.fft(input, output);
111
- }
112
-
113
- /// Inverse FFT, with windowing, 1/N scaling and rotation (if enabled)
114
- template<bool withWindow=true, bool withScaling=true, class Input, class Output>
115
- void ifft(Input &&input, Output &&output) {
116
- mrfft.ifft(input, timeBuffer);
117
- int fftSize = mrfft.size();
118
- const Sample norm = (withScaling ? 1/(Sample)fftSize : 1);
119
-
120
- for (int i = 0; i < offsetSamples; ++i) {
121
- // Inverted polarity since we're using the MRFFT
122
- output[i] = -timeBuffer[i + fftSize - offsetSamples]*norm*(withWindow ? fftWindow[i] : Sample(1));
123
- }
124
- for (int i = offsetSamples; i < fftSize; ++i) {
125
- output[i] = timeBuffer[i - offsetSamples]*norm*(withWindow ? fftWindow[i] : Sample(1));
126
- }
127
- }
128
- /// Performs an IFFT (no windowing, scaling or rotation)
129
- template<class Input, class Output>
130
- void ifftRaw(Input &&input, Output &&output) {
131
- mrfft.ifft(input, output);
132
- }
133
- };
134
-
135
- /** STFT synthesis, built on a `MultiBuffer`.
136
-
137
- Any window length and block interval is supported, but the FFT size may be rounded up to a faster size (by zero-padding). It uses a heuristically-optimal Kaiser window modified for perfect-reconstruction.
138
-
139
- \diagram{stft-aliasing-simulated.svg,Simulated bad-case aliasing (random phase-shift for each band) for overlapping ratios}
140
-
141
- There is a "latest valid index", and you can read the output up to one `historyLength` behind this (see `.resize()`). You can read up to one window-length _ahead_ to get partially-summed future output.
142
-
143
- \diagram{stft-buffer-validity.svg}
144
-
145
- You move the valid index along using `.ensureValid()`, passing in a functor which provides spectra (using `.analyse()` and/or direct modification through `.spectrum[c]`):
146
-
147
- \code
148
- void processSample(...) {
149
- stft.ensureValid([&](int) {
150
- // Here, we introduce (1 - windowSize) of latency
151
- stft.analyse(inputBuffer.view(1 - windowSize))
152
- });
153
- // read as a MultiBuffer
154
- auto result = stft.at(0);
155
- ++stft; // also moves the latest valid index
156
- }
157
-
158
- void processBlock(...) {
159
- // assuming `historyLength` == blockSize
160
- stft.ensureValid(blockSize, [&](int blockStartIndex) {
161
- int inputStart = blockStartIndex + (1 - windowSize);
162
- stft.analyse(inputBuffer.view(inputStart));
163
- });
164
- auto earliestValid = stft.at(0);
165
- auto latestValid = stft.at(blockSize);
166
- stft += blockSize;
167
- }
168
- \endcode
169
-
170
- The index passed to this functor will be greater than the previous valid index, and `<=` the index you pass in. Therefore, if you call `.ensureValid()` every sample, it can only ever be `0`.
171
- */
172
- template<typename Sample>
173
- class STFT : public signalsmith::delay::MultiBuffer<Sample> {
174
-
175
- using Super = signalsmith::delay::MultiBuffer<Sample>;
176
- using Complex = std::complex<Sample>;
177
-
178
- int channels = 0, _windowSize = 0, _fftSize = 0, _interval = 1;
179
- int validUntilIndex = 0;
180
-
181
- class MultiSpectrum {
182
- int channels, stride;
183
- std::vector<Complex> buffer;
184
- public:
185
- MultiSpectrum() : MultiSpectrum(0, 0) {}
186
- MultiSpectrum(int channels, int bands) : channels(channels), stride(bands), buffer(channels*bands, 0) {}
187
-
188
- void resize(int nChannels, int nBands) {
189
- channels = nChannels;
190
- stride = nBands;
191
- buffer.assign(channels*stride, 0);
192
- }
193
-
194
- void reset() {
195
- buffer.assign(buffer.size(), 0);
196
- }
197
-
198
- void swap(MultiSpectrum &other) {
199
- using std::swap;
200
- swap(buffer, other.buffer);
201
- }
202
-
203
- Complex * operator [](int channel) {
204
- return buffer.data() + channel*stride;
205
- }
206
- const Complex * operator [](int channel) const {
207
- return buffer.data() + channel*stride;
208
- }
209
- };
210
- std::vector<Sample> timeBuffer;
211
-
212
- bool rotate = false;
213
- void resizeInternal(int newChannels, int windowSize, int newInterval, int historyLength, int zeroPadding) {
214
- Super::resize(newChannels,
215
- windowSize /* for output summing */
216
- + newInterval /* so we can read `windowSize` ahead (we'll be at most `interval-1` from the most recent block */
217
- + historyLength);
218
-
219
- int fftSize = fft.fastSizeAbove(windowSize + zeroPadding);
220
-
221
- this->channels = newChannels;
222
- _windowSize = windowSize;
223
- this->_fftSize = fftSize;
224
- this->_interval = newInterval;
225
- validUntilIndex = -1;
226
-
227
- setWindow(windowShape, rotate);
228
-
229
- spectrum.resize(channels, fftSize/2);
230
- timeBuffer.resize(fftSize);
231
- }
232
- public:
233
- enum class Window {kaiser, acg};
234
- /// \deprecated use `.setWindow()` which actually updates the window when you change it
235
- Window windowShape = Window::kaiser;
236
- // for convenience
237
- static constexpr Window kaiser = Window::kaiser;
238
- static constexpr Window acg = Window::acg;
239
-
240
- /** Swaps between the default (Kaiser) shape and Approximate Confined Gaussian (ACG).
241
- \diagram{stft-windows.svg,Default (Kaiser) windows and partial cumulative sum}
242
- The ACG has better rolloff since its edges go to 0:
243
- \diagram{stft-windows-acg.svg,ACG windows and partial cumulative sum}
244
- However, it generally has worse performance in terms of total sidelobe energy, affecting worst-case aliasing levels for (most) higher overlap ratios:
245
- \diagram{stft-aliasing-simulated-acg.svg,Simulated bad-case aliasing for ACG windows - compare with above}*/
246
- // TODO: these should both be set before resize()
247
- void setWindow(Window shape, bool rotateToZero=false) {
248
- windowShape = shape;
249
- rotate = rotateToZero;
250
-
251
- auto &window = fft.setSizeWindow(_fftSize, rotateToZero ? _windowSize/2 : 0);
252
- if (windowShape == Window::kaiser) {
253
- /// Roughly optimal Kaiser for STFT analysis (forced to perfect reconstruction)
254
- audioapi::dsp::Kaiser::withBandwidth(_windowSize/_interval, true).apply(window.data(), _windowSize);
255
- } else {
256
- audioapi::dsp::ApproximateConfinedGaussian::withBandwidth(_windowSize/_interval).apply(window.data(), _windowSize);
257
- }
258
- audioapi::dsp::WindowFunction::forcePerfectReconstruction(window.data(), _windowSize, _interval);
259
-
260
- // TODO: fill extra bits of an input buffer with NaN/Infinity, to break this, and then fix by adding zero-padding to WindowedFFT (as opposed to zero-valued window sections)
261
- for (int i = _windowSize; i < _fftSize; ++i) {
262
- window[i] = 0;
263
- }
264
- }
265
-
266
- using Spectrum = MultiSpectrum;
267
- Spectrum spectrum;
268
- WindowedFFT<Sample> fft;
269
-
270
- STFT() {}
271
- /// Parameters passed straight to `.resize()`
272
- STFT(int channels, int windowSize, int interval, int historyLength=0, int zeroPadding=0) {
273
- resize(channels, windowSize, interval, historyLength, zeroPadding);
274
- }
275
-
276
- /// Sets the channel-count, FFT size and interval.
277
- void resize(int nChannels, int windowSize, int interval, int historyLength=0, int zeroPadding=0) {
278
- resizeInternal(nChannels, windowSize, interval, historyLength, zeroPadding);
279
- }
280
-
281
- int windowSize() const {
282
- return _windowSize;
283
- }
284
- int fftSize() const {
285
- return _fftSize;
286
- }
287
- int interval() const {
288
- return _interval;
289
- }
290
- /// Returns the (analysis and synthesis) window
291
- decltype(fft.window()) window() const {
292
- return fft.window();
293
- }
294
- /// Calculates the effective window for the partially-summed future output (relative to the most recent block)
295
- std::vector<Sample> partialSumWindow(bool includeLatestBlock=true) const {
296
- const auto &w = window();
297
- std::vector<Sample> result(_windowSize, 0);
298
- int firstOffset = (includeLatestBlock ? 0 : _interval);
299
- for (int offset = firstOffset; offset < _windowSize; offset += _interval) {
300
- for (int i = 0; i < _windowSize - offset; ++i) {
301
- Sample value = w[i + offset];
302
- result[i] += value*value;
303
- }
304
- }
305
- return result;
306
- }
307
-
308
- /// Resets everything - since we clear the output sum, it will take `windowSize` samples to get proper output.
309
- void reset() {
310
- Super::reset();
311
- spectrum.reset();
312
- validUntilIndex = -1;
313
- }
314
-
315
- /** Generates valid output up to the specified index (or 0), using the callback as many times as needed.
316
-
317
- The callback should be a functor accepting a single integer argument, which is the index for which a spectrum is required.
318
-
319
- The block created from these spectra will start at this index in the output, plus `.latency()`.
320
- */
321
- template<class AnalysisFn>
322
- void ensureValid(int i, AnalysisFn fn) {
323
- while (validUntilIndex < i) {
324
- int blockIndex = validUntilIndex + 1;
325
- fn(blockIndex);
326
-
327
- auto output = this->view(blockIndex);
328
- for (int c = 0; c < channels; ++c) {
329
- auto channel = output[c];
330
-
331
- // Clear out the future sum, a window-length and an interval ahead
332
- for (int wi = _windowSize; wi < _windowSize + _interval; ++wi) {
333
- channel[wi] = 0;
334
- }
335
-
336
- // Add in the IFFT'd result
337
- fft.ifft(spectrum[c], timeBuffer);
338
- for (int wi = 0; wi < _windowSize; ++wi) {
339
- channel[wi] += timeBuffer[wi];
340
- }
341
- }
342
- validUntilIndex += _interval;
343
- }
344
- }
345
- /// The same as above, assuming index 0
346
- template<class AnalysisFn>
347
- void ensureValid(AnalysisFn fn) {
348
- return ensureValid(0, fn);
349
- }
350
- /// Returns the next invalid index (a.k.a. the index of the next block)
351
- int nextInvalid() const {
352
- return validUntilIndex + 1;
353
- }
354
-
355
- /** Analyse a multi-channel input, for any type where `data[channel][index]` returns samples
356
-
357
- Results can be read/edited using `.spectrum`. */
358
- template<class Data>
359
- void analyse(Data &&data) {
360
- for (int c = 0; c < channels; ++c) {
361
- fft.fft(data[c], spectrum[c]);
362
- }
363
- }
364
- template<class Data>
365
- void analyse(int c, Data &&data) {
366
- fft.fft(data, spectrum[c]);
367
- }
368
- /// Analyse without windowing or zero-rotation
369
- template<class Data>
370
- void analyseRaw(Data &&data) {
371
- for (int c = 0; c < channels; ++c) {
372
- fft.fftRaw(data[c], spectrum[c]);
373
- }
374
- }
375
- template<class Data>
376
- void analyseRaw(int c, Data &&data) {
377
- fft.fftRaw(data, spectrum[c]);
378
- }
379
-
380
- int bands() const {
381
- return _fftSize/2;
382
- }
383
-
384
- /** Internal latency (between the block-index requested in `.ensureValid()` and its position in the output)
385
-
386
- Currently unused, but it's in here to allow for a future implementation which spreads the FFT calculations out across each interval.*/
387
- int latency() {
388
- return 0;
389
- }
390
-
391
- // @name Shift the underlying buffer (moving the "valid" index accordingly)
392
- // @{
393
- STFT & operator ++() {
394
- Super::operator ++();
395
- validUntilIndex--;
396
- return *this;
397
- }
398
- STFT & operator +=(int i) {
399
- Super::operator +=(i);
400
- validUntilIndex -= i;
401
- return *this;
402
- }
403
- STFT & operator --() {
404
- Super::operator --();
405
- validUntilIndex++;
406
- return *this;
407
- }
408
- STFT & operator -=(int i) {
409
- Super::operator -=(i);
410
- validUntilIndex += i;
411
- return *this;
412
- }
413
- // @}
414
-
415
- typename Super::MutableView operator ++(int postIncrement) {
416
- auto result = Super::operator ++(postIncrement);
417
- validUntilIndex--;
418
- return result;
419
- }
420
- typename Super::MutableView operator --(int postIncrement) {
421
- auto result = Super::operator --(postIncrement);
422
- validUntilIndex++;
423
- return result;
424
- }
425
- };
426
-
427
- /** STFT processing, with input/output.
428
- Before calling `.ensureValid(index)`, you should make sure the input is filled up to `index`.
429
- */
430
- template<typename Sample>
431
- class ProcessSTFT : public STFT<Sample> {
432
- using Super = STFT<Sample>;
433
- public:
434
- signalsmith::delay::MultiBuffer<Sample> input;
435
-
436
- ProcessSTFT(int inChannels, int outChannels, int windowSize, int interval, int historyLength=0) {
437
- resize(inChannels, outChannels, windowSize, interval, historyLength);
438
- }
439
-
440
- /** Alter the spectrum, using input up to this point, for the output block starting from this point.
441
- Sub-classes should replace this with whatever processing is desired. */
442
- virtual void processSpectrum(int /*blockIndex*/) {}
443
-
444
- /// Sets the input/output channels, FFT size and interval.
445
- void resize(int inChannels, int outChannels, int windowSize, int interval, int historyLength=0) {
446
- Super::resize(outChannels, windowSize, interval, historyLength);
447
- input.resize(inChannels, windowSize + interval + historyLength);
448
- }
449
- void reset(Sample value=Sample()) {
450
- Super::reset(value);
451
- input.reset(value);
452
- }
453
-
454
- /// Internal latency, including buffering samples for analysis.
455
- int latency() {
456
- return Super::latency() + (this->windowSize() - 1);
457
- }
458
-
459
- void ensureValid(int i=0) {
460
- Super::ensureValid(i, [&](int blockIndex) {
461
- this->analyse(input.view(blockIndex - this->windowSize() + 1));
462
- this->processSpectrum(blockIndex);
463
- });
464
- }
465
-
466
- // @name Shift the output, input, and valid index.
467
- // @{
468
- ProcessSTFT & operator ++() {
469
- Super::operator ++();
470
- ++input;
471
- return *this;
472
- }
473
- ProcessSTFT & operator +=(int i) {
474
- Super::operator +=(i);
475
- input += i;
476
- return *this;
477
- }
478
- ProcessSTFT & operator --() {
479
- Super::operator --();
480
- --input;
481
- return *this;
482
- }
483
- ProcessSTFT & operator -=(int i) {
484
- Super::operator -=(i);
485
- input -= i;
486
- return *this;
487
- }
488
- // @}
489
- };
490
-
491
- /** @} */
492
- }} // signalsmith::spectral::
493
- #endif // include guard