Delay buffer pointer problems


#1

hi,

I’m trying to implement a delay buffer in the juce audio plugin framework.

I have defined two buffers

float* m_bufferL;
float* m_bufferR;

and newed them

m_bufferL = new float[BUFFER_SIZE];
m_bufferR = new float[BUFFER_SIZE];

then in the processBlock method I’m trying to loopthrough the audiosamplebuffer and copy each sample into my buffer. what I have tried is

int bSize=buffer.getNumSamples();

for (int i=0;bSize-1;++i)
	{
		m_bufferL[m_cursor]=buffer.getSampleData(i);
		m_bufferR[m_cursor]=buffer.getSampleData(i);
		if(m_cursor>=bSize) m_cursor=0;
	}

which gives me the following compilation error
error C2440: ‘=’ : cannot convert from ‘float *’ to ‘float’

so its something to do with getSampleData returning a pointer to a sample and then I want to copy the the value of what the pointer points to into my array.

I know this is easy but I’m a java guy and I have trouble getting pointer stuff that’s obvious to the pros…

edit:- some spelling


#2

All sorts of problems here…

… e.g., once you got it compile you’d have an infinite loop :

should be something like:

AudioSampleBuffer::getSampleData() with a single int arg gets a pointer to the first sample in the channel provided by the arg, so here you’re iterating through channels getting a pointer to the first sample in each. You then need to dereference the pointer. Also you’re not incrementing the m_cursor. Let’s say you have valid channels 0 & 1, you could do:

for (int i=0;i < bSize;++i) { m_bufferL[m_cursor]=*buffer.getSampleData(0, i); m_bufferR[m_cursor]=*buffer.getSampleData(1, i); m_cursor++; if(m_cursor>=BUFFER_SIZE) m_cursor=0; }

… but there are more efficient algorithms.


#3

ok, yeah, thanks for the tips those were pretty obvious bugs in there :smiley:

from what I can understand the de-referencing of the pointer is copying the value of the pointer (a sample from the audiosamplebuffer in this case) to my new pointer (an element in my array buffer). this is pretty much what I was aiming for

what would be an example of a more efficient way to do this?

I was thinking that it would be good to copy out the whole audiosamplebuffer but I didn’t see a method for doing this. also I might possibly run into problems when my bufferL was not an integer number of audiosamplebuffers (becuase of the cirular buffer implementation).


#4

[quote=“ninjasmith”]
what would be an example of a more efficient way to do this?[/quote]

Well one thing is calling getSampleData() twice for every sample frame, you could call these once (for each channel) before the loop and increment the pointers on each loop iteration, e.g.

float *samples0 = buffer.getSampleData(0, i); float *samples1 = buffer.getSampleData(1, i); for (int i=0;i < bSize;++i) { m_bufferL[m_cursor]=*samples0; m_bufferR[m_cursor]=*samples1; samples0++; samples1++; m_cursor++; if(m_cursor>=BUFFER_SIZE) m_cursor=0; }


#5

[quote=“martinrobinson”][quote=“ninjasmith”]
what would be an example of a more efficient way to do this?[/quote]

Well one thing is calling getSampleData() twice for every sample frame, you could call these once (for each channel) before the loop and increment the pointers on each loop iteration, e.g.

float *samples0 = buffer.getSampleData(0, i); float *samples1 = buffer.getSampleData(1, i); for (int i=0;i < bSize;++i) { m_bufferL[m_cursor]=*samples0; m_bufferR[m_cursor]=*samples1; samples0++; samples1++; m_cursor++; if(m_cursor>=BUFFER_SIZE) m_cursor=0; }[/quote]

nice, I like it - much appreciated. its a whole different way of thinking about coding that I need to get my head round :?