Jamoma API  0.6.0.a19
j.zerox~.cpp
Go to the documentation of this file.
1 /** @file
2  *
3  * @ingroup implementationMaxExternalsDSP
4  *
5  * @brief j.zerox~ : wraps the #TTZerocrossing class as a Jamoma external for MSP
6  *
7  * @details
8  *
9  * @authors Tim Place, Trond Lossius
10  *
11  * @copyright © 2006 by Timothy Place @n
12  * This code is licensed under the terms of the "New BSD License" @n
13  * http://creativecommons.org/licenses/BSD/
14  */
15 
16 
17 #include "TTClassWrapperMax.h"
18 #include "ext.h" // Max Header
19 #include "z_dsp.h" // MSP Header
20 #include "ext_strings.h" // String Functions
21 #include "commonsyms.h" // Common symbols used by the Max 4.5 API
22 #include "ext_obex.h" // Max Object Extensions (attributes) Header
23 
24 #include "TTDSP.h"
25 
26 
27 // Data Structure for this object
28 typedef struct _zerox {
29  t_pxobject obj; // This object - must be first
30  TTAudioObjectBasePtr zeroxUnit; //
31  TTAudioSignalPtr signalIn; //
32  TTAudioSignalPtr signalOut; //
33  long attr_size; //
34 } t_zerox;
35 
36 
37 // Prototypes for methods: need a method for each incoming message type
38 void* zerox_new(t_symbol *msg, long argc, t_atom *argv);
39 void zerox_free(t_zerox *x);
40 void zerox_assist(t_zerox *x, void *b, long m, long a, char *s);
41 t_max_err attr_set_size(t_zerox *x, void *attr, long argc, t_atom *argv);
42 t_int* zerox_perform(t_int *w);
43 void zerox_dsp(t_zerox *x, t_signal **sp, short *count);
44 void zerox_dsp64(t_zerox *x, t_object *dsp64, short *count, double samplerate, long maxvectorsize, long flags); // DSP64 Method
45 
46 // Globals
47 static t_class *s_zerox_class;
48 
49 
50 /************************************************************************************/
51 
52 int C74_EXPORT main(void)
53 {
54  long attrflags = 0;
55  t_class *c;
56  t_object *attr;
57 
58  TTDSPInit();
59  common_symbols_init();
60 
61  c = class_new("j.zerox~", (method)zerox_new, (method)zerox_free, sizeof(t_zerox), (method)0L, A_GIMME, 0);
62 
63  class_addmethod(c, (method)zerox_dsp, "dsp", A_CANT, 0L);
64  class_addmethod(c, (method)zerox_dsp64, "dsp64", A_CANT, 0);
65  class_addmethod(c, (method)zerox_assist, "assist", A_CANT, 0L);
66 
67  attr = attr_offset_new("size", _sym_long, attrflags,
68  (method)0L, (method)attr_set_size, calcoffset(t_zerox, attr_size));
69  class_addattr(c, attr);
70 
71  class_dspinit(c);
72  class_register(CLASS_BOX, c);
73  s_zerox_class = c;
74  return 0;
75 }
76 
77 
78 /************************************************************************************/
79 // Object Life
80 
81 // Create
82 void *zerox_new(t_symbol *msg, long argc, t_atom *argv)
83 {
84  t_zerox* x = (t_zerox*)object_alloc(s_zerox_class);
85 
86  if (x) {
87  object_obex_store((void *)x, _sym_dumpout, (object *)outlet_new(x, NULL)); // dumpout
88  dsp_setup((t_pxobject *)x, 1); // Create Object and 1 Inlet (last argument)
89  outlet_new((t_pxobject *)x, "signal"); // Create a signal Outlet
90  outlet_new((t_pxobject *)x, "signal"); // Create a signal Outlet
91 
92  TTObjectBaseInstantiate(TT("zerocross"), &x->zeroxUnit, 1);
93  TTObjectBaseInstantiate(TT("audiosignal"), &x->signalIn, 2);
94  TTObjectBaseInstantiate(TT("audiosignal"), &x->signalOut, 2);
95 
96  x->attr_size = 0;
97  attr_args_process(x, argc, argv); //handle attribute args
98  }
99  return x;
100 }
101 
102 // Destroy
103 void zerox_free(t_zerox *x)
104 {
105  dsp_free((t_pxobject *)x);
106  TTObjectBaseRelease(&x->zeroxUnit);
107  TTObjectBaseRelease(&x->signalIn);
108  TTObjectBaseRelease(&x->signalOut);
109 }
110 
111 
112 /************************************************************************************/
113 // Methods bound to input/inlets
114 
115 // Method for Assistance Messages
116 void zerox_assist(t_zerox *x, void *b, long msg, long arg, char *dst)
117 {
118  if (msg==1) // Inlet
119  strcpy(dst, "(signal) Input");
120  else if (msg==2) { // Outlet
121  switch(arg) {
122  case 0: strcpy(dst, "(signal) number of zero crossings"); break;
123  case 1: strcpy(dst, "(signal) trigger"); break;
124  case 2: strcpy(dst, "dumpout"); break;
125  }
126  }
127 }
128 
129 
130 // ATTRIBUTE: analysis size
131 t_max_err attr_set_size(t_zerox *x, void *attr, long argc, t_atom *argv)
132 {
133  x->attr_size = atom_getfloat(argv);
134  x->zeroxUnit->setAttributeValue(TT("size"), (TTUInt16)x->attr_size);
135  return MAX_ERR_NONE;
136 }
137 
138 
139 // Perform (signal) Method
140 t_int *zerox_perform(t_int *w)
141 {
142  t_zerox *x = (t_zerox *)(w[1]); // Pointer
143  t_float *in = (t_float *)(w[2]); // Input
144  t_float *out1 = (t_float *)(w[3]); // Output
145  t_float *out2 = (t_float *)(w[4]); // Output
146  TTUInt16 vs = x->signalIn->getVectorSizeAsInt();
147 
148  if (!x->obj.z_disabled) { // if we are not muted...
149  x->signalIn->setVector(0, vs, (t_float *)in);
150  x->zeroxUnit->process(x->signalIn, x->signalOut);
151  x->signalOut->getVector(0, vs, (t_float *)out1);
152  x->signalOut->getVector(1, vs, (t_float *)out2);
153  }
154  return w+6;
155 }
156 
157 
158 // DSP Method
159 void zerox_dsp(t_zerox *x, t_signal **sp, short *count)
160 {
161  x->zeroxUnit->sendMessage(TT("clear"));
162  x->zeroxUnit->setAttributeValue(kTTSym_sampleRate, sp[0]->s_sr);
163  dsp_add(zerox_perform, 5, x, sp[0]->s_vec, sp[1]->s_vec, sp[2]->s_vec, sp[0]->s_n);
164 
165  x->signalIn->setAttributeValue(kTTSym_vectorSize, (TTUInt16)sp[0]->s_n);
166  x->signalOut->setAttributeValue(kTTSym_vectorSize, (int) sp[0]->s_n);
167 
168  //signalIn will be set (alloc'd) in the perform method
169  x->signalOut->sendMessage(TT("alloc"));
170 }
171 
172 
173 void zerox_perform64(t_zerox *x, t_object *dsp64, double **ins, long numins, double **outs, long numouts, long sampleframes, long flags, void *userparam)
174 {
175  TTUInt16 vs = x->signalIn->getVectorSizeAsInt();
176 
177  x->signalIn->setVector(0, vs, ins[0]);
178  x->zeroxUnit->process(x->signalIn, x->signalOut);
179  x->signalOut->getVectorCopy(0, vs, outs[0]);
180  x->signalOut->getVectorCopy(1, vs, outs[1]);
181 }
182 
183 void zerox_dsp64(t_zerox *x, t_object *dsp64, short *count, double samplerate, long maxvectorsize, long flags)
184 {
185  x->zeroxUnit->sendMessage(TT("clear"));
186  x->zeroxUnit->setAttributeValue(kTTSym_sampleRate, samplerate);
187 
188  x->signalIn->setAttributeValue(kTTSym_vectorSize, (TTUInt16)maxvectorsize);
189  x->signalOut->setAttributeValue(kTTSym_vectorSize, (TTUInt16)maxvectorsize);
190 
191  x->signalOut->sendMessage(TT("alloc"));
192  object_method(dsp64, gensym("dsp_add64"), x, zerox_perform64, 0, NULL);
193 
194 }
std::uint16_t TTUInt16
16 bit unsigned integer
Definition: TTBase.h:176
TTErr TTObjectBaseRelease(TTObjectBasePtr *anObject)
DEPRECATED.
TTAudioObjectBase is the base class for all audio generating and processing objects in Jamoma DSP...
Jamoma DSP Library.
#define TT
This macro is defined as a shortcut for doing a lookup in the symbol table.
Definition: TTSymbol.h:155
TTErr TTObjectBaseInstantiate(const TTSymbol className, TTObjectBasePtr *returnedObjectPtr, const TTValue arguments)
DEPRECATED.
The TTAudioSignal class represents N vectors of audio samples for M channels.
Definition: TTAudioSignal.h:57
void TTDSP_EXPORT TTDSPInit(const char *pathToBinaries=NULL)
Initialise the Jamoma DSP library, as well as Jamoma Foundation foundation if needed.
Definition: TTDSP.cpp:30
int C74_EXPORT main(void)
Set up this class as a Max external the first time an object of this kind is instantiated.
Definition: j.zerox~.cpp:52