aa41c40fe46208bac6850d63a7f107988be4fb9a
[ardour.git] / libs / appleutility / CAStreamBasicDescription.h
1 /*      Copyright:      � Copyright 2005 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 #pragma mark    This file needs to compile on more earlier versions of the OS, so please keep that in mind when editing it
59
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 };
63 #else
64         enum { kIsNonMixableFlag = (1L << 6) };
65 #endif
66
67 //=============================================================================
68 //      CAStreamBasicDescription
69 //
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
76 {
77
78 //      Constants
79 public:
80         static const AudioStreamBasicDescription        sEmpty;
81
82 //      Construction/Destruction
83 public:
84         CAStreamBasicDescription() { memset (this, 0, sizeof(AudioStreamBasicDescription)); }
85         
86         CAStreamBasicDescription(const AudioStreamBasicDescription &desc)
87         {
88                 SetFrom(desc);
89         }
90         
91         CAStreamBasicDescription(               double inSampleRate,            UInt32 inFormatID,
92                                                                         UInt32 inBytesPerPacket,        UInt32 inFramesPerPacket,
93                                                                         UInt32 inBytesPerFrame,         UInt32 inChannelsPerFrame,
94                                                                         UInt32 inBitsPerChannel,        UInt32 inFormatFlags);
95
96 //      Assignment
97         CAStreamBasicDescription&       operator=(const AudioStreamBasicDescription& v) { SetFrom(v); return *this; }
98
99         void    SetFrom(const AudioStreamBasicDescription &desc)
100         {
101                 memcpy(this, &desc, sizeof(AudioStreamBasicDescription));
102         }
103         
104         // _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
105         //
106         // interrogation
107         
108         bool    IsPCM() const { return mFormatID == kAudioFormatLinearPCM; }
109         
110         bool    PackednessIsSignificant() const
111         {
112                 Assert(IsPCM(), "PackednessIsSignificant only applies for PCM");
113                 return (SampleWordSize() << 3) != mBitsPerChannel;
114         }
115         
116         bool    AlignmentIsSignificant() const
117         {
118                 return PackednessIsSignificant() || (mBitsPerChannel & 7) != 0;
119         }
120         
121         bool    IsInterleaved() const
122         {
123                 return !IsPCM() || !(mFormatFlags & kAudioFormatFlagIsNonInterleaved);
124         }
125         
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;}
131
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;
136         }
137         
138         bool    SameChannelsAndInterleaving(const CAStreamBasicDescription &a) const
139         {
140                 return this->NumberChannels() == a.NumberChannels() && this->IsInterleaved() == a.IsInterleaved();
141         }
142         
143         // _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
144         //
145         //      manipulation
146         
147         void    SetCanonical(UInt32 nChannels, bool interleaved)
148                                 // note: leaves sample rate untouched
149         {
150                 mFormatID = kAudioFormatLinearPCM;
151                 mFormatFlags = kAudioFormatFlagsNativeFloatPacked;
152                 mBitsPerChannel = 32;
153                 mChannelsPerFrame = nChannels;
154                 mFramesPerPacket = 1;
155                 if (interleaved)
156                         mBytesPerPacket = mBytesPerFrame = nChannels * sizeof(Float32);
157                 else {
158                         mBytesPerPacket = mBytesPerFrame = sizeof(Float32);
159                         mFormatFlags |= kAudioFormatFlagIsNonInterleaved;
160                 }
161         }
162         
163         void    ChangeNumberChannels(UInt32 nChannels, bool interleaved)
164                                 // alter an existing format
165         {
166                 Assert(IsPCM(), "ChangeNumberChannels only works for PCM formats");
167                 UInt32 wordSize = SampleWordSize();     // get this before changing ANYTHING
168                 if (wordSize == 0)
169                         wordSize = (mBitsPerChannel + 7) / 8;
170                 mChannelsPerFrame = nChannels;
171                 mFramesPerPacket = 1;
172                 if (interleaved) {
173                         mBytesPerPacket = mBytesPerFrame = nChannels * wordSize;
174                         mFormatFlags &= ~kAudioFormatFlagIsNonInterleaved;
175                 } else {
176                         mBytesPerPacket = mBytesPerFrame = wordSize;
177                         mFormatFlags |= kAudioFormatFlagIsNonInterleaved;
178                 }
179         }
180         
181         // _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
182         //
183         //      other
184         
185         void    Print() const
186         {
187                 Print (stdout);
188         }
189
190         void    Print(FILE* file) const
191         {
192                 PrintFormat (file, "", "AudioStreamBasicDescription:"); 
193         }
194
195         void PrintFormat(FILE *f, const char *indent, const char *name) const;
196
197         OSStatus                        Save(CFPropertyListRef *outData) const;
198                 
199         OSStatus                        Restore(CFPropertyListRef &inData);
200
201 //      Operations
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);
207 #if CoreAudio_Debug
208         static void                     PrintToLog(const AudioStreamBasicDescription& inDesc);
209 #endif
210 };
211
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)); }
219 #endif
220
221 bool SanityCheck(const AudioStreamBasicDescription& x);
222
223
224 #endif // __CAStreamBasicDescription_h__