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 CAAudioChannelLayout.cpp
41 =============================================================================*/
43 //=============================================================================
45 //=============================================================================
48 #include "CAAudioChannelLayout.h"
52 //=============================================================================
53 // CAAudioChannelLayout
54 //=============================================================================
56 AudioChannelLayout
* CAAudioChannelLayout::Create(UInt32 inNumberChannelDescriptions
)
58 UInt32 theSize
= CalculateByteSize(inNumberChannelDescriptions
);
59 AudioChannelLayout
* theAnswer
= static_cast<AudioChannelLayout
*>(calloc(1, theSize
));
62 SetAllToUnknown(*theAnswer
, inNumberChannelDescriptions
);
67 void CAAudioChannelLayout::Destroy(AudioChannelLayout
* inChannelLayout
)
69 free(inChannelLayout
);
72 void CAAudioChannelLayout::SetAllToUnknown(AudioChannelLayout
& outChannelLayout
, UInt32 inNumberChannelDescriptions
)
74 outChannelLayout
.mChannelLayoutTag
= kAudioChannelLayoutTag_UseChannelDescriptions
;
75 outChannelLayout
.mChannelBitmap
= 0;
76 outChannelLayout
.mNumberChannelDescriptions
= inNumberChannelDescriptions
;
77 for(UInt32 theChannelIndex
= 0; theChannelIndex
< inNumberChannelDescriptions
; ++theChannelIndex
)
79 outChannelLayout
.mChannelDescriptions
[theChannelIndex
].mChannelLabel
= kAudioChannelLabel_Unknown
;
80 outChannelLayout
.mChannelDescriptions
[theChannelIndex
].mChannelFlags
= 0;
81 outChannelLayout
.mChannelDescriptions
[theChannelIndex
].mCoordinates
[0] = 0;
82 outChannelLayout
.mChannelDescriptions
[theChannelIndex
].mCoordinates
[1] = 0;
83 outChannelLayout
.mChannelDescriptions
[theChannelIndex
].mCoordinates
[2] = 0;
87 bool operator== (const AudioChannelLayout
&x
, const AudioChannelLayout
&y
)
89 // compare based on the number of channel descriptions present
90 // (this may be too strict a comparison if all you care about are matching layout tags)
91 UInt32 theSize1
= CAAudioChannelLayout::CalculateByteSize(x
.mNumberChannelDescriptions
);
92 UInt32 theSize2
= CAAudioChannelLayout::CalculateByteSize(y
.mNumberChannelDescriptions
);
94 if (theSize1
!= theSize2
)
97 return !memcmp (&x
, &y
, theSize1
);
100 // counting the one bits in a word
101 inline UInt32
CountOnes(UInt32 x
)
103 // secret magic algorithm for counting bits in a word.
105 x
= x
- ((x
>> 1) & 0x55555555);
106 t
= ((x
>> 2) & 0x33333333);
107 x
= (x
& 0x33333333) + t
;
108 x
= (x
+ (x
>> 4)) & 0x0F0F0F0F;
114 UInt32
CAAudioChannelLayout::NumberChannels (const AudioChannelLayout
& inLayout
)
116 if (inLayout
.mChannelLayoutTag
== kAudioChannelLayoutTag_UseChannelDescriptions
)
117 return inLayout
.mNumberChannelDescriptions
;
119 if (inLayout
.mChannelLayoutTag
== kAudioChannelLayoutTag_UseChannelBitmap
)
120 return CountOnes (inLayout
.mChannelBitmap
);
122 return AudioChannelLayoutTag_GetNumberOfChannels(inLayout
.mChannelLayoutTag
);
125 void CAShowAudioChannelLayout (FILE* file
, const AudioChannelLayout
*layout
)
127 fprintf (file
, "\tTag=0x%lX, ", layout
->mChannelLayoutTag
);
128 if (layout
->mChannelLayoutTag
== kAudioChannelLayoutTag_UseChannelBitmap
)
129 fprintf (file
, "Using Bitmap:0x%lX\n", layout
->mChannelBitmap
);
131 fprintf (file
, "Num Chan Descs=%ld\n", layout
->mNumberChannelDescriptions
);
132 const AudioChannelDescription
*desc
= layout
->mChannelDescriptions
;
133 for (unsigned int i
= 0; i
< layout
->mNumberChannelDescriptions
; ++i
, ++desc
) {
134 fprintf (file
, "\t\tLabel=%ld, Flags=0x%lX, ", desc
->mChannelLabel
, desc
->mChannelFlags
);
135 fprintf (file
, "[az=%f,el=%f,dist=%f]\n", desc
->mCoordinates
[0], desc
->mCoordinates
[1], desc
->mCoordinates
[2]);