From b74e80568b6432d00a704e29ff62d66a18bf962d Mon Sep 17 00:00:00 2001 From: "Matthew D. Scholefield" Date: Thu, 19 Apr 2018 13:49:44 -0500 Subject: [PATCH] Fix bug with reading from non-pyaudio stream pyaudio's stream.read is in samples whereas most other streams read in bytes --- runner/precise_runner/runner.py | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/runner/precise_runner/runner.py b/runner/precise_runner/runner.py index ecb199c..122c0e0 100644 --- a/runner/precise_runner/runner.py +++ b/runner/precise_runner/runner.py @@ -124,6 +124,7 @@ class PreciseRunner(object): self.on_prediction = on_prediction self.on_activation = on_activation self.chunk_size = engine.chunk_size + self.read_divisor = 1 self.pa = None self.thread = None @@ -131,6 +132,19 @@ class PreciseRunner(object): self.is_paused = False atexit.register(self.stop) + def _calc_read_divisor(self): + """ + pyaudio.Stream.read takes samples as n, not bytes + so read(n) should be read(n // sample_depth + """ + try: + import pyaudio + if isinstance(self.stream, pyaudio.Stream): + return 2 + except ImportError: + pass + return 1 + def start(self): """Start listening from stream""" if self.stream is None: @@ -140,6 +154,8 @@ class PreciseRunner(object): 16000, 1, paInt16, True, frames_per_buffer=self.chunk_size ) + self.read_divisor = self._calc_read_divisor() + self.engine.start() self.running = True self.is_paused = False @@ -173,7 +189,7 @@ class PreciseRunner(object): """Continuously check Precise process output""" activation = 0 while self.running: - chunk = self.stream.read(self.chunk_size // 2) + chunk = self.stream.read(self.chunk_size // self.read_divisor) if self.is_paused: continue