# Can't copy buffer in processBlock

**URL:** <https://forum.juce.com/t/cant-copy-buffer-in-processblock/13777>\
**Category:** General JUCE discussion\
**Created:** [October 28, 2014, 10:27am UTC](https://forum.juce.com/t/cant-copy-buffer-in-processblock/13777 "2014-10-28T10:27:48Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![dinaiz](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/dinaiz/32/182_2.png) [@dinaiz](https://forum.juce.com/u/dinaiz)\
**Post date:** [October 28, 2014, 10:27am UTC](https://forum.juce.com/t/cant-copy-buffer-in-processblock/13777/1 "2014-10-28T10:27:48Z")

</div>

Hi all,&nbsp;

&nbsp;

This simple code in processBlock produces silence :

> &nbsp; &nbsp; AudioSampleBuffer in(buffer);  
> &nbsp;&nbsp; &nbsp;in.copyFrom(0,0,buffer,0,0,buffer.getNumSamples());  
> &nbsp;&nbsp; &nbsp;in.copyFrom(1,0,buffer,1,0,buffer.getNumSamples());  
> &nbsp;&nbsp; &nbsp;buffer.clear();  
> &nbsp; &nbsp;&nbsp;buffer.copyFrom(0,0,in,0,0,in.getNumSamples());  
> &nbsp;&nbsp;&nbsp; buffer.copyFrom(1,0,in,1,0,in.getNumSamples());  
> &nbsp;

If I remove&nbsp;buffer.clear();, the output is as expected. Any clues ?

---

<div class="post-metadata">

**Author:** ![siedschl](https://avatars.discourse-cdn.com/v4/letter/s/4bbf92/32.png) [@siedschl](https://forum.juce.com/u/siedschl)\
**Post date:** [October 28, 2014, 11:02am UTC](https://forum.juce.com/t/cant-copy-buffer-in-processblock/13777/2 "2014-10-28T11:02:00Z")

</div>

From the documentation on the AudioSampleBuffer copy constructor:

_Copies another buffer._

_This buffer will make its own copy of the other's data, unless the buffer was created using an external data buffer, in which case boths buffers will just point to the same shared block of data._

It is indeed a bit unfortunate (I ran into the same issue once) that when you don't know how the buffer to be copied from was created, you cannot know what the constructor does (own copy of data or refer to the same data). To be safe, always create the new buffer blank, with the same number of channels and samples as the original, and then copy the data.

---

<div class="post-metadata">

**Author:** ![dinaiz](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/dinaiz/32/182_2.png) [@dinaiz](https://forum.juce.com/u/dinaiz)\
**Post date:** [October 28, 2014, 11:07am UTC](https://forum.juce.com/t/cant-copy-buffer-in-processblock/13777/3 "2014-10-28T11:07:14Z")

</div>

Thanks for your prompt reply

I thought of that but normally when I modify in, buffer should be modified because they're shared ? And anyway the 2 first&nbsp;copyFrom should handle that.

But anyway, I'm gonna try that ! Thanks

---

<div class="post-metadata">

**Author:** ![dinaiz](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/dinaiz/32/182_2.png) [@dinaiz](https://forum.juce.com/u/dinaiz)\
**Post date:** [October 28, 2014, 11:10am UTC](https://forum.juce.com/t/cant-copy-buffer-in-processblock/13777/4 "2014-10-28T11:10:00Z")

</div>

...and it works.&nbsp;
