Jamoma API  0.6.0.a19
TTInput.cpp
Go to the documentation of this file.
1 /** @file
2  *
3  * @ingroup modularLibrary
4  *
5  * @brief Handles any signal input
6  *
7  * @details
8  *
9  * @authors Théo de la Hogue
10  *
11  * @copyright © 2010, Théo de la Hogue @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 "TTInput.h"
18 
19 #define thisTTClass TTInput
20 #define thisTTClassName "Input"
21 #define thisTTClassTags "input"
22 
23 TT_MODULAR_CONSTRUCTOR,
24 mType(kTTSymEmpty),
25 mOutputAddress(kTTAdrsEmpty),
26 mMute(NO),
27 mBypass(NO),
28 mSignalAttr(NULL)
29 {
30  if (arguments.size() > 0)
31  mReturnSignalCallback = arguments[0];
32 
33  if (arguments.size() > 1) {
34  mSignalIn = arguments[1];
35  mSignalOut = arguments[2];
36  mSignalZero = arguments[3];
37  }
38 
40 
41  addAttributeWithSetter(OutputAddress, kTypeSymbol);
42  addAttributeProperty(OutputAddress, hidden, YES);
43 
45 
46  removeAttribute(kTTSym_bypass); // because there is already a Bypass attribute registered for all TTDataObject instances
47  addAttribute(Bypass, kTypeBoolean);
48 
50  addAttributeProperty(Signal, hidden, YES);
51  addAttributeProperty(Signal, readOnly, YES);
52 
54  addMessageProperty(Send, hidden, YES);
55 
57  addMessageProperty(Link, hidden, YES);
58 
59  addMessage(Unlink);
60  addMessageProperty(Unlink, hidden, YES);
61 
62  this->findAttribute(TTSymbol("signal"), &mSignalAttr);
63 }
64 
65 TTInput::~TTInput()
66 {
67  if (mOutputAddress != kTTSymEmpty)
68  accessApplicationLocalDirectory->removeObserverForNotifications(mOutputAddress, mAddressObserver);
69 }
70 
71 TTErr TTInput::Send(const TTValue& inputValue, TTValue& outputValue)
72 {
73  TTErr err;
74  TTValue none;
75 
76  if (!mReturnSignalCallback.valid())
77  return kTTErrGeneric;
78 
79  if (mMute)
80  return kTTErrNone;
81 
82  else if (mBypass && mOutputObject.valid())
83  err = mOutputObject.send("SendBypassed", inputValue);
84 
85  else
86  err = mReturnSignalCallback.send("notify", inputValue);
87 
88  notifySignalObserver(inputValue);
89 
90  return err;
91 }
92 
93 TTErr TTInput::Link(const TTValue& inputValue, TTValue& outputValue)
94 {
95  mOutputObject = inputValue[0];
96  return kTTErrNone;
97 }
98 
99 TTErr TTInput::Unlink()
100 {
101  mOutputObject = TTObject();
102  return kTTErrNone;
103 }
104 
105 TTErr TTInput::setOutputAddress(const TTValue& value)
106 {
107  TTValue args, none;
108  TTAddress newAddress;
109  TTNodePtr aNode;
110  TTList aNodeList;
111  TTObject o;
112 
113  newAddress = value[0];
114 
115  if (newAddress == kTTAdrsEmpty) {
116 
117  mAddressObserver.set(kTTSym_address, kTTAdrsEmpty);
118  mAddressObserver = TTObject();
119  return kTTErrGeneric;
120  }
121 
122  if (!accessApplicationLocalDirectory->getTTNode(newAddress, &aNode)) {
123 
124  o = aNode->getObject();
125  if (o.name() == kTTSym_Output|| o.name() == kTTSym_OutputAudio)
126  Link(o, none);
127  }
128 
129  //create a receiver if needed
130  if (!mAddressObserver.valid()) {
131 
132  // prepare arguments
133  mAddressObserver = TTObject("callback");
134 
135  mAddressObserver.set(kTTSym_baton, TTPtr(this)); // théo -- we have to register our self as a #TTPtr to not reference this instance otherwhise the destructor will never be called
136  mAddressObserver.set(kTTSym_function, TTPtr(&TTInputDirectoryCallback));
137  }
138 
139  if (mOutputAddress != kTTAdrsEmpty)
140  accessApplicationLocalDirectory->removeObserverForNotifications(mOutputAddress, mAddressObserver);
141 
142  accessApplicationLocalDirectory->addObserverForNotifications(newAddress, mAddressObserver, 0); // ask for notification only for equal addresses
143 
144  mOutputAddress = newAddress;
145 
146  return kTTErrNone;
147 }
148 
149 TTErr TTInput::notifySignalObserver(const TTValue& value)
150 {
151  mSignal = value;
152 
153  mSignalAttr->sendNotification(kTTSym_notify, mSignal); // we use kTTSym_notify because we know that observers are TTCallback
154 
155  return kTTErrNone;
156 }
157 
158 #if 0
159 #pragma mark -
160 #pragma mark Some Methods
161 #endif
162 
163 TTErr TTInputDirectoryCallback(const TTValue& baton, const TTValue& data)
164 {
165  TTObject o;
166  TTInputPtr anInput;
167  TTSymbol anAddress;
168  TTNodePtr aNode;
169  TTUInt8 flag;
170  TTValue none;
171 
172  // unpack baton (a #TTInputPtr)
173  anInput = TTInputPtr((TTPtr)baton[0]); // théo -- we have to register our self as a #TTPtr to not reference this instance otherwhise the destructor will never be called
174 
175  // Unpack data (anAddress, aNode, flag, anObserver)
176  anAddress = data[0];
177  aNode = TTNodePtr((TTPtr)data[1]);
178  flag = data[2];
179 
180  o = aNode->getObject();
181  if (o.valid()) {
182  if (o.name() == kTTSym_Output || o.name() == kTTSym_OutputAudio) {
183 
184  switch (flag) {
185 
186  case kAddressCreated :
187  {
188  anInput->Link(o, none);
189  break;
190  }
191 
192  case kAddressDestroyed :
193  {
194  anInput->Unlink();
195  break;
196  }
197 
198  default:
199  break;
200  }
201  }
202  }
203 
204  return kTTErrNone;
205 }
this flag means that a TTNode have been destroyed in the tree structure
Definition: TTAddressBase.h:56
#define addAttribute(name, type)
A convenience macro to be used by subclasses for registering attributes with a custom getter...
Definition: TTAttribute.h:29
We build a directory of TTNodes, and you can request a pointer for any TTNode, or add an observer to ...
Definition: TTNode.h:59
TTErr TTInputDirectoryCallback(const TTValue &baton, const TTValue &data)
Definition: TTInput.cpp:163
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
TTObject & getObject()
Get the object binded by this node.
Definition: TTNode.cpp:468
Symbol type.
Definition: TTBase.h:282
This is a special type used by TTAttribute to indicate that a value is a TTValue and is locally maint...
Definition: TTBase.h:286
void * TTPtr
A generic pointer.
Definition: TTBase.h:248
TTSymbol name() const
Return the name of this class.
Definition: TTObject.cpp:129
#define addAttributeProperty(attributeName, propertyName, initialValue)
A convenience macro to be used for registering properties of attributes.
Definition: TTAttribute.h:68
The TTSymbol class is used to represent a string and efficiently pass and compare that string...
Definition: TTSymbol.h:26
Boolean (1/0) or (true/false) flag.
Definition: TTBase.h:281
#define addMessageWithArguments(name)
A convenience macro to be used by subclasses for registering messages.
Definition: TTMessage.h:27
Handles any signal input.
void set(const TTUInt16 index, const T &anElementValue)
DEPRECATED.
Definition: TTValue.h:569
Something went wrong, but what exactly is not known. Typically used for context-specific problems...
Definition: TTBase.h:344
TTErr
Jamoma Error Codes Enumeration of error codes that might be returned by any of the TTBlue functions a...
Definition: TTBase.h:342
this flag means that a TTNode have been created in the tree structure
Definition: TTAddressBase.h:57
#define addAttributeWithSetter(name, type)
A convenience macro to be used by subclasses for registering attributes with a custom setter...
Definition: TTAttribute.h:47
#define addMessage(name)
A convenience macro to be used by subclasses for registering messages.
Definition: TTMessage.h:19
No Error.
Definition: TTBase.h:343
TTBoolean valid() const
Determine if the object contained by this TTObject is truly ready for use.
Definition: TTObject.cpp:179
#define accessApplicationLocalDirectory
Access to the local application directory.
[doxygenAppendixC_copyExample]
Definition: TTValue.h:34
unsigned char TTUInt8
8 bit unsigned integer (char)
Definition: TTBase.h:174
#define addMessageProperty(messageName, propertyName, initialValue)
A convenience macro to be used for registering properties of messages.
Definition: TTMessage.h:37