Jamoma API  0.6.0.a19
SynthElement.cpp
1 /* Copyright 2007 Apple Inc. All Rights Reserved.
2 
3  Disclaimer: IMPORTANT: This Apple software is supplied to you by
4  Apple Inc. ("Apple") in consideration of your agreement to the
5  following terms, and your use, installation, modification or
6  redistribution of this Apple software constitutes acceptance of these
7  terms. If you do not agree with these terms, please do not use,
8  install, modify or redistribute this Apple software.
9 
10  In consideration of your agreement to abide by the following terms, and
11  subject to these terms, Apple grants you a personal, non-exclusive
12  license, under Apple's copyrights in this original Apple software (the
13  "Apple Software"), to use, reproduce, modify and redistribute the Apple
14  Software, with or without modifications, in source and/or binary forms;
15  provided that if you redistribute the Apple Software in its entirety and
16  without modifications, you must retain this notice and the following
17  text and disclaimers in all such redistributions of the Apple Software.
18  Neither the name, trademarks, service marks or logos of Apple Inc.
19  may be used to endorse or promote products derived from the Apple
20  Software without specific prior written permission from Apple. Except
21  as expressly stated in this notice, no other rights or licenses, express
22  or implied, are granted by Apple herein, including but not limited to
23  any patent rights that may be infringed by your derivative works or by
24  other works in which the Apple Software may be incorporated.
25 
26  The Apple Software is provided by Apple on an "AS IS" basis. APPLE
27  MAKES NO WARRANTIES, EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION
28  THE IMPLIED WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS
29  FOR A PARTICULAR PURPOSE, REGARDING THE APPLE SOFTWARE OR ITS USE AND
30  OPERATION ALONE OR IN COMBINATION WITH YOUR PRODUCTS.
31 
32  IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL
33  OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
34  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
35  INTERRUPTION) ARISING IN ANY WAY OUT OF THE USE, REPRODUCTION,
36  MODIFICATION AND/OR DISTRIBUTION OF THE APPLE SOFTWARE, HOWEVER CAUSED
37  AND WHETHER UNDER THEORY OF CONTRACT, TORT (INCLUDING NEGLIGENCE),
38  STRICT LIABILITY OR OTHERWISE, EVEN IF APPLE HAS BEEN ADVISED OF THE
39  POSSIBILITY OF SUCH DAMAGE.
40 */
41 #include "SynthElement.h"
42 #include "AUInstrumentBase.h"
43 
44 ////////////////////////////////////////////////////////////////////////////////////////////////////////////
45 MidiControls::MidiControls()
46 {
47  Reset();
48 }
49 
50 void MidiControls::Reset()
51 {
52  memset(this, 0, sizeof(MidiControls));
53  mControls[kMidiController_Pan] = 64;
54  mControls[kMidiController_Expression] = 127;
55  mPitchBendDepth = 2 << 7;
56  mFPitchBendDepth = 2.;
57 }
58 
59 
60 SynthElement::SynthElement(AUInstrumentBase *audioUnit, UInt32 inElement)
61  : AUElement(audioUnit), mName(0), mIndex(inElement)
62 {
63 }
64 
65 SynthElement::~SynthElement()
66 {
67  if (mName) CFRelease(mName);
68 }
69 
70 SynthGroupElement::SynthGroupElement(AUInstrumentBase *audioUnit, UInt32 inElement)
71  : SynthElement(audioUnit, inElement), mSustainIsOn(false), mSostenutoIsOn(false), mOutputBus(0), mGroupID(kUnassignedGroup)
72 {
73 #if DEBUG_PRINT
74  printf("SynthGroupElement::SynthGroupElement %d\n", inElement);
75 #endif
76  for (UInt32 i=0; i<kNumberOfSoundingNoteStates; ++i)
77  mNoteList[i].mState = i;
78 }
79 
80 void SynthGroupElement::SetGroupID (MusicDeviceGroupID inGroup)
81 {
82  // can't re-assign a group once its been assigned
83  if (mGroupID != kUnassignedGroup) throw static_cast<OSStatus>(kAudioUnitErr_InvalidElement);
84  mGroupID = inGroup;
85 }
86 
87 void SynthGroupElement::Reset()
88 {
89 #if DEBUG_PRINT
90  printf("SynthGroupElement::Reset\n");
91 #endif
92  mMidiControls.Reset();
93  for (UInt32 i=0; i<kNumberOfSoundingNoteStates; ++i)
94  mNoteList[i].Empty();
95 }
96 
97 SynthPartElement::SynthPartElement(AUInstrumentBase *audioUnit, UInt32 inElement)
98  : SynthElement(audioUnit, inElement)
99 {
100 }
101 
102 void SynthGroupElement::NoteOff(NoteInstanceID inNoteID, UInt32 inFrame)
103 {
104 #if DEBUG_PRINT
105  printf("SynthGroupElement::NoteOff %d\n", inNoteID);
106 #endif
107  SynthNote *note = mNoteList[kNoteState_Attacked].mHead;
108  // see if this note is attacked.
109  while (note && note->mNoteID != inNoteID)
110  {
111 #if DEBUG_PRINT
112  printf(" ? %08X %d\n", note, note->mNoteID);
113 #endif
114  note = note->mNext;
115  }
116 
117 #if DEBUG_PRINT
118  printf(" found %08X\n", note);
119 #endif
120  if (note)
121  {
122 #if DEBUG_PRINT
123  printf(" old state %d\n", note->mState);
124 #endif
125  mNoteList[kNoteState_Attacked].RemoveNote(note);
126  note->Release(inFrame);
127  if (mSustainIsOn) {
128  mNoteList[kNoteState_ReleasedButSustained].AddNote(note);
129  } else {
130  mNoteList[kNoteState_Released].AddNote(note);
131  }
132 #if DEBUG_PRINT
133  printf(" new state %d\n", note->mState);
134 #endif
135  }
136  else if (mSostenutoIsOn)
137  {
138  // see if this note is sostenutoed.
139  note = mNoteList[kNoteState_Sostenutoed].mHead;
140  while (note && note->mNoteID != inNoteID)
141  note = note->mNext;
142  if (note)
143  {
144  mNoteList[kNoteState_Sostenutoed].RemoveNote(note);
145  mNoteList[kNoteState_ReleasedButSostenutoed].AddNote(note);
146  }
147  }
148 }
149 
150 void SynthGroupElement::NoteEnded(SynthNote *inNote, UInt32 inFrame)
151 {
152 #if DEBUG_PRINT
153  printf("SynthGroupElement::NoteEnded %d %d\n", inNote->mNoteID, inNote->mState);
154 #endif
155  SynthNoteList *list = mNoteList + inNote->mState;
156  list->RemoveNote(inNote);
157 
158  GetAUInstrument()->AddFreeNote(inNote);
159 }
160 
161 void SynthGroupElement::SostenutoOn(UInt32 inFrame)
162 {
163 #if DEBUG_PRINT
164  printf("SynthGroupElement::SostenutoOn\n");
165 #endif
166  mSostenutoIsOn = true;
167  mNoteList[kNoteState_Sostenutoed].TransferAllFrom(&mNoteList[kNoteState_Attacked], inFrame);
168 }
169 
170 void SynthGroupElement::SostenutoOff(UInt32 inFrame)
171 {
172 #if DEBUG_PRINT
173  printf("SynthGroupElement::SostenutoOff\n");
174 #endif
175  mSostenutoIsOn = false;
176  mNoteList[kNoteState_Attacked].TransferAllFrom(&mNoteList[kNoteState_Sostenutoed], inFrame);
177  if (mSustainIsOn)
178  mNoteList[kNoteState_ReleasedButSustained].TransferAllFrom(&mNoteList[kNoteState_ReleasedButSostenutoed], inFrame);
179  else
180  mNoteList[kNoteState_Released].TransferAllFrom(&mNoteList[kNoteState_ReleasedButSostenutoed], inFrame);
181 }
182 
183 
184 void SynthGroupElement::SustainOn(UInt32 inFrame)
185 {
186 #if DEBUG_PRINT
187  printf("SynthGroupElement::SustainOn\n");
188 #endif
189  mSustainIsOn = true;
190 }
191 
192 void SynthGroupElement::SustainOff(UInt32 inFrame)
193 {
194 #if DEBUG_PRINT
195  printf("SynthGroupElement::SustainOff\n");
196 #endif
197  mSustainIsOn = false;
198 
199  mNoteList[kNoteState_Released].TransferAllFrom(&mNoteList[kNoteState_ReleasedButSustained], inFrame);
200 }
201 
202 void SynthGroupElement::AllNotesOff(UInt32 inFrame)
203 {
204 #if DEBUG_PRINT
205  printf("SynthGroupElement::AllNotesOff\n");
206 #endif
207  SynthNote *note;
208 
209  for (UInt32 i=0 ; i<kNumberOfActiveNoteStates; ++i)
210  {
211  note = mNoteList[i].mHead;
212  while (note)
213  {
214  SynthNote *nextNote = note->mNext;
215 
216  mNoteList[i].RemoveNote(note);
217  note->FastRelease(inFrame);
218  mNoteList[kNoteState_FastReleased].AddNote(note);
219 
220  note = nextNote;
221  }
222  }
223 }
224 
225 void SynthGroupElement::AllSoundOff(UInt32 inFrame)
226 {
227 #if DEBUG_PRINT
228  printf("SynthGroupElement::AllSoundOff\n");
229 #endif
230  AllNotesOff(inFrame);
231 }
232 
233 void SynthGroupElement::ResetAllControllers(UInt32 inFrame)
234 {
235 #if DEBUG_PRINT
236  printf("SynthGroupElement::ResetAllControllers\n");
237 #endif
238  mMidiControls.Reset();
239 }
240 
241 OSStatus SynthGroupElement::Render(UInt32 inNumberFrames)
242 {
243  SynthNote *note;
244  AudioBufferList& bufferList = GetAudioUnit()->GetOutput(mOutputBus)->GetBufferList();
245 
246  for (UInt32 i=0 ; i<kNumberOfSoundingNoteStates; ++i)
247  {
248  note = mNoteList[i].mHead;
249  while (note)
250  {
251 #if DEBUG_PRINT
252  printf(" note %d %08X %d\n", i, note, inNumberFrames);
253 #endif
254  SynthNote *nextNote = note->mNext;
255 
256  OSStatus err = note->Render(inNumberFrames, bufferList);
257  if (err) return err;
258 
259  note = nextNote;
260  }
261  }
262 
263  return noErr;
264 }
265 
266