Jamoma API  0.6.0.a19
CAAudioUnitOutputCapturer.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 __CAAudioUnitOutputCapturer_h__
42 #define __CAAudioUnitOutputCapturer_h__
43 
44 #include <AudioToolbox/ExtendedAudioFile.h>
45 
46 /*
47  Class to capture output from an AudioUnit for analysis.
48 
49  example:
50 
51  CFURL fileurl = CFURLCreateWithFileSystemPath(NULL, CFSTR("/tmp/recording.caf"), kCFURLPOSIXPathStyle, false);
52 
53  CAAudioUnitOutputCapturer captor(someAU, fileurl, 'caff', anASBD);
54 
55  {
56  captor.Start();
57  ...
58  captor.Stop();
59  } // can repeat
60 
61  captor.Close(); // can be omitted; happens automatically from destructor
62 */
63 
64 class CAAudioUnitOutputCapturer {
65 public:
66  enum { noErr = 0 };
67 
68  CAAudioUnitOutputCapturer(AudioUnit au, CFURLRef outputFileURL, AudioFileTypeID fileType, const AudioStreamBasicDescription &format, UInt32 busNumber = 0) :
69  mFileOpen(false),
70  mClientFormatSet(false),
71  mAudioUnit(au),
72  mExtAudioFile(NULL),
73  mBusNumber (busNumber)
74  {
75  CFShow(outputFileURL);
76  OSStatus err = ExtAudioFileCreateWithURL(outputFileURL, fileType, &format, NULL, kAudioFileFlags_EraseFile, &mExtAudioFile);
77  if (!err)
78  mFileOpen = true;
79  }
80 
81  void Start() {
82  if (mFileOpen) {
83  if (!mClientFormatSet) {
84  AudioStreamBasicDescription clientFormat;
85  UInt32 size = sizeof(clientFormat);
86  AudioUnitGetProperty(mAudioUnit, kAudioUnitProperty_StreamFormat, kAudioUnitScope_Output, mBusNumber, &clientFormat, &size);
87  ExtAudioFileSetProperty(mExtAudioFile, kExtAudioFileProperty_ClientDataFormat, size, &clientFormat);
88  mClientFormatSet = true;
89  }
90  ExtAudioFileWriteAsync(mExtAudioFile, 0, NULL); // initialize async writes
91  AudioUnitAddRenderNotify(mAudioUnit, RenderCallback, this);
92  }
93  }
94 
95  void Stop() {
96  if (mFileOpen)
97  AudioUnitRemoveRenderNotify(mAudioUnit, RenderCallback, this);
98  }
99 
100  void Close() {
101  if (mExtAudioFile) {
102  ExtAudioFileDispose(mExtAudioFile);
103  mExtAudioFile = NULL;
104  }
105  }
106 
107  ~CAAudioUnitOutputCapturer() {
108  Close();
109  }
110 
111 private:
112  static OSStatus RenderCallback( void * inRefCon,
113  AudioUnitRenderActionFlags * ioActionFlags,
114  const AudioTimeStamp * inTimeStamp,
115  UInt32 inBusNumber,
116  UInt32 inNumberFrames,
117  AudioBufferList * ioData)
118  {
119  if (*ioActionFlags & kAudioUnitRenderAction_PostRender) {
120  CAAudioUnitOutputCapturer *This = (CAAudioUnitOutputCapturer *)inRefCon;
121  static int TEMP_kAudioUnitRenderAction_PostRenderError = (1 << 8);
122  if (This->mBusNumber == inBusNumber && !(*ioActionFlags & TEMP_kAudioUnitRenderAction_PostRenderError)) {
123  OSStatus result = ExtAudioFileWriteAsync(This->mExtAudioFile, inNumberFrames, ioData);
124  if (result) DebugMessageN1("ERROR WRITING FRAMES: %d\n", result);
125  }
126  }
127  return noErr;
128  }
129 
130  bool mFileOpen;
131  bool mClientFormatSet;
132  AudioUnit mAudioUnit;
133  ExtAudioFileRef mExtAudioFile;
134  UInt32 mBusNumber;
135 };
136 
137 #endif // __CAAudioUnitOutputCapturer_h__