41 #include "SynthElement.h"
42 #include "AUInstrumentBase.h"
45 MidiControls::MidiControls()
50 void MidiControls::Reset()
52 memset(
this, 0,
sizeof(MidiControls));
53 mControls[kMidiController_Pan] = 64;
54 mControls[kMidiController_Expression] = 127;
55 mPitchBendDepth = 2 << 7;
56 mFPitchBendDepth = 2.;
60 SynthElement::SynthElement(AUInstrumentBase *audioUnit, UInt32 inElement)
61 : AUElement(audioUnit), mName(0), mIndex(inElement)
65 SynthElement::~SynthElement()
67 if (mName) CFRelease(mName);
70 SynthGroupElement::SynthGroupElement(AUInstrumentBase *audioUnit, UInt32 inElement)
71 : SynthElement(audioUnit, inElement), mSustainIsOn(false), mSostenutoIsOn(false), mOutputBus(0), mGroupID(kUnassignedGroup)
74 printf(
"SynthGroupElement::SynthGroupElement %d\n", inElement);
76 for (UInt32 i=0; i<kNumberOfSoundingNoteStates; ++i)
77 mNoteList[i].mState = i;
80 void SynthGroupElement::SetGroupID (MusicDeviceGroupID inGroup)
83 if (mGroupID != kUnassignedGroup)
throw static_cast<OSStatus
>(kAudioUnitErr_InvalidElement);
87 void SynthGroupElement::Reset()
90 printf(
"SynthGroupElement::Reset\n");
92 mMidiControls.Reset();
93 for (UInt32 i=0; i<kNumberOfSoundingNoteStates; ++i)
97 SynthPartElement::SynthPartElement(AUInstrumentBase *audioUnit, UInt32 inElement)
98 : SynthElement(audioUnit, inElement)
102 void SynthGroupElement::NoteOff(NoteInstanceID inNoteID, UInt32 inFrame)
105 printf(
"SynthGroupElement::NoteOff %d\n", inNoteID);
107 SynthNote *note = mNoteList[kNoteState_Attacked].mHead;
109 while (note && note->mNoteID != inNoteID)
112 printf(
" ? %08X %d\n", note, note->mNoteID);
118 printf(
" found %08X\n", note);
123 printf(
" old state %d\n", note->mState);
125 mNoteList[kNoteState_Attacked].RemoveNote(note);
126 note->Release(inFrame);
128 mNoteList[kNoteState_ReleasedButSustained].AddNote(note);
130 mNoteList[kNoteState_Released].AddNote(note);
133 printf(
" new state %d\n", note->mState);
136 else if (mSostenutoIsOn)
139 note = mNoteList[kNoteState_Sostenutoed].mHead;
140 while (note && note->mNoteID != inNoteID)
144 mNoteList[kNoteState_Sostenutoed].RemoveNote(note);
145 mNoteList[kNoteState_ReleasedButSostenutoed].AddNote(note);
150 void SynthGroupElement::NoteEnded(SynthNote *inNote, UInt32 inFrame)
153 printf(
"SynthGroupElement::NoteEnded %d %d\n", inNote->mNoteID, inNote->mState);
155 SynthNoteList *list = mNoteList + inNote->mState;
156 list->RemoveNote(inNote);
158 GetAUInstrument()->AddFreeNote(inNote);
161 void SynthGroupElement::SostenutoOn(UInt32 inFrame)
164 printf(
"SynthGroupElement::SostenutoOn\n");
166 mSostenutoIsOn =
true;
167 mNoteList[kNoteState_Sostenutoed].TransferAllFrom(&mNoteList[kNoteState_Attacked], inFrame);
170 void SynthGroupElement::SostenutoOff(UInt32 inFrame)
173 printf(
"SynthGroupElement::SostenutoOff\n");
175 mSostenutoIsOn =
false;
176 mNoteList[kNoteState_Attacked].TransferAllFrom(&mNoteList[kNoteState_Sostenutoed], inFrame);
178 mNoteList[kNoteState_ReleasedButSustained].TransferAllFrom(&mNoteList[kNoteState_ReleasedButSostenutoed], inFrame);
180 mNoteList[kNoteState_Released].TransferAllFrom(&mNoteList[kNoteState_ReleasedButSostenutoed], inFrame);
184 void SynthGroupElement::SustainOn(UInt32 inFrame)
187 printf(
"SynthGroupElement::SustainOn\n");
192 void SynthGroupElement::SustainOff(UInt32 inFrame)
195 printf(
"SynthGroupElement::SustainOff\n");
197 mSustainIsOn =
false;
199 mNoteList[kNoteState_Released].TransferAllFrom(&mNoteList[kNoteState_ReleasedButSustained], inFrame);
202 void SynthGroupElement::AllNotesOff(UInt32 inFrame)
205 printf(
"SynthGroupElement::AllNotesOff\n");
209 for (UInt32 i=0 ; i<kNumberOfActiveNoteStates; ++i)
211 note = mNoteList[i].mHead;
214 SynthNote *nextNote = note->mNext;
216 mNoteList[i].RemoveNote(note);
217 note->FastRelease(inFrame);
218 mNoteList[kNoteState_FastReleased].AddNote(note);
225 void SynthGroupElement::AllSoundOff(UInt32 inFrame)
228 printf(
"SynthGroupElement::AllSoundOff\n");
230 AllNotesOff(inFrame);
233 void SynthGroupElement::ResetAllControllers(UInt32 inFrame)
236 printf(
"SynthGroupElement::ResetAllControllers\n");
238 mMidiControls.Reset();
241 OSStatus SynthGroupElement::Render(UInt32 inNumberFrames)
244 AudioBufferList& bufferList = GetAudioUnit()->GetOutput(mOutputBus)->GetBufferList();
246 for (UInt32 i=0 ; i<kNumberOfSoundingNoteStates; ++i)
248 note = mNoteList[i].mHead;
252 printf(
" note %d %08X %d\n", i, note, inNumberFrames);
254 SynthNote *nextNote = note->mNext;
256 OSStatus err = note->Render(inNumberFrames, bufferList);