root/trunk/PublicUtility/CAStreamBasicDescription.h

Revision 1, 8.5 kB (checked in by gbooker, 3 years ago)

Initial Import

Line 
1 /*      Copyright:      © Copyright 2004 Apple Computer, Inc. All rights reserved.
2
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.
8
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.
23
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.
29
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.
37 */
38 /*=============================================================================
39         CAStreamBasicDescription.h
40        
41 =============================================================================*/
42
43 #ifndef __CAStreamBasicDescription_h__
44 #define __CAStreamBasicDescription_h__
45
46 #if !defined(__COREAUDIO_USE_FLAT_INCLUDES__)
47         #include <CoreAudio/CoreAudioTypes.h>
48         #include <CoreFoundation/CoreFoundation.h>
49 #else
50         #include "CoreAudioTypes.h"
51         #include "CoreFoundation.h"
52 #endif
53
54 #include "CADebugMacros.h"
55 #include <string.h>     // for memset, memcpy
56 #include <stdio.h>      // for FILE *
57
58 //=============================================================================
59 //      CAStreamBasicDescription
60 //
61 //      This is a wrapper class for the AudioStreamBasicDescription struct.
62 //      It adds a number of convenience routines, but otherwise adds nothing
63 //      to the footprint of the original struct.
64 //=============================================================================
65 class CAStreamBasicDescription :
66         public AudioStreamBasicDescription
67 {
68
69 //      Constants
70 public:
71         static const AudioStreamBasicDescription        sEmpty;
72
73 //      Construction/Destruction
74 public:
75         CAStreamBasicDescription() { memset (this, 0, sizeof(AudioStreamBasicDescription)); }
76        
77         CAStreamBasicDescription(const AudioStreamBasicDescription &desc)
78         {
79                 SetFrom(desc);
80         }
81        
82         CAStreamBasicDescription(               double inSampleRate,            UInt32 inFormatID,
83                                                                         UInt32 inBytesPerPacket,        UInt32 inFramesPerPacket,
84                                                                         UInt32 inBytesPerFrame,         UInt32 inChannelsPerFrame,
85                                                                         UInt32 inBitsPerChannel,        UInt32 inFormatFlags);
86
87 //      Assignment
88         CAStreamBasicDescription&       operator=(const AudioStreamBasicDescription& v) { SetFrom(v); return *this; }
89
90         void    SetFrom(const AudioStreamBasicDescription &desc)
91         {
92                 memcpy(this, &desc, sizeof(AudioStreamBasicDescription));
93         }
94        
95         // _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
96         //
97         // interrogation
98        
99         bool    IsPCM() const { return mFormatID == kAudioFormatLinearPCM; }
100        
101         bool    PackednessIsSignificant() const
102         {
103                 Assert(IsPCM(), "PackednessIsSignificant only applies for PCM");
104                 return (SampleWordSize() << 3) != mBitsPerChannel;
105         }
106        
107         bool    AlignmentIsSignificant() const
108         {
109                 return PackednessIsSignificant() || (mBitsPerChannel & 7) != 0;
110         }
111        
112         bool    IsInterleaved() const
113         {
114                 return !IsPCM() || !(mFormatFlags & kAudioFormatFlagIsNonInterleaved);
115         }
116        
117         // for sanity with interleaved/deinterleaved possibilities, never access mChannelsPerFrame, use these:
118         UInt32  NumberInterleavedChannels() const       { return IsInterleaved() ? mChannelsPerFrame : 1; }     
119         UInt32  NumberChannelStreams() const            { return IsInterleaved() ? 1 : mChannelsPerFrame; }
120         UInt32  NumberChannels() const                          { return mChannelsPerFrame; }
121         UInt32  SampleWordSize() const                          { return (mBytesPerFrame > 0) ? mBytesPerFrame / NumberInterleavedChannels() :  0;}
122
123         UInt32  FramesToBytes(UInt32 nframes) const     { return nframes * mBytesPerFrame; }
124         UInt32  BytesToFrames(UInt32 nbytes) const      {
125                 Assert(mBytesPerFrame > 0, "bytesPerFrame must be > 0 in BytesToFrames");
126                 return nbytes / mBytesPerFrame;
127         }
128        
129         bool    SameChannelsAndInterleaving(const CAStreamBasicDescription &a) const
130         {
131                 return this->NumberChannels() == a.NumberChannels() && this->IsInterleaved() == a.IsInterleaved();
132         }
133        
134         // _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
135         //
136         //      manipulation
137        
138         void    SetCanonical(UInt32 nChannels, bool interleaved)
139                                 // note: leaves sample rate untouched
140         {
141                 mFormatID = kAudioFormatLinearPCM;
142                 mFormatFlags = kAudioFormatFlagsNativeFloatPacked;
143                 mBitsPerChannel = 32;
144                 mChannelsPerFrame = nChannels;
145                 mFramesPerPacket = 1;
146                 if (interleaved)
147                         mBytesPerPacket = mBytesPerFrame = nChannels * sizeof(Float32);
148                 else {
149                         mBytesPerPacket = mBytesPerFrame = sizeof(Float32);
150                         mFormatFlags |= kAudioFormatFlagIsNonInterleaved;
151                 }
152         }
153        
154         void    ChangeNumberChannels(UInt32 nChannels, bool interleaved)
155                                 // alter an existing format
156         {
157                 Assert(IsPCM(), "ChangeNumberChannels only works for PCM formats");
158                 UInt32 wordSize = SampleWordSize();     // get this before changing ANYTHING
159                 mChannelsPerFrame = nChannels;
160                 mFramesPerPacket = 1;
161                 if (interleaved) {
162                         mBytesPerPacket = mBytesPerFrame = nChannels * wordSize;
163                         mFormatFlags &= ~kAudioFormatFlagIsNonInterleaved;
164                 } else {
165                         mBytesPerPacket = mBytesPerFrame = wordSize;
166                         mFormatFlags |= kAudioFormatFlagIsNonInterleaved;
167                 }
168         }
169        
170         // _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
171         //
172         //      other
173        
174         void    Print() const
175         {
176                 Print (stdout);
177         }
178
179         void    Print(FILE* file) const
180         {
181                 PrintFormat (file, "", "AudioStreamBasicDescription:");
182         }
183
184         void PrintFormat(FILE *f, const char *indent, const char *name) const;
185
186         OSStatus                        Save(CFPropertyListRef *outData) const;
187                
188         OSStatus                        Restore(CFPropertyListRef &inData);
189
190 //      Operations
191         static void                     NormalizeLinearPCMFormat(AudioStreamBasicDescription& ioDescription);
192         static void                     ResetFormat(AudioStreamBasicDescription& ioDescription);
193         static void                     FillOutFormat(AudioStreamBasicDescription& ioDescription, const AudioStreamBasicDescription& inTemplateDescription);
194         static void                     GetSimpleName(const AudioStreamBasicDescription& inDescription, char* outName, bool inAbbreviate);
195 #if CoreAudio_Debug
196         static void                     PrintToLog(const AudioStreamBasicDescription& inDesc);
197 #endif
198 };
199
200 bool            operator<(const AudioStreamBasicDescription& x, const AudioStreamBasicDescription& y);
201 bool            operator==(const AudioStreamBasicDescription& x, const AudioStreamBasicDescription& y);
202 #if TARGET_OS_MAC || (TARGET_OS_WIN32 && (_MSC_VER > 600))
203 inline bool     operator!=(const AudioStreamBasicDescription& x, const AudioStreamBasicDescription& y) { return !(x == y); }
204 inline bool     operator<=(const AudioStreamBasicDescription& x, const AudioStreamBasicDescription& y) { return (x < y) || (x == y); }
205 inline bool     operator>=(const AudioStreamBasicDescription& x, const AudioStreamBasicDescription& y) { return !(x < y); }
206 inline bool     operator>(const AudioStreamBasicDescription& x, const AudioStreamBasicDescription& y) { return !((x < y) || (x == y)); }
207 #endif
208
209 bool SanityCheck(const AudioStreamBasicDescription& x);
210
211
212 #endif // __CAStreamBasicDescription_h__
Note: See TracBrowser for help on using the browser.