Add microphone picker and device management features
- Implement `list_input_devices` to retrieve and filter input-capable audio devices. - Create `MicPickerDialog` for selecting and saving microphone settings. - Add `set_env_value` function to update .env file with selected microphone index. - Update UI components to integrate microphone selection functionality. - Enhance tests for microphone device handling and environment variable management.
This commit is contained in:
+33
-12
@@ -11,13 +11,14 @@ from __future__ import annotations
|
||||
import sys
|
||||
|
||||
from PySide6.QtCore import QThread
|
||||
from PySide6.QtWidgets import QApplication
|
||||
from PySide6.QtWidgets import QApplication, QMessageBox
|
||||
|
||||
from .. import config, updater
|
||||
from ..controller import PetController
|
||||
from ..hotkey import GlobalHotkey
|
||||
from ..state import PetState
|
||||
from .history_window import HistoryWindow
|
||||
from .mic_picker import MicPickerDialog
|
||||
from .pet_window import PetWindow
|
||||
from .tray import PetTray
|
||||
from .wake_tuner import WakeTunerWindow
|
||||
@@ -57,6 +58,19 @@ def run() -> int:
|
||||
window.publish_monitors()
|
||||
window.copied.connect(lambda text: _log(f"Copied to clipboard: {text[:60]}"))
|
||||
|
||||
# The updater has already moved the checkout by the time this fires; all
|
||||
# that's left is to let Qt tear down cleanly (so the mic and the tray
|
||||
# icon are released) and then exec the new code. Doing the exec after
|
||||
# app.exec() returns, rather than from the controller thread, is what
|
||||
# guarantees the audio device is free before the new process opens it.
|
||||
# Defined early so the mic picker (below) can trigger the same restart
|
||||
# a self-update or self-restart does.
|
||||
pending_restart = {"tag": None}
|
||||
|
||||
def _handle_restart(tag: str) -> None:
|
||||
pending_restart["tag"] = tag
|
||||
app.quit()
|
||||
|
||||
history_window = HistoryWindow(controller.history)
|
||||
tuner_window = WakeTunerWindow(
|
||||
get_threshold=controller.wake_threshold,
|
||||
@@ -65,6 +79,23 @@ def run() -> int:
|
||||
on_reset=controller.reset_wake_stats,
|
||||
)
|
||||
|
||||
def _save_mic_device(value) -> None:
|
||||
config.set_env_value("MIC_DEVICE", str(value) if value is not None else "")
|
||||
_log(f"Microphone set to device #{value}." if value is not None else
|
||||
"Microphone set to system default.")
|
||||
answer = QMessageBox.question(
|
||||
None, "Restart Bolt?",
|
||||
"Restart now so Bolt starts listening on the new microphone?",
|
||||
QMessageBox.Yes | QMessageBox.No,
|
||||
)
|
||||
if answer == QMessageBox.Yes:
|
||||
_handle_restart("microphone change")
|
||||
|
||||
mic_picker = MicPickerDialog(
|
||||
get_current=lambda: config.MIC_DEVICE,
|
||||
on_save=_save_mic_device,
|
||||
)
|
||||
|
||||
def _set_nap(napping: bool) -> None:
|
||||
# Clicking the tray item pins the state; the schedule takes over again
|
||||
# only after a restart or a `petctl nap off`.
|
||||
@@ -82,6 +113,7 @@ def run() -> int:
|
||||
on_set_nap=_set_nap,
|
||||
on_show_history=history_window.show_refreshed,
|
||||
on_show_wake_tuner=tuner_window.show_refreshed,
|
||||
on_show_mic_picker=mic_picker.show_refreshed,
|
||||
on_reset_voice=controller.reset_voice,
|
||||
)
|
||||
|
||||
@@ -104,17 +136,6 @@ def run() -> int:
|
||||
elif hotkey.running:
|
||||
_log(f"Push-to-talk: {config.PUSH_TO_TALK_HOTKEY}")
|
||||
|
||||
# The updater has already moved the checkout by the time this fires; all
|
||||
# that's left is to let Qt tear down cleanly (so the mic and the tray
|
||||
# icon are released) and then exec the new code. Doing the exec after
|
||||
# app.exec() returns, rather than from the controller thread, is what
|
||||
# guarantees the audio device is free before the new process opens it.
|
||||
pending_restart = {"tag": None}
|
||||
|
||||
def _handle_restart(tag: str) -> None:
|
||||
pending_restart["tag"] = tag
|
||||
app.quit()
|
||||
|
||||
controller.restart_requested.connect(_handle_restart)
|
||||
|
||||
def _shutdown() -> None:
|
||||
|
||||
@@ -0,0 +1,98 @@
|
||||
"""Microphone picker — MIC_DEVICE in .env is a device index most people have
|
||||
no way to look up short of running a script. This dialog lists whatever
|
||||
sounddevice can actually see, lets you click one, and writes its index back
|
||||
to .env so it survives a restart.
|
||||
|
||||
The index, not the name, is what gets saved: the same device is commonly
|
||||
listed once per host API (MME, DirectSound, WASAPI, WDM-KS, ...) under the
|
||||
same or a near-identical name, which makes a bare name an ambiguous pick that
|
||||
sounddevice will flatly refuse to resolve (see audio/mic.list_input_devices).
|
||||
The index is exact.
|
||||
|
||||
The mic is only ever opened once, at startup (see controller.run()), so a
|
||||
pick made here still needs a restart before Bolt actually uses it — this
|
||||
dialog just makes finding and saving the right device painless, and offers
|
||||
to do the restart for you.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from typing import Callable, Optional
|
||||
|
||||
from PySide6.QtCore import Qt
|
||||
from PySide6.QtWidgets import (
|
||||
QDialog, QDialogButtonBox, QHBoxLayout, QLabel, QListWidget,
|
||||
QListWidgetItem, QPushButton, QVBoxLayout,
|
||||
)
|
||||
|
||||
from ..audio.mic import list_input_devices
|
||||
|
||||
_DEFAULT_LABEL = "System default"
|
||||
|
||||
|
||||
class MicPickerDialog(QDialog):
|
||||
def __init__(
|
||||
self,
|
||||
get_current: Callable[[], Optional[int]],
|
||||
on_save: Callable[[Optional[int]], None],
|
||||
):
|
||||
super().__init__()
|
||||
self._get_current = get_current
|
||||
self._on_save = on_save
|
||||
|
||||
self.setWindowTitle("Bolt — select microphone")
|
||||
self.resize(440, 320)
|
||||
|
||||
self._list = QListWidget()
|
||||
refresh_button = QPushButton("Refresh")
|
||||
refresh_button.clicked.connect(self.refresh)
|
||||
|
||||
top = QHBoxLayout()
|
||||
top.addWidget(QLabel("Pick the microphone Bolt should listen on:"))
|
||||
top.addStretch(1)
|
||||
top.addWidget(refresh_button)
|
||||
|
||||
buttons = QDialogButtonBox(QDialogButtonBox.Save | QDialogButtonBox.Cancel)
|
||||
buttons.accepted.connect(self._save)
|
||||
buttons.rejected.connect(self.reject)
|
||||
|
||||
layout = QVBoxLayout(self)
|
||||
layout.addLayout(top)
|
||||
layout.addWidget(self._list)
|
||||
layout.addWidget(buttons)
|
||||
|
||||
def refresh(self) -> None:
|
||||
self._list.clear()
|
||||
default_item = QListWidgetItem(_DEFAULT_LABEL)
|
||||
default_item.setData(Qt.UserRole, None)
|
||||
self._list.addItem(default_item)
|
||||
|
||||
current = self._get_current()
|
||||
selected = default_item
|
||||
try:
|
||||
devices = list_input_devices()
|
||||
except Exception as exc:
|
||||
self._list.addItem(f"Could not list devices: {exc}")
|
||||
devices = []
|
||||
|
||||
for device in devices:
|
||||
item = QListWidgetItem(
|
||||
f"{device['name']} [{device['hostapi']}] (#{device['index']})"
|
||||
)
|
||||
item.setData(Qt.UserRole, device["index"])
|
||||
self._list.addItem(item)
|
||||
if current == device["index"]:
|
||||
selected = item
|
||||
self._list.setCurrentItem(selected)
|
||||
|
||||
def _save(self) -> None:
|
||||
item = self._list.currentItem()
|
||||
value = item.data(Qt.UserRole) if item is not None else None
|
||||
self._on_save(value)
|
||||
self.accept()
|
||||
|
||||
def show_refreshed(self) -> None:
|
||||
self.refresh()
|
||||
self.show()
|
||||
self.raise_()
|
||||
self.activateWindow()
|
||||
@@ -47,6 +47,7 @@ class PetTray(QSystemTrayIcon):
|
||||
on_set_nap: Optional[Callable[[bool], None]] = None,
|
||||
on_show_history: Optional[Callable[[], None]] = None,
|
||||
on_show_wake_tuner: Optional[Callable[[], None]] = None,
|
||||
on_show_mic_picker: Optional[Callable[[], None]] = None,
|
||||
on_reset_voice: Optional[Callable[[], None]] = None,
|
||||
parent=None,
|
||||
):
|
||||
@@ -104,6 +105,11 @@ class PetTray(QSystemTrayIcon):
|
||||
tuner_action.triggered.connect(on_show_wake_tuner)
|
||||
menu.addAction(tuner_action)
|
||||
|
||||
if on_show_mic_picker is not None:
|
||||
mic_action = QAction("Select microphone…", menu)
|
||||
mic_action.triggered.connect(on_show_mic_picker)
|
||||
menu.addAction(mic_action)
|
||||
|
||||
# Only ever enabled while a server-picked voice (speak_as) is in use —
|
||||
# it's the way back from "talk like a pirate", which nothing else
|
||||
# undoes short of a restart.
|
||||
|
||||
Reference in New Issue
Block a user