# AudioApp parallel buffer processing

**URL:** <https://forum.juce.com/t/audioapp-parallel-buffer-processing/28445>\
**Category:** Development\
**Created:** [July 7, 2018, 8:14am UTC](https://forum.juce.com/t/audioapp-parallel-buffer-processing/28445 "2018-07-07T08:14:31Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![arstudio](https://avatars.discourse-cdn.com/v4/letter/a/bc8723/32.png) [@arstudio](https://forum.juce.com/u/arstudio)\
**Post date:** [July 7, 2018, 8:14am UTC](https://forum.juce.com/t/audioapp-parallel-buffer-processing/28445/1 "2018-07-07T08:14:31Z")

</div>

Hi… thanks to @daniel I’ve understood my problem:  
I have an audio app in which I need to apply parallel effects on the main buffer channels 0 and 1.  
I’ ve rea about a technique like copying the buffer to another clean one, but it doesn’t work. What is the correct method to split the buffer into four different operations?  
(if I use the buffer in getNextAudioBlock once, it is occupied for the other effects)

---

<div class="post-metadata">

**Author:** ![danielrudrich](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/danielrudrich/32/3735_2.png) [@danielrudrich](https://forum.juce.com/u/danielrudrich)\
**Post date:** [July 7, 2018, 9:59am UTC](https://forum.juce.com/t/audioapp-parallel-buffer-processing/28445/2 "2018-07-07T09:59:26Z")

</div>

> [@arstudio](#):
>
> I’ ve rea about a technique like copying the buffer to another clean one, but it doesn’t work.

Why doesn’t it work? The general approach would be:

- declaring an `AudioBuffer<float> copyBuffer;` in the header
- in the prepare section, resize the buffer according to the number of copies and samplesPerBlock (e.g. `copyBuffer.setSize(4, numSamples);`
- use `copyBuffer.copyFrom(...)` methods in the processing

After processing each copy you would merge them to your main buffer.

---

<div class="post-metadata">

**Author:** ![arstudio](https://avatars.discourse-cdn.com/v4/letter/a/bc8723/32.png) [@arstudio](https://forum.juce.com/u/arstudio)\
**Post date:** [July 7, 2018, 10:27am UTC](https://forum.juce.com/t/audioapp-parallel-buffer-processing/28445/3 "2018-07-07T10:27:34Z")

</div>

It produces noise…  
How can I declare the size of thecopyBuffer in prepareToPlay (I don’t have access to buffer.getNumSamples())?  
Now I have declared and setted the copy buffers in header and prepareToPlay() and that in getNextAudioBlock():

```
const auto numSamples = bufferToFill.buffer->getNumSamples();

pitchABuf.copyFrom(0, 0, *bufferToFill.buffer, 0, 0, numSamples);

pitchABuf.copyFrom(1, 0, *bufferToFill.buffer, 1, 0, numSamples);

if (pitchOn)
{
    pitchA.process(pitchABuf);
    //pitchB.process(pitchBBuf);
}

bufferToFill.buffer->copyFrom(0, 0, pitchABuf, 0, 0, numSamples);
bufferToFill.buffer->copyFrom(1, 0, pitchABuf, 1, 0, numSamples);

```

It works but make terrible noise…

---

<div class="post-metadata">

**Author:** ![arstudio](https://avatars.discourse-cdn.com/v4/letter/a/bc8723/32.png) [@arstudio](https://forum.juce.com/u/arstudio)\
**Post date:** [July 7, 2018, 10:56am UTC](https://forum.juce.com/t/audioapp-parallel-buffer-processing/28445/4 "2018-07-07T10:56:05Z")

</div>

Ok, now both effects work with this code in getNextAudioBlock();

```
const auto numSamples = bufferToFill.buffer->getNumSamples();

if (pitchOn)
{
    effectsBuffer.copyFrom(0, 0, *bufferToFill.buffer, 0, 0, numSamples);
    effectsBuffer.copyFrom(1, 0, *bufferToFill.buffer, 1, 0, numSamples);

    effectsBuffer.copyFrom(2, 0, *bufferToFill.buffer, 0, 0, numSamples);
    effectsBuffer.copyFrom(3, 0, *bufferToFill.buffer, 1, 0, numSamples);

    pitchA.process(effectsBuffer);
    pitchB.process(effectsBuffer);

    bufferToFill.buffer->copyFrom(0, 0, effectsBuffer, 0, 0, numSamples);
    bufferToFill.buffer->copyFrom(1, 0, effectsBuffer, 1, 0, numSamples);
    
    bufferToFill.buffer->copyFrom(0, 0, effectsBuffer, 2, 0, numSamples);
    bufferToFill.buffer->copyFrom(1, 0, effectsBuffer, 3, 0, numSamples);
}

```

But it makes a lot of noise… I think the buffer size is wrong, isn’t it?

That code in prepareToPlay():

```
effectsBuffer.setSize(4, samplesPerBlockExpected);
effectsBuffer.clear();
```

---

<div class="post-metadata">

**Author:** ![arstudio](https://avatars.discourse-cdn.com/v4/letter/a/bc8723/32.png) [@arstudio](https://forum.juce.com/u/arstudio)\
**Post date:** [July 7, 2018, 12:40pm UTC](https://forum.juce.com/t/audioapp-parallel-buffer-processing/28445/5 "2018-07-07T12:40:18Z")

</div>

Ok I’ve arrived: no more noise, only the pitch effect:  
In getNextAudioBlock():

```
if (pitchOn)
{
    pitchABuffer.copyFrom(0, 0, *bufferToFill.buffer, 0, 0, numSamples);
    
    pitchA.process(pitchABuffer);

    bufferToFill.buffer->copyFrom(0, 0, pitchABuffer, 0, 0, numSamples);
    bufferToFill.buffer->copyFrom(1, 0, pitchABuffer, 0, 0, numSamples);

    pitchBBuffer.copyFrom(0, 0, *bufferToFill.buffer, 0, 0, numSamples);
    
    pitchB.process(pitchBBuffer);
    
    bufferToFill.buffer->copyFrom(0, 0, pitchBBuffer, 0, 0, numSamples);
    bufferToFill.buffer->copyFrom(1, 0, pitchBBuffer, 0, 0, numSamples);
}

```

in prepareToPlay():

```
pitchABuffer.setSize(1, samplesPerBlockExpected);
pitchBBuffer.setSize(1, samplesPerBlockExpected);

```

now… how can I pan each effect to left and right channels?

---

<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:** [July 7, 2018, 12:40pm UTC](https://forum.juce.com/t/audioapp-parallel-buffer-processing/28445/6 "2018-07-07T12:40:26Z")

</div>

Without knowing anything about your actual processing algorithm, there are two things that I think are not your intention. First:

```auto
    bufferToFill.buffer->copyFrom(0, 0, effectsBuffer, 0, 0, numSamples);
    bufferToFill.buffer->copyFrom(1, 0, effectsBuffer, 1, 0, numSamples);
    
    bufferToFill.buffer->copyFrom(0, 0, effectsBuffer, 2, 0, numSamples);
    bufferToFill.buffer->copyFrom(1, 0, effectsBuffer, 3, 0, numSamples);

```

So you have a four channel buffer (`effectsBuffer`) and copy the first two channels of it to the output buffer. This operation will replace all previous content in the buffer and replace it by the content of the selected `effectsBuffer` channels. After that, you do the same thing with channels three and four which means that you will again replace the whole buffer content. So you are overwriting the data you just copied two lines before. I don’t think you want that. Maybe you want to mix both signals? Then `addFrom` is the right call for the second pair of channels.

Second:  
As I said I know nothing about the internals of your algorithm. But

```auto
pitchA.process(effectsBuffer);
pitchB.process(effectsBuffer);

```

looks like you are processing the buffer sequentially rather then parallel? So you pass the data processed by pitchA over to pitchB to be processed again. So what happens inside those calls?

---

<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:** [July 7, 2018, 1:02pm UTC](https://forum.juce.com/t/audioapp-parallel-buffer-processing/28445/7 "2018-07-07T13:02:02Z")

</div>

I see I came a few seconds to late with my last post 😉

But the same applies to your last post. Currently you’ll only hear the result of pitchB, as you replace the content that came from pitchA with the copyFrom call. You definitively want to use `addFrom` in the last two lines to hear the blended results of both.

> [@arstudio](#):
>
> now… how can I pan each effect to left and right channels?

How do you expect your panning to work? Basically instead of simply copying data you could scale it by a different gain for the left and right channel. Both `copyFrom` and `addFrom` allow you to apply a gain too. So for an example to pan one unit more to the right, the gain for channel 1 has to be higher than the one for channel 0. For the actual gain values, there are various approaches out there, google is your friend here, I don’t think JUCE has something for panning, but I might be wrong.

---

<div class="post-metadata">

**Author:** ![arstudio](https://avatars.discourse-cdn.com/v4/letter/a/bc8723/32.png) [@arstudio](https://forum.juce.com/u/arstudio)\
**Post date:** [July 7, 2018, 1:58pm UTC](https://forum.juce.com/t/audioapp-parallel-buffer-processing/28445/8 "2018-07-07T13:58:59Z")

</div>

Great!! I feel a bit stupid… I was trying for hours with copyFrom without thinking that it overwrites all the buffer…now with addFrom it works great!  
For the panning I use the gain value of copyFrom and addFrom with a function that @daniel suggest me:

```
auto angleA = jmap(params[2] / 50.0, -1.0, 1.0, 0.0, float_Pi * 0.5);
    
    bufferToFill.buffer->copyFrom(0, 0, pitchABuffer.getReadPointer(0), numSamples, std::cos(angleA));
    bufferToFill.buffer->copyFrom(1, 0, pitchABuffer.getReadPointer(0), numSamples, std::sin(angleA));

```

where params[2] is a value from a slider that has a value between -50.0 and 50.0

Thanks Guys!! Now I can return to my app and I know how to do all!

---

<div class="post-metadata">

**Author:** ![arstudio](https://avatars.discourse-cdn.com/v4/letter/a/bc8723/32.png) [@arstudio](https://forum.juce.com/u/arstudio)\
**Post date:** [July 23, 2018, 5:36pm UTC](https://forum.juce.com/t/audioapp-parallel-buffer-processing/28445/9 "2018-07-23T17:36:21Z")

</div>

Excuse me, but I’m using the same technique in an AudioPlugin instead of an AudioApp, and I get noise and then stop sounding (with a delay from stk CCRMA toolkit)
