Can JUCE audio buffers not be separated by channels?

I have old Oboe code that I’m moving to Juce. It processes buffers in a different way than Juce (I guess).

For example, a stereo channel on Oboe is ABABABABABAB where A is a float from the first channel and B is a float from the second channel.

On juce, it looks like there are separate buffers for each channel:

void processBlock (juce::AudioSampleBuffer& buffer, juce::MidiBuffer&) override
{
    auto* audioData = buffer.getReadPointer(0);
    //I'd like to pass audioData for some old oboe code to fill, but it would have to be in the format ABABABABAB

Is there a way that I can get the juce buffer in this desired format that oboe uses?

Juce AudioBuffers are arranged like :

AAAAAAAA...
BBBBBBBB...

That is, they are split audio buffers. If you need to handle your data as interleaved buffers like ABABABABAB... , you will need to have some helper buffers and do some conversions between the split and interleaved channel formats.

template<typename T>
inline void interleave (T* const dst,
                        const T* const *const src,
                        const int channels,
                        const int count)
{
    switch (channels) {
        case 1:
            memcpy (dst, src[0], (size_t) count * sizeof (T));
            return;
        default:
            int idx = 0;
            for (int i = 0; i < count; ++i) {
                for (int j = 0; j < channels; ++j) {
                    dst[idx++] = src[j][i];
                }
            }
    }
}

There is already a library function for that, just have a look at juce:: AudioDataConverters::interleaveSamples

https://docs.juce.com/master/classAudioDataConverters.html#aac3f177b8617346f5cb1dd93cf09d152

3 Likes

Problem is where/how to allocate the temporary memory if the block size is not constant/known.

You can just use prepareToPlay… Because you’re interleaving samples in this case, you can just allocate a buffer of blocksize * 2 samples in prepareToPlay, and you should be golden…

1 Like

The maximumExpectedSamplesPerBlock value is a strong hint about the maximum number of samples that will be provided in each block. You may want to use this value to resize internal buffers. You should program defensively in case a buggy host exceeds this value. The actual block sizes that the host uses may be different each time the callback happens: completely variable block sizes can be expected from some hosts.

Is it frequent to have an host that exceeds the block size value provided in prepareToPlay?

EDIT: Ok, i should have searched more… :roll_eyes: