@libraz/libsonare 1.0.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 +190 -0
- package/README.md +65 -0
- package/README.npm.md +65 -0
- package/dist/index.d.ts +831 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +1002 -0
- package/dist/index.js.map +1 -0
- package/dist/sonare.js +4056 -0
- package/dist/sonare.wasm +0 -0
- package/package.json +82 -0
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,831 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* sonare - Audio Analysis Library
|
|
3
|
+
*
|
|
4
|
+
* @example
|
|
5
|
+
* ```typescript
|
|
6
|
+
* import { init, detectBpm, detectKey, analyze } from '@libraz/sonare';
|
|
7
|
+
*
|
|
8
|
+
* await init();
|
|
9
|
+
*
|
|
10
|
+
* // Detect BPM from audio samples
|
|
11
|
+
* const bpm = detectBpm(samples, sampleRate);
|
|
12
|
+
*
|
|
13
|
+
* // Detect musical key
|
|
14
|
+
* const key = detectKey(samples, sampleRate);
|
|
15
|
+
*
|
|
16
|
+
* // Full analysis
|
|
17
|
+
* const result = analyze(samples, sampleRate);
|
|
18
|
+
* ```
|
|
19
|
+
*/
|
|
20
|
+
/**
|
|
21
|
+
* Progress callback for analysis progress reporting.
|
|
22
|
+
* @param progress - Progress value (0.0 to 1.0)
|
|
23
|
+
* @param stage - Current analysis stage name
|
|
24
|
+
*/
|
|
25
|
+
export type ProgressCallback = (progress: number, stage: string) => void;
|
|
26
|
+
/**
|
|
27
|
+
* Pitch class enum (C=0, C#=1, ..., B=11)
|
|
28
|
+
*/
|
|
29
|
+
export declare const PitchClass: {
|
|
30
|
+
readonly C: 0;
|
|
31
|
+
readonly Cs: 1;
|
|
32
|
+
readonly D: 2;
|
|
33
|
+
readonly Ds: 3;
|
|
34
|
+
readonly E: 4;
|
|
35
|
+
readonly F: 5;
|
|
36
|
+
readonly Fs: 6;
|
|
37
|
+
readonly G: 7;
|
|
38
|
+
readonly Gs: 8;
|
|
39
|
+
readonly A: 9;
|
|
40
|
+
readonly As: 10;
|
|
41
|
+
readonly B: 11;
|
|
42
|
+
};
|
|
43
|
+
export type PitchClass = (typeof PitchClass)[keyof typeof PitchClass];
|
|
44
|
+
/**
|
|
45
|
+
* Musical mode
|
|
46
|
+
*/
|
|
47
|
+
export declare const Mode: {
|
|
48
|
+
readonly Major: 0;
|
|
49
|
+
readonly Minor: 1;
|
|
50
|
+
};
|
|
51
|
+
export type Mode = (typeof Mode)[keyof typeof Mode];
|
|
52
|
+
/**
|
|
53
|
+
* Chord quality
|
|
54
|
+
*/
|
|
55
|
+
export declare const ChordQuality: {
|
|
56
|
+
readonly Major: 0;
|
|
57
|
+
readonly Minor: 1;
|
|
58
|
+
readonly Diminished: 2;
|
|
59
|
+
readonly Augmented: 3;
|
|
60
|
+
readonly Dominant7: 4;
|
|
61
|
+
readonly Major7: 5;
|
|
62
|
+
readonly Minor7: 6;
|
|
63
|
+
readonly Sus2: 7;
|
|
64
|
+
readonly Sus4: 8;
|
|
65
|
+
};
|
|
66
|
+
export type ChordQuality = (typeof ChordQuality)[keyof typeof ChordQuality];
|
|
67
|
+
/**
|
|
68
|
+
* Section type
|
|
69
|
+
*/
|
|
70
|
+
export declare const SectionType: {
|
|
71
|
+
readonly Intro: 0;
|
|
72
|
+
readonly Verse: 1;
|
|
73
|
+
readonly PreChorus: 2;
|
|
74
|
+
readonly Chorus: 3;
|
|
75
|
+
readonly Bridge: 4;
|
|
76
|
+
readonly Instrumental: 5;
|
|
77
|
+
readonly Outro: 6;
|
|
78
|
+
};
|
|
79
|
+
export type SectionType = (typeof SectionType)[keyof typeof SectionType];
|
|
80
|
+
/**
|
|
81
|
+
* Detected musical key
|
|
82
|
+
*/
|
|
83
|
+
export interface Key {
|
|
84
|
+
root: PitchClass;
|
|
85
|
+
mode: Mode;
|
|
86
|
+
confidence: number;
|
|
87
|
+
name: string;
|
|
88
|
+
shortName: string;
|
|
89
|
+
}
|
|
90
|
+
/**
|
|
91
|
+
* Detected beat
|
|
92
|
+
*/
|
|
93
|
+
export interface Beat {
|
|
94
|
+
time: number;
|
|
95
|
+
strength: number;
|
|
96
|
+
}
|
|
97
|
+
/**
|
|
98
|
+
* Detected chord
|
|
99
|
+
*/
|
|
100
|
+
export interface Chord {
|
|
101
|
+
root: PitchClass;
|
|
102
|
+
quality: ChordQuality;
|
|
103
|
+
start: number;
|
|
104
|
+
end: number;
|
|
105
|
+
confidence: number;
|
|
106
|
+
name: string;
|
|
107
|
+
}
|
|
108
|
+
/**
|
|
109
|
+
* Detected section
|
|
110
|
+
*/
|
|
111
|
+
export interface Section {
|
|
112
|
+
type: SectionType;
|
|
113
|
+
start: number;
|
|
114
|
+
end: number;
|
|
115
|
+
energyLevel: number;
|
|
116
|
+
confidence: number;
|
|
117
|
+
name: string;
|
|
118
|
+
}
|
|
119
|
+
/**
|
|
120
|
+
* Timbre characteristics
|
|
121
|
+
*/
|
|
122
|
+
export interface Timbre {
|
|
123
|
+
brightness: number;
|
|
124
|
+
warmth: number;
|
|
125
|
+
density: number;
|
|
126
|
+
roughness: number;
|
|
127
|
+
complexity: number;
|
|
128
|
+
}
|
|
129
|
+
/**
|
|
130
|
+
* Dynamics characteristics
|
|
131
|
+
*/
|
|
132
|
+
export interface Dynamics {
|
|
133
|
+
dynamicRangeDb: number;
|
|
134
|
+
loudnessRangeDb: number;
|
|
135
|
+
crestFactor: number;
|
|
136
|
+
isCompressed: boolean;
|
|
137
|
+
}
|
|
138
|
+
/**
|
|
139
|
+
* Time signature
|
|
140
|
+
*/
|
|
141
|
+
export interface TimeSignature {
|
|
142
|
+
numerator: number;
|
|
143
|
+
denominator: number;
|
|
144
|
+
confidence: number;
|
|
145
|
+
}
|
|
146
|
+
/**
|
|
147
|
+
* Rhythm features
|
|
148
|
+
*/
|
|
149
|
+
export interface RhythmFeatures {
|
|
150
|
+
syncopation: number;
|
|
151
|
+
grooveType: string;
|
|
152
|
+
patternRegularity: number;
|
|
153
|
+
}
|
|
154
|
+
/**
|
|
155
|
+
* Complete analysis result
|
|
156
|
+
*/
|
|
157
|
+
export interface AnalysisResult {
|
|
158
|
+
bpm: number;
|
|
159
|
+
bpmConfidence: number;
|
|
160
|
+
key: Key;
|
|
161
|
+
timeSignature: TimeSignature;
|
|
162
|
+
beats: Beat[];
|
|
163
|
+
chords: Chord[];
|
|
164
|
+
sections: Section[];
|
|
165
|
+
timbre: Timbre;
|
|
166
|
+
dynamics: Dynamics;
|
|
167
|
+
rhythm: RhythmFeatures;
|
|
168
|
+
form: string;
|
|
169
|
+
}
|
|
170
|
+
/**
|
|
171
|
+
* HPSS (Harmonic-Percussive Source Separation) result
|
|
172
|
+
*/
|
|
173
|
+
export interface HpssResult {
|
|
174
|
+
harmonic: Float32Array;
|
|
175
|
+
percussive: Float32Array;
|
|
176
|
+
sampleRate: number;
|
|
177
|
+
}
|
|
178
|
+
/**
|
|
179
|
+
* STFT (Short-Time Fourier Transform) result
|
|
180
|
+
*/
|
|
181
|
+
export interface StftResult {
|
|
182
|
+
nBins: number;
|
|
183
|
+
nFrames: number;
|
|
184
|
+
nFft: number;
|
|
185
|
+
hopLength: number;
|
|
186
|
+
sampleRate: number;
|
|
187
|
+
magnitude: Float32Array;
|
|
188
|
+
power: Float32Array;
|
|
189
|
+
}
|
|
190
|
+
/**
|
|
191
|
+
* Mel spectrogram result
|
|
192
|
+
*/
|
|
193
|
+
export interface MelSpectrogramResult {
|
|
194
|
+
nMels: number;
|
|
195
|
+
nFrames: number;
|
|
196
|
+
sampleRate: number;
|
|
197
|
+
hopLength: number;
|
|
198
|
+
power: Float32Array;
|
|
199
|
+
db: Float32Array;
|
|
200
|
+
}
|
|
201
|
+
/**
|
|
202
|
+
* MFCC result
|
|
203
|
+
*/
|
|
204
|
+
export interface MfccResult {
|
|
205
|
+
nMfcc: number;
|
|
206
|
+
nFrames: number;
|
|
207
|
+
coefficients: Float32Array;
|
|
208
|
+
}
|
|
209
|
+
/**
|
|
210
|
+
* Chroma features result
|
|
211
|
+
*/
|
|
212
|
+
export interface ChromaResult {
|
|
213
|
+
nChroma: number;
|
|
214
|
+
nFrames: number;
|
|
215
|
+
sampleRate: number;
|
|
216
|
+
hopLength: number;
|
|
217
|
+
features: Float32Array;
|
|
218
|
+
meanEnergy: number[];
|
|
219
|
+
}
|
|
220
|
+
/**
|
|
221
|
+
* Pitch detection result
|
|
222
|
+
*/
|
|
223
|
+
export interface PitchResult {
|
|
224
|
+
f0: Float32Array;
|
|
225
|
+
voicedProb: Float32Array;
|
|
226
|
+
voicedFlag: boolean[];
|
|
227
|
+
nFrames: number;
|
|
228
|
+
medianF0: number;
|
|
229
|
+
meanF0: number;
|
|
230
|
+
}
|
|
231
|
+
/**
|
|
232
|
+
* Initialize the WASM module.
|
|
233
|
+
* Must be called before using any analysis functions.
|
|
234
|
+
*
|
|
235
|
+
* @param options - Optional module configuration
|
|
236
|
+
* @returns Promise that resolves when initialization is complete
|
|
237
|
+
*/
|
|
238
|
+
export declare function init(options?: {
|
|
239
|
+
locateFile?: (path: string, prefix: string) => string;
|
|
240
|
+
}): Promise<void>;
|
|
241
|
+
/**
|
|
242
|
+
* Check if the module is initialized.
|
|
243
|
+
*/
|
|
244
|
+
export declare function isInitialized(): boolean;
|
|
245
|
+
/**
|
|
246
|
+
* Get the library version.
|
|
247
|
+
*/
|
|
248
|
+
export declare function version(): string;
|
|
249
|
+
/**
|
|
250
|
+
* Detect BPM from audio samples.
|
|
251
|
+
*
|
|
252
|
+
* @param samples - Audio samples (mono, float32)
|
|
253
|
+
* @param sampleRate - Sample rate in Hz
|
|
254
|
+
* @returns Detected BPM
|
|
255
|
+
*/
|
|
256
|
+
export declare function detectBpm(samples: Float32Array, sampleRate: number): number;
|
|
257
|
+
/**
|
|
258
|
+
* Detect musical key from audio samples.
|
|
259
|
+
*
|
|
260
|
+
* @param samples - Audio samples (mono, float32)
|
|
261
|
+
* @param sampleRate - Sample rate in Hz
|
|
262
|
+
* @returns Detected key
|
|
263
|
+
*/
|
|
264
|
+
export declare function detectKey(samples: Float32Array, sampleRate: number): Key;
|
|
265
|
+
/**
|
|
266
|
+
* Detect onset times from audio samples.
|
|
267
|
+
*
|
|
268
|
+
* @param samples - Audio samples (mono, float32)
|
|
269
|
+
* @param sampleRate - Sample rate in Hz
|
|
270
|
+
* @returns Array of onset times in seconds
|
|
271
|
+
*/
|
|
272
|
+
export declare function detectOnsets(samples: Float32Array, sampleRate: number): Float32Array;
|
|
273
|
+
/**
|
|
274
|
+
* Detect beat times from audio samples.
|
|
275
|
+
*
|
|
276
|
+
* @param samples - Audio samples (mono, float32)
|
|
277
|
+
* @param sampleRate - Sample rate in Hz
|
|
278
|
+
* @returns Array of beat times in seconds
|
|
279
|
+
*/
|
|
280
|
+
export declare function detectBeats(samples: Float32Array, sampleRate: number): Float32Array;
|
|
281
|
+
/**
|
|
282
|
+
* Perform complete music analysis.
|
|
283
|
+
*
|
|
284
|
+
* @param samples - Audio samples (mono, float32)
|
|
285
|
+
* @param sampleRate - Sample rate in Hz
|
|
286
|
+
* @returns Complete analysis result
|
|
287
|
+
*/
|
|
288
|
+
export declare function analyze(samples: Float32Array, sampleRate: number): AnalysisResult;
|
|
289
|
+
/**
|
|
290
|
+
* Perform complete music analysis with progress reporting.
|
|
291
|
+
*
|
|
292
|
+
* @param samples - Audio samples (mono, float32)
|
|
293
|
+
* @param sampleRate - Sample rate in Hz
|
|
294
|
+
* @param onProgress - Progress callback (progress: 0-1, stage: string)
|
|
295
|
+
* @returns Complete analysis result
|
|
296
|
+
*/
|
|
297
|
+
export declare function analyzeWithProgress(samples: Float32Array, sampleRate: number, onProgress: ProgressCallback): AnalysisResult;
|
|
298
|
+
/**
|
|
299
|
+
* Perform Harmonic-Percussive Source Separation (HPSS).
|
|
300
|
+
*
|
|
301
|
+
* @param samples - Audio samples (mono, float32)
|
|
302
|
+
* @param sampleRate - Sample rate in Hz
|
|
303
|
+
* @param kernelHarmonic - Horizontal median filter size for harmonic (default: 31)
|
|
304
|
+
* @param kernelPercussive - Vertical median filter size for percussive (default: 31)
|
|
305
|
+
* @returns Separated harmonic and percussive components
|
|
306
|
+
*/
|
|
307
|
+
export declare function hpss(samples: Float32Array, sampleRate: number, kernelHarmonic?: number, kernelPercussive?: number): HpssResult;
|
|
308
|
+
/**
|
|
309
|
+
* Extract harmonic component from audio.
|
|
310
|
+
*
|
|
311
|
+
* @param samples - Audio samples (mono, float32)
|
|
312
|
+
* @param sampleRate - Sample rate in Hz
|
|
313
|
+
* @returns Harmonic component
|
|
314
|
+
*/
|
|
315
|
+
export declare function harmonic(samples: Float32Array, sampleRate: number): Float32Array;
|
|
316
|
+
/**
|
|
317
|
+
* Extract percussive component from audio.
|
|
318
|
+
*
|
|
319
|
+
* @param samples - Audio samples (mono, float32)
|
|
320
|
+
* @param sampleRate - Sample rate in Hz
|
|
321
|
+
* @returns Percussive component
|
|
322
|
+
*/
|
|
323
|
+
export declare function percussive(samples: Float32Array, sampleRate: number): Float32Array;
|
|
324
|
+
/**
|
|
325
|
+
* Time-stretch audio without changing pitch.
|
|
326
|
+
*
|
|
327
|
+
* @param samples - Audio samples (mono, float32)
|
|
328
|
+
* @param sampleRate - Sample rate in Hz
|
|
329
|
+
* @param rate - Time stretch rate (0.5 = double duration, 2.0 = half duration)
|
|
330
|
+
* @returns Time-stretched audio
|
|
331
|
+
*/
|
|
332
|
+
export declare function timeStretch(samples: Float32Array, sampleRate: number, rate: number): Float32Array;
|
|
333
|
+
/**
|
|
334
|
+
* Pitch-shift audio without changing duration.
|
|
335
|
+
*
|
|
336
|
+
* @param samples - Audio samples (mono, float32)
|
|
337
|
+
* @param sampleRate - Sample rate in Hz
|
|
338
|
+
* @param semitones - Pitch shift in semitones (+12 = one octave up, -12 = one octave down)
|
|
339
|
+
* @returns Pitch-shifted audio
|
|
340
|
+
*/
|
|
341
|
+
export declare function pitchShift(samples: Float32Array, sampleRate: number, semitones: number): Float32Array;
|
|
342
|
+
/**
|
|
343
|
+
* Normalize audio to target peak level.
|
|
344
|
+
*
|
|
345
|
+
* @param samples - Audio samples (mono, float32)
|
|
346
|
+
* @param sampleRate - Sample rate in Hz
|
|
347
|
+
* @param targetDb - Target peak level in dB (default: 0 dB = full scale)
|
|
348
|
+
* @returns Normalized audio
|
|
349
|
+
*/
|
|
350
|
+
export declare function normalize(samples: Float32Array, sampleRate: number, targetDb?: number): Float32Array;
|
|
351
|
+
/**
|
|
352
|
+
* Trim silence from beginning and end of audio.
|
|
353
|
+
*
|
|
354
|
+
* @param samples - Audio samples (mono, float32)
|
|
355
|
+
* @param sampleRate - Sample rate in Hz
|
|
356
|
+
* @param thresholdDb - Silence threshold in dB (default: -60 dB)
|
|
357
|
+
* @returns Trimmed audio
|
|
358
|
+
*/
|
|
359
|
+
export declare function trim(samples: Float32Array, sampleRate: number, thresholdDb?: number): Float32Array;
|
|
360
|
+
/**
|
|
361
|
+
* Compute Short-Time Fourier Transform (STFT).
|
|
362
|
+
*
|
|
363
|
+
* @param samples - Audio samples (mono, float32)
|
|
364
|
+
* @param sampleRate - Sample rate in Hz
|
|
365
|
+
* @param nFft - FFT size (default: 2048)
|
|
366
|
+
* @param hopLength - Hop length (default: 512)
|
|
367
|
+
* @returns STFT result with magnitude and power spectrograms
|
|
368
|
+
*/
|
|
369
|
+
export declare function stft(samples: Float32Array, sampleRate: number, nFft?: number, hopLength?: number): StftResult;
|
|
370
|
+
/**
|
|
371
|
+
* Compute STFT and return magnitude in decibels.
|
|
372
|
+
*
|
|
373
|
+
* @param samples - Audio samples (mono, float32)
|
|
374
|
+
* @param sampleRate - Sample rate in Hz
|
|
375
|
+
* @param nFft - FFT size (default: 2048)
|
|
376
|
+
* @param hopLength - Hop length (default: 512)
|
|
377
|
+
* @returns STFT result with dB values
|
|
378
|
+
*/
|
|
379
|
+
export declare function stftDb(samples: Float32Array, sampleRate: number, nFft?: number, hopLength?: number): {
|
|
380
|
+
nBins: number;
|
|
381
|
+
nFrames: number;
|
|
382
|
+
db: Float32Array;
|
|
383
|
+
};
|
|
384
|
+
/**
|
|
385
|
+
* Compute Mel spectrogram.
|
|
386
|
+
*
|
|
387
|
+
* @param samples - Audio samples (mono, float32)
|
|
388
|
+
* @param sampleRate - Sample rate in Hz
|
|
389
|
+
* @param nFft - FFT size (default: 2048)
|
|
390
|
+
* @param hopLength - Hop length (default: 512)
|
|
391
|
+
* @param nMels - Number of Mel bands (default: 128)
|
|
392
|
+
* @returns Mel spectrogram result
|
|
393
|
+
*/
|
|
394
|
+
export declare function melSpectrogram(samples: Float32Array, sampleRate: number, nFft?: number, hopLength?: number, nMels?: number): MelSpectrogramResult;
|
|
395
|
+
/**
|
|
396
|
+
* Compute MFCC (Mel-Frequency Cepstral Coefficients).
|
|
397
|
+
*
|
|
398
|
+
* @param samples - Audio samples (mono, float32)
|
|
399
|
+
* @param sampleRate - Sample rate in Hz
|
|
400
|
+
* @param nFft - FFT size (default: 2048)
|
|
401
|
+
* @param hopLength - Hop length (default: 512)
|
|
402
|
+
* @param nMels - Number of Mel bands (default: 128)
|
|
403
|
+
* @param nMfcc - Number of MFCC coefficients (default: 13)
|
|
404
|
+
* @returns MFCC result
|
|
405
|
+
*/
|
|
406
|
+
export declare function mfcc(samples: Float32Array, sampleRate: number, nFft?: number, hopLength?: number, nMels?: number, nMfcc?: number): MfccResult;
|
|
407
|
+
/**
|
|
408
|
+
* Compute chromagram (pitch class distribution).
|
|
409
|
+
*
|
|
410
|
+
* @param samples - Audio samples (mono, float32)
|
|
411
|
+
* @param sampleRate - Sample rate in Hz
|
|
412
|
+
* @param nFft - FFT size (default: 2048)
|
|
413
|
+
* @param hopLength - Hop length (default: 512)
|
|
414
|
+
* @returns Chroma features result
|
|
415
|
+
*/
|
|
416
|
+
export declare function chroma(samples: Float32Array, sampleRate: number, nFft?: number, hopLength?: number): ChromaResult;
|
|
417
|
+
/**
|
|
418
|
+
* Compute spectral centroid (center of mass of spectrum).
|
|
419
|
+
*
|
|
420
|
+
* @param samples - Audio samples (mono, float32)
|
|
421
|
+
* @param sampleRate - Sample rate in Hz
|
|
422
|
+
* @param nFft - FFT size (default: 2048)
|
|
423
|
+
* @param hopLength - Hop length (default: 512)
|
|
424
|
+
* @returns Spectral centroid in Hz for each frame
|
|
425
|
+
*/
|
|
426
|
+
export declare function spectralCentroid(samples: Float32Array, sampleRate: number, nFft?: number, hopLength?: number): Float32Array;
|
|
427
|
+
/**
|
|
428
|
+
* Compute spectral bandwidth.
|
|
429
|
+
*
|
|
430
|
+
* @param samples - Audio samples (mono, float32)
|
|
431
|
+
* @param sampleRate - Sample rate in Hz
|
|
432
|
+
* @param nFft - FFT size (default: 2048)
|
|
433
|
+
* @param hopLength - Hop length (default: 512)
|
|
434
|
+
* @returns Spectral bandwidth in Hz for each frame
|
|
435
|
+
*/
|
|
436
|
+
export declare function spectralBandwidth(samples: Float32Array, sampleRate: number, nFft?: number, hopLength?: number): Float32Array;
|
|
437
|
+
/**
|
|
438
|
+
* Compute spectral rolloff frequency.
|
|
439
|
+
*
|
|
440
|
+
* @param samples - Audio samples (mono, float32)
|
|
441
|
+
* @param sampleRate - Sample rate in Hz
|
|
442
|
+
* @param nFft - FFT size (default: 2048)
|
|
443
|
+
* @param hopLength - Hop length (default: 512)
|
|
444
|
+
* @param rollPercent - Percentage threshold (default: 0.85)
|
|
445
|
+
* @returns Rolloff frequency in Hz for each frame
|
|
446
|
+
*/
|
|
447
|
+
export declare function spectralRolloff(samples: Float32Array, sampleRate: number, nFft?: number, hopLength?: number, rollPercent?: number): Float32Array;
|
|
448
|
+
/**
|
|
449
|
+
* Compute spectral flatness.
|
|
450
|
+
*
|
|
451
|
+
* @param samples - Audio samples (mono, float32)
|
|
452
|
+
* @param sampleRate - Sample rate in Hz
|
|
453
|
+
* @param nFft - FFT size (default: 2048)
|
|
454
|
+
* @param hopLength - Hop length (default: 512)
|
|
455
|
+
* @returns Spectral flatness for each frame (0 = tonal, 1 = noise-like)
|
|
456
|
+
*/
|
|
457
|
+
export declare function spectralFlatness(samples: Float32Array, sampleRate: number, nFft?: number, hopLength?: number): Float32Array;
|
|
458
|
+
/**
|
|
459
|
+
* Compute zero crossing rate.
|
|
460
|
+
*
|
|
461
|
+
* @param samples - Audio samples (mono, float32)
|
|
462
|
+
* @param sampleRate - Sample rate in Hz
|
|
463
|
+
* @param frameLength - Frame length (default: 2048)
|
|
464
|
+
* @param hopLength - Hop length (default: 512)
|
|
465
|
+
* @returns Zero crossing rate for each frame
|
|
466
|
+
*/
|
|
467
|
+
export declare function zeroCrossingRate(samples: Float32Array, sampleRate: number, frameLength?: number, hopLength?: number): Float32Array;
|
|
468
|
+
/**
|
|
469
|
+
* Compute RMS energy.
|
|
470
|
+
*
|
|
471
|
+
* @param samples - Audio samples (mono, float32)
|
|
472
|
+
* @param sampleRate - Sample rate in Hz
|
|
473
|
+
* @param frameLength - Frame length (default: 2048)
|
|
474
|
+
* @param hopLength - Hop length (default: 512)
|
|
475
|
+
* @returns RMS energy for each frame
|
|
476
|
+
*/
|
|
477
|
+
export declare function rmsEnergy(samples: Float32Array, sampleRate: number, frameLength?: number, hopLength?: number): Float32Array;
|
|
478
|
+
/**
|
|
479
|
+
* Detect pitch using YIN algorithm.
|
|
480
|
+
*
|
|
481
|
+
* @param samples - Audio samples (mono, float32)
|
|
482
|
+
* @param sampleRate - Sample rate in Hz
|
|
483
|
+
* @param frameLength - Frame length (default: 2048)
|
|
484
|
+
* @param hopLength - Hop length (default: 512)
|
|
485
|
+
* @param fmin - Minimum frequency in Hz (default: 65)
|
|
486
|
+
* @param fmax - Maximum frequency in Hz (default: 2093)
|
|
487
|
+
* @param threshold - YIN threshold (default: 0.3)
|
|
488
|
+
* @returns Pitch detection result
|
|
489
|
+
*/
|
|
490
|
+
export declare function pitchYin(samples: Float32Array, sampleRate: number, frameLength?: number, hopLength?: number, fmin?: number, fmax?: number, threshold?: number): PitchResult;
|
|
491
|
+
/**
|
|
492
|
+
* Detect pitch using pYIN algorithm (probabilistic YIN with HMM smoothing).
|
|
493
|
+
*
|
|
494
|
+
* @param samples - Audio samples (mono, float32)
|
|
495
|
+
* @param sampleRate - Sample rate in Hz
|
|
496
|
+
* @param frameLength - Frame length (default: 2048)
|
|
497
|
+
* @param hopLength - Hop length (default: 512)
|
|
498
|
+
* @param fmin - Minimum frequency in Hz (default: 65)
|
|
499
|
+
* @param fmax - Maximum frequency in Hz (default: 2093)
|
|
500
|
+
* @param threshold - YIN threshold (default: 0.3)
|
|
501
|
+
* @returns Pitch detection result
|
|
502
|
+
*/
|
|
503
|
+
export declare function pitchPyin(samples: Float32Array, sampleRate: number, frameLength?: number, hopLength?: number, fmin?: number, fmax?: number, threshold?: number): PitchResult;
|
|
504
|
+
/**
|
|
505
|
+
* Convert frequency in Hz to Mel scale.
|
|
506
|
+
*
|
|
507
|
+
* @param hz - Frequency in Hz
|
|
508
|
+
* @returns Mel frequency
|
|
509
|
+
*/
|
|
510
|
+
export declare function hzToMel(hz: number): number;
|
|
511
|
+
/**
|
|
512
|
+
* Convert Mel scale to frequency in Hz.
|
|
513
|
+
*
|
|
514
|
+
* @param mel - Mel frequency
|
|
515
|
+
* @returns Frequency in Hz
|
|
516
|
+
*/
|
|
517
|
+
export declare function melToHz(mel: number): number;
|
|
518
|
+
/**
|
|
519
|
+
* Convert frequency in Hz to MIDI note number.
|
|
520
|
+
*
|
|
521
|
+
* @param hz - Frequency in Hz
|
|
522
|
+
* @returns MIDI note number (A4 = 440 Hz = 69)
|
|
523
|
+
*/
|
|
524
|
+
export declare function hzToMidi(hz: number): number;
|
|
525
|
+
/**
|
|
526
|
+
* Convert MIDI note number to frequency in Hz.
|
|
527
|
+
*
|
|
528
|
+
* @param midi - MIDI note number
|
|
529
|
+
* @returns Frequency in Hz
|
|
530
|
+
*/
|
|
531
|
+
export declare function midiToHz(midi: number): number;
|
|
532
|
+
/**
|
|
533
|
+
* Convert frequency in Hz to note name.
|
|
534
|
+
*
|
|
535
|
+
* @param hz - Frequency in Hz
|
|
536
|
+
* @returns Note name (e.g., "A4", "C#5")
|
|
537
|
+
*/
|
|
538
|
+
export declare function hzToNote(hz: number): string;
|
|
539
|
+
/**
|
|
540
|
+
* Convert note name to frequency in Hz.
|
|
541
|
+
*
|
|
542
|
+
* @param note - Note name (e.g., "A4", "C#5")
|
|
543
|
+
* @returns Frequency in Hz
|
|
544
|
+
*/
|
|
545
|
+
export declare function noteToHz(note: string): number;
|
|
546
|
+
/**
|
|
547
|
+
* Convert frame index to time in seconds.
|
|
548
|
+
*
|
|
549
|
+
* @param frames - Frame index
|
|
550
|
+
* @param sr - Sample rate in Hz
|
|
551
|
+
* @param hopLength - Hop length in samples
|
|
552
|
+
* @returns Time in seconds
|
|
553
|
+
*/
|
|
554
|
+
export declare function framesToTime(frames: number, sr: number, hopLength: number): number;
|
|
555
|
+
/**
|
|
556
|
+
* Convert time in seconds to frame index.
|
|
557
|
+
*
|
|
558
|
+
* @param time - Time in seconds
|
|
559
|
+
* @param sr - Sample rate in Hz
|
|
560
|
+
* @param hopLength - Hop length in samples
|
|
561
|
+
* @returns Frame index
|
|
562
|
+
*/
|
|
563
|
+
export declare function timeToFrames(time: number, sr: number, hopLength: number): number;
|
|
564
|
+
/**
|
|
565
|
+
* Resample audio to a different sample rate.
|
|
566
|
+
*
|
|
567
|
+
* @param samples - Audio samples (mono, float32)
|
|
568
|
+
* @param srcSr - Source sample rate in Hz
|
|
569
|
+
* @param targetSr - Target sample rate in Hz
|
|
570
|
+
* @returns Resampled audio
|
|
571
|
+
*/
|
|
572
|
+
export declare function resample(samples: Float32Array, srcSr: number, targetSr: number): Float32Array;
|
|
573
|
+
/**
|
|
574
|
+
* Wrapper around audio data that exposes all analysis and feature functions as instance methods.
|
|
575
|
+
*
|
|
576
|
+
* @example
|
|
577
|
+
* ```typescript
|
|
578
|
+
* import { init, Audio } from '@libraz/sonare';
|
|
579
|
+
*
|
|
580
|
+
* await init();
|
|
581
|
+
*
|
|
582
|
+
* const audio = Audio.fromBuffer(samples, 44100);
|
|
583
|
+
* console.log('BPM:', audio.detectBpm());
|
|
584
|
+
* console.log('Key:', audio.detectKey().name);
|
|
585
|
+
*
|
|
586
|
+
* const mel = audio.melSpectrogram();
|
|
587
|
+
* ```
|
|
588
|
+
*/
|
|
589
|
+
export declare class Audio {
|
|
590
|
+
private _samples;
|
|
591
|
+
private _sampleRate;
|
|
592
|
+
private constructor();
|
|
593
|
+
/** Create an Audio instance from raw sample data. */
|
|
594
|
+
static fromBuffer(samples: Float32Array, sampleRate: number): Audio;
|
|
595
|
+
/** The raw audio samples. */
|
|
596
|
+
get data(): Float32Array;
|
|
597
|
+
/** Number of samples. */
|
|
598
|
+
get length(): number;
|
|
599
|
+
/** Sample rate in Hz. */
|
|
600
|
+
get sampleRate(): number;
|
|
601
|
+
/** Duration in seconds. */
|
|
602
|
+
get duration(): number;
|
|
603
|
+
detectBpm(): number;
|
|
604
|
+
detectKey(): Key;
|
|
605
|
+
detectOnsets(): Float32Array;
|
|
606
|
+
detectBeats(): Float32Array;
|
|
607
|
+
analyze(): AnalysisResult;
|
|
608
|
+
analyzeWithProgress(onProgress: ProgressCallback): AnalysisResult;
|
|
609
|
+
hpss(kernelHarmonic?: number, kernelPercussive?: number): HpssResult;
|
|
610
|
+
harmonic(): Float32Array;
|
|
611
|
+
percussive(): Float32Array;
|
|
612
|
+
timeStretch(rate: number): Float32Array;
|
|
613
|
+
pitchShift(semitones: number): Float32Array;
|
|
614
|
+
normalize(targetDb?: number): Float32Array;
|
|
615
|
+
trim(thresholdDb?: number): Float32Array;
|
|
616
|
+
stft(nFft?: number, hopLength?: number): StftResult;
|
|
617
|
+
stftDb(nFft?: number, hopLength?: number): {
|
|
618
|
+
nBins: number;
|
|
619
|
+
nFrames: number;
|
|
620
|
+
db: Float32Array;
|
|
621
|
+
};
|
|
622
|
+
melSpectrogram(nFft?: number, hopLength?: number, nMels?: number): MelSpectrogramResult;
|
|
623
|
+
mfcc(nFft?: number, hopLength?: number, nMels?: number, nMfcc?: number): MfccResult;
|
|
624
|
+
chroma(nFft?: number, hopLength?: number): ChromaResult;
|
|
625
|
+
spectralCentroid(nFft?: number, hopLength?: number): Float32Array;
|
|
626
|
+
spectralBandwidth(nFft?: number, hopLength?: number): Float32Array;
|
|
627
|
+
spectralRolloff(nFft?: number, hopLength?: number, rollPercent?: number): Float32Array;
|
|
628
|
+
spectralFlatness(nFft?: number, hopLength?: number): Float32Array;
|
|
629
|
+
zeroCrossingRate(frameLength?: number, hopLength?: number): Float32Array;
|
|
630
|
+
rmsEnergy(frameLength?: number, hopLength?: number): Float32Array;
|
|
631
|
+
pitchYin(frameLength?: number, hopLength?: number, fmin?: number, fmax?: number, threshold?: number): PitchResult;
|
|
632
|
+
pitchPyin(frameLength?: number, hopLength?: number, fmin?: number, fmax?: number, threshold?: number): PitchResult;
|
|
633
|
+
resample(targetSr: number): Float32Array;
|
|
634
|
+
}
|
|
635
|
+
/**
|
|
636
|
+
* A detected chord change in the progression
|
|
637
|
+
*/
|
|
638
|
+
export interface ChordChange {
|
|
639
|
+
root: PitchClass;
|
|
640
|
+
quality: ChordQuality;
|
|
641
|
+
startTime: number;
|
|
642
|
+
confidence: number;
|
|
643
|
+
}
|
|
644
|
+
/**
|
|
645
|
+
* A chord detected at bar boundary (beat-synchronized)
|
|
646
|
+
*/
|
|
647
|
+
export interface BarChord {
|
|
648
|
+
barIndex: number;
|
|
649
|
+
root: PitchClass;
|
|
650
|
+
quality: ChordQuality;
|
|
651
|
+
startTime: number;
|
|
652
|
+
confidence: number;
|
|
653
|
+
}
|
|
654
|
+
/**
|
|
655
|
+
* Pattern score for known chord progressions
|
|
656
|
+
*/
|
|
657
|
+
export interface PatternScore {
|
|
658
|
+
name: string;
|
|
659
|
+
score: number;
|
|
660
|
+
}
|
|
661
|
+
/**
|
|
662
|
+
* Progressive estimation results for BPM, Key, and Chord
|
|
663
|
+
*/
|
|
664
|
+
export interface ProgressiveEstimate {
|
|
665
|
+
bpm: number;
|
|
666
|
+
bpmConfidence: number;
|
|
667
|
+
bpmCandidateCount: number;
|
|
668
|
+
key: PitchClass;
|
|
669
|
+
keyMinor: boolean;
|
|
670
|
+
keyConfidence: number;
|
|
671
|
+
chordRoot: PitchClass;
|
|
672
|
+
chordQuality: ChordQuality;
|
|
673
|
+
chordConfidence: number;
|
|
674
|
+
chordProgression: ChordChange[];
|
|
675
|
+
barChordProgression: BarChord[];
|
|
676
|
+
currentBar: number;
|
|
677
|
+
barDuration: number;
|
|
678
|
+
votedPattern: BarChord[];
|
|
679
|
+
patternLength: number;
|
|
680
|
+
detectedPatternName: string;
|
|
681
|
+
detectedPatternScore: number;
|
|
682
|
+
allPatternScores: PatternScore[];
|
|
683
|
+
accumulatedSeconds: number;
|
|
684
|
+
usedFrames: number;
|
|
685
|
+
updated: boolean;
|
|
686
|
+
}
|
|
687
|
+
/**
|
|
688
|
+
* Statistics and current state of the analyzer
|
|
689
|
+
*/
|
|
690
|
+
export interface AnalyzerStats {
|
|
691
|
+
totalFrames: number;
|
|
692
|
+
totalSamples: number;
|
|
693
|
+
durationSeconds: number;
|
|
694
|
+
estimate: ProgressiveEstimate;
|
|
695
|
+
}
|
|
696
|
+
/**
|
|
697
|
+
* Frame buffer with analysis results
|
|
698
|
+
*/
|
|
699
|
+
export interface FrameBuffer {
|
|
700
|
+
nFrames: number;
|
|
701
|
+
timestamps: Float32Array;
|
|
702
|
+
mel: Float32Array;
|
|
703
|
+
chroma: Float32Array;
|
|
704
|
+
onsetStrength: Float32Array;
|
|
705
|
+
rmsEnergy: Float32Array;
|
|
706
|
+
spectralCentroid: Float32Array;
|
|
707
|
+
spectralFlatness: Float32Array;
|
|
708
|
+
chordRoot: Int32Array;
|
|
709
|
+
chordQuality: Int32Array;
|
|
710
|
+
chordConfidence: Float32Array;
|
|
711
|
+
}
|
|
712
|
+
/**
|
|
713
|
+
* Configuration for StreamAnalyzer
|
|
714
|
+
*/
|
|
715
|
+
export interface StreamConfig {
|
|
716
|
+
sampleRate: number;
|
|
717
|
+
nFft?: number;
|
|
718
|
+
hopLength?: number;
|
|
719
|
+
nMels?: number;
|
|
720
|
+
computeMel?: boolean;
|
|
721
|
+
computeChroma?: boolean;
|
|
722
|
+
computeOnset?: boolean;
|
|
723
|
+
emitEveryNFrames?: number;
|
|
724
|
+
}
|
|
725
|
+
/**
|
|
726
|
+
* Real-time streaming audio analyzer.
|
|
727
|
+
*
|
|
728
|
+
* @example
|
|
729
|
+
* ```typescript
|
|
730
|
+
* import { init, StreamAnalyzer } from '@libraz/sonare';
|
|
731
|
+
*
|
|
732
|
+
* await init();
|
|
733
|
+
*
|
|
734
|
+
* const analyzer = new StreamAnalyzer({ sampleRate: 44100 });
|
|
735
|
+
*
|
|
736
|
+
* // In audio processing callback
|
|
737
|
+
* analyzer.process(samples);
|
|
738
|
+
*
|
|
739
|
+
* // Get current analysis state
|
|
740
|
+
* const stats = analyzer.stats();
|
|
741
|
+
* console.log('BPM:', stats.estimate.bpm);
|
|
742
|
+
* console.log('Key:', stats.estimate.key);
|
|
743
|
+
* console.log('Chord progression:', stats.estimate.chordProgression);
|
|
744
|
+
* ```
|
|
745
|
+
*/
|
|
746
|
+
export declare class StreamAnalyzer {
|
|
747
|
+
private analyzer;
|
|
748
|
+
/**
|
|
749
|
+
* Create a new StreamAnalyzer.
|
|
750
|
+
*
|
|
751
|
+
* @param config - Configuration options
|
|
752
|
+
*/
|
|
753
|
+
constructor(config: StreamConfig);
|
|
754
|
+
/**
|
|
755
|
+
* Process audio samples.
|
|
756
|
+
*
|
|
757
|
+
* @param samples - Audio samples (mono, float32)
|
|
758
|
+
*/
|
|
759
|
+
process(samples: Float32Array): void;
|
|
760
|
+
/**
|
|
761
|
+
* Process audio samples with explicit sample offset.
|
|
762
|
+
*
|
|
763
|
+
* @param samples - Audio samples (mono, float32)
|
|
764
|
+
* @param sampleOffset - Cumulative sample count at start of this chunk
|
|
765
|
+
*/
|
|
766
|
+
processWithOffset(samples: Float32Array, sampleOffset: number): void;
|
|
767
|
+
/**
|
|
768
|
+
* Get the number of frames available to read.
|
|
769
|
+
*/
|
|
770
|
+
availableFrames(): number;
|
|
771
|
+
/**
|
|
772
|
+
* Read processed frames as Structure of Arrays.
|
|
773
|
+
*
|
|
774
|
+
* @param maxFrames - Maximum number of frames to read
|
|
775
|
+
* @returns Frame buffer with analysis results
|
|
776
|
+
*/
|
|
777
|
+
readFrames(maxFrames: number): FrameBuffer;
|
|
778
|
+
/**
|
|
779
|
+
* Reset the analyzer state.
|
|
780
|
+
*
|
|
781
|
+
* @param baseSampleOffset - Starting sample offset (default 0)
|
|
782
|
+
*/
|
|
783
|
+
reset(baseSampleOffset?: number): void;
|
|
784
|
+
/**
|
|
785
|
+
* Get current statistics and progressive estimates.
|
|
786
|
+
*
|
|
787
|
+
* @returns Analyzer statistics including BPM, key, and chord progression
|
|
788
|
+
*/
|
|
789
|
+
stats(): AnalyzerStats;
|
|
790
|
+
/**
|
|
791
|
+
* Get total frames processed.
|
|
792
|
+
*/
|
|
793
|
+
frameCount(): number;
|
|
794
|
+
/**
|
|
795
|
+
* Get current time position in seconds.
|
|
796
|
+
*/
|
|
797
|
+
currentTime(): number;
|
|
798
|
+
/**
|
|
799
|
+
* Get the sample rate.
|
|
800
|
+
*/
|
|
801
|
+
sampleRate(): number;
|
|
802
|
+
/**
|
|
803
|
+
* Set the expected total duration for pattern lock timing.
|
|
804
|
+
*
|
|
805
|
+
* @param durationSeconds - Total duration in seconds
|
|
806
|
+
*/
|
|
807
|
+
setExpectedDuration(durationSeconds: number): void;
|
|
808
|
+
/**
|
|
809
|
+
* Set normalization gain for loud/compressed audio.
|
|
810
|
+
*
|
|
811
|
+
* @param gain - Gain factor to apply (e.g., 0.5 for -6dB reduction)
|
|
812
|
+
*/
|
|
813
|
+
setNormalizationGain(gain: number): void;
|
|
814
|
+
/**
|
|
815
|
+
* Set tuning reference frequency for non-standard tuning.
|
|
816
|
+
*
|
|
817
|
+
* @param refHz - Reference frequency for A4 (default 440 Hz)
|
|
818
|
+
* @example
|
|
819
|
+
* // If audio is 1 semitone sharp (A4 = 466.16 Hz)
|
|
820
|
+
* analyzer.setTuningRefHz(466.16);
|
|
821
|
+
* // If audio is 1 semitone flat (A4 = 415.30 Hz)
|
|
822
|
+
* analyzer.setTuningRefHz(415.30);
|
|
823
|
+
*/
|
|
824
|
+
setTuningRefHz(refHz: number): void;
|
|
825
|
+
/**
|
|
826
|
+
* Release resources. Call when done using the analyzer.
|
|
827
|
+
*/
|
|
828
|
+
dispose(): void;
|
|
829
|
+
}
|
|
830
|
+
export { PitchClass as Pitch };
|
|
831
|
+
//# sourceMappingURL=index.d.ts.map
|