Jamoma API  0.6.0.a19
quarantined/j.gain%/j.gain.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  TTMatrix* m_x; // Jamoma matrix object for input
25  TTMatrix* m_y; // Jamoma matrix object for output
26  TTMatrixObject* m_gain; // Jamoma object performing matrix operations
27 } t_gain;
28 
29 
30 // method prototypes
31 t_jit_err GainClassInit (void);
32 t_gain* GainNew (void);
33 void GainFree (t_gain* self);
34 t_jit_err GainMatrixCalc (t_gain* self, void *inputs, void *outputs);
35 t_jit_err GainGetGain (t_gain* self, Ptr attr, long* ac, t_atom** av);
36 t_jit_err GainSetGain (t_gain* self, Ptr attr, long ac, t_atom* av);
37 
38 
39 // globals
40 static t_class* s_gain_class = NULL;
41 
42 
43 /************************************************************************************/
44 
45 t_jit_err GainClassInit(void)
46 {
47  long attrflags = 0;// = JIT_ATTR_GET_DEFER_LOW | JIT_ATTR_SET_USURP_LOW;
48  t_jit_object *attr;
49  t_jit_object *mop;
50 
51  s_gain_class = (t_class*)jit_class_new((char*)"j_gain", (method)GainNew, (method)GainFree, sizeof(t_gain), 0);
52 
53  // add matrix operator (mop)
54  mop = (t_jit_object*)jit_object_new(_jit_sym_jit_mop, 1, 1); // args are num inputs and num outputs
55  jit_class_addadornment(s_gain_class, mop);
56 
57  // add method(s)
58  jit_class_addmethod(s_gain_class, (method)GainMatrixCalc, (char*)"matrix_calc", A_CANT, 0);
59 
60  // add attribute(s)
61  attr = (t_jit_object*)jit_object_new(_jit_sym_jit_attr_offset,
62  "gain",
63  _jit_sym_float32,
64  attrflags,
65  (method)GainGetGain, (method)GainSetGain,
66  NULL);
67  jit_class_addattr(s_gain_class, attr);
68 
69  // finalize class
70  jit_class_register(s_gain_class);
71  return JIT_ERR_NONE;
72 }
73 
74 
75 /************************************************************************************/
76 // Object Life Cycle
77 
78 t_gain* GainNew(void)
79 {
80  t_gain* self = (t_gain*)jit_object_alloc(s_gain_class);
81  TTValue empty;
82 
83  self->m_x = new TTMatrix();
84  self->m_y = new TTMatrix();
85  self->m_gain = new TTMatrixObject("matrix.gain", empty);
86 
87  return self;
88 }
89 
90 
91 void GainFree(t_gain* self)
92 {
93  delete self->m_gain;
94  delete self->m_y;
95  delete self->m_x;
96 }
97 
98 
99 /************************************************************************************/
100 // Methods bound to input/inlets
101 
102 t_jit_err GainMatrixCalc(t_gain* self, void *inputs, void *outputs)
103 {
104  t_jit_err err = JIT_ERR_NONE;
105  long in_savelock;
106  long out_savelock;
107  void *in_matrix;
108  void *out_matrix;
109 
110  in_matrix = jit_object_method(inputs,_jit_sym_getindex,0);
111  out_matrix = jit_object_method(outputs,_jit_sym_getindex,0);
112 
113  if (self && in_matrix && out_matrix) {
114  in_savelock = TTMatrixReferenceJitterMatrix(*self->m_x, in_matrix, false); // we're just scaling, and this is a trivial example, so we don't copy
115  out_savelock = TTMatrixReferenceJitterMatrix(*self->m_y, out_matrix, false); // we're just scaling, and this is a trivial example, so we don't copy
116 
117  self->m_gain->calculate(*self->m_x, *self->m_y);
118 
119  jit_object_method(out_matrix, _jit_sym_lock, out_savelock);
120  jit_object_method(in_matrix, _jit_sym_lock, in_savelock);
121  }
122  else
123  return JIT_ERR_INVALID_PTR;
124 
125  return err;
126 }
127 
128 
129 /************************************************************************************/
130 // Attribute Accessors
131 
132 t_jit_err GainGetGain(t_gain* self, Ptr attr, long* ac, t_atom** av)
133 {
134  TTFloat64 gain;
135 
136  if (*ac && *av) {
137  ; // memory passed-in, use it
138  }
139  else {
140  *av = (t_atom*)sysmem_newptr(sizeof(t_atom));
141  }
142  *ac = 1;
143 
144  self->m_gain->get(kTTSym_gain, gain);
145  atom_setfloat(*av, gain);
146  return JIT_ERR_NONE;
147 }
148 
149 
150 t_jit_err GainSetGain(t_gain* self, Ptr attr, long ac, t_atom* av)
151 {
152  self->m_gain->set(kTTSym_gain, atom_getfloat(av));
153  return JIT_ERR_NONE;
154 }
155 
double TTFloat64
64 bit floating point number
Definition: TTBase.h:188
Wrap TTMatrixBase instances.
Definition: TTMatrix.h:27
Wrap TTMatrixBase instances.
[doxygenAppendixC_copyExample]
Definition: TTValue.h:34