我有这段代码,它获取麦克风的实时音频,将其转换为频率,最后显示时间与频率图。它工作正常。
我需要做什么?当我从麦克风获取音频时,录制的音频中有噪音,频率波动。
我需要消除/减少噪音/背景干扰并获得更平滑的输出 - 音频。
代码
import { PitchDetector } from "https://esm.sh/pitchy@3";
function updatePitch(analyserNode, detector, input, sampleRate) {
analyserNode.getFloatTimeDomainData(input);
const [pitch, clarity] = detector.findPitch(input, sampleRate);
Plotly.extendTraces('chart', {y: [[Math.round(pitch * 10) / 10]]}, [0])
document.getElementById("pitch").textContent = `${Math.round(pitch * 10) / 10
} Hz`;
document.getElementById("clarity").textContent = `${Math.round(
clarity * 100
)} %`;
window.setTimeout(
() => {
updatePitch(analyserNode, detector, input, sampleRate);
},
250
);
}
Plotly.plot(
'chart', [{
y: [Math.round(pitch * 10) / 10],
type: 'line'
}]
);
document.addEventListener("DOMContentLoaded", () => {
const audioContext = new window.AudioContext();
const analyserNode = audioContext.createAnalyser();
document
.getElementById("resume-button")
.addEventListener("click", () => audioContext.resume());
navigator.mediaDevices.getUserMedia({ audio: true }).then((stream) => {
audioContext.createMediaStreamSource(stream).connect(analyserNode);
const detector = PitchDetector.forFloat32Array(analyserNode.fftSize);
const input = new Float32Array(detector.inputLength);
updatePitch(analyserNode, detector, input, audioContext.sampleRate);
});
});