Jamoma API  0.6.0.a19
j.ramp~.cpp
Go to the documentation of this file.
1 /** @file
2  *
3  * @ingroup implementationMaxExternalsDSP
4  *
5  * @brief j.ramp~ : wraps the #TTRamp class as a Jamoma external for MSP
6  *
7  * @details
8  *
9  * @authors Tim Place, Trond Lossius
10  *
11  * @copyright © 2008 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" // TTBlue Interfaces...
25 
26 
27 // Data Structure for this object
28 typedef struct _ramp {
29  t_pxobject obj;
31  TTAudioSignalPtr audioOut;
32  t_symbol* attrMode;
33  TTUInt16 maxNumChannels;
34  TTUInt16 vs;
35 } t_ramp;
36 
37 
38 // Prototypes for methods: need a method for each incoming message type
39 void* ramp_new(t_symbol *msg, short argc, t_atom *argv); // New Object Creation Method
40 void ramp_free(t_ramp *x);
41 void ramp_assist(t_ramp *x, void *b, long msg, long arg, char *dst); // Assistance Method
42 t_int* ramp_perform(t_int *w); // An MSP Perform (signal) Method
43 void ramp_dsp(t_ramp *x, t_signal **sp, short *count); // DSP Method
44 void ramp_dsp64(t_ramp *x, t_object *dsp64, short *count, double samplerate, long maxvectorsize, long flags); // DSP64 Method
45 void ramp_stop(t_ramp *x);
46 void ramp_int(t_ramp *x, long newCurrentValue);
47 void ramp_float(t_ramp *x, double newCurrentValue);
48 void ramp_list(t_ramp *x, double endValue, double time);
49 t_max_err ramp_setMode(t_ramp *x, void *attr, long argc, t_atom *argv);
50 
51 
52 // Globals
53 t_class *ramp_class; // Required. Global pointing to this class
54 
55 
56 /************************************************************************************/
57 // Main() Function
58 
59 int C74_EXPORT main(void)
60 {
61  long attrflags = 0;
62  t_class *c;
63  t_object *attr;
64 
65  TTDSPInit();
66  common_symbols_init();
67 
68  c = class_new("j.ramp~",(method)ramp_new, (method)ramp_free, (short)sizeof(t_ramp),
69  (method)0L, A_GIMME, 0);
70 
71  class_addmethod(c, (method)ramp_int, "int", A_FLOAT, 0L);
72  class_addmethod(c, (method)ramp_float, "float", A_FLOAT, 0L);
73  class_addmethod(c, (method)ramp_list, "list", A_FLOAT, A_FLOAT);
74  class_addmethod(c, (method)ramp_stop, "stop", 0L);
75  class_addmethod(c, (method)ramp_dsp, "dsp", A_CANT, 0L);
76  class_addmethod(c, (method)ramp_dsp64, "dsp64", A_CANT, 0);
77  class_addmethod(c, (method)ramp_assist, "assist", A_CANT, 0L);
78  class_addmethod(c, (method)object_obex_dumpout, "dumpout", A_CANT, 0);
79 
80  attr = attr_offset_new("mode", _sym_symbol, attrflags,
81  (method)0L, (method)ramp_setMode, calcoffset(t_ramp, attrMode));
82  class_addattr(c, attr);
83 
84  class_dspinit(c); // Setup object's class to work with MSP
85  class_register(CLASS_BOX, c);
86  ramp_class = c;
87  return 0;
88 }
89 
90 
91 /************************************************************************************/
92 // Object Creation Method
93 
94 void* ramp_new(t_symbol *msg, short argc, t_atom *argv)
95 {
96  t_ramp *x;
97  TTValue sr(sys_getsr());
98  long attrstart = attr_args_offset(argc, argv); // support normal arguments
99  short i;
100 
101  x = (t_ramp *)object_alloc(ramp_class);
102  if (x) {
103  x->maxNumChannels = 2; // An initial argument to this object will set the maximum number of channels
104  if (attrstart && argv)
105  x->maxNumChannels = atom_getlong(argv);
106 
107  ttEnvironment->setAttributeValue(kTTSym_sampleRate, sr);
108  //x->ramp = new TTRamp(x->maxNumChannels);
109  TTObjectBaseInstantiate(TT("ramp"), &x->ramp, x->maxNumChannels);
110  TTObjectBaseInstantiate(TT("audiosignal"), &x->audioOut, x->maxNumChannels);
111 
112  attr_args_process(x,argc,argv); // handle attribute args
113 
114  object_obex_store((void *)x, _sym_dumpout, (object *)outlet_new(x,NULL)); // dumpout
115  dsp_setup((t_pxobject *)x, x->maxNumChannels); // inlets
116  for (i=0; i < x->maxNumChannels; i++)
117  outlet_new((t_pxobject *)x, "signal"); // outlets
118 
119  x->obj.z_misc = Z_NO_INPLACE;
120  }
121  return (x); // Return the pointer
122 }
123 
124 // Memory Deallocation
125 void ramp_free(t_ramp *x)
126 {
127  dsp_free((t_pxobject *)x);
128  TTObjectBaseRelease(&x->ramp);
129  TTObjectBaseRelease(&x->audioOut);
130 }
131 
132 
133 /************************************************************************************/
134 // Methods bound to input/inlets
135 
136 // Method for Assistance Messages
137 void ramp_assist(t_ramp *x, void *b, long msg, long arg, char *dst)
138 {
139  if (msg==1) { // Inlets
140  if (arg == 0)
141  strcpy(dst, "(signal) input 1, control messages");
142  else
143  snprintf(dst, 256, "(signal) input %ld", arg+1);
144  }
145  else if (msg==2) { // Outlets
146  if (arg == x->maxNumChannels)
147  strcpy(dst, "dumpout");
148  else
149  snprintf(dst, 256, "(signal) Filtered output %ld", arg+1);
150  }
151 }
152 
153 
154 void ramp_stop(t_ramp *x)
155 {
156  x->ramp->sendMessage(TT("stop"));
157 }
158 
159 
160 void ramp_int(t_ramp *x, long newCurrentValue)
161 {
162  ramp_float(x, newCurrentValue);
163 }
164 
165 void ramp_float(t_ramp *x, double newCurrentValue)
166 {
167  x->ramp->setAttributeValue(TT("startValue"), newCurrentValue);
168 }
169 
170 void ramp_list(t_ramp *x, double endValue, double time)
171 {
172  x->ramp->setAttributeValue(TT("destinationValue"), endValue);
173  x->ramp->setAttributeValue(TT("rampTime"), time);
174 }
175 
176 
177 // Perform (signal) Method
178 t_int *ramp_perform(t_int *w)
179 {
180  t_ramp *x = (t_ramp *)(w[1]);
181 
182  if (!(x->obj.z_disabled))
183  x->ramp->process(*x->audioOut);
184 
185  //TTAUDIOSIGNAL_GETVECTOR32(x->audioOut, 0, x->vs, w[2]);
186  x->audioOut->getVector(0, x->vs, (t_float *)(w[2]));
187 
188  return w+3;
189 }
190 
191 
192 // DSP Method
193 void ramp_dsp(t_ramp *x, t_signal **sp, short *count)
194 {
195  x->ramp->setAttributeValue(TT("sampleRate"), sp[0]->s_sr);
196  x->vs = sp[0]->s_n;
197 
198  x->audioOut->setAttributeValue(TT("numChannels"), x->maxNumChannels);
199  x->audioOut->setAttributeValue(TT("vectorSize"), TTUInt16(sp[0]->s_n));
200  x->audioOut->sendMessage(TT("alloc"));
201 
202  dsp_add(ramp_perform, 2, x, sp[0]->s_vec);
203 }
204 
205 
206 t_max_err ramp_setMode(t_ramp *x, void *attr, long argc, t_atom *argv)
207 {
208  if (argc) {
209  x->attrMode = atom_getsym(argv);
210  if (x->attrMode == gensym("sample_accurate"))
211  x->ramp->setAttributeValue(TT("mode"), TT("sample"));
212  else if (x->attrMode == gensym("vector_accurate"))
213  x->ramp->setAttributeValue(TT("mode"), TT("vector"));
214  }
215  return MAX_ERR_NONE;
216 }
217 
218 void ramp_perform64(t_ramp *x, t_object *dsp64, double **ins, long numins, double **outs, long numouts, long sampleframes, long flags, void *userparam)
219 {
220  x->ramp->process(*x->audioOut);
221  x->audioOut->getVector(0, x->vs, (t_float *)(outs[0]));
222 }
223 
224 void ramp_dsp64(t_ramp *x, t_object *dsp64, short *count, double samplerate, long maxvectorsize, long flags)
225 {
226  x->ramp->setAttributeValue(TT("sampleRate"), samplerate);
227  x->vs = maxvectorsize;
228 
229  x->audioOut->setAttributeValue(TT("numChannels"), x->maxNumChannels);
230  x->audioOut->setAttributeValue(TT("vectorSize"), (TTUInt16) maxvectorsize);
231  x->audioOut->sendMessage(TT("alloc"));
232  object_method(dsp64, gensym("dsp_add64"), x, ramp_perform64, 0, NULL);
233 }
234 
std::uint16_t TTUInt16
16 bit unsigned integer
Definition: TTBase.h:176
TTErr TTObjectBaseRelease(TTObjectBasePtr *anObject)
DEPRECATED.
TTFOUNDATION_EXPORT TTEnvironment * ttEnvironment
The environment object has one instance, which is global in scope.
TTAudioObjectBase is the base class for all audio generating and processing objects in Jamoma DSP...
void ramp_stop(TTPtr self)
Stop ongoing ramp.
Definition: j.ramp.cpp:227
Jamoma DSP Library.
TTErr setAttributeValue(const TTSymbol name, TTValue &value)
Set an attribute value for an object.
#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.ramp~.cpp:59
void ramp_int(TTPtr self, long n)
Method for int input, instantly updates current value of the object.
Definition: j.ramp.cpp:188
void ramp_list(TTPtr self, t_symbol *msg, long argc, t_atom *argv)
Method for list input
Definition: j.ramp.cpp:236
void ramp_float(TTPtr self, double f)
Method for float input, instantly updates current value of the object.
Definition: j.ramp.cpp:201
void ramp_assist(TTPtr self, void *b, long msg, long arg, char *dst)
Method for Assistance Messages.
Definition: j.ramp.cpp:162
[doxygenAppendixC_copyExample]
Definition: TTValue.h:34