Jamoma API  0.6.0.a19
TTGraphObjectBase.cpp
Go to the documentation of this file.
1 /** @file
2  *
3  * @ingroup graphLibrary
4  *
5  * @brief Jamoma Asynchronous Object Graph Layer
6  *
7  * @details Creates a wrapper for TTObjectBases that can be used to build a control graph for asynchronous message passing
8  *
9  * @authors Timothy Place
10  *
11  * @copyright Copyright © 2010 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 "TTGraphObjectBase.h"
18 #include "TTGraphInlet.h"
19 #include "TTGraphOutlet.h"
20 
21 #define thisTTClass TTGraphObjectBase
22 #define thisTTClassName "graph.object"
23 #define thisTTClassTags "graph, wrapper"
24 
25 
26 // Arguments
27 // 1. (required) The name of the Jamoma DSP object you want to wrap
28 // 2. (optional) Number of inlets, default = 1
29 // 3. (optional) Number of outlets, default = 1
30 
32 mKernel(arguments[0], 1) // first arg is the name of the class, second arg is initialChannelCount
33 {
34  TT_ASSERT(graph_correct_instantiation_args, (arguments.size() > 0));
35 
36  TTUInt16 numInlets = 1;
37  TTUInt16 numOutlets = 1;
38 
39  if (arguments.size() > 1)
40  numInlets = arguments[1];
41  if (arguments.size() > 2)
42  numOutlets = arguments[2];
43 
44  mDictionary = new TTDictionary;
45 
46  mInlets.resize(numInlets);
47  mOutlets.resize(numOutlets);
48 }
49 
50 
51 TTGraphObjectBase::~TTGraphObjectBase()
52 {
53  delete mDictionary;
54 }
55 
56 
57 void TTGraphObjectBase::prepareDescription()
58 {
59  if (valid && mDescription.mClassName != kTTSymEmpty) {
60  mDescription.sIndex = 0;
61  mDescription.mClassName = kTTSymEmpty;
62 
63  for (TTGraphInletIter inlet = mInlets.begin(); inlet != mInlets.end(); inlet++)
64  inlet->prepareDescriptions();
65  }
66 }
67 
68 
69 void TTGraphObjectBase::getDescription(TTGraphDescription& desc)
70 {
71  if (mDescription.mClassName != kTTSymEmpty) { // a description for this object has already been created -- use it.
72  desc = mDescription;
73  }
74  else { // create a new description for this object.
75  desc.mClassName = mKernel.name();
76  desc.mObjectInstance = mKernel;
77  desc.mInputDescriptions.clear();
78  desc.mID = desc.sIndex++;
79  mDescription = desc;
80 
81  for (TTGraphInletIter inlet = mInlets.begin(); inlet != mInlets.end(); inlet++)
82  inlet->getDescriptions(desc.mInputDescriptions);
83  }
84 }
85 
86 
88 {
89  for_each(mInlets.begin(), mInlets.end(), std::mem_fun_ref(&TTGraphInlet::reset));
90  for_each(mOutlets.begin(), mOutlets.end(), std::mem_fun_ref(&TTGraphOutlet::reset));
91  return kTTErrNone;
92 }
93 
94 
95 TTErr TTGraphObjectBase::handshake(TTGraphObjectBasePtr objectWhichIsBeingConnected, TTUInt16 fromOutletNumber, TTUInt16 toInletNumber)
96 {
97  TTErr err;
98 
99  err = mOutlets[fromOutletNumber].connect(objectWhichIsBeingConnected, toInletNumber);
100  return err;
101 }
102 
103 
104 TTErr TTGraphObjectBase::connect(TTGraphObjectBasePtr anObject, TTUInt16 fromOutletNumber, TTUInt16 toInletNumber)
105 {
106  TTErr err;
107 
108  err = mInlets[toInletNumber].connect(anObject, fromOutletNumber);
109  anObject->handshake(this, fromOutletNumber, toInletNumber);
110 
111  return err;
112 }
113 
114 
115 TTErr TTGraphObjectBase::drop(TTGraphObjectBasePtr anObject, TTUInt16 fromOutletNumber, TTUInt16 toInletNumber)
116 {
118 
119  if (toInletNumber < mInlets.size())
120  err = mInlets[toInletNumber].drop(anObject, fromOutletNumber);
121  if (fromOutletNumber < mOutlets.size())
122  err = mOutlets[fromOutletNumber].drop(anObject, toInletNumber);
123  return err;
124 }
125 
126 
127 TTErr TTGraphObjectBase::push(const TTDictionary& aDictionary)
128 {
129  TTSymbol schema = aDictionary.getSchema();
130  TTValue v;
132  TTMessagePtr message = NULL;
133 
134  // If an object defines a 'dictionary' message then this trumps all the others
135  err = mKernel.instance()->findMessage(TT("dictionary"), &message);
136  if (!err && message) {
137  (*mDictionary) = aDictionary;
138  v.append(TTPtr(mDictionary));
139  err = mKernel.send(TT("dictionary"), v, v); // returns an error if dictionary is unhandled
140  }
141 
142  if (err) {
143  if (schema == TT("number")) {
144  aDictionary.getValue(v);
145  // TODO: maybe try seeing if there is a "number" message first and then prefer that if it exists?
146  err = mKernel.send(TT("calculate"), v, v);
147 
148  mDictionary->setSchema(TT("number"));
149  mDictionary->setValue(v);
150  // NOTE: doesn't have inlet/outlet info at this point
151  }
152  else if (schema == TT("message")) {
153  TTValue nameValue;
154  TTSymbol nameSymbol = kTTSymEmpty;
155 
156  aDictionary.lookup(TT("name"), nameValue);
157  aDictionary.getValue(v);
158  nameSymbol = nameValue[0];
159  err = mKernel.send(nameSymbol, v, v);
160 
161  mDictionary->setSchema(TT("message"));
162  mDictionary->append(TT("name"), nameValue);
163  mDictionary->setValue(v);
164  }
165  else if (schema == TT("attribute")) {
166  TTValue nameValue;
167  TTSymbol nameSymbol = kTTSymEmpty;
168 
169  aDictionary.lookup(TT("name"), nameValue);
170  aDictionary.getValue(v);
171  nameSymbol = nameValue[0];
172  err = mKernel.set(nameSymbol, v);
173 
174  mDictionary->setSchema(TT("attribute"));
175  mDictionary->remove(TT("name"));
176  mDictionary->append(TT("name"), nameValue);
177  mDictionary->setValue(v);
178  }
179  else {
180  // not sure what to do with other dictionary schemas yet...
181  (*mDictionary) = aDictionary;
182  }
183  }
184 
185  for (TTGraphOutletIter outlet = mOutlets.begin(); outlet != mOutlets.end(); outlet++)
186  outlet->push(*mDictionary);
187 
188  return err;
189 }
190 
The TTGraphObjectBase wraps a TTDSP object such that it is possible to build a dynamic graph of audio...
std::uint16_t TTUInt16
16 bit unsigned integer
Definition: TTBase.h:176
An inappropriate value was specified for an attribute or variable.
Definition: TTBase.h:349
TTErr send(const TTSymbol aName)
Send a message to this object with no arguments.
Definition: TTObject.cpp:135
This class represents a single message, as used by the TTObjectBase class.
Definition: TTMessage.h:55
TTErr reset()
Clear the lists of connected objects.
TTErr append(const TTSymbol aKey, const TTValue aValue)
Insert an item into the hash table.
Definition: TTDictionary.h:212
TTErr connect(TTGraphObjectBasePtr anObject, TTUInt16 fromOutletNumber=0, TTUInt16 toInletNumber=0)
Add a source to the list of objects from which to request audio.
TTErr findMessage(const TTSymbol name, TTMessage **message)
Find a message registered with this object.
TTGraphInletVector mInlets
The inlets through which we pull audio from sources.
Jamoma Asynchronous Object Graph Layer.
TTErr getValue(T &aReturnedValue) const
Get the dictionary's primary value.
Definition: TTDictionary.h:202
TTBoolean valid
If the object isn't completely built, or is in the process of freeing, this will be false...
Definition: TTObjectBase.h:124
TTGraphDescription mDescription
our copy of the data, from which we pass out references to the outlets
#define TT
This macro is defined as a shortcut for doing a lookup in the symbol table.
Definition: TTSymbol.h:155
A type that represents the key as a C-String and the value as a pointer to the matching TTSymbol obje...
Definition: TTDictionary.h:47
void append(const T &anElementValueToAppend)
Insert a single TTElement at the end.
Definition: TTValue.h:243
void * TTPtr
A generic pointer.
Definition: TTBase.h:248
Jamoma Asynchronous Object Graph Layer.
TTSymbol name() const
Return the name of this class.
Definition: TTObject.cpp:129
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
const TTSymbol getSchema() const
TODO: Add documentation.
Definition: TTDictionary.h:181
Jamoma Asynchronous Object Graph Layer.
static TTInt32 sIndex
Used for creating mID.
TTErr setSchema(const TTSymbol aSchemaName)
TODO: Add documentation schemaName TODO: Add documentation.
Definition: TTDictionary.h:172
TTErr setValue(const TTValue aNewValue)
TODO: Add documentation.
Definition: TTDictionary.h:191
TTErr
Jamoma Error Codes Enumeration of error codes that might be returned by any of the TTBlue functions a...
Definition: TTBase.h:342
TTInt32 mID
An index number that uniquely identifies this instance.
TTErr lookup(const TTSymbol aKey, TTValue &aValue) const
Find the value for the given key.
Definition: TTDictionary.h:221
TTObject mKernel
The actual TT object doing the processing.
TTObjectBase * instance() const
Return a direct pointer to the internal instance.
Definition: TTObject.cpp:105
No Error.
Definition: TTBase.h:343
TTGraphOutletVector mOutlets
The inlets through which we pull audio from sources.
TTErr drop(TTGraphObjectBasePtr anObject, TTUInt16 fromOutletNumber=0, TTUInt16 toInletNumber=0)
Drop a source from the list of objects from which to request audio.
Method not found. Typically returned by the TTObject::sendMessage() function.
Definition: TTBase.h:351
TT_OBJECT_CONSTRUCTOR
Constructor macro.
This object represents a single 'outlet' from a TTGraphObjectBase.
[doxygenAppendixC_copyExample]
Definition: TTValue.h:34
TTErr remove(const TTSymbol aKey)
Remove an item from the hash table.
Definition: TTDictionary.h:231