从 websocket 播放 OPUS 时的声音调度问题
Sound scheduling issue when playing OPUS from websocket
我正在尝试使用库 https://github.com/AnthumChris/opus-stream-decoder/
我有来自高质量麦克风的 OPUS 编码声音流(2 声道,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, samplesDecoded, 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, samplesDecoded, 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/AnthumChris/opus-stream-decoder/
我有来自高质量麦克风的 OPUS 编码声音流(2 声道,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, samplesDecoded, 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, samplesDecoded, sampleRate);
buffer.copyToChannel(left, 0);
buffer.copyToChannel(right, 1);
source.buffer = buffer;
source.connect(audioCtx.destination);
source.start(startTime);
startTime += buffer.duration;
}