-
Notifications
You must be signed in to change notification settings - Fork 121
Expand file tree
/
Copy pathpublish_mic.py
More file actions
189 lines (157 loc) · 5.51 KB
/
Copy pathpublish_mic.py
File metadata and controls
189 lines (157 loc) · 5.51 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
"""
Publish microphone audio using PlatformAudio.
This example demonstrates:
- Using PlatformAudio for microphone capture with built-in voice processing (AEC, NS, AGC)
- Monitoring microphone dB levels via AudioStream
- Device enumeration and selection
Usage:
python publish_mic.py
python publish_mic.py --list-devices
python publish_mic.py --mic-id "device-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="Publish microphone 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)",
)
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()
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")
if not url or not api_key or not api_secret:
raise RuntimeError(
"LIVEKIT_URL and LIVEKIT_API_KEY and LIVEKIT_API_SECRET must be set in env"
)
# Initialize PlatformAudio for microphone capture
# PlatformAudio provides built-in AEC, NS, and AGC
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}")
room = rtc.Room()
source = None
# dB level monitoring
mic_db_queue: queue.Queue[float] = queue.Queue()
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, "Mic: "), 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)
try:
async for frame_event in mic_stream:
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))