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

相关文章

Python中logging模块用法示例总结

《Python中logging模块用法示例总结》在Python中logging模块是一个强大的日志记录工具,它允许用户将程序运行期间产生的日志信息输出到控制台或者写入到文件中,:本文主要介绍Pyt... 目录前言一. 基本使用1. 五种日志等级2.  设置报告等级3. 自定义格式4. C语言风格的格式化方法

Spring 中的切面与事务结合使用完整示例

《Spring中的切面与事务结合使用完整示例》本文给大家介绍Spring中的切面与事务结合使用完整示例,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考... 目录 一、前置知识:Spring AOP 与 事务的关系 事务本质上就是一个“切面”二、核心组件三、完

sky-take-out项目中Redis的使用示例详解

《sky-take-out项目中Redis的使用示例详解》SpringCache是Spring的缓存抽象层,通过注解简化缓存管理,支持Redis等提供者,适用于方法结果缓存、更新和删除操作,但无法实现... 目录Spring Cache主要特性核心注解1.@Cacheable2.@CachePut3.@Ca

QT Creator配置Kit的实现示例

《QTCreator配置Kit的实现示例》本文主要介绍了使用Qt5.12.12与VS2022时,因MSVC编译器版本不匹配及WindowsSDK缺失导致配置错误的问题解决,感兴趣的可以了解一下... 目录0、背景:qt5.12.12+vs2022一、症状:二、原因:(可以跳过,直奔后面的解决方法)三、解决方

MySQL中On duplicate key update的实现示例

《MySQL中Onduplicatekeyupdate的实现示例》ONDUPLICATEKEYUPDATE是一种MySQL的语法,它在插入新数据时,如果遇到唯一键冲突,则会执行更新操作,而不是抛... 目录1/ ON DUPLICATE KEY UPDATE的简介2/ ON DUPLICATE KEY UP

Python中Json和其他类型相互转换的实现示例

《Python中Json和其他类型相互转换的实现示例》本文介绍了在Python中使用json模块实现json数据与dict、object之间的高效转换,包括loads(),load(),dumps()... 项目中经常会用到json格式转为object对象、dict字典格式等。在此做个记录,方便后续用到该方

MySQL分库分表的实践示例

《MySQL分库分表的实践示例》MySQL分库分表适用于数据量大或并发压力高的场景,核心技术包括水平/垂直分片和分库,需应对分布式事务、跨库查询等挑战,通过中间件和解决方案实现,最佳实践为合理策略、备... 目录一、分库分表的触发条件1.1 数据量阈值1.2 并发压力二、分库分表的核心技术模块2.1 水平分

SpringBoot请求参数传递与接收示例详解

《SpringBoot请求参数传递与接收示例详解》本文给大家介绍SpringBoot请求参数传递与接收示例详解,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋... 目录I. 基础参数传递i.查询参数(Query Parameters)ii.路径参数(Path Va

RabbitMQ 延时队列插件安装与使用示例详解(基于 Delayed Message Plugin)

《RabbitMQ延时队列插件安装与使用示例详解(基于DelayedMessagePlugin)》本文详解RabbitMQ通过安装rabbitmq_delayed_message_exchan... 目录 一、什么是 RabbitMQ 延时队列? 二、安装前准备✅ RabbitMQ 环境要求 三、安装延时队

Redis实现高效内存管理的示例代码

《Redis实现高效内存管理的示例代码》Redis内存管理是其核心功能之一,为了高效地利用内存,Redis采用了多种技术和策略,如优化的数据结构、内存分配策略、内存回收、数据压缩等,下面就来详细的介绍... 目录1. 内存分配策略jemalloc 的使用2. 数据压缩和编码ziplist示例代码3. 优化的