Jamoma API  0.6.0.a19
j.unpack-/j.unpack.cpp
Go to the documentation of this file.
1 /** @file
2  *
3  * @ingroup implementationMaxExternalsGraph
4  *
5  * @brief j.unpack# - External object for Max/MSP to get Max values from a Jamoma Graph.
6  *
7  * @details
8  *
9  * @authors Tim Place, Trond Lossius
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 "maxGraph.h"
18 #include "TTCallback.h"
19 
20 
21 // Data Structure for this object
22 struct Unpack {
23  t_object obj;
24  TTGraphObjectBasePtr graphObject;
25  TTPtr graphOutlets[16]; ///< this _must_ be third (for the setup call)
26  TTObject* callback; ///< TTCallback object that attaches to the graphObject to be notified when there is new data to output.
27 };
28 typedef Unpack* UnpackPtr;
29 
30 
31 // Prototypes for methods
32 UnpackPtr UnpackNew (t_symbol *msg, long argc, t_atom* argv);
33 void UnpackFree (UnpackPtr self);
34 void UnpackAssist (UnpackPtr self, void* b, long msg, long arg, char* dst);
35 void UnpackGraphCallback (UnpackPtr self, TTValue& arg);
36 
37 
38 // Globals
39 static t_class* sUnpackClass;
40 
41 
42 /************************************************************************************/
43 // Main() Function
44 
45 int C74_EXPORT main(void)
46 {
47  t_class *c;
48 
49  TTGraphInit();
50  common_symbols_init();
51 
52  c = class_new("j.unpack-", (method)UnpackNew, (method)UnpackFree, sizeof(Unpack), (method)0L, A_GIMME, 0);
53 
54  class_addmethod(c, (method)MaxGraphReset, "graph.reset", A_CANT, 0);
55  class_addmethod(c, (method)MaxGraphConnect, "graph.connect", A_OBJ, A_LONG, 0);
56  class_addmethod(c, (method)MaxGraphDrop, "graph.drop", A_CANT, 0);
57  class_addmethod(c, (method)MaxGraphObject, "graph.object", A_CANT, 0);
58 
59  class_addmethod(c, (method)UnpackAssist, "assist", A_CANT, 0);
60  class_addmethod(c, (method)object_obex_dumpout, "dumpout", A_CANT, 0);
61 
62  class_register(_sym_box, c);
63  sUnpackClass = c;
64  return 0;
65 }
66 
67 
68 /************************************************************************************/
69 // Object Creation Method
70 
71 UnpackPtr UnpackNew(t_symbol *msg, long argc, t_atom* argv)
72 {
73  UnpackPtr self;
74  TTValue v, none;
75  TTErr err;
76 
77  self = UnpackPtr(object_alloc(sUnpackClass));
78  if (self) {
79  object_obex_store((void*)self, _sym_dumpout, (t_object*)outlet_new(self, NULL)); // dumpout
80  self->graphOutlets[0] = outlet_new(self, NULL);
81 
82  v.resize(2);
83  v[0] = "graph.output";
84  v[1] = 1;
85  err = TTObjectBaseInstantiate(TT("graph.object"), (TTObjectBasePtr*)&self->graphObject, v);
86 
87  if (!self->graphObject->mKernel.valid()) {
88  object_error(SELF, "cannot load Jamoma object");
89  return NULL;
90  }
91 
92  self->callback = new TTObject("callback");
93  self->callback->set(TT("function"), TTPtr(&UnpackGraphCallback));
94  self->callback->set(TT("baton"), TTPtr(self));
95 
96  // Dynamically add a message to the callback object so that it can handle the 'dictionaryReceived' notification
97  self->callback->instance()->registerMessage(TT("dictionaryReceived"), (TTMethod)&TTCallback::notify, kTTMessagePassValue);
98  // Tell the graph object that we want to watch it
99  self->graphObject->mKernel.registerObserverForNotifications(*self->callback);
100 
101  attr_args_process(self, argc, argv);
102  }
103  return self;
104 }
105 
106 
107 // Memory Deallocation
108 void UnpackFree(UnpackPtr self)
109 {
110  TTObjectBaseRelease((TTObjectBasePtr*)&self->graphObject);
111 }
112 
113 
114 /************************************************************************************/
115 // Methods bound to input/inlets
116 
117 // Method for Assistance Messages
118 void UnpackAssist(UnpackPtr self, void* b, long msg, long arg, char* dst)
119 {
120  if (msg==1) // Inlets
121  strcpy (dst, "multichannel input and control messages");
122  else if (msg==2) { // Outlets
123  if (arg == 0)
124  strcpy(dst, "multichannel output");
125  else
126  strcpy(dst, "dumpout");
127  }
128 }
129 
130 
131 // C Callback from any Graph Source objects we are observing
132 void UnpackGraphCallback(UnpackPtr self, TTValue& arg)
133 {
134  TTDictionaryPtr aDictionary = NULL;
135  TTValue v;
136  long ac;
137  t_atom* ap;
138  TTBoolean firstItemASymbol = NO;
139  TTSymbol firstItem;
140 
141  //arg.get(0, (TTPtr*)(&aDictionary));
142  aDictionary = (TTDictionaryPtr)(TTPtr)arg[0];
143  aDictionary->getValue(v);
144  ac = v.size();
145  if (ac) {
146  ap = new t_atom[ac];
147  for (int i=0; i<ac; i++) {
148  if (v[i].type() == kTypeInt8 ||
149  v[i].type() == kTypeUInt8 ||
150  v[i].type() == kTypeInt16 ||
151  v[i].type() == kTypeUInt16 ||
152  v[i].type() == kTypeInt32 ||
153  v[i].type() == kTypeUInt32 ||
154  v[i].type() == kTypeInt64 ||
155  v[i].type() == kTypeUInt64)
156  {
157  TTInt32 ival;
158 
159  ival = v[i];
160  atom_setlong(ap+i, ival);
161  }
162  else if (v[i].type() == kTypeFloat32 || v[i].type() == kTypeFloat64)
163  {
164  atom_setfloat(ap+i, v[i]);
165  }
166  else if (v[i].type() == kTypeSymbol)
167  {
168  TTSymbol s;
169 
170  s = v[i];
171  atom_setsym(ap+i, gensym((char*)s.c_str()));
172  if (i==0) {
173  firstItemASymbol = YES;
174  firstItem = s;
175  }
176  }
177  }
178 
179  if (firstItemASymbol)
180  outlet_anything(self->graphOutlets[0], gensym((char*)firstItem.c_str()), ac-1, ap+1);
181  else if (ac == 1)
182  outlet_float(self->graphOutlets[0], atom_getfloat(ap));
183  else
184  outlet_anything(self->graphOutlets[0], _sym_list, ac, ap);
185 
186  delete ap;
187  }
188 }
189 
190 
The TTGraphObjectBase wraps a TTDSP object such that it is possible to build a dynamic graph of audio...
bool TTBoolean
Boolean flag, same as Boolean on the Mac.
Definition: TTBase.h:167
TTErr TTObjectBaseRelease(TTObjectBasePtr *anObject)
DEPRECATED.
8-bit unsigned integer, range is 0 through 255.
Definition: TTBase.h:274
Create and use Jamoma object instances.
Definition: TTObject.h:29
64-bit unsigned integer, range is 0 through 18,446,744,073,709,551,615.
Definition: TTBase.h:280
size_type size() const noexcept
Return the number of elements.
Base class for all first-class Jamoma objects.
Definition: TTObjectBase.h:109
TTErr(TTObjectBase::* TTMethod)(const TTSymbol methodName, const TTValue &anInputValue, TTValue &anOutputValue)
A type that can be used to store a pointer to a message for an object.
Definition: TTObjectBase.h:46
TTErr getValue(T &aReturnedValue) const
Get the dictionary's primary value.
Definition: TTDictionary.h:202
Symbol type.
Definition: TTBase.h:282
16-bit unsigned integer, range is 0 through 65,535.
Definition: TTBase.h:276
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
#define TT
This macro is defined as a shortcut for doing a lookup in the symbol table.
Definition: TTSymbol.h:155
void * TTPtr
A generic pointer.
Definition: TTBase.h:248
int C74_EXPORT main(void)
Set up this class as a Max external the first time an object of this kind is instantiated.
This class is used to create a backward communication channel to notify a client that something chang...
16-bit signed integer, range is −32,768 through 32,767.
Definition: TTBase.h:275
64-bit floating point
Definition: TTBase.h:272
The TTSymbol class is used to represent a string and efficiently pass and compare that string...
Definition: TTSymbol.h:26
TTErr TTObjectBaseInstantiate(const TTSymbol className, TTObjectBasePtr *returnedObjectPtr, const TTValue arguments)
DEPRECATED.
const char * c_str() const
Return a pointer to the internal string as a C-string.
Definition: TTSymbol.h:77
std::int32_t TTInt32
32 bit signed integer
Definition: TTBase.h:177
64-bit signed integer, ragne is −9,223,372,036,854,775,808 through 9,223,372,036,854,775,807
Definition: TTBase.h:279
32-bit floating point
Definition: TTBase.h:271
Set this flag if the method you are binding to this message is prototyped with a single TTValue& argu...
Definition: TTObjectBase.h:87
32-bit signed integer, range is -2,147,483,648 through 2,147,483,647.
Definition: TTBase.h:277
TTErr
Jamoma Error Codes Enumeration of error codes that might be returned by any of the TTBlue functions a...
Definition: TTBase.h:342
32-bit unsigned integer, range is 0 through 4,294,967,295.
Definition: TTBase.h:278
8-bit signed integer, range is -128 through 127.
Definition: TTBase.h:273
TTErr notify(const TTValue &anInputValue, TTValue &anUnusedOutputValue)
Message called because we are registered as an observer to some other object, and then calls our exte...
Definition: TTCallback.cpp:69
TTDictionary * TTDictionaryPtr
[doxygenAppendixC_typedefExample]
Definition: TTDictionary.h:279
void resize(size_type n)
Change the number of elements.
[doxygenAppendixC_copyExample]
Definition: TTValue.h:34