2017-12-18 21:50:27 +01:00
|
|
|
import time
|
|
|
|
import numpy as np
|
|
|
|
import pyaudio
|
2018-02-27 21:12:08 +01:00
|
|
|
import lib.config as config
|
2017-12-18 21:50:27 +01:00
|
|
|
|
|
|
|
|
|
|
|
def start_stream(callback):
|
|
|
|
p = pyaudio.PyAudio()
|
2018-02-27 21:12:08 +01:00
|
|
|
frames_per_buffer = int(config.settings["configuration"]["MIC_RATE"] / config.settings["configuration"]["FPS"])
|
2017-12-18 21:50:27 +01:00
|
|
|
stream = p.open(format=pyaudio.paInt16,
|
|
|
|
channels=1,
|
2018-02-27 21:12:08 +01:00
|
|
|
rate=config.settings["configuration"]["MIC_RATE"],
|
2017-12-18 21:50:27 +01:00
|
|
|
input=True,
|
|
|
|
frames_per_buffer=frames_per_buffer)
|
|
|
|
overflows = 0
|
|
|
|
prev_ovf_time = time.time()
|
|
|
|
while True:
|
|
|
|
try:
|
2018-02-27 21:12:08 +01:00
|
|
|
y = np.fromstring(stream.read(frames_per_buffer), dtype=np.int16)
|
2017-12-18 21:50:27 +01:00
|
|
|
y = y.astype(np.float32)
|
|
|
|
callback(y)
|
|
|
|
except IOError:
|
|
|
|
overflows += 1
|
|
|
|
if time.time() > prev_ovf_time + 1:
|
|
|
|
prev_ovf_time = time.time()
|
2018-02-27 21:12:08 +01:00
|
|
|
if config.settings["configuration"]["USE_GUI"]:
|
2017-12-18 21:50:27 +01:00
|
|
|
gui.label_error.setText('Audio buffer has overflowed {} times'.format(overflows))
|
|
|
|
else:
|
|
|
|
print('Audio buffer has overflowed {} times'.format(overflows))
|
|
|
|
stream.stop_stream()
|
|
|
|
stream.close()
|
|
|
|
p.terminate()
|