Skip to content

Commit

Permalink
refactor(audio): convert AudioPlayChimeEvents to `AudioPlayAudioEve…
Browse files Browse the repository at this point in the history
…nt`s instead of directly playing the chime
  • Loading branch information
sassanh committed Sep 28, 2024
1 parent f953da8 commit c453747
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 6 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
- feat(display): add `DisplayCompressedRenderEvent` as a compressed version of `DisplayRenderEvent`
- feat(rpc): add reflection to rpc server, limited to root service, but good enough for health checking purposes
- refactor(rpc): preserve the order of fields of `oneof` declarations generated for `Union` types
- refactor(audio): convert `AudioPlayChimeEvent`s to `AudioPlayAudioEvent`s instead of directly playing the chime

## Version 0.17.0

Expand Down
25 changes: 19 additions & 6 deletions ubo_app/services/000-audio/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from __future__ import annotations

import asyncio
import wave
from pathlib import Path
from typing import TYPE_CHECKING, ParamSpec

Expand Down Expand Up @@ -54,12 +55,24 @@ def _(volume: float) -> None:
def _(is_mute: bool) -> None: # noqa: FBT001
audio_manager.set_playback_mute(mute=is_mute)

store.subscribe_event(
AudioPlayChimeEvent,
lambda event: audio_manager.play_file(
Path(__file__).parent.joinpath(f'sounds/{event.name}.wav').as_posix(),
),
)
def play_file(event: AudioPlayChimeEvent) -> None:
filename = Path(__file__).parent.joinpath(f'sounds/{event.name}.wav').as_posix()
with wave.open(filename, 'rb') as wave_file:
sample_rate = wave_file.getframerate()
channels = wave_file.getnchannels()
sample_width = wave_file.getsampwidth()
audio_data = wave_file.readframes(wave_file.getnframes())

store.dispatch(
AudioPlayAudioEvent(
rate=sample_rate,
channels=channels,
width=sample_width,
sample=audio_data,
),
)

store.subscribe_event(AudioPlayChimeEvent, play_file)

store.subscribe_event(
AudioPlayAudioEvent,
Expand Down

0 comments on commit c453747

Please sign in to comment.