擅长:python、mysql、java
<p>下面的代码将采用默认输入设备,并将记录的内容输出到默认输出设备中。</p>
<pre><code>import PyAudio
import numpy as np
p = pyaudio.PyAudio()
CHANNELS = 2
RATE = 44100
def callback(in_data, frame_count, time_info, flag):
# using Numpy to convert to array for processing
# audio_data = np.fromstring(in_data, dtype=np.float32)
return in_data, pyaudio.paContinue
stream = p.open(format=pyaudio.paFloat32,
channels=CHANNELS,
rate=RATE,
output=True,
input=True,
stream_callback=callback)
stream.start_stream()
while stream.is_active():
time.sleep(20)
stream.stop_stream()
print("Stream is stopped")
stream.close()
p.terminate()
</code></pre>
<p>这将持续20秒并停止。方法回调用于处理信号:
<code>audio_data = np.fromstring(in_data, dtype=np.float32)</code></p>
<p><code>return in_data</code>是将后处理数据发送回输出设备的位置。</p>
<p>注释块的默认参数为1024,如PyAudio文档中所述:
<a href="http://people.csail.mit.edu/hubert/pyaudio/docs/#pyaudio.PyAudio.open" rel="nofollow noreferrer">http://people.csail.mit.edu/hubert/pyaudio/docs/#pyaudio.PyAudio.open</a></p>