我正在开发一个实时音频处理动态链接库,其中有一个代表音频缓冲区的 float 据的二维 C 数组。一维是时间(样本),另一维是 channel 。我想将其作为用于 DSP 处理的 numpy 数组传递给 python 脚本,然后我想将其传递回 C,以便数据可以在 C 中的处理链中继续进行。C++ 中的成员函数执行处理看起来像这样:
void myEffect::process (float** inputs, float** outputs, int buffersize)
{
//Some processing stuff
}
数组输入 和输出 大小相等。整数buffersize 是输入 和输出 数组中的列数。在 python 方面,我希望通过如下所示的函数执行处理:
class myPyEffect
...
...
def process(self,inBuff):
#inBuff and outBuff should be numpy arrays
outBuff = inBuff * self.whatever # some DSP stuff
return outBuff
...
...
现在,我的问题是,如何以最有效的方式(避免不必要的内存复制等)将数据传入和传出 C?到目前为止,对于简单的参数更改,我一直在使用如下所示的 C-API 调用:
pValue = PyObject_CallMethod(pInstance, "setParameter", "(f)", value);
我是对我的 numpy 数组使用类似的东西还是有更好的方法?感谢阅读。
最佳答案
您可以完全避免处理 NumPy C API。 Python 可以使用 ctypes
模块调用 C 代码,您可以使用数组的 ctypes 属性访问指向 numpy 数据的指针。
这是一个显示一维平方和函数过程的最小示例。
ctsquare.c
#include <stdlib.h>
float mysumsquares(float * array, size_t size) {
float total = 0.0f;
size_t idx;
for (idx = 0; idx < size; ++idx) {
total += array[idx]*array[idx];
}
return total;
}
编译为ctsquare.so
这些命令行适用于 OS X,您的操作系统可能会有所不同。
$ gcc -O3 -fPIC -c ctsquare.c -o ctsquare.o
$ ld -dylib -o ctsquare.so -lc ctsquare.o
ctsquare.py
import numpy
import ctypes
# pointer to float type, for convenience
c_float_p = ctypes.POINTER(ctypes.c_float)
# load the library
ctsquarelib = ctypes.cdll.LoadLibrary("ctsquare.so")
# define the return type and arguments of the function
ctsquarelib.mysumsquares.restype = ctypes.c_float
ctsquarelib.mysumsquares.argtypes = [c_float_p, ctypes.c_size_t]
# python front-end function, takes care of the ctypes interface
def myssq(arr):
# make sure that the array is contiguous and the right data type
arr = numpy.ascontiguousarray(arr, dtype='float32')
# grab a pointer to the array's data
dataptr = arr.ctypes.data_as(c_float_p)
# this assumes that the array is 1-dimensional. 2d is more complex.
datasize = arr.ctypes.shape[0]
# call the C function
ret = ctsquarelib.mysumsquares(dataptr, datasize)
return ret
if __name__ == '__main__':
a = numpy.array([1,2,3,4])
print 'sum of squares of [1,2,3,4] =', myssq(a)
关于c++ - 初学者用 Python 扩展 C(特别是 Numpy),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2290007/