gpt4 book ai didi

javascript - 当检测到静音(JS)时,如何提取前面的音频(来自麦克风)作为缓冲区?

转载 作者:IT老高 更新时间:2023-10-28 23:00:01 33 4
gpt4 key购买 nike

我正在使用带有 NodeJS 后端的 Google Cloud API for Speech-to-text。该应用程序需要能够监听语音命令,并将它们作为缓冲区传输到后端。为此,我需要在检测到静音时发送前面音频的缓冲区。

任何帮助将不胜感激。包括下面的js代码

 if (!navigator.getUserMedia)
navigator.getUserMedia = navigator.getUserMedia || navigator.webkitGetUserMedia ||
navigator.mozGetUserMedia || navigator.msGetUserMedia;

if (navigator.getUserMedia) {
navigator.getUserMedia({audio: true}, success, function (e) {
alert('Error capturing audio.');
});
} else alert('getUserMedia not supported in this browser.');

var recording = false;

window.startRecording = function () {
recording = true;
};

window.stopRecording = function () {
recording = false;
// window.Stream.end();
};

function success(e) {
audioContext = window.AudioContext || window.webkitAudioContext;
context = new audioContext();

// the sample rate is in context.sampleRate
audioInput = context.createMediaStreamSource(e);

var bufferSize = 4096;
recorder = context.createScriptProcessor(bufferSize, 1, 1);

recorder.onaudioprocess = function (e) {
if (!recording) return;
console.log('recording');
var left = e.inputBuffer.getChannelData(0);
console.log(convertoFloat32ToInt16(left));
};

audioInput.connect(recorder);
recorder.connect(context.destination);
}

最佳答案

我不太确定问题中到底要问什么,所以这个答案只是为了提供一种检测音频流中静音的方法。


要检测音频流中的静音,您可以使用 AudioAnalyser Node ,您将在该 Node 上定期调用 getByteFrequencyData 方法,并检查在给定时间内是否有声音高于您的预期水平。

您可以直接使用 AnalyserNode 的 minDecibels 属性设置阈值级别。

function detectSilence(
stream,
onSoundEnd = _=>{},
onSoundStart = _=>{},
silence_delay = 500,
min_decibels = -80
) {
const ctx = new AudioContext();
const analyser = ctx.createAnalyser();
const streamNode = ctx.createMediaStreamSource(stream);
streamNode.connect(analyser);
analyser.minDecibels = min_decibels;

const data = new Uint8Array(analyser.frequencyBinCount); // will hold our data
let silence_start = performance.now();
let triggered = false; // trigger only once per silence event

function loop(time) {
requestAnimationFrame(loop); // we'll loop every 60th of a second to check
analyser.getByteFrequencyData(data); // get current data
if (data.some(v => v)) { // if there is data above the given db limit
if(triggered){
triggered = false;
onSoundStart();
}
silence_start = time; // set it to now
}
if (!triggered && time - silence_start > silence_delay) {
onSoundEnd();
triggered = true;
}
}
loop();
}

function onSilence() {
console.log('silence');
}
function onSpeak() {
console.log('speaking');
}

navigator.mediaDevices.getUserMedia({
audio: true
})
.then(stream => {
detectSilence(stream, onSilence, onSpeak);
// do something else with the stream
})
.catch(console.error);

还有 as a fiddle因为 stackSnippets 可能会阻止 gUM。

关于javascript - 当检测到静音(JS)时,如何提取前面的音频(来自麦克风)作为缓冲区?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46543341/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com