Change microphone.py, decodes data before callback
Microphone.py is now responsible for decoding audio data. The decoded data now passed to the callback function
This commit is contained in:
parent
35c26ca7bb
commit
ee0eda9a4c
@ -1,16 +1,29 @@
|
|||||||
|
import time
|
||||||
|
import numpy as np
|
||||||
import pyaudio
|
import pyaudio
|
||||||
import config
|
import config
|
||||||
|
|
||||||
|
|
||||||
def start_stream(callback):
|
def start_stream(callback):
|
||||||
p = pyaudio.PyAudio()
|
p = pyaudio.PyAudio()
|
||||||
|
frames_per_buffer = int(config.MIC_RATE / config.FPS)
|
||||||
stream = p.open(format=pyaudio.paInt16,
|
stream = p.open(format=pyaudio.paInt16,
|
||||||
channels=1,
|
channels=1,
|
||||||
rate=config.MIC_RATE,
|
rate=config.MIC_RATE,
|
||||||
input=True,
|
input=True,
|
||||||
frames_per_buffer=int(config.MIC_RATE / config.FPS))
|
frames_per_buffer=frames_per_buffer)
|
||||||
|
overflows = 0
|
||||||
|
prev_ovf_time = time.time()
|
||||||
while True:
|
while True:
|
||||||
callback(stream)
|
try:
|
||||||
|
y = np.fromstring(stream.read(frames_per_buffer), dtype=np.int16)
|
||||||
|
y = y.astype(np.float32)
|
||||||
|
callback(y)
|
||||||
|
except IOError:
|
||||||
|
overflows += 1
|
||||||
|
if time.time() > prev_ovf_time + 1:
|
||||||
|
prev_ovf_time = time.time()
|
||||||
|
print('Audio buffer has overflowed {} times'.format(overflows))
|
||||||
stream.stop_stream()
|
stream.stop_stream()
|
||||||
stream.close()
|
stream.close()
|
||||||
p.terminate()
|
p.terminate()
|
||||||
|
Loading…
x
Reference in New Issue
Block a user