You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

99 lines
3.0 KiB

document.querySelector('#analyze-pitch button').addEventListener('click', startListening);
const calculateCent = (frequency) => {
return frequency * 2 ** (1/1200) - frequency
}
const getCentDifference = (startingFrequency, newFrequency) => {
const frequencyDifference = newFrequency - startingFrequency
const cent = calculateCent(startingFrequency)
return frequencyDifference/cent
}
const generateCentsString = (centDifference) => {
console.log(centDifference);
const flatSharpChar = (centDifference > 0) ? '♯' : '♭'
return flatSharpChar + Math.abs(parseInt(centDifference)).toString().padStart(2, '0')
}
function startListening() {
navigator.mediaDevices.getUserMedia({ audio: true, video: false })
.then(stream => {
const audioContext = new (window.AudioContext || window.webkitAudioContext)();
const analyser = audioContext.createAnalyser();
const microphone = audioContext.createMediaStreamSource(stream);
const scriptProcessor = audioContext.createScriptProcessor(2048, 1, 1);
analyser.smoothingTimeConstant = 0.3;
analyser.fftSize = 2048;
microphone.connect(analyser);
analyser.connect(scriptProcessor);
scriptProcessor.connect(audioContext.destination);
scriptProcessor.onaudioprocess = () => {
const buffer = new Float32Array(analyser.fftSize);
analyser.getFloatTimeDomainData(buffer);
const pitch = autoCorrelate(buffer, audioContext.sampleRate);
if(pitch != -1){
document.querySelector('#analyze-pitch dd:nth-of-type(1)').textContent = pitch.toFixed(2)
document.querySelector('#analyze-pitch dd:nth-of-type(2)').innerHTML = generateCentsString(getCentDifference(newTone, pitch))
}
};
})
.catch(err => {
console.error('Error accessing microphone: ' + err);
});
}
function autoCorrelate(buffer, sampleRate) {
const SIZE = buffer.length;
const MAX_SAMPLES = Math.floor(SIZE / 2);
const MIN_SAMPLES = 0;
const GOOD_ENOUGH_CORRELATION = 0.9;
let bestOffset = -1;
let bestCorrelation = 0;
let rms = 0;
let foundGoodCorrelation = false;
let correlations = new Array(MAX_SAMPLES);
for (let i = 0; i < SIZE; i++) {
const val = buffer[i];
rms += val * val;
}
rms = Math.sqrt(rms / SIZE);
if (rms < 0.01) // not enough signal
return -1;
let lastCorrelation = 1;
for (let offset = MIN_SAMPLES; offset < MAX_SAMPLES; offset++) {
let correlation = 0;
for (let i = 0; i < MAX_SAMPLES; i++) {
correlation += Math.abs((buffer[i]) - (buffer[i + offset]));
}
correlation = 1 - (correlation / MAX_SAMPLES);
correlations[offset] = correlation;
if ((correlation > GOOD_ENOUGH_CORRELATION) && (correlation > lastCorrelation)) {
foundGoodCorrelation = true;
if (correlation > bestCorrelation) {
bestCorrelation = correlation;
bestOffset = offset;
}
} else if (foundGoodCorrelation) {
const shift = (correlations[bestOffset + 1] - correlations[bestOffset - 1]) / correlations[bestOffset];
return sampleRate / (bestOffset + (8 * shift));
}
lastCorrelation = correlation;
}
if (bestCorrelation > 0.01) {
return sampleRate / bestOffset;
}
return -1;
}