AudioBlock missing getArrayOfChannelPointers() function?

Is there a specific reason for not having this kind of function? I just tried to inherit from AudioBlock to implement my own function, but the member channels is private…

If you take a close look at how it works internally, you’ll see that the AudioBlock stores an array of channel pointer and a sample offset. The getChannelPointer function is implemented like this:

/** Returns a raw pointer into one of the channels in this block. */
SampleType* getChannelPointer (size_t channel) const noexcept
    jassert (channel < numChannels);
    jassert (numSamples > 0);
    return channels[channel] + startSample;

This means, returning the channels array directly would not allow you to take that offset into account. So, if you are dealing with legacy APIs that take a 2d pointer array as argument, you’ll need to create a temporary one on the stack and pass it to that function. You could build a helper function like e.g.

template <size_t maxNumChans, typename SampleType>
std::array<SampleType*, maxNumChans> getArrayOfChannelPointers (const juce::dsp::AudioBlock<SampleType>& block)
    std::array<SampleType*, maxNumChans> a;
    auto numChans = block.getNumChannels();

    jassert (maxNumChannels >= numChans);

    for (size_t c = 0; c < numChans; ++c)
        a[c] = block.getChannelPointer (c);

    return a;

I personally found it most convenient to rebuild all functions taking raw pointer arrays so that they accept Audio blocks directly – you can still re-add a “legacy” overload that converts a pointer array into a block, which works straightforward in contrast to the other direction, to not break any API. But obviously, that only works in case you have control over the functions you are calling.

1 Like

Thank you @PluginPenguin! That makes sense :wink: