@codexo/exojs-audio-fx 0.19.0 → 0.20.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.
- package/README.md +7 -53
- package/index.d.ts +1 -0
- package/index.js +1 -0
- package/package.json +12 -20
- package/dist/esm/AudioAnalyser.d.ts +0 -125
- package/dist/esm/AudioAnalyser.d.ts.map +0 -1
- package/dist/esm/AudioAnalyser.js +0 -369
- package/dist/esm/AudioAnalyser.js.map +0 -1
- package/dist/esm/BeatDetector.d.ts +0 -286
- package/dist/esm/BeatDetector.d.ts.map +0 -1
- package/dist/esm/BeatDetector.js +0 -419
- package/dist/esm/BeatDetector.js.map +0 -1
- package/dist/esm/PcmStreamSource.d.ts +0 -127
- package/dist/esm/PcmStreamSource.d.ts.map +0 -1
- package/dist/esm/PcmStreamSource.js +0 -308
- package/dist/esm/PcmStreamSource.js.map +0 -1
- package/dist/esm/dsp/fft.d.ts +0 -22
- package/dist/esm/dsp/fft.d.ts.map +0 -1
- package/dist/esm/dsp/mel.d.ts +0 -43
- package/dist/esm/dsp/mel.d.ts.map +0 -1
- package/dist/esm/dsp/mel.js +0 -61
- package/dist/esm/dsp/mel.js.map +0 -1
- package/dist/esm/dsp/tempogram.d.ts +0 -192
- package/dist/esm/dsp/tempogram.d.ts.map +0 -1
- package/dist/esm/effects/AutoWahEffect.d.ts +0 -128
- package/dist/esm/effects/AutoWahEffect.d.ts.map +0 -1
- package/dist/esm/effects/AutoWahEffect.js +0 -214
- package/dist/esm/effects/AutoWahEffect.js.map +0 -1
- package/dist/esm/effects/BitCrusherEffect.d.ts +0 -64
- package/dist/esm/effects/BitCrusherEffect.d.ts.map +0 -1
- package/dist/esm/effects/BitCrusherEffect.js +0 -76
- package/dist/esm/effects/BitCrusherEffect.js.map +0 -1
- package/dist/esm/effects/ChorusEffect.d.ts +0 -54
- package/dist/esm/effects/ChorusEffect.d.ts.map +0 -1
- package/dist/esm/effects/ChorusEffect.js +0 -136
- package/dist/esm/effects/ChorusEffect.js.map +0 -1
- package/dist/esm/effects/CompressorEffect.d.ts +0 -56
- package/dist/esm/effects/CompressorEffect.d.ts.map +0 -1
- package/dist/esm/effects/CompressorEffect.js +0 -108
- package/dist/esm/effects/CompressorEffect.js.map +0 -1
- package/dist/esm/effects/ConvolutionEffect.d.ts +0 -119
- package/dist/esm/effects/ConvolutionEffect.d.ts.map +0 -1
- package/dist/esm/effects/ConvolutionEffect.js +0 -212
- package/dist/esm/effects/ConvolutionEffect.js.map +0 -1
- package/dist/esm/effects/DelayEffect.d.ts +0 -36
- package/dist/esm/effects/DelayEffect.d.ts.map +0 -1
- package/dist/esm/effects/DelayEffect.js +0 -109
- package/dist/esm/effects/DelayEffect.js.map +0 -1
- package/dist/esm/effects/DistortionEffect.d.ts +0 -120
- package/dist/esm/effects/DistortionEffect.d.ts.map +0 -1
- package/dist/esm/effects/DistortionEffect.js +0 -194
- package/dist/esm/effects/DistortionEffect.js.map +0 -1
- package/dist/esm/effects/DuckingEffect.d.ts +0 -44
- package/dist/esm/effects/DuckingEffect.d.ts.map +0 -1
- package/dist/esm/effects/DuckingEffect.js +0 -95
- package/dist/esm/effects/DuckingEffect.js.map +0 -1
- package/dist/esm/effects/EqualizerEffect.d.ts +0 -51
- package/dist/esm/effects/EqualizerEffect.d.ts.map +0 -1
- package/dist/esm/effects/EqualizerEffect.js +0 -124
- package/dist/esm/effects/EqualizerEffect.js.map +0 -1
- package/dist/esm/effects/FlangerEffect.d.ts +0 -99
- package/dist/esm/effects/FlangerEffect.d.ts.map +0 -1
- package/dist/esm/effects/FlangerEffect.js +0 -176
- package/dist/esm/effects/FlangerEffect.js.map +0 -1
- package/dist/esm/effects/GranularEffect.d.ts +0 -67
- package/dist/esm/effects/GranularEffect.d.ts.map +0 -1
- package/dist/esm/effects/GranularEffect.js +0 -104
- package/dist/esm/effects/GranularEffect.js.map +0 -1
- package/dist/esm/effects/LimiterEffect.d.ts +0 -125
- package/dist/esm/effects/LimiterEffect.d.ts.map +0 -1
- package/dist/esm/effects/LimiterEffect.js +0 -186
- package/dist/esm/effects/LimiterEffect.js.map +0 -1
- package/dist/esm/effects/PhaserEffect.d.ts +0 -127
- package/dist/esm/effects/PhaserEffect.d.ts.map +0 -1
- package/dist/esm/effects/PhaserEffect.js +0 -218
- package/dist/esm/effects/PhaserEffect.js.map +0 -1
- package/dist/esm/effects/PingPongDelayEffect.d.ts +0 -84
- package/dist/esm/effects/PingPongDelayEffect.d.ts.map +0 -1
- package/dist/esm/effects/PingPongDelayEffect.js +0 -176
- package/dist/esm/effects/PingPongDelayEffect.js.map +0 -1
- package/dist/esm/effects/PitchShiftEffect.d.ts +0 -46
- package/dist/esm/effects/PitchShiftEffect.d.ts.map +0 -1
- package/dist/esm/effects/PitchShiftEffect.js +0 -64
- package/dist/esm/effects/PitchShiftEffect.js.map +0 -1
- package/dist/esm/effects/ReverbEffect.d.ts +0 -38
- package/dist/esm/effects/ReverbEffect.d.ts.map +0 -1
- package/dist/esm/effects/ReverbEffect.js +0 -117
- package/dist/esm/effects/ReverbEffect.js.map +0 -1
- package/dist/esm/effects/RingModulatorEffect.d.ts +0 -96
- package/dist/esm/effects/RingModulatorEffect.d.ts.map +0 -1
- package/dist/esm/effects/RingModulatorEffect.js +0 -157
- package/dist/esm/effects/RingModulatorEffect.js.map +0 -1
- package/dist/esm/effects/TremoloEffect.d.ts +0 -111
- package/dist/esm/effects/TremoloEffect.d.ts.map +0 -1
- package/dist/esm/effects/TremoloEffect.js +0 -197
- package/dist/esm/effects/TremoloEffect.js.map +0 -1
- package/dist/esm/effects/VocoderEffect.d.ts +0 -45
- package/dist/esm/effects/VocoderEffect.d.ts.map +0 -1
- package/dist/esm/effects/VocoderEffect.js +0 -73
- package/dist/esm/effects/VocoderEffect.js.map +0 -1
- package/dist/esm/index.d.ts +0 -25
- package/dist/esm/index.d.ts.map +0 -1
- package/dist/esm/index.js +0 -24
- package/dist/esm/worklets/beat-detector.worklet.js +0 -6
- package/dist/esm/worklets/beat-detector.worklet.js.map +0 -1
- package/dist/esm/worklets/bit-crusher.worklet.js +0 -6
- package/dist/esm/worklets/bit-crusher.worklet.js.map +0 -1
- package/dist/esm/worklets/ducking.worklet.js +0 -6
- package/dist/esm/worklets/ducking.worklet.js.map +0 -1
- package/dist/esm/worklets/granular.worklet.js +0 -6
- package/dist/esm/worklets/granular.worklet.js.map +0 -1
- package/dist/esm/worklets/pcm-stream.worklet.js +0 -6
- package/dist/esm/worklets/pcm-stream.worklet.js.map +0 -1
- package/dist/esm/worklets/pitch-shift.worklet.js +0 -6
- package/dist/esm/worklets/pitch-shift.worklet.js.map +0 -1
- package/dist/esm/worklets/vocoder.worklet.js +0 -6
- package/dist/esm/worklets/vocoder.worklet.js.map +0 -1
package/README.md
CHANGED
|
@@ -1,59 +1,13 @@
|
|
|
1
1
|
# @codexo/exojs-audio-fx
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Since ExoJS v0.20, import from @codexo/exojs/audio-fx. This package is a compatibility shim for the 0.20 line; no independent feature development.
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
Every export of this package is the very binding its Core entry exports, so code that still imports `@codexo/exojs-audio-fx` shares one engine instance with code that already imports `@codexo/exojs/audio-fx`. The package contains no implementation of its own and depends on nothing but its `@codexo/exojs` peer.
|
|
6
6
|
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
7
|
+
| Import | Replace with | Forwards |
|
|
8
|
+
| --- | --- | --- |
|
|
9
|
+
| `@codexo/exojs-audio-fx` | `@codexo/exojs/audio-fx` | everything |
|
|
10
10
|
|
|
11
|
-
|
|
11
|
+
## Migrating
|
|
12
12
|
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
This helper expects an initialized application with audio already playing on its music bus. It does not load or start a track:
|
|
16
|
-
|
|
17
|
-
```ts
|
|
18
|
-
import type { Application } from '@codexo/exojs';
|
|
19
|
-
import { AudioAnalyser } from '@codexo/exojs-audio-fx';
|
|
20
|
-
|
|
21
|
-
export const inspectMusic = (app: Application): { read: () => Uint8Array; destroy: () => void } => {
|
|
22
|
-
const analyser = new AudioAnalyser({ source: app.audio.music, fftSize: 1024 });
|
|
23
|
-
|
|
24
|
-
return {
|
|
25
|
-
read: () => analyser.getSpectrumLog(undefined, { bands: 16 }),
|
|
26
|
-
destroy: () => analyser.destroy(),
|
|
27
|
-
};
|
|
28
|
-
};
|
|
29
|
-
```
|
|
30
|
-
|
|
31
|
-
Call `read` from the owner's update path and destroy the helper when that owner ends. An analyser taps a live bus or voice, not an unloaded asset descriptor. A silent or muted routing path is not repaired by repeatedly creating analysers.
|
|
32
|
-
|
|
33
|
-
## Externally produced PCM
|
|
34
|
-
|
|
35
|
-
Use `PcmStreamSource` for bounded mono/stereo Float32 streaming into an `AudioBus`. Await readiness, enqueue at the source's sample rate, then explicitly start playback; caller-owned arrays are copied and remain reusable. See [the PCM contract and lifecycle guide](docs/pcm-streaming.md) and [the runnable streaming example](../../examples/audio-fx/pcm-stream.ts) for scheduling, overflow, underrun telemetry, clear and drain.
|
|
36
|
-
|
|
37
|
-
For GPU-generated samples, the [GPU stereo PCM guide](docs/gpu-pcm.md) and [example](../../examples/audio-fx/gpu-pcm-stream.ts) connect bounded readback slots to the source, with explicit byte packing or typed float readback on both rendering backends.
|
|
38
|
-
|
|
39
|
-
## Important boundaries
|
|
40
|
-
|
|
41
|
-
Browser audio needs a real user-gesture path. Worklet-backed processors can have asynchronous initialization and capability requirements; handle failure and teardown rather than assuming construction means readiness.
|
|
42
|
-
|
|
43
|
-
Effects, analyser nodes, detectors, and playing voices have distinct lifetimes. Track caller-created resources with their scene or dispose them at their own boundary. Do not leave a detector connected merely because its visual widget was removed.
|
|
44
|
-
|
|
45
|
-
Beat detection estimates tempo and phase. Polling windows such as `justBeat` are not one-shot events, and asynchronous messages are not hard real-time delivery. A rhythm-game scoring timeline needs an explicit authoritative timing design. Analysis values are not a calibrated acoustic loudness measurement.
|
|
46
|
-
|
|
47
|
-
## Documentation
|
|
48
|
-
|
|
49
|
-
[Audio basics](https://exoridus.github.io/ExoJS/en/guide/audio/audio-basics/) · [Effects and routing](https://exoridus.github.io/ExoJS/en/guide/audio/audio-effects/) · [Beat detection](https://exoridus.github.io/ExoJS/en/guide/audio/beat-detection/) · [Audio-reactive visuals](https://exoridus.github.io/ExoJS/en/guide/audio/audio-reactive-visualization/) · [AudioAnalyser API](https://exoridus.github.io/ExoJS/en/api/audio-analyser/)
|
|
50
|
-
|
|
51
|
-
## Beat evaluation
|
|
52
|
-
|
|
53
|
-
[The synthetic MIR evaluation](docs/mir-evaluation.md) documents reproducible quality and posting-latency measurements, adversarial fixtures, provisional-versus-locked comparisons, and their limits. Run the named evaluation test from the repository root; no external audio dataset is required.
|
|
54
|
-
|
|
55
|
-
[The recorded music corpus](test/fixtures/music/README.md) complements it with five committed CC0 excerpts. Its tests run offline and distinguish unreviewed beat proposals from reviewed references; passing integration checks alone does not establish accuracy on music.
|
|
56
|
-
|
|
57
|
-
## License
|
|
58
|
-
|
|
59
|
-
MIT
|
|
13
|
+
Change each import to the Core entry in the table, then remove this package with `npm uninstall @codexo/exojs-audio-fx`. `@codexo/exojs` already contains the module; there is nothing else to install. The migration guide on the ExoJS site lists every renamed import.
|
package/index.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from '@codexo/exojs/audio-fx';
|
package/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from '@codexo/exojs/audio-fx';
|
package/package.json
CHANGED
|
@@ -1,46 +1,38 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@codexo/exojs-audio-fx",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"description": "
|
|
3
|
+
"version": "0.20.0",
|
|
4
|
+
"description": "Compatibility shim that forwards to @codexo/exojs/audio-fx. Since ExoJS v0.20, import from @codexo/exojs/audio-fx.",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
7
7
|
"url": "git+https://github.com/Exoridus/ExoJS.git",
|
|
8
|
-
"directory": "packages/exojs-audio-fx"
|
|
8
|
+
"directory": "packages/legacy/exojs-audio-fx"
|
|
9
9
|
},
|
|
10
10
|
"type": "module",
|
|
11
11
|
"sideEffects": false,
|
|
12
|
-
"main": "./
|
|
13
|
-
"
|
|
14
|
-
"types": "./dist/esm/index.d.ts",
|
|
12
|
+
"main": "./index.js",
|
|
13
|
+
"types": "./index.d.ts",
|
|
15
14
|
"exports": {
|
|
16
15
|
".": {
|
|
17
|
-
"types": "./
|
|
18
|
-
"import": "./
|
|
19
|
-
"default": "./
|
|
16
|
+
"types": "./index.d.ts",
|
|
17
|
+
"import": "./index.js",
|
|
18
|
+
"default": "./index.js"
|
|
20
19
|
},
|
|
21
20
|
"./package.json": "./package.json"
|
|
22
21
|
},
|
|
23
22
|
"files": [
|
|
24
|
-
"
|
|
23
|
+
"index.js",
|
|
24
|
+
"index.d.ts",
|
|
25
25
|
"README.md",
|
|
26
26
|
"LICENSE"
|
|
27
27
|
],
|
|
28
28
|
"peerDependencies": {
|
|
29
|
-
"@codexo/exojs": "0.
|
|
29
|
+
"@codexo/exojs": "0.20.x"
|
|
30
30
|
},
|
|
31
31
|
"devDependencies": {
|
|
32
|
-
"@codexo/exojs": "0.
|
|
33
|
-
"@codexo/exojs-config": "0.0.0"
|
|
32
|
+
"@codexo/exojs": "0.20.0"
|
|
34
33
|
},
|
|
35
34
|
"license": "MIT",
|
|
36
35
|
"publishConfig": {
|
|
37
36
|
"access": "public"
|
|
38
|
-
},
|
|
39
|
-
"scripts": {
|
|
40
|
-
"build": "tsx ../../scripts/build-extension.ts",
|
|
41
|
-
"build:dev": "tsx ../../scripts/build-extension.ts --dev",
|
|
42
|
-
"typecheck": "exo-tsc --noEmit && exo-tsc --noEmit -p tsconfig.test.json && exo-tsc --noEmit -p tsconfig.worklets.json",
|
|
43
|
-
"lint": "eslint \"src/**/*.ts\" \"test/**/*.ts\"",
|
|
44
|
-
"test": "vitest run --root ../.. --project=exojs-audio-fx"
|
|
45
37
|
}
|
|
46
38
|
}
|
|
@@ -1,125 +0,0 @@
|
|
|
1
|
-
import { type AudioBus, type Voice } from '@codexo/exojs';
|
|
2
|
-
export type AudioAnalyserSource = AudioBus | Voice | MediaStream | AudioNode | null;
|
|
3
|
-
/** Construction options for {@link AudioAnalyser}. */
|
|
4
|
-
export interface AudioAnalyserOptions {
|
|
5
|
-
/** FFT window size; must be a power of two between 32 and 32768. Default 2048. */
|
|
6
|
-
fftSize?: number;
|
|
7
|
-
/** Time-domain smoothing constant in 0..1 applied between successive analyses. Default 0.8. */
|
|
8
|
-
smoothingTimeConstant?: number;
|
|
9
|
-
/** Minimum dBFS rendered by the byte-domain spectrum getters. Default -100. */
|
|
10
|
-
minDecibels?: number;
|
|
11
|
-
/** Maximum dBFS rendered by the byte-domain spectrum getters. Default -30. */
|
|
12
|
-
maxDecibels?: number;
|
|
13
|
-
/**
|
|
14
|
-
* Optional initial source. Equivalent to `analyser.source = value` after
|
|
15
|
-
* construction; provided for ergonomic one-shot construction. The setter
|
|
16
|
-
* remains usable for runtime source switches.
|
|
17
|
-
*/
|
|
18
|
-
source?: AudioAnalyserSource;
|
|
19
|
-
}
|
|
20
|
-
/** Mapping options for {@link AudioAnalyser.getSpectrumMel} and {@link AudioAnalyser.getSpectrumLog}. */
|
|
21
|
-
export interface SpectrumMappingOptions {
|
|
22
|
-
/** Number of output bands. Default 32. */
|
|
23
|
-
bands?: number;
|
|
24
|
-
/** Lower frequency bound in Hz. Default 20. */
|
|
25
|
-
fMin?: number;
|
|
26
|
-
/** Upper frequency bound in Hz. Default 20000 (clamped to nyquist at runtime). */
|
|
27
|
-
fMax?: number;
|
|
28
|
-
}
|
|
29
|
-
/**
|
|
30
|
-
* Lightweight visualisation analyser backed by a Web Audio AnalyserNode.
|
|
31
|
-
*
|
|
32
|
-
* Accepts any of: AudioBus, Voice, MediaStream, AudioNode, or null. The tap is
|
|
33
|
-
* a parallel branch - it does not affect the source's main routing. Tap a bus
|
|
34
|
-
* for a whole submix, or an individual {@link Voice} (its `output` node) for a
|
|
35
|
-
* single playing instance.
|
|
36
|
-
*/
|
|
37
|
-
export declare class AudioAnalyser {
|
|
38
|
-
private _analyser;
|
|
39
|
-
private readonly _options;
|
|
40
|
-
private _source;
|
|
41
|
-
private _tapSource;
|
|
42
|
-
private _streamSource;
|
|
43
|
-
private _pendingSourceSetup;
|
|
44
|
-
private readonly _onAudioContextReady;
|
|
45
|
-
private _byteSpectrum;
|
|
46
|
-
private _floatSpectrum;
|
|
47
|
-
private _byteWaveform;
|
|
48
|
-
private _floatWaveform;
|
|
49
|
-
private _melCache;
|
|
50
|
-
private _logCache;
|
|
51
|
-
/**
|
|
52
|
-
* Create an `AudioAnalyser` with the given options. The underlying
|
|
53
|
-
* `AnalyserNode` is created immediately if the `AudioContext` is already
|
|
54
|
-
* running, or deferred until {@link onAudioContextReady} fires.
|
|
55
|
-
*/
|
|
56
|
-
constructor(options?: AudioAnalyserOptions);
|
|
57
|
-
/**
|
|
58
|
-
* The currently tapped audio source, or `null` if none is set.
|
|
59
|
-
* Assigning a new value disconnects the previous tap and connects the new
|
|
60
|
-
* source as a parallel branch without affecting the source's main routing.
|
|
61
|
-
*/
|
|
62
|
-
get source(): AudioAnalyserSource;
|
|
63
|
-
set source(value: AudioAnalyserSource);
|
|
64
|
-
get fftSize(): number;
|
|
65
|
-
/** Number of frequency bins available - half of `fftSize`. */
|
|
66
|
-
get frequencyBinCount(): number;
|
|
67
|
-
get smoothingTimeConstant(): number;
|
|
68
|
-
set smoothingTimeConstant(v: number);
|
|
69
|
-
get minDecibels(): number;
|
|
70
|
-
set minDecibels(v: number);
|
|
71
|
-
get maxDecibels(): number;
|
|
72
|
-
set maxDecibels(v: number);
|
|
73
|
-
/** Fill and return the byte frequency spectrum (0..255 per bin). */
|
|
74
|
-
getSpectrum(into?: Uint8Array<ArrayBuffer>): Uint8Array<ArrayBuffer>;
|
|
75
|
-
/** Fill and return the float frequency spectrum (dB per bin). */
|
|
76
|
-
getSpectrumFloat(into?: Float32Array<ArrayBuffer>): Float32Array<ArrayBuffer>;
|
|
77
|
-
/** Fill and return the byte time-domain waveform (0..255 per sample). */
|
|
78
|
-
getWaveform(into?: Uint8Array<ArrayBuffer>): Uint8Array<ArrayBuffer>;
|
|
79
|
-
/** Fill and return the float time-domain waveform (-1..1 per sample). */
|
|
80
|
-
getWaveformFloat(into?: Float32Array<ArrayBuffer>): Float32Array<ArrayBuffer>;
|
|
81
|
-
/**
|
|
82
|
-
* Return the mean energy of frequency bins in the given Hz range, normalised 0..1.
|
|
83
|
-
* Uses the byte spectrum (0..255), normalised by dividing by 255.
|
|
84
|
-
*/
|
|
85
|
-
getBandEnergy(fromHz: number, toHz: number): number;
|
|
86
|
-
/** Return low (0-250 Hz), mid (250-2 kHz), and high (2 k-20 kHz) band energies (0..1 each). */
|
|
87
|
-
getLowMidHigh(): {
|
|
88
|
-
low: number;
|
|
89
|
-
mid: number;
|
|
90
|
-
high: number;
|
|
91
|
-
};
|
|
92
|
-
/** Return overall RMS energy across all bins, normalised 0..1. */
|
|
93
|
-
getRms(): number;
|
|
94
|
-
/**
|
|
95
|
-
* Mel-scaled spectrum, byte domain (0..255). Output length = `bands`.
|
|
96
|
-
* Each output bin is a triangular-weighted sum of the linear FFT bins
|
|
97
|
-
* underneath it, mel-spaced from `fMin` to `fMax`.
|
|
98
|
-
*/
|
|
99
|
-
getSpectrumMel(into?: Uint8Array<ArrayBuffer>, options?: SpectrumMappingOptions): Uint8Array<ArrayBuffer>;
|
|
100
|
-
/** Mel-scaled spectrum, float domain (dBFS values). Output length = `bands`. */
|
|
101
|
-
getSpectrumMelFloat(into?: Float32Array<ArrayBuffer>, options?: SpectrumMappingOptions): Float32Array<ArrayBuffer>;
|
|
102
|
-
/**
|
|
103
|
-
* Log-scaled spectrum, byte domain (0..255). Output length = `bands`.
|
|
104
|
-
* Bins are spaced so each output covers an equal fraction of the
|
|
105
|
-
* `log2(fMax / fMin)` octave range - useful when you want a
|
|
106
|
-
* frequency-axis visualization where each octave gets the same width.
|
|
107
|
-
*/
|
|
108
|
-
getSpectrumLog(into?: Uint8Array<ArrayBuffer>, options?: SpectrumMappingOptions): Uint8Array<ArrayBuffer>;
|
|
109
|
-
/** Log-scaled spectrum, float domain (dBFS values). Output length = `bands`. */
|
|
110
|
-
getSpectrumLogFloat(into?: Float32Array<ArrayBuffer>, options?: SpectrumMappingOptions): Float32Array<ArrayBuffer>;
|
|
111
|
-
/**
|
|
112
|
-
* Disconnect the analyser tap, release the internal `AnalyserNode`, and
|
|
113
|
-
* cancel any pending setup callbacks. Call when the analyser is no longer
|
|
114
|
-
* needed to avoid memory leaks.
|
|
115
|
-
*/
|
|
116
|
-
destroy(): void;
|
|
117
|
-
private _getMelFilterbank;
|
|
118
|
-
private _getLogRanges;
|
|
119
|
-
private _setupAnalyser;
|
|
120
|
-
private _connectSource;
|
|
121
|
-
private _resolveToAudioNode;
|
|
122
|
-
private _deferConnectionViaBus;
|
|
123
|
-
private _disconnectTap;
|
|
124
|
-
}
|
|
125
|
-
//# sourceMappingURL=AudioAnalyser.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"AudioAnalyser.d.ts","sourceRoot":"","sources":["../../src/AudioAnalyser.ts"],"names":[],"mappings":"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"}
|
|
@@ -1,369 +0,0 @@
|
|
|
1
|
-
import { buildMelFilterbank } from "./dsp/mel.js";
|
|
2
|
-
import { getAudioContext, isAudioContextReady, onAudioContextReady } from "@codexo/exojs";
|
|
3
|
-
|
|
4
|
-
//#region src/AudioAnalyser.ts
|
|
5
|
-
/**
|
|
6
|
-
* Lightweight visualisation analyser backed by a Web Audio AnalyserNode.
|
|
7
|
-
*
|
|
8
|
-
* Accepts any of: AudioBus, Voice, MediaStream, AudioNode, or null. The tap is
|
|
9
|
-
* a parallel branch - it does not affect the source's main routing. Tap a bus
|
|
10
|
-
* for a whole submix, or an individual {@link Voice} (its `output` node) for a
|
|
11
|
-
* single playing instance.
|
|
12
|
-
*/
|
|
13
|
-
var AudioAnalyser = class {
|
|
14
|
-
_analyser = null;
|
|
15
|
-
_options;
|
|
16
|
-
_source = null;
|
|
17
|
-
_tapSource = null;
|
|
18
|
-
_streamSource = null;
|
|
19
|
-
_pendingSourceSetup = null;
|
|
20
|
-
_onAudioContextReady = (ctx) => {
|
|
21
|
-
onAudioContextReady.remove(this._onAudioContextReady);
|
|
22
|
-
this._setupAnalyser(ctx);
|
|
23
|
-
};
|
|
24
|
-
_byteSpectrum;
|
|
25
|
-
_floatSpectrum;
|
|
26
|
-
_byteWaveform;
|
|
27
|
-
_floatWaveform;
|
|
28
|
-
_melCache = /* @__PURE__ */ new Map();
|
|
29
|
-
_logCache = /* @__PURE__ */ new Map();
|
|
30
|
-
/**
|
|
31
|
-
* Create an `AudioAnalyser` with the given options. The underlying
|
|
32
|
-
* `AnalyserNode` is created immediately if the `AudioContext` is already
|
|
33
|
-
* running, or deferred until {@link onAudioContextReady} fires.
|
|
34
|
-
*/
|
|
35
|
-
constructor(options) {
|
|
36
|
-
this._options = {
|
|
37
|
-
fftSize: options?.fftSize ?? 2048,
|
|
38
|
-
smoothingTimeConstant: options?.smoothingTimeConstant ?? .8,
|
|
39
|
-
minDecibels: options?.minDecibels ?? -100,
|
|
40
|
-
maxDecibels: options?.maxDecibels ?? -30,
|
|
41
|
-
source: options?.source ?? null
|
|
42
|
-
};
|
|
43
|
-
const binCount = this._options.fftSize >> 1;
|
|
44
|
-
this._byteSpectrum = new Uint8Array(binCount);
|
|
45
|
-
this._floatSpectrum = new Float32Array(binCount);
|
|
46
|
-
this._byteWaveform = new Uint8Array(binCount);
|
|
47
|
-
this._floatWaveform = new Float32Array(binCount);
|
|
48
|
-
if (isAudioContextReady()) this._setupAnalyser(getAudioContext());
|
|
49
|
-
else onAudioContextReady.add(this._onAudioContextReady);
|
|
50
|
-
if (options?.source !== void 0 && options.source !== null) this.source = options.source;
|
|
51
|
-
}
|
|
52
|
-
/**
|
|
53
|
-
* The currently tapped audio source, or `null` if none is set.
|
|
54
|
-
* Assigning a new value disconnects the previous tap and connects the new
|
|
55
|
-
* source as a parallel branch without affecting the source's main routing.
|
|
56
|
-
*/
|
|
57
|
-
get source() {
|
|
58
|
-
return this._source;
|
|
59
|
-
}
|
|
60
|
-
set source(value) {
|
|
61
|
-
if (value === this._source) return;
|
|
62
|
-
this._disconnectTap();
|
|
63
|
-
this._source = value;
|
|
64
|
-
if (value === null) return;
|
|
65
|
-
if (isAudioContextReady()) {
|
|
66
|
-
this._pendingSourceSetup = null;
|
|
67
|
-
this._connectSource(value, getAudioContext());
|
|
68
|
-
} else {
|
|
69
|
-
if (this._pendingSourceSetup !== null) onAudioContextReady.remove(this._pendingSourceSetup);
|
|
70
|
-
const handler = (ctx) => {
|
|
71
|
-
onAudioContextReady.remove(handler);
|
|
72
|
-
this._pendingSourceSetup = null;
|
|
73
|
-
this._connectSource(value, ctx);
|
|
74
|
-
};
|
|
75
|
-
this._pendingSourceSetup = handler;
|
|
76
|
-
onAudioContextReady.add(handler);
|
|
77
|
-
}
|
|
78
|
-
}
|
|
79
|
-
get fftSize() {
|
|
80
|
-
return this._options.fftSize;
|
|
81
|
-
}
|
|
82
|
-
/** Number of frequency bins available - half of `fftSize`. */
|
|
83
|
-
get frequencyBinCount() {
|
|
84
|
-
return this._options.fftSize >> 1;
|
|
85
|
-
}
|
|
86
|
-
get smoothingTimeConstant() {
|
|
87
|
-
return this._analyser?.smoothingTimeConstant ?? this._options.smoothingTimeConstant;
|
|
88
|
-
}
|
|
89
|
-
set smoothingTimeConstant(v) {
|
|
90
|
-
this._options.smoothingTimeConstant = v;
|
|
91
|
-
if (this._analyser) this._analyser.smoothingTimeConstant = v;
|
|
92
|
-
}
|
|
93
|
-
get minDecibels() {
|
|
94
|
-
return this._analyser?.minDecibels ?? this._options.minDecibels;
|
|
95
|
-
}
|
|
96
|
-
set minDecibels(v) {
|
|
97
|
-
this._options.minDecibels = v;
|
|
98
|
-
if (this._analyser) this._analyser.minDecibels = v;
|
|
99
|
-
}
|
|
100
|
-
get maxDecibels() {
|
|
101
|
-
return this._analyser?.maxDecibels ?? this._options.maxDecibels;
|
|
102
|
-
}
|
|
103
|
-
set maxDecibels(v) {
|
|
104
|
-
this._options.maxDecibels = v;
|
|
105
|
-
if (this._analyser) this._analyser.maxDecibels = v;
|
|
106
|
-
}
|
|
107
|
-
/** Fill and return the byte frequency spectrum (0..255 per bin). */
|
|
108
|
-
getSpectrum(into) {
|
|
109
|
-
const buf = into ?? this._byteSpectrum;
|
|
110
|
-
if (this._analyser) this._analyser.getByteFrequencyData(buf);
|
|
111
|
-
else buf.fill(0);
|
|
112
|
-
return buf;
|
|
113
|
-
}
|
|
114
|
-
/** Fill and return the float frequency spectrum (dB per bin). */
|
|
115
|
-
getSpectrumFloat(into) {
|
|
116
|
-
const buf = into ?? this._floatSpectrum;
|
|
117
|
-
if (this._analyser) this._analyser.getFloatFrequencyData(buf);
|
|
118
|
-
else buf.fill(0);
|
|
119
|
-
return buf;
|
|
120
|
-
}
|
|
121
|
-
/** Fill and return the byte time-domain waveform (0..255 per sample). */
|
|
122
|
-
getWaveform(into) {
|
|
123
|
-
const buf = into ?? this._byteWaveform;
|
|
124
|
-
if (this._analyser) this._analyser.getByteTimeDomainData(buf);
|
|
125
|
-
else buf.fill(0);
|
|
126
|
-
return buf;
|
|
127
|
-
}
|
|
128
|
-
/** Fill and return the float time-domain waveform (-1..1 per sample). */
|
|
129
|
-
getWaveformFloat(into) {
|
|
130
|
-
const buf = into ?? this._floatWaveform;
|
|
131
|
-
if (this._analyser) this._analyser.getFloatTimeDomainData(buf);
|
|
132
|
-
else buf.fill(0);
|
|
133
|
-
return buf;
|
|
134
|
-
}
|
|
135
|
-
/**
|
|
136
|
-
* Return the mean energy of frequency bins in the given Hz range, normalised 0..1.
|
|
137
|
-
* Uses the byte spectrum (0..255), normalised by dividing by 255.
|
|
138
|
-
*/
|
|
139
|
-
getBandEnergy(fromHz, toHz) {
|
|
140
|
-
if (!this._analyser) return 0;
|
|
141
|
-
const nyquist = getAudioContext().sampleRate / 2;
|
|
142
|
-
const binCount = this.frequencyBinCount;
|
|
143
|
-
const fromBin = Math.max(0, Math.min(binCount - 1, Math.round(fromHz / nyquist * binCount)));
|
|
144
|
-
const toBin = Math.max(0, Math.min(binCount - 1, Math.round(toHz / nyquist * binCount)));
|
|
145
|
-
const lo = Math.min(fromBin, toBin);
|
|
146
|
-
const hi = Math.max(fromBin, toBin);
|
|
147
|
-
const spectrum = this.getSpectrum();
|
|
148
|
-
let sum = 0;
|
|
149
|
-
const count = hi - lo + 1;
|
|
150
|
-
for (let i = lo; i <= hi; i++) sum += spectrum[i];
|
|
151
|
-
return sum / (count * 255);
|
|
152
|
-
}
|
|
153
|
-
/** Return low (0-250 Hz), mid (250-2 kHz), and high (2 k-20 kHz) band energies (0..1 each). */
|
|
154
|
-
getLowMidHigh() {
|
|
155
|
-
return {
|
|
156
|
-
low: this.getBandEnergy(0, 250),
|
|
157
|
-
mid: this.getBandEnergy(250, 2e3),
|
|
158
|
-
high: this.getBandEnergy(2e3, 2e4)
|
|
159
|
-
};
|
|
160
|
-
}
|
|
161
|
-
/** Return overall RMS energy across all bins, normalised 0..1. */
|
|
162
|
-
getRms() {
|
|
163
|
-
if (!this._analyser) return 0;
|
|
164
|
-
const nyquist = getAudioContext().sampleRate / 2;
|
|
165
|
-
return this.getBandEnergy(0, nyquist);
|
|
166
|
-
}
|
|
167
|
-
/**
|
|
168
|
-
* Mel-scaled spectrum, byte domain (0..255). Output length = `bands`.
|
|
169
|
-
* Each output bin is a triangular-weighted sum of the linear FFT bins
|
|
170
|
-
* underneath it, mel-spaced from `fMin` to `fMax`.
|
|
171
|
-
*/
|
|
172
|
-
getSpectrumMel(into, options) {
|
|
173
|
-
const bandCount = options?.bands ?? 32;
|
|
174
|
-
const out = into ?? new Uint8Array(bandCount);
|
|
175
|
-
const filterbank = this._getMelFilterbank(bandCount, options?.fMin ?? 20, options?.fMax ?? 2e4);
|
|
176
|
-
if (!this._analyser || filterbank === null) {
|
|
177
|
-
out.fill(0);
|
|
178
|
-
return out;
|
|
179
|
-
}
|
|
180
|
-
this._analyser.getByteFrequencyData(this._byteSpectrum);
|
|
181
|
-
applyFilterbank(this._byteSpectrum, filterbank, out);
|
|
182
|
-
return out;
|
|
183
|
-
}
|
|
184
|
-
/** Mel-scaled spectrum, float domain (dBFS values). Output length = `bands`. */
|
|
185
|
-
getSpectrumMelFloat(into, options) {
|
|
186
|
-
const bandCount = options?.bands ?? 32;
|
|
187
|
-
const out = into ?? new Float32Array(bandCount);
|
|
188
|
-
const filterbank = this._getMelFilterbank(bandCount, options?.fMin ?? 20, options?.fMax ?? 2e4);
|
|
189
|
-
if (!this._analyser || filterbank === null) {
|
|
190
|
-
out.fill(0);
|
|
191
|
-
return out;
|
|
192
|
-
}
|
|
193
|
-
this._analyser.getFloatFrequencyData(this._floatSpectrum);
|
|
194
|
-
applyFilterbank(this._floatSpectrum, filterbank, out);
|
|
195
|
-
return out;
|
|
196
|
-
}
|
|
197
|
-
/**
|
|
198
|
-
* Log-scaled spectrum, byte domain (0..255). Output length = `bands`.
|
|
199
|
-
* Bins are spaced so each output covers an equal fraction of the
|
|
200
|
-
* `log2(fMax / fMin)` octave range - useful when you want a
|
|
201
|
-
* frequency-axis visualization where each octave gets the same width.
|
|
202
|
-
*/
|
|
203
|
-
getSpectrumLog(into, options) {
|
|
204
|
-
const bandCount = options?.bands ?? 32;
|
|
205
|
-
const out = into ?? new Uint8Array(bandCount);
|
|
206
|
-
const ranges = this._getLogRanges(bandCount, options?.fMin ?? 20, options?.fMax ?? 2e4);
|
|
207
|
-
if (!this._analyser || ranges === null) {
|
|
208
|
-
out.fill(0);
|
|
209
|
-
return out;
|
|
210
|
-
}
|
|
211
|
-
this._analyser.getByteFrequencyData(this._byteSpectrum);
|
|
212
|
-
applyLogRanges(this._byteSpectrum, ranges, out);
|
|
213
|
-
return out;
|
|
214
|
-
}
|
|
215
|
-
/** Log-scaled spectrum, float domain (dBFS values). Output length = `bands`. */
|
|
216
|
-
getSpectrumLogFloat(into, options) {
|
|
217
|
-
const bandCount = options?.bands ?? 32;
|
|
218
|
-
const out = into ?? new Float32Array(bandCount);
|
|
219
|
-
const ranges = this._getLogRanges(bandCount, options?.fMin ?? 20, options?.fMax ?? 2e4);
|
|
220
|
-
if (!this._analyser || ranges === null) {
|
|
221
|
-
out.fill(0);
|
|
222
|
-
return out;
|
|
223
|
-
}
|
|
224
|
-
this._analyser.getFloatFrequencyData(this._floatSpectrum);
|
|
225
|
-
applyLogRanges(this._floatSpectrum, ranges, out);
|
|
226
|
-
return out;
|
|
227
|
-
}
|
|
228
|
-
/**
|
|
229
|
-
* Disconnect the analyser tap, release the internal `AnalyserNode`, and
|
|
230
|
-
* cancel any pending setup callbacks. Call when the analyser is no longer
|
|
231
|
-
* needed to avoid memory leaks.
|
|
232
|
-
*/
|
|
233
|
-
destroy() {
|
|
234
|
-
onAudioContextReady.remove(this._onAudioContextReady);
|
|
235
|
-
if (this._pendingSourceSetup !== null) {
|
|
236
|
-
onAudioContextReady.remove(this._pendingSourceSetup);
|
|
237
|
-
this._pendingSourceSetup = null;
|
|
238
|
-
}
|
|
239
|
-
this._disconnectTap();
|
|
240
|
-
this._analyser?.disconnect();
|
|
241
|
-
this._analyser = null;
|
|
242
|
-
this._source = null;
|
|
243
|
-
}
|
|
244
|
-
_getMelFilterbank(bands, fMin, fMax) {
|
|
245
|
-
if (!this._analyser) return null;
|
|
246
|
-
const ctx = getAudioContext();
|
|
247
|
-
const fftSize = this._options.fftSize;
|
|
248
|
-
const clampedFmax = Math.min(fMax, ctx.sampleRate / 2);
|
|
249
|
-
const key = `${bands}|${fMin}|${clampedFmax}|${fftSize}`;
|
|
250
|
-
let cached = this._melCache.get(key);
|
|
251
|
-
if (cached === void 0) {
|
|
252
|
-
cached = buildMelFilterbank(bands, fMin, clampedFmax, fftSize, ctx.sampleRate);
|
|
253
|
-
this._melCache.set(key, cached);
|
|
254
|
-
}
|
|
255
|
-
return cached;
|
|
256
|
-
}
|
|
257
|
-
_getLogRanges(bands, fMin, fMax) {
|
|
258
|
-
if (!this._analyser) return null;
|
|
259
|
-
const ctx = getAudioContext();
|
|
260
|
-
const fftSize = this._options.fftSize;
|
|
261
|
-
const clampedFmax = Math.min(fMax, ctx.sampleRate / 2);
|
|
262
|
-
const key = `${bands}|${fMin}|${clampedFmax}|${fftSize}`;
|
|
263
|
-
let cached = this._logCache.get(key);
|
|
264
|
-
if (cached === void 0) {
|
|
265
|
-
cached = buildLogRanges(bands, fMin, clampedFmax, fftSize, ctx.sampleRate);
|
|
266
|
-
this._logCache.set(key, cached);
|
|
267
|
-
}
|
|
268
|
-
return cached;
|
|
269
|
-
}
|
|
270
|
-
_setupAnalyser(audioContext) {
|
|
271
|
-
const node = audioContext.createAnalyser();
|
|
272
|
-
node.fftSize = this._options.fftSize;
|
|
273
|
-
node.minDecibels = this._options.minDecibels;
|
|
274
|
-
node.maxDecibels = this._options.maxDecibels;
|
|
275
|
-
node.smoothingTimeConstant = this._options.smoothingTimeConstant;
|
|
276
|
-
this._analyser = node;
|
|
277
|
-
if (this._source !== null) this._connectSource(this._source, audioContext);
|
|
278
|
-
}
|
|
279
|
-
_connectSource(source, audioContext) {
|
|
280
|
-
if (!this._analyser) return;
|
|
281
|
-
const tap = this._resolveToAudioNode(source, audioContext);
|
|
282
|
-
if (!tap) {
|
|
283
|
-
this._deferConnectionViaBus(source);
|
|
284
|
-
return;
|
|
285
|
-
}
|
|
286
|
-
this._tapSource = tap;
|
|
287
|
-
tap.connect(this._analyser);
|
|
288
|
-
}
|
|
289
|
-
_resolveToAudioNode(source, audioContext) {
|
|
290
|
-
if (source === null) return null;
|
|
291
|
-
if (typeof source.getTracks === "function") {
|
|
292
|
-
if (this._streamSource) {
|
|
293
|
-
this._streamSource.disconnect();
|
|
294
|
-
this._streamSource = null;
|
|
295
|
-
}
|
|
296
|
-
const msNode = audioContext.createMediaStreamSource(source);
|
|
297
|
-
this._streamSource = msNode;
|
|
298
|
-
return msNode;
|
|
299
|
-
}
|
|
300
|
-
const asBus = source;
|
|
301
|
-
if (typeof asBus.getOutputNode === "function") return asBus.getOutputNode();
|
|
302
|
-
const asVoice = source;
|
|
303
|
-
if ("output" in asVoice && asVoice.output) return asVoice.output;
|
|
304
|
-
const asNode = source;
|
|
305
|
-
if (typeof asNode.connect === "function" && typeof asNode.disconnect === "function") return source;
|
|
306
|
-
return null;
|
|
307
|
-
}
|
|
308
|
-
_deferConnectionViaBus(source) {
|
|
309
|
-
const asBus = source;
|
|
310
|
-
if (typeof asBus.onceSetup === "function") {
|
|
311
|
-
asBus.onceSetup(() => {
|
|
312
|
-
if (this._source === source && this._analyser && isAudioContextReady()) this._connectSource(source, getAudioContext());
|
|
313
|
-
});
|
|
314
|
-
return;
|
|
315
|
-
}
|
|
316
|
-
onAudioContextReady.once(() => {
|
|
317
|
-
if (this._source === source && this._analyser && isAudioContextReady()) this._connectSource(source, getAudioContext());
|
|
318
|
-
});
|
|
319
|
-
}
|
|
320
|
-
_disconnectTap() {
|
|
321
|
-
if (this._tapSource && this._analyser) try {
|
|
322
|
-
this._tapSource.disconnect(this._analyser);
|
|
323
|
-
} catch {}
|
|
324
|
-
this._tapSource = null;
|
|
325
|
-
if (this._streamSource) {
|
|
326
|
-
this._streamSource.disconnect();
|
|
327
|
-
this._streamSource = null;
|
|
328
|
-
}
|
|
329
|
-
}
|
|
330
|
-
};
|
|
331
|
-
const buildLogRanges = (bandCount, fMin, fMax, fftSize, sampleRate) => {
|
|
332
|
-
const binCount = fftSize >> 1;
|
|
333
|
-
const nyquist = sampleRate / 2;
|
|
334
|
-
const logMin = Math.log(fMin);
|
|
335
|
-
const logMax = Math.log(fMax);
|
|
336
|
-
const ranges = [];
|
|
337
|
-
for (let b = 0; b < bandCount; b++) {
|
|
338
|
-
const lowHz = Math.exp(logMin + (logMax - logMin) * b / bandCount);
|
|
339
|
-
const highHz = Math.exp(logMin + (logMax - logMin) * (b + 1) / bandCount);
|
|
340
|
-
const startBin = Math.max(0, Math.min(binCount - 1, Math.floor(lowHz / nyquist * binCount)));
|
|
341
|
-
const endBin = Math.max(startBin, Math.min(binCount - 1, Math.ceil(highHz / nyquist * binCount) - 1));
|
|
342
|
-
ranges.push({
|
|
343
|
-
startBin,
|
|
344
|
-
endBin
|
|
345
|
-
});
|
|
346
|
-
}
|
|
347
|
-
return ranges;
|
|
348
|
-
};
|
|
349
|
-
const applyFilterbank = (spectrum, bands, out) => {
|
|
350
|
-
for (let b = 0; b < bands.length; b++) {
|
|
351
|
-
const { startBin, weights } = bands[b];
|
|
352
|
-
let sum = 0;
|
|
353
|
-
for (let i = 0; i < weights.length; i++) sum += spectrum[startBin + i] * weights[i];
|
|
354
|
-
out[b] = sum;
|
|
355
|
-
}
|
|
356
|
-
};
|
|
357
|
-
const applyLogRanges = (spectrum, ranges, out) => {
|
|
358
|
-
for (let b = 0; b < ranges.length; b++) {
|
|
359
|
-
const { startBin, endBin } = ranges[b];
|
|
360
|
-
let sum = 0;
|
|
361
|
-
const count = endBin - startBin + 1;
|
|
362
|
-
for (let i = startBin; i <= endBin; i++) sum += spectrum[i];
|
|
363
|
-
out[b] = sum / count;
|
|
364
|
-
}
|
|
365
|
-
};
|
|
366
|
-
|
|
367
|
-
//#endregion
|
|
368
|
-
export { AudioAnalyser };
|
|
369
|
-
//# sourceMappingURL=AudioAnalyser.js.map
|