Float* of multichannel samples into a buffer

I am receiving a float* of samples, but the channels come one after the other, so if the sample Number is 12 and the channel number is 2, the float* will point to 24 elements.

what is the best way to get this into a multichannel buffer?

I’ve done it when the channel is just 1, but after that I’m stuck

AudioBuffer<float> myBuffer(&audioFileToLoad->samples, audioFileToLoad->channelNumber, audioFileToLoad->numOfSamples);```

Maybe AudioDataConverters::deinterleaveSamples is what you are looking for. It doesn’t take in an AudioBuffer directly as the destination but you can get the appropriate array of pointers with the AudioBuffer::getArrayOfWritePointers method.

I am a bit curious, though. Why are you dealing with interleaved samples coming in from an audio file? Juce already has facilities to deal with audio files together with its native AudioBuffer class…

This is in a DLL and the host handling the data file is passing the sample data this way (not by my choice - although there is some method to the madness upstream)

I looked and didn’t quite understand what the process would be.

My assumption is that if I’m passing the samples into the buffer and the channel number is > 1, then the source must be float**

maybe my assumption is wrong?

You won’t be able to use the data from the float* array directly with Juce’s AudioBuffer if it has more than 1 channels. You need to convert it, which will use CPU and additional memory. (Of course, if you can get the data from the host as split audio channels via a float**, then the situation is better…Unless the host also would need to do a similar double memory use and conversion process itself.)

Anyway, here’s how to use the audio data converter of Juce :

float* mysourcedata = nullptr; // obviously this would be a pointer to your actual data
int numChannels = 2;
int numSamples = 1024;
AudioBuffer<float> destination(numChannels, numSamples);
AudioDataConverters::deinterleaveSamples(mysourcedata, destination.getArrayOfWritePointers(), numSamples, numChannels);
1 Like

i think the host will need to do the interleaving and double memory use anyway :slight_smile:

probably best in JUCE, where we know what’s going on :wink:

thanks for the snippet

If the host itself is written with Juce, it isn’t a particularly good idea to pass around audio buffers as float*'s. (Because Juce’s AudioBuffer is a split channel buffer that isn’t compatible with interleaved data.)

You can check out the AudioFormatReader class’ ReadHelper to check out how they change the buffer layout if necessary.

Rail

1 Like