MAX/MSP SDK学习07:list传递

2023-12-13 07:51:56

实现自定义Obejct,要求将传入的一组数据+100后传出。

#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.

文章来源:https://blog.csdn.net/qq_38967414/article/details/134557655
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。