【问题标题】:Manipulating audio buffers in real time - Python 3.7实时操作音频缓冲区 - Python 3.7
【发布时间】:2020-08-12 13:11:11
【问题描述】:

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

这个过程是立体声的(两个通道),我正在努力提高它的效率。这是一个简单的低通滤波器。

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

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

流程: 必须逐个采样,如下所示:[ Xleft1, XLeft2 , ... , XLeft512 ] 然后右声道也一样。

输出缓冲区 必须与缓冲区相同。所以在这里我需要做一些我试图最小化的数据转换。

完成此任务的 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 

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

【问题讨论】:

    标签: python list signal-processing real-time audio-processing


    【解决方案1】:

    您的输出块不会改变大小,并且一次只存在一个。所以预先分配好,然后把输出数据直接放到正确的位置。

    类似:

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

    【讨论】:

      猜你喜欢
      • 2018-07-22
      • 1970-01-01
      • 2014-11-14
      • 1970-01-01
      • 2021-09-07
      • 2021-03-25
      • 2018-03-08
      • 2016-01-10
      • 2011-10-31
      相关资源
      最近更新 更多