Jamoma API  0.6.0.a19
j.unit-.cpp
Go to the documentation of this file.
1 /** @file
2  *
3  * @ingroup implementationMaxExternalsGraph
4  *
5  * @brief j.unit- - Max external for Jamoma Graph that converts values from one kind of unit to another kind of unit
6  *
7  * @details The convertions make use of the DataspaceLib
8  *
9  * @authors Tim Place, Trond Lossius
10  *
11  * @copyright Copyright © 2011 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 
19 
20 // Data Structure for this object
21 struct Dataspace {
22  t_object obj;
23  TTGraphObjectBasePtr graphObject;
24  TTPtr graphOutlets[16]; // this _must_ be third (for the setup call)
25  t_symbol* attrDataspace;
26  t_symbol* attrInput;
27  t_symbol* attrOutput;
28 };
29 typedef Dataspace* DataspacePtr;
30 
31 
32 // Prototypes for methods
33 DataspacePtr DataspaceNew (t_symbol* msg, long argc, t_atom* argv);
34 void DataspaceFree (DataspacePtr self);
35 void DataspaceAssist (DataspacePtr self, void* b, long msg, long arg, char* dst);
36 t_max_err DataspaceSetDataspace (DataspacePtr self, void* attr, long argc, t_atom* argv);
37 t_max_err DataspaceSetInput (DataspacePtr self, void* attr, long argc, t_atom* argv);
38 t_max_err DataspaceGetInput (DataspacePtr self, void* attr, long* argc, t_atom** argv);
39 t_max_err DataspaceSetOutput (DataspacePtr self, void* attr, long argc, t_atom* argv);
40 t_max_err DataspaceGetOutput (DataspacePtr self, void* attr, long* argc, t_atom** argv);
41 
42 
43 // Globals
44 static t_class* sDataspaceClass;
45 
46 
47 /************************************************************************************/
48 // Main() Function
49 
50 int C74_EXPORT main(void)
51 {
52  t_class* c;
53 
54  TTGraphInit();
55  common_symbols_init();
56 
57  c = class_new("j.unit-", (method)DataspaceNew, (method)DataspaceFree, sizeof(Dataspace), (method)0L, A_GIMME, 0);
58 
59  class_addmethod(c, (method)MaxGraphReset, "graph.reset", A_CANT, 0);
60  class_addmethod(c, (method)MaxGraphSetup, "graph.setup", A_CANT, 0);
61  class_addmethod(c, (method)MaxGraphConnect, "graph.connect", A_OBJ, A_LONG, 0);
62  class_addmethod(c, (method)MaxGraphDrop, "graph.drop", A_CANT, 0);
63  class_addmethod(c, (method)MaxGraphObject, "graph.object", A_CANT, 0);
64 
65  class_addmethod(c, (method)DataspaceAssist, "assist", A_CANT, 0);
66  class_addmethod(c, (method)object_obex_dumpout, "dumpout", A_CANT, 0);
67 
68  CLASS_ATTR_SYM(c, "dataspace", 0, Dataspace, attrDataspace);
69  CLASS_ATTR_ACCESSORS(c, "dataspace", NULL, DataspaceSetDataspace);
70 
71  CLASS_ATTR_SYM(c, "input", 0, Dataspace, attrInput);
72  CLASS_ATTR_ACCESSORS(c, "input", DataspaceGetInput, DataspaceSetInput);
73 
74  CLASS_ATTR_SYM(c, "output", 0, Dataspace, attrOutput);
75  CLASS_ATTR_ACCESSORS(c, "output", DataspaceGetInput, DataspaceSetOutput);
76 
77  class_register(_sym_box, c);
78  sDataspaceClass = c;
79  return 0;
80 }
81 
82 
83 /************************************************************************************/
84 // Object Creation Method
85 
86 DataspacePtr DataspaceNew(t_symbol* msg, long argc, t_atom* argv)
87 {
88  DataspacePtr self;
89  TTValue v;
90  TTErr err;
91 
92  self = DataspacePtr(object_alloc(sDataspaceClass));
93  if (self) {
94  object_obex_store((void*)self, _sym_dumpout, (t_object*)outlet_new(self, NULL)); // dumpout
95  self->graphOutlets[0] = outlet_new(self, "graph.connect");
96  self->attrDataspace = _sym_none;
97 
98  v.resize(2);
99  v[0] = "dataspace";
100  v[1] = 1;
101  err = TTObjectBaseInstantiate(TT("graph.object"), (TTObjectBasePtr*)&self->graphObject, v);
102 
103  if (!self->graphObject->mKernel.valid()) {
104  object_error(SELF, "cannot load Jamoma object");
105  return NULL;
106  }
107 
108  attr_args_process(self, argc, argv);
109  }
110  return self;
111 }
112 
113 
114 // Memory Deallocation
115 void DataspaceFree(DataspacePtr self)
116 {
117  TTObjectBaseRelease((TTObjectBasePtr*)&self->graphObject);
118 }
119 
120 
121 /************************************************************************************/
122 // Methods bound to input/inlets
123 
124 // Method for Assistance Messages
125 void DataspaceAssist(DataspacePtr self, void* b, long msg, long arg, char* dst)
126 {
127  if (msg==1) // Inlets
128  strcpy (dst, "dictionary input and control messages");
129  else if (msg==2) { // Outlets
130  if (arg == 0)
131  strcpy(dst, "dictionary output");
132  else
133  strcpy(dst, "dumpout");
134  }
135 }
136 
137 
138 // ATTRIBUTE SETTERS
139 
140 t_max_err DataspaceSetDataspace(DataspacePtr self, void* attr, long argc, t_atom* argv)
141 {
142  if (argc) {
143  self->attrDataspace = atom_getsym(argv);
144  self->graphObject->mKernel.set(TT("dataspace"), TT(self->attrDataspace->s_name));
145  }
146  return MAX_ERR_NONE;
147 }
148 
149 
150 t_max_err DataspaceSetInput(DataspacePtr self, void* attr, long argc, t_atom* argv)
151 {
152  if (argc) {
153  self->attrInput = atom_getsym(argv);
154  self->graphObject->mKernel.set(TT("inputUnit"), TT(self->attrInput->s_name));
155  }
156  return MAX_ERR_NONE;
157 }
158 
159 
160 t_max_err DataspaceGetInput(DataspacePtr self, void* attr, long* argc, t_atom** argv)
161 {
162  TTValue v;
163  TTSymbol s;
164 
165  if (*argc && *argv) {
166  self->graphObject->mKernel.get(TT("inputUnit"), v);
167  s = v[0];
168  atom_setsym(*argv, gensym(s.c_str()));
169  }
170  return MAX_ERR_NONE;
171 }
172 
173 
174 t_max_err DataspaceSetOutput(DataspacePtr self, void* attr, long argc, t_atom* argv)
175 {
176  if (argc) {
177  self->attrOutput = atom_getsym(argv);
178  self->graphObject->mKernel.set(TT("outputUnit"), TT(self->attrOutput->s_name));
179  }
180  return MAX_ERR_NONE;
181 }
182 
183 
184 t_max_err DataspaceGetOutput(DataspacePtr self, void* attr, long* argc, t_atom** argv)
185 {
186  TTValue v;
187  TTSymbol s;
188 
189  if (*argc && *argv) {
190  self->graphObject->mKernel.get(TT("outputUnit"), v);
191  s = v[0];
192  atom_setsym(*argv, gensym(s.c_str()));
193  }
194  return MAX_ERR_NONE;
195 }
196 
The TTGraphObjectBase wraps a TTDSP object such that it is possible to build a dynamic graph of audio...
TTErr TTObjectBaseRelease(TTObjectBasePtr *anObject)
DEPRECATED.
Base class for all first-class Jamoma objects.
Definition: TTObjectBase.h:109
#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
The TTSymbol class is used to represent a string and efficiently pass and compare that string...
Definition: TTSymbol.h:26
int C74_EXPORT main(void)
Set up this class as a Max external the first time an object of this kind is instantiated.
Definition: j.unit-.cpp:50
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
TTErr
Jamoma Error Codes Enumeration of error codes that might be returned by any of the TTBlue functions a...
Definition: TTBase.h:342
void resize(size_type n)
Change the number of elements.
[doxygenAppendixC_copyExample]
Definition: TTValue.h:34