1 /* Copyright © 2007 Apple Inc. All Rights Reserved.
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.
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.
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.
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.
41 /*=============================================================================
42 CAStreamBasicDescription.h
44 =============================================================================*/
46 #ifndef __CAStreamBasicDescription_h__
47 #define __CAStreamBasicDescription_h__
49 #if !defined(__COREAUDIO_USE_FLAT_INCLUDES__)
50 #include <CoreAudio/CoreAudioTypes.h>
51 #include <CoreFoundation/CoreFoundation.h>
53 #include "CoreAudioTypes.h"
54 #include "CoreFoundation.h"
57 #include "CADebugMacros.h"
58 #include <string.h> // for memset, memcpy
59 #include <stdio.h> // for FILE *
61 #pragma mark This file needs to compile on more earlier versions of the OS, so please keep that in mind when editing it
63 // define Leopard specific symbols for backward compatibility if applicable
64 #if COREAUDIOTYPES_VERSION < 1050
65 typedef Float32 AudioSampleType
;
66 enum { kAudioFormatFlagsCanonical
= kAudioFormatFlagIsFloat
| kAudioFormatFlagsNativeEndian
| kAudioFormatFlagIsPacked
};
69 // define the IsMixable format flag for all versions of the system
70 #if (MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_3)
71 enum { kIsNonMixableFlag
= kAudioFormatFlagIsNonMixable
};
73 enum { kIsNonMixableFlag
= (1L << 6) };
76 //=============================================================================
77 // CAStreamBasicDescription
79 // This is a wrapper class for the AudioStreamBasicDescription struct.
80 // It adds a number of convenience routines, but otherwise adds nothing
81 // to the footprint of the original struct.
82 //=============================================================================
83 class CAStreamBasicDescription
:
84 public AudioStreamBasicDescription
89 static const AudioStreamBasicDescription sEmpty
;
91 // Construction/Destruction
93 CAStreamBasicDescription() { memset (this, 0, sizeof(AudioStreamBasicDescription
)); }
95 CAStreamBasicDescription(const AudioStreamBasicDescription
&desc
)
100 CAStreamBasicDescription( double inSampleRate
, UInt32 inFormatID
,
101 UInt32 inBytesPerPacket
, UInt32 inFramesPerPacket
,
102 UInt32 inBytesPerFrame
, UInt32 inChannelsPerFrame
,
103 UInt32 inBitsPerChannel
, UInt32 inFormatFlags
);
106 CAStreamBasicDescription
& operator=(const AudioStreamBasicDescription
& v
) { SetFrom(v
); return *this; }
108 void SetFrom(const AudioStreamBasicDescription
&desc
)
110 memcpy(this, &desc
, sizeof(AudioStreamBasicDescription
));
113 // _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
117 bool IsPCM() const { return mFormatID
== kAudioFormatLinearPCM
; }
119 bool PackednessIsSignificant() const
121 Assert(IsPCM(), "PackednessIsSignificant only applies for PCM");
122 return (SampleWordSize() << 3) != mBitsPerChannel
;
125 bool AlignmentIsSignificant() const
127 return PackednessIsSignificant() || (mBitsPerChannel
& 7) != 0;
130 bool IsInterleaved() const
132 return !IsPCM() || !(mFormatFlags
& kAudioFormatFlagIsNonInterleaved
);
135 // for sanity with interleaved/deinterleaved possibilities, never access mChannelsPerFrame, use these:
136 UInt32
NumberInterleavedChannels() const { return IsInterleaved() ? mChannelsPerFrame
: 1; }
137 UInt32
NumberChannelStreams() const { return IsInterleaved() ? 1 : mChannelsPerFrame
; }
138 UInt32
NumberChannels() const { return mChannelsPerFrame
; }
139 UInt32
SampleWordSize() const {
140 return (mBytesPerFrame
> 0 && NumberInterleavedChannels()) ? mBytesPerFrame
/ NumberInterleavedChannels() : 0;
143 UInt32
FramesToBytes(UInt32 nframes
) const { return nframes
* mBytesPerFrame
; }
144 UInt32
BytesToFrames(UInt32 nbytes
) const {
145 Assert(mBytesPerFrame
> 0, "bytesPerFrame must be > 0 in BytesToFrames");
146 return nbytes
/ mBytesPerFrame
;
149 bool SameChannelsAndInterleaving(const CAStreamBasicDescription
&a
) const
151 return this->NumberChannels() == a
.NumberChannels() && this->IsInterleaved() == a
.IsInterleaved();
154 // _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
158 void SetCanonical(UInt32 nChannels
, bool interleaved
)
159 // note: leaves sample rate untouched
161 mFormatID
= kAudioFormatLinearPCM
;
162 mFormatFlags
= kAudioFormatFlagsCanonical
;
163 mBitsPerChannel
= 8 * sizeof(AudioSampleType
);
164 mChannelsPerFrame
= nChannels
;
165 mFramesPerPacket
= 1;
167 mBytesPerPacket
= mBytesPerFrame
= nChannels
* sizeof(AudioSampleType
);
169 mBytesPerPacket
= mBytesPerFrame
= sizeof(AudioSampleType
);
170 mFormatFlags
|= kAudioFormatFlagIsNonInterleaved
;
174 void ChangeNumberChannels(UInt32 nChannels
, bool interleaved
)
175 // alter an existing format
177 Assert(IsPCM(), "ChangeNumberChannels only works for PCM formats");
178 UInt32 wordSize
= SampleWordSize(); // get this before changing ANYTHING
180 wordSize
= (mBitsPerChannel
+ 7) / 8;
181 mChannelsPerFrame
= nChannels
;
182 mFramesPerPacket
= 1;
184 mBytesPerPacket
= mBytesPerFrame
= nChannels
* wordSize
;
185 mFormatFlags
&= ~kAudioFormatFlagIsNonInterleaved
;
187 mBytesPerPacket
= mBytesPerFrame
= wordSize
;
188 mFormatFlags
|= kAudioFormatFlagIsNonInterleaved
;
192 // _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
196 bool IsEqual(const AudioStreamBasicDescription
&other
, bool interpretingWildcards
=true) const;
203 void Print(FILE* file
) const
205 PrintFormat (file
, "", "AudioStreamBasicDescription:");
208 void PrintFormat(FILE *f
, const char *indent
, const char *name
) const {
209 PrintFormat2(f
, indent
, name
);
212 void PrintFormat2(FILE *f
, const char *indent
, const char *name
) const; // no trailing newline
214 static void Print (const AudioStreamBasicDescription
&inDesc
)
216 CAStreamBasicDescription
desc(inDesc
);
220 OSStatus
Save(CFPropertyListRef
*outData
) const;
222 OSStatus
Restore(CFPropertyListRef
&inData
);
225 static bool IsMixable(const AudioStreamBasicDescription
& inDescription
) { return (inDescription
.mFormatID
== kAudioFormatLinearPCM
) && ((inDescription
.mFormatFlags
& kIsNonMixableFlag
) == 0); }
226 static void NormalizeLinearPCMFormat(AudioStreamBasicDescription
& ioDescription
);
227 static void ResetFormat(AudioStreamBasicDescription
& ioDescription
);
228 static void FillOutFormat(AudioStreamBasicDescription
& ioDescription
, const AudioStreamBasicDescription
& inTemplateDescription
);
229 static void GetSimpleName(const AudioStreamBasicDescription
& inDescription
, char* outName
, bool inAbbreviate
);
231 static void PrintToLog(const AudioStreamBasicDescription
& inDesc
);
235 bool operator<(const AudioStreamBasicDescription
& x
, const AudioStreamBasicDescription
& y
);
236 bool operator==(const AudioStreamBasicDescription
& x
, const AudioStreamBasicDescription
& y
);
237 #if TARGET_OS_MAC || (TARGET_OS_WIN32 && (_MSC_VER > 600))
238 inline bool operator!=(const AudioStreamBasicDescription
& x
, const AudioStreamBasicDescription
& y
) { return !(x
== y
); }
239 inline bool operator<=(const AudioStreamBasicDescription
& x
, const AudioStreamBasicDescription
& y
) { return (x
< y
) || (x
== y
); }
240 inline bool operator>=(const AudioStreamBasicDescription
& x
, const AudioStreamBasicDescription
& y
) { return !(x
< y
); }
241 inline bool operator>(const AudioStreamBasicDescription
& x
, const AudioStreamBasicDescription
& y
) { return !((x
< y
) || (x
== y
)); }
244 bool SanityCheck(const AudioStreamBasicDescription
& x
);
247 #endif // __CAStreamBasicDescription_h__