使用python进行实时音频信号处理

问题描述 投票:0回答:3

我一直在尝试使用Python中的“pyAudio”模块进行实时音频信号处理。我所做的是一个简单的例子,从麦克风读取音频数据并通过耳机播放。我尝试使用以下代码(Python 和 Cython 版本)。以为它可以工作,但不幸的是它是失速的并且不够流畅。我该如何改进代码以使其顺利运行。我的电脑是 i7,8GB RAM。

Python版本

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()

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()
python audio signals real-time cython
3个回答
10
投票

我相信您缺少

CHUNK
作为
player.write
调用的第二个参数。

player.write(np.fromstring(stream.read(CHUNK),dtype=np.int16),CHUNK)

另外,不确定是否格式错误。但是

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()

最后,您可能需要将

rate
增加到
44100
CHUNK
1024
以及
CHANNEL
2
以获得更好的保真度。


8
投票

下面的代码将采用默认输入设备,并将录制的内容输出到默认输出设备中。

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()

这将运行 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()
© www.soinside.com 2019 - 2024. All rights reserved.