1 /* Copyright: © Copyright 2005 Apple Computer, Inc. All rights reserved.
3 Disclaimer: IMPORTANT: This Apple software is supplied to you by Apple Computer, Inc.
4 ("Apple") in consideration of your agreement to the following terms, and your
5 use, installation, modification or redistribution of this Apple software
6 constitutes acceptance of these terms. If you do not agree with these terms,
7 please do not use, install, modify or redistribute this Apple software.
9 In consideration of your agreement to abide by the following terms, and subject
10 to these terms, Apple grants you a personal, non-exclusive license, under AppleÕs
11 copyrights in this original Apple software (the "Apple Software"), to use,
12 reproduce, modify and redistribute the Apple Software, with or without
13 modifications, in source and/or binary forms; provided that if you redistribute
14 the Apple Software in its entirety and without modifications, you must retain
15 this notice and the following text and disclaimers in all such redistributions of
16 the Apple Software. Neither the name, trademarks, service marks or logos of
17 Apple Computer, Inc. may be used to endorse or promote products derived from the
18 Apple Software without specific prior written permission from Apple. Except as
19 expressly stated in this notice, no other rights or licenses, express or implied,
20 are granted by Apple herein, including but not limited to any patent rights that
21 may be infringed by your derivative works or by other works in which the Apple
22 Software may be incorporated.
24 The Apple Software is provided by Apple on an "AS IS" basis. APPLE MAKES NO
25 WARRANTIES, EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION THE IMPLIED
26 WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
27 PURPOSE, REGARDING THE APPLE SOFTWARE OR ITS USE AND OPERATION ALONE OR IN
28 COMBINATION WITH YOUR PRODUCTS.
30 IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL OR
31 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
32 GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 ARISING IN ANY WAY OUT OF THE USE, REPRODUCTION, MODIFICATION AND/OR DISTRIBUTION
34 OF THE APPLE SOFTWARE, HOWEVER CAUSED AND WHETHER UNDER THEORY OF CONTRACT, TORT
35 (INCLUDING NEGLIGENCE), STRICT LIABILITY OR OTHERWISE, EVEN IF APPLE HAS BEEN
36 ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
38 /*=============================================================================
39 CAStreamBasicDescription.h
41 =============================================================================*/
43 #ifndef __CAStreamBasicDescription_h__
44 #define __CAStreamBasicDescription_h__
46 #if !defined(__COREAUDIO_USE_FLAT_INCLUDES__)
47 #include <CoreAudio/CoreAudioTypes.h>
48 #include <CoreFoundation/CoreFoundation.h>
50 #include "CoreAudioTypes.h"
51 #include "CoreFoundation.h"
54 #include "CADebugMacros.h"
55 #include <string.h> // for memset, memcpy
56 #include <stdio.h> // for FILE *
58 #pragma mark This file needs to compile on more earlier versions of the OS, so please keep that in mind when editing it
60 // define the IsMixable format flag for all versions of the system
61 #if (MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_3)
62 enum { kIsNonMixableFlag
= kAudioFormatFlagIsNonMixable
};
64 enum { kIsNonMixableFlag
= (1L << 6) };
67 //=============================================================================
68 // CAStreamBasicDescription
70 // This is a wrapper class for the AudioStreamBasicDescription struct.
71 // It adds a number of convenience routines, but otherwise adds nothing
72 // to the footprint of the original struct.
73 //=============================================================================
74 class CAStreamBasicDescription
:
75 public AudioStreamBasicDescription
80 static const AudioStreamBasicDescription sEmpty
;
82 // Construction/Destruction
84 CAStreamBasicDescription() { memset (this, 0, sizeof(AudioStreamBasicDescription
)); }
86 CAStreamBasicDescription(const AudioStreamBasicDescription
&desc
)
91 CAStreamBasicDescription( double inSampleRate
, UInt32 inFormatID
,
92 UInt32 inBytesPerPacket
, UInt32 inFramesPerPacket
,
93 UInt32 inBytesPerFrame
, UInt32 inChannelsPerFrame
,
94 UInt32 inBitsPerChannel
, UInt32 inFormatFlags
);
97 CAStreamBasicDescription
& operator=(const AudioStreamBasicDescription
& v
) { SetFrom(v
); return *this; }
99 void SetFrom(const AudioStreamBasicDescription
&desc
)
101 memcpy(this, &desc
, sizeof(AudioStreamBasicDescription
));
104 // _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
108 bool IsPCM() const { return mFormatID
== kAudioFormatLinearPCM
; }
110 bool PackednessIsSignificant() const
112 Assert(IsPCM(), "PackednessIsSignificant only applies for PCM");
113 return (SampleWordSize() << 3) != mBitsPerChannel
;
116 bool AlignmentIsSignificant() const
118 return PackednessIsSignificant() || (mBitsPerChannel
& 7) != 0;
121 bool IsInterleaved() const
123 return !IsPCM() || !(mFormatFlags
& kAudioFormatFlagIsNonInterleaved
);
126 // for sanity with interleaved/deinterleaved possibilities, never access mChannelsPerFrame, use these:
127 UInt32
NumberInterleavedChannels() const { return IsInterleaved() ? mChannelsPerFrame
: 1; }
128 UInt32
NumberChannelStreams() const { return IsInterleaved() ? 1 : mChannelsPerFrame
; }
129 UInt32
NumberChannels() const { return mChannelsPerFrame
; }
130 UInt32
SampleWordSize() const { return (mBytesPerFrame
> 0) ? mBytesPerFrame
/ NumberInterleavedChannels() : 0;}
132 UInt32
FramesToBytes(UInt32 nframes
) const { return nframes
* mBytesPerFrame
; }
133 UInt32
BytesToFrames(UInt32 nbytes
) const {
134 Assert(mBytesPerFrame
> 0, "bytesPerFrame must be > 0 in BytesToFrames");
135 return nbytes
/ mBytesPerFrame
;
138 bool SameChannelsAndInterleaving(const CAStreamBasicDescription
&a
) const
140 return this->NumberChannels() == a
.NumberChannels() && this->IsInterleaved() == a
.IsInterleaved();
143 // _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
147 void SetCanonical(UInt32 nChannels
, bool interleaved
)
148 // note: leaves sample rate untouched
150 mFormatID
= kAudioFormatLinearPCM
;
151 mFormatFlags
= kAudioFormatFlagsNativeFloatPacked
;
152 mBitsPerChannel
= 32;
153 mChannelsPerFrame
= nChannels
;
154 mFramesPerPacket
= 1;
156 mBytesPerPacket
= mBytesPerFrame
= nChannels
* sizeof(Float32
);
158 mBytesPerPacket
= mBytesPerFrame
= sizeof(Float32
);
159 mFormatFlags
|= kAudioFormatFlagIsNonInterleaved
;
163 void ChangeNumberChannels(UInt32 nChannels
, bool interleaved
)
164 // alter an existing format
166 Assert(IsPCM(), "ChangeNumberChannels only works for PCM formats");
167 UInt32 wordSize
= SampleWordSize(); // get this before changing ANYTHING
169 wordSize
= (mBitsPerChannel
+ 7) / 8;
170 mChannelsPerFrame
= nChannels
;
171 mFramesPerPacket
= 1;
173 mBytesPerPacket
= mBytesPerFrame
= nChannels
* wordSize
;
174 mFormatFlags
&= ~kAudioFormatFlagIsNonInterleaved
;
176 mBytesPerPacket
= mBytesPerFrame
= wordSize
;
177 mFormatFlags
|= kAudioFormatFlagIsNonInterleaved
;
181 // _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
190 void Print(FILE* file
) const
192 PrintFormat (file
, "", "AudioStreamBasicDescription:");
195 void PrintFormat(FILE *f
, const char *indent
, const char *name
) const;
197 OSStatus
Save(CFPropertyListRef
*outData
) const;
199 OSStatus
Restore(CFPropertyListRef
&inData
);
202 static bool IsMixable(const AudioStreamBasicDescription
& inDescription
) { return (inDescription
.mFormatID
== kAudioFormatLinearPCM
) && ((inDescription
.mFormatFlags
& kIsNonMixableFlag
) == 0); }
203 static void NormalizeLinearPCMFormat(AudioStreamBasicDescription
& ioDescription
);
204 static void ResetFormat(AudioStreamBasicDescription
& ioDescription
);
205 static void FillOutFormat(AudioStreamBasicDescription
& ioDescription
, const AudioStreamBasicDescription
& inTemplateDescription
);
206 static void GetSimpleName(const AudioStreamBasicDescription
& inDescription
, char* outName
, bool inAbbreviate
);
208 static void PrintToLog(const AudioStreamBasicDescription
& inDesc
);
212 bool operator<(const AudioStreamBasicDescription
& x
, const AudioStreamBasicDescription
& y
);
213 bool operator==(const AudioStreamBasicDescription
& x
, const AudioStreamBasicDescription
& y
);
214 #if TARGET_OS_MAC || (TARGET_OS_WIN32 && (_MSC_VER > 600))
215 inline bool operator!=(const AudioStreamBasicDescription
& x
, const AudioStreamBasicDescription
& y
) { return !(x
== y
); }
216 inline bool operator<=(const AudioStreamBasicDescription
& x
, const AudioStreamBasicDescription
& y
) { return (x
< y
) || (x
== y
); }
217 inline bool operator>=(const AudioStreamBasicDescription
& x
, const AudioStreamBasicDescription
& y
) { return !(x
< y
); }
218 inline bool operator>(const AudioStreamBasicDescription
& x
, const AudioStreamBasicDescription
& y
) { return !((x
< y
) || (x
== y
)); }
221 bool SanityCheck(const AudioStreamBasicDescription
& x
);
224 #endif // __CAStreamBasicDescription_h__