Jamoma API  0.6.0.a19
TTPulseSub.cpp
1 /*
2  * TTBlue Pulse-based Envelope Substitution
3  * Copyright © 2004, Timothy Place
4  *
5  * License: This code is licensed under the terms of the "New BSD License"
6  * http://creativecommons.org/licenses/BSD/
7  */
8 
9 #include "TTPulseSub.h"
10 
11 #define thisTTClass TTPulseSub
12 #define thisTTClassName "pulsesub"
13 #define thisTTClassTags "dspGeneratorLib, audio, processor, dynamics, envelope"
14 
15 
16 TT_AUDIO_CONSTRUCTOR,
17  attrMode(TT("linear")),
18  env_gen(NULL),
19  phasor(NULL),
20  offset(NULL),
21  scaler(NULL),
22  sig1(NULL),
23  sig2(NULL)
24 {
25  TTUInt16 initialMaxNumChannels = arguments;
26 
27  registerAttribute(TT("attack"), kTypeFloat64, &attrAttack, (TTSetterMethod)&TTPulseSub::setAttack);
28  registerAttribute(TT("decay"), kTypeFloat64, &attrDecay, (TTSetterMethod)&TTPulseSub::setDecay);
29  registerAttribute(TT("release"), kTypeFloat64, &attrRelease, (TTSetterMethod)&TTPulseSub::setRelease);
30  registerAttribute(TT("sustain"), kTypeFloat64, &attrSustain, (TTSetterMethod)&TTPulseSub::setSustain);
31  registerAttribute(TT("trigger"), kTypeBoolean, &attrTrigger, (TTSetterMethod)&TTPulseSub::setTrigger);
32  registerAttribute(TT("mode"), kTypeSymbol, &attrMode, (TTSetterMethod)&TTPulseSub::setMode);
33  registerAttribute(TT("frequency"), kTypeFloat64, &attrFrequency, (TTSetterMethod)&TTPulseSub::setFrequency);
34  registerAttribute(TT("length"), kTypeFloat64, &attrLength, (TTSetterMethod)&TTPulseSub::setLength);
35 
36  // register for notifications
37  addUpdates(MaxNumChannels);
38  addUpdates(SampleRate);
39 
40  TTObjectBaseInstantiate(kTTSym_adsr, &env_gen, initialMaxNumChannels);
41  TTObjectBaseInstantiate(kTTSym_phasor, &phasor, initialMaxNumChannels);
42  TTObjectBaseInstantiate(kTTSym_operator, &offset, initialMaxNumChannels);
43  TTObjectBaseInstantiate(kTTSym_operator, &scaler, initialMaxNumChannels);
44  TTObjectBaseInstantiate(kTTSym_audiosignal, &sig1, 1);
45  TTObjectBaseInstantiate(kTTSym_audiosignal, &sig2, 1);
46  offset->setAttributeValue(TT("operator"), TT("+"));
47  scaler->setAttributeValue(TT("operator"), TT("*"));
48 
49  setAttributeValue(TT("attack"), 50.);
50  setAttributeValue(TT("decay"), 100.);
51  setAttributeValue(TT("sustain"), -6.);
52  setAttributeValue(TT("release"), 500.);
53  setAttributeValue(TT("mode"), TT("linear")); // <-- sets the process method
54  setAttributeValue(TT("length"), 0.25);
55 
56  setProcessMethod(processAudio);
57 }
58 
59 TTPulseSub::~TTPulseSub()
60 {
65  TTObjectBaseRelease(&sig1);
66  TTObjectBaseRelease(&sig2);
67 }
68 
69 
70 TTErr TTPulseSub::updateSampleRate(const TTValue& oldSampleRate, TTValue&)
71 {
72  phasor->setAttributeValue(kTTSym_sampleRate, sr);
73  offset->setAttributeValue(kTTSym_sampleRate, sr);
74  env_gen->setAttributeValue(kTTSym_sampleRate, sr);
75  scaler->setAttributeValue(kTTSym_sampleRate, sr);
76  return kTTErrNone;
77 }
78 
79 
80 TTErr TTPulseSub::updateMaxNumChannels(const TTValue& oldMaxNumChannels, TTValue&)
81 {
82  phasor->setAttributeValue(kTTSym_maxNumChannels, mMaxNumChannels);
83  offset->setAttributeValue(kTTSym_maxNumChannels, mMaxNumChannels);
84  env_gen->setAttributeValue(kTTSym_maxNumChannels, mMaxNumChannels);
85  scaler->setAttributeValue(kTTSym_maxNumChannels, mMaxNumChannels);
86  return kTTErrNone;
87 }
88 
89 
90 TTErr TTPulseSub::setAttack(const TTValue& newValue)
91 {
92  attrAttack = newValue;
93  return env_gen->setAttributeValue(TT("attack"), const_cast<TTValue&>(newValue));
94 }
95 
96 TTErr TTPulseSub::setDecay(const TTValue& newValue)
97 {
98  attrDecay = newValue;
99  return env_gen->setAttributeValue(TT("decay"), attrDecay);
100 }
101 
102 
103 TTErr TTPulseSub::setSustain(const TTValue& newValue)
104 {
105  attrSustain = newValue;
106  return env_gen->setAttributeValue(TT("sustain"), const_cast<TTValue&>(newValue));
107 }
108 
109 TTErr TTPulseSub::setRelease(const TTValue& newValue)
110 {
111  attrRelease = newValue;
112  return env_gen->setAttributeValue(TT("release"), const_cast<TTValue&>(newValue));
113 }
114 
115 
116 TTErr TTPulseSub::setMode(const TTValue& newValue)
117 {
118  attrMode = newValue;
119  return env_gen->setAttributeValue(TT("mode"), const_cast<TTValue&>(newValue));
120 }
121 
122 TTErr TTPulseSub::setTrigger(const TTValue& newValue)
123 {
124  attrTrigger = newValue;
125  return env_gen->setAttributeValue(TT("trigger"), const_cast<TTValue&>(newValue));
126 }
127 
128 
129 TTErr TTPulseSub::setFrequency(const TTValue& newValue)
130 {
131  attrFrequency = newValue;
132  return phasor->setAttributeValue(TT("frequency"), attrFrequency);
133 }
134 
135 TTErr TTPulseSub::setLength(const TTValue& newValue)
136 {
137  attrLength = newValue;
138  return offset->setAttributeValue(TT("operand"), attrLength - 0.5);
139 }
140 
141 
142 TTErr TTPulseSub::processAudio(TTAudioSignalArrayPtr inputs, TTAudioSignalArrayPtr outputs)
143 {
144  TTAudioSignal& in = inputs->getSignal(0);
145  TTAudioSignal& out = outputs->getSignal(0);
146  TTUInt16 vs = in.getVectorSizeAsInt();
147 
148  sig1->allocWithVectorSize(vs);
149  sig2->allocWithVectorSize(vs);
150 
151  phasor->process(*sig1); // ramp wave, stored in a temporary vector
152  offset->process(*sig1, *sig2); // offset the ramp wave, effectively altering the duty cycle
153  env_gen->process(*sig2, *sig1); // generate the envelope, reusing the temp[0] vector
154  scaler->process(in, *sig1, out); // apply the envelope to the input vector
155 
156  return kTTErrNone;
157 }
TTErr(TTObjectBase::* TTSetterMethod)(const TTAttribute &attribute, const TTValue &value)
A type that can be used to store a pointer to a message for an object.
Definition: TTObjectBase.h:78
std::uint16_t TTUInt16
16 bit unsigned integer
Definition: TTBase.h:176
TTErr TTObjectBaseRelease(TTObjectBasePtr *anObject)
DEPRECATED.
TTChannelCount mMaxNumChannels
This is the maximum number of channels that can be guaranteed to work.
#define setProcessMethod(methodName)
A convenience macro to be used by subclasses for setting the process method.
TTErr setAttributeValue(const TTSymbol name, TTValue &value)
Set an attribute value for an object.
Symbol type.
Definition: TTBase.h:282
#define TT
This macro is defined as a shortcut for doing a lookup in the symbol table.
Definition: TTSymbol.h:155
64-bit floating point
Definition: TTBase.h:272
TTAudioObjectBasePtr phasor
TTPhasor.
Definition: TTPulseSub.h:30
TTErr TTObjectBaseInstantiate(const TTSymbol className, TTObjectBasePtr *returnedObjectPtr, const TTValue arguments)
DEPRECATED.
Boolean (1/0) or (true/false) flag.
Definition: TTBase.h:281
The TTAudioSignal class represents N vectors of audio samples for M channels.
Definition: TTAudioSignal.h:57
TTAudioObjectBasePtr scaler
TTOperator.
Definition: TTPulseSub.h:32
TTErr process(TTAudioSignal &in, TTAudioSignal &out)
Process the input signal, resulting in an output signal.
TTAudioObjectBasePtr env_gen
TTAdsr.
Definition: TTPulseSub.h:29
A simple container for an array of TTAudioSignal pointers.
TTErr
Jamoma Error Codes Enumeration of error codes that might be returned by any of the TTBlue functions a...
Definition: TTBase.h:342
TTAudioObjectBasePtr offset
TTOperator.
Definition: TTPulseSub.h:31
No Error.
Definition: TTBase.h:343
TTErr allocWithVectorSize(const TTUInt16 newVectorSize)
Allocate memory for all channels at the specified vectorsize, if the vectorsize is different from the...
[doxygenAppendixC_copyExample]
Definition: TTValue.h:34
#define addUpdates(updateName)
An 'update' is a message sent to a subclass instance from its parent class.
Definition: TTMessage.h:44
TTUInt32 sr
Current sample rate being used by this object.