Max 5 API Reference


Your object can access shared data stored in an MSP buffer~ object. More...

Collaboration diagram for Buffers:

Data Structures

struct  t_buffer
 Data structure for the buffer~ object. More...

Detailed Description

Your object can access shared data stored in an MSP buffer~ object.

Similar to table and coll objects, buffer~ objects are bound to a t_symbol from which you can gain access to the t_buffer struct. Consider the following example.

    t_symbol *s;
    t_object *o;
    s = gensym("foo");
    o = s->s_thing;
    // if an object is bound to the symbol "foo", then o is that object.
    if (ob_sym(o) == gensym("buffer~")) {
        // that object is a buffer~, so we can use it
        x->x_buffer = (t_buffer*)o;

Having stored a pointer to the buffer~ is the first step toward working with its data. However, you must not go accessing the data directly without taking some precautions regarding thread-safety.

To access the data in a buffer you first increment the b_inuse member of the t_buffer's struct. Then you perform the requisite operations on the data, which is stored in the b_samples member. When you are done you decrement the b_inuse member to return it to the state in which you found it.

In the past you may have set the buffer's b_inuse flag directly and cleared it when you were done. This is no longer good enough, and you must instead use the threadsafe macros ATOMIC_INCREMENT and ATOMIC_DECREMENT for modifying the b_inuse flag. The example below demonstrates what this might look like in an MSP object's perform routine. Notice that extra care has been taken to ensure that the ATOMIC_INCREMENT is always balanced with an ATOMIC_DECREMENT call.

    if (!x->w_buf->b_valid) {
        goto byebye;
    // do something with the buffer
    return (w + 7);

A class that accesses buffer~ objects is the simpwave~ object that is included with Max 5 SDK example projects.

Copyright © 2008, Cycling '74