1 /*
2      File: CAAudioUnitOutputCapturer.h
3  Abstract: Part of CoreAudio Utility Classes
4   Version: 1.1
5 
6  Disclaimer: IMPORTANT:  This Apple software is supplied to you by Apple
7  Inc. ("Apple") in consideration of your agreement to the following
8  terms, and your use, installation, modification or redistribution of
9  this Apple software constitutes acceptance of these terms.  If you do
10  not agree with these terms, please do not use, install, modify or
11  redistribute this Apple software.
12 
13  In consideration of your agreement to abide by the following terms, and
14  subject to these terms, Apple grants you a personal, non-exclusive
15  license, under Apple's copyrights in this original Apple software (the
16  "Apple Software"), to use, reproduce, modify and redistribute the Apple
17  Software, with or without modifications, in source and/or binary forms;
18  provided that if you redistribute the Apple Software in its entirety and
19  without modifications, you must retain this notice and the following
20  text and disclaimers in all such redistributions of the Apple Software.
21  Neither the name, trademarks, service marks or logos of Apple Inc. may
22  be used to endorse or promote products derived from the Apple Software
23  without specific prior written permission from Apple.  Except as
24  expressly stated in this notice, no other rights or licenses, express or
25  implied, are granted by Apple herein, including but not limited to any
26  patent rights that may be infringed by your derivative works or by other
27  works in which the Apple Software may be incorporated.
28 
29  The Apple Software is provided by Apple on an "AS IS" basis.  APPLE
30  MAKES NO WARRANTIES, EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION
31  THE IMPLIED WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS
32  FOR A PARTICULAR PURPOSE, REGARDING THE APPLE SOFTWARE OR ITS USE AND
33  OPERATION ALONE OR IN COMBINATION WITH YOUR PRODUCTS.
34 
35  IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL
36  OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
37  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
38  INTERRUPTION) ARISING IN ANY WAY OUT OF THE USE, REPRODUCTION,
39  MODIFICATION AND/OR DISTRIBUTION OF THE APPLE SOFTWARE, HOWEVER CAUSED
40  AND WHETHER UNDER THEORY OF CONTRACT, TORT (INCLUDING NEGLIGENCE),
41  STRICT LIABILITY OR OTHERWISE, EVEN IF APPLE HAS BEEN ADVISED OF THE
42  POSSIBILITY OF SUCH DAMAGE.
43 
44  Copyright (C) 2014 Apple Inc. All Rights Reserved.
45 
46 */
47 #ifndef __CAAudioUnitOutputCapturer_h__
48 #define __CAAudioUnitOutputCapturer_h__
49 
50 #include <AudioToolbox/ExtendedAudioFile.h>
51 
52 /*
53 	Class to capture output from an AudioUnit for analysis.
54 
55 	example:
56 
57 	CFURL fileurl = CFURLCreateWithFileSystemPath(NULL, CFSTR("/tmp/recording.caf"), kCFURLPOSIXPathStyle, false);
58 
59 	CAAudioUnitOutputCapturer captor(someAU, fileurl, 'caff', anASBD);
60 
61 	{
62 	captor.Start();
63 	...
64 	captor.Stop();
65 	} // can repeat
66 
67 	captor.Close(); // can be omitted; happens automatically from destructor
68 */
69 
70 class CAAudioUnitOutputCapturer {
71 public:
72 	enum { noErr = 0 };
73 
74 	CAAudioUnitOutputCapturer(AudioUnit au, CFURLRef outputFileURL, AudioFileTypeID fileType, const AudioStreamBasicDescription &format, UInt32 busNumber = 0) :
mFileOpen(false)75 		mFileOpen(false),
76 		mClientFormatSet(false),
77 		mAudioUnit(au),
78 		mExtAudioFile(NULL),
79 		mBusNumber (busNumber)
80 	{
81 		CFShow(outputFileURL);
82 		OSStatus err = ExtAudioFileCreateWithURL(outputFileURL, fileType, &format, NULL, kAudioFileFlags_EraseFile, &mExtAudioFile);
83 		if (!err)
84 			mFileOpen = true;
85 	}
86 
Start()87 	void	Start() {
88 		if (mFileOpen) {
89 			if (!mClientFormatSet) {
90 				AudioStreamBasicDescription clientFormat;
91 				UInt32 size = sizeof(clientFormat);
92 				AudioUnitGetProperty(mAudioUnit, kAudioUnitProperty_StreamFormat, kAudioUnitScope_Output, mBusNumber, &clientFormat, &size);
93 				ExtAudioFileSetProperty(mExtAudioFile, kExtAudioFileProperty_ClientDataFormat, size, &clientFormat);
94 				mClientFormatSet = true;
95 			}
96 			ExtAudioFileWriteAsync(mExtAudioFile, 0, NULL);	// initialize async writes
97 			AudioUnitAddRenderNotify(mAudioUnit, RenderCallback, this);
98 		}
99 	}
100 
Stop()101 	void	Stop() {
102 		if (mFileOpen)
103 			AudioUnitRemoveRenderNotify(mAudioUnit, RenderCallback, this);
104 	}
105 
Close()106 	void	Close() {
107 		if (mExtAudioFile) {
108 			ExtAudioFileDispose(mExtAudioFile);
109 			mExtAudioFile = NULL;
110 		}
111 	}
112 
~CAAudioUnitOutputCapturer()113 	~CAAudioUnitOutputCapturer() {
114 		Close();
115 	}
116 
117 private:
RenderCallback(void * inRefCon,AudioUnitRenderActionFlags * ioActionFlags,const AudioTimeStamp * inTimeStamp,UInt32 inBusNumber,UInt32 inNumberFrames,AudioBufferList * ioData)118 	static OSStatus RenderCallback(	void *							inRefCon,
119 									AudioUnitRenderActionFlags *	ioActionFlags,
120 									const AudioTimeStamp *			inTimeStamp,
121 									UInt32							inBusNumber,
122 									UInt32							inNumberFrames,
123 									AudioBufferList *				ioData)
124 	{
125 		if (*ioActionFlags & kAudioUnitRenderAction_PostRender) {
126 			CAAudioUnitOutputCapturer *This = (CAAudioUnitOutputCapturer *)inRefCon;
127 			static int TEMP_kAudioUnitRenderAction_PostRenderError	= (1 << 8);
128 			if (This->mBusNumber == inBusNumber && !(*ioActionFlags & TEMP_kAudioUnitRenderAction_PostRenderError)) {
129 				OSStatus result = ExtAudioFileWriteAsync(This->mExtAudioFile, inNumberFrames, ioData);
130 				if (result) DebugMessageN1("ERROR WRITING FRAMES: %d\n", (int)result);
131 			}
132 		}
133 		return noErr;
134 	}
135 
136 	bool				mFileOpen;
137 	bool				mClientFormatSet;
138 	AudioUnit			mAudioUnit;
139 	ExtAudioFileRef		mExtAudioFile;
140 	UInt32				mBusNumber;
141 };
142 
143 #endif // __CAAudioUnitOutputCapturer_h__
144