3 Abstract: Part of CoreAudio Utility Classes
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.
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.
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.
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.
44 Copyright (C) 2014 Apple Inc. All Rights Reserved.
47 #ifndef __CAExtAudioFile_h__
48 #define __CAExtAudioFile_h__
50 #include <TargetConditionals.h>
52 #if !defined(__COREAUDIO_USE_FLAT_INCLUDES__)
53 #include <AudioToolbox/ExtendedAudioFile.h>
54 #include <AudioToolbox/AudioConverter.h>
56 #include "ExtendedAudioFile.h"
57 #include "AudioConverter.h"
59 #include "CAXException.h"
60 //#include "CAAutoDisposer.h"
61 #include "CAStreamBasicDescription.h"
62 #include "CAAudioChannelLayout.h"
63 #include "CACFObject.h"
65 // A C++ wrapper for ExtAudioFile
66 // Error returns throw CAXExceptions.
67 class CAExtAudioFile {
69 // instances are not automatically associated with open files.
71 mExtAudioFile(NULL) { }
73 virtual ~CAExtAudioFile()
78 bool IsValid() const { return mExtAudioFile != NULL; }
80 void Open(const char* filename)
83 CFURLRef url = CFURLCreateFromFileSystemRepresentation(kCFAllocatorDefault, (const UInt8*)filename, strlen(filename), false);
84 XThrowIf(!url, -1, "couldn't convert path to CFURLRef");
85 OSStatus res = ExtAudioFileOpenURL(url, &mExtAudioFile);
88 Check(res, "ExtAudioFileOpenURL");
92 // this group of methods maps directly to the API other than OSStatus results translating into exceptions.
93 // you must explicitly open, wrap or create a file.
94 void OpenURL(CFURLRef url)
97 Check(ExtAudioFileOpenURL(url, &mExtAudioFile), "ExtAudioFileOpenURL");
100 void WrapAudioFileID(AudioFileID inFileID, Boolean forWriting)
103 Check(ExtAudioFileWrapAudioFileID(inFileID, forWriting, &mExtAudioFile), "ExtAudioFileWrapAudioFileID");
106 void Create(const char *filePath, AudioFileTypeID filetype, const AudioStreamBasicDescription &streamDesc, const AudioChannelLayout *channelLayout, UInt32 flags)
108 CFURLRef url = CFURLCreateFromFileSystemRepresentation(NULL, (const UInt8 *)filePath, strlen(filePath), false);
109 XThrowIf(!url, -1, "couldn't convert path to CFURLRef");
111 OSStatus res = ExtAudioFileCreateWithURL(url, filetype, &streamDesc, channelLayout, flags, &mExtAudioFile);
114 Check(res, "ExtAudioFileCreateWithURL");
118 void CreateWithURL(CFURLRef url, AudioFileTypeID filetype, const AudioStreamBasicDescription &streamDesc, const AudioChannelLayout *channelLayout, UInt32 flags)
121 Check(ExtAudioFileCreateWithURL(url, filetype, &streamDesc, channelLayout, flags, &mExtAudioFile), "ExtAudioFileCreateWithURL");
124 // you may explicitly close a file, or have it closed automatically by the destructor.
127 if (mExtAudioFile != NULL) {
128 Check(ExtAudioFileDispose(mExtAudioFile), "ExtAudioFileClose");
129 mExtAudioFile = NULL;
133 void Read(UInt32 &ioNumberFrames, AudioBufferList *ioData)
135 Check(ExtAudioFileRead(mExtAudioFile, &ioNumberFrames, ioData), "ExtAudioFileRead");
138 OSStatus Write(UInt32 inNumberFrames, const AudioBufferList *ioData)
140 OSStatus err = ExtAudioFileWrite(mExtAudioFile, inNumberFrames, ioData);
142 // noErr and certain special errors are returned without an exception
146 case kExtAudioFileError_CodecUnavailableInputConsumed:
147 case kExtAudioFileError_CodecUnavailableInputNotConsumed:
151 // throw an exception
152 Check(err, "ExtAudioFileWrite");
158 void WriteAsync(UInt32 inNumberFrames, const AudioBufferList *ioData)
160 Check(ExtAudioFileWriteAsync(mExtAudioFile, inNumberFrames, ioData), "ExtAudioFileWriteAsync");
163 void Seek(SInt64 inFrameOffset)
165 Check(ExtAudioFileSeek(mExtAudioFile, inFrameOffset), "ExtAudioFileSeek");
171 Check(ExtAudioFileTell(mExtAudioFile, &pos), "ExtAudioFileTell");
175 UInt32 GetPropertyInfo(ExtAudioFilePropertyID propid, Boolean *outWritable) const
178 CheckProperty(ExtAudioFileGetPropertyInfo(mExtAudioFile, propid, &size, outWritable), "ExtAudioFileGetPropertyInfo", propid);
182 void GetProperty(ExtAudioFilePropertyID propid, UInt32 &ioSize, void *outData) const
184 CheckProperty(ExtAudioFileGetProperty(mExtAudioFile, propid, &ioSize, outData), "ExtAudioFileGetProperty", propid);
187 void SetProperty(ExtAudioFilePropertyID propid, UInt32 size, const void *inData)
189 CheckProperty(ExtAudioFileSetProperty(mExtAudioFile, propid, size, inData), "ExtAudioFileSetProperty", propid);
192 const CAAudioChannelLayout &GetFileChannelLayout()
194 return FetchChannelLayout(mFileChannelLayout, kExtAudioFileProperty_FileChannelLayout);
197 void SetFileChannelLayout(const CAAudioChannelLayout &layout) {
198 SetProperty(kExtAudioFileProperty_FileChannelLayout, layout.Size(), &layout.Layout());
201 const CAStreamBasicDescription &GetFileDataFormat()
203 UInt32 size = sizeof(mFileDataFormat);
204 GetProperty(kExtAudioFileProperty_FileDataFormat, size, &mFileDataFormat);
205 return mFileDataFormat;
208 const CAStreamBasicDescription &GetClientDataFormat() {
209 UInt32 size = sizeof(mClientDataFormat);
210 GetProperty(kExtAudioFileProperty_ClientDataFormat, size, &mClientDataFormat);
211 return mClientDataFormat;
215 void SetClientFormat(const CAStreamBasicDescription &dataFormat, const CAAudioChannelLayout *layout=NULL, UInt32 codecManuf=0) {
217 SetProperty('cman' /*kExtAudioFileProperty_CodecManufacturer*/, sizeof(codecManuf), &codecManuf);
218 SetProperty(kExtAudioFileProperty_ClientDataFormat, sizeof(dataFormat), &dataFormat);
220 SetClientChannelLayout(*layout);
223 void SetClientChannelLayout(const CAAudioChannelLayout &layout) {
224 SetProperty(kExtAudioFileProperty_ClientChannelLayout, layout.Size(), &layout.Layout());
227 AudioConverterRef GetConverter() const {
228 UInt32 size = sizeof(AudioConverterRef);
229 AudioConverterRef converter = NULL;
230 GetProperty(kExtAudioFileProperty_AudioConverter, size, &converter);
234 bool HasConverter() const { return GetConverter() != NULL; }
236 OSStatus SetConverterProperty(AudioConverterPropertyID inPropertyID, UInt32 inPropertyDataSize, const void *inPropertyData, bool inCanFail=false)
238 OSStatus err = AudioConverterSetProperty(GetConverter(), inPropertyID, inPropertyDataSize, inPropertyData);
240 XThrowIfError(err, "Couldn't set audio converter property");
242 // must tell the file that we have changed the converter; a NULL converter config is sufficient
243 CFPropertyListRef config = NULL;
244 SetProperty(kExtAudioFileProperty_ConverterConfig, sizeof(CFPropertyListRef), &config);
249 SInt64 GetNumberFrames() {
251 UInt32 size = sizeof(SInt64);
252 GetProperty(kExtAudioFileProperty_FileLengthFrames, size, &length);
258 virtual void Check(OSStatus err, const char *func) const
262 snprintf(txt, sizeof(txt), "%s failed", func);
263 throw CAXException(txt, err);
267 virtual void CheckProperty(OSStatus err, const char *func, UInt32 propid) const
271 char propstr[] = CA4CCToCString(propid);
272 snprintf(txt, sizeof(txt), "%s ('%s') failed", func, propstr);
273 throw CAXException(txt, err);
277 const CAAudioChannelLayout & FetchChannelLayout(CAAudioChannelLayout &layoutObj, ExtAudioFilePropertyID propID) {
278 UInt32 size = GetPropertyInfo(propID, NULL);
279 CAAutoFree<AudioChannelLayout> layout;
280 layout.allocBytes(size);
281 GetProperty(propID, size, layout);
287 CAExtAudioFile(const CAExtAudioFile &) { } // prohibit
288 CAExtAudioFile & operator = (const CAExtAudioFile &) { return *this; } // prohibit
291 ExtAudioFileRef mExtAudioFile;
293 // for convenience to the client, it helps if we hold onto some storage for these
294 CAStreamBasicDescription mFileDataFormat;
295 CAAudioChannelLayout mFileChannelLayout;
296 CAStreamBasicDescription mClientDataFormat;
300 #endif // __CAExtAudioFile_h__