Jamoma API  0.6.0.a19
AUEffectBase.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 /*=============================================================================
42  AUEffectBase.h
43 
44 =============================================================================*/
45 
46 #ifndef __AUEffectBase_h__
47 #define __AUEffectBase_h__
48 
49 #include "AUBase.h"
50 #include "AUSilentTimeout.h"
51 
52 class AUKernelBase;
53 
54 // Base class for an effect with one input stream, one output stream,
55 // any number of channels.
56  /*! @class AUEffectBase */
57 class AUEffectBase : public AUBase {
58 public:
59  /*! @ctor AUEffectBase */
60  AUEffectBase( AudioUnit audioUnit,
61  bool inProcessesInPlace = true );
62  /*! @dtor ~AUEffectBase */
63  ~AUEffectBase();
64 
65  /*! @method Initialize */
66  virtual ComponentResult Initialize();
67 
68  /*! @method Cleanup */
69  virtual void Cleanup();
70 
71 
72  /*! @method Reset */
73  virtual ComponentResult Reset( AudioUnitScope inScope,
74  AudioUnitElement inElement);
75 
76  /*! @method GetPropertyInfo */
77  virtual ComponentResult GetPropertyInfo (AudioUnitPropertyID inID,
78  AudioUnitScope inScope,
79  AudioUnitElement inElement,
80  UInt32 & outDataSize,
81  Boolean & outWritable);
82 
83  /*! @method GetProperty */
84  virtual ComponentResult GetProperty (AudioUnitPropertyID inID,
85  AudioUnitScope inScope,
86  AudioUnitElement inElement,
87  void * outData);
88 
89  /*! @method SetProperty */
90  virtual ComponentResult SetProperty(AudioUnitPropertyID inID,
91  AudioUnitScope inScope,
92  AudioUnitElement inElement,
93  const void * inData,
94  UInt32 inDataSize);
95 
96  /*! @method StreamFormatWritable */
97  virtual bool StreamFormatWritable (AudioUnitScope scope,
98  AudioUnitElement element);
99 
100  /*! @method ChangeStreamFormat */
101  virtual ComponentResult ChangeStreamFormat (
102  AudioUnitScope inScope,
103  AudioUnitElement inElement,
104  const CAStreamBasicDescription & inPrevFormat,
105  const CAStreamBasicDescription & inNewFormat);
106 
107  /*! @method Render */
108  virtual ComponentResult Render(AudioUnitRenderActionFlags & ioActionFlags,
109  const AudioTimeStamp & inTimeStamp,
110  UInt32 inNumberFrames);
111 
112  // our virtual methods
113 
114  // If your unit processes N to N channels, and there are no interactions between channels,
115  // it can override NewKernel to create a mono processing object per channel. Otherwise,
116  // don't override NewKernel, and instead, override ProcessBufferLists.
117  /*! @method NewKernel */
118  virtual AUKernelBase * NewKernel() { return NULL; }
119 
120  /*! @method ProcessBufferLists */
121  virtual OSStatus ProcessBufferLists(
122  AudioUnitRenderActionFlags & ioActionFlags,
123  const AudioBufferList & inBuffer,
124  AudioBufferList & outBuffer,
125  UInt32 inFramesToProcess );
126 
127  /*! @method MaintainKernels */
128  void MaintainKernels();
129 
130  // convenience format accessors (use output 0's format)
131  /*! @method GetSampleRate */
132  Float64 GetSampleRate();
133 
134  /*! @method GetNumberOfChannels */
135  UInt32 GetNumberOfChannels();
136 
137  // convenience wrappers for accessing parameters in the global scope
138  /*! @method SetParameter */
139  void SetParameter( AudioUnitParameterID paramID,
140  AudioUnitParameterValue value)
141  {
142  Globals()->SetParameter(paramID, value);
143  }
144 
145  /*! @method GetParameter */
146  AudioUnitParameterValue GetParameter( AudioUnitParameterID paramID )
147  {
148  return Globals()->GetParameter(paramID );
149  }
150 
151  /*! @method IsBypassEffect */
152  // This is used for the property value - to reflect to the UI if an effect is bypassed
153  bool IsBypassEffect () { return mBypassEffect; }
154 
155 protected:
156  /*! @method ShouldBypassEffect */
157  // This is used in the render call to see if an effect is bypassed
158  // It can return a different status than IsBypassEffect (though it MUST take that into account)
159  virtual bool ShouldBypassEffect () { return IsBypassEffect(); }
160 
161 public:
162  /*! @method SetBypassEffect */
163  virtual void SetBypassEffect (bool inFlag) { mBypassEffect = inFlag; }
164 
165  /*! @method SetParamHasSampleRateDependency */
166  void SetParamHasSampleRateDependency (bool inFlag)
167  {
168  mParamSRDep = inFlag;
169  }
170 
171  /*! @method GetParamHasSampleRateDependency */
172  bool GetParamHasSampleRateDependency () const { return mParamSRDep; }
173 
174 
175  struct ScheduledProcessParams // pointer passed in as void* userData param for ProcessScheduledSlice()
176  {
177  AudioUnitRenderActionFlags *actionFlags;
178  AudioBufferList *inputBufferList;
179  AudioBufferList *outputBufferList;
180  };
181 
182  virtual ComponentResult ProcessScheduledSlice( void *inUserData,
183  UInt32 inStartFrameInBuffer,
184  UInt32 inSliceFramesToProcess,
185  UInt32 inTotalBufferFrames );
186 
187 
188  bool ProcessesInPlace() const {return mProcessesInPlace;};
189  void SetProcessesInPlace(bool inProcessesInPlace) {mProcessesInPlace = inProcessesInPlace;};
190 
191  typedef std::vector<AUKernelBase *> KernelList;
192 
193 protected:
194  /*! @var mKernelList */
195  KernelList mKernelList;
196 
197  /*! @method IsInputSilent */
198  bool IsInputSilent (AudioUnitRenderActionFlags inActionFlags, UInt32 inFramesToProcess)
199  {
200  bool inputSilent = (inActionFlags & kAudioUnitRenderAction_OutputIsSilence) != 0;
201 
202  // take latency and tail time into account when propagating the silent bit
203  UInt32 silentTimeoutFrames = UInt32(GetSampleRate() * (GetLatency() + GetTailTime()));
204  mSilentTimeout.Process (inFramesToProcess, silentTimeoutFrames, inputSilent);
205  return inputSilent;
206  }
207 
208 private:
209  /*! @var mBypassEffect */
210  bool mBypassEffect;
211  /*! @var mParamSRDep */
212  bool mParamSRDep;
213 
214  /*! @var mProcessesInplace */
215  bool mProcessesInPlace;
216 
217  /*! @var mSilentTimeout */
218  AUSilentTimeout mSilentTimeout;
219 
220 
221 };
222 
223 
224 // Base class for a "kernel", an object that performs DSP on one channel of an interleaved stream.
225  /*! @class AUKernelBase */
226 class AUKernelBase {
227 public:
228  /*! @ctor AUKernelBase */
229  AUKernelBase(AUEffectBase *inAudioUnit ) :
230  mAudioUnit(inAudioUnit), mIsLastKernel(false) { }
231 
232  /*! @dtor ~AUKernelBase */
233  virtual ~AUKernelBase() { }
234 
235  /*! @method Reset */
236  virtual void Reset() { }
237 
238  /*! @method Process */
239  virtual void Process( const AudioSampleType * inSourceP,
240  AudioSampleType * inDestP,
241  UInt32 inFramesToProcess,
242  UInt32 inNumChannels,
243  bool & ioSilence) = 0;
244 
245  /*! @method GetSampleRate */
246  Float64 GetSampleRate()
247  {
248  return mAudioUnit->GetSampleRate();
249  }
250 
251  /*! @method GetParameter */
252  AudioUnitParameterValue GetParameter (AudioUnitParameterID paramID)
253  {
254  return mAudioUnit->GetParameter(paramID);
255  }
256 
257  void SetChannelNum (UInt32 inChan) { mChannelNum = inChan; }
258  UInt32 GetChannelNum () { return mChannelNum; }
259 
260  bool IsLastKernel() const {return mIsLastKernel;};
261  void SetLastKernel(bool inIsLastKernel) {mIsLastKernel = inIsLastKernel;};
262 
263 protected:
264  /*! @var mAudioUnit */
265  AUEffectBase * mAudioUnit;
266  bool mIsLastKernel;
267  UInt32 mChannelNum;
268 
269 };
270 
271 
272 
273 #endif // __AUEffectBase_h__