|
| 1 | +# Local Audio Capture Example |
| 2 | + |
| 3 | +This example demonstrates how to capture audio from a local microphone and stream it to a LiveKit room while simultaneously playing back audio from other participants. It provides a complete bidirectional audio experience with real-time level monitoring. |
| 4 | + |
| 5 | +## Features |
| 6 | + |
| 7 | +- **Bidirectional Audio**: Capture from local microphone and play back remote participants |
| 8 | +- **Device Selection**: Choose specific input/output devices or use system defaults |
| 9 | +- **Real-time Level Meter**: Visual dB meter showing local microphone levels |
| 10 | +- **Audio Processing**: Echo cancellation, noise suppression, and auto gain control (enabled by default) |
| 11 | +- **Volume Control**: Adjustable playback volume for remote participants |
| 12 | +- **Audio Mixing**: Combines audio from multiple remote participants |
| 13 | +- **Format Support**: Handles F32, I16, and U16 sample formats |
| 14 | +- **Cross-platform**: Works on Windows, macOS, and Linux |
| 15 | + |
| 16 | +## Prerequisites |
| 17 | + |
| 18 | +1. **Rust**: Install Rust 1.70+ from [rustup.rs](https://rustup.rs/) |
| 19 | +2. **LiveKit Server**: Access to a LiveKit server instance |
| 20 | +3. **Audio Devices**: Working microphone and speakers/headphones |
| 21 | +4. **System Permissions**: Audio device access permissions |
| 22 | + |
| 23 | +### Platform-specific Requirements |
| 24 | + |
| 25 | +- **macOS**: Grant microphone permissions in System Preferences → Privacy & Security → Microphone |
| 26 | +- **Windows**: Ensure audio drivers are installed and microphone is not in use by other applications |
| 27 | +- **Linux**: May need ALSA or PulseAudio libraries (`sudo apt install libasound2-dev` on Ubuntu/Debian) |
| 28 | + |
| 29 | +## Setup |
| 30 | + |
| 31 | +1. **LiveKit Connection Details** (choose one method): |
| 32 | + |
| 33 | + **Option A: Environment Variables** |
| 34 | + ```bash |
| 35 | + export LIVEKIT_URL="wss://your-livekit-server.com" |
| 36 | + export LIVEKIT_API_KEY="your-api-key" |
| 37 | + export LIVEKIT_API_SECRET="your-api-secret" |
| 38 | + ``` |
| 39 | + |
| 40 | + **Option B: CLI Arguments** |
| 41 | + Pass connection details directly to the command (see examples below) |
| 42 | + |
| 43 | + **Note**: CLI arguments take precedence over environment variables. You can mix both methods - for example, set API credentials via environment variables but override the URL via CLI. |
| 44 | + |
| 45 | +2. **Build the Example**: |
| 46 | + |
| 47 | +```bash |
| 48 | +cd examples/local_audio |
| 49 | +cargo build --release |
| 50 | +``` |
| 51 | + |
| 52 | +## Usage |
| 53 | + |
| 54 | +### List Available Audio Devices |
| 55 | + |
| 56 | +```bash |
| 57 | +cargo run -- --list-devices |
| 58 | +``` |
| 59 | + |
| 60 | +Example output: |
| 61 | +``` |
| 62 | +Available Input Devices: |
| 63 | +─────────────────────────────────────────────────────────────── |
| 64 | +1. MacBook Pro Microphone |
| 65 | + ├─ Sample Rate: 8000-48000 Hz |
| 66 | + ├─ Channels: 1-2 |
| 67 | + └─ Formats: F32, I16 |
| 68 | +
|
| 69 | +2. USB Microphone |
| 70 | + ├─ Sample Rate: 44100-48000 Hz |
| 71 | + ├─ Channels: 1-2 |
| 72 | + └─ Formats: F32, I16 |
| 73 | +
|
| 74 | +Default Input Device: MacBook Pro Microphone |
| 75 | +
|
| 76 | +Available Output Devices: |
| 77 | +─────────────────────────────────────────────────────────────── |
| 78 | +1. MacBook Pro Speakers |
| 79 | + ├─ Sample Rate: 8000-48000 Hz |
| 80 | + ├─ Channels: 2 |
| 81 | + └─ Formats: F32, I16 |
| 82 | +
|
| 83 | +2. USB Headphones |
| 84 | + ├─ Sample Rate: 44100-48000 Hz |
| 85 | + ├─ Channels: 2 |
| 86 | + └─ Formats: F32, I16 |
| 87 | +
|
| 88 | +Default Output Device: MacBook Pro Speakers |
| 89 | +``` |
| 90 | + |
| 91 | +### Basic Usage |
| 92 | + |
| 93 | +Stream audio with default settings (using environment variables): |
| 94 | + |
| 95 | +```bash |
| 96 | +cargo run |
| 97 | +``` |
| 98 | + |
| 99 | +Using CLI arguments for connection details: |
| 100 | + |
| 101 | +```bash |
| 102 | +cargo run -- \ |
| 103 | + --url "wss://your-project.livekit.cloud" \ |
| 104 | + --api-key "your-api-key" \ |
| 105 | + --api-secret "your-api-secret" |
| 106 | +``` |
| 107 | + |
| 108 | +Join a specific room with custom identity: |
| 109 | + |
| 110 | +```bash |
| 111 | +cargo run -- \ |
| 112 | + --url "wss://your-project.livekit.cloud" \ |
| 113 | + --api-key "your-api-key" \ |
| 114 | + --api-secret "your-api-secret" \ |
| 115 | + --room-name "my-meeting" \ |
| 116 | + --identity "john-doe" |
| 117 | +``` |
| 118 | + |
| 119 | +### Advanced Configuration |
| 120 | + |
| 121 | +```bash |
| 122 | +cargo run -- \ |
| 123 | + --url "wss://your-project.livekit.cloud" \ |
| 124 | + --api-key "your-api-key" \ |
| 125 | + --api-secret "your-api-secret" \ |
| 126 | + --input-device "USB Microphone" \ |
| 127 | + --output-device "USB Headphones" \ |
| 128 | + --sample-rate 44100 \ |
| 129 | + --channels 2 \ |
| 130 | + --volume 0.8 \ |
| 131 | + --room-name "conference-room" |
| 132 | +``` |
| 133 | + |
| 134 | +### Capture-Only Mode |
| 135 | + |
| 136 | +Disable audio playback and only capture: |
| 137 | + |
| 138 | +```bash |
| 139 | +cargo run -- \ |
| 140 | + --url "wss://your-project.livekit.cloud" \ |
| 141 | + --api-key "your-api-key" \ |
| 142 | + --api-secret "your-api-secret" \ |
| 143 | + --no-playback |
| 144 | +``` |
| 145 | + |
| 146 | +## Command Line Options |
| 147 | + |
| 148 | +| Option | Description | Default | |
| 149 | +|--------|-------------|---------| |
| 150 | +| `--list-devices` | List available audio devices and exit | - | |
| 151 | +| `--input-device <NAME>` | Input device name | System default | |
| 152 | +| `--output-device <NAME>` | Output device name | System default | |
| 153 | +| `--sample-rate <HZ>` | Sample rate in Hz | 48000 | |
| 154 | +| `--channels <COUNT>` | Number of channels | 1 | |
| 155 | +| `--echo-cancellation` | Enable echo cancellation | true | |
| 156 | +| `--noise-suppression` | Enable noise suppression | true | |
| 157 | +| `--auto-gain-control` | Enable auto gain control | true | |
| 158 | +| `--no-playback` | Disable audio playback (capture only) | false | |
| 159 | +| `--volume <LEVEL>` | Playback volume (0.0 to 1.0) | 1.0 | |
| 160 | +| `--identity <NAME>` | LiveKit participant identity | "rust-audio-streamer" | |
| 161 | +| `--room-name <NAME>` | LiveKit room name | "audio-room" | |
| 162 | +| `--url <URL>` | LiveKit server URL | From LIVEKIT_URL env var | |
| 163 | +| `--api-key <KEY>` | LiveKit API key | From LIVEKIT_API_KEY env var | |
| 164 | +| `--api-secret <SECRET>` | LiveKit API secret | From LIVEKIT_API_SECRET env var | |
0 commit comments