Jamoma API  0.6.0.a19
pack.cpp
1 /*
2  * in≈
3  * External object for Pd to Provide a source for TTAudioSignals usable by a Jamoma AudioGraph dsp chain.
4  * Copyright © 2010 by Timothy Place
5  *
6  * License: This code is licensed under the terms of the "New BSD License"
7  * http://creativecommons.org/licenses/BSD/
8  */
9 
10 #include "PureAudioGraph.h"
11 
12 // Data Structure for this object
13 struct In {
14  Object obj;
15  TTAudioGraphObjectPtr audioGraphObject;
16  _outlet* audioGraphObjectOutlet;
17  t_float f; // dummy for signal in first inlet
18  TTUInt32 maxNumChannels; // the number of inlets or outlets, which is an argument at instantiation
19  TTUInt32 numChannels; // the actual number of channels to use, set by the dsp method
20  TTUInt32 vectorSize; // cached by the DSP method
21 };
22 typedef In* InPtr;
23 
24 
25 // Prototypes for methods
26 extern "C" void setup_jcom_pack0x3d(void);
27 InPtr InNew(SymbolPtr msg, AtomCount argc, AtomPtr argv);
28 void InFree(InPtr self);
29 TTErr InReset(InPtr self, long vectorSize);
30 TTErr InSetup(InPtr self);
31 TTErr InObject(InPtr self, TTAudioGraphObjectPtr audioSourceObject);
32 t_int* InPerform(t_int* w);
33 void InDsp(InPtr self, t_signal** sp, short* count);
34 
35 
36 // Globals
37 static ClassPtr sInClass;
38 
39 
40 /************************************************************************************/
41 // Main() Function
42 
43 void setup_jcom_pack0x3d(void)
44 {
45  TTAudioGraphInit();
46 
47  sInClass = class_new(gensym("jcom_pack="), (t_newmethod)InNew, (t_method)InFree, sizeof(In), 0, A_GIMME, 0);
48 
49  CLASS_MAINSIGNALIN(sInClass, In, f);
50  class_addmethod(sInClass, (t_method)InReset, gensym("audio.reset"), A_CANT, 0);
51  class_addmethod(sInClass, (t_method)InSetup, gensym("audio.setup"), A_CANT, 0);
52  class_addmethod(sInClass, (t_method)InDsp, gensym("dsp"), A_CANT, 0);
53 
54  class_sethelpsymbol(sInClass, gensym("help-jcom_pack=.pd"));
55 }
56 
57 
58 /************************************************************************************/
59 // Object Creation Method
60 
61 InPtr InNew(SymbolPtr msg, AtomCount argc, AtomPtr argv)
62 {
63  InPtr self;
64  TTValue sr(sys_getsr());
65  TTValue v;
66  TTErr err;
67 
68  self = InPtr(pd_new(sInClass));
69  if (self) {
70  self->maxNumChannels = 2;
71  if (argc && argv)
72  self->maxNumChannels = atom_getfloat(argv) + 0.1;
73 
74  ttEnvironment->setAttributeValue(kTTSym_SampleRate, sr);
75 
76  v.setSize(3);
77  v.set(0, TT("audio.generator"));
78  v.set(1, 0); // no audio graph inlets (only pd audio inlets)
79  v.set(2, 1); // one audio graph outlet
80  err = TTObjectInstantiate(TT("audio.object"), (TTObjectPtr*)&self->audioGraphObject, v);
81  self->audioGraphObject->addAudioFlag(kTTAudioGraphGenerator);
82 
83  if (!self->audioGraphObject->getUnitGenerator()) {
84  error("in=: cannot load audio.generator");
85  return NULL;
86  }
87 
88  self->audioGraphObjectOutlet = outlet_new(SELF, gensym("audio.connect"));
89 
90  for (TTUInt16 i=1; i<self->maxNumChannels; i++)
91  inlet_new(&self->obj, &self->obj.ob_pd, &s_signal, &s_signal);
92 
93  //self->obj.z_misc = Z_NO_INPLACE | Z_PUT_FIRST;
94  }
95  return self;
96 }
97 
98 
99 // Memory Deallocation
100 void InFree(InPtr self)
101 {
102  TTObjectRelease((TTObjectPtr*)&self->audioGraphObject);
103 }
104 
105 
106 /************************************************************************************/
107 // Methods bound to input/inlets
108 
109 
110 TTErr InReset(InPtr self, long vectorSize)
111 {
112  return self->audioGraphObject->resetAudio();
113 }
114 
115 
116 TTErr InSetup(InPtr self)
117 {
118  Atom a[2];
119 
120  a[0].a_type = A_POINTER;
121  a[1].a_type = A_POINTER;
122  a[0].a_w.w_symbol = SymbolPtr(self->audioGraphObject);
123  a[1].a_w.w_symbol = 0;
124 
125  outlet_anything(self->audioGraphObjectOutlet, gensym("audio.connect"), 2, a);
126  return kTTErrNone;
127 }
128 
129 
130 // Perform (signal) Method
131 t_int* InPerform(t_int* w)
132 {
133  InPtr self = (InPtr)(w[1]);
134  TTUInt32 i;
135 
136  for (i=0; i < self->numChannels; i++)
137  TTAudioGraphGeneratorPtr(self->audioGraphObject->getUnitGenerator())->mBuffer->setVector(i, self->vectorSize, (TTFloat32*)w[i+2]);
138 
139  return w + (self->numChannels+2);
140 }
141 
142 
143 // DSP Method
144 void InDsp(InPtr self, t_signal** sp, short* count)
145 {
146  TTUInt16 i, k=0;
147  void **audioVectors = NULL;
148 
149  self->vectorSize = sp[0]->s_n;
150 
151  // Setup the perform method
152  audioVectors = (void**)malloc(sizeof(void*) * (self->maxNumChannels + 1));
153  audioVectors[k] = self;
154  k++;
155 
156  self->numChannels = 0;
157  for (i=0; i < self->maxNumChannels; i++) {
158  self->numChannels++;
159  audioVectors[k] = sp[i]->s_vec;
160  k++;
161  }
162 
163  self->audioGraphObject->setOutputNumChannels(0, self->maxNumChannels);
164  self->audioGraphObject->getUnitGenerator()->setAttributeValue(TT("VectorSize"), self->vectorSize);
165  self->audioGraphObject->getUnitGenerator()->setAttributeValue(TT("MaxNumChannels"), self->maxNumChannels);
166  self->audioGraphObject->getUnitGenerator()->setAttributeValue(TT("SampleRate"), sp[0]->s_sr);
167 
168  dsp_addv(InPerform, k, (t_int*)audioVectors);
169  free(audioVectors);
170 }
std::uint16_t TTUInt16
16 bit unsigned integer
Definition: TTBase.h:176
TTFOUNDATION_EXPORT TTEnvironment * ttEnvironment
The environment object has one instance, which is global in scope.
This object is an audio generator, and do not expect audio input.
Definition: TTAudioGraph.h:73
TTErr setAttributeValue(const TTSymbol name, TTValue &value)
Set an attribute value for an object.
#define TT
This macro is defined as a shortcut for doing a lookup in the symbol table.
Definition: TTSymbol.h:155
void setSize(const TTUInt16 arg)
DEPRECATED.
Definition: TTValue.h:528
TTAudioGraphGenerator * TTAudioGraphGeneratorPtr
Pointer to a TTAudioGraphGenerator.
float TTFloat32
32 bit floating point number
Definition: TTBase.h:187
void set(const TTUInt16 index, const T &anElementValue)
DEPRECATED.
Definition: TTValue.h:569
TTErr
Jamoma Error Codes Enumeration of error codes that might be returned by any of the TTBlue functions a...
Definition: TTBase.h:342
std::uint32_t TTUInt32
32 bit unsigned integer
Definition: TTBase.h:178
No Error.
Definition: TTBase.h:343
[doxygenAppendixC_copyExample]
Definition: TTValue.h:34