Skip to content

Commit c6c8dab

Browse files
committed
feat(streaming): Add sample rate normalization, tuning support, and improve chord detection
Enhance streaming analyzer with robust audio handling and improved chord accuracy: Audio preprocessing: - Add automatic resampling for high sample rates (>44100 Hz) to internal 44100 Hz - Add normalization gain support for loud/compressed audio via setNormalizationGain() - Add tuning reference frequency support via setTuningRefHz() for non-standard tuning Chord detection improvements: - Replace chroma averaging with median filtering for better noise robustness - Enhance chord template correlation with third and fifth note emphasis - Add penalty for notes not present in chord pattern - Add diatonic chord bonus in voted pattern computation based on detected key - Add pattern correction using known progressions for confusable chords Pattern locking: - Lock chord progression pattern once detected with high confidence - Use expected duration (if set) to optimize lock timing - Prevent pattern changes after early-song detection API additions: - setExpectedDuration(): Set total duration for optimal pattern lock timing - setNormalizationGain(): Apply gain for loud audio normalization - setTuningRefHz(): Correct chroma analysis for non-440Hz tuning Cleanup: - Remove package-lock.json from repository
1 parent 2ff331d commit c6c8dab

8 files changed

Lines changed: 494 additions & 1368 deletions

File tree

js/index.ts

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,9 @@ interface WasmStreamAnalyzer {
234234
frameCount: () => number;
235235
currentTime: () => number;
236236
sampleRate: () => number;
237+
setExpectedDuration: (durationSeconds: number) => void;
238+
setNormalizationGain: (gain: number) => void;
239+
setTuningRefHz: (refHz: number) => void;
237240
delete: () => void;
238241
}
239242

@@ -1649,6 +1652,38 @@ export class StreamAnalyzer {
16491652
return this.analyzer.sampleRate();
16501653
}
16511654

1655+
/**
1656+
* Set the expected total duration for pattern lock timing.
1657+
*
1658+
* @param durationSeconds - Total duration in seconds
1659+
*/
1660+
setExpectedDuration(durationSeconds: number): void {
1661+
this.analyzer.setExpectedDuration(durationSeconds);
1662+
}
1663+
1664+
/**
1665+
* Set normalization gain for loud/compressed audio.
1666+
*
1667+
* @param gain - Gain factor to apply (e.g., 0.5 for -6dB reduction)
1668+
*/
1669+
setNormalizationGain(gain: number): void {
1670+
this.analyzer.setNormalizationGain(gain);
1671+
}
1672+
1673+
/**
1674+
* Set tuning reference frequency for non-standard tuning.
1675+
*
1676+
* @param refHz - Reference frequency for A4 (default 440 Hz)
1677+
* @example
1678+
* // If audio is 1 semitone sharp (A4 = 466.16 Hz)
1679+
* analyzer.setTuningRefHz(466.16);
1680+
* // If audio is 1 semitone flat (A4 = 415.30 Hz)
1681+
* analyzer.setTuningRefHz(415.30);
1682+
*/
1683+
setTuningRefHz(refHz: number): void {
1684+
this.analyzer.setTuningRefHz(refHz);
1685+
}
1686+
16521687
/**
16531688
* Release resources. Call when done using the analyzer.
16541689
*/

js/sonare.js.d.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -409,6 +409,9 @@ interface WasmStreamAnalyzer {
409409
frameCount: () => number;
410410
currentTime: () => number;
411411
sampleRate: () => number;
412+
setExpectedDuration: (durationSeconds: number) => void;
413+
setNormalizationGain: (gain: number) => void;
414+
setTuningRefHz: (refHz: number) => void;
412415
delete: () => void;
413416
}
414417

0 commit comments

Comments
 (0)