> ## Documentation Index
> Fetch the complete documentation index at: https://docs.tryhamsa.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Audio: levels, visualizers and capture

> Read the visitor's and the agent's audio levels, draw a visualizer, and capture the call's audio with the Web SDK to record it or send it to another service.

## Audio levels

`getInputVolume()` is how loud the visitor's microphone is right now, from 0 to 1. It returns 0 when there's no call. Use it for a simple "you're being heard" light:

```javascript theme={null}
setInterval(() => {
  micLight.classList.toggle("on", agent.getInputVolume() > 0.1);
}, 100);
```

For the agent's sound, use the frequencies below. (`getOutputVolume()` is the volume you set with `setVolume()`, not how loud the agent is right now.)

## Draw a visualizer

`getInputByteFrequencyData()` (the visitor) and `getOutputByteFrequencyData()` (the agent) return the sound's frequencies as a `Uint8Array`, each value from 0 to 255. They return an empty array when there's no call.

```javascript theme={null}
const canvas = document.getElementById("visualizer");
const ctx = canvas.getContext("2d");

function draw() {
  const bins = agent.getOutputByteFrequencyData();
  ctx.clearRect(0, 0, canvas.width, canvas.height);
  const width = canvas.width / (bins.length || 1);
  bins.forEach((value, i) => {
    const height = (value / 255) * canvas.height;
    ctx.fillRect(i * width, canvas.height - height, width - 1, height);
  });
  requestAnimationFrame(draw);
}

draw(); // empty until a call starts
```

## Capture call audio

Capture the agent's voice, the visitor's, or both, to record the call or send it to another service as it happens. There are three ways, from simplest to most control.

**With `onAudioData`**, the agent's voice from the start of the call, in `opus-webm`, every 100 ms:

```javascript theme={null}
agent.start({
  agentId: "YOUR_AGENT_ID",
  onAudioData: (audioData) => socket.send(audioData),
});
```

**With `captureAudio`**, choosing what and how, from the start of the call:

```javascript theme={null}
agent.start({
  agentId: "YOUR_AGENT_ID",
  captureAudio: {
    source: "both",
    format: "pcm-f32",
    onData: (audioData, metadata) => {
      if (metadata.source === "agent") processAgent(audioData);
      else processVisitor(audioData);
    },
  },
});
```

**With `enableAudioCapture()`**, at any time during the call, and `disableAudioCapture()` to stop:

```javascript theme={null}
agent.on("callStarted", () => {
  if (visitorAgreedToRecording) {
    agent.enableAudioCapture({
      source: "both",
      format: "opus-webm",
      callback: (audioData, metadata) => recorder.add(audioData, metadata),
    });
  }
});

agent.disableAudioCapture();
```

`enableAudioCapture()` throws if there's no call yet. If you pass both `captureAudio` and `onAudioData`, `captureAudio` is used.

### Capture options

| Option | What it does | Default |
| - | - | - |
| `source` | `agent`, `user` (the visitor) or `both` | `agent` |
| `format` | `opus-webm`, `pcm-f32` or `pcm-i16` (below) | `opus-webm` |
| `chunkSize` | How often you get a chunk, in milliseconds, with `opus-webm` | `100` |
| `bufferSize` | How many samples each chunk has, with the PCM formats | `4096` |
| `trackSourceFilter` | `microphone`, `screen_share` or `all` | `microphone` |
| `onData` or `callback` | Your function, called with `(audioData, metadata)` for each chunk | |

### Formats

| Format | `audioData` is | Use it to |
| - | - | - |
| `opus-webm` | an `ArrayBuffer` of Opus audio in WebM | Record the call or send it on. It's small. |
| `pcm-f32` | a `Float32Array`, values from -1 to 1, 16 kHz mono | Analyse or process the sound yourself. |
| `pcm-i16` | an `Int16Array`, values from -32768 to 32767, 16 kHz mono | Send it to a service that expects 16-bit PCM. |

### What comes with each chunk

| Field | What it is |
| - | - |
| `source` | `agent` or `user`. |
| `participant` | The identity of whoever the audio is from. |
| `trackId` | The audio track it came from. |
| `trackSource` | Which source, such as `microphone`. |
| `format` | The chunk's format. |
| `timestamp` | When it was captured, in milliseconds. |
| `sampleRate`, `channels` | With the PCM formats. |

Recording someone usually needs their agreement. Ask before you capture the visitor's audio.

## Related

> [card] [During a call](https://docs.tryhamsa.com/developers/sdks/web-sdk/during-a-call.md)
>
> Mute the microphone and set the agent's volume.

> [card] [Events](https://docs.tryhamsa.com/developers/sdks/web-sdk/events.md)
>
> Transcripts and the call's numbers.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.