Jamoma API  0.6.0.a19
noise~.model.cpp
Go to the documentation of this file.
1 /** @file
2  * @ingroup implementationMaxModels
3  *
4  * @brief noise~.model: Wraps the #TTNoise class as a Jamoma model in the form of a C++ external
5  *
6  * @details
7  *
8  * @authors Tim Place, Trond Lossius
9  *
10  * @copyright Copyright © 2013, Tim Place @n
11  * This code is licensed under the terms of the "New BSD License" @n
12  * http://creativecommons.org/licenses/BSD/
13  */
14 
16 #include "ext.h" // Max Header
17 #include "z_dsp.h" // MSP Header
18 #include "ext_obex.h" // Max Object Extensions (attributes) Header
19 
20 #include "TTModel.h"
21 
22 // Data Structure for this object
23 typedef struct _noisemod {
24  t_pxobject obj;
25  TTModel *model;
26  TTAudioObject noise;
27  TTObject signalIn;
28  TTObject signalOut;
29  TTObject out;
30 } t_noisemod;
31 
32 
33 // Prototypes for methods
34 void* noisemod_new(t_symbol *s, long argc, t_atom *argv);
35 void noisemod_free(t_noisemod *x);
36 void noisemod_dsp64(t_noisemod *x, t_object *dsp64, short *count, double samplerate, long maxvectorsize, long flags);
37 void noisemod_assist(t_noisemod *x, void *b, long m, long a, char *s);
38 void noisemod_parameter_mode_callback(const TTValue& baton, const TTValue& v);
39 
40 
41 // Globals
42 static t_class* s_noisemod_class;
43 
44 
45 /************************************************************************************/
46 // Define our class
47 
48 int C74_EXPORT main(void)
49 {
50  t_class *c = class_new("noise~.model", (method)noisemod_new, (method)noisemod_free, sizeof(t_noisemod), (method)0L, A_GIMME, 0);
51 
52  TTDSPInit();
53 
54  class_addmethod(c, (method)noisemod_dsp64, "dsp64", A_CANT, 0);
55  class_addmethod(c, (method)object_obex_dumpout, "dumpout", A_CANT,0);
56  class_addmethod(c, (method)noisemod_assist, "assist", A_CANT, 0L);
57  class_dspinit(c);
58 
59  class_register(CLASS_BOX, c);
60  s_noisemod_class = c;
61  return 0;
62 }
63 
64 
65 /************************************************************************************/
66 // Object Life
67 
68 // Create
69 void* noisemod_new(t_symbol* s, long argc, t_atom* argv)
70 {
71  t_noisemod* x = (t_noisemod*)object_alloc(s_noisemod_class);
72  long attrstart = attr_args_offset(argc, argv);
73  TTString name;
74  TTPtr context;
75 
76  // Edit the name of our noise model using the first argument if exists
77  if (attrstart) {
78  if (atom_getsym(argv)->s_name[0] == '/')
79  name += atom_getsym(argv)->s_name + 1;
80  else
81  name += atom_getsym(argv)->s_name;
82  }
83  // Else call it "noise"
84  else
85  name += s->s_name;
86 
87  // Get the patcher as context pointer
88  object_obex_lookup(x, gensym("#P"), (t_object**)&context);
89 
90  // Create the actual DSP object
91  x->noise = TTObject("noise", 2);
92  assert(x->noise.valid());
93  x->signalOut = TTObject(kTTSym_audiosignal, 2);
94  x->signalIn = TTObject(kTTSym_audiosignal, 2);
95 
96  // Create the "model" container
97  x->model = new TTModel(name, context);
98 
99  // Add things to our model
100  x->model->createParameter("mode", (TTFunctionWithBatonAndValue)noisemod_parameter_mode_callback, x, "string", "set the mode of the noise generator", TTSymbol("white"));
101  x->model->createOutput("", x->out);
102  x->model->createPresetManager();
103 
104  // Initialize the module (set default values, etc)
105  x->model->init();
106 
107  // Do some final Max-specific stuff
108  dsp_setup((t_pxobject*)x, 1);
109  x->obj.z_misc = Z_NO_INPLACE;
110  outlet_new((t_pxobject*)x, "signal");
111  attr_args_process(x, argc, argv);
112  return x;
113 }
114 
115 
116 // Destroy
117 void noisemod_free(t_noisemod *x)
118 {
119  dsp_free((t_pxobject *)x);
120 
121  delete x->model;
122 }
123 
124 
125 /************************************************************************************/
126 
127 void noisemod_assist(t_noisemod *x, void *b, long msg, long arg, char *dst)
128 {
129 }
130 
131 
132 // Callback we receive when the parameter value changes
133 void noisemod_parameter_mode_callback(const TTValue& baton, const TTValue& v)
134 {
135  t_noisemod *x = (t_noisemod*)TTPtr(baton[0]);
136  x->noise.set("mode", v);
137 }
138 
139 
140 void noisemod_perform64(t_noisemod *x, t_object *dsp64, double **ins, long numins, double **outs, long numouts, long sampleframes, long flags, void *userparam)
141 {
142  // actually process the audio
143  TTAudioObjectBasePtr(x->noise.instance())->process(TTAudioSignalPtr(x->signalOut.instance()));
144 
145  // we re-use the memory from ins[], which is safe because we set Z_NO_INPLACE in the object constructor
146  TTAudioSignalPtr(x->signalOut.instance())->getVectorCopy(0, sampleframes, ins[0]);
147 
148  TTOutputAudioPtr(x->out.instance())->process(ins[0], outs[0], sampleframes);
149 }
150 
151 
152 void noisemod_dsp64(t_noisemod *x, t_object *dsp64, short *count, double samplerate, long maxvectorsize, long flags)
153 {
154  x->signalOut.set(kTTSym_numChannels, 1);
155  x->signalOut.set(kTTSym_vectorSize, (TTUInt16)maxvectorsize);;
156  x->signalOut.send(kTTSym_alloc);
157 
158  x->noise.set(kTTSym_sampleRate, samplerate);
159  object_method(dsp64, gensym("dsp_add64"), x, noisemod_perform64, 0, NULL);
160 
161  TTOutputAudioPtr(x->out.instance())->setupAudioSignals(maxvectorsize, samplerate);
162 }
std::uint16_t TTUInt16
16 bit unsigned integer
Definition: TTBase.h:176
int C74_EXPORT main(void)
Set up this class as a Max external the first time an object of this kind is instantiated.
Create and use Jamoma object instances.
Definition: TTObject.h:29
Wrap audio objects for convenience.
void * TTPtr
A generic pointer.
Definition: TTBase.h:248
TTAudioObjectBase * TTAudioObjectBasePtr
Pointer to a TTAudioObjectBase.
The TTSymbol class is used to represent a string and efficiently pass and compare that string...
Definition: TTSymbol.h:26
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
TTErr(* TTFunctionWithBatonAndValue)(const TTValue &, const TTValue &)
A simple/generic function pointer with a baton as TTValueRef and the value to send back as a TTValueR...
Definition: TTBase.h:361
Wraps Jamoma Core classes as objects for Max/MSP.
The TTString class is used to represent a string.
Definition: TTString.h:34
An audio output component for Jamoma models.
Definition: TTOutputAudio.h:23
[doxygenAppendixC_copyExample]
Definition: TTValue.h:34