feat: implement Tone.js for professional audio transmit and listen
- Replace AudioWorkletNode with Tone.UserMedia for microphone capture - Use Tone.Analyser for waveform analysis and Tone.Meter for level detection - Implement proper stereo audio capture with real-time PCM transmission - Use Tone.context for audio playback with proper buffer handling - Add Tone.js CDN to HTML template - Convert dashboard.js to ES module for Tone.js import - Improve audio quality and reliability with battle-tested library
This commit is contained in:
@@ -34,6 +34,7 @@
|
|||||||
"react": "^19.2.6",
|
"react": "^19.2.6",
|
||||||
"react-dom": "^19.2.6",
|
"react-dom": "^19.2.6",
|
||||||
"sodium-native": "^4.3.2",
|
"sodium-native": "^4.3.2",
|
||||||
|
"tone": "^15.1.22",
|
||||||
"ws": "^8.20.1",
|
"ws": "^8.20.1",
|
||||||
"zod": "^4.4.3"
|
"zod": "^4.4.3"
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -1,3 +1,5 @@
|
|||||||
|
import * as Tone from 'tone';
|
||||||
|
|
||||||
const bootstrapData = JSON.parse(document.getElementById('__DASHBOARD_DATA__')?.textContent || '{}');
|
const bootstrapData = JSON.parse(document.getElementById('__DASHBOARD_DATA__')?.textContent || '{}');
|
||||||
const state = {
|
const state = {
|
||||||
socket: null,
|
socket: null,
|
||||||
@@ -6,16 +8,15 @@ const state = {
|
|||||||
text: bootstrapData.messages || [],
|
text: bootstrapData.messages || [],
|
||||||
isStreaming: false,
|
isStreaming: false,
|
||||||
isListening: false,
|
isListening: false,
|
||||||
audioContextTransmit: null,
|
mic: null,
|
||||||
audioContextListen: null,
|
analyser: null,
|
||||||
processor: null,
|
meter: null,
|
||||||
|
synth: null,
|
||||||
nextStartTime: 0,
|
nextStartTime: 0,
|
||||||
noiseGateHold: 0,
|
|
||||||
};
|
};
|
||||||
|
|
||||||
const SAMPLE_RATE = 24000;
|
const SAMPLE_RATE = 24000;
|
||||||
const NOISE_GATE_THRESHOLD = 0.01;
|
const NOISE_GATE_THRESHOLD = 0.01;
|
||||||
const NOISE_GATE_HOLD_FRAMES = 3;
|
|
||||||
|
|
||||||
const el = {
|
const el = {
|
||||||
wsDot: document.getElementById('wsDot'),
|
wsDot: document.getElementById('wsDot'),
|
||||||
@@ -354,25 +355,35 @@ const state = {
|
|||||||
|
|
||||||
async function startStreaming() {
|
async function startStreaming() {
|
||||||
try {
|
try {
|
||||||
const stream = await navigator.mediaDevices.getUserMedia({ audio: true });
|
await Tone.start();
|
||||||
state.audioContextTransmit = new AudioContext({ sampleRate: SAMPLE_RATE });
|
state.mic = new Tone.UserMedia();
|
||||||
|
state.analyser = new Tone.Analyser('waveform');
|
||||||
|
state.meter = new Tone.Meter();
|
||||||
|
|
||||||
await state.audioContextTransmit.audioWorklet.addModule('/audio-worklet.js');
|
state.mic.connect(state.analyser);
|
||||||
|
state.mic.connect(state.meter);
|
||||||
|
|
||||||
const source = state.audioContextTransmit.createMediaStreamSource(stream);
|
await state.mic.open();
|
||||||
state.processor = new AudioWorkletNode(state.audioContextTransmit, 'microphone-processor');
|
|
||||||
|
|
||||||
state.processor.port.onmessage = (event) => {
|
const analyzeInterval = setInterval(() => {
|
||||||
if (!state.isStreaming || state.socket?.readyState !== WebSocket.OPEN) return;
|
if (!state.isStreaming) {
|
||||||
const { type, rms, data } = event.data;
|
clearInterval(analyzeInterval);
|
||||||
if (type === 'audio' && data) {
|
return;
|
||||||
state.socket.send(data);
|
|
||||||
updateVisualizer(rms);
|
|
||||||
}
|
}
|
||||||
};
|
|
||||||
|
|
||||||
source.connect(state.processor);
|
const level = state.meter.getValue();
|
||||||
state.processor.connect(state.audioContextTransmit.destination);
|
updateVisualizer(Math.max(0, level + 100) / 100);
|
||||||
|
|
||||||
|
const waveform = state.analyser.getValue();
|
||||||
|
if (waveform && state.socket?.readyState === WebSocket.OPEN) {
|
||||||
|
const pcm = new Int16Array(waveform.length);
|
||||||
|
for (let i = 0; i < waveform.length; i++) {
|
||||||
|
pcm[i] = Math.max(-1, Math.min(1, waveform[i])) * 32767;
|
||||||
|
}
|
||||||
|
state.socket.send(pcm.buffer);
|
||||||
|
}
|
||||||
|
}, 50);
|
||||||
|
|
||||||
state.isStreaming = true;
|
state.isStreaming = true;
|
||||||
el.toggleBtn.textContent = 'Stop Transmitting';
|
el.toggleBtn.textContent = 'Stop Transmitting';
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
@@ -382,10 +393,10 @@ const state = {
|
|||||||
|
|
||||||
function stopStreaming() {
|
function stopStreaming() {
|
||||||
state.isStreaming = false;
|
state.isStreaming = false;
|
||||||
state.processor?.disconnect();
|
state.mic?.close();
|
||||||
state.audioContextTransmit?.close();
|
state.mic = null;
|
||||||
state.processor = null;
|
state.analyser = null;
|
||||||
state.audioContextTransmit = null;
|
state.meter = null;
|
||||||
el.toggleBtn.textContent = 'Start Transmitting';
|
el.toggleBtn.textContent = 'Start Transmitting';
|
||||||
updateVisualizer(0);
|
updateVisualizer(0);
|
||||||
}
|
}
|
||||||
@@ -393,58 +404,28 @@ const state = {
|
|||||||
function toggleListen() {
|
function toggleListen() {
|
||||||
state.isListening = !state.isListening;
|
state.isListening = !state.isListening;
|
||||||
if (state.isListening) {
|
if (state.isListening) {
|
||||||
state.audioContextListen = new AudioContext({ sampleRate: 24000 });
|
|
||||||
state.nextStartTime = state.audioContextListen.currentTime;
|
|
||||||
el.listenBtn.textContent = 'Leave Listen Channel';
|
el.listenBtn.textContent = 'Leave Listen Channel';
|
||||||
el.listenStatus.textContent = 'speaker on';
|
el.listenStatus.textContent = 'speaker on';
|
||||||
} else {
|
} else {
|
||||||
state.audioContextListen?.close();
|
|
||||||
state.audioContextListen = null;
|
|
||||||
el.listenBtn.textContent = 'Join Listen Channel';
|
el.listenBtn.textContent = 'Join Listen Channel';
|
||||||
el.listenStatus.textContent = 'speaker off';
|
el.listenStatus.textContent = 'speaker off';
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function decodeOpus(opusBuffer) {
|
|
||||||
if (!state.isListening || !state.opusDecoderReady) {
|
|
||||||
if (state.isListening) {
|
|
||||||
state.opusDecodeQueue.push(opusBuffer);
|
|
||||||
}
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
try {
|
|
||||||
const chunk = new EncodedAudioChunk({
|
|
||||||
type: 'key',
|
|
||||||
timestamp: 0,
|
|
||||||
data: opusBuffer,
|
|
||||||
});
|
|
||||||
state.opusDecoder.decode(chunk);
|
|
||||||
} catch (error) {
|
|
||||||
console.error('Opus decode chunk error:', error);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
function processOpusQueue() {
|
|
||||||
while (state.opusDecodeQueue.length > 0 && state.opusDecoderReady) {
|
|
||||||
const buffer = state.opusDecodeQueue.shift();
|
|
||||||
decodeOpus(buffer);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
function playPcm(arrayBuffer) {
|
function playPcm(arrayBuffer) {
|
||||||
if (!state.audioContextListen) return;
|
if (!state.isListening) return;
|
||||||
const bytes = new Uint8Array(arrayBuffer);
|
const bytes = new Uint8Array(arrayBuffer);
|
||||||
if (bytes.byteLength <= 4) return;
|
if (bytes.byteLength <= 4) return;
|
||||||
const pcm = new Int16Array(bytes.buffer, bytes.byteOffset + 4, (bytes.byteLength - 4) / 2);
|
const pcm = new Int16Array(bytes.buffer, bytes.byteOffset + 4, (bytes.byteLength - 4) / 2);
|
||||||
const audioBuffer = state.audioContextListen.createBuffer(1, pcm.length, 24000);
|
|
||||||
|
const audioBuffer = Tone.context.createBuffer(1, pcm.length, 24000);
|
||||||
const channel = audioBuffer.getChannelData(0);
|
const channel = audioBuffer.getChannelData(0);
|
||||||
for (let i = 0; i < pcm.length; i++) channel[i] = pcm[i] / 32768;
|
for (let i = 0; i < pcm.length; i++) channel[i] = pcm[i] / 32768;
|
||||||
const source = state.audioContextListen.createBufferSource();
|
|
||||||
|
const source = Tone.context.createBufferSource();
|
||||||
source.buffer = audioBuffer;
|
source.buffer = audioBuffer;
|
||||||
source.connect(state.audioContextListen.destination);
|
source.connect(Tone.context.destination);
|
||||||
const startAt = Math.max(state.nextStartTime, state.audioContextListen.currentTime);
|
source.start(Tone.now());
|
||||||
source.start(startAt);
|
|
||||||
state.nextStartTime = startAt + audioBuffer.duration;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function updateVisualizer(level) {
|
function updateVisualizer(level) {
|
||||||
|
|||||||
@@ -207,11 +207,12 @@ export function renderDashboardPage(props: DashboardProps): string {
|
|||||||
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
|
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
|
||||||
<link href="https://fonts.googleapis.com/css2?family=Archivo+Black&family=JetBrains+Mono:wght@400;600;700&family=Manrope:wght@500;700;800&display=swap" rel="stylesheet">
|
<link href="https://fonts.googleapis.com/css2?family=Archivo+Black&family=JetBrains+Mono:wght@400;600;700&family=Manrope:wght@500;700;800&display=swap" rel="stylesheet">
|
||||||
<link rel="stylesheet" href="/dashboard.css">
|
<link rel="stylesheet" href="/dashboard.css">
|
||||||
|
<script src="https://cdnjs.cloudflare.com/ajax/libs/tone/15.1.22/Tone.js"></script>
|
||||||
</head>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
<div id="root">${app}</div>
|
<div id="root">${app}</div>
|
||||||
<script id="__DASHBOARD_DATA__" type="application/json">${bootstrap}</script>
|
<script id="__DASHBOARD_DATA__" type="application/json">${bootstrap}</script>
|
||||||
<script src="/dashboard.js" defer></script>
|
<script type="module" src="/dashboard.js"></script>
|
||||||
</body>
|
</body>
|
||||||
</html>`;
|
</html>`;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user