Skip to content

armgit / txtai

Public Imported from GitHub
0

💡 All-in-one AI framework for semantic search, LLM orchestration and language model workflows

Preserve stereo channels when AudioStream resamples playback (#1465)

Hua.T8540e393d92e
diff --git a/src/python/txtai/pipeline/audio/audiostream.py b/src/python/txtai/pipeline/audio/audiostream.py
index e0daf5a7..0ab77d86 100644
--- a/src/python/txtai/pipeline/audio/audiostream.py
+++ b/src/python/txtai/pipeline/audio/audiostream.py
@@ -85,7 +85,7 @@ class AudioStream(Pipeline):
audio, rate = self.queue.get()
while not isinstance(audio, int) or (audio, rate) != AudioStream.COMPLETE:
# Resample to target sample rate, if necessary
- audio, rate = (Signal.resample(audio, rate, self.rate), self.rate) if self.rate else (audio, rate)
+ audio, rate = (Signal.resample(audio.T, rate, self.rate).T, self.rate) if self.rate else (audio, rate)
# Play audio segment
sd.play(audio, rate, blocking=True)
diff --git a/test/python/testpipeline/testaudio/testaudiostream.py b/test/python/testpipeline/testaudio/testaudiostream.py
index 18eb54b8..fba6124b 100644
--- a/test/python/testpipeline/testaudio/testaudiostream.py
+++ b/test/python/testpipeline/testaudio/testaudiostream.py
@@ -2,10 +2,14 @@
AudioStream module tests
"""
+import tempfile
import unittest
+from pathlib import Path
+
from unittest.mock import patch
+import numpy as np
import soundfile as sf
from txtai.pipeline import AudioStream
@@ -19,6 +23,28 @@ class TestAudioStream(unittest.TestCase):
AudioStream tests.
"""
+ @patch("sounddevice.play")
+ def testStereoResampling(self, play):
+ """
+ Test resampling preserves stereo channels and adjusts frame count
+ """
+
+ with tempfile.TemporaryDirectory() as directory:
+ path = Path(directory) / "stereo.wav"
+ sf.write(path, np.tile([0.25, -0.5], (640, 1)), 16000)
+ audio, rate = sf.read(path)
+
+ for target, frames in ((8000, 320), (32000, 1280)):
+ with self.subTest(rate=target):
+ stream = AudioStream(rate=target)
+ stream([(audio, rate), AudioStream.COMPLETE])
+ stream.wait()
+
+ played, outputrate = play.call_args.args
+ self.assertEqual(outputrate, target)
+ self.assertEqual(played.shape, (frames, 2))
+ np.testing.assert_allclose(played, np.tile([0.25, -0.5], (frames, 1)))
+
@patch("sounddevice.play")
def testAudioStream(self, play):
"""