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:
parent
09767fa675
commit
b74e80568b
@ -124,6 +124,7 @@ class PreciseRunner(object):
|
|||||||
self.on_prediction = on_prediction
|
self.on_prediction = on_prediction
|
||||||
self.on_activation = on_activation
|
self.on_activation = on_activation
|
||||||
self.chunk_size = engine.chunk_size
|
self.chunk_size = engine.chunk_size
|
||||||
|
self.read_divisor = 1
|
||||||
|
|
||||||
self.pa = None
|
self.pa = None
|
||||||
self.thread = None
|
self.thread = None
|
||||||
@ -131,6 +132,19 @@ class PreciseRunner(object):
|
|||||||
self.is_paused = False
|
self.is_paused = False
|
||||||
atexit.register(self.stop)
|
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):
|
def start(self):
|
||||||
"""Start listening from stream"""
|
"""Start listening from stream"""
|
||||||
if self.stream is None:
|
if self.stream is None:
|
||||||
@ -140,6 +154,8 @@ class PreciseRunner(object):
|
|||||||
16000, 1, paInt16, True, frames_per_buffer=self.chunk_size
|
16000, 1, paInt16, True, frames_per_buffer=self.chunk_size
|
||||||
)
|
)
|
||||||
|
|
||||||
|
self.read_divisor = self._calc_read_divisor()
|
||||||
|
|
||||||
self.engine.start()
|
self.engine.start()
|
||||||
self.running = True
|
self.running = True
|
||||||
self.is_paused = False
|
self.is_paused = False
|
||||||
@ -173,7 +189,7 @@ class PreciseRunner(object):
|
|||||||
"""Continuously check Precise process output"""
|
"""Continuously check Precise process output"""
|
||||||
activation = 0
|
activation = 0
|
||||||
while self.running:
|
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:
|
if self.is_paused:
|
||||||
continue
|
continue
|
||||||
|
Loading…
Reference in New Issue
Block a user