实现自定义Obejct,要求将传入的一组数据+100后传出。
cpp
#include "ext.h"
#include "ext_obex.h"
typedef struct _listTrans {
t_object ob;
void* outLet;
t_atom* fList; // 经常发送较大的list,则建议使用t_atom数组保存
long listNum;
} t_listTrans;
void* listTrans_new(t_symbol* s, long argc, t_atom* argv);
void listTrans_free(t_listTrans* x);
void listTrans_assist(t_listTrans* x, void* b, long m, long a, char* s);
void listTrans_recv_list(t_listTrans* x, t_symbol* s, long argc, t_atom* argv);
void listTrans_snd_list(t_listTrans* x);
void* listTrans_class;
void ext_main(void* r) {
t_class* c;
c = class_new("listTrans", (method)listTrans_new, (method)listTrans_free, (long)sizeof(t_listTrans),
0L /* leave NULL!! */, A_GIMME, 0);
class_addmethod(c, (method)listTrans_assist, "assist", A_CANT, 0);
class_addmethod(c, (method)listTrans_recv_list, "list", A_GIMME, 0);
class_addmethod(c, (method)listTrans_snd_list, "bang", 0);
class_register(CLASS_BOX, c); /* CLASS_NOBOX */
listTrans_class = c;
post("I am the listTrans object");
}
void listTrans_assist(t_listTrans* x, void* b, long m, long a, char* s) {
if (m == ASSIST_INLET) { // inlet
sprintf(s, "I am inlet %ld", a);
} else { // outlet
sprintf(s, "I am outlet %ld", a);
}
}
void listTrans_free(t_listTrans* x) {
sysmem_freeptr(x->fList);
}
void* listTrans_new(t_symbol* s, long argc, t_atom* argv) {
t_listTrans* x = NULL;
x = (t_listTrans*)object_alloc(listTrans_class);
//x->outLet = outlet_new(x, NULL);
x->outLet = listout(x);
x->listNum = 0;
return (x);
}
void listTrans_recv_list(t_listTrans* x, t_symbol* s, long argc, t_atom* argv) {
x->fList = (t_atom*)sysmem_newptr(sizeof(t_atom) * argc);
x->listNum = argc;
//outlet_list(x->outLet, gensym("list"), argc, argv);
for (int i = 0; i < argc; i++) { // 经常发送较大的list,则建议使用t_atom数组
atom_setfloat(&(x->fList[i]), atom_getfloat(&argv[i]) + 100);
}
}
void listTrans_snd_list(t_listTrans* x) {
//for (int i = 0; i < x->listNum; i++) {
// outlet_float(x->outLet, atom_getfloat(&(x->fList[i])) + 100);
//}
outlet_list(x->outLet, gensym("list"), x->listNum, x->fList);
//outlet_anything(x->outLet, gensym("list"), x->listNum, x->fList);
}
运行结果:
补充:
除了使用outlet_anything 外,还可使用outlet_list输出list.