Skip to content

Commit 2ba2504

Browse files
xianshijing-lkclaudegithub-actions[bot]
authored
Sxian/clt 2919/integrate platform audio (#669)
Co-authored-by: Claude Opus 4.5 <noreply@anthropic.com> Co-authored-by: github-actions <41898282+github-actions[bot]@users.noreply.github.com>
1 parent 25774bb commit 2ba2504

8 files changed

Lines changed: 1713 additions & 140 deletions

File tree

examples/README.md

Lines changed: 301 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,301 @@
1+
# LiveKit Python SDK Examples
2+
3+
This directory contains examples demonstrating various features of the LiveKit Python SDK.
4+
5+
## Prerequisites
6+
7+
Set the following environment variables:
8+
9+
```bash
10+
export LIVEKIT_URL=ws://localhost:7880
11+
export LIVEKIT_API_KEY=devkey
12+
export LIVEKIT_API_SECRET=secret
13+
```
14+
15+
## Examples Overview
16+
17+
| Example | Description |
18+
|---------|-------------|
19+
| [basic_room.py](#basic_roompy) | Room connection with PlatformAudio and synthetic audio modes |
20+
| [room_example.py](#room_examplepy) | Basic room connection and event handling |
21+
| [api.py](#apipy) | Room management via LiveKit API |
22+
| [e2ee.py](#e2eepy) | End-to-end encryption demo |
23+
| [rpc.py](#rpcpy) | Remote Procedure Call between participants |
24+
| [multiple_connections.py](#multiple_connectionspy) | Sequential connections for sync frameworks |
25+
| [participant_attributes.py](#participant_attributespy) | Dynamic participant attributes |
26+
| [publish_wave.py](#publish_wavepy) | Publish sine wave audio |
27+
| [publish_hue.py](#publish_huepy) | Publish color-cycling video |
28+
| [play_audio_stream.py](#play_audio_streampy) | Play received audio with sounddevice |
29+
| [webhook.py](#webhookpy) | Webhook event handling |
30+
| [agent_dispatch.py](#agent_dispatchpy) | Manual agent dispatch |
31+
32+
### Subdirectories
33+
34+
| Directory | Description |
35+
|-----------|-------------|
36+
| [face_landmark/](face_landmark/) | Facial landmark detection using MediaPipe |
37+
| [video-stream/](video-stream/) | Video/audio sync with AVSynchronizer |
38+
| [data_tracks/](data_tracks/) | Data channel examples |
39+
| [data-streams/](data-streams/) | Data streaming examples |
40+
| [local_audio/](local_audio/) | Local audio capture with dB metering |
41+
42+
---
43+
44+
## basic_room.py
45+
46+
The most comprehensive example, demonstrating room connection with audio capabilities. It showcases two audio capture modes that can be used independently or combined.
47+
48+
### Quick Start
49+
50+
```bash
51+
# List available audio devices
52+
python basic_room.py --list-devices
53+
54+
# Connect with PlatformAudio (recommended)
55+
python basic_room.py --platform-audio --room my-room
56+
57+
# Connect with specific devices
58+
python basic_room.py --platform-audio --mic-id "device-guid" --speaker-id "device-guid"
59+
60+
# Publish a WAV file
61+
python basic_room.py --file audio.wav --room my-room
62+
63+
# Mix microphone + WAV file (both modes together)
64+
python basic_room.py --platform-audio --file background.wav --room my-room
65+
```
66+
67+
### Audio Modes
68+
69+
#### PlatformAudio Mode (`--platform-audio`)
70+
71+
Uses WebRTC's Audio Device Module (ADM) for microphone capture. **Recommended for most applications.**
72+
73+
**Features:**
74+
- Built-in voice processing:
75+
- **Echo Cancellation (AEC)** - removes echo from speaker playback
76+
- **Noise Suppression (NS)** - reduces background noise
77+
- **Auto Gain Control (AGC)** - normalizes audio levels
78+
- Hardware-accelerated processing on supported platforms (e.g., iOS VPIO)
79+
- Automatic speaker playout for received audio
80+
- Device enumeration and selection
81+
- No external audio libraries required
82+
83+
**Limitations:**
84+
- No direct access to raw audio frames (ADM sends directly to WebRTC)
85+
- Cannot apply custom audio processing before publishing
86+
87+
```python
88+
platform_audio = rtc.PlatformAudio()
89+
source = platform_audio.create_audio_source(
90+
rtc.PlatformAudioOptions(
91+
echo_cancellation=True,
92+
noise_suppression=True,
93+
auto_gain_control=True,
94+
)
95+
)
96+
track = rtc.LocalAudioTrack.create_audio_track("microphone", source)
97+
```
98+
99+
#### Synthetic Mode (Default)
100+
101+
Manual control over audio frames via `AudioSource.capture_frame()`. Use this for custom audio processing or programmatic audio generation.
102+
103+
**Features:**
104+
- Full control over audio data
105+
- Access to raw audio frames for custom processing
106+
- Generate synthetic audio (files, TTS, audio synthesis)
107+
- Apply custom filters, effects, or ML models
108+
109+
**Limitations:**
110+
- No built-in AEC/NS/AGC - implement yourself or use `AudioProcessingModule`
111+
- Must handle speaker playout manually via `AudioStream`
112+
- Requires external audio libraries for mic capture (sounddevice, pyaudio)
113+
114+
```python
115+
source = rtc.AudioSource(sample_rate=48000, num_channels=1)
116+
track = rtc.LocalAudioTrack.create_audio_track("audio", source)
117+
118+
frame = rtc.AudioFrame(data=audio_bytes, sample_rate=48000, ...)
119+
await source.capture_frame(frame)
120+
```
121+
122+
#### Mixing Both Modes
123+
124+
PlatformAudio and Synthetic modes can run simultaneously. The `--file` option demonstrates this by publishing a WAV file (synthetic) alongside microphone capture (PlatformAudio).
125+
126+
```bash
127+
python basic_room.py --platform-audio --file music.wav --room my-room
128+
```
129+
130+
This creates two audio tracks:
131+
1. **Microphone** - via PlatformAudio with voice processing
132+
2. **File** - via synthetic mode from WAV
133+
134+
Use cases: background music while speaking, sound effects, mixing pre-recorded audio with live input.
135+
136+
### Command Line Options
137+
138+
| Option | Description |
139+
|--------|-------------|
140+
| `--list-devices` | List available audio devices and exit |
141+
| `--platform-audio` | Use PlatformAudio for microphone (recommended) |
142+
| `--file WAV_PATH` | Publish audio from WAV file (synthetic mode) |
143+
| `--room NAME` | Room name (default: my-room) |
144+
| `--mic-id ID` | Select microphone by device ID |
145+
| `--speaker-id ID` | Select speaker by device ID |
146+
147+
### When to Use Each Mode
148+
149+
| Use Case | Mode |
150+
|----------|------|
151+
| Voice/video calls | PlatformAudio |
152+
| Conferencing apps | PlatformAudio |
153+
| Playing audio files | Synthetic |
154+
| Text-to-speech | Synthetic |
155+
| Custom audio processing | Synthetic |
156+
| ML audio effects | Synthetic |
157+
| Background music + voice | Both |
158+
159+
---
160+
161+
## local_audio/
162+
163+
Examples for local audio capture with microphone and speaker management.
164+
165+
### full_duplex.py
166+
167+
Full-duplex audio with microphone capture and speaker playout.
168+
169+
```bash
170+
cd local_audio
171+
python full_duplex.py
172+
```
173+
174+
Features:
175+
- Microphone capture via MediaDevices
176+
- Speaker playout for received audio
177+
- Real-time dB level metering
178+
179+
### publish_mic.py
180+
181+
Publish microphone audio with dB level monitoring.
182+
183+
```bash
184+
cd local_audio
185+
python publish_mic.py
186+
```
187+
188+
Features:
189+
- Microphone capture with AEC enabled
190+
- Real-time dB level visualization
191+
192+
---
193+
194+
## room_example.py
195+
196+
Basic room connection demonstrating event handling and participant tracking.
197+
198+
```bash
199+
python room_example.py
200+
```
201+
202+
---
203+
204+
## api.py
205+
206+
Room management using the LiveKit API (create rooms, list rooms).
207+
208+
```bash
209+
python api.py
210+
```
211+
212+
---
213+
214+
## e2ee.py
215+
216+
End-to-end encryption with a rotating 3D cube visualization.
217+
218+
```bash
219+
python e2ee.py
220+
```
221+
222+
---
223+
224+
## rpc.py
225+
226+
Remote Procedure Call (RPC) between participants - greetings, math operations, timeout handling.
227+
228+
```bash
229+
python rpc.py
230+
```
231+
232+
---
233+
234+
## multiple_connections.py
235+
236+
Sequential room connections in a single thread. Useful for Django/Flask integration.
237+
238+
```bash
239+
python multiple_connections.py
240+
```
241+
242+
---
243+
244+
## participant_attributes.py
245+
246+
Set, update, and delete participant attributes dynamically.
247+
248+
```bash
249+
python participant_attributes.py
250+
```
251+
252+
---
253+
254+
## publish_wave.py
255+
256+
Publish a sine wave audio track at a specified frequency.
257+
258+
```bash
259+
python publish_wave.py
260+
```
261+
262+
---
263+
264+
## publish_hue.py
265+
266+
Publish a color-cycling animated video track.
267+
268+
```bash
269+
python publish_hue.py
270+
```
271+
272+
---
273+
274+
## play_audio_stream.py
275+
276+
Play incoming audio from remote participants using sounddevice.
277+
278+
```bash
279+
pip install sounddevice
280+
python play_audio_stream.py
281+
```
282+
283+
---
284+
285+
## webhook.py
286+
287+
Handle LiveKit webhook events using aiohttp.
288+
289+
```bash
290+
python webhook.py
291+
```
292+
293+
---
294+
295+
## agent_dispatch.py
296+
297+
Manually dispatch agents to rooms (instead of automatic dispatch).
298+
299+
```bash
300+
python agent_dispatch.py
301+
```

0 commit comments

Comments
 (0)