parent
07f13a4398
commit
bf41389360
@ -1,78 +1,78 @@
|
|||||||
document.querySelector('#analyze-pitch button').addEventListener('click', startListening);
|
document.querySelector('#analyze-pitch button').addEventListener('click', startListening);
|
||||||
|
|
||||||
function startListening() {
|
function startListening() {
|
||||||
navigator.mediaDevices.getUserMedia({ audio: true, video: false })
|
navigator.mediaDevices.getUserMedia({ audio: true, video: false })
|
||||||
.then(stream => {
|
.then(stream => {
|
||||||
const audioContext = new (window.AudioContext || window.webkitAudioContext)();
|
const audioContext = new (window.AudioContext || window.webkitAudioContext)();
|
||||||
const analyser = audioContext.createAnalyser();
|
const analyser = audioContext.createAnalyser();
|
||||||
const microphone = audioContext.createMediaStreamSource(stream);
|
const microphone = audioContext.createMediaStreamSource(stream);
|
||||||
const scriptProcessor = audioContext.createScriptProcessor(2048, 1, 1);
|
const scriptProcessor = audioContext.createScriptProcessor(2048, 1, 1);
|
||||||
|
|
||||||
analyser.smoothingTimeConstant = 0.3;
|
analyser.smoothingTimeConstant = 0.3;
|
||||||
analyser.fftSize = 2048;
|
analyser.fftSize = 2048;
|
||||||
|
|
||||||
microphone.connect(analyser);
|
microphone.connect(analyser);
|
||||||
analyser.connect(scriptProcessor);
|
analyser.connect(scriptProcessor);
|
||||||
scriptProcessor.connect(audioContext.destination);
|
scriptProcessor.connect(audioContext.destination);
|
||||||
|
|
||||||
scriptProcessor.onaudioprocess = () => {
|
scriptProcessor.onaudioprocess = () => {
|
||||||
const buffer = new Float32Array(analyser.fftSize);
|
const buffer = new Float32Array(analyser.fftSize);
|
||||||
analyser.getFloatTimeDomainData(buffer);
|
analyser.getFloatTimeDomainData(buffer);
|
||||||
const pitch = autoCorrelate(buffer, audioContext.sampleRate);
|
const pitch = autoCorrelate(buffer, audioContext.sampleRate);
|
||||||
document.querySelector('#analyze-pitch dd').textContent = pitch.toFixed(2);
|
document.querySelector('#analyze-pitch dd').textContent = pitch.toFixed(2);
|
||||||
};
|
};
|
||||||
})
|
})
|
||||||
.catch(err => {
|
.catch(err => {
|
||||||
console.error('Error accessing microphone: ' + err);
|
console.error('Error accessing microphone: ' + err);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
function autoCorrelate(buffer, sampleRate) {
|
function autoCorrelate(buffer, sampleRate) {
|
||||||
const SIZE = buffer.length;
|
const SIZE = buffer.length;
|
||||||
const MAX_SAMPLES = Math.floor(SIZE / 2);
|
const MAX_SAMPLES = Math.floor(SIZE / 2);
|
||||||
const MIN_SAMPLES = 0;
|
const MIN_SAMPLES = 0;
|
||||||
const GOOD_ENOUGH_CORRELATION = 0.9;
|
const GOOD_ENOUGH_CORRELATION = 0.9;
|
||||||
|
|
||||||
let bestOffset = -1;
|
let bestOffset = -1;
|
||||||
let bestCorrelation = 0;
|
let bestCorrelation = 0;
|
||||||
let rms = 0;
|
let rms = 0;
|
||||||
let foundGoodCorrelation = false;
|
let foundGoodCorrelation = false;
|
||||||
let correlations = new Array(MAX_SAMPLES);
|
let correlations = new Array(MAX_SAMPLES);
|
||||||
|
|
||||||
for (let i = 0; i < SIZE; i++) {
|
for (let i = 0; i < SIZE; i++) {
|
||||||
const val = buffer[i];
|
const val = buffer[i];
|
||||||
rms += val * val;
|
rms += val * val;
|
||||||
}
|
}
|
||||||
rms = Math.sqrt(rms / SIZE);
|
rms = Math.sqrt(rms / SIZE);
|
||||||
|
|
||||||
if (rms < 0.01) // not enough signal
|
if (rms < 0.01) // not enough signal
|
||||||
return -1;
|
return -1;
|
||||||
|
|
||||||
let lastCorrelation = 1;
|
let lastCorrelation = 1;
|
||||||
for (let offset = MIN_SAMPLES; offset < MAX_SAMPLES; offset++) {
|
for (let offset = MIN_SAMPLES; offset < MAX_SAMPLES; offset++) {
|
||||||
let correlation = 0;
|
let correlation = 0;
|
||||||
|
|
||||||
for (let i = 0; i < MAX_SAMPLES; i++) {
|
for (let i = 0; i < MAX_SAMPLES; i++) {
|
||||||
correlation += Math.abs((buffer[i]) - (buffer[i + offset]));
|
correlation += Math.abs((buffer[i]) - (buffer[i + offset]));
|
||||||
}
|
}
|
||||||
correlation = 1 - (correlation / MAX_SAMPLES);
|
correlation = 1 - (correlation / MAX_SAMPLES);
|
||||||
correlations[offset] = correlation;
|
correlations[offset] = correlation;
|
||||||
|
|
||||||
if ((correlation > GOOD_ENOUGH_CORRELATION) && (correlation > lastCorrelation)) {
|
if ((correlation > GOOD_ENOUGH_CORRELATION) && (correlation > lastCorrelation)) {
|
||||||
foundGoodCorrelation = true;
|
foundGoodCorrelation = true;
|
||||||
if (correlation > bestCorrelation) {
|
if (correlation > bestCorrelation) {
|
||||||
bestCorrelation = correlation;
|
bestCorrelation = correlation;
|
||||||
bestOffset = offset;
|
bestOffset = offset;
|
||||||
}
|
}
|
||||||
} else if (foundGoodCorrelation) {
|
} else if (foundGoodCorrelation) {
|
||||||
const shift = (correlations[bestOffset + 1] - correlations[bestOffset - 1]) / correlations[bestOffset];
|
const shift = (correlations[bestOffset + 1] - correlations[bestOffset - 1]) / correlations[bestOffset];
|
||||||
return sampleRate / (bestOffset + (8 * shift));
|
return sampleRate / (bestOffset + (8 * shift));
|
||||||
}
|
}
|
||||||
lastCorrelation = correlation;
|
lastCorrelation = correlation;
|
||||||
}
|
}
|
||||||
if (bestCorrelation > 0.01) {
|
if (bestCorrelation > 0.01) {
|
||||||
return sampleRate / bestOffset;
|
return sampleRate / bestOffset;
|
||||||
}
|
}
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in new issue