Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 9 additions & 7 deletions js/utils/synthutils.js
Original file line number Diff line number Diff line change
Expand Up @@ -2275,8 +2275,10 @@ function Synth() {
this.tunerMic = new Tone.UserMedia();
await this.tunerMic.open();

const analyser = new Tone.Analyser("waveform", 2048);
this.tunerMic.connect(analyser);
this.tunerAnalyser = new Tone.Analyser("waveform", 2048);
this.tunerMic.connect(this.tunerAnalyser);

const analyser = this.tunerAnalyser;

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

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

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

const buffer = this.tunerAnalyser.getValue();
// TODO: Implement actual pitch detection algorithm
// For now, return a default value
return 440;
const pitch = this.detectPitch(buffer);
return pitch > 0 ? pitch : 440;
};

// Test function to verify tuner accuracy
Expand Down
Loading