Jamoma API  0.6.0.a19
CAAudioChannelLayout.cpp
1 /* Copyright © 2007 Apple Inc. All Rights Reserved.
2 
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.
9 
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.
25 
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.
31 
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.
40 */
41 //=============================================================================
42 // Includes
43 //=============================================================================
44 
45 // Self Include
46 #include "CAAudioChannelLayout.h"
47 #include "CAAutoDisposer.h"
48 #include <stdlib.h>
49 #include <string.h>
50 
51 //=============================================================================
52 // CAAudioChannelLayout
53 //=============================================================================
54 
55 AudioChannelLayout* CAAudioChannelLayout::Create(UInt32 inNumberChannelDescriptions)
56 {
57  UInt32 theSize = CalculateByteSize(inNumberChannelDescriptions);
58  AudioChannelLayout* theAnswer = static_cast<AudioChannelLayout*>(CA_calloc(1, theSize));
59  if(theAnswer != NULL)
60  {
61  SetAllToUnknown(*theAnswer, inNumberChannelDescriptions);
62  }
63  return theAnswer;
64 }
65 
66 void CAAudioChannelLayout::Destroy(AudioChannelLayout* inChannelLayout)
67 {
68  free(inChannelLayout);
69 }
70 
71 void CAAudioChannelLayout::SetAllToUnknown(AudioChannelLayout& outChannelLayout, UInt32 inNumberChannelDescriptions)
72 {
73  outChannelLayout.mChannelLayoutTag = kAudioChannelLayoutTag_UseChannelDescriptions;
74  outChannelLayout.mChannelBitmap = 0;
75  outChannelLayout.mNumberChannelDescriptions = inNumberChannelDescriptions;
76  for(UInt32 theChannelIndex = 0; theChannelIndex < inNumberChannelDescriptions; ++theChannelIndex)
77  {
78  outChannelLayout.mChannelDescriptions[theChannelIndex].mChannelLabel = kAudioChannelLabel_Unknown;
79  outChannelLayout.mChannelDescriptions[theChannelIndex].mChannelFlags = 0;
80  outChannelLayout.mChannelDescriptions[theChannelIndex].mCoordinates[0] = 0;
81  outChannelLayout.mChannelDescriptions[theChannelIndex].mCoordinates[1] = 0;
82  outChannelLayout.mChannelDescriptions[theChannelIndex].mCoordinates[2] = 0;
83  }
84 }
85 
86 bool operator== (const AudioChannelLayout &x, const AudioChannelLayout &y)
87 {
88  // compare based on the number of channel descriptions present
89  // (this may be too strict a comparison if all you care about are matching layout tags)
90  UInt32 theSize1 = CAAudioChannelLayout::CalculateByteSize(x.mNumberChannelDescriptions);
91  UInt32 theSize2 = CAAudioChannelLayout::CalculateByteSize(y.mNumberChannelDescriptions);
92 
93  if (theSize1 != theSize2)
94  return false;
95 
96  return !memcmp (&x, &y, theSize1);
97 }
98 
99 // counting the one bits in a word
100 inline UInt32 CountOnes(UInt32 x)
101 {
102  // secret magic algorithm for counting bits in a word.
103  UInt32 t;
104  x = x - ((x >> 1) & 0x55555555);
105  t = ((x >> 2) & 0x33333333);
106  x = (x & 0x33333333) + t;
107  x = (x + (x >> 4)) & 0x0F0F0F0F;
108  x = x + (x << 8);
109  x = x + (x << 16);
110  return x >> 24;
111 }
112 
113 UInt32 CAAudioChannelLayout::NumberChannels (const AudioChannelLayout& inLayout)
114 {
115  if (inLayout.mChannelLayoutTag == kAudioChannelLayoutTag_UseChannelDescriptions)
116  return inLayout.mNumberChannelDescriptions;
117 
118  if (inLayout.mChannelLayoutTag == kAudioChannelLayoutTag_UseChannelBitmap)
119  return CountOnes (inLayout.mChannelBitmap);
120 
121  return AudioChannelLayoutTag_GetNumberOfChannels(inLayout.mChannelLayoutTag);
122 }
123 
124 void CAShowAudioChannelLayout (FILE* file, const AudioChannelLayout *layout)
125 {
126  if (layout == NULL)
127  {
128  fprintf (file, "\tNULL layout\n");
129  return;
130  }
131  fprintf (file, "\tTag=0x%X, ", (int)layout->mChannelLayoutTag);
132  if (layout->mChannelLayoutTag == kAudioChannelLayoutTag_UseChannelBitmap)
133  fprintf (file, "Using Bitmap:0x%X\n", (int)layout->mChannelBitmap);
134  else {
135  fprintf (file, "Num Chan Descs=%d\n", (int)layout->mNumberChannelDescriptions);
136  const AudioChannelDescription *desc = layout->mChannelDescriptions;
137  for (unsigned int i = 0; i < layout->mNumberChannelDescriptions; ++i, ++desc) {
138  fprintf (file, "\t\tLabel=%d, Flags=0x%X, ", (int)desc->mChannelLabel, (int)desc->mChannelFlags);
139  fprintf (file, "[az=%f,el=%f,dist=%f]\n", desc->mCoordinates[0], desc->mCoordinates[1], desc->mCoordinates[2]);
140  }
141  }
142 }
bool TTFOUNDATION_EXPORT operator==(const TTObject &anObject, const TTObject &anotherObject)
Compare two objects for equality.
Definition: TTObject.cpp:167