Spaces:
Paused
Paused
audio capture/playback/alarm
Browse files- web/src/audio.js +2 -9
web/src/audio.js
CHANGED
|
@@ -1,13 +1,8 @@
|
|
| 1 |
-
// PTT audio pipeline.
|
| 2 |
-
// Capture: AudioWorklet reads mic at 16 kHz, posts Int16 PCM chunks to the main
|
| 3 |
-
// thread, which sends them over the WebSocket. Playback: incoming PCM chunks are
|
| 4 |
-
// queued as AudioBuffers and played back-to-back.
|
| 5 |
-
|
| 6 |
const WORKLET_SRC = `
|
| 7 |
class PCMCapture extends AudioWorkletProcessor {
|
| 8 |
process(inputs) {
|
| 9 |
const input = inputs[0];
|
| 10 |
-
if (input && input[0]
|
| 11 |
const f32 = input[0];
|
| 12 |
const i16 = new Int16Array(f32.length);
|
| 13 |
for (let i = 0; i < f32.length; i++) {
|
|
@@ -35,12 +30,10 @@ export async function startCapture(onChunk) {
|
|
| 35 |
const url = URL.createObjectURL(new Blob([WORKLET_SRC], { type: 'application/javascript' }));
|
| 36 |
await ctx.audioWorklet.addModule(url);
|
| 37 |
URL.revokeObjectURL(url);
|
| 38 |
-
|
| 39 |
const source = ctx.createMediaStreamSource(stream);
|
| 40 |
const node = new AudioWorkletNode(ctx, 'pcm-capture');
|
| 41 |
node.port.onmessage = (e) => onChunk(e.data);
|
| 42 |
-
source.connect(node); //
|
| 43 |
-
|
| 44 |
return {
|
| 45 |
stop() {
|
| 46 |
node.port.onmessage = null;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
const WORKLET_SRC = `
|
| 2 |
class PCMCapture extends AudioWorkletProcessor {
|
| 3 |
process(inputs) {
|
| 4 |
const input = inputs[0];
|
| 5 |
+
if (input && input[0]) {
|
| 6 |
const f32 = input[0];
|
| 7 |
const i16 = new Int16Array(f32.length);
|
| 8 |
for (let i = 0; i < f32.length; i++) {
|
|
|
|
| 30 |
const url = URL.createObjectURL(new Blob([WORKLET_SRC], { type: 'application/javascript' }));
|
| 31 |
await ctx.audioWorklet.addModule(url);
|
| 32 |
URL.revokeObjectURL(url);
|
|
|
|
| 33 |
const source = ctx.createMediaStreamSource(stream);
|
| 34 |
const node = new AudioWorkletNode(ctx, 'pcm-capture');
|
| 35 |
node.port.onmessage = (e) => onChunk(e.data);
|
| 36 |
+
source.connect(node); // not connected to destination -> no local feedback
|
|
|
|
| 37 |
return {
|
| 38 |
stop() {
|
| 39 |
node.port.onmessage = null;
|