Jamoma API  0.6.0.a19
PlugtasticAUInstrument.cpp
1 /*=============================================================================
2  PlugtasticAUInstrument.cpp
3 
4  =============================================================================*/
5 
6 /*
7  This is an example implementation of a sin wave synth using AUInstrumentBase classes
8 
9  It illustrates a basic usage of these classes
10 
11  It artificially limits the number of notes at one time to 12, so the note-stealing
12  algorithm is used - you should know how this works!
13 
14  Most of the work you need to do is defining a Note class (see TestNote). AUInstrument manages the
15  creation and destruction of notes, the various stages of a note's lifetime.
16 
17  The project also defines CA_AUTO_MIDI_MAP (OTHER_C_FLAGS). This adds all the code that is needed
18  to map MIDI messages to specific parameter changes. This can be seen in AU Lab's MIDI Editor window
19  CA_AUTO_MIDI_MAP is implemented in AUMIDIBase.cpp/.h
20 */
21 
22 
23 #include "PlugtasticAUInstrument.h"
24 
25 COMPONENT_ENTRY(PlugtasticAUInstrument)
26 
27 #pragma mark PlugtasticAUInstrument Methods
28 
29 //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
30 // PlugtasticAUInstrument::PlugtasticAUInstrument
31 //
32 // This synth has No inputs, One output
33 //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
34 PlugtasticAUInstrument::PlugtasticAUInstrument(ComponentInstance inComponentInstance)
35 // : AUMonotimbralInstrumentBase(inComponentInstance, 0, 1)
36  : AUInstrumentBase(inComponentInstance, 0, 1)
37 {
38  CreateElements();
39  Globals()->UseIndexedParameters (kNumberOfParameters); // we're only defining one param
40 // Globals()->SetParameter (kGlobalVolumeParam, 1.0);
41 
42 #if AU_DEBUG_DISPATCHER
43  mDebugDispatcher = new AUDebugDispatcher (this);
44 #endif
45 
46  mGraph = new PlugtasticAUInstrumentGraph;
47  mParameters = new PlugtasticAUParameters;
48  mParameters->setDefaults(this);
49 }
50 
51 /*
52 //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
53 // PlugtasticAUInstrument::Initialize
54 //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
55 OSStatus PlugtasticAUInstrument::Initialize()
56 {
57  AUMonotimbralInstrumentBase::Initialize();
58  SetNotes(kNumNotes, kMaxActiveNotes, mTestNotes, sizeof(TestNote));
59 
60  return noErr;
61 }
62 */
63 
64 //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
65 // PlugtasticAUInstrument::GetParameterInfo
66 //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
67 OSStatus PlugtasticAUInstrument::GetParameterInfo(AudioUnitScope inScope, AudioUnitParameterID inParameterID, AudioUnitParameterInfo &outParameterInfo)
68 {
69  return mParameters->getInfo(inScope, inParameterID, outParameterInfo);
70 }
71 
72 
73 
74 
75 ComponentResult PlugtasticAUInstrument::SetParameter(AudioUnitParameterID inID,
76  AudioUnitScope inScope,
77  AudioUnitElement inElement,
78  Float32 inValue,
79  UInt32 inBufferOffsetInFrames)
80 {
81  mParameters->setParameter(mGraph, inID,inValue);
82  return AUBase::SetParameter(inID, inScope, inElement, inValue, inBufferOffsetInFrames);
83 }
84 
85 
86 //void PlugtasticAUInstrument::SetParameter(AudioUnitParameterID inID, Float32 inValue)
87 //{
88 // mParameters->setParameter(mGraph, inID, inValue);
89 // AUInstrumentBase::SetParameter(inID, inValue);
90 //}
91 
92 
93 
94 
95 
96 #pragma mark TestNote Methods
97 
98 //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
99 // PlugtasticAUInstrument::Render
100 //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
101 OSStatus PlugtasticAUInstrument::Render(UInt32 inNumFrames, AudioBufferList& inBufferList)
102 {
103  float *left, *right;
104 /* ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~
105  Changes to this parameter (kGlobalVolumeParam) are not being de-zippered;
106  Left as an exercise for the reader
107  ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ */
108  float globalVol = GetGlobalParameter(kGlobalVolumeParam);
109 
110  int numChans = inBufferList.mNumberBuffers;
111  if (numChans > 2)
112  return -1;
113 
114  left = (float*)inBufferList.mBuffers[0].mData;
115  right = numChans == 2 ? (float*)inBufferList.mBuffers[1].mData : 0;
116 
117  double sampleRate = SampleRate();
118  double freq = Frequency() * (twopi/sampleRate);
119 
120  switch (GetState()) {
121  case kNoteState_Attacked :
122  case kNoteState_Sostenutoed :
123  case kNoteState_ReleasedButSostenutoed :
124  case kNoteState_ReleasedButSustained : {
125  for (UInt32 frame=0; frame<inNumFrames; ++frame) {
126  if (amp < maxamp)
127  amp += up_slope;
128  float out = pow5(sin(phase)) * amp * globalVol;
129  phase += freq;
130  if (phase > twopi)
131  phase -= twopi;
132  left[frame] += out;
133  if (right)
134  right[frame] += out;
135  }
136  }
137  break;
138 
139  case kNoteState_Released :
140  {
141  UInt32 endFrame = 0xFFFFFFFF;
142  for (UInt32 frame=0; frame<inNumFrames; ++frame)
143  {
144  if (amp > 0.0)
145  amp += dn_slope;
146  else if (endFrame == 0xFFFFFFFF)
147  endFrame = frame;
148  float out = pow5(sin(phase)) * amp * globalVol;
149  phase += freq;
150  left[frame] += out;
151  if (right)
152  right[frame] += out;
153  }
154  if (endFrame != 0xFFFFFFFF)
155  NoteEnded(endFrame);
156  }
157  break;
158 
159  case kNoteState_FastReleased :
160  {
161  UInt32 endFrame = 0xFFFFFFFF;
162  for (UInt32 frame=0; frame<inNumFrames; ++frame)
163  {
164  if (amp > 0.0)
165  amp += fast_dn_slope;
166  else if (endFrame == 0xFFFFFFFF)
167  endFrame = frame;
168  float out = pow5(sin(phase)) * amp * globalVol;
169  phase += freq;
170  left[frame] += out;
171  if (right)
172  right[frame] += out;
173  }
174  if (endFrame != 0xFFFFFFFF)
175  NoteEnded(endFrame);
176  }
177  break;
178  default :
179  break;
180  }
181 
182  return noErr;
183 }
184