Notify method when accessing two buffers

Matthias's icon

Hi folks,

I'm writing an external which requires access to two buffers simultaneously. I see that a "notify" method is required for buffer handling. I can see this logic perfectly when handling a single buffer in an external.

Message "notify" calls the notify function --> function return "buffer_ref_notify" with the first argument being the buffer reference in the object structure.

This works like a charm when accessing a single buffer, but how do you handle it when there are two buffers?

I'd be glad for some help.

Best,
Matthias

Matthias's icon

OK, I did some wild guessing and suddenly came up with this. Here's my notify method:

t_max_err myobject_bnotify(t_myobject *x, t_symbol *s, t_symbol *msg, void *sender, void *data) {
    t_symbol *buffer_name = (t_symbol *)object_method((t_object *)sender, gensym("getname"));
    if (buffer_name == x->buffera_name) {
        return buffer_ref_notify(x->buffera, s, msg, sender, data);
    }
    else if (buffer_name == x->bufferb_name) {
        return buffer_ref_notify(x->bufferb, s, msg, sender, data);
    }
}

I now get the xcode warning message that "control may reach end of non-void function". Is there a more elegant or standard way of handling notify messages for more than one buffer?

Best,
Matthias

Matthias's icon

I did some more refinements to solve the issue I had in my previous post. I just create temporary buffer reference pointer and assign it to the calling buffer:

t_max_err myobject_notify(t_myobject *x, t_symbol *s, t_symbol *msg, void *sender, void *data) {
    t_symbol *buffer_name = (t_symbol *)object_method((t_object *)sender, gensym("getname"));
    t_buffer_ref *calling_buffer;
    if (buffer_name == x->buffera_name) {
        calling_buffer = x->buffera;
    }
    if (buffer_name == x->bufferb_name) {
        calling_buffer = x->bufferb;
    }
    return buffer_ref_notify(calling_buffer, s, msg, sender, data);
}

This works like a charm. But here's an other question. Is it possible that the calling buffer could be any other than the ones referenced by my object? This would mean that my temporary buffer ref pointer remains uninitialized when it is used in the return function, which would inevitably lead to a crash. Any leads on this?

Matthias's icon

Anyone any thoughts on this?

Richard Mitic's icon

Late to the game, but I just ran into the same problem myself.

Check out the possible values for

t_max_err

. Choose one that best suits your application and return it if the name of the buffer isn't recognised. I chose

 MAX_ERR_GENERIC

.

e_max_errorcodes {
MAX_ERR_NONE,
MAX_ERR_GENERIC,
MAX_ERR_INVALID_PTR,
MAX_ERR_DUPLICATE,
MAX_ERR_OUT_OF_MEM
}

Matthias's icon

Thanks for pointing this out. It's so simple when you think of it..