pyaudio回调仅调用一次

时间:2019-09-29 22:54:28

标签: python-3.x callback pyaudio

我尝试将pyaudio与callback选项一起使用,我想产生数据而不是从文件中读取。当我使用回调选项时,它只会被调用一次。

存在another个问题,但同样的问题,但没有答案。我做了一个最小的可复制示例。使用阻塞时,该代码有效。

import time
import numpy as np
import scipy.signal
import sounddevice as sd
import pyaudio

sample_rate=44100
max_amp =  2**(15)-1
f0 = 500
duration = 1
f1 = 3000

x = np.arange(0, duration, 1/sample_rate)
y_float = max_amp*scipy.signal.chirp(x, f0, duration, f1)
y = y_float.astype(np.int16)
data = y.tostring()

def create_data_generator(data):
    periodsize = 1000
    for i in range(int(len(data)/(periodsize))):
        chunk = data[periodsize*i:periodsize*(i+1)]
        yield chunk

data_generator = create_data_generator(data)

def callback(in_data, frame_count, time_info, status):
    data = next(data_generator)
    return (data, pyaudio.paContinue)

# -------- blocking------------------
# periodsize = 1000
# p = pyaudio.PyAudio()
# stream = p.open(format=pyaudio.paInt16,
#         channels=1,
#         rate=sample_rate,
#         output=True)

# start = time.time()
# for i in range(int(len(data)/(periodsize))):
#     chunk, status = callback(0, 0, 0, 0)
#     stream.write(chunk)

# time.sleep(duration-(time.time()-start))

# stream.stop_stream()
# stream.close()
# p.terminate()

# -------- callback ------------------
periodsize = 1000
p = pyaudio.PyAudio()

stream = p.open(format=pyaudio.paInt16,
        channels=1,
        rate=sample_rate,
        output=True, 
        stream_callback=callback)

# start the stream (4)
stream.start_stream()

# wait for stream to finish (5)
while stream.is_active():
    time.sleep(0.1)

# stop stream (6)
stream.stop_stream()
stream.close()

# close PyAudio (7)
p.terminate()

0 个答案:

没有答案