feat: add microphone field and save_config to Config

This commit is contained in:
2026-04-10 21:01:08 +02:00
parent 3a938ca35b
commit 1637314c1d
2 changed files with 48 additions and 0 deletions
+29
View File
@@ -76,3 +76,32 @@ class TestConfigPath:
from whisper_local.config import _default_config_path from whisper_local.config import _default_config_path
result = _default_config_path() result = _default_config_path()
assert result == Path.home() / ".config" / "whisper-local" / "config.toml" assert result == Path.home() / ".config" / "whisper-local" / "config.toml"
class TestMicrophoneConfig:
def test_default_microphone_is_empty(self):
config = Config()
assert config.microphone == ""
def test_load_device_from_toml(self, tmp_path):
config_file = tmp_path / "config.toml"
config_file.write_text('[audio]\ndevice = "Headset Mic"\n')
config = load_config(config_file)
assert config.microphone == "Headset Mic"
class TestSaveConfig:
def test_save_and_reload(self, tmp_path):
from whisper_local.config import save_config
path = tmp_path / "config.toml"
config = Config(hotkey="KEY_F8", microphone="USB Mic")
save_config(config, path)
loaded = load_config(path)
assert loaded.hotkey == "KEY_F8"
assert loaded.microphone == "USB Mic"
def test_save_creates_parent_dirs(self, tmp_path):
from whisper_local.config import save_config
path = tmp_path / "subdir" / "config.toml"
save_config(Config(), path)
assert path.exists()
+19
View File
@@ -25,6 +25,7 @@ class Config:
sample_rate: int = 16000 sample_rate: int = 16000
channels: int = 1 channels: int = 1
min_duration: float = 0.5 min_duration: float = 0.5
microphone: str = ""
def load_config(path: Path = DEFAULT_CONFIG_PATH) -> Config: def load_config(path: Path = DEFAULT_CONFIG_PATH) -> Config:
@@ -56,5 +57,23 @@ def load_config(path: Path = DEFAULT_CONFIG_PATH) -> Config:
config.channels = audio_section["channels"] config.channels = audio_section["channels"]
if "min_duration" in audio_section: if "min_duration" in audio_section:
config.min_duration = audio_section["min_duration"] config.min_duration = audio_section["min_duration"]
if "device" in audio_section:
config.microphone = audio_section["device"]
return config return config
def save_config(config: Config, path: Path = DEFAULT_CONFIG_PATH) -> None:
"""Schreibt Config als TOML-Datei. Erstellt Verzeichnisse bei Bedarf."""
path.parent.mkdir(parents=True, exist_ok=True)
content = (
f'[hotkey]\nkey = "{config.hotkey}"\n\n'
f'[whisper]\nmodel = "{config.whisper_model}"\n'
f'language = "{config.language}"\n'
f'compute_type = "{config.compute_type}"\n\n'
f'[audio]\nsample_rate = {config.sample_rate}\n'
f'channels = {config.channels}\n'
f'min_duration = {config.min_duration}\n'
f'device = "{config.microphone}"\n'
)
path.write_text(content, encoding="utf-8")