41 #ifndef __CAStreamBasicDescription_h__
42 #define __CAStreamBasicDescription_h__
44 #if !defined(__COREAUDIO_USE_FLAT_INCLUDES__)
45 #include <CoreAudio/CoreAudioTypes.h>
46 #include <CoreFoundation/CoreFoundation.h>
48 #include "CoreAudioTypes.h"
49 #include "CoreFoundation.h"
52 #include "CADebugMacros.h"
56 #pragma mark This file needs to compile on more earlier versions of the OS, so please keep that in mind when editing it
58 extern char *CAStringForOSType (OSType t,
char *writeLocation);
61 #if COREAUDIOTYPES_VERSION < 1050
62 typedef Float32 AudioSampleType;
63 enum { kAudioFormatFlagsCanonical = kAudioFormatFlagIsFloat | kAudioFormatFlagsNativeEndian | kAudioFormatFlagIsPacked };
65 #if COREAUDIOTYPES_VERSION < 1051
66 typedef Float32 AudioUnitSampleType;
68 kLinearPCMFormatFlagsSampleFractionShift = 7,
69 kLinearPCMFormatFlagsSampleFractionMask = (0x3F << kLinearPCMFormatFlagsSampleFractionShift),
74 #if (MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_3)
75 enum { kIsNonMixableFlag = kAudioFormatFlagIsNonMixable };
77 enum { kIsNonMixableFlag = (1L << 6) };
87 class CAStreamBasicDescription :
88 public AudioStreamBasicDescription
93 static const AudioStreamBasicDescription sEmpty;
97 CAStreamBasicDescription() { memset (
this, 0,
sizeof(AudioStreamBasicDescription)); }
99 CAStreamBasicDescription(
const AudioStreamBasicDescription &desc)
104 CAStreamBasicDescription(
double inSampleRate, UInt32 inFormatID,
105 UInt32 inBytesPerPacket, UInt32 inFramesPerPacket,
106 UInt32 inBytesPerFrame, UInt32 inChannelsPerFrame,
107 UInt32 inBitsPerChannel, UInt32 inFormatFlags);
110 CAStreamBasicDescription& operator=(
const AudioStreamBasicDescription& v) { SetFrom(v);
return *
this; }
112 void SetFrom(
const AudioStreamBasicDescription &desc)
114 memcpy(
this, &desc,
sizeof(AudioStreamBasicDescription));
121 bool IsPCM()
const {
return mFormatID == kAudioFormatLinearPCM; }
123 bool PackednessIsSignificant()
const
125 Assert(IsPCM(),
"PackednessIsSignificant only applies for PCM");
126 return (SampleWordSize() << 3) != mBitsPerChannel;
129 bool AlignmentIsSignificant()
const
131 return PackednessIsSignificant() || (mBitsPerChannel & 7) != 0;
134 bool IsInterleaved()
const
136 return !IsPCM() || !(mFormatFlags & kAudioFormatFlagIsNonInterleaved);
139 bool IsNativeEndian()
const
141 return (mFormatFlags & kAudioFormatFlagIsBigEndian) == kAudioFormatFlagsNativeEndian;
145 UInt32 NumberInterleavedChannels()
const {
return IsInterleaved() ? mChannelsPerFrame : 1; }
146 UInt32 NumberChannelStreams()
const {
return IsInterleaved() ? 1 : mChannelsPerFrame; }
147 UInt32 NumberChannels()
const {
return mChannelsPerFrame; }
148 UInt32 SampleWordSize()
const {
149 return (mBytesPerFrame > 0 && NumberInterleavedChannels()) ? mBytesPerFrame / NumberInterleavedChannels() : 0;
152 UInt32 FramesToBytes(UInt32 nframes)
const {
return nframes * mBytesPerFrame; }
153 UInt32 BytesToFrames(UInt32 nbytes)
const {
154 Assert(mBytesPerFrame > 0,
"bytesPerFrame must be > 0 in BytesToFrames");
155 return nbytes / mBytesPerFrame;
158 bool SameChannelsAndInterleaving(
const CAStreamBasicDescription &a)
const
160 return this->NumberChannels() == a.NumberChannels() && this->IsInterleaved() == a.IsInterleaved();
167 void SetCanonical(UInt32 nChannels,
bool interleaved)
170 mFormatID = kAudioFormatLinearPCM;
171 int sampleSize = SizeOf32(AudioSampleType);
172 mFormatFlags = kAudioFormatFlagsCanonical;
173 mBitsPerChannel = 8 * sampleSize;
174 mChannelsPerFrame = nChannels;
175 mFramesPerPacket = 1;
177 mBytesPerPacket = mBytesPerFrame = nChannels * sampleSize;
179 mBytesPerPacket = mBytesPerFrame = sampleSize;
180 mFormatFlags |= kAudioFormatFlagIsNonInterleaved;
184 bool IsCanonical()
const
186 if (mFormatID != kAudioFormatLinearPCM)
return false;
187 UInt32 reqFormatFlags;
188 UInt32 flagsMask = (kLinearPCMFormatFlagIsFloat | kLinearPCMFormatFlagIsBigEndian | kLinearPCMFormatFlagIsSignedInteger | kLinearPCMFormatFlagIsPacked | kLinearPCMFormatFlagIsAlignedHigh | kLinearPCMFormatFlagsSampleFractionMask);
189 bool interleaved = (mFormatFlags & kAudioFormatFlagIsNonInterleaved) == 0;
190 unsigned sampleSize = SizeOf32(AudioSampleType);
191 reqFormatFlags = kAudioFormatFlagsCanonical;
192 UInt32 reqFrameSize = interleaved ? (mChannelsPerFrame * sampleSize) : sampleSize;
194 return ((mFormatFlags & flagsMask) == reqFormatFlags
195 && mBitsPerChannel == 8 * sampleSize
196 && mFramesPerPacket == 1
197 && mBytesPerFrame == reqFrameSize
198 && mBytesPerPacket == reqFrameSize);
201 void SetAUCanonical(UInt32 nChannels,
bool interleaved)
203 mFormatID = kAudioFormatLinearPCM;
204 #if CA_PREFER_FIXED_POINT
205 mFormatFlags = kAudioFormatFlagsCanonical | (kAudioUnitSampleFractionBits << kLinearPCMFormatFlagsSampleFractionShift);
207 mFormatFlags = kAudioFormatFlagsCanonical;
209 mChannelsPerFrame = nChannels;
210 mFramesPerPacket = 1;
211 mBitsPerChannel = 8 * SizeOf32(AudioUnitSampleType);
213 mBytesPerPacket = mBytesPerFrame = nChannels * SizeOf32(AudioUnitSampleType);
215 mBytesPerPacket = mBytesPerFrame = SizeOf32(AudioUnitSampleType);
216 mFormatFlags |= kAudioFormatFlagIsNonInterleaved;
220 void ChangeNumberChannels(UInt32 nChannels,
bool interleaved)
223 Assert(IsPCM(),
"ChangeNumberChannels only works for PCM formats");
224 UInt32 wordSize = SampleWordSize();
226 wordSize = (mBitsPerChannel + 7) / 8;
227 mChannelsPerFrame = nChannels;
228 mFramesPerPacket = 1;
230 mBytesPerPacket = mBytesPerFrame = nChannels * wordSize;
231 mFormatFlags &= ~kAudioFormatFlagIsNonInterleaved;
233 mBytesPerPacket = mBytesPerFrame = wordSize;
234 mFormatFlags |= kAudioFormatFlagIsNonInterleaved;
242 bool IsEqual(
const AudioStreamBasicDescription &other,
bool interpretingWildcards=
true)
const;
248 void Print(FILE* file)
const {
249 PrintFormat (file,
"",
"AudioStreamBasicDescription:");
252 void PrintFormat(FILE *f,
const char *indent,
const char *name)
const {
254 fprintf(f,
"%s%s %s\n", indent, name, AsString(buf,
sizeof(buf)));
257 void PrintFormat2(FILE *f,
const char *indent,
const char *name)
const {
259 fprintf(f,
"%s%s %s", indent, name, AsString(buf,
sizeof(buf)));
262 char * AsString(
char *buf,
size_t bufsize)
const;
264 static void Print (
const AudioStreamBasicDescription &inDesc)
266 CAStreamBasicDescription desc(inDesc);
270 OSStatus Save(CFPropertyListRef *outData)
const;
272 OSStatus Restore(CFPropertyListRef &inData);
275 static bool IsMixable(
const AudioStreamBasicDescription& inDescription) {
return (inDescription.mFormatID == kAudioFormatLinearPCM) && ((inDescription.mFormatFlags & kIsNonMixableFlag) == 0); }
276 static void NormalizeLinearPCMFormat(AudioStreamBasicDescription& ioDescription);
277 static void ResetFormat(AudioStreamBasicDescription& ioDescription);
278 static void FillOutFormat(AudioStreamBasicDescription& ioDescription,
const AudioStreamBasicDescription& inTemplateDescription);
279 static void GetSimpleName(
const AudioStreamBasicDescription& inDescription,
char* outName, UInt32 inMaxNameLength,
bool inAbbreviate);
281 static void PrintToLog(
const AudioStreamBasicDescription& inDesc);
285 bool operator<(
const AudioStreamBasicDescription& x,
const AudioStreamBasicDescription& y);
286 bool operator==(
const AudioStreamBasicDescription& x,
const AudioStreamBasicDescription& y);
287 #if TARGET_OS_MAC || (TARGET_OS_WIN32 && (_MSC_VER > 600))
288 inline bool operator!=(
const AudioStreamBasicDescription& x,
const AudioStreamBasicDescription& y) {
return !(x == y); }
289 inline bool operator<=(
const AudioStreamBasicDescription& x,
const AudioStreamBasicDescription& y) {
return (x < y) || (x == y); }
290 inline bool operator>=(
const AudioStreamBasicDescription& x,
const AudioStreamBasicDescription& y) {
return !(x < y); }
291 inline bool operator>(
const AudioStreamBasicDescription& x,
const AudioStreamBasicDescription& y) {
return !((x < y) || (x == y)); }
294 bool SanityCheck(
const AudioStreamBasicDescription& x);
297 #endif // __CAStreamBasicDescription_h__
bool TTFOUNDATION_EXPORT operator!=(const TTObject &anObject, const TTObject &anotherObject)
Compare two objects for inequality.
bool TTFOUNDATION_EXPORT operator==(const TTObject &anObject, const TTObject &anotherObject)
Compare two objects for equality.