# Can JUCE audio buffers not be separated by channels?

**URL:** <https://forum.juce.com/t/can-juce-audio-buffers-not-be-separated-by-channels/46070>\
**Category:** General JUCE discussion\
**Created:** [May 18, 2021, 3:27am UTC](https://forum.juce.com/t/can-juce-audio-buffers-not-be-separated-by-channels/46070 "2021-05-18T03:27:18Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![guerlando](https://avatars.discourse-cdn.com/v4/letter/g/db5fbb/32.png) [@guerlando](https://forum.juce.com/u/guerlando)\
**Post date:** [May 18, 2021, 3:27am UTC](https://forum.juce.com/t/can-juce-audio-buffers-not-be-separated-by-channels/46070/1 "2021-05-18T03:27:18Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![xenakios](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/xenakios/32/4047_2.png) [@xenakios](https://forum.juce.com/u/xenakios)\
**Post date:** [May 18, 2021, 3:52am UTC](https://forum.juce.com/t/can-juce-audio-buffers-not-be-separated-by-channels/46070/2 "2021-05-18T03:52:54Z")

</div>

Juce AudioBuffers are arranged like :

```auto
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.

---

<div class="post-metadata">

**Author:** ![benvining](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/benvining/32/9922_2.png) [@benvining](https://forum.juce.com/u/benvining)\
**Post date:** [May 18, 2021, 5:35am UTC](https://forum.juce.com/t/can-juce-audio-buffers-not-be-separated-by-channels/46070/3 "2021-05-18T05:35:57Z")

</div>

```cpp
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];
                }
            }
    }
}

```

---

<div class="post-metadata">

**Author:** ![PluginPenguin](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/pluginpenguin/32/10203_2.png) [@PluginPenguin](https://forum.juce.com/u/PluginPenguin)\
**Post date:** [May 18, 2021, 6:58am UTC](https://forum.juce.com/t/can-juce-audio-buffers-not-be-separated-by-channels/46070/4 "2021-05-18T06:58:36Z")

</div>

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

[https://docs.juce.com/master/classAudioDataConverters.html#aac3f177b8617346f5cb1dd93cf09d152](https://docs.juce.com/master/classAudioDataConverters.html#aac3f177b8617346f5cb1dd93cf09d152)

---

<div class="post-metadata">

**Author:** ![nicolasdanet](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/nicolasdanet/32/56_2.png) [@nicolasdanet](https://forum.juce.com/u/nicolasdanet)\
**Post date:** [May 18, 2021, 8:47am UTC](https://forum.juce.com/t/can-juce-audio-buffers-not-be-separated-by-channels/46070/5 "2021-05-18T08:47:46Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![benvining](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/benvining/32/9922_2.png) [@benvining](https://forum.juce.com/u/benvining)\
**Post date:** [May 18, 2021, 8:56am UTC](https://forum.juce.com/t/can-juce-audio-buffers-not-be-separated-by-channels/46070/6 "2021-05-18T08:56:06Z")

</div>

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…

---

<div class="post-metadata">

**Author:** ![nicolasdanet](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/nicolasdanet/32/56_2.png) [@nicolasdanet](https://forum.juce.com/u/nicolasdanet)\
**Post date:** [May 18, 2021, 9:30am UTC](https://forum.juce.com/t/can-juce-audio-buffers-not-be-separated-by-channels/46070/7 "2021-05-18T09:30:28Z")

</div>

> 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… 🙄

> [@BlockSize and BufferSize](https://forum.juce.com/t/blocksize-and-buffersize/23204):
>
> I just wondered - Is there any guarantee that the size of the AudioSampleBuffer in AudioProcessor::ProcessBlock is always smaller or equal to the blocksize? I know it should be but I would like to be on the safe side. Thanks!
