Saj*_*C K 11 python audio signals real-time cython
我一直在尝试使用 python 中的“pyAudio”模块进行实时音频信号处理。我所做的是从麦克风读取音频数据并通过耳机播放的简单案例。我尝试使用以下代码(Python 和 Cython 版本)。认为它有效,但不幸的是它是停顿并且不够流畅。我该如何改进代码才能让它顺利运行。我的电脑是 i7,8GB 内存。
蟒蛇版
import pyaudio
import numpy as np
RATE = 16000
CHUNK = 256
p = pyaudio.PyAudio()
player = p.open(format=pyaudio.paInt16, channels=1, rate=RATE, output=True,
frames_per_buffer=CHUNK)
stream = p.open(format=pyaudio.paInt16, channels=1, rate=RATE, input=True, frames_per_buffer=CHUNK)
for i in range(int(20*RATE/CHUNK)): #do this for 10 seconds
player.write(np.fromstring(stream.read(CHUNK),dtype=np.int16))
stream.stop_stream()
stream.close()
p.terminate()
Run Code Online (Sandbox Code Playgroud)
Cython 版本
import pyaudio
import numpy as np
cdef int RATE = 16000
cdef int CHUNK = 1024
cdef int i
p = pyaudio.PyAudio()
player = p.open(format=pyaudio.paInt16, channels=1, rate=RATE, output=True, frames_per_buffer=CHUNK)
stream = p.open(format=pyaudio.paInt16, channels=1, rate=RATE, input=True, frames_per_buffer=CHUNK)
for i in range(500): #do this for 10 seconds
player.write(np.fromstring(stream.read(CHUNK),dtype=np.int16))
stream.stop_stream()
stream.close()
p.terminate()
Run Code Online (Sandbox Code Playgroud)
我相信你缺少CHUNK第二个player.write调用参数。
player.write(np.fromstring(stream.read(CHUNK),dtype=np.int16),CHUNK)
Run Code Online (Sandbox Code Playgroud)
另外,不确定它的格式是否错误。但player.write需要加入for循环
每pyaudio网站,你需要有RATE / CHUNK * RECORD_SECONDS,而不是RECORD *RATE/CHUNK作为python执行*乘法之前/分裂。
for i in range(int(20*RATE/CHUNK)): #do this for 10 seconds
player.write(np.fromstring(stream.read(CHUNK),dtype=np.int16),CHUNK)
stream.stop_stream()
stream.close()
p.terminate()
Run Code Online (Sandbox Code Playgroud)
最后,你可能要增加rate对44100,CHUNK对1024,并CHANNEL以2更好的保真度。
下面的代码将采用默认输入设备,并将记录的内容输出到默认输出设备中。
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()
Run Code Online (Sandbox Code Playgroud)
这将运行 20 秒并停止。方法回调是您可以处理信号的地方:
audio_data = np.fromstring(in_data, dtype=np.float32)
return in_data是将后处理数据发送回输出设备的地方。
注意 chunk 的默认参数为 1024,如 PyAudio 文档中所述: http://people.csail.mit.edu/hubert/pyaudio/docs/#pyaudio.PyAudio.open
小智 5
我正在做一个类似的项目。我修改了你的代码,现在摊位已经消失了。块越大,延迟越大。这就是为什么我保持低调。
import pyaudio
import numpy as np
CHUNK = 2**5
RATE = 44100
LEN = 10
p = pyaudio.PyAudio()
stream = p.open(format=pyaudio.paInt16, channels=1, rate=RATE, input=True, frames_per_buffer=CHUNK)
player = p.open(format=pyaudio.paInt16, channels=1, rate=RATE, output=True, frames_per_buffer=CHUNK)
for i in range(int(LEN*RATE/CHUNK)): #go for a LEN seconds
data = np.fromstring(stream.read(CHUNK),dtype=np.int16)
player.write(data,CHUNK)
stream.stop_stream()
stream.close()
p.terminate()
Run Code Online (Sandbox Code Playgroud)