实时处理音频缓冲区-Python 3.7

问题描述

使用“声音设备”库,我构建了一个python 3.7程序,该程序从音频设备接收缓冲区(512个样本),对其进行处理并将其发送到同一设备。 “音频设备”是一种声卡,因此可以连接麦克风,处理输入并将其处理后实时发送给扬声器。

该过程是立体声的(两个通道),我正在尝试使其更加高效。这是一个简单的低通滤波器。

接收缓冲区: 512个形状如下的样本:

[
[XLeft1,XRight1]
[XLeft2,XRight2]
...
[XLeft512,XRight512]
]

过程: 必须逐个样本进行采样,例如:[Xleft1,XLeft2,...,XLeft512],然后对Right通道相同。

缓冲区不足 必须与缓冲区中的相同。所以在这里,我需要做一些数据转换,尝试将其最小化。

最有效的CPU方式是什么?

我现在正在使用的代码:

def do_process(self,indata):
  Y=[]
  for i in range(0,len(indata[0])):                    #Two channels for stereo
      v=indata[:,i]# i represent number of channel     #v is the current channel 
      if i ==0:
       current_chunk_filter=self.low_pass1.do_calculation(v)   
      if i==1:
       current_chunk_filter=self.low_pass2.do_calculation(v)
      Y.insert(i,current_chunk_filter)
      
  outdata=list(map(list,zip(*Y)))# transpose the list
  dimensioned_numpy_array = np.reshape(outdata,(int(len(outdata)),2)) #Makes the list vertical
  return   dimensioned_numpy_array 

如何避免(或提高效率)从列表,转置,重塑等等?

解决方法

您的输出块不会更改大小,一次只能存在一个。因此,请对其进行预先分配,然后将输出数据直接放入正确的位置。

类似的东西:

channels = 2
samples = 512
out = numpy.zeros(shape=(samples,channels))
filters = [self.low_pass1,self.low_pass2]

def do_process(in,out):
  
  for channel in range(in.shape[1]):
     out[:,channel] = filter[channel].do_calculation(in[:,channel])


相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...