Jamoma API  0.6.0.a19
AUInstrumentBase.h
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 #ifndef __AUInstrumentBase__
42 #define __AUInstrumentBase__
43 
44 #include <vector>
45 #include <stdexcept>
46 #include <AudioUnit/AudioUnit.h>
47 #include <CoreAudio/CoreAudio.h>
48 #include "MusicDeviceBase.h"
49 #include "LockFreeFIFO.h"
50 #include "SynthEvent.h"
51 #include "SynthNote.h"
52 #include "SynthElement.h"
53 
54 ////////////////////////////////////////////////////////////////////////////////////////////////////////////
55 
56 typedef LockFreeFIFOWithFree<SynthEvent> SynthEventQueue;
57 
58 class AUInstrumentBase : public MusicDeviceBase
59 {
60 public:
61  AUInstrumentBase(
62  ComponentInstance inInstance,
63  UInt32 numInputs,
64  UInt32 numOutputs,
65  UInt32 numGroups = 32,
66  UInt32 numParts = 0);
67  virtual ~AUInstrumentBase();
68 
69  virtual OSStatus Initialize();
70 
71  virtual void Cleanup();
72 
73  virtual AUElement* CreateElement( AudioUnitScope scope,
74  AudioUnitElement element);
75 
76  virtual OSStatus Reset( AudioUnitScope inScope,
77  AudioUnitElement inElement);
78 
79  virtual bool ValidFormat( AudioUnitScope inScope,
80  AudioUnitElement inElement,
81  const CAStreamBasicDescription & inNewFormat);
82 
83  virtual bool StreamFormatWritable( AudioUnitScope scope,
84  AudioUnitElement element);
85 
86  virtual OSStatus Render( AudioUnitRenderActionFlags & ioActionFlags,
87  const AudioTimeStamp & inTimeStamp,
88  UInt32 inNumberFrames);
89 
90  virtual OSStatus StartNote( MusicDeviceInstrumentID inInstrument,
91  MusicDeviceGroupID inGroupID,
92  NoteInstanceID * outNoteInstanceID,
93  UInt32 inOffsetSampleFrame,
94  const MusicDeviceNoteParams &inParams);
95 
96  virtual OSStatus StopNote( MusicDeviceGroupID inGroupID,
97  NoteInstanceID inNoteInstanceID,
98  UInt32 inOffsetSampleFrame);
99 
100  virtual OSStatus RealTimeStartNote( SynthGroupElement *inGroup,
101  NoteInstanceID inNoteInstanceID,
102  UInt32 inOffsetSampleFrame,
103  const MusicDeviceNoteParams &inParams);
104 
105  virtual OSStatus RealTimeStopNote( MusicDeviceGroupID inGroupID,
106  NoteInstanceID inNoteInstanceID,
107  UInt32 inOffsetSampleFrame);
108 
109  virtual OSStatus HandleControlChange( UInt8 inChannel,
110  UInt8 inController,
111  UInt8 inValue,
112  UInt32 inStartFrame);
113 
114  virtual OSStatus HandlePitchWheel( UInt8 inChannel,
115  UInt8 inPitch1,
116  UInt8 inPitch2,
117  UInt32 inStartFrame);
118 
119  virtual OSStatus HandleChannelPressure( UInt8 inChannel,
120  UInt8 inValue,
121  UInt32 inStartFrame);
122 
123  virtual OSStatus HandleProgramChange( UInt8 inChannel,
124  UInt8 inValue);
125 
126  virtual OSStatus HandlePolyPressure( UInt8 inChannel,
127  UInt8 inKey,
128  UInt8 inValue,
129  UInt32 inStartFrame);
130 
131  virtual OSStatus HandleResetAllControllers( UInt8 inChannel);
132 
133  virtual OSStatus HandleAllNotesOff( UInt8 inChannel);
134 
135  virtual OSStatus HandleAllSoundOff( UInt8 inChannel);
136 
137  SynthNote* GetNote(UInt32 inIndex)
138  {
139  if (!mNotes) throw std::runtime_error("no notes");
140  return (SynthNote*)((char*)mNotes + inIndex * mNoteSize);
141  }
142 
143  SynthNote* GetAFreeNote(UInt32 inFrame);
144  void AddFreeNote(SynthNote* inNote);
145 
146  MidiControls* GetControls( MusicDeviceGroupID inChannel)
147  {
148  SynthGroupElement *group = GetElForGroupID(inChannel);
149  return &(group->mMidiControls);
150  }
151 
152 
153 protected:
154 
155  UInt32 NextNoteID() { return IncrementAtomic(&mNoteIDCounter); }
156 
157 
158  // call SetNotes in your Initialize() method to give the base class your note structures and to set the maximum
159  // number of active notes. inNoteData should be an array of size inMaxActiveNotes.
160  void SetNotes(UInt32 inNumNotes, UInt32 inMaxActiveNotes, SynthNote* inNotes, UInt32 inNoteSize);
161 
162  void PerformEvents( const AudioTimeStamp & inTimeStamp);
163  OSStatus SendPedalEvent(MusicDeviceGroupID inGroupID, UInt32 inEventType, UInt32 inOffsetSampleFrame);
164  virtual SynthNote* VoiceStealing(UInt32 inFrame, bool inKillIt);
165  UInt32 MaxActiveNotes() const { return mMaxActiveNotes; }
166  UInt32 NumActiveNotes() const { return mNumActiveNotes; }
167  void IncNumActiveNotes() { mNumActiveNotes ++; }
168  void DecNumActiveNotes() { mNumActiveNotes --; }
169  UInt32 CountActiveNotes();
170  // this call throws if there's no assigned element for the group ID
171  SynthGroupElement * GetElForGroupID (MusicDeviceGroupID inGroupID);
172  SynthGroupElement * GetElForNoteID (NoteInstanceID inNoteID);
173 
174  SInt64 mAbsoluteSampleFrame;
175 
176 
177 private:
178 
179  SInt32 mNoteIDCounter;
180 
181  SynthEventQueue mEventQueue;
182 
183  UInt32 mNumNotes;
184  UInt32 mNumActiveNotes;
185  UInt32 mMaxActiveNotes;
186  SynthNote* mNotes;
187  SynthNoteList mFreeNotes;
188  UInt32 mNoteSize;
189 };
190 
191 ////////////////////////////////////////////////////////////////////////////////////////////////////////////
192 
193 class AUMonotimbralInstrumentBase : public AUInstrumentBase
194 {
195 public:
196  AUMonotimbralInstrumentBase(
197  ComponentInstance inInstance,
198  UInt32 numInputs,
199  UInt32 numOutputs,
200  UInt32 numGroups = 32,
201  UInt32 numParts = 0);
202 
203  virtual OSStatus RealTimeStartNote( SynthGroupElement *inGroup,
204  NoteInstanceID inNoteInstanceID,
205  UInt32 inOffsetSampleFrame,
206  const MusicDeviceNoteParams &inParams);
207 };
208 
209 ////////////////////////////////////////////////////////////////////////////////////////////////////////////
210 
211 // this is a work in progress! The mono-timbral one is finished though!
212 class AUMultitimbralInstrumentBase : public AUInstrumentBase
213 {
214 public:
215  AUMultitimbralInstrumentBase(
216  ComponentInstance inInstance,
217  UInt32 numInputs,
218  UInt32 numOutputs,
219  UInt32 numGroups,
220  UInt32 numParts);
221 
222  virtual OSStatus GetPropertyInfo( AudioUnitPropertyID inID,
223  AudioUnitScope inScope,
224  AudioUnitElement inElement,
225  UInt32 & outDataSize,
226  Boolean & outWritable);
227 
228  virtual OSStatus GetProperty( AudioUnitPropertyID inID,
229  AudioUnitScope inScope,
230  AudioUnitElement inElement,
231  void * outData);
232 
233  virtual OSStatus SetProperty( AudioUnitPropertyID inID,
234  AudioUnitScope inScope,
235  AudioUnitElement inElement,
236  const void * inData,
237  UInt32 inDataSize);
238 
239 private:
240 
241 };
242 
243 ////////////////////////////////////////////////////////////////////////////////////////////////////////////
244 
245 #endif
246