Fix bug with reading from non-pyaudio stream

pyaudio's stream.read is in samples whereas most other streams read in bytes
This commit is contained in:
Matthew D. Scholefield 2018-04-19 13:49:44 -05:00
parent 09767fa675
commit b74e80568b

View File

@ -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