Jamoma API  0.6.0.a19
ttclip~.cpp
1 /*
2  * ttclip~ Object for Pure Data
3  * Written by Tim Place
4  * Copyright © 2009
5  *
6  */
7 
8 #include "m_pd.h"
9 #include "TTDSP.h"
10 
11 
12 // Types & Structs
13 typedef struct _ttclip {
14  t_object obj;
15  t_outlet* outlet; // outlet for status and version messages
16  t_float f; // dummy for signal in first inlet
17  TTAudioObject* clipper; // the Jamoma DSP clipper object
18  TTAudioSignal* audioIn;
19  TTAudioSignal* audioOut;
20 } t_ttclip;
21 
22 
23 // Prototypes
24 extern "C" {
25  void ttclip_tilde_setup();
26 }
27 
28 void* ttclip_new(t_symbol *s, long ac, t_atom *at);
29 void ttclip_free(t_ttclip *x);
30 void ttclip_setlowbound(t_ttclip *x, float f);
31 void ttclip_sethighbound(t_ttclip *x, float f);
32 t_int* ttclip_perform(t_int *w);
33 void ttclip_dsp(t_ttclip *x, t_signal **sp);
34 
35 
36 // Statics & Globals
37 static t_class *ttclip_class;
38 
39 
40 // Class Definition
41 void ttclip_tilde_setup(void)
42 {
43  TTDSPInit();
44 
45  ttclip_class = class_new(gensym("ttclip~"), (t_newmethod)ttclip_new, (t_method)ttclip_free, sizeof(t_ttclip), 0, A_GIMME, 0);
46 
47  CLASS_MAINSIGNALIN(ttclip_class, t_ttclip, f);
48  class_addmethod(ttclip_class, (t_method)ttclip_dsp, gensym("dsp"), A_NULL);
49  class_addmethod(ttclip_class, (t_method)ttclip_setlowbound, gensym("lowbound"), A_FLOAT, 0);
50  class_addmethod(ttclip_class, (t_method)ttclip_sethighbound, gensym("highbound"), A_FLOAT, 0);
51 
52  class_sethelpsymbol(ttclip_class, gensym("help-ttclip~.pd"));
53 }
54 
55 
56 // Life Cycle
57 
58 void *ttclip_new(t_symbol *s, long ac, t_atom *at)
59 {
60  t_ttclip* x = (t_ttclip*)pd_new(ttclip_class);
61  TTUInt16 numChannels = 1; // Just mono now...
62  TTErr err;
63 
64  if(x){
65  outlet_new(&x->obj, gensym("signal")); // Create new signal outlet
66  x->clipper = NULL;
67  err = TTObjectInstantiate(TT("clipper"), &x->clipper, numChannels);
68  if (err)
69  post("ERROR FROM TTCLIP_NEW: %ld", err);
70 
71  TTObjectInstantiate(kTTSym_audiosignal, &x->audioIn, numChannels);
72  TTObjectInstantiate(kTTSym_audiosignal, &x->audioOut, numChannels);
73  }
74  return(x);
75 }
76 
77 
78 void ttclip_free(t_ttclip *x)
79 {
80  TTObjectRelease(&x->clipper);
81  TTObjectRelease(&x->audioIn);
82  TTObjectRelease(&x->audioOut);
83 }
84 
85 
86 // Methods
87 
88 // set attr
89 void ttclip_setlowbound(t_ttclip *x, float f)
90 {
91  x->clipper->setAttributeValue(TT("lowBound"), f);
92 }
93 
94 // set attr
95 void ttclip_sethighbound(t_ttclip *x, float f)
96 {
97  x->clipper->setAttributeValue(TT("highBound"), f);
98 }
99 
100 
101 // Perform (signal) Method
102 t_int *ttclip_perform(t_int *w)
103 {
104  t_ttclip* x = (t_ttclip*)(w[1]);
105  t_float* in = (t_float*)(w[2]);
106  t_float* out = (t_float*)(w[3]);
107  TTUInt16 vs = x->audioIn->getVectorSizeAsInt();
108 
109  x->audioIn->setVector(0, vs, (t_float*)in);
110  x->clipper->process(x->audioIn, x->audioOut);
111  x->audioOut->getVector(0, vs, (t_float*)out);
112 
113  return (w+5);
114 }
115 
116 
117 // DSP Method
118 void ttclip_dsp(t_ttclip *x, t_signal **sp)
119 {
120  x->audioIn->setNumChannels(1);
121  x->audioOut->setNumChannels(1);
122  x->audioIn->setVectorSizeWithInt(sp[0]->s_n);
123  x->audioOut->setVectorSizeWithInt(sp[0]->s_n);
124  //audioIn will be set in the perform method
125  x->audioOut->alloc();
126 
127  x->clipper->setAttributeValue(kTTSym_sampleRate, sp[0]->s_sr);
128 
129  dsp_add(ttclip_perform, 4, x, sp[0]->s_vec, sp[1]->s_vec, sp[0]->s_n);
130 }
131 
std::uint16_t TTUInt16
16 bit unsigned integer
Definition: TTBase.h:176
Wrap audio objects for convenience.
Jamoma DSP Library.
#define TT
This macro is defined as a shortcut for doing a lookup in the symbol table.
Definition: TTSymbol.h:155
The TTAudioSignal class represents N vectors of audio samples for M channels.
Definition: TTAudioSignal.h:57
void TTDSP_EXPORT TTDSPInit(const char *pathToBinaries=NULL)
Initialise the Jamoma DSP library, as well as Jamoma Foundation foundation if needed.
Definition: TTDSP.cpp:30
TTErr
Jamoma Error Codes Enumeration of error codes that might be returned by any of the TTBlue functions a...
Definition: TTBase.h:342