我试图通过连接音频设备的输出来记录一些数据,通过吉他踏板,然后回到同一个音频设备的线路输入。你知道吗
在pyAudio中,我可以毫无问题地输出到设备,但当我打开2个流(输入/输出)或1个流时,两个输入/输出pyAudio都停止工作。当我监视音频设备和stream.u是否处于活动状态总是返回true
我的代码:
from synthesizer import Player, Synthesizer, Waveform
import numpy as np
import time
BITRATE = 44100 #number of frames per second/frameset.
CHUNK = 8192
FORMAT = pyaudio.paInt16
CHANNELS = 1
RATE = 44100
SAMPLE_LENGTH = 5
# open audio device
py_audio = pyaudio.PyAudio()
# generate audio
synthesizer = Synthesizer(osc1_waveform=Waveform.square, osc1_volume=1.0, use_osc2=False, rate=BITRATE)
out_wave = synthesizer.generate_constant_wave(440.0, SAMPLE_LENGTH)
def stream_callback(wave, frame):
audio_data = (wave * float(2 ** 15 - 1)).astype(np.int16).tobytes()
index = 0
def callback(in_data, frame_count, time_info, status):
nonlocal audio_data
nonlocal index
nonlocal frame
print("got data!")
frame.append(in_data)
data_length = frame_count * CHANNELS * 2
data = audio_data[index:(index+data_length)]
index += data_length
return (data, pyaudio.paContinue)
return callback
frame_data = []
stream_options = {
"format": FORMAT,
"channels": CHANNELS,
"rate": BITRATE,
"output_device_index": 7,
"input_device_index": 2,
"output": True,
"input": True,
"stream_callback": stream_callback(out_wave, frame_data)
}
# Open Streams
stream_out = py_audio.open(**stream_options)
stream_out.start_stream()
# wait for stream to finish (5)
while stream_out.is_active():
time.sleep(0.1)
print("Done!")
stream_out.stop_stream()
stream_out.close()
py_audio.terminate()
你得选一个。Pyaudio在同一对象中不支持两者。你知道吗
相关问题 更多 >
编程相关推荐