Any thoughts on this?

FRid's icon

Hello folks,

Just saw this:

This has got me thinking though. What is a buffer actually? I assume it's an array, not resizable after construction therefore fixed. What if it's constructed as an arraylist, wouldn't that make it dynamic? I think i'll look into this, would make for a nice holiday-project. Any pro's and con's? Please do share your thoughts/experience.

FRid

The mxj in pseudo-code

- catch the incoming samples (44100 a/sec, this is gonna eat ram though)
- and after stopping the recording from outside create a buffer inside and transfer samples to that
- Voila?

gwsounddsg's icon

If I'm not mistaken, buffer~ loads audio data into RAM preping it for playback.

Roth's icon

What is a buffer actually? I assume it's an array, not resizable after construction therefore fixed

Yeah, buffers are C structures that contain some information about the buffer (size, channels, etc) and a pointer to some memory (basically an array) that has been allocated for storing the audio samples.

There are a few possible issues with your ArrayList idea. Before getting into that, I want to explain how the dynamic sizing of ArrayList works. Internally, ArrayList has an array that it uses to store your objects. Every time you add something to your ArrayList a size check is performed and if the internal array is too small a new internal array is created with twice the size of the old array and then the data is copied from the old array to the new array.

The first problem with your ArrayList idea is that ArrayList is for storing objects not primitive types, so while you could make ArrayList you can't make ArrayList. The first issue with this is if you are storing every sample indivually you not only have the normal ArrayList overhead of the method call and the range check but you will also have the boxing overhead of converting float to Float (not that I'm sure exactly what this overhead would be). Also, because you would be storing Float not float in your ArrayList you can't use something fast and convenient like System.arraycopy(...) but will need to loop through all the data in your ArrayList assigning the values to the buffer's array.

If I were you I would manage things yourself with some behavior like ArrayList. Make a float[] with a good starting size. Every time you are going to add something to the array, check the size first and if you are out of room make a new bigger array and use System.arraycopy(...) to copy the values from the old array.

Another option is to creat a ArrayList. Every signal vector, copy the incoming samples to a new float[] with the size of your signal vector size and and this new float[] to your ArrayList. This will most likely perform worse than my first suggestion as you will be allocating more arrays and you will need to iterate through your ArrayList copying small float[] one at a time vs just using one call to System.arraycopy(...) in my first suggestion (just wanted to share another way of thinking about the problem).

44100 a/sec, this is gonna eat ram though

Eh, I wouldn't worry about it. buffer~ is also storing data in RAM. Plus, one second of 44.1KHz/32Bit audio uses 172.3KB of memory.

FRid's icon

Thanks for the insight! A very clear description. Damn it, i knew it wasn't gonna be easy but i thought maybe a healthy mix of naivete and enthusiasm would do :) It seemed such a nice idea. I'll look into this nonetheless, will be good for learning because i never used msp in mxj before.

FRid

And right now i can't even get the samples to show or store in the arraylist :(

This is the method in short. No samples will show, not even in the [test]-arraylist. I don't understand what i'm doing wrong here.

//MSPPerform
float[] in = ins[0].vec;

for(i = 0; i < in.length;i++) {
test.add(in[i]);
}
//Another method to copy from arraylist to buffer
for (int i=0;i
MSPBuffer.poke(bufname, 1, i, test.get(i));
}