# Audio plumbing

> Deepgram workshops › UC Berkeley AI workshop 2026 › Audio plumbing
> Web version: https://workshops.deepgram.com/events/uc-berkeley-2026/audio
> About 6 min.

Audio bytes flow in both directions. The agent's speech arrives over the WebSocket and needs somewhere to play. Your microphone audio arrives from the OS and needs to go out to Deepgram.

### Open the speaker stream first {#speaker-first}

**Goal:** Play the agent's voice through your speaker and stream your microphone to Deepgram.

The libraries that talk to the operating system differ by language:

- **Python** uses [`sounddevice`](https://python-sounddevice.readthedocs.io/), which ships PortAudio binaries inside its Python wheels. Most attendees won't need to install anything system-level on macOS, Windows, or desktop Linux.
- **JavaScript** uses [`mic`](https://www.npmjs.com/package/mic) for input and [`speaker`](https://www.npmjs.com/package/speaker) for output. `mic` calls `sox` (macOS, Windows) or `arecord` (Linux), so install those first. See the [Setup](/uc-berkeley-2026/setup#install) lesson.

Open the speaker before you send the agent settings, so it's ready for the agent's greeting:

@tab python Python
```python
import sounddevice as sd

SAMPLE_RATE = 24000
CHANNELS = 1
DTYPE = "int16"  # linear16 == int16 PCM

speaker = sd.RawOutputStream(
    samplerate=SAMPLE_RATE,
    channels=CHANNELS,
    dtype=DTYPE,
)
speaker.start()
```

@tab javascript JavaScript
```javascript
import Speaker from "speaker";

const SAMPLE_RATE = 24000;
const CHANNELS = 1;
const BIT_DEPTH = 16;

const speaker = new Speaker({
  channels:   CHANNELS,
  bitDepth:   BIT_DEPTH,
  sampleRate: SAMPLE_RATE,
  signed:     true,
});
```
:::

Inside the message handler, send agent audio bytes straight to the speaker:

@tab python Python
```python
if isinstance(message, bytes):
    speaker.write(message)
    return
```

@tab javascript JavaScript
```javascript
if (Buffer.isBuffer(message)) {
  speaker.write(message);
  return;
}
```
:::

You need no decoding and no buffering of your own. The agent gives you bytes in your declared output format, and the speaker plays them.

**You should see:** the agent's greeting play through your speaker once the settings are applied. If it stays silent, check the troubleshooting list below.

### Open the mic stream after `SettingsApplied` {#mic-after-settings}

Open the microphone and forward each chunk of audio to Deepgram:

@tab python Python
```python
def microphone_callback(indata, _frames, _time_info, _status):
    agent.send_media(bytes(indata))

microphone = sd.RawInputStream(
    samplerate=SAMPLE_RATE,
    channels=CHANNELS,
    dtype=DTYPE,
    callback=microphone_callback,
)
microphone.start()
```

@tab javascript JavaScript
```javascript
import micFactory from "mic";

const microphone = micFactory({
  rate:     String(SAMPLE_RATE),
  channels: String(CHANNELS),
  bitwidth: String(BIT_DEPTH),
  encoding: "signed-integer",
  endian:   "little",
  device:   "default",
});

const micStream = microphone.getAudioStream();
micStream.on("data", (chunk) => agent.sendMedia(chunk));

microphone.start();
```
:::

Each chunk goes to Deepgram through the agent's `send_media` (Python) or `sendMedia` (JavaScript) call. The Voice Agent handles all the buffering and word-boundary detection on its side, so you don't need to chunk to phrase boundaries or pre-process.

Start the mic *after* `SettingsApplied`, so you don't send audio while the agent is still configuring itself.

**You should see:** the agent answer you once the mic is open, with `[user]` and `[assistant]` lines in your terminal. If you see a device error, check the troubleshooting list below.

### Clean shutdown on Ctrl+C {#cleanup}

The program runs until you press Ctrl+C. Stop both streams cleanly on shutdown:

@tab python Python
```python
try:
    while True:
        time.sleep(0.1)
except KeyboardInterrupt:
    print("\nShutting down...")
finally:
    microphone.stop()
    speaker.stop()
```

@tab javascript JavaScript
```javascript
const shutdown = () => {
  console.log("\nShutting down...");
  microphone.stop();
  speaker.end();
  process.exit(0);
};

process.on("SIGINT", shutdown);
process.on("SIGTERM", shutdown);
```
:::

Stopping the streams releases the underlying audio handles. It isn't strictly necessary on process exit, but keep the habit. The pattern matters much more when the voice agent is embedded in a longer-running application.

### Troubleshooting audio devices {#troubleshooting}

If the audio library can't find an input or output device, you'll see an error on startup. Check these common causes:

- **macOS:** the terminal doesn't have microphone permission yet. Run the script once, accept the system prompt, and try again.
- **WSL:** no native audio routing. See the [WSL bridge step in Setup](/uc-berkeley-2026/setup#wsl).
- **Linux (JavaScript flow):** `sox` or `arecord` isn't installed. Run `sudo apt install -y sox alsa-utils`.
- **Headless Linux server:** there is no audio device. Pipe audio from a file or another source instead.
