buffer~ in mxj~

Niklas Saers's icon

Hi guys,
I've been going through the documentation and I've been going through
the JAR but couldn't figure this out: how do I access a named buffer~
from within mxj~? From the discussion on the list I'm not even sure
it's possible right now?? My purpose is to load up a couple of files
in buffers, have them quickly resized in mxj~ where I do onset-
detection, normalizing and making sure they start at a zero crossing,
and after this initialization I play with them in Max/MSP.

Cheers

    Nik

topher lafata's icon

did you look at the MSPBuffer java doc?

Class MSPBuffer

java.lang.Object

com.cycling74.msp.MSPBuffer

public class MSPBuffer
extends java.lang.Object
Provides static methods that get and set buffer data. created on 9-
April-2004

Niklas Saers's icon

Hi Topher,
I looked at it, but I must have been very tired. ;-) I guess I
expected to find something like a constructor MSPBuffer(String
bufferName) and then access it that way and then misunderstood how it
worked. Thanks for pointing me back to it. :-)

Cheers

    Nik

On Sep 1, 2007, at 1:51 AM, topher lafata wrote:

> did you look at the MSPBuffer java doc?
>
> Class MSPBuffer
>
> java.lang.Object
>
>
> com.cycling74.msp.MSPBuffer
>
>
> public class MSPBuffer
> extends java.lang.Object
> Provides static methods that get and set buffer data. created on 9-
> April-2004
>
>

tommymetz's icon

I know this is really old but can somebody show me the basic code to peek a Buffer object? I'm just confused how to access it without constructing an MSPBuffer object like Niklas' last comment. I think I must be missing something. Is it something like the following because it's not working:

float mypeek = MSPBuffer.peek("Buckets");

Do I need to create a new class that extends MSPBuffer? Just really confused about this.

Mike S's icon

Hi Tommy, can't give a definite answer on this - however I would recommend viewing the source of [mxj buf.Op] I managed to hack something together from that.

tommymetz's icon

Thanks Mike, I will dig into that!

StuartM's icon

Hey Tommy,
I too had a hair pulling experience with accessing MSPBuffers from mxj! Took me over a month to filter through hundreds of tutorials and example patches. I can help you out... a lot.

Firstly:

The class should extend MaxObject as usual for max object creation from mxj.
like this "public class genInverseFilter extends MaxObject {"

If you want to access a buffer from your patch window in max and you want to supply the name of that buffer
as an argument then you need to create a global variable -
"String bufname = null;"

Then your usual declare inlets and outlets inside your constructor - titled the same as your classname with:
"(Atom[] args)"

Still in the constructor...
"if(args.length>0)
            set(args[0].toString()); "

This code will check your arguments to see if they are present.

Your first method will be:
"public void set(String s) {
        bufname = s;
    }
"

This will set the string variable "bufname" to the argument you supplied when you created the object inside max msp. Sorry this is a long winded explanation..

Now onto the important stuff:
To access the buffer you simply use a single line of code:

"float[] s = MSPBuffer.peek(bufname);"
This will "peek" inot the buffer with the name you have supplied as an argument and that has been set as a global variable. The data will then be stored in the "float[] s" array or whatever you decide to call it. One thing to note - MSPBuffer peek returns float values and not double. This means if you are doing math using java.math you will have to do some annoying casting for loops "(double) ....".

Once you have performed all of the functions neccessary to fill a new buffer or replace the data in the original buffer you have to:

Designate a new buffername or supply the original in the code somehow - this can be as an argument to your method which accesses the buffer or has to be done in the same way as I showed you before.

    "int channels = MSPBuffer.getChannels(bufname); // this calculates how many channels are on the original channel.
                MSPBuffer.setSize(destBuf, channels, size); // size should be a long type variable and is total number of samples.
                MSPBuffer.poke(destBuf, dataArray); "
This is fairly self explanatory. Array has to, as mentioned earlier be a float array.

Hope that helps!

tommymetz's icon

Thanks Smellor, good info here! And Mike S's suggestion of "viewing the source of [mxj buf.Op]" really helped me a lot. Very cool stuff!

kylinden's icon

Also note that the index of the first channel is 1. To get a good example have look at the source code of buf.Op.