@dawcore/spectrogram 0.0.1
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/LICENSE.md +21 -0
- package/README.md +15 -0
- package/dist/index.d.mts +242 -0
- package/dist/index.d.ts +242 -0
- package/dist/index.js +3916 -0
- package/dist/index.js.map +1 -0
- package/dist/index.mjs +3869 -0
- package/dist/index.mjs.map +1 -0
- package/dist/orchestrator/index.d.mts +173 -0
- package/dist/orchestrator/index.mjs +3615 -0
- package/dist/orchestrator/index.mjs.map +1 -0
- package/dist/worker/spectrogram.worker.mjs +606 -0
- package/dist/worker/spectrogram.worker.mjs.map +1 -0
- package/package.json +60 -0
package/LICENSE.md
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2015 Naomi
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
# @dawcore/spectrogram
|
|
2
|
+
|
|
3
|
+
Framework-agnostic spectrogram computation, worker, and viewport-aware rendering orchestrator for the dawcore family.
|
|
4
|
+
|
|
5
|
+
Used by `@dawcore/components` (the Lit web component layer) and `@waveform-playlist/spectrogram` (the React Provider).
|
|
6
|
+
|
|
7
|
+
## Exports
|
|
8
|
+
|
|
9
|
+
- `computeSpectrogram`, `getColorMap`, `getFrequencyScale` — pure computation
|
|
10
|
+
- `createSpectrogramWorker`, `createSpectrogramWorkerPool` — worker factories
|
|
11
|
+
- `SpectrogramOrchestrator` (via `./orchestrator` subpath) — viewport/abort/tier/grouping logic
|
|
12
|
+
|
|
13
|
+
## License
|
|
14
|
+
|
|
15
|
+
MIT
|
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,242 @@
|
|
|
1
|
+
import { SpectrogramConfig, SpectrogramData, ColorMapValue } from '@waveform-playlist/core';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Compute spectrogram data from an AudioBuffer.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Compute spectrogram for a single channel of audio.
|
|
9
|
+
*
|
|
10
|
+
* @param audioBuffer - Source audio buffer
|
|
11
|
+
* @param config - Spectrogram configuration
|
|
12
|
+
* @param offsetSamples - Start offset into the audio buffer
|
|
13
|
+
* @param durationSamples - Number of samples to process
|
|
14
|
+
* @param channel - Channel index (0 = left, 1 = right). Default: 0
|
|
15
|
+
*/
|
|
16
|
+
declare function computeSpectrogram(audioBuffer: AudioBuffer, config?: SpectrogramConfig, offsetSamples?: number, durationSamples?: number, channel?: number): SpectrogramData;
|
|
17
|
+
/**
|
|
18
|
+
* Compute a mono (mixed-down) spectrogram from all channels.
|
|
19
|
+
*/
|
|
20
|
+
declare function computeSpectrogramMono(audioBuffer: AudioBuffer, config?: SpectrogramConfig, offsetSamples?: number, durationSamples?: number): SpectrogramData;
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Color maps for spectrogram rendering.
|
|
24
|
+
* Each map is a 256-entry RGB lookup table (768 bytes).
|
|
25
|
+
*
|
|
26
|
+
* Viridis, Magma, Inferno: Perceptually uniform colormaps from matplotlib.
|
|
27
|
+
* By Stéfan van der Walt, Nathaniel Smith, and Eric Firing. Released under CC0.
|
|
28
|
+
* Data from https://github.com/BIDS/colormap
|
|
29
|
+
*
|
|
30
|
+
* Roseus: Perceptually uniform colormap from https://github.com/dofuuz/roseus
|
|
31
|
+
* By dofuuz, licensed under CC0 1.0 Universal.
|
|
32
|
+
*/
|
|
33
|
+
|
|
34
|
+
type ColorLUT = Uint8Array;
|
|
35
|
+
/**
|
|
36
|
+
* Get a 256-entry RGB LUT for the given color map.
|
|
37
|
+
*/
|
|
38
|
+
declare function getColorMap(value: ColorMapValue): ColorLUT;
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* Frequency scale mapping functions.
|
|
42
|
+
* Each maps a frequency (Hz) to a normalized position [0, 1].
|
|
43
|
+
*/
|
|
44
|
+
type FrequencyScaleName = 'linear' | 'logarithmic' | 'mel' | 'bark' | 'erb';
|
|
45
|
+
/**
|
|
46
|
+
* Returns a mapping function: (frequencyHz, minFrequency, maxFrequency) → [0, 1]
|
|
47
|
+
*/
|
|
48
|
+
declare function getFrequencyScale(name: FrequencyScaleName): (f: number, minF: number, maxF: number) => number;
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* Error thrown when a spectrogram computation is aborted due to a generation change.
|
|
52
|
+
* Use `instanceof SpectrogramAbortError` instead of string matching on error messages.
|
|
53
|
+
*/
|
|
54
|
+
declare class SpectrogramAbortError extends Error {
|
|
55
|
+
constructor();
|
|
56
|
+
}
|
|
57
|
+
interface SpectrogramWorkerFFTParams {
|
|
58
|
+
clipId: string;
|
|
59
|
+
channelDataArrays: Float32Array[];
|
|
60
|
+
config: SpectrogramConfig;
|
|
61
|
+
sampleRate: number;
|
|
62
|
+
offsetSamples: number;
|
|
63
|
+
durationSamples: number;
|
|
64
|
+
mono: boolean;
|
|
65
|
+
sampleRange?: {
|
|
66
|
+
start: number;
|
|
67
|
+
end: number;
|
|
68
|
+
};
|
|
69
|
+
/** If set, compute only this channel index (used by worker pool). */
|
|
70
|
+
channelFilter?: number;
|
|
71
|
+
}
|
|
72
|
+
interface SpectrogramWorkerRenderChunksParams {
|
|
73
|
+
cacheKey: string;
|
|
74
|
+
canvasIds: string[];
|
|
75
|
+
canvasWidths: number[];
|
|
76
|
+
globalPixelOffsets: number[];
|
|
77
|
+
canvasHeight: number;
|
|
78
|
+
devicePixelRatio: number;
|
|
79
|
+
samplesPerPixel: number;
|
|
80
|
+
colorLUT: Uint8Array;
|
|
81
|
+
frequencyScale: string;
|
|
82
|
+
minFrequency: number;
|
|
83
|
+
maxFrequency: number;
|
|
84
|
+
gainDb: number;
|
|
85
|
+
rangeDb: number;
|
|
86
|
+
channelIndex: number;
|
|
87
|
+
}
|
|
88
|
+
interface SpectrogramWorkerApi {
|
|
89
|
+
computeFFT(params: SpectrogramWorkerFFTParams, generation?: number): Promise<{
|
|
90
|
+
cacheKey: string;
|
|
91
|
+
}>;
|
|
92
|
+
renderChunks(params: SpectrogramWorkerRenderChunksParams, generation?: number): Promise<void>;
|
|
93
|
+
abortGeneration(generation: number): void;
|
|
94
|
+
registerCanvas(canvasId: string, canvas: OffscreenCanvas): void;
|
|
95
|
+
unregisterCanvas(canvasId: string): void;
|
|
96
|
+
registerAudioData(clipId: string, channelDataArrays: Float32Array[], sampleRate: number): void;
|
|
97
|
+
unregisterAudioData(clipId: string): void;
|
|
98
|
+
terminate(): void;
|
|
99
|
+
}
|
|
100
|
+
/**
|
|
101
|
+
* Wraps a Web Worker running `spectrogram.worker.ts` with a promise-based API.
|
|
102
|
+
*
|
|
103
|
+
* The caller is responsible for creating the Worker, e.g.:
|
|
104
|
+
* ```ts
|
|
105
|
+
* const worker = new Worker(
|
|
106
|
+
* new URL('@dawcore/spectrogram/worker/spectrogram.worker', import.meta.url),
|
|
107
|
+
* { type: 'module' }
|
|
108
|
+
* );
|
|
109
|
+
* const api = createSpectrogramWorker(worker);
|
|
110
|
+
* ```
|
|
111
|
+
*/
|
|
112
|
+
declare function createSpectrogramWorker(worker: Worker): SpectrogramWorkerApi;
|
|
113
|
+
|
|
114
|
+
declare function createSpectrogramWorkerPool(createWorker: () => Worker, poolSize?: number): SpectrogramWorkerApi;
|
|
115
|
+
|
|
116
|
+
/**
|
|
117
|
+
* Cache of color LUTs (Uint8Array of length 768 = 256 RGB).
|
|
118
|
+
*
|
|
119
|
+
* `getColorMap` already returns shared module-level constants for the named
|
|
120
|
+
* maps, but rebuilds an interpolated LUT every call for custom
|
|
121
|
+
* `ColorMapEntry[]` stops. This cache memoizes by stringified key so the
|
|
122
|
+
* orchestrator does not re-interpolate per render.
|
|
123
|
+
*
|
|
124
|
+
* Worst case ~8 entries × 768B = ~6KB total — no eviction needed.
|
|
125
|
+
*/
|
|
126
|
+
declare class ColorLUTCache {
|
|
127
|
+
private cache;
|
|
128
|
+
get(colorMap: ColorMapValue): Uint8Array;
|
|
129
|
+
clear(): void;
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
interface CanvasMeta {
|
|
133
|
+
canvasId: string;
|
|
134
|
+
globalPixelOffset: number;
|
|
135
|
+
widthPx: number;
|
|
136
|
+
}
|
|
137
|
+
interface ViewportBounds {
|
|
138
|
+
visibleStartPx: number;
|
|
139
|
+
visibleEndPx: number;
|
|
140
|
+
bufferStartPx: number;
|
|
141
|
+
bufferEndPx: number;
|
|
142
|
+
}
|
|
143
|
+
interface ClassifiedTiers<T extends CanvasMeta> {
|
|
144
|
+
viewport: T[];
|
|
145
|
+
buffer: T[];
|
|
146
|
+
remaining: T[];
|
|
147
|
+
}
|
|
148
|
+
declare function classifyViewport<T extends CanvasMeta>(canvases: T[], bounds: ViewportBounds): ClassifiedTiers<T>;
|
|
149
|
+
|
|
150
|
+
interface SpectrogramOrchestratorOptions {
|
|
151
|
+
workerFactory: () => Worker;
|
|
152
|
+
workerPoolSize?: number;
|
|
153
|
+
config: SpectrogramConfig;
|
|
154
|
+
colorMap?: ColorMapValue;
|
|
155
|
+
devicePixelRatio?: number;
|
|
156
|
+
}
|
|
157
|
+
interface ClipRegistration {
|
|
158
|
+
clipId: string;
|
|
159
|
+
trackId: string;
|
|
160
|
+
channelData: Float32Array[];
|
|
161
|
+
sampleRate: number;
|
|
162
|
+
durationSamples: number;
|
|
163
|
+
offsetSamples: number;
|
|
164
|
+
}
|
|
165
|
+
interface CanvasRegistration {
|
|
166
|
+
canvasId: string;
|
|
167
|
+
canvas: OffscreenCanvas;
|
|
168
|
+
clipId: string;
|
|
169
|
+
trackId: string;
|
|
170
|
+
channelIndex: number;
|
|
171
|
+
chunkIndex: number;
|
|
172
|
+
globalPixelOffset: number;
|
|
173
|
+
widthPx: number;
|
|
174
|
+
heightPx: number;
|
|
175
|
+
}
|
|
176
|
+
interface ViewportState extends ViewportBounds {
|
|
177
|
+
samplesPerPixel: number;
|
|
178
|
+
}
|
|
179
|
+
interface ClipEntry {
|
|
180
|
+
trackId: string;
|
|
181
|
+
channelData: Float32Array[];
|
|
182
|
+
sampleRate: number;
|
|
183
|
+
durationSamples: number;
|
|
184
|
+
offsetSamples: number;
|
|
185
|
+
}
|
|
186
|
+
interface CanvasEntry extends CanvasMeta {
|
|
187
|
+
clipId: string;
|
|
188
|
+
trackId: string;
|
|
189
|
+
channelIndex: number;
|
|
190
|
+
chunkIndex: number;
|
|
191
|
+
heightPx: number;
|
|
192
|
+
}
|
|
193
|
+
declare class SpectrogramOrchestrator extends EventTarget {
|
|
194
|
+
protected pool: SpectrogramWorkerApi;
|
|
195
|
+
protected config: SpectrogramConfig;
|
|
196
|
+
protected colorMap: ColorMapValue;
|
|
197
|
+
protected devicePixelRatio: number;
|
|
198
|
+
protected clips: Map<string, ClipEntry>;
|
|
199
|
+
protected canvases: Map<string, CanvasEntry>;
|
|
200
|
+
protected viewport: ViewportState | null;
|
|
201
|
+
protected generation: number;
|
|
202
|
+
protected colorLUT: ColorLUTCache;
|
|
203
|
+
protected disposed: boolean;
|
|
204
|
+
protected renderInFlight: boolean;
|
|
205
|
+
constructor(opts: SpectrogramOrchestratorOptions);
|
|
206
|
+
registerClip(reg: ClipRegistration): void;
|
|
207
|
+
unregisterClip(clipId: string): void;
|
|
208
|
+
registerCanvas(reg: CanvasRegistration): void;
|
|
209
|
+
unregisterCanvas(canvasId: string): void;
|
|
210
|
+
setViewport(state: ViewportState): void;
|
|
211
|
+
setConfig(config: SpectrogramConfig): void;
|
|
212
|
+
setColorMap(colorMap: ColorMapValue): void;
|
|
213
|
+
setDevicePixelRatio(dpr: number): void;
|
|
214
|
+
dispose(): void;
|
|
215
|
+
protected scheduleRender(): void;
|
|
216
|
+
protected runRender(generation: number): Promise<void>;
|
|
217
|
+
protected renderTier(canvases: CanvasEntry[], generation: number, viewport: ViewportState): Promise<void>;
|
|
218
|
+
protected renderGroup(group: CanvasEntry[], generation: number, viewport: ViewportState): Promise<void>;
|
|
219
|
+
protected renderRemainingViaIdle(canvases: CanvasEntry[], generation: number, viewport: ViewportState): Promise<void>;
|
|
220
|
+
protected yieldUntilIdle(): Promise<void>;
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
interface ViewportReadyDetail {
|
|
224
|
+
trackId: string;
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
interface ChunkLike {
|
|
228
|
+
chunkIndex: number;
|
|
229
|
+
}
|
|
230
|
+
/**
|
|
231
|
+
* Group a list of chunks into runs of contiguous chunk indices.
|
|
232
|
+
*
|
|
233
|
+
* Sorts input first (input may be non-monotonic from viewport classification).
|
|
234
|
+
* Returns groups in ascending chunk-index order.
|
|
235
|
+
*
|
|
236
|
+
* Without this, computing FFT for chunks [10, 14, 15, 11] would compute a
|
|
237
|
+
* single FFT spanning chunks 10–15 (~96K frames / 4.5s of audio) instead of
|
|
238
|
+
* two smaller FFTs (10–11, 14–15) totaling ~32K frames.
|
|
239
|
+
*/
|
|
240
|
+
declare function groupContiguousChunks<T extends ChunkLike>(chunks: T[]): T[][];
|
|
241
|
+
|
|
242
|
+
export { type CanvasMeta, type CanvasRegistration, type ChunkLike, type ClassifiedTiers, type ClipRegistration, ColorLUTCache, type FrequencyScaleName, SpectrogramAbortError, SpectrogramOrchestrator, type SpectrogramOrchestratorOptions, type SpectrogramWorkerApi, type SpectrogramWorkerFFTParams, type SpectrogramWorkerRenderChunksParams, type ViewportBounds, type ViewportReadyDetail, type ViewportState, classifyViewport, computeSpectrogram, computeSpectrogramMono, createSpectrogramWorker, createSpectrogramWorkerPool, getColorMap, getFrequencyScale, groupContiguousChunks };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,242 @@
|
|
|
1
|
+
import { SpectrogramConfig, SpectrogramData, ColorMapValue } from '@waveform-playlist/core';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Compute spectrogram data from an AudioBuffer.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Compute spectrogram for a single channel of audio.
|
|
9
|
+
*
|
|
10
|
+
* @param audioBuffer - Source audio buffer
|
|
11
|
+
* @param config - Spectrogram configuration
|
|
12
|
+
* @param offsetSamples - Start offset into the audio buffer
|
|
13
|
+
* @param durationSamples - Number of samples to process
|
|
14
|
+
* @param channel - Channel index (0 = left, 1 = right). Default: 0
|
|
15
|
+
*/
|
|
16
|
+
declare function computeSpectrogram(audioBuffer: AudioBuffer, config?: SpectrogramConfig, offsetSamples?: number, durationSamples?: number, channel?: number): SpectrogramData;
|
|
17
|
+
/**
|
|
18
|
+
* Compute a mono (mixed-down) spectrogram from all channels.
|
|
19
|
+
*/
|
|
20
|
+
declare function computeSpectrogramMono(audioBuffer: AudioBuffer, config?: SpectrogramConfig, offsetSamples?: number, durationSamples?: number): SpectrogramData;
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Color maps for spectrogram rendering.
|
|
24
|
+
* Each map is a 256-entry RGB lookup table (768 bytes).
|
|
25
|
+
*
|
|
26
|
+
* Viridis, Magma, Inferno: Perceptually uniform colormaps from matplotlib.
|
|
27
|
+
* By Stéfan van der Walt, Nathaniel Smith, and Eric Firing. Released under CC0.
|
|
28
|
+
* Data from https://github.com/BIDS/colormap
|
|
29
|
+
*
|
|
30
|
+
* Roseus: Perceptually uniform colormap from https://github.com/dofuuz/roseus
|
|
31
|
+
* By dofuuz, licensed under CC0 1.0 Universal.
|
|
32
|
+
*/
|
|
33
|
+
|
|
34
|
+
type ColorLUT = Uint8Array;
|
|
35
|
+
/**
|
|
36
|
+
* Get a 256-entry RGB LUT for the given color map.
|
|
37
|
+
*/
|
|
38
|
+
declare function getColorMap(value: ColorMapValue): ColorLUT;
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* Frequency scale mapping functions.
|
|
42
|
+
* Each maps a frequency (Hz) to a normalized position [0, 1].
|
|
43
|
+
*/
|
|
44
|
+
type FrequencyScaleName = 'linear' | 'logarithmic' | 'mel' | 'bark' | 'erb';
|
|
45
|
+
/**
|
|
46
|
+
* Returns a mapping function: (frequencyHz, minFrequency, maxFrequency) → [0, 1]
|
|
47
|
+
*/
|
|
48
|
+
declare function getFrequencyScale(name: FrequencyScaleName): (f: number, minF: number, maxF: number) => number;
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* Error thrown when a spectrogram computation is aborted due to a generation change.
|
|
52
|
+
* Use `instanceof SpectrogramAbortError` instead of string matching on error messages.
|
|
53
|
+
*/
|
|
54
|
+
declare class SpectrogramAbortError extends Error {
|
|
55
|
+
constructor();
|
|
56
|
+
}
|
|
57
|
+
interface SpectrogramWorkerFFTParams {
|
|
58
|
+
clipId: string;
|
|
59
|
+
channelDataArrays: Float32Array[];
|
|
60
|
+
config: SpectrogramConfig;
|
|
61
|
+
sampleRate: number;
|
|
62
|
+
offsetSamples: number;
|
|
63
|
+
durationSamples: number;
|
|
64
|
+
mono: boolean;
|
|
65
|
+
sampleRange?: {
|
|
66
|
+
start: number;
|
|
67
|
+
end: number;
|
|
68
|
+
};
|
|
69
|
+
/** If set, compute only this channel index (used by worker pool). */
|
|
70
|
+
channelFilter?: number;
|
|
71
|
+
}
|
|
72
|
+
interface SpectrogramWorkerRenderChunksParams {
|
|
73
|
+
cacheKey: string;
|
|
74
|
+
canvasIds: string[];
|
|
75
|
+
canvasWidths: number[];
|
|
76
|
+
globalPixelOffsets: number[];
|
|
77
|
+
canvasHeight: number;
|
|
78
|
+
devicePixelRatio: number;
|
|
79
|
+
samplesPerPixel: number;
|
|
80
|
+
colorLUT: Uint8Array;
|
|
81
|
+
frequencyScale: string;
|
|
82
|
+
minFrequency: number;
|
|
83
|
+
maxFrequency: number;
|
|
84
|
+
gainDb: number;
|
|
85
|
+
rangeDb: number;
|
|
86
|
+
channelIndex: number;
|
|
87
|
+
}
|
|
88
|
+
interface SpectrogramWorkerApi {
|
|
89
|
+
computeFFT(params: SpectrogramWorkerFFTParams, generation?: number): Promise<{
|
|
90
|
+
cacheKey: string;
|
|
91
|
+
}>;
|
|
92
|
+
renderChunks(params: SpectrogramWorkerRenderChunksParams, generation?: number): Promise<void>;
|
|
93
|
+
abortGeneration(generation: number): void;
|
|
94
|
+
registerCanvas(canvasId: string, canvas: OffscreenCanvas): void;
|
|
95
|
+
unregisterCanvas(canvasId: string): void;
|
|
96
|
+
registerAudioData(clipId: string, channelDataArrays: Float32Array[], sampleRate: number): void;
|
|
97
|
+
unregisterAudioData(clipId: string): void;
|
|
98
|
+
terminate(): void;
|
|
99
|
+
}
|
|
100
|
+
/**
|
|
101
|
+
* Wraps a Web Worker running `spectrogram.worker.ts` with a promise-based API.
|
|
102
|
+
*
|
|
103
|
+
* The caller is responsible for creating the Worker, e.g.:
|
|
104
|
+
* ```ts
|
|
105
|
+
* const worker = new Worker(
|
|
106
|
+
* new URL('@dawcore/spectrogram/worker/spectrogram.worker', import.meta.url),
|
|
107
|
+
* { type: 'module' }
|
|
108
|
+
* );
|
|
109
|
+
* const api = createSpectrogramWorker(worker);
|
|
110
|
+
* ```
|
|
111
|
+
*/
|
|
112
|
+
declare function createSpectrogramWorker(worker: Worker): SpectrogramWorkerApi;
|
|
113
|
+
|
|
114
|
+
declare function createSpectrogramWorkerPool(createWorker: () => Worker, poolSize?: number): SpectrogramWorkerApi;
|
|
115
|
+
|
|
116
|
+
/**
|
|
117
|
+
* Cache of color LUTs (Uint8Array of length 768 = 256 RGB).
|
|
118
|
+
*
|
|
119
|
+
* `getColorMap` already returns shared module-level constants for the named
|
|
120
|
+
* maps, but rebuilds an interpolated LUT every call for custom
|
|
121
|
+
* `ColorMapEntry[]` stops. This cache memoizes by stringified key so the
|
|
122
|
+
* orchestrator does not re-interpolate per render.
|
|
123
|
+
*
|
|
124
|
+
* Worst case ~8 entries × 768B = ~6KB total — no eviction needed.
|
|
125
|
+
*/
|
|
126
|
+
declare class ColorLUTCache {
|
|
127
|
+
private cache;
|
|
128
|
+
get(colorMap: ColorMapValue): Uint8Array;
|
|
129
|
+
clear(): void;
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
interface CanvasMeta {
|
|
133
|
+
canvasId: string;
|
|
134
|
+
globalPixelOffset: number;
|
|
135
|
+
widthPx: number;
|
|
136
|
+
}
|
|
137
|
+
interface ViewportBounds {
|
|
138
|
+
visibleStartPx: number;
|
|
139
|
+
visibleEndPx: number;
|
|
140
|
+
bufferStartPx: number;
|
|
141
|
+
bufferEndPx: number;
|
|
142
|
+
}
|
|
143
|
+
interface ClassifiedTiers<T extends CanvasMeta> {
|
|
144
|
+
viewport: T[];
|
|
145
|
+
buffer: T[];
|
|
146
|
+
remaining: T[];
|
|
147
|
+
}
|
|
148
|
+
declare function classifyViewport<T extends CanvasMeta>(canvases: T[], bounds: ViewportBounds): ClassifiedTiers<T>;
|
|
149
|
+
|
|
150
|
+
interface SpectrogramOrchestratorOptions {
|
|
151
|
+
workerFactory: () => Worker;
|
|
152
|
+
workerPoolSize?: number;
|
|
153
|
+
config: SpectrogramConfig;
|
|
154
|
+
colorMap?: ColorMapValue;
|
|
155
|
+
devicePixelRatio?: number;
|
|
156
|
+
}
|
|
157
|
+
interface ClipRegistration {
|
|
158
|
+
clipId: string;
|
|
159
|
+
trackId: string;
|
|
160
|
+
channelData: Float32Array[];
|
|
161
|
+
sampleRate: number;
|
|
162
|
+
durationSamples: number;
|
|
163
|
+
offsetSamples: number;
|
|
164
|
+
}
|
|
165
|
+
interface CanvasRegistration {
|
|
166
|
+
canvasId: string;
|
|
167
|
+
canvas: OffscreenCanvas;
|
|
168
|
+
clipId: string;
|
|
169
|
+
trackId: string;
|
|
170
|
+
channelIndex: number;
|
|
171
|
+
chunkIndex: number;
|
|
172
|
+
globalPixelOffset: number;
|
|
173
|
+
widthPx: number;
|
|
174
|
+
heightPx: number;
|
|
175
|
+
}
|
|
176
|
+
interface ViewportState extends ViewportBounds {
|
|
177
|
+
samplesPerPixel: number;
|
|
178
|
+
}
|
|
179
|
+
interface ClipEntry {
|
|
180
|
+
trackId: string;
|
|
181
|
+
channelData: Float32Array[];
|
|
182
|
+
sampleRate: number;
|
|
183
|
+
durationSamples: number;
|
|
184
|
+
offsetSamples: number;
|
|
185
|
+
}
|
|
186
|
+
interface CanvasEntry extends CanvasMeta {
|
|
187
|
+
clipId: string;
|
|
188
|
+
trackId: string;
|
|
189
|
+
channelIndex: number;
|
|
190
|
+
chunkIndex: number;
|
|
191
|
+
heightPx: number;
|
|
192
|
+
}
|
|
193
|
+
declare class SpectrogramOrchestrator extends EventTarget {
|
|
194
|
+
protected pool: SpectrogramWorkerApi;
|
|
195
|
+
protected config: SpectrogramConfig;
|
|
196
|
+
protected colorMap: ColorMapValue;
|
|
197
|
+
protected devicePixelRatio: number;
|
|
198
|
+
protected clips: Map<string, ClipEntry>;
|
|
199
|
+
protected canvases: Map<string, CanvasEntry>;
|
|
200
|
+
protected viewport: ViewportState | null;
|
|
201
|
+
protected generation: number;
|
|
202
|
+
protected colorLUT: ColorLUTCache;
|
|
203
|
+
protected disposed: boolean;
|
|
204
|
+
protected renderInFlight: boolean;
|
|
205
|
+
constructor(opts: SpectrogramOrchestratorOptions);
|
|
206
|
+
registerClip(reg: ClipRegistration): void;
|
|
207
|
+
unregisterClip(clipId: string): void;
|
|
208
|
+
registerCanvas(reg: CanvasRegistration): void;
|
|
209
|
+
unregisterCanvas(canvasId: string): void;
|
|
210
|
+
setViewport(state: ViewportState): void;
|
|
211
|
+
setConfig(config: SpectrogramConfig): void;
|
|
212
|
+
setColorMap(colorMap: ColorMapValue): void;
|
|
213
|
+
setDevicePixelRatio(dpr: number): void;
|
|
214
|
+
dispose(): void;
|
|
215
|
+
protected scheduleRender(): void;
|
|
216
|
+
protected runRender(generation: number): Promise<void>;
|
|
217
|
+
protected renderTier(canvases: CanvasEntry[], generation: number, viewport: ViewportState): Promise<void>;
|
|
218
|
+
protected renderGroup(group: CanvasEntry[], generation: number, viewport: ViewportState): Promise<void>;
|
|
219
|
+
protected renderRemainingViaIdle(canvases: CanvasEntry[], generation: number, viewport: ViewportState): Promise<void>;
|
|
220
|
+
protected yieldUntilIdle(): Promise<void>;
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
interface ViewportReadyDetail {
|
|
224
|
+
trackId: string;
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
interface ChunkLike {
|
|
228
|
+
chunkIndex: number;
|
|
229
|
+
}
|
|
230
|
+
/**
|
|
231
|
+
* Group a list of chunks into runs of contiguous chunk indices.
|
|
232
|
+
*
|
|
233
|
+
* Sorts input first (input may be non-monotonic from viewport classification).
|
|
234
|
+
* Returns groups in ascending chunk-index order.
|
|
235
|
+
*
|
|
236
|
+
* Without this, computing FFT for chunks [10, 14, 15, 11] would compute a
|
|
237
|
+
* single FFT spanning chunks 10–15 (~96K frames / 4.5s of audio) instead of
|
|
238
|
+
* two smaller FFTs (10–11, 14–15) totaling ~32K frames.
|
|
239
|
+
*/
|
|
240
|
+
declare function groupContiguousChunks<T extends ChunkLike>(chunks: T[]): T[][];
|
|
241
|
+
|
|
242
|
+
export { type CanvasMeta, type CanvasRegistration, type ChunkLike, type ClassifiedTiers, type ClipRegistration, ColorLUTCache, type FrequencyScaleName, SpectrogramAbortError, SpectrogramOrchestrator, type SpectrogramOrchestratorOptions, type SpectrogramWorkerApi, type SpectrogramWorkerFFTParams, type SpectrogramWorkerRenderChunksParams, type ViewportBounds, type ViewportReadyDetail, type ViewportState, classifyViewport, computeSpectrogram, computeSpectrogramMono, createSpectrogramWorker, createSpectrogramWorkerPool, getColorMap, getFrequencyScale, groupContiguousChunks };
|