MAX/MSP SDK学习09:重要示例1

2023-12-13 17:36
文章标签 sdk 学习 示例 重要 09 max msp

本文主要是介绍MAX/MSP SDK学习09:重要示例1,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

本示例涉及到单个MSP对象同时使用Signal类型、Message类型的入口;代理入口的使用。

注意:MSP对象的入口默认为代理入口,因此Signal类型、Message类型的数据都可接收;

#include "ext.h"			
#include "ext_obex.h"		
#include "z_dsp.h"			typedef struct _butterFilterTest {t_pxobject		ob;			// the object itself (t_pxobject in MSP instead of t_object)long iirOrder;			// 滤波器阶数   nt_atom* iirState;		// 存储滤波器状态   nint fltStateParaNum;	// 滤波器状态参数个数   nt_atom* a;				// 滤波器参数a   (n+1)t_atom* b;				// 滤波器参数bint fltParaNum;			// 滤波器参数个数    (n+1)int sampleframes;    // 帧大小long lastVectorSize; // 用以判定signal vector size是否有变//void* outLet;/*** 用以测试 ***/t_atom* sigList;     // 存储滤波后的单帧信号t_atom* tmpList;     // 暂存到来的单帧信号/***************/
} t_butterFilterTest;void* butterFilterTest_new(t_symbol* s, long argc, t_atom* argv);
void butterFilterTest_free(t_butterFilterTest* x);
void butterFilterTest_assist(t_butterFilterTest* x, void* b, long m, long a, char* s);
void butterFilterTest_dsp64(t_butterFilterTest* x, t_object* dsp64, short* count, double samplerate, long maxvectorsize, long flags);
void butterFilterTest_perform64(t_butterFilterTest* x, t_object* dsp64, double** ins, long numins, double** outs, long numouts, long sampleframes, long flags, void* userparam);
void butterFilterTest_perform64_list(t_butterFilterTest* x, t_object* dsp64, double** ins, long numins, double** outs, long numouts, long sampleframes, long flags, void* userparam);
void butterFilterTest_feed(t_butterFilterTest* x, t_double* inSig, int sampleframes);void butterFilterTest_recvList(t_butterFilterTest* x, t_symbol* s, long argc, t_atom* argv);
void butterFilterTest_feed_test(t_butterFilterTest* x, long sampleframes);static t_class* butterFilterTest_class = NULL;void ext_main(void* r) {t_class* c = class_new("butterFilterTest~", (method)butterFilterTest_new, (method)butterFilterTest_free, (long)sizeof(t_butterFilterTest), 0L, A_GIMME, 0);class_addmethod(c, (method)butterFilterTest_dsp64, "dsp64", A_CANT, 0);class_addmethod(c, (method)butterFilterTest_assist, "assist", A_CANT, 0);class_addmethod(c, (method)butterFilterTest_recvList, "list", A_GIMME, 0);class_dspinit(c); // 初始化 & 加入通用处理方法class_register(CLASS_BOX, c);butterFilterTest_class = c;
}void* butterFilterTest_new(t_symbol* s, long argc, t_atom* argv) {t_butterFilterTest* x = (t_butterFilterTest*)object_alloc(butterFilterTest_class);dsp_setup((t_pxobject*)x, 3);	// MSP inlets: arg is # of inlets and is REQUIRED!  信号入口数为1,3则信号入口数为3// use 0 if you don't need inletsoutlet_new(x, "signal"); 		// signal outlet (note "signal" rather than NULL)   信号出口//x->outLet = listout(x);x->a = NULL;x->b = NULL;x->fltParaNum = 0;x->iirState = NULL;x->fltStateParaNum = 0;x->sigList = NULL;x->tmpList = NULL;x->lastVectorSize = 0;return (x);
}void butterFilterTest_free(t_butterFilterTest* x) {dsp_free((t_pxobject*)x);sysmem_freeptr(x->a);sysmem_freeptr(x->b);sysmem_freeptr(x->iirState);sysmem_freeptr(x->sigList);sysmem_freeptr(x->tmpList);
}void butterFilterTest_assist(t_butterFilterTest* x, void* b, long m, long a, char* s) {if (m == ASSIST_INLET) { //inletsprintf(s, "I am inlet %ld", a);} else {	// outletsprintf(s, "I am outlet %ld", a);}
}void butterFilterTest_recvList(t_butterFilterTest* x, t_symbol* s, long argc, t_atom* argv) {long inNum = proxy_getinlet((t_object*)x);post("inNum: %d. list recv.", inNum);x->iirOrder = argc - 1;				// 滤波器阶数x->fltStateParaNum = argc - 1;		// 滤波器状态参数个数if (x->a == NULL || x->fltParaNum != argc) {if (x->a != NULL) {sysmem_freeptr(x->a);sysmem_freeptr(x->iirState);}x->a = (t_atom*)sysmem_newptr(sizeof(t_atom) * argc);x->iirState = (t_atom*)sysmem_newptr(sizeof(t_atom) * x->fltStateParaNum);if (x->a == NULL || x->iirState == NULL) {return;}}if (x->b == NULL || x->fltParaNum != argc) {if (x->b != NULL) {sysmem_freeptr(x->b);}x->b = (t_atom*)sysmem_newptr(sizeof(t_atom) * argc);if (x->b == NULL) {return;}}x->fltParaNum = argc;				// 更新滤波器参数个数switch (inNum) {case 0:post("List size: %d", argc);for (int i = 0; i < argc; i++) {atom_setfloat(&(x->tmpList[i]), atom_getfloat(&(argv[i])));}break;case 1:post("List size: %d", argc);for (int i = 0; i < argc; i++) {atom_setfloat(&(x->a[i]), atom_getfloat(&(argv[i])));}post("AAAA");for (int i = 0; i < argc; i++) {post("%lf", atom_getfloat(&(x->a[i])));}break;case 2:post("List size: %d", argc);for (int i = 0; i < argc; i++) {atom_setfloat(&(x->b[i]), atom_getfloat(&(argv[i])));}post("AAAA");for (int i = 0; i < argc; i++) {post("%lf", atom_getfloat(&(x->a[i])));}post("BBB");for (int i = 0; i < argc; i++) {post("%lf", atom_getfloat(&(x->b[i])));}post("recv iirState:");for (int i = 0; i < x->fltStateParaNum; i++) {post("%lf", atom_getfloat(&(x->iirState[i])));}break;}
}// registers a function for the signal chain in Max
void butterFilterTest_dsp64(t_butterFilterTest* x, t_object* dsp64, short* count, double samplerate, long maxvectorsize, long flags) {post("my sample rate is: %f,vector size is: %ld", samplerate, maxvectorsize);   // 和Audio status中设置相同x->sampleframes = maxvectorsize;/* 滤波器状态初始化*/for (int i = 0; i < x->fltStateParaNum; i++) {atom_setfloat(&(x->iirState[i]), 0);}post("dsp64 iirstate:");for (int i = 0; i < x->fltStateParaNum; i++) {post("%lf", atom_getfloat(&(x->iirState[i])));}if (x->sigList == NULL || x->lastVectorSize != maxvectorsize) {if (x->sigList != NULL) {sysmem_freeptr(x->sigList);}x->sigList = (t_atom*)sysmem_newptr(sizeof(t_atom) * maxvectorsize);if (x->sigList == NULL) {return;}}if (x->tmpList == NULL || x->lastVectorSize != maxvectorsize) {if (x->tmpList != NULL) {sysmem_freeptr(x->tmpList);}x->tmpList = (t_atom*)sysmem_newptr(sizeof(t_atom) * maxvectorsize);if (x->tmpList == NULL) {return;}}x->lastVectorSize = maxvectorsize;post("%d", count[0]);post("%d", count[1]);if (count[0]) {post("Signal come.");object_method(dsp64, gensym("dsp_add64"), x, butterFilterTest_perform64, 0, NULL);} else {//post("List come.");//object_method(dsp64, gensym("dsp_add64"), x, butterFilterTest_perform64_list, 0, NULL);}if (count[1]) {}if (count[2]) {}
}/*******************************************************逐帧验证**********************************************************/
void butterFilterTest_perform64_list(t_butterFilterTest* x, t_object* dsp64, double** ins, long numins, double** outs, long numouts,long sampleframes, long flags, void* userparam) {butterFilterTest_feed_test(x, sampleframes);//outlet_anything(x->outLet, gensym("list"), x->sampleframes, x->sigList);//post("%lf, %lf", x->iirState[0], x->iirState[1]);
}void butterFilterTest_feed_test(t_butterFilterTest* x, long sampleframes) {//post("%lf, %lf", x->iirState[0], x->iirState[1]);for (int k = 0; k < x->fltParaNum; k++) { //归一化atom_setfloat(&(x->b[k]), atom_getfloat(&(x->b[k])) / atom_getfloat(&(x->a[0])));atom_setfloat(&(x->a[k]), atom_getfloat(&(x->a[k])) / atom_getfloat(&(x->a[0])));}//Sleep(2000);for (int i = 0; i < sampleframes; i++) {atom_setfloat(&(x->sigList[i]), (atom_getfloat(&(x->b[0])) * atom_getfloat(&(x->tmpList[i])) + atom_getfloat(&(x->iirState[0]))));//post("sampleframs:%d, x->sigList[%d]: %lf, x->tmpList[%d]: %lf, x->iirState[0]: %lf", sampleframes, i, atom_getfloat(&(x->sigList[i])), i, atom_getfloat(&(x->tmpList[i])), x->iirState[0]);for (int j = 1; j < x->fltParaNum - 1; j++) { // j = 1atom_setfloat(&(x->iirState[j - 1]), (atom_getfloat(&(x->iirState[j])) + atom_getfloat(&(x->b[j])) * atom_getfloat(&(x->tmpList[i])) - atom_getfloat(&(x->a[j])) * atom_getfloat(&(x->sigList[i]))));//x->iirState[j - 1] = x->iirState[j] + x->b[j] * atom_getfloat(&(x->tmpList[i])) - x->a[j] * atom_getfloat(&(x->sigList[i]));}atom_setfloat(&(x->iirState[x->fltParaNum - 2]), (atom_getfloat(&(x->b[x->fltParaNum - 1])) * atom_getfloat(&(x->tmpList[i])) - atom_getfloat(&(x->a[x->fltParaNum - 1])) * atom_getfloat(&(x->sigList[i]))));//x->iirState[x->fltParaNum - 2] = x->b[x->fltParaNum - 1] * atom_getfloat(&(x->tmpList[i])) - x->a[x->fltParaNum - 1] * atom_getfloat(&(x->sigList[i]));}
}
/*************************************************************************************************************************/void butterFilterTest_perform64(t_butterFilterTest* x, t_object* dsp64, double** ins, long numins, double** outs, long numouts,long sampleframes, long flags, void* userparam) {  // this is the 64-bit perform method audio vectorst_double* inL = ins[0];		// we get audio for each inlet of the object from the **ins argumentt_double* outL = outs[0];	// we get audio for each outlet of the object from the **outs argumentint n = sampleframes;       // vector sizebutterFilterTest_feed(x, inL, sampleframes);post("perform64 iirstate:");for (int i = 0; i < x->fltStateParaNum; i++) {post("%lf", atom_getfloat(&(x->iirState[i])));}while (n--) {*outL++ = *inL++;}
}void butterFilterTest_feed(t_butterFilterTest* x, t_double* inSig, int sampleframes) {for (int k = 0; k < x->fltParaNum; k++) { // 归一化atom_setfloat(&(x->b[k]), atom_getfloat(&(x->b[k])) / atom_getfloat(&(x->a[0])));atom_setfloat(&(x->a[k]), atom_getfloat(&(x->a[k])) / atom_getfloat(&(x->a[0])));}for (int i = 0; i < sampleframes; i++) {t_double tmp = inSig[i];inSig[i] = atom_getfloat(&(x->b[0])) * tmp + atom_getfloat(&(x->iirState[0]));for (int j = 1; j < x->fltParaNum - 1; j++) {atom_setfloat(&(x->iirState[j - 1]), atom_getfloat(&(x->iirState[j])) + atom_getfloat(&(x->b[j])) * tmp - atom_getfloat(&(x->a[j])) * inSig[i]);//x->iirState[j - 1] = atom_getfloat(&(x->iirState[j])) + atom_getfloat(&(x->b[j])) * tmp - atom_getfloat(&(x->a[j])) * inSig[i];}atom_setfloat(&(x->iirState[x->fltParaNum - 2]), atom_getfloat(&(x->b[x->fltParaNum - 1])) * tmp - atom_getfloat(&(x->a[x->fltParaNum - 1])) * inSig[i]);//x->iirState[x->fltParaNum - 2] = x->b[x->fltParaNum - 1] * tmp - x->a[x->fltParaNum - 1] * inSig[i];}
}

这篇关于MAX/MSP SDK学习09:重要示例1的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/489329

相关文章

MySQL 添加索引5种方式示例详解(实用sql代码)

《MySQL添加索引5种方式示例详解(实用sql代码)》在MySQL数据库中添加索引可以帮助提高查询性能,尤其是在数据量大的表中,下面给大家分享MySQL添加索引5种方式示例详解(实用sql代码),... 在mysql数据库中添加索引可以帮助提高查询性能,尤其是在数据量大的表中。索引可以在创建表时定义,也可

Java集成Onlyoffice的示例代码及场景分析

《Java集成Onlyoffice的示例代码及场景分析》:本文主要介绍Java集成Onlyoffice的示例代码及场景分析,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要... 需求场景:实现文档的在线编辑,团队协作总结:两个接口 + 前端页面 + 配置项接口1:一个接口,将o

MySQL基本查询示例总结

《MySQL基本查询示例总结》:本文主要介绍MySQL基本查询示例总结,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧... 目录Create插入替换Retrieve(读取)select(确定列)where条件(确定行)null查询order by语句li

MybatisX快速生成增删改查的方法示例

《MybatisX快速生成增删改查的方法示例》MybatisX是基于IDEA的MyBatis/MyBatis-Plus开发插件,本文主要介绍了MybatisX快速生成增删改查的方法示例,文中通过示例代... 目录1 安装2 基本功能2.1 XML跳转2.2 代码生成2.2.1 生成.xml中的sql语句头2

MySQL数据库实现批量表分区完整示例

《MySQL数据库实现批量表分区完整示例》通俗地讲表分区是将一大表,根据条件分割成若干个小表,:本文主要介绍MySQL数据库实现批量表分区的相关资料,文中通过代码介绍的非常详细,需要的朋友可以参考... 目录一、表分区条件二、常规表和分区表的区别三、表分区的创建四、将既有表转换分区表脚本五、批量转换表为分区

PyTorch中cdist和sum函数使用示例详解

《PyTorch中cdist和sum函数使用示例详解》torch.cdist是PyTorch中用于计算**两个张量之间的成对距离(pairwisedistance)**的函数,常用于点云处理、图神经网... 目录基本语法输出示例1. 简单的 2D 欧几里得距离2. 批量形式(3D Tensor)3. 使用不

Python模拟串口通信的示例详解

《Python模拟串口通信的示例详解》pySerial是Python中用于操作串口的第三方模块,它支持Windows、Linux、OSX、BSD等多个平台,下面我们就来看看Python如何使用pySe... 目录1.win 下载虚www.chinasem.cn拟串口2、确定串口号3、配置串口4、串口通信示例5

C#使用MQTTnet实现服务端与客户端的通讯的示例

《C#使用MQTTnet实现服务端与客户端的通讯的示例》本文主要介绍了C#使用MQTTnet实现服务端与客户端的通讯的示例,包括协议特性、连接管理、QoS机制和安全策略,具有一定的参考价值,感兴趣的可... 目录一、MQTT 协议简介二、MQTT 协议核心特性三、MQTTNET 库的核心功能四、服务端(BR

SQL Server身份验证模式步骤和示例代码

《SQLServer身份验证模式步骤和示例代码》SQLServer是一个广泛使用的关系数据库管理系统,通常使用两种身份验证模式:Windows身份验证和SQLServer身份验证,本文将详细介绍身份... 目录身份验证方式的概念更改身份验证方式的步骤方法一:使用SQL Server Management S

Spring Boot 集成 Solr 的详细示例

《SpringBoot集成Solr的详细示例》:本文主要介绍SpringBoot集成Solr的详细示例,本文通过实例代码给大家介绍的非常详细,感兴趣的朋友一起看看吧... 目录环境准备添加依赖配置 Solr 连接定义实体类编写 Repository 接口创建 Service 与 Controller示例运行