# Understanding AudioSampleBuffer - copying

**URL:** <https://forum.juce.com/t/understanding-audiosamplebuffer-copying/36761>\
**Category:** General JUCE discussion\
**Created:** [December 20, 2019, 7:37pm UTC](https://forum.juce.com/t/understanding-audiosamplebuffer-copying/36761 "2019-12-20T19:37:45Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![cackland](https://avatars.discourse-cdn.com/v4/letter/c/9de0a6/32.png) [@cackland](https://forum.juce.com/u/cackland)\
**Post date:** [December 20, 2019, 7:37pm UTC](https://forum.juce.com/t/understanding-audiosamplebuffer-copying/36761/1 "2019-12-20T19:37:46Z")

</div>

So I’m trying to understand how to copy one AudioSampleBuffer to another. Struggling a little.

I understand JUCE has some memcpy function, COPYFROM and another ADDFROM.

What I’m trying to do is when a file is loaded, loaded the samples from that file into a master AudioSampleBuffer, say called BufferStream. And continually fill up BufferStream for every file that is loaded.

Assuming I have to create a multi-dimensional for loop (auto channel, auto sample) and go through the .copyFrom function? Using ‘sample’ as the iterator?

```
    for (auto channel = 0; channel < fileBuffer.getNumChannels(); channel++)
    {
        for (auto sample = 0; sample < fileBuffer.getNumSamples(); sample++)
        {
            bufferStream.copyFrom(<#int destChannel#>, <#int destStartSample#>, <#const AudioBuffer<float> &source#>, <#int sourceChannel#>, <#int sourceStartSample#>, <#int numSamples#>)
        }
    }

```

Do I need to set the size of the ‘master’ buffer, BufferStream first?

Any help would be great 🙂

---

<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:** [December 20, 2019, 7:51pm UTC](https://forum.juce.com/t/understanding-audiosamplebuffer-copying/36761/2 "2019-12-20T19:51:18Z")

</div>

You must not iterate over each sample when using copyFrom, that already does that iteration internally. You do, however, need to iterate over the channels. (For some reason, there isn’t a copyFrom overload that handles multiple channels directly.)

---

<div class="post-metadata">

**Author:** ![cackland](https://avatars.discourse-cdn.com/v4/letter/c/9de0a6/32.png) [@cackland](https://forum.juce.com/u/cackland)\
**Post date:** [December 20, 2019, 7:56pm UTC](https://forum.juce.com/t/understanding-audiosamplebuffer-copying/36761/3 "2019-12-20T19:56:33Z")

</div>

Ok thanks.

So this is the updated version to add any new buffer to an existing one… I have to set the size of the master buffer each time before I copy samples. Does this look correct?

```
    int newBufferSize = bufferStream.getNumSamples() + fileBuffer.getNumSamples();

    bufferStream.setSize(fileBuffer.getNumChannels(), newBufferSize);
    
    for (auto channel = 0; channel < fileBuffer.getNumChannels(); channel++)
    {
        bufferStream.addFrom(channel, 0, fileBuffer, channel, 0, fileBuffer.getNumSamples());
    }
```

---

<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:** [December 20, 2019, 8:10pm UTC](https://forum.juce.com/t/understanding-audiosamplebuffer-copying/36761/4 "2019-12-20T20:10:49Z")

</div>

addFrom _sums_ the audio to the existing audio, it doesn’t mean the data is appended to the buffer. If you need to append, you need keep track of the destination start index yourself. (In addition to resizing the destination buffer.)

---

<div class="post-metadata">

**Author:** ![cackland](https://avatars.discourse-cdn.com/v4/letter/c/9de0a6/32.png) [@cackland](https://forum.juce.com/u/cackland)\
**Post date:** [December 20, 2019, 8:36pm UTC](https://forum.juce.com/t/understanding-audiosamplebuffer-copying/36761/5 "2019-12-20T20:36:21Z")

</div>

Oops… my bad. Forgot to change the addFrom to copyFrom.

---

<div class="post-metadata">

**Author:** ![cackland](https://avatars.discourse-cdn.com/v4/letter/c/9de0a6/32.png) [@cackland](https://forum.juce.com/u/cackland)\
**Post date:** [December 20, 2019, 8:53pm UTC](https://forum.juce.com/t/understanding-audiosamplebuffer-copying/36761/6 "2019-12-20T20:53:18Z")

</div>

Hmmm this isn’t working.

```
    int newBufferSize = bufferStream.getNumSamples() + fileBuffer.getNumSamples();

    bufferStream.setSize(fileBuffer.getNumChannels(), newBufferSize);

    for (auto channel = 0; channel < fileBuffer.getNumChannels(); channel++)
    {
        bufferStream.copyFrom(channel, 0, fileBuffer, channel, 0, fileBuffer.getNumSamples());
    }

```

Thinking I would have to iterate through the fileBuffer using a internal for loop, but as you said not to… Otherwise each time a new file is added, the bufferStream just gets replaced, and not appended.

---

<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:** [December 20, 2019, 9:06pm UTC](https://forum.juce.com/t/understanding-audiosamplebuffer-copying/36761/7 "2019-12-20T21:06:13Z")

</div>

Like I mentioned, you have to keep track of the destination buffer index. Your code looks like it’s always copying into the destination starting from index 0. (destStartSample in the documentation.)

---

<div class="post-metadata">

**Author:** ![cackland](https://avatars.discourse-cdn.com/v4/letter/c/9de0a6/32.png) [@cackland](https://forum.juce.com/u/cackland)\
**Post date:** [December 20, 2019, 9:09pm UTC](https://forum.juce.com/t/understanding-audiosamplebuffer-copying/36761/8 "2019-12-20T21:09:54Z")

</div>

Right… so I though using an internal for loop… But you said not to.

something like `bufferStream.getNumSamples()` (which would be 0 initially) plus an iterator.

```
for (auto sample = 0; sample < fileBuffer.getNumSamples(); sample++)
{
int iterator = bufferStream.getNumSamples() + sample;
}
```

---

<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:** [December 20, 2019, 9:11pm UTC](https://forum.juce.com/t/understanding-audiosamplebuffer-copying/36761/9 "2019-12-20T21:11:13Z")

</div>

You don’t need to loop over the samples yourself, the copyFrom method already does it for you. You _can_ of course implement the sample iteration and copying completely yourself, if you wish to do so. (But then you wouldn’t be using the copyFrom method at all.)

---

<div class="post-metadata">

**Author:** ![cackland](https://avatars.discourse-cdn.com/v4/letter/c/9de0a6/32.png) [@cackland](https://forum.juce.com/u/cackland)\
**Post date:** [December 20, 2019, 9:20pm UTC](https://forum.juce.com/t/understanding-audiosamplebuffer-copying/36761/10 "2019-12-20T21:20:25Z")

</div>

Yes ok, but how would I iterator through the bufferStream position if I don’t have an iterator.

`bufferStream.copyFrom(channel, **0** , fileBuffer, channel, 0, fileBuffer.getNumSamples());`

Sorry, I’m struggling on this one.

---

<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:** [December 20, 2019, 9:26pm UTC](https://forum.juce.com/t/understanding-audiosamplebuffer-copying/36761/11 "2019-12-20T21:26:32Z")

</div>

Something like this :

```auto
int newDestPosition = bufferStream.getNumSamples();
    int newBufferSize = bufferStream.getNumSamples() + fileBuffer.getNumSamples();

    bufferStream.setSize(fileBuffer.getNumChannels(), newBufferSize);

    for (auto channel = 0; channel < fileBuffer.getNumChannels(); channel++)
    {
        bufferStream.copyFrom(channel, newDestPosition, fileBuffer, channel, 0, fileBuffer.getNumSamples());
    }
```

That code isn’t tested, but that would be the general idea.

Why are you trying to join multiple AudioBuffers into one, to begin with? Wouldn’t it be simpler to just have an array/vector for the buffers?
