Jamoma API  0.6.0.a19
CAAudioFileFormats.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 __CAAudioFileFormats_h__
42 #define __CAAudioFileFormats_h__
43 
44 #if !defined(__COREAUDIO_USE_FLAT_INCLUDES__)
45  #include <AudioToolbox/AudioToolbox.h>
46 #else
47  #include <AudioToolbox.h>
48 #endif
49 #include "CAStreamBasicDescription.h"
50 
51 class CAAudioFileFormats {
52 public:
53  enum { noErr = 0 };
54 
55  struct DataFormatInfo {
56  DataFormatInfo() : mVariants(NULL) { }
57  ~DataFormatInfo() { delete[] mVariants; }
58 
59  UInt32 mFormatID;
60  int mNumVariants;
61  AudioStreamBasicDescription * mVariants;
62  bool mReadable;
63  bool mWritable;
64  bool mEitherEndianPCM;
65 
66 #if DEBUG
67  void DebugPrint();
68 #endif
69  };
70 
71  struct FileFormatInfo {
72  FileFormatInfo() : mFileTypeName(NULL), mExtensions(NULL), mDataFormats(NULL) { }
73  ~FileFormatInfo() {
74  delete[] mDataFormats;
75  if (mFileTypeName)
76  CFRelease(mFileTypeName);
77  if (mExtensions)
78  CFRelease(mExtensions);
79  }
80 
81  AudioFileTypeID mFileTypeID;
82  CFStringRef mFileTypeName;
83  CFArrayRef mExtensions;
84  int mNumDataFormats;
85  DataFormatInfo * mDataFormats; // NULL until loaded!
86 
87  int NumberOfExtensions() { return mExtensions ? CFArrayGetCount(mExtensions) : 0; }
88  char * GetExtension(int index, char *buf, int buflen) {
89  CFStringRef cfext = (CFStringRef)CFArrayGetValueAtIndex(mExtensions, index);
90  CFStringGetCString(cfext, buf, buflen, kCFStringEncodingUTF8);
91  return buf;
92  }
93  bool MatchExtension(CFStringRef testExt) { // testExt should not include "."
94  CFIndex n = NumberOfExtensions();
95  for (CFIndex i = 0; i < n; ++i) {
96  CFStringRef ext = (CFStringRef)CFArrayGetValueAtIndex(mExtensions, i);
97  if (CFStringCompare(ext, testExt, kCFCompareCaseInsensitive) == kCFCompareEqualTo)
98  return true;
99  }
100  return false;
101  }
102  bool AnyWritableFormats();
103  void LoadDataFormats();
104 
105 #if DEBUG
106  void DebugPrint();
107 #endif
108  };
109 
110 private: // use Instance()
111  CAAudioFileFormats(bool loadDataFormats);
112  ~CAAudioFileFormats();
113 public:
114 
115  bool InferDataFormatFromFileFormat(AudioFileTypeID filetype, CAStreamBasicDescription &fmt);
116 
117  bool InferFileFormatFromFilename(const char *filename, AudioFileTypeID &filetype);
118 
119  bool InferFileFormatFromFilename(CFStringRef filename, AudioFileTypeID &filetype);
120 
121  bool InferFileFormatFromDataFormat(const CAStreamBasicDescription &fmt, AudioFileTypeID &filetype);
122 
123  bool IsKnownDataFormat(UInt32 dataFormat);
124 
125 #if DEBUG
126  void DebugPrint();
127 #endif
128 
129  int mNumFileFormats;
130  FileFormatInfo * mFileFormats;
131 
132  FileFormatInfo * FindFileFormat(UInt32 formatID);
133 
134  static CAAudioFileFormats * Instance(bool loadDataFormats=true);
135 
136 private:
137  static CAAudioFileFormats * sInstance;
138 };
139 
140 char * OSTypeToStr(char *buf, UInt32 t);
141 int StrToOSType(const char *str, UInt32 &t);
142 
143 #endif // __CAAudioFileFormats_h__