parent
72a10ad32b
commit
b7416c7c2b
2 changed files with 46 additions and 10 deletions
|
|
@ -785,7 +785,8 @@ async function buildAudioPipeline(rawStream) {
|
|||
analyser.fftSize = 1024;
|
||||
processedSource.connect(analyser);
|
||||
|
||||
const timeData = new Float32Array(analyser.fftSize);
|
||||
const supportsFloatTimeDomain = typeof analyser.getFloatTimeDomainData === 'function';
|
||||
const timeData = supportsFloatTimeDomain ? new Float32Array(analyser.fftSize) : new Uint8Array(analyser.fftSize);
|
||||
let localIsSpeaking = false;
|
||||
let speechFrames = 0;
|
||||
let silenceFrames = 0;
|
||||
|
|
@ -795,12 +796,23 @@ async function buildAudioPipeline(rawStream) {
|
|||
const checkVolume = () => {
|
||||
if (!state.voice.audioContext || state.voice.audioContext.state === 'closed') return;
|
||||
|
||||
analyser.getFloatTimeDomainData(timeData);
|
||||
try {
|
||||
if (supportsFloatTimeDomain) {
|
||||
analyser.getFloatTimeDomainData(timeData);
|
||||
} else {
|
||||
analyser.getByteTimeDomainData(timeData);
|
||||
}
|
||||
} catch (err) {
|
||||
setTimeout(checkVolume, 60);
|
||||
return;
|
||||
}
|
||||
|
||||
let sumSquares = 0;
|
||||
let peak = 0;
|
||||
for (let i = 0; i < timeData.length; i++) {
|
||||
const v = timeData[i];
|
||||
const v = supportsFloatTimeDomain
|
||||
? timeData[i]
|
||||
: (timeData[i] - 128) / 128;
|
||||
sumSquares += v * v;
|
||||
const abs = Math.abs(v);
|
||||
if (abs > peak) peak = abs;
|
||||
|
|
@ -868,7 +880,13 @@ async function createLocalVoiceStream() {
|
|||
video: false,
|
||||
};
|
||||
const rawStream = await navigator.mediaDevices.getUserMedia(constraints);
|
||||
const localStream = await buildAudioPipeline(rawStream);
|
||||
let localStream = rawStream;
|
||||
try {
|
||||
localStream = await buildAudioPipeline(rawStream);
|
||||
} catch (err) {
|
||||
console.warn('voice audio pipeline failed, using raw mic stream', err);
|
||||
state.voice.activeNoiseFilter = 'off';
|
||||
}
|
||||
state.voice.rawStream = rawStream;
|
||||
state.voice.localStream = localStream;
|
||||
if (state.voice.muted) {
|
||||
|
|
@ -1016,7 +1034,7 @@ async function joinVoice() {
|
|||
try {
|
||||
await createLocalVoiceStream();
|
||||
} catch (err) {
|
||||
console.error("microphone denied", err);
|
||||
console.error('failed to initialize local voice stream', err);
|
||||
return;
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue