-
Notifications
You must be signed in to change notification settings - Fork 113
Expand file tree
/
Copy pathpublish_mic.py
More file actions
99 lines (82 loc) · 2.93 KB
/
publish_mic.py
File metadata and controls
99 lines (82 loc) · 2.93 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
import os
import asyncio
import logging
import threading
import queue
from dotenv import load_dotenv, find_dotenv
from livekit import api, rtc
from db_meter import calculate_db_level, display_single_db_meter
async def main() -> None:
logging.basicConfig(level=logging.INFO)
# Load environment variables from a .env file if present
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"
)
room = rtc.Room()
# Create media devices helper and open default microphone with AEC enabled
devices = rtc.MediaDevices()
mic = devices.open_input(enable_aec=True)
# dB level monitoring
mic_db_queue = 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)
track = rtc.LocalAudioTrack.create_audio_track("mic", 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")
# 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
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 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:
pass
finally:
await mic.aclose()
try:
await room.disconnect()
except Exception:
pass
if __name__ == "__main__":
asyncio.run(main())