How to copy an audioSampleBuffer? Probably a dumb question

I’m trying to do a simple wet/dry mix of buffers in my MPESynthesiser. I am trying to set up the buffer processing like this:

void renderNextBlockCustom(AudioBuffer<float>& outputAudio,
		const MidiBuffer& inputMidi,
		int startSample,
		int numSamples)
	{
		MPESynthesiser::renderNextBlock(outputAudio, inputMidi, startSample, numSamples);

	//DISTORTION
	if (monoDistOnOff) {

		AudioBuffer<float> outputAudioPreMonoDist = outputAudio;
		
		monoLPF.renderNextBlock(outputAudio, startSample, numSamples);
		monoDistortion.renderNextBlock(outputAudio, startSample, numSamples);

		float* chanbufDistorted = outputAudio.getWritePointer(0);
		float* chanbufClean = outputAudioPreMonoDist.getWritePointer(0);

		for (int j = startSample; j < numSamples; ++j) {
			chanbufDistorted[j] = (chanbufClean[j] +chanbufDistorted[j])*0.5f;

			for (int i = 1; i < outputAudio.getNumChannels(); ++i) {
				outputAudio.copyFrom(i, startSample, outputAudio.getReadPointer(0), numSamples);
			}
		}

In other words, I am trying to make a new AudioBuffer<float> outputAudioPreMonoDist which is a clean copy of outputBuffer before the LPF & mono distortion units so that I can then later mix the two together and get a dry/wet mix.

But it’s not working. The clean sound is not getting in there. Even just doing this to try to get a pure clean output doesn’t work:

	if (monoDistOnOff) {

		AudioBuffer<float> outputAudioPreMonoDist = outputAudio;
		
		monoLPF.renderNextBlock(outputAudio, startSample, numSamples);
		monoDistortion.renderNextBlock(outputAudio, startSample, numSamples);

		float* chanbufDistorted = outputAudio.getWritePointer(0);
		float* chanbufClean = outputAudioPreMonoDist.getWritePointer(0);

		for (int j = startSample; j < numSamples; ++j) {
			chanbufDistorted[j] = chanbufClean[j];

			for (int i = 1; i < outputAudio.getNumChannels(); ++i) {
				outputAudio.copyFrom(i, startSample, outputAudio.getReadPointer(0), numSamples);
			}
		}

It just sounds the same with the distorted sound only. So I am clearly not actually copying the buffer with AudioBuffer<float> outputAudioPreMonoDist = outputAudio; Is this just a stupid syntax thing because it’s passed in by reference or what? How do I word this to make it work?

Yeah there’s other ways I can do this (like just iterating through the buffer once and doing the LPF, distortion, and clean mixing all at once per sample). For example, this works and it’s more efficient:

if (monoDistOnOff) {

		float* chanbuf = outputAudio.getWritePointer(0);
		
		for (int j = startSample; j < numSamples; ++j) {
			chanbuf[j] = (monoDistortion.getNextSample(monoDistLPF.processAudioSample(chanbuf[j], 0)) + chanbuf[j])*0.5f;

			for (int i = 1; i < outputAudio.getNumChannels(); ++i) {
				outputAudio.copyFrom(i, startSample, outputAudio.getReadPointer(0), numSamples);
			}
		}
	}

But I want to know why the original method wasn’t working too. Thanks a bunch.

C++ is fun :slight_smile:

  1. Try with:
    makeCopyOf

  2. Mind that AudioBuffer<Type>& is not exacly const AudioBuffer<Type>& :slight_smile:
    See the documentation as there are 2 operator= (one is copy and one is move).