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

时间:2017-09-24 02:29:22

标签: python audio signals real-time cython

我一直在尝试使用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()

3 个答案:

答案 0 :(得分:1)

我相信你错过CHUNK作为player.write来电的第二个参数。

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

另外,不确定它的格式错误。但player.write需要标记为for循环

根据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增加到Options -MultiViews RewriteEngine On RewriteCond %{REQUEST_FILENAME} !-f RewriteRule ^ index.html [QSA,L] ,以获得更高的保真度。< / p>

答案 1 :(得分:1)

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

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 docs中所述: http://people.csail.mit.edu/hubert/pyaudio/docs/#pyaudio.PyAudio.open

答案 2 :(得分:1)

我正在从事类似的项目。我修改了您的代码,现在停滞了。块越大,延迟越大。这就是为什么我保持低调。

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