Enhance pipeline audio in the executor

enhance_chunk blocks the event loop for a bit every
time its called because its CPU intensive
This commit is contained in:
J. Nick Koston 2024-11-18 14:54:30 -06:00
parent 2cf3f2b243
commit 2dc113ba50
No known key found for this signature in database
2 changed files with 8 additions and 2 deletions

View File

@ -575,8 +575,11 @@ class PipelineRun:
_device_id: str | None = None
"""Optional device id set during run start."""
_loop: asyncio.AbstractEventLoop | None = field(init=False, default=None)
def __post_init__(self) -> None:
"""Set language for pipeline."""
self._loop = asyncio.get_running_loop()
self.language = self.pipeline.language or self.hass.config.language
# wake -> stt -> intent -> tts
@ -1284,6 +1287,7 @@ class PipelineRun:
assert self.audio_enhancer is not None
timestamp_ms = 0
assert self._loop is not None
async for dirty_samples in audio_stream:
if self.audio_settings.volume_multiplier != 1.0:
# Static gain
@ -1295,7 +1299,9 @@ class PipelineRun:
for dirty_chunk in chunk_samples(
dirty_samples, BYTES_PER_CHUNK, self.audio_chunking_buffer
):
yield self.audio_enhancer.enhance_chunk(dirty_chunk, timestamp_ms)
yield await self._loop.run_in_executor(
None, self.audio_enhancer.enhance_chunk, dirty_chunk, timestamp_ms
)
timestamp_ms += MS_PER_CHUNK

View File

@ -708,7 +708,7 @@ async def test_wake_word_detection_aborted(
assert process_events(events) == snapshot
def test_pipeline_run_equality(hass: HomeAssistant, init_components) -> None:
async def test_pipeline_run_equality(hass: HomeAssistant, init_components) -> None:
"""Test that pipeline run equality uses unique id."""
def event_callback(event):