replicate the remove-all-trailing whitespace commit(s) in master
[ardour.git] / libs / appleutility / CoreAudio / PublicUtility / CAAudioValueRange.cpp
1 /*
2      File: CAAudioValueRange.cpp
3  Abstract: CAAudioValueRange.h
4   Version: 1.1
5
6  Disclaimer: IMPORTANT:  This Apple software is supplied to you by Apple
7  Inc. ("Apple") in consideration of your agreement to the following
8  terms, and your use, installation, modification or redistribution of
9  this Apple software constitutes acceptance of these terms.  If you do
10  not agree with these terms, please do not use, install, modify or
11  redistribute this Apple software.
12
13  In consideration of your agreement to abide by the following terms, and
14  subject to these terms, Apple grants you a personal, non-exclusive
15  license, under Apple's copyrights in this original Apple software (the
16  "Apple Software"), to use, reproduce, modify and redistribute the Apple
17  Software, with or without modifications, in source and/or binary forms;
18  provided that if you redistribute the Apple Software in its entirety and
19  without modifications, you must retain this notice and the following
20  text and disclaimers in all such redistributions of the Apple Software.
21  Neither the name, trademarks, service marks or logos of Apple Inc. may
22  be used to endorse or promote products derived from the Apple Software
23  without specific prior written permission from Apple.  Except as
24  expressly stated in this notice, no other rights or licenses, express or
25  implied, are granted by Apple herein, including but not limited to any
26  patent rights that may be infringed by your derivative works or by other
27  works in which the Apple Software may be incorporated.
28
29  The Apple Software is provided by Apple on an "AS IS" basis.  APPLE
30  MAKES NO WARRANTIES, EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION
31  THE IMPLIED WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS
32  FOR A PARTICULAR PURPOSE, REGARDING THE APPLE SOFTWARE OR ITS USE AND
33  OPERATION ALONE OR IN COMBINATION WITH YOUR PRODUCTS.
34
35  IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL
36  OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
37  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
38  INTERRUPTION) ARISING IN ANY WAY OUT OF THE USE, REPRODUCTION,
39  MODIFICATION AND/OR DISTRIBUTION OF THE APPLE SOFTWARE, HOWEVER CAUSED
40  AND WHETHER UNDER THEORY OF CONTRACT, TORT (INCLUDING NEGLIGENCE),
41  STRICT LIABILITY OR OTHERWISE, EVEN IF APPLE HAS BEEN ADVISED OF THE
42  POSSIBILITY OF SUCH DAMAGE.
43
44  Copyright (C) 2014 Apple Inc. All Rights Reserved.
45
46 */
47 //==================================================================================================
48 //      Includes
49 //==================================================================================================
50
51 //      Self Include
52 #include "CAAudioValueRange.h"
53
54 //      Standard Library
55 #include <algorithm>
56
57 //==================================================================================================
58 //      CAAudioValueRange
59 //==================================================================================================
60
61 Float64 CAAudioValueRange::BoundValue(const AudioValueRange& inRange, Float64 inValue)
62 {
63         if (inValue <= inRange.mMinimum)
64         {
65                 return inRange.mMinimum;
66         }
67         else if (inValue >= inRange.mMaximum)
68         {
69                 return inRange.mMaximum;
70         }
71         else
72         {
73                 return inValue;
74         }
75 }
76
77 Float64 CAAudioValueRange::PickCommonSampleRate(const AudioValueRange& inRange)
78 {
79         //  This routine will pick a "common" sample rate from the give range of rates or the maximum
80         //  if no common rates can be found. It assumes that inRange contains a continuous range of
81         //  sample rates.
82         Float64 theAnswer = inRange.mMaximum;
83
84         if(ContainsValue(inRange, 44100.0))
85         {
86                 theAnswer = 44100.0;
87         }
88         else if(ContainsValue(inRange, 48000.0))
89         {
90                 theAnswer = 48000.0;
91         }
92         else if(ContainsValue(inRange, 96000.0))
93         {
94                 theAnswer = 96000.0;
95         }
96         else if(ContainsValue(inRange, 88200.0))
97         {
98                 theAnswer = 88200.0;
99         }
100         else if(ContainsValue(inRange, 64000.0))
101         {
102                 theAnswer = 64000.0;
103         }
104         else if(ContainsValue(inRange, 32000.0))
105         {
106                 theAnswer = 32000.0;
107         }
108         else if(ContainsValue(inRange, 24000.0))
109         {
110                 theAnswer = 24000.0;
111         }
112         else if(ContainsValue(inRange, 22050.0))
113         {
114                 theAnswer = 22050.0;
115         }
116         else if(ContainsValue(inRange, 16000.0))
117         {
118                 theAnswer = 16000.0;
119         }
120         else if(ContainsValue(inRange, 12000.0))
121         {
122                 theAnswer = 12000.0;
123         }
124         else if(ContainsValue(inRange, 11025.0))
125         {
126                 theAnswer = 11025.0;
127         }
128         else if(ContainsValue(inRange, 8000.0))
129         {
130                 theAnswer = 8000.0;
131         }
132
133         return theAnswer;
134 }
135
136 bool    CAAudioValueRange::Intersection(const AudioValueRange& x, const AudioValueRange& y, AudioValueRange& outRange)
137 {
138         bool isNonEmpty;
139         if(!IsStrictlyLessThan(x, y) && !IsStrictlyGreaterThan(x, y))
140         {
141                 outRange.mMinimum = std::max(x.mMinimum, y.mMinimum);
142                 outRange.mMaximum = std::min(x.mMaximum, y.mMaximum);
143                 isNonEmpty = true;
144         }
145         else
146         {
147                 outRange.mMinimum = 0;
148                 outRange.mMaximum = 0;
149                 isNonEmpty = false;
150         }
151         return isNonEmpty;
152 }
153
154 bool    CAAudioValueRange::Union(const AudioValueRange& x, const AudioValueRange& y, AudioValueRange& outRange)
155 {
156         bool isDisjoint;
157         if(!IsStrictlyLessThan(x, y) && !IsStrictlyGreaterThan(x, y))
158         {
159                 outRange.mMinimum = std::min(x.mMinimum, y.mMinimum);
160                 outRange.mMaximum = std::max(x.mMaximum, y.mMaximum);
161                 isDisjoint = false;
162         }
163         else
164         {
165                 outRange.mMinimum = 0;
166                 outRange.mMaximum = 0;
167                 isDisjoint = true;
168         }
169         return isDisjoint;
170 }
171
172 void    CAAudioValueRange_ComputeUnion(const AudioValueRange& inRange, const CAAudioValueRangeList& inRangeList, CAAudioValueRangeList& outUnion)
173 {
174         //      this method assumes that the ranges in inRangeList are disjoint and that they are sorted from low to high and
175         outUnion.clear();
176
177         //      start at the beginning of inRangeList
178         CAAudioValueRangeList::const_iterator theIterator = inRangeList.begin();
179
180         //      iterate through inRangeList and add all the ranges that are strictly less than inRange
181         while((theIterator != inRangeList.end()) && CAAudioValueRange::IsStrictlyLessThan(*theIterator, inRange))
182         {
183                 //      put this range in the union
184                 outUnion.push_back(*theIterator);
185
186                 //      go to the next one
187                 std::advance(theIterator, 1);
188         }
189
190         if(theIterator != inRangeList.end())
191         {
192                 if(!CAAudioValueRange::IsStrictlyGreaterThan(*theIterator, inRange))
193                 {
194                         //      inRange intersects the range that theIterator points at, but might actually intersect several contiguous ranges
195
196                         //      initialize the starting point, noting that we can skip the current one since we already know it's in the intersection
197                         CAAudioValueRangeList::const_iterator theGreaterIterator = theIterator;
198                         std::advance(theGreaterIterator, 1);
199
200                         //      iterate until we find a range that is strictly greater than inRange
201                         while((theGreaterIterator != inRangeList.end()) && !CAAudioValueRange::IsStrictlyGreaterThan(*theGreaterIterator, inRange))
202                         {
203                                 //      go to the next one
204                                 std::advance(theGreaterIterator, 1);
205                         }
206
207                         //      theGreaterIterator now points at either one past the highest range in the intersection or the end of the vector
208                         //      Either way, we have to adjust it to point at the true highest range in the intersection
209                         std::advance(theGreaterIterator, -1);
210
211                         //      now theIterator points at the lowest range in the intersection and theGreaterIterator points at the highest
212                         //      so we can compute the coagulated range
213                         AudioValueRange theCoagulation;
214                         theCoagulation.mMinimum = std::min(theIterator->mMinimum, inRange.mMinimum);
215                         theCoagulation.mMaximum = std::max(theGreaterIterator->mMaximum, inRange.mMaximum);
216
217                         //      add the coagulation to the union
218                         outUnion.push_back(theCoagulation);
219
220                         //      adjust theIterator to point at the next range for processing
221                         theIterator = theGreaterIterator;
222                         std::advance(theIterator, 1);
223                 }
224                 else
225                 {
226                         //      the range theIterator points at is strictly greater than inRange, so insert inRange in front of it and we're done
227                         outUnion.push_back(inRange);
228                 }
229
230                 //      we need to now copy the remaining higher ranges in inRangeList into the union
231                 while(theIterator != inRangeList.end())
232                 {
233                         //      put this range in the union
234                         outUnion.push_back(*theIterator);
235
236                         //      go to the next one
237                         std::advance(theIterator, 1);
238                 }
239         }
240         else
241         {
242                 //      inRange is larger than all of the ranges in inRangeList, so just add it onto the end of the union and we're done
243                 //      This is also the case if inRangeList is empty
244                 outUnion.push_back(inRange);
245         }
246 }
247
248 void    CAAudioValueRange_ComputeIntersection(UInt32 inNumberRangeList1Items, AudioValueRange inRangeList1[], const CAAudioValueRangeList& inRangeList2, CAAudioValueRangeList& outIntersections)
249 {
250         outIntersections.clear();
251         for(UInt32 theRangeList1Index = 0; theRangeList1Index < inNumberRangeList1Items; ++theRangeList1Index)
252         {
253                 for(CAAudioValueRangeList::const_iterator theRangeList2Iterator = inRangeList2.begin(); theRangeList2Iterator != inRangeList2.end(); std::advance(theRangeList2Iterator, 1))
254                 {
255                         AudioValueRange theIntersection;
256                         if(CAAudioValueRange::Intersection(inRangeList1[theRangeList1Index], *theRangeList2Iterator, theIntersection))
257                         {
258                                 outIntersections.push_back(theIntersection);
259                         }
260                 }
261         }
262 }