2017-01-30 16:19:50 +01:00
|
|
|
import time
|
|
|
|
import numpy as np
|
2016-10-12 23:50:00 +02:00
|
|
|
import pyaudio
|
2016-10-14 07:27:45 +02:00
|
|
|
import config
|
2016-10-12 23:50:00 +02:00
|
|
|
|
|
|
|
|
|
|
|
def start_stream(callback):
|
|
|
|
p = pyaudio.PyAudio()
|
2017-01-30 16:19:50 +01:00
|
|
|
frames_per_buffer = int(config.MIC_RATE / config.FPS)
|
2016-10-12 23:50:00 +02:00
|
|
|
stream = p.open(format=pyaudio.paInt16,
|
|
|
|
channels=1,
|
2016-10-14 07:27:45 +02:00
|
|
|
rate=config.MIC_RATE,
|
2016-10-12 23:50:00 +02:00
|
|
|
input=True,
|
2017-01-30 16:19:50 +01:00
|
|
|
frames_per_buffer=frames_per_buffer)
|
|
|
|
overflows = 0
|
|
|
|
prev_ovf_time = time.time()
|
2016-10-12 23:50:00 +02:00
|
|
|
while True:
|
2017-01-30 16:19:50 +01:00
|
|
|
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))
|
2016-10-12 23:50:00 +02:00
|
|
|
stream.stop_stream()
|
|
|
|
stream.close()
|
|
|
|
p.terminate()
|