feat: add recorder module with sounddevice audio capture

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-04-06 20:25:31 +02:00
parent ef6ff5b35b
commit 949eb679e1
2 changed files with 128 additions and 0 deletions
+65
View File
@@ -0,0 +1,65 @@
import numpy as np
import pytest
from unittest.mock import MagicMock, patch
from whisper_local.recorder import Recorder
@pytest.fixture
def recorder():
return Recorder(sample_rate=16000, channels=1, min_duration=0.5)
class TestRecorder:
def test_init(self, recorder):
assert recorder.sample_rate == 16000
assert recorder.channels == 1
assert not recorder.is_recording
def test_start_recording(self, recorder):
with patch("sounddevice.InputStream") as mock_stream_cls:
mock_stream = MagicMock()
mock_stream_cls.return_value = mock_stream
recorder.start()
assert recorder.is_recording
assert len(recorder._chunks) == 0
recorder.stop() # Cleanup
def test_stop_without_start_returns_none(self, recorder):
result = recorder.stop()
assert result is None
def test_add_chunk_and_stop(self, recorder):
with patch("sounddevice.InputStream") as mock_stream_cls:
mock_stream_cls.return_value = MagicMock()
recorder.start()
# Simuliere 1 Sekunde Audio (16000 Samples)
chunk = np.zeros((16000, 1), dtype=np.float32)
recorder._chunks.append(chunk)
result = recorder.stop()
assert result is not None
assert result.shape == (16000,)
assert result.dtype == np.float32
def test_short_audio_returns_none(self, recorder):
with patch("sounddevice.InputStream") as mock_stream_cls:
mock_stream_cls.return_value = MagicMock()
recorder.start()
# Nur 0.1s Audio — unter min_duration
chunk = np.zeros((1600, 1), dtype=np.float32)
recorder._chunks.append(chunk)
result = recorder.stop()
assert result is None
def test_multiple_chunks_concatenated(self, recorder):
with patch("sounddevice.InputStream") as mock_stream_cls:
mock_stream_cls.return_value = MagicMock()
recorder.start()
chunk1 = np.ones((8000, 1), dtype=np.float32)
chunk2 = np.ones((8000, 1), dtype=np.float32) * 0.5
recorder._chunks.extend([chunk1, chunk2])
result = recorder.stop()
assert result is not None
assert result.shape == (16000,)
assert result[0] == 1.0
assert result[8000] == 0.5