feat: add optional device parameter to Recorder
This commit is contained in:
@@ -63,3 +63,16 @@ class TestRecorder:
|
|||||||
assert result.shape == (16000,)
|
assert result.shape == (16000,)
|
||||||
assert result[0] == 1.0
|
assert result[0] == 1.0
|
||||||
assert result[8000] == 0.5
|
assert result[8000] == 0.5
|
||||||
|
|
||||||
|
def test_device_passed_to_inputstream(self):
|
||||||
|
recorder = Recorder(sample_rate=16000, channels=1, min_duration=0.0, device="USB Mic")
|
||||||
|
with patch("sounddevice.InputStream") as mock_cls:
|
||||||
|
mock_cls.return_value = MagicMock()
|
||||||
|
recorder.start()
|
||||||
|
call_kwargs = mock_cls.call_args.kwargs
|
||||||
|
assert call_kwargs["device"] == "USB Mic"
|
||||||
|
recorder.stop()
|
||||||
|
|
||||||
|
def test_default_device_is_none(self):
|
||||||
|
recorder = Recorder()
|
||||||
|
assert recorder.device is None
|
||||||
|
|||||||
@@ -9,10 +9,17 @@ logger = logging.getLogger(__name__)
|
|||||||
|
|
||||||
|
|
||||||
class Recorder:
|
class Recorder:
|
||||||
def __init__(self, sample_rate: int = 16000, channels: int = 1, min_duration: float = 0.5):
|
def __init__(
|
||||||
|
self,
|
||||||
|
sample_rate: int = 16000,
|
||||||
|
channels: int = 1,
|
||||||
|
min_duration: float = 0.5,
|
||||||
|
device: str | None = None,
|
||||||
|
):
|
||||||
self.sample_rate = sample_rate
|
self.sample_rate = sample_rate
|
||||||
self.channels = channels
|
self.channels = channels
|
||||||
self.min_duration = min_duration
|
self.min_duration = min_duration
|
||||||
|
self.device = device
|
||||||
self.is_recording = False
|
self.is_recording = False
|
||||||
self._chunks: list[np.ndarray] = []
|
self._chunks: list[np.ndarray] = []
|
||||||
self._stream: sd.InputStream | None = None
|
self._stream: sd.InputStream | None = None
|
||||||
@@ -31,6 +38,7 @@ class Recorder:
|
|||||||
channels=self.channels,
|
channels=self.channels,
|
||||||
dtype=np.float32,
|
dtype=np.float32,
|
||||||
callback=self._audio_callback,
|
callback=self._audio_callback,
|
||||||
|
device=self.device,
|
||||||
)
|
)
|
||||||
self._stream.start()
|
self._stream.start()
|
||||||
logger.info("Aufnahme gestartet")
|
logger.info("Aufnahme gestartet")
|
||||||
|
|||||||
Reference in New Issue
Block a user