从websocket播放OPUS时出现声音调度问题

问题描述

我正在尝试使用库https://github.com/AnthumChris/opus-stream-decoder/

我有来自高质量麦克风的OPUS编码声音流(2ch,48kHz)(但是我循环播放音乐来对此进行测试)。我知道它有效,因为如果我使用它,我会听到:

websocat --binary ws://third-i.local/api/sound - | mpv -

(它正在打开websocket并将其输出流式传输到mpv(mplayer))。

但是,当我在浏览器中播放时,每秒听到的声音很小。但是声音本身听起来不错(我相信这只是音乐的一小部分)。

这是我编写的在浏览器中监听的JS代码

let audioWorker: any;
let exampleSocket;
let opusDecoder: any;
let audioCtx: any;
let startTime = 0;
let counter = 0;

function startAudio() {
  /*
  const host = document.location.hostname;
  const scheme = document.location.protocol.startsWith("https") ? "wss" : "ws";
  const uri = `${scheme}://${host}/api/sound`;
  */
  const uri = "ws://third-i.local/api/sound";
  audioCtx = new AudioContext();
  startTime = 100 / 1000;
  exampleSocket = new WebSocket(uri);
  exampleSocket.binaryType = "arraybuffer";
  opusDecoder = new OpusstreamDecoder({onDecode});
  exampleSocket.onmessage = (event) => opusDecoder.ready.then(
    () => opusDecoder.decode(new Uint8Array(event.data))
  );
  exampleSocket.onclose = () => console.log("socket is closed!!");
}

function onDecode({left,right,samplesDecoded,sampleRate}: any) {
  const source = audioCtx.createBufferSource();
  const buffer = audioCtx.createBuffer(2,sampleRate);
  buffer.copytochannel(left,0);
  buffer.copytochannel(right,1);
  source.buffer = buffer;
  source.connect(audioCtx.destination);
  source.start(startTime);
  startTime += buffer.duration;
}

https://github.com/BigBoySystems/third-i-frontend/blob/play-audio/src/App.tsx#L54-L88

解决方法

计划的问题是由于在创建WebSocket的同时创建了AudioContext,从而将连接时间添加到AudioContext的计划中。

换句话说,当您创建AudioContext时,调度将立即开始,但是由于AudioContext是在创建WebSocket时创建的(仅开始连接),因此该调度会花费一定的时间连接到WebSocket以连接到上游并接收第一个字节。

这是您的固定代码:

let audioStreamSocket;
let opusDecoder: any;
let audioCtx: AudioContext;
let startTime: number;

function startAudio() {
  const host = document.location.hostname;
  const scheme = document.location.protocol.startsWith("https") ? "wss" : "ws";
  const uri = `${scheme}://${host}/api/sound`;
  audioStreamSocket = new WebSocket(uri);
  audioStreamSocket.binaryType = "arraybuffer";
  opusDecoder = new OpusStreamDecoder({ onDecode });
  audioStreamSocket.onmessage = (event) =>
    opusDecoder.ready.then(() => opusDecoder.decode(new Uint8Array(event.data)));
}

function onDecode({ left,right,samplesDecoded,sampleRate }: any) {
  if (audioCtx === undefined) {
    // See how we create the AudioContext only after some data has been received
    // and successfully decoded <=====================================
    console.log("Audio stream connected");
    audioCtx = new AudioContext();
    startTime = 0.1;
  }
  const source = audioCtx.createBufferSource();
  const buffer = audioCtx.createBuffer(2,sampleRate);
  buffer.copyToChannel(left,0);
  buffer.copyToChannel(right,1);
  source.buffer = buffer;
  source.connect(audioCtx.destination);
  source.start(startTime);
  startTime += buffer.duration;
}