拣货设备
您可以通过 PyAudio API 获取支持的设备列表:
p = pyaudio.PyAudio()
host_info = p.get_host_api_info_by_index(0)
device_count = host_info.get('deviceCount')
# can iterate through devices and check their info:
p.get_device_info_by_host_api_device_index(0, device_index)
{'defaultSampleRate': 44100.0, 'defaultLowOutputLatency': 0.01, 'defaultLowInputLatency': 0.0029024943310657597, 'maxInputChannels': 2L, 'structVersion': 2L, 'hostApi': 0L, 'index': 0L, 'defaultHighOutputLatency': 0.1, 'maxOutputChannels': 0L, 'name': u'Built-in Microphone', 'defaultHighInputLatency': 0.013061224489795919}
实时录制和播放
可以通过打开和读取输入流来完成录制,并且可以同时通过输出流播放。
可以使用input_device_index或output_device_index选择设备
例如,这会立即录制和播放:
import pyaudio
BUFFER_SIZE = 4096
DURATION = 5
SAMPLE_RATE = 44100
p = pyaudio.PyAudio()
input_stream = p.open(
format=pyaudio.paInt16,
channels=2,
rate=SAMPLE_RATE,
input=True,
frames_per_buffer=BUFFER_SIZE,
input_device_index=0
)
output_stream = p.open(
format=pyaudio.paInt16,
channels=2,
rate=SAMPLE_RATE,
output=True
)
for i in xrange(int(SAMPLE_RATE / BUFFER_SIZE * DURATION)):
data = input_stream.read(BUFFER_SIZE)
output_stream.write(data)
input_stream.stop_stream()
input_stream.close()
output_stream.stop_stream()
output_stream.close()
p.terminate()
我并没有完全关注您关于在卡之间弹跳音频等的确切问题。但我相信您可以从我上面提到的内容中弄清楚。
希望对你有帮助