Skip to content

Commit 08ceb17

Browse files
committed
Implement pitch detection in tuner frequency method
Replaces the placeholder in getTunerFrequency with actual pitch detection using the YIN algorithm. Stores analyser and pitch detection function as instance properties for reuse and reliability.
1 parent 0693ea8 commit 08ceb17

File tree

1 file changed

+9
-7
lines changed

1 file changed

+9
-7
lines changed

js/utils/synthutils.js

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2275,8 +2275,10 @@ function Synth() {
22752275
this.tunerMic = new Tone.UserMedia();
22762276
await this.tunerMic.open();
22772277

2278-
const analyser = new Tone.Analyser("waveform", 2048);
2279-
this.tunerMic.connect(analyser);
2278+
this.tunerAnalyser = new Tone.Analyser("waveform", 2048);
2279+
this.tunerMic.connect(this.tunerAnalyser);
2280+
2281+
const analyser = this.tunerAnalyser;
22802282

22812283
const YIN = (sampleRate, bufferSize = 2048, threshold = 0.1) => {
22822284
// Low-Pass Filter to remove high-frequency noise
@@ -2353,7 +2355,8 @@ function Synth() {
23532355
};
23542356
};
23552357

2356-
const detectPitch = YIN(Tone.context.sampleRate);
2358+
this.detectPitch = YIN(Tone.context.sampleRate);
2359+
const detectPitch = this.detectPitch;
23572360
let tunerMode = "chromatic"; // Add mode state
23582361
let targetPitch = { note: "A4", frequency: 440 }; // Default target pitch
23592362

@@ -3169,12 +3172,11 @@ function Synth() {
31693172
* @returns {number} The detected frequency in Hz
31703173
*/
31713174
this.getTunerFrequency = () => {
3172-
if (!this.tunerAnalyser) return 440; // Default to A4 if no analyser
3175+
if (!this.tunerAnalyser || !this.detectPitch) return 440; // Default to A4 if no analyser
31733176

31743177
const buffer = this.tunerAnalyser.getValue();
3175-
// TODO: Implement actual pitch detection algorithm
3176-
// For now, return a default value
3177-
return 440;
3178+
const pitch = this.detectPitch(buffer);
3179+
return pitch > 0 ? pitch : 440;
31783180
};
31793181

31803182
// Test function to verify tuner accuracy

0 commit comments

Comments
 (0)