Question

I'm working with DSP in code that's a mashup of objective C++ and C.

The CoreAudio framework is providing me buffers of audio sample data as float pointers

e.g. float *bufferData = (float *)malloc(sizeof(float) * 1024);

The C function that I'm calling to do processing on the sample data requires a pointer of type double.

double returnAValue(double *sampleData, int samplecount);

What's the most CPU efficient way of converting my float pointer into a double pointer before passing it to this function?

Was it helpful?

Solution

One of the many functions that the Accelerate framework provides is vDSP_vspdp, which you can use like this:

float * singleBuf;
float * doubleBuf = malloc(nElements * sizeof(double));
vDSP_vspdp(singleBuf, 1, doubleBuf, 1, nElements);

If you're doing any kind of DSP programming on a Mac, you could save tons of time and gain tons of efficiency by learning about vDSP, CBLAS, and other Accelerate framework APIs. These APIs tend to go through the processor's vector unit (SSE, etc) and know about memory hinting, so they tend to be much more efficient than what you'd typically code by hand.

OTHER TIPS

Changing the type of the pointer does not change the type of the underlying data. If you are trying to link together two packages that expect different contents in the buffers, you are going to have to copy the data from one buffer to another, converting on the way.

So your question is: given a buffer full of floats, how do you convert it to a buffer full of doubles? A simple for loop is probably your best bet.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top