-
Notifications
You must be signed in to change notification settings - Fork 120
Expand file tree
/
Copy pathfull_duplex.py
More file actions
255 lines (214 loc) · 8.03 KB
/
Copy pathfull_duplex.py
File metadata and controls
255 lines (214 loc) · 8.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
"""
Full-duplex audio using PlatformAudio.
This example demonstrates:
- Using PlatformAudio for microphone capture with built-in voice processing (AEC, NS, AGC)
- Automatic speaker playout for received audio (handled by PlatformAudio)
- Monitoring microphone dB levels via AudioStream
- Device enumeration and selection
With PlatformAudio:
- Microphone audio is captured with AEC, NS, and AGC applied automatically
- Received audio from remote participants is played through speakers automatically
- No manual audio routing needed
Usage:
python full_duplex.py
python full_duplex.py --list-devices
python full_duplex.py --mic-id "mic-guid" --speaker-id "speaker-guid"
"""
import argparse
import asyncio
import logging
import os
import queue
import threading
try:
from dotenv import find_dotenv, load_dotenv
HAS_DOTENV = True
except ImportError:
HAS_DOTENV = False
from livekit import api, rtc
from db_meter import calculate_db_level, display_single_db_meter
def parse_args() -> argparse.Namespace:
parser = argparse.ArgumentParser(description="Full-duplex audio using PlatformAudio")
parser.add_argument(
"--list-devices",
action="store_true",
help="List available audio devices and exit",
)
parser.add_argument(
"--mic-id",
type=str,
help="Select microphone by device ID (from --list-devices)",
)
parser.add_argument(
"--speaker-id",
type=str,
help="Select speaker by device ID (from --list-devices)",
)
return parser.parse_args()
def list_audio_devices() -> None:
"""List available audio devices using PlatformAudio."""
try:
platform_audio = rtc.PlatformAudio()
except rtc.PlatformAudioError as e:
print(f"Failed to initialize PlatformAudio: {e}")
return
try:
print("\nRecording devices (microphones):")
for device in platform_audio.recording_devices():
print(f" [{device.index}] {device.name}")
print(f" ID: {device.id}")
print("\nPlayout devices (speakers):")
for device in platform_audio.playout_devices():
print(f" [{device.index}] {device.name}")
print(f" ID: {device.id}")
print()
finally:
platform_audio.close()
async def main(args: argparse.Namespace) -> None:
logging.basicConfig(level=logging.INFO)
# Load environment variables from a .env file if present
if HAS_DOTENV:
load_dotenv(find_dotenv())
url = os.getenv("LIVEKIT_URL")
api_key = os.getenv("LIVEKIT_API_KEY")
api_secret = os.getenv("LIVEKIT_API_SECRET")
token = os.getenv("LIVEKIT_TOKEN")
# check for either token or api_key and api_secret
if not url or (not token and (not api_key or not api_secret)):
raise RuntimeError(
"LIVEKIT_TOKEN or LIVEKIT_API_KEY and LIVEKIT_API_SECRET must be set in env"
)
# Initialize PlatformAudio for microphone capture and speaker playout
# PlatformAudio provides:
# - Built-in AEC (echo cancellation), NS (noise suppression), AGC (auto gain control)
# - Automatic speaker playout for received audio
try:
platform_audio = rtc.PlatformAudio()
logging.info("PlatformAudio initialized")
except rtc.PlatformAudioError as e:
logging.error(f"Failed to initialize PlatformAudio: {e}")
return
# Select microphone if specified
if args.mic_id:
try:
platform_audio.set_recording_device(args.mic_id)
logging.info(f"Selected microphone: {args.mic_id}")
except rtc.PlatformAudioError as e:
logging.warning(f"Failed to select microphone: {e}")
# Select speaker if specified
if args.speaker_id:
try:
platform_audio.set_playout_device(args.speaker_id)
logging.info(f"Selected speaker: {args.speaker_id}")
except rtc.PlatformAudioError as e:
logging.warning(f"Failed to select speaker: {e}")
room = rtc.Room()
source = None
# dB level monitoring (mic only)
mic_db_queue: queue.Queue[float] = queue.Queue()
# With PlatformAudio, received audio is automatically played through speakers
# We just log when tracks are subscribed/unsubscribed
def on_track_subscribed(
track: rtc.Track,
publication: rtc.RemoteTrackPublication,
participant: rtc.RemoteParticipant,
):
if track.kind == rtc.TrackKind.KIND_AUDIO:
logging.info(
"Subscribed to audio from %s (auto-playing through speaker)",
participant.identity,
)
room.on("track_subscribed", on_track_subscribed)
def on_track_unsubscribed(
track: rtc.Track,
publication: rtc.RemoteTrackPublication,
participant: rtc.RemoteParticipant,
):
if track.kind == rtc.TrackKind.KIND_AUDIO:
logging.info("Unsubscribed from audio of %s", participant.identity)
room.on("track_unsubscribed", on_track_unsubscribed)
# generate token if not provided
if not token:
token = (
api.AccessToken(api_key, api_secret)
.with_identity("local-audio")
.with_name("Local Audio")
.with_grants(
api.VideoGrants(
room_join=True,
room="local-audio",
)
)
.to_jwt()
)
try:
await room.connect(url, token)
logging.info("connected to room %s", room.name)
# Create audio source with voice processing enabled
source = platform_audio.create_audio_source(
rtc.PlatformAudioOptions(
echo_cancellation=True,
noise_suppression=True,
auto_gain_control=True,
)
)
track = rtc.LocalAudioTrack.create_audio_track("mic", source)
pub_opts = rtc.TrackPublishOptions()
pub_opts.source = rtc.TrackSource.SOURCE_MICROPHONE
await room.local_participant.publish_track(track, pub_opts)
logging.info("published local microphone with PlatformAudio")
# Start dB meter display in a separate thread
meter_thread = threading.Thread(
target=display_single_db_meter,
args=(mic_db_queue,),
kwargs={"label": "Mic Level: "},
daemon=True,
)
meter_thread.start()
# Monitor microphone dB levels via AudioStream
async def monitor_mic_db():
mic_stream = rtc.AudioStream(track, sample_rate=48000, num_channels=1)
frame_count = 0
sample_interval = 5 # Process every 5th frame to reduce load
try:
async for frame_event in mic_stream:
# Skip frames to reduce processing load
frame_count += 1
if frame_count % sample_interval != 0:
continue
frame = frame_event.frame
# Convert frame data to list of samples
samples = list(frame.data)
db_level = calculate_db_level(samples)
# Update queue with latest value (non-blocking)
try:
mic_db_queue.put_nowait(db_level)
except queue.Full:
pass # Drop if queue is full
except asyncio.CancelledError:
pass
except Exception:
pass
finally:
await mic_stream.aclose()
asyncio.create_task(monitor_mic_db())
# Run until Ctrl+C
while True:
await asyncio.sleep(1)
except (KeyboardInterrupt, asyncio.CancelledError):
pass
finally:
try:
await room.disconnect()
except Exception:
pass
# Clean up PlatformAudio resources
if source is not None:
source.close()
platform_audio.close()
if __name__ == "__main__":
args = parse_args()
if args.list_devices:
list_audio_devices()
else:
asyncio.run(main(args))