@waveform-playlist/spectrogram 5.3.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/LICENSE.md +21 -0
- package/dist/index.d.mts +161 -0
- package/dist/index.d.ts +161 -0
- package/dist/index.js +4485 -0
- package/dist/index.js.map +1 -0
- package/dist/index.mjs +4440 -0
- package/dist/index.mjs.map +1 -0
- package/dist/worker/spectrogram.worker.mjs +511 -0
- package/dist/worker/spectrogram.worker.mjs.map +1 -0
- package/package.json +62 -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/dist/index.d.mts
ADDED
|
@@ -0,0 +1,161 @@
|
|
|
1
|
+
import { SpectrogramConfig, SpectrogramData, ColorMapValue, RenderMode } from '@waveform-playlist/core';
|
|
2
|
+
import React, { ReactNode } from 'react';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Compute spectrogram data from an AudioBuffer.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Compute spectrogram for a single channel of audio.
|
|
10
|
+
*
|
|
11
|
+
* @param audioBuffer - Source audio buffer
|
|
12
|
+
* @param config - Spectrogram configuration
|
|
13
|
+
* @param offsetSamples - Start offset into the audio buffer
|
|
14
|
+
* @param durationSamples - Number of samples to process
|
|
15
|
+
* @param channel - Channel index (0 = left, 1 = right). Default: 0
|
|
16
|
+
*/
|
|
17
|
+
declare function computeSpectrogram(audioBuffer: AudioBuffer, config?: SpectrogramConfig, offsetSamples?: number, durationSamples?: number, channel?: number): SpectrogramData;
|
|
18
|
+
/**
|
|
19
|
+
* Compute a mono (mixed-down) spectrogram from all channels.
|
|
20
|
+
*/
|
|
21
|
+
declare function computeSpectrogramMono(audioBuffer: AudioBuffer, config?: SpectrogramConfig, offsetSamples?: number, durationSamples?: number): SpectrogramData;
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Color maps for spectrogram rendering.
|
|
25
|
+
* Each map is a 256-entry RGB lookup table (768 bytes).
|
|
26
|
+
*
|
|
27
|
+
* Viridis, Magma, Inferno: Perceptually uniform colormaps from matplotlib.
|
|
28
|
+
* By Stéfan van der Walt, Nathaniel Smith, and Eric Firing. Released under CC0.
|
|
29
|
+
* Data from https://github.com/BIDS/colormap
|
|
30
|
+
*
|
|
31
|
+
* Roseus: Perceptually uniform colormap from https://github.com/dofuuz/roseus
|
|
32
|
+
* By dofuuz, licensed under CC0 1.0 Universal.
|
|
33
|
+
*/
|
|
34
|
+
|
|
35
|
+
type ColorLUT = Uint8Array;
|
|
36
|
+
/**
|
|
37
|
+
* Get a 256-entry RGB LUT for the given color map.
|
|
38
|
+
*/
|
|
39
|
+
declare function getColorMap(value: ColorMapValue): ColorLUT;
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* Frequency scale mapping functions.
|
|
43
|
+
* Each maps a frequency (Hz) to a normalized position [0, 1].
|
|
44
|
+
*/
|
|
45
|
+
type FrequencyScaleName = 'linear' | 'logarithmic' | 'mel' | 'bark' | 'erb';
|
|
46
|
+
/**
|
|
47
|
+
* Returns a mapping function: (frequencyHz, minFrequency, maxFrequency) → [0, 1]
|
|
48
|
+
*/
|
|
49
|
+
declare function getFrequencyScale(name: FrequencyScaleName): (f: number, minF: number, maxF: number) => number;
|
|
50
|
+
|
|
51
|
+
interface TrackMenuItem {
|
|
52
|
+
id: string;
|
|
53
|
+
label?: string;
|
|
54
|
+
content: ReactNode;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
interface SpectrogramMenuItemsProps {
|
|
58
|
+
renderMode: RenderMode;
|
|
59
|
+
onRenderModeChange: (mode: RenderMode) => void;
|
|
60
|
+
onOpenSettings: () => void;
|
|
61
|
+
onClose?: () => void;
|
|
62
|
+
}
|
|
63
|
+
/**
|
|
64
|
+
* Returns TrackMenuItem[] for the spectrogram display mode radios and settings button.
|
|
65
|
+
*/
|
|
66
|
+
declare function SpectrogramMenuItems({ renderMode, onRenderModeChange, onOpenSettings, onClose, }: SpectrogramMenuItemsProps): TrackMenuItem[];
|
|
67
|
+
|
|
68
|
+
interface SpectrogramSettingsModalProps {
|
|
69
|
+
open: boolean;
|
|
70
|
+
onClose: () => void;
|
|
71
|
+
config: SpectrogramConfig;
|
|
72
|
+
colorMap: ColorMapValue;
|
|
73
|
+
onApply: (config: SpectrogramConfig, colorMap: ColorMapValue) => void;
|
|
74
|
+
}
|
|
75
|
+
declare const SpectrogramSettingsModal: React.FC<SpectrogramSettingsModalProps>;
|
|
76
|
+
|
|
77
|
+
interface SpectrogramWorkerComputeParams {
|
|
78
|
+
channelDataArrays: Float32Array[];
|
|
79
|
+
config: SpectrogramConfig;
|
|
80
|
+
sampleRate: number;
|
|
81
|
+
offsetSamples: number;
|
|
82
|
+
durationSamples: number;
|
|
83
|
+
mono: boolean;
|
|
84
|
+
}
|
|
85
|
+
interface SpectrogramWorkerRenderParams extends SpectrogramWorkerComputeParams {
|
|
86
|
+
render: {
|
|
87
|
+
canvasIds: string[][];
|
|
88
|
+
canvasWidths: number[];
|
|
89
|
+
canvasHeight: number;
|
|
90
|
+
devicePixelRatio: number;
|
|
91
|
+
samplesPerPixel: number;
|
|
92
|
+
colorLUT: Uint8Array;
|
|
93
|
+
frequencyScale: string;
|
|
94
|
+
minFrequency: number;
|
|
95
|
+
maxFrequency: number;
|
|
96
|
+
};
|
|
97
|
+
}
|
|
98
|
+
interface SpectrogramWorkerFFTParams {
|
|
99
|
+
clipId: string;
|
|
100
|
+
channelDataArrays: Float32Array[];
|
|
101
|
+
config: SpectrogramConfig;
|
|
102
|
+
sampleRate: number;
|
|
103
|
+
offsetSamples: number;
|
|
104
|
+
durationSamples: number;
|
|
105
|
+
mono: boolean;
|
|
106
|
+
sampleRange?: {
|
|
107
|
+
start: number;
|
|
108
|
+
end: number;
|
|
109
|
+
};
|
|
110
|
+
}
|
|
111
|
+
interface SpectrogramWorkerRenderChunksParams {
|
|
112
|
+
cacheKey: string;
|
|
113
|
+
canvasIds: string[];
|
|
114
|
+
canvasWidths: number[];
|
|
115
|
+
globalPixelOffsets: number[];
|
|
116
|
+
canvasHeight: number;
|
|
117
|
+
devicePixelRatio: number;
|
|
118
|
+
samplesPerPixel: number;
|
|
119
|
+
colorLUT: Uint8Array;
|
|
120
|
+
frequencyScale: string;
|
|
121
|
+
minFrequency: number;
|
|
122
|
+
maxFrequency: number;
|
|
123
|
+
gainDb: number;
|
|
124
|
+
rangeDb: number;
|
|
125
|
+
channelIndex: number;
|
|
126
|
+
}
|
|
127
|
+
interface SpectrogramWorkerApi {
|
|
128
|
+
compute(params: SpectrogramWorkerComputeParams): Promise<SpectrogramData[]>;
|
|
129
|
+
computeFFT(params: SpectrogramWorkerFFTParams): Promise<{
|
|
130
|
+
cacheKey: string;
|
|
131
|
+
}>;
|
|
132
|
+
renderChunks(params: SpectrogramWorkerRenderChunksParams): Promise<void>;
|
|
133
|
+
registerCanvas(canvasId: string, canvas: OffscreenCanvas): void;
|
|
134
|
+
unregisterCanvas(canvasId: string): void;
|
|
135
|
+
registerAudioData(clipId: string, channelDataArrays: Float32Array[], sampleRate: number): void;
|
|
136
|
+
unregisterAudioData(clipId: string): void;
|
|
137
|
+
computeAndRender(params: SpectrogramWorkerRenderParams): Promise<void>;
|
|
138
|
+
terminate(): void;
|
|
139
|
+
}
|
|
140
|
+
/**
|
|
141
|
+
* Wraps a Web Worker running `spectrogram.worker.ts` with a promise-based API.
|
|
142
|
+
*
|
|
143
|
+
* The caller is responsible for creating the Worker, e.g.:
|
|
144
|
+
* ```ts
|
|
145
|
+
* const worker = new Worker(
|
|
146
|
+
* new URL('@waveform-playlist/spectrogram/worker/spectrogram.worker', import.meta.url),
|
|
147
|
+
* { type: 'module' }
|
|
148
|
+
* );
|
|
149
|
+
* const api = createSpectrogramWorker(worker);
|
|
150
|
+
* ```
|
|
151
|
+
*/
|
|
152
|
+
declare function createSpectrogramWorker(worker: Worker): SpectrogramWorkerApi;
|
|
153
|
+
|
|
154
|
+
interface SpectrogramProviderProps {
|
|
155
|
+
config?: SpectrogramConfig;
|
|
156
|
+
colorMap?: ColorMapValue;
|
|
157
|
+
children: ReactNode;
|
|
158
|
+
}
|
|
159
|
+
declare const SpectrogramProvider: React.FC<SpectrogramProviderProps>;
|
|
160
|
+
|
|
161
|
+
export { type FrequencyScaleName, SpectrogramMenuItems, type SpectrogramMenuItemsProps, SpectrogramProvider, type SpectrogramProviderProps, SpectrogramSettingsModal, type SpectrogramSettingsModalProps, type SpectrogramWorkerApi, type SpectrogramWorkerRenderParams, type TrackMenuItem, computeSpectrogram, computeSpectrogramMono, createSpectrogramWorker, getColorMap, getFrequencyScale };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,161 @@
|
|
|
1
|
+
import { SpectrogramConfig, SpectrogramData, ColorMapValue, RenderMode } from '@waveform-playlist/core';
|
|
2
|
+
import React, { ReactNode } from 'react';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Compute spectrogram data from an AudioBuffer.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Compute spectrogram for a single channel of audio.
|
|
10
|
+
*
|
|
11
|
+
* @param audioBuffer - Source audio buffer
|
|
12
|
+
* @param config - Spectrogram configuration
|
|
13
|
+
* @param offsetSamples - Start offset into the audio buffer
|
|
14
|
+
* @param durationSamples - Number of samples to process
|
|
15
|
+
* @param channel - Channel index (0 = left, 1 = right). Default: 0
|
|
16
|
+
*/
|
|
17
|
+
declare function computeSpectrogram(audioBuffer: AudioBuffer, config?: SpectrogramConfig, offsetSamples?: number, durationSamples?: number, channel?: number): SpectrogramData;
|
|
18
|
+
/**
|
|
19
|
+
* Compute a mono (mixed-down) spectrogram from all channels.
|
|
20
|
+
*/
|
|
21
|
+
declare function computeSpectrogramMono(audioBuffer: AudioBuffer, config?: SpectrogramConfig, offsetSamples?: number, durationSamples?: number): SpectrogramData;
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Color maps for spectrogram rendering.
|
|
25
|
+
* Each map is a 256-entry RGB lookup table (768 bytes).
|
|
26
|
+
*
|
|
27
|
+
* Viridis, Magma, Inferno: Perceptually uniform colormaps from matplotlib.
|
|
28
|
+
* By Stéfan van der Walt, Nathaniel Smith, and Eric Firing. Released under CC0.
|
|
29
|
+
* Data from https://github.com/BIDS/colormap
|
|
30
|
+
*
|
|
31
|
+
* Roseus: Perceptually uniform colormap from https://github.com/dofuuz/roseus
|
|
32
|
+
* By dofuuz, licensed under CC0 1.0 Universal.
|
|
33
|
+
*/
|
|
34
|
+
|
|
35
|
+
type ColorLUT = Uint8Array;
|
|
36
|
+
/**
|
|
37
|
+
* Get a 256-entry RGB LUT for the given color map.
|
|
38
|
+
*/
|
|
39
|
+
declare function getColorMap(value: ColorMapValue): ColorLUT;
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* Frequency scale mapping functions.
|
|
43
|
+
* Each maps a frequency (Hz) to a normalized position [0, 1].
|
|
44
|
+
*/
|
|
45
|
+
type FrequencyScaleName = 'linear' | 'logarithmic' | 'mel' | 'bark' | 'erb';
|
|
46
|
+
/**
|
|
47
|
+
* Returns a mapping function: (frequencyHz, minFrequency, maxFrequency) → [0, 1]
|
|
48
|
+
*/
|
|
49
|
+
declare function getFrequencyScale(name: FrequencyScaleName): (f: number, minF: number, maxF: number) => number;
|
|
50
|
+
|
|
51
|
+
interface TrackMenuItem {
|
|
52
|
+
id: string;
|
|
53
|
+
label?: string;
|
|
54
|
+
content: ReactNode;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
interface SpectrogramMenuItemsProps {
|
|
58
|
+
renderMode: RenderMode;
|
|
59
|
+
onRenderModeChange: (mode: RenderMode) => void;
|
|
60
|
+
onOpenSettings: () => void;
|
|
61
|
+
onClose?: () => void;
|
|
62
|
+
}
|
|
63
|
+
/**
|
|
64
|
+
* Returns TrackMenuItem[] for the spectrogram display mode radios and settings button.
|
|
65
|
+
*/
|
|
66
|
+
declare function SpectrogramMenuItems({ renderMode, onRenderModeChange, onOpenSettings, onClose, }: SpectrogramMenuItemsProps): TrackMenuItem[];
|
|
67
|
+
|
|
68
|
+
interface SpectrogramSettingsModalProps {
|
|
69
|
+
open: boolean;
|
|
70
|
+
onClose: () => void;
|
|
71
|
+
config: SpectrogramConfig;
|
|
72
|
+
colorMap: ColorMapValue;
|
|
73
|
+
onApply: (config: SpectrogramConfig, colorMap: ColorMapValue) => void;
|
|
74
|
+
}
|
|
75
|
+
declare const SpectrogramSettingsModal: React.FC<SpectrogramSettingsModalProps>;
|
|
76
|
+
|
|
77
|
+
interface SpectrogramWorkerComputeParams {
|
|
78
|
+
channelDataArrays: Float32Array[];
|
|
79
|
+
config: SpectrogramConfig;
|
|
80
|
+
sampleRate: number;
|
|
81
|
+
offsetSamples: number;
|
|
82
|
+
durationSamples: number;
|
|
83
|
+
mono: boolean;
|
|
84
|
+
}
|
|
85
|
+
interface SpectrogramWorkerRenderParams extends SpectrogramWorkerComputeParams {
|
|
86
|
+
render: {
|
|
87
|
+
canvasIds: string[][];
|
|
88
|
+
canvasWidths: number[];
|
|
89
|
+
canvasHeight: number;
|
|
90
|
+
devicePixelRatio: number;
|
|
91
|
+
samplesPerPixel: number;
|
|
92
|
+
colorLUT: Uint8Array;
|
|
93
|
+
frequencyScale: string;
|
|
94
|
+
minFrequency: number;
|
|
95
|
+
maxFrequency: number;
|
|
96
|
+
};
|
|
97
|
+
}
|
|
98
|
+
interface SpectrogramWorkerFFTParams {
|
|
99
|
+
clipId: string;
|
|
100
|
+
channelDataArrays: Float32Array[];
|
|
101
|
+
config: SpectrogramConfig;
|
|
102
|
+
sampleRate: number;
|
|
103
|
+
offsetSamples: number;
|
|
104
|
+
durationSamples: number;
|
|
105
|
+
mono: boolean;
|
|
106
|
+
sampleRange?: {
|
|
107
|
+
start: number;
|
|
108
|
+
end: number;
|
|
109
|
+
};
|
|
110
|
+
}
|
|
111
|
+
interface SpectrogramWorkerRenderChunksParams {
|
|
112
|
+
cacheKey: string;
|
|
113
|
+
canvasIds: string[];
|
|
114
|
+
canvasWidths: number[];
|
|
115
|
+
globalPixelOffsets: number[];
|
|
116
|
+
canvasHeight: number;
|
|
117
|
+
devicePixelRatio: number;
|
|
118
|
+
samplesPerPixel: number;
|
|
119
|
+
colorLUT: Uint8Array;
|
|
120
|
+
frequencyScale: string;
|
|
121
|
+
minFrequency: number;
|
|
122
|
+
maxFrequency: number;
|
|
123
|
+
gainDb: number;
|
|
124
|
+
rangeDb: number;
|
|
125
|
+
channelIndex: number;
|
|
126
|
+
}
|
|
127
|
+
interface SpectrogramWorkerApi {
|
|
128
|
+
compute(params: SpectrogramWorkerComputeParams): Promise<SpectrogramData[]>;
|
|
129
|
+
computeFFT(params: SpectrogramWorkerFFTParams): Promise<{
|
|
130
|
+
cacheKey: string;
|
|
131
|
+
}>;
|
|
132
|
+
renderChunks(params: SpectrogramWorkerRenderChunksParams): Promise<void>;
|
|
133
|
+
registerCanvas(canvasId: string, canvas: OffscreenCanvas): void;
|
|
134
|
+
unregisterCanvas(canvasId: string): void;
|
|
135
|
+
registerAudioData(clipId: string, channelDataArrays: Float32Array[], sampleRate: number): void;
|
|
136
|
+
unregisterAudioData(clipId: string): void;
|
|
137
|
+
computeAndRender(params: SpectrogramWorkerRenderParams): Promise<void>;
|
|
138
|
+
terminate(): void;
|
|
139
|
+
}
|
|
140
|
+
/**
|
|
141
|
+
* Wraps a Web Worker running `spectrogram.worker.ts` with a promise-based API.
|
|
142
|
+
*
|
|
143
|
+
* The caller is responsible for creating the Worker, e.g.:
|
|
144
|
+
* ```ts
|
|
145
|
+
* const worker = new Worker(
|
|
146
|
+
* new URL('@waveform-playlist/spectrogram/worker/spectrogram.worker', import.meta.url),
|
|
147
|
+
* { type: 'module' }
|
|
148
|
+
* );
|
|
149
|
+
* const api = createSpectrogramWorker(worker);
|
|
150
|
+
* ```
|
|
151
|
+
*/
|
|
152
|
+
declare function createSpectrogramWorker(worker: Worker): SpectrogramWorkerApi;
|
|
153
|
+
|
|
154
|
+
interface SpectrogramProviderProps {
|
|
155
|
+
config?: SpectrogramConfig;
|
|
156
|
+
colorMap?: ColorMapValue;
|
|
157
|
+
children: ReactNode;
|
|
158
|
+
}
|
|
159
|
+
declare const SpectrogramProvider: React.FC<SpectrogramProviderProps>;
|
|
160
|
+
|
|
161
|
+
export { type FrequencyScaleName, SpectrogramMenuItems, type SpectrogramMenuItemsProps, SpectrogramProvider, type SpectrogramProviderProps, SpectrogramSettingsModal, type SpectrogramSettingsModalProps, type SpectrogramWorkerApi, type SpectrogramWorkerRenderParams, type TrackMenuItem, computeSpectrogram, computeSpectrogramMono, createSpectrogramWorker, getColorMap, getFrequencyScale };
|