Jamoma API  0.6.0.a19
j.gain copy.cpp
Go to the documentation of this file.
1 /** @file
2  *
3  * @ingroup implementationMaxExternalsJitter
4  *
5  * @brief j.gain% : Scale values in a matrix
6  *
7  * @details This is the actual Jitter object
8  *
9  * @authors Tim Place, Trond Lossius
10  *
11  * @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 "jit.common.h"
18 #include "TTClassWrapperMax.h"
19 
20 
21 // types and structures
22 typedef struct _j_gain {
23  t_object obj;
24  TTMatrixPtr x; // Jamoma matrix object for input
25  TTMatrixPtr y; // Jamoma matrix object for output
26  TTDataObjectBasePtr gainObject; // Jamoma object performing matrix operations
27 } GainObject;
28 
29 typedef GainObject* GainObjectPtr;
30 
31 
32 // method prototypes
33 t_jit_err GainClassInit (void);
34 GainObjectPtr GainNew (void);
35 void GainFree (GainObjectPtr self);
36 t_jit_err GainMatrixCalc (GainObjectPtr self, void *inputs, void *outputs);
37 t_jit_err GainGetGain (GainObjectPtr self, Ptr attr, AtomCount* ac, AtomPtr* av);
38 t_jit_err GainSetGain (GainObjectPtr self, Ptr attr, AtomCount ac, AtomPtr av);
39 
40 
41 // globals
42 static ClassPtr sGainClass = NULL;
43 
44 
45 /************************************************************************************/
46 
47 t_jit_err GainClassInit(void)
48 {
49  long attrflags = 0;// = JIT_ATTR_GET_DEFER_LOW | JIT_ATTR_SET_USURP_LOW;
50  t_jit_object *attr;
51  t_jit_object *mop;
52 
53  sGainClass = (ClassPtr)jit_class_new((char*)"j_gain", (method)GainNew, (method)GainFree, sizeof(GainObject), 0);
54 
55  // add matrix operator (mop)
56  mop = (t_jit_object*)jit_object_new(_jit_sym_jit_mop, 1, 1); // args are num inputs and num outputs
57  jit_class_addadornment(sGainClass, mop);
58 
59  // add method(s)
60  jit_class_addmethod(sGainClass, (method)GainMatrixCalc, (char*)"matrix_calc", A_CANT, 0);
61 
62  // add attribute(s)
63  attr = (t_jit_object*)jit_object_new(_jit_sym_jit_attr_offset,
64  "gain",
65  _jit_sym_float32,
66  attrflags,
67  (method)GainGetGain, (method)GainSetGain,
68  NULL);
69  jit_class_addattr(sGainClass, attr);
70 
71  // finalize class
72  jit_class_register(sGainClass);
73  return JIT_ERR_NONE;
74 }
75 
76 
77 /************************************************************************************/
78 // Object Life Cycle
79 
80 GainObjectPtr GainNew(void)
81 {
82  GainObjectPtr self = (GainObjectPtr)jit_object_alloc(sGainClass);
83  TTValue none;
84  TTErr err;
85 
86  if (self) {
87  err = TTObjectBaseInstantiate(kTTSym_matrix, (TTObjectBasePtr*)&self->x, none);
88  err = TTObjectBaseInstantiate(kTTSym_matrix, (TTObjectBasePtr*)&self->y, none);
89  err = TTObjectBaseInstantiate(TT("matrix.gain"), (TTObjectBasePtr*)&self->gainObject, none);
90  }
91  return self;
92 }
93 
94 
95 void GainFree(GainObjectPtr self)
96 {
97  TTErr err;
98 
99  err = TTObjectBaseRelease((TTObjectBasePtr*)&self->x);
100  err = TTObjectBaseRelease((TTObjectBasePtr*)&self->y);
101  err = TTObjectBaseRelease((TTObjectBasePtr*)&self->gainObject);
102 }
103 
104 
105 /************************************************************************************/
106 // Methods bound to input/inlets
107 
108 t_jit_err GainMatrixCalc(GainObjectPtr self, void *inputs, void *outputs)
109 {
110  t_jit_err err = JIT_ERR_NONE;
111  long in_savelock;
112  long out_savelock;
113  void *in_matrix;
114  void *out_matrix;
115 
116  in_matrix = jit_object_method(inputs,_jit_sym_getindex,0);
117  out_matrix = jit_object_method(outputs,_jit_sym_getindex,0);
118 
119  if (self && in_matrix && out_matrix) {
120  in_savelock = TTMatrixReferenceJitterMatrix(self->x, in_matrix, false); // we're just scaling, and this is a trivial example, so we don't copy
121  out_savelock = TTMatrixReferenceJitterMatrix(self->y, out_matrix, false); // we're just scaling, and this is a trivial example, so we don't copy
122 
123  self->gainObject->calculate(self->x, self->y);
124 
125  jit_object_method(out_matrix, _jit_sym_lock, out_savelock);
126  jit_object_method(in_matrix, _jit_sym_lock, in_savelock);
127  }
128  else
129  return JIT_ERR_INVALID_PTR;
130 
131  return err;
132 }
133 
134 
135 /************************************************************************************/
136 // Attribute Accessors
137 
138 t_jit_err GainGetGain(GainObjectPtr self, Ptr attr, AtomCount* ac, AtomPtr* av)
139 {
140  TTFloat64 gain;
141 
142  if (*ac && *av) {
143  ; // memory passed-in, use it
144  }
145  else {
146  *av = (AtomPtr)sysmem_newptr(sizeof(Atom));
147  }
148  *ac = 1;
149 
150  self->gainObject->getAttributeValue(kTTSym_gain, gain);
151  atom_setfloat(*av, gain);
152  return JIT_ERR_NONE;
153 }
154 
155 
156 t_jit_err GainSetGain(GainObjectPtr self, Ptr attr, AtomCount ac, AtomPtr av)
157 {
158  self->gainObject->setAttributeValue(kTTSym_gain, atom_getfloat(av));
159  return JIT_ERR_NONE;
160 }
161 
TTErr TTObjectBaseRelease(TTObjectBasePtr *anObject)
DEPRECATED.
TTDataObjectBase is the base class for all data generating and processing objects.
Base class for all first-class Jamoma objects.
Definition: TTObjectBase.h:109
double TTFloat64
64 bit floating point number
Definition: TTBase.h:188
#define TT
This macro is defined as a shortcut for doing a lookup in the symbol table.
Definition: TTSymbol.h:155
TTErr TTObjectBaseInstantiate(const TTSymbol className, TTObjectBasePtr *returnedObjectPtr, const TTValue arguments)
DEPRECATED.
TTErr
Jamoma Error Codes Enumeration of error codes that might be returned by any of the TTBlue functions a...
Definition: TTBase.h:342
[doxygenAppendixC_copyExample]
Definition: TTValue.h:34