Jamoma API  0.6.0.a19
AUInputFormatConverter.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 __AUInputFormatConverter_h__
42 #define __AUInputFormatConverter_h__
43 
44 #include "FormatConverterClient.h"
45 #include "AUTimestampGenerator.h"
46 
47 // ____________________________________________________________________________
48 // AUInputFormatConverter
49 //
50 // Subclass of FormatConverterClient that applies a format conversion
51 // to an input of an AudioUnit.
52  /*! @class AUInputFormatConverter */
53 class AUInputFormatConverter : public FormatConverterClient {
54 public:
55  /*! @ctor AUInputFormatConverter */
56  AUInputFormatConverter(AUBase *hostAU, int inputBus) :
57  mHost(hostAU),
58  mHostBus(inputBus),
59  mPreviousSilentFrames(0x1000)
60  {
61 #if DEBUG
62  mTimestampGenerator.mVerbosity = 0;
63  strcpy(mTimestampGenerator.mDebugName, "AUConverter");
64 #endif
65  }
66 
67  // need to subsequently call Initialize, with the desired formats
68 
69  /*! @dtor ~AUInputFormatConverter */
70  ~AUInputFormatConverter()
71  {
72  }
73 
74  virtual OSStatus Initialize(const AudioStreamBasicDescription &src, const AudioStreamBasicDescription &dest)
75  {
76  OSStatus err = FormatConverterClient::Initialize(src, dest);
77  if (err) return err;
78  mIsPCMToPCM = (src.mFormatID == kAudioFormatLinearPCM) && (dest.mFormatID == kAudioFormatLinearPCM);
79  mHasSRC = (fnonzero(src.mSampleRate) && fnonzero(dest.mSampleRate) && fnotequal(src.mSampleRate, dest.mSampleRate));
80  return ca_noErr;
81  }
82 
83  virtual OSStatus Reset()
84  {
85  mPreviousSilentFrames = 0x1000;
86  mTimestampGenerator.Reset();
87  return FormatConverterClient::Reset();
88  }
89 
90  void SetStartInputTimeAtZero(bool b)
91  {
92  mTimestampGenerator.SetStartInputAtZero(b);
93  }
94 
95  /*! @method FillComplexBuffer */
96  OSStatus AUFillComplexBuffer(const AudioTimeStamp & inTimeStamp,
97  UInt32 & ioOutputDataPacketSize,
98  AudioBufferList & outOutputData,
99  AudioStreamPacketDescription* outPacketDescription,
100  bool& outSilence)
101  {
102  mTimestampGenerator.AddOutputTime(inTimeStamp, ioOutputDataPacketSize, mOutputFormat.mSampleRate);
103  mSilentOutput = true;
104  OSStatus err = FillComplexBuffer(ioOutputDataPacketSize, outOutputData, outPacketDescription);
105  if (mSilentOutput) {
106  if (!mIsPCMToPCM || (mHasSRC && mPreviousSilentFrames < 32))
107  mSilentOutput = false;
108  mPreviousSilentFrames += ioOutputDataPacketSize;
109  } else
110  mPreviousSilentFrames = 0;
111  outSilence = mSilentOutput;
112  return err;
113  }
114 
115  /*! @method FormatConverterInputProc */
116  virtual OSStatus FormatConverterInputProc(
117  UInt32 & ioNumberDataPackets,
118  AudioBufferList & ioData,
119  AudioStreamPacketDescription** outDataPacketDescription)
120  {
121  OSStatus err = ca_noErr;
122 
123  AudioUnitRenderActionFlags actionFlags = 0;
124  AUInputElement *input = mHost->GetInput(mHostBus);
125  *ioNumberDataPackets = std::min(*ioNumberDataPackets, This->mHost->GetMaxFramesPerSlice());
126  const AudioTimeStamp &inputTime = mTimestampGenerator.GenerateInputTime(ioNumberDataPackets, mInputFormat.mSampleRate);
127  err = input->PullInput(actionFlags, inputTime, mHostBus, ioNumberDataPackets);
128  if (!err) {
129  input->CopyBufferListTo(ioData);
130  if (!(actionFlags & kAudioUnitRenderAction_OutputIsSilence))
131  mSilentOutput = false;
132  }
133  return err;
134  }
135 
136 protected:
137  /*! @var mHost */
138  AUBase * mHost;
139  /*! @var mHostBus */
140  int mHostBus;
141 
142  AUTimestampGenerator mTimestampGenerator;
143  bool mIsPCMToPCM;
144  bool mHasSRC;
145  bool mSilentOutput;
146  UInt32 mPreviousSilentFrames;
147 };
148 
149 #endif // __AUInputFormatConverter_h__