Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,11 @@ LVA_PULSE_COOKIE="/run/user/${LVA_USER_ID}/pulse/cookie"
### Enable thinking sound (optional):
# ENABLE_THINKING_SOUND="1"

### Sound for continued conversations (optional):
# Plays CONTINUE_CONVERSATION_SOUND when the microphone opens again for the answer.
# "1" plays the sound, "0" (default) does not
# ENABLE_CONTINUE_CONVERSATION_SOUND="1"

### Start listening during wake sound (optional):
# Start listening immediately after wake word detection, without waiting for the wake sound to finish
# LISTEN_DURING_WAKE_SOUND="1"
Expand Down Expand Up @@ -167,6 +172,9 @@ LVA_PULSE_COOKIE="/run/user/${LVA_USER_ID}/pulse/cookie"
# BUTTON_DOUBLE_PRESS_SOUND="sounds/button_double_press.flac"
# BUTTON_TRIPLE_PRESS_SOUND="sounds/button_triple_press.flac"
# BUTTON_LONG_PRESS_SOUND="sounds/button_long_press.flac"
# Sound played when the microphone re-opens for a follow-up question
# (enable it with ENABLE_CONTINUE_CONVERSATION_SOUND).
# CONTINUE_CONVERSATION_SOUND="sounds/wake_word_triggered.flac"

### Enable output-only mode (optional):
# ENABLE_OUTPUT_ONLY="1"
Expand Down
8 changes: 8 additions & 0 deletions docker-entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,10 @@ if [ "$ENABLE_THINKING_SOUND" = "1" ]; then
EXTRA_ARGS+=( "--enable-thinking-sound" )
fi

if [ -n "${ENABLE_CONTINUE_CONVERSATION_SOUND}" ]; then
EXTRA_ARGS+=( "--enable-continue-conversation-sound" "$ENABLE_CONTINUE_CONVERSATION_SOUND" )
fi

if [ "$LISTEN_DURING_WAKE_SOUND" = "1" ]; then
EXTRA_ARGS+=( "--listen-during-wake-sound" )
fi
Expand Down Expand Up @@ -233,6 +237,10 @@ if [ -n "${BUTTON_LONG_PRESS_SOUND}" ]; then
EXTRA_ARGS+=( "--button-long-press-sound" "$BUTTON_LONG_PRESS_SOUND" )
fi

if [ -n "${CONTINUE_CONVERSATION_SOUND}" ]; then
EXTRA_ARGS+=( "--continue-conversation-sound" "$CONTINUE_CONVERSATION_SOUND" )
fi

if [ -n "${TIMER_MAX_RING_SECONDS}" ]; then
EXTRA_ARGS+=( "--timer-max-ring-seconds" "$TIMER_MAX_RING_SECONDS" )
fi
Expand Down
3 changes: 2 additions & 1 deletion docs/behavior.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ LVA has two independent audio players that share one volume setting:
| Player | Used for |
|--------|----------|
| **Music player** | Media sent from Home Assistant (music, radio, `media_player.play_media` without `announce`) |
| **Assistant player** | Wake / mute / unmute chimes, the thinking sound, spoken replies (TTS), announcements, timer ringing, button sounds |
| **Assistant player** | Wake / mute / unmute chimes, the thinking sound, the continued conversation sound, spoken replies (TTS), announcements, timer ringing, button sounds |

The music player can be sent to a different sound card with `--music-output-device`. By default both use `--audio-output-device`. `--audio-output-device` defaults to `pulse`, the default PulseAudio output (PulseAudio itself, or PipeWire through `pipewire-pulse`). With `pulse` alone LVA names no device, so the sound server decides where the sound goes and the output follows a change of the default output while LVA runs, for example when a USB sound card is unplugged (observed with PipeWire). A device name carries its mpv driver as a prefix (for example `pulse/...`) and that driver is used.

Expand Down Expand Up @@ -74,6 +74,7 @@ Details:
- **Early speech.** If Home Assistant streams the reply (the response starts before it is complete), playback starts as soon as the first part is available.
- **No spoken reply.** If the pipeline finishes without any speech (for example no speech was detected, an error, or a command with no reply), the pipeline ended sound (`--pipeline-ended-sound`) plays. Afterwards the assistant goes back to idle and music is restored. The sound is not played after announcements, when the reply is stopped with the stop word, when another satellite heard the same wake word and Home Assistant gave the run to it (`duplicate_wake_up_detected`), or when Home Assistant ends the run right after speech recognition because something else handles the transcript (for example the answer to a question asked by an automation).
- **Continued conversation.** If Home Assistant asks a follow-up question, the assistant listens again after the reply, without a wake word. It waits `--continue-conversation-delay` (default 0.5 s) after the reply so the microphone does not record the tail of its own voice. Music stays ducked through the whole conversation and is restored only when the conversation ends. The follow-up runs on the same pipeline as the wake word that started the conversation. After a button press or an announcement from Home Assistant there is no wake word, so the first pipeline is used. If you mute the assistant during that delay, the follow-up is skipped and music is restored.
- **Continued conversation sound.** With `--enable-continue-conversation-sound 1` (default 0 = off; there is no switch for it in Home Assistant), a sound tells you that the assistant listens again: `--continue-conversation-sound`, by default `sounds/wake_word_triggered.flac`, the file of the default wake sound (`--wakeup-sound` does not change it). It plays after the delay above, also after an announcement that starts a conversation. Listening starts when the sound has finished, with `--listen-during-wake-sound` right away. Without `--listen-during-wake-sound`, stopping the sound (for example with a peripheral's stop command) or losing the connection to Home Assistant while it plays means that the assistant does not listen, and music is restored. A muted assistant plays no sound.
- **Wake word while a timer rings.** Saying the wake word silences the ringing timer and starts a normal interaction.
- **Starting by button.** A peripheral button can start listening without a wake word. Instead of the wake sound, the start-listening sound (`--start-listening-sound`) plays; everything else is identical. This also silences a ringing timer. It does nothing while no wake word is active, see below.
- **Errors.** If the pipeline reports an error, version 1 peripherals receive a `pipeline_error` event, version 2 peripherals the state `pipeline_error`. See [peripheral_api/readme.md](peripheral_api/readme.md).
Expand Down
6 changes: 4 additions & 2 deletions docs/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@

```sh
usage: __main__.py [-h] [--name NAME] [--audio-input-device AUDIO_INPUT_DEVICE] [--list-input-devices] [--audio-input-block-size AUDIO_INPUT_BLOCK_SIZE] [--audio-output-device AUDIO_OUTPUT_DEVICE] [--music-output-device MUSIC_OUTPUT_DEVICE] [--mpv-player-duck-mode {duck,pause}] [--enable-sendspin-bridge {0,1}] [--sendspin-control-dir SENDSPIN_CONTROL_DIR] [--sendspin-player-duck-mode {pulse,stop,duck}] [--list-output-devices] [--wake-word-root-dir WAKE_WORD_ROOT_DIR] [--mic-auto-gain] [--mic-noise-suppression]
[--wake-model WAKE_MODEL] [--wake-model-2 WAKE_MODEL_2] [--stop-model STOP_MODEL] [--refractory-seconds REFRACTORY_SECONDS] [--wakeup-sound WAKEUP_SOUND] [--timer-finished-sound TIMER_FINISHED_SOUND] [--processing-sound PROCESSING_SOUND]
[--mute-sound MUTE_SOUND] [--unmute-sound UNMUTE_SOUND] [--preferences-file PREFERENCES_FILE] [--host HOST] [--network-interface NETWORK_INTERFACE] [--mac-address MAC_ADDRESS] [--port PORT] [--enable-encryption] [--encryption-key-file ENCRYPTION_KEY_FILE] [--enable-thinking-sound] [--enable-zeroconf {0,1}] [--listen-during-wake-sound] [--debug]
[--wake-model WAKE_MODEL] [--wake-model-2 WAKE_MODEL_2] [--stop-model STOP_MODEL] [--refractory-seconds REFRACTORY_SECONDS] [--wakeup-sound WAKEUP_SOUND] [--timer-finished-sound TIMER_FINISHED_SOUND] [--processing-sound PROCESSING_SOUND] [--continue-conversation-sound CONTINUE_CONVERSATION_SOUND]
[--mute-sound MUTE_SOUND] [--unmute-sound UNMUTE_SOUND] [--preferences-file PREFERENCES_FILE] [--host HOST] [--network-interface NETWORK_INTERFACE] [--mac-address MAC_ADDRESS] [--port PORT] [--enable-encryption] [--encryption-key-file ENCRYPTION_KEY_FILE] [--enable-thinking-sound] [--enable-continue-conversation-sound {0,1}] [--enable-zeroconf {0,1}] [--listen-during-wake-sound] [--debug]
```

</details>
Expand Down Expand Up @@ -66,6 +66,7 @@ usage: __main__.py [-h] [--name NAME] [--audio-input-device AUDIO_INPUT_DEVICE]
| `--listen-during-wake-sound` | `LISTEN_DURING_WAKE_SOUND` | Start listening immediately after wake word detection, without waiting for the wake sound to finish | False |
| `--continue-conversation-delay` | `CONTINUE_CONVERSATION_DELAY` | Delay before mic opens for continued conversation | 0.5 |
| `--enable-thinking-sound` | `ENABLE_THINKING_SOUND` | Enable thinking sound on startup | False |
| `--enable-continue-conversation-sound` | `ENABLE_CONTINUE_CONVERSATION_SOUND` | `1` plays a sound when the microphone opens again for a follow-up question, `0` does not. See [behavior](behavior.md#talking-to-the-assistant) | 0 |
| `--timer-max-ring-seconds` | `TIMER_MAX_RING_SECONDS` | Seconds after which the timer stops ringing | 900.0 |

</details>
Expand All @@ -79,6 +80,7 @@ usage: __main__.py [-h] [--name NAME] [--audio-input-device AUDIO_INPUT_DEVICE]
| `--start-listening-sound` | `START_LISTENING_SOUND` | Sound file played when button is pressed to start listening | `sounds/start_listening_button.flac` |
| `--timer-finished-sound` | `TIMER_FINISHED_SOUND` | Sound file played when timer finishes | `sounds/timer_finished.flac` |
| `--processing-sound` | `PROCESSING_SOUND` | Sound played while assistant is processing | `sounds/processing.wav` |
| `--continue-conversation-sound` | `CONTINUE_CONVERSATION_SOUND` | Sound file played when the microphone opens again for a follow-up question, if enabled | `sounds/wake_word_triggered.flac` |
| `--mute-sound` | `MUTE_SOUND` | Sound played when muting the assistant | `sounds/mute_switch_on.flac` |
| `--unmute-sound` | `UNMUTE_SOUND` | Sound played when unmuting the assistant | `sounds/mute_switch_off.flac` |
| `--button-double-press-sound` | `BUTTON_DOUBLE_PRESS_SOUND` | Sound played on button double press | `sounds/button_double_press.flac` |
Expand Down
2 changes: 2 additions & 0 deletions docs/installation_part2_application.md
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,7 @@ Environment=PREFERENCES_FILE="/home/pi/linux-voice-assistant/preferences.json"
# Environment=MIC_NOISE_SUPPRESSION="0"
# Environment=AUDIO_INPUT_CHANNELS=1
# Environment=ENABLE_THINKING_SOUND="1"
# Environment=ENABLE_CONTINUE_CONVERSATION_SOUND="1"
# Environment=WAKE_WORD_ROOT_DIR="/home/pi/linux-voice-assistant/wakewords"
# Environment=WAKE_MODEL="okay_nabu"
# Environment=WAKE_MODEL_2="hey_jarvis"
Expand All @@ -212,6 +213,7 @@ Environment=PREFERENCES_FILE="/home/pi/linux-voice-assistant/preferences.json"
# Environment=START_LISTENING_SOUND="sounds/start_listening_button.flac"
# Environment=TIMER_FINISHED_SOUND="sounds/timer_finished.flac"
# Environment=PROCESSING_SOUND="sounds/processing.wav"
# Environment=CONTINUE_CONVERSATION_SOUND="sounds/wake_word_triggered.flac"
# Environment=LISTEN_DURING_WAKE_SOUND="0"
# Environment=MUTE_SOUND="sounds/mute_switch_on.flac"
# Environment=UNMUTE_SOUND="sounds/mute_switch_off.flac"
Expand Down
14 changes: 14 additions & 0 deletions linux_voice_assistant/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,11 @@ async def main() -> None:
default=str(_SOUNDS_DIR / "pipeline_ended_low.flac"),
help="Sound to play when the voice pipeline ends without a response (e.g. no speech detected)",
)
parser.add_argument(
"--continue-conversation-sound",
default=str(_SOUNDS_DIR / "wake_word_triggered.flac"),
help="Sound to play when the microphone re-opens for a follow-up question, if enabled with --enable-continue-conversation-sound 1",
)
parser.add_argument(
"--button-double-press-sound",
default=str(_SOUNDS_DIR / "button_double_press.flac"),
Expand Down Expand Up @@ -275,6 +280,13 @@ async def main() -> None:
action="store_true",
help="Enable thinking sound on startup",
)
parser.add_argument(
"--enable-continue-conversation-sound",
type=int,
default=0,
choices=(0, 1),
help="1 plays --continue-conversation-sound when the microphone re-opens for a follow-up question, 0 does not (default: %(default)s)",
)
# ------------------------------------------------------------------
# Peripheral API (LEDs, buttons, HAT boards)
# ------------------------------------------------------------------
Expand Down Expand Up @@ -605,6 +617,8 @@ async def main() -> None:
button_double_press_sound=args.button_double_press_sound,
button_triple_press_sound=args.button_triple_press_sound,
button_long_press_sound=args.button_long_press_sound,
continue_conversation_sound=args.continue_conversation_sound,
continue_conversation_sound_enabled=args.enable_continue_conversation_sound == 1,
preferences=preferences,
preferences_path=preferences_path,
timers=TimerList.from_dicts(preferences.timers),
Expand Down
2 changes: 2 additions & 0 deletions linux_voice_assistant/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,7 @@ class ServerState:
# Sendspin player controlled through its control socket, None if the Sendspin bridge is off.
sendspin_player: "Optional[SendspinPlayer]" = None

continue_conversation_sound: str = ""
media_player_entity: "Optional[MediaPlayerEntity]" = None
satellite: "Optional[VoiceSatelliteProtocol]" = None
connections: "List[VoiceSatelliteProtocol]" = field(default_factory=list)
Expand Down Expand Up @@ -248,6 +249,7 @@ class ServerState:
wake_words_changed: bool = False
refractory_seconds: float = 0.0
thinking_sound_enabled: bool = False
continue_conversation_sound_enabled: bool = False
button_controls_locked: bool = False
output_only: bool = False
muted: bool = False
Expand Down
33 changes: 29 additions & 4 deletions linux_voice_assistant/satellite.py
Original file line number Diff line number Diff line change
Expand Up @@ -999,10 +999,17 @@ def _start_continued_conversation() -> None:
self._pipeline_active = False
self.unduck()
return
_LOGGER.debug("Sending VoiceAssistantRequest to Home Assistant with wake_word_phrase '%s' (continued conversation)", self._wake_word_phrase)
self.send_messages([_pipeline_start_request(self._wake_word_phrase)])
self._is_streaming_audio = True
_LOGGER.debug("Continued conversation started")

if not (self.state.continue_conversation_sound_enabled and self.state.continue_conversation_sound):
self._send_continued_conversation_request()
elif self.state.listen_during_wake_sound:
_LOGGER.debug("Playing continued conversation sound while listening (listen_during_wake_sound enabled)")
self.state.tts_player.play(self.state.continue_conversation_sound)
self._send_continued_conversation_request()
else:
# The sound plays first, so the microphone does not stream it back to Home Assistant
_LOGGER.debug("Playing continued conversation sound before listening")
self.state.tts_player.play(self.state.continue_conversation_sound, done_callback=self._on_continued_conversation_sound_finished)

threading.Timer(self.state.continue_conversation_delay, _start_continued_conversation).start()
elif pipeline_ended and self.state.pipeline_ended_sound:
Expand All @@ -1017,6 +1024,24 @@ def _start_continued_conversation() -> None:

_LOGGER.debug("TTS response finished")

def _on_continued_conversation_sound_finished(self) -> None:
"""Callback invoked when the continued conversation sound finishes or is stopped; begin STT streaming unless the conversation was ended meanwhile."""
# tts_player.stop() also invokes this callback, after stop() or a disconnect has ended the conversation
if not self._pipeline_active or self.state.muted:
_LOGGER.debug("Skipping continued conversation after the continued conversation sound (pipeline active: %s, muted: %s)", self._pipeline_active, self.state.muted)
self._pipeline_active = False
self.unduck()
self._emit(LVAEvent.IDLE)
return
self._send_continued_conversation_request()

def _send_continued_conversation_request(self) -> None:
"""Ask Home Assistant to listen for the answer to a follow-up question."""
_LOGGER.debug("Sending VoiceAssistantRequest to Home Assistant with wake_word_phrase '%s' (continued conversation)", self._wake_word_phrase)
self.send_messages([_pipeline_start_request(self._wake_word_phrase)])
self._is_streaming_audio = True
_LOGGER.debug("Continued conversation started")

def _pipeline_ended_sound_finished(self) -> None:
self._pipeline_active = False
self.unduck()
Expand Down
89 changes: 89 additions & 0 deletions tests/unit/test_satellite_continued_conversation_sound.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
"""Sound played when the microphone opens again for a follow-up question."""

import logging
from unittest.mock import MagicMock

import pytest

from .conftest import make_satellite

SOUND = "/sounds/continued_conversation.flac"


@pytest.fixture(name="timer")
def timer_fixture(monkeypatch):
timer = MagicMock()
monkeypatch.setattr("linux_voice_assistant.satellite.threading.Timer", timer)
return timer


def _continue_conversation(tmp_path, timer, enabled=True, **state_overrides):
"""Finish a reply that asked a follow-up question and let the settle delay pass."""
satellite = make_satellite(tmp_path, {"continue_conversation_sound": SOUND, "continue_conversation_sound_enabled": enabled, **state_overrides})
satellite.send_messages = MagicMock()
satellite._continue_conversation = True
satellite._tts_finished()
satellite.send_messages.reset_mock()
timer.call_args.args[1]()
return satellite


def _finish_sound(satellite):
satellite.state.tts_player.play.call_args.kwargs["done_callback"]()


def test_listens_after_the_sound(tmp_path, timer):
satellite = _continue_conversation(tmp_path, timer)

assert satellite.state.tts_player.play.call_args.args == (SOUND,)
satellite.send_messages.assert_not_called()
assert not satellite._is_streaming_audio

_finish_sound(satellite)

assert satellite.send_messages.call_args.args[0][0].start
assert satellite._is_streaming_audio


def test_listens_at_once_with_listen_during_wake_sound(tmp_path, timer):
satellite = _continue_conversation(tmp_path, timer, listen_during_wake_sound=True)

satellite.state.tts_player.play.assert_called_once_with(SOUND)
assert satellite._is_streaming_audio


def test_no_sound_when_disabled(tmp_path, timer):
satellite = _continue_conversation(tmp_path, timer, enabled=False)

satellite.state.tts_player.play.assert_not_called()
assert satellite._is_streaming_audio


def test_no_sound_when_muted(tmp_path, timer):
satellite = _continue_conversation(tmp_path, timer, muted=True)

satellite.state.tts_player.play.assert_not_called()
assert not satellite._is_streaming_audio


def test_stop_during_the_sound_ends_the_conversation(tmp_path, timer, caplog):
satellite = _continue_conversation(tmp_path, timer)
satellite.state.tts_player.stop.side_effect = lambda: _finish_sound(satellite)

with caplog.at_level(logging.DEBUG):
satellite.stop()

satellite.send_messages.assert_not_called()
assert not satellite._is_streaming_audio
assert not satellite.pipeline_active
assert "Skipping continued conversation after the continued conversation sound" in caplog.text


def test_disconnect_during_the_sound_does_not_listen(tmp_path, timer):
satellite = _continue_conversation(tmp_path, timer)
satellite.state.tts_player.stop.side_effect = lambda: _finish_sound(satellite)

satellite.connection_lost(None)

satellite.send_messages.assert_not_called()
assert not satellite._is_streaming_audio
Loading