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

相关文章

SpringCloud集成AlloyDB的示例代码

《SpringCloud集成AlloyDB的示例代码》AlloyDB是GoogleCloud提供的一种高度可扩展、强性能的关系型数据库服务,它兼容PostgreSQL,并提供了更快的查询性能... 目录1.AlloyDBjavascript是什么?AlloyDB 的工作原理2.搭建测试环境3.代码工程1.

Java中ArrayList的8种浅拷贝方式示例代码

《Java中ArrayList的8种浅拷贝方式示例代码》:本文主要介绍Java中ArrayList的8种浅拷贝方式的相关资料,讲解了Java中ArrayList的浅拷贝概念,并详细分享了八种实现浅... 目录引言什么是浅拷贝?ArrayList 浅拷贝的重要性方法一:使用构造函数方法二:使用 addAll(

Golang使用etcd构建分布式锁的示例分享

《Golang使用etcd构建分布式锁的示例分享》在本教程中,我们将学习如何使用Go和etcd构建分布式锁系统,分布式锁系统对于管理对分布式系统中共享资源的并发访问至关重要,它有助于维护一致性,防止竞... 目录引言环境准备新建Go项目实现加锁和解锁功能测试分布式锁重构实现失败重试总结引言我们将使用Go作

JAVA利用顺序表实现“杨辉三角”的思路及代码示例

《JAVA利用顺序表实现“杨辉三角”的思路及代码示例》杨辉三角形是中国古代数学的杰出研究成果之一,是我国北宋数学家贾宪于1050年首先发现并使用的,:本文主要介绍JAVA利用顺序表实现杨辉三角的思... 目录一:“杨辉三角”题目链接二:题解代码:三:题解思路:总结一:“杨辉三角”题目链接题目链接:点击这里

SpringBoot使用注解集成Redis缓存的示例代码

《SpringBoot使用注解集成Redis缓存的示例代码》:本文主要介绍在SpringBoot中使用注解集成Redis缓存的步骤,包括添加依赖、创建相关配置类、需要缓存数据的类(Tes... 目录一、创建 Caching 配置类二、创建需要缓存数据的类三、测试方法Spring Boot 熟悉后,集成一个外

Springboot使用RabbitMQ实现关闭超时订单(示例详解)

《Springboot使用RabbitMQ实现关闭超时订单(示例详解)》介绍了如何在SpringBoot项目中使用RabbitMQ实现订单的延时处理和超时关闭,通过配置RabbitMQ的交换机、队列和... 目录1.maven中引入rabbitmq的依赖:2.application.yml中进行rabbit

Python绘制土地利用和土地覆盖类型图示例详解

《Python绘制土地利用和土地覆盖类型图示例详解》本文介绍了如何使用Python绘制土地利用和土地覆盖类型图,并提供了详细的代码示例,通过安装所需的库,准备地理数据,使用geopandas和matp... 目录一、所需库的安装二、数据准备三、绘制土地利用和土地覆盖类型图四、代码解释五、其他可视化形式1.

opencv实现像素统计的示例代码

《opencv实现像素统计的示例代码》本文介绍了OpenCV中统计图像像素信息的常用方法和函数,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一... 目录1. 统计像素值的基本信息2. 统计像素值的直方图3. 统计像素值的总和4. 统计非零像素的数量

Python使用asyncio实现异步操作的示例

《Python使用asyncio实现异步操作的示例》本文主要介绍了Python使用asyncio实现异步操作的示例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋... 目录1. 基础概念2. 实现异步 I/O 的步骤2.1 定义异步函数2.2 使用 await 等待异

如何评价Ubuntu 24.04 LTS? Ubuntu 24.04 LTS新功能亮点和重要变化

《如何评价Ubuntu24.04LTS?Ubuntu24.04LTS新功能亮点和重要变化》Ubuntu24.04LTS即将发布,带来一系列提升用户体验的显著功能,本文深入探讨了该版本的亮... Ubuntu 24.04 LTS,代号 Noble NumBAT,正式发布下载!如果你在使用 Ubuntu 23.