Skip to content

Commit a831e67

Browse files
committed
media devices should import sounddevice lazily
1 parent e0e99ae commit a831e67

File tree

1 file changed

+17
-3
lines changed

1 file changed

+17
-3
lines changed

livekit-rtc/livekit/rtc/media_devices.py

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,14 @@
1616

1717
import asyncio
1818
from dataclasses import dataclass
19-
from typing import Any, AsyncIterator, Optional
19+
from typing import TYPE_CHECKING, Any, AsyncIterator, Optional
2020

2121
import numpy as np
22-
import sounddevice as sd # type: ignore[import-not-found, import-untyped]
2322
import threading
2423

24+
if TYPE_CHECKING:
25+
import sounddevice as sd # type: ignore[import-not-found, import-untyped]
26+
2527
from . import AudioSource
2628
from .audio_frame import AudioFrame
2729
from .apm import AudioProcessingModule
@@ -119,7 +121,7 @@ class InputCapture:
119121
"""
120122

121123
source: AudioSource
122-
input_stream: sd.InputStream
124+
input_stream: "sd.InputStream"
123125
task: asyncio.Task
124126
apm: Optional[AudioProcessingModule]
125127
delay_estimator: Optional[_APMDelayEstimator]
@@ -161,6 +163,8 @@ def __init__(
161163
output_device: Optional[int] = None,
162164
delay_estimator: Optional[_APMDelayEstimator] = None,
163165
) -> None:
166+
import sounddevice as sd # type: ignore[import-not-found, import-untyped]
167+
164168
self._sample_rate = sample_rate
165169
self._num_channels = num_channels
166170
self._blocksize = blocksize
@@ -398,6 +402,8 @@ def list_input_devices(self) -> list[dict[str, Any]]:
398402
Returns a list of dictionaries with the `sounddevice` metadata and an
399403
added `index` key corresponding to the device index.
400404
"""
405+
import sounddevice as sd # type: ignore[import-not-found, import-untyped]
406+
401407
devices = sd.query_devices()
402408
result: list[dict[str, Any]] = []
403409
for idx, dev in enumerate(devices):
@@ -407,6 +413,8 @@ def list_input_devices(self) -> list[dict[str, Any]]:
407413

408414
def list_output_devices(self) -> list[dict[str, Any]]:
409415
"""List available output devices with indices."""
416+
import sounddevice as sd # type: ignore[import-not-found, import-untyped]
417+
410418
devices = sd.query_devices()
411419
result: list[dict[str, Any]] = []
412420
for idx, dev in enumerate(devices):
@@ -416,11 +424,15 @@ def list_output_devices(self) -> list[dict[str, Any]]:
416424

417425
def default_input_device(self) -> Optional[int]:
418426
"""Return the default input device index (or None)."""
427+
import sounddevice as sd # type: ignore[import-not-found, import-untyped]
428+
419429
dev = sd.default.device
420430
return dev[0] if isinstance(dev, (list, tuple)) else None
421431

422432
def default_output_device(self) -> Optional[int]:
423433
"""Return the default output device index (or None)."""
434+
import sounddevice as sd # type: ignore[import-not-found, import-untyped]
435+
424436
dev = sd.default.device
425437
return dev[1] if isinstance(dev, (list, tuple)) else None
426438

@@ -459,6 +471,8 @@ def open_input(
459471
Returns:
460472
InputCapture: Holder with `source`, `apm`, and `aclose()`.
461473
"""
474+
import sounddevice as sd # type: ignore[import-not-found, import-untyped]
475+
462476
loop = self._loop
463477
source = AudioSource(self._in_sr, self._channels, loop=loop)
464478
apm: Optional[AudioProcessingModule] = None

0 commit comments

Comments
 (0)