Can't get second channel of Buffer


I can’t seem to get only the right channel of a stereo AudioSource to output.

If I run this:

All is well and I get the stereo output.

If I run this:
auto writePointerL = bufferToFill.buffer->getWritePointer(0);
AudioBuffer proxyBufferL(&writePointerL, 1, bufferToFill.buffer->getNumSamples());
AudioSourceChannelInfo proxyInfoL(&proxyBufferL, bufferToFill.startSample, bufferToFill.numSamples);

All is well and I get the left channel.

But if I run this:
auto writePointerR = bufferToFill.buffer->getWritePointer(1);
AudioBuffer proxyBufferR(&writePointerR, 1, bufferToFill.buffer->getNumSamples());
AudioSourceChannelInfo proxyInfoR(&proxyBufferR, bufferToFill.startSample, bufferToFill.numSamples);

It still gives me the left channel.


I’d try stepping into getWritePointer() to see what it is doing. It’s a very simple function (which asserts if the index is out of range, but does no runtime checks other than that). You can inspect bufferToFill in the debugger, to see what its numChannels member is, and to see what it contains in its channels member. Perhaps both channels[0] and channels[1] point to the same memory?

1 Like

Thanks for the reply!

I did some serious debugging and got it working!
For those who are having this problem there is a slight discrepancy in the source code.
The getWritePointer() was working as it should. The problem was with calling getNextAudioBlock() on the mixer.

My chain looks like this:

The problem is with the AudioFormatReader->read function.
This function takes two bool arguments(…, useReaderLeftChan, useReaderRightChan)
These are both set to true. Which means if you ask for only the right channel it will still read both in and give you the left channel.

To solve this I just created another constructor for the AudioSourceChannelInfo Struct which takes in a AudioChannelSet::ChannelType. Then I just did some simple conditional statements to decide which channels to read.

Hopefully whoever else has this problem sees this post and doesn’t have to pull out as much hair as I did.

P.S future people DM if you need more info.