Jamoma API  0.6.0.a19
TTModel.h
1 /*
2  allpass~.model
3  A simple Jamoma 0.6 model, created in C++ rather than in Max.
4 
5  By Tim Place, Copyright © 2013
6 
7  License: This code is licensed under the terms of the "New BSD License"
8  http://creativecommons.org/licenses/BSD/
9 */
10 
12 #include "ext.h" // Max Header
13 #include "z_dsp.h" // MSP Header
14 #include "ext_obex.h" // Max Object Extensions (attributes) Header
15 
16 
17 class TTModel {
18 
19  TTObject mContainer; // a model is based on a #TTcontainer class
20  TTSymbol mName; // the name of our model
21  TTPtr mContext; // the context of our model
22 
23  TTObject mApplicationLocal; // ease the access to our local application
24  TTList mMembers; // the name of each registered members
25 
26 public:
27  TTModel(TTSymbol aName, TTPtr aContext) :
28  mName(aName),
29  mContext(aContext),
30  mApplicationLocal(accessApplicationLocal)
31  {
32  // create container object
33  mContainer = TTObject("Container"); // TODO: (optional) pass address callback and value callback (these are both for the activity return mechanism)
34  mContainer.set(kTTSym_tags, kTTSym_model);
35 
36  // register the container object into the local application directly under the root
37  TTAddress address = kTTAdrsRoot.appendAddress(TTAddress(mName));
38  TTValue v, args = TTValue(address, mContainer, mContext);
39  mApplicationLocal.send("ObjectRegister", args, v);
40 
41  // set the effective registration address as container address
42  mContainer.set("address", v);
43  }
44 
45  ~TTModel()
46  {
47  TTValue none;
48 
49  // unregister all members (and destroy them if nobody else references them)
50  for (mMembers.begin(); mMembers.end(); mMembers.end())
51  {
52  TTAddress address = mMembers.current()[0];
53  mApplicationLocal.send("ObjectUnregister", address, none);
54  }
55  }
56 
57  /** Create a data object -- used by the creators for parameter/message/return */
58  TTErr createData(TTSymbol aName, TTSymbol aServiceName, TTFunctionWithBatonAndValue aCallbackFunction, TTPtr aBaton, TTObject& dataObject)
59  {
60  // create a data object
61  dataObject = TTObject("Data", aServiceName);
62  dataObject.set("baton", aBaton);
63  dataObject.set("function", TTPtr(aCallbackFunction));
64 
65  // register the data object into the local application under a /mName/aName address
66  TTAddress address = kTTAdrsRoot.appendAddress(TTAddress(mName)).appendAddress(TTAddress(aName));
67  TTValue v, args = TTValue(address, dataObject);
68  mApplicationLocal.send("ObjectRegister", args, v);
69 
70  // remember its effective registration address to unregister it later
71  mMembers.append(v);
72 
73  return kTTErrNone;
74  }
75 
76 
77  TTErr createParameter(TTSymbol aName, TTFunctionWithBatonAndValue aCallbackFunction, TTPtr aBaton, TTSymbol aType, TTSymbol aDescription, TTValue aDefaultValue)
78  {
79  TTObject parameterObject;
80  TTErr err = createData(aName, kTTSym_parameter, aCallbackFunction, aBaton, parameterObject);
81 
82  // set attributes
83  if (!err) {
84 
85  parameterObject.set("type", aType);
86  parameterObject.set("description", aDescription);
87  parameterObject.set("defaultValue", aDescription);
88  }
89 
90  return err;
91  }
92 
93 
94  TTErr createMessage(TTSymbol aName, TTFunctionWithBatonAndValue aCallbackFunction, TTPtr aBaton, TTSymbol aType, TTSymbol aDescription)
95  {
96  TTObject messageObject;
97  TTErr err = createData(aName, kTTSym_message, aCallbackFunction, aBaton, messageObject);
98 
99  // set attributes
100  if (!err) {
101 
102  messageObject.set("type", aType);
103  messageObject.set("description", aDescription);
104  }
105 
106  return err;
107  }
108 
109 
110  TTErr createReturn(TTSymbol aName, TTFunctionWithBatonAndValue aCallbackFunction, TTPtr aBaton, TTSymbol aType, TTSymbol aDescription)
111  {
112  TTObject returnObject;
113  TTErr err = createData(aName, kTTSym_return, aCallbackFunction, aBaton, returnObject);
114 
115  // set attributes
116  if (!err) {
117 
118  returnObject.set("type", aType);
119  returnObject.set("description", aDescription);
120  }
121 
122  return err;
123  }
124 
125 
126  TTErr createInput(TTSymbol aName, TTObject& inputAudioObject)
127  {
128  // create a audio input object
129  inputAudioObject = TTObject("Input.audio");
130 
131  // register the audio input object into the local application under a /mName/audio/in.aName address
132  TTAddress address = kTTAdrsRoot.appendAddress(TTAddress(mName)).appendAddress(TTAddress("audio/in")).appendInstance(TTAddress(aName));
133  TTValue v, args = TTValue(address, inputAudioObject);
134  mApplicationLocal.send("ObjectRegister", args, v);
135 
136  // remember its effective registration address to unregister it later
137  mMembers.append(v);
138 
139  return kTTErrNone;
140  }
141 
142 
143  TTErr createOutput(TTSymbol aName, TTObject& outputAudioObject)
144  {
145  // create a audio output object
146  outputAudioObject = TTObject("Output.audio");
147 
148  // register the audio output object into the local application under a /mName/audio/out.aName address
149  TTAddress address = kTTAdrsRoot.appendAddress(TTAddress(mName)).appendAddress(TTAddress("audio/out")).appendInstance(TTAddress(aName));
150  TTValue v, args = TTValue(address, outputAudioObject);
151  mApplicationLocal.send("ObjectRegister", args, v);
152 
153  // remember its effective registration address to unregister it later
154  mMembers.append(v);
155 
156  return kTTErrNone;
157  }
158 
159  TTErr createPresetManager()
160  {
161  // create a preset manager object
162  TTObject presetManagerObject = TTObject("PresetManager");
163 
164  // register the preset manager object into the local application under a /mName/preset address
165  TTAddress address = kTTAdrsRoot.appendAddress(TTAddress(mName)).appendAddress(TTAddress("preset"));
166  TTValue v, args = TTValue(address, presetManagerObject);
167  mApplicationLocal.send("ObjectRegister", args, v);
168 
169  // remember its effective registration address to unregister it later
170  mMembers.append(v);
171 
172  // TODO: tell it to load a preset
173  // 1. create TTXMLHandler object
174  // 2. set the object attr of the above object to be the PresetManager
175  // 3. send read message to the TTXMLHandler w/ the filepath as an argument
176  // 4. send recall message on the PresetManager
177  // 5. free the TTXMLHandler
178 
179  return kTTErrNone;
180  }
181 
182 
183  void init()
184  {
185  mContainer.send("Init");
186  }
187 
188 };
189 
TTAddress appendAddress(const TTAddress &toAppend)
Return a new TTAddress with the appended part.
Definition: TTAddress.h:167
TTErr send(const TTSymbol aName)
Send a message to this object with no arguments.
Definition: TTObject.cpp:135
The TTAddress class is used to represent a string and efficiently pass and compare that string...
Definition: TTAddress.h:29
Create and use Jamoma object instances.
Definition: TTObject.h:29
TTAddress appendInstance(const TTSymbol anInstance)
Return a new TTAddress with a instance part.
Definition: TTAddress.h:173
void * TTPtr
A generic pointer.
Definition: TTBase.h:248
TTErr set(const TTSymbol aName, T aValue)
Set an attribute value for an object.
The TTSymbol class is used to represent a string and efficiently pass and compare that string...
Definition: TTSymbol.h:26
#define accessApplicationLocal
Access to local application.
TTErr
Jamoma Error Codes Enumeration of error codes that might be returned by any of the TTBlue functions a...
Definition: TTBase.h:342
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.
No Error.
Definition: TTBase.h:343
[doxygenAppendixC_copyExample]
Definition: TTValue.h:34