globally remove all trailing whitespace from .cpp and .hpp files missed by previous...
[ardour.git] / libs / appleutility / CAAudioChannelLayoutObject.cpp
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     CAAudioChannelLayoutObject.cpp
40
41 =============================================================================*/
42
43 #include "CAAudioChannelLayout.h"
44 #if !defined(__COREAUDIO_USE_FLAT_INCLUDES__)
45         #include <CoreServices/CoreServices.h>
46         #include <AudioToolbox/AudioFormat.h>
47 #else
48         #include <CoreServices.h>
49         #include <AudioFormat.h>
50 #endif
51
52
53 CAAudioChannelLayout::CAAudioChannelLayout ()
54 {
55         mLayoutHolder = new ACLRefCounter (offsetof(AudioChannelLayout, mChannelDescriptions));
56 }
57
58 //=============================================================================
59 //      CAAudioChannelLayout::CAAudioChannelLayout
60 //=============================================================================
61 CAAudioChannelLayout::CAAudioChannelLayout (UInt32 inNumberChannels, bool inChooseSurround)
62 {               
63                 // this chooses default layouts based on the number of channels...
64         UInt32 theSize = CalculateByteSize (inNumberChannels);
65                 
66         mLayoutHolder = new ACLRefCounter (theSize);
67         
68         AudioChannelLayout* layout = mLayoutHolder->GetLayout();
69
70         layout->mNumberChannelDescriptions = inNumberChannels;
71         
72         switch (inNumberChannels)
73         {
74                 case 1:
75                         layout->mChannelLayoutTag = kAudioChannelLayoutTag_Mono;
76                         break;
77                 case 2:
78                         layout->mChannelLayoutTag = inChooseSurround ? kAudioChannelLayoutTag_Binaural : kAudioChannelLayoutTag_Stereo;
79                         break;
80                 case 4:
81                         layout->mChannelLayoutTag = inChooseSurround ? kAudioChannelLayoutTag_Ambisonic_B_Format : kAudioChannelLayoutTag_AudioUnit_4;
82                         break;
83                 case 5:
84                         layout->mChannelLayoutTag = inChooseSurround ? kAudioChannelLayoutTag_AudioUnit_5_0 : kAudioChannelLayoutTag_AudioUnit_5;
85                         break;
86                 case 6:
87                         layout->mChannelLayoutTag = inChooseSurround ? kAudioChannelLayoutTag_AudioUnit_6_0 : kAudioChannelLayoutTag_AudioUnit_6;
88                         break;
89                 case 7:
90                         layout->mChannelLayoutTag = kAudioChannelLayoutTag_AudioUnit_7_0;
91                         break;
92                 case 8:
93                         layout->mChannelLayoutTag = kAudioChannelLayoutTag_AudioUnit_8;
94                         break;
95                 default:
96                         // here we have a "broken" layout, in the sense that we haven't any idea how to lay this out
97                         // the layout itself is all set to zeros
98                         // ### no longer true ###
99                         SetAllToUnknown(*layout, inNumberChannels);
100                         break;
101         }
102 }
103
104 //=============================================================================
105 //      CAAudioChannelLayout::CAAudioChannelLayout
106 //=============================================================================
107 CAAudioChannelLayout::CAAudioChannelLayout (AudioChannelLayoutTag inLayoutTag)
108         : mLayoutHolder(NULL)
109 {
110         SetWithTag(inLayoutTag);
111 }
112
113 //=============================================================================
114 //      CAAudioChannelLayout::CAAudioChannelLayout
115 //=============================================================================
116 CAAudioChannelLayout::CAAudioChannelLayout (const CAAudioChannelLayout &c)
117         : mLayoutHolder(NULL)
118 {
119         *this = c;
120 }
121
122
123 //=============================================================================
124 //      CAAudioChannelLayout::AudioChannelLayout
125 //=============================================================================
126 CAAudioChannelLayout::CAAudioChannelLayout (const AudioChannelLayout* inChannelLayout)
127         : mLayoutHolder(NULL)
128 {
129         *this = inChannelLayout;
130 }
131
132 //=============================================================================
133 //      CAAudioChannelLayout::~CAAudioChannelLayout
134 //=============================================================================
135 CAAudioChannelLayout::~CAAudioChannelLayout ()
136 {
137         if (mLayoutHolder) {
138                 mLayoutHolder->release();
139                 mLayoutHolder = NULL;
140         }
141 }
142
143 //=============================================================================
144 //      CAAudioChannelLayout::CAAudioChannelLayout
145 //=============================================================================
146 CAAudioChannelLayout& CAAudioChannelLayout::operator= (const CAAudioChannelLayout &c)
147 {
148         if (mLayoutHolder != c.mLayoutHolder) {
149                 if (mLayoutHolder)
150                         mLayoutHolder->release();
151         
152                 if ((mLayoutHolder = c.mLayoutHolder) != NULL)
153                         mLayoutHolder->retain();
154         }
155         
156         return *this;
157 }
158
159 CAAudioChannelLayout&   CAAudioChannelLayout::operator= (const AudioChannelLayout* inChannelLayout)
160 {
161         if (mLayoutHolder)
162                 mLayoutHolder->release();
163
164         UInt32 theSize = CalculateByteSize (inChannelLayout->mNumberChannelDescriptions);
165         
166         mLayoutHolder = new ACLRefCounter (theSize);
167         
168         memcpy(mLayoutHolder->mLayout, inChannelLayout, theSize);
169         return *this;
170 }
171
172 void    CAAudioChannelLayout::SetWithTag(AudioChannelLayoutTag inTag)
173 {
174         if (mLayoutHolder)
175                 mLayoutHolder->release();
176         
177         mLayoutHolder = new ACLRefCounter(offsetof(AudioChannelLayout, mChannelDescriptions[0]));
178         AudioChannelLayout* layout = mLayoutHolder->GetLayout();
179         layout->mChannelLayoutTag = inTag;
180 }
181
182 //=============================================================================
183 //      CAAudioChannelLayout::operator==
184 //=============================================================================
185 bool            CAAudioChannelLayout::operator== (const CAAudioChannelLayout &c) const
186 {
187         if (mLayoutHolder == c.mLayoutHolder)
188                 return true;
189         return Layout() == c.Layout();
190 }
191
192 //=============================================================================
193 //      CAAudioChannelLayout::Print
194 //=============================================================================
195 void            CAAudioChannelLayout::Print (FILE* file) const
196 {
197         CAShowAudioChannelLayout (file, &Layout());
198 }
199