2 File: CAAudioValueRange.cpp
3 Abstract: CAAudioValueRange.h
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.
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.
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.
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.
44 Copyright (C) 2014 Apple Inc. All Rights Reserved.
47 //==================================================================================================
49 //==================================================================================================
52 #include "CAAudioValueRange.h"
57 //==================================================================================================
59 //==================================================================================================
61 Float64 CAAudioValueRange::BoundValue(const AudioValueRange& inRange, Float64 inValue)
63 if (inValue <= inRange.mMinimum)
65 return inRange.mMinimum;
67 else if (inValue >= inRange.mMaximum)
69 return inRange.mMaximum;
77 Float64 CAAudioValueRange::PickCommonSampleRate(const AudioValueRange& inRange)
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
82 Float64 theAnswer = inRange.mMaximum;
84 if(ContainsValue(inRange, 44100.0))
88 else if(ContainsValue(inRange, 48000.0))
92 else if(ContainsValue(inRange, 96000.0))
96 else if(ContainsValue(inRange, 88200.0))
100 else if(ContainsValue(inRange, 64000.0))
104 else if(ContainsValue(inRange, 32000.0))
108 else if(ContainsValue(inRange, 24000.0))
112 else if(ContainsValue(inRange, 22050.0))
116 else if(ContainsValue(inRange, 16000.0))
120 else if(ContainsValue(inRange, 12000.0))
124 else if(ContainsValue(inRange, 11025.0))
128 else if(ContainsValue(inRange, 8000.0))
136 bool CAAudioValueRange::Intersection(const AudioValueRange& x, const AudioValueRange& y, AudioValueRange& outRange)
139 if(!IsStrictlyLessThan(x, y) && !IsStrictlyGreaterThan(x, y))
141 outRange.mMinimum = std::max(x.mMinimum, y.mMinimum);
142 outRange.mMaximum = std::min(x.mMaximum, y.mMaximum);
147 outRange.mMinimum = 0;
148 outRange.mMaximum = 0;
154 bool CAAudioValueRange::Union(const AudioValueRange& x, const AudioValueRange& y, AudioValueRange& outRange)
157 if(!IsStrictlyLessThan(x, y) && !IsStrictlyGreaterThan(x, y))
159 outRange.mMinimum = std::min(x.mMinimum, y.mMinimum);
160 outRange.mMaximum = std::max(x.mMaximum, y.mMaximum);
165 outRange.mMinimum = 0;
166 outRange.mMaximum = 0;
172 void CAAudioValueRange_ComputeUnion(const AudioValueRange& inRange, const CAAudioValueRangeList& inRangeList, CAAudioValueRangeList& outUnion)
174 // this method assumes that the ranges in inRangeList are disjoint and that they are sorted from low to high and
177 // start at the beginning of inRangeList
178 CAAudioValueRangeList::const_iterator theIterator = inRangeList.begin();
180 // iterate through inRangeList and add all the ranges that are strictly less than inRange
181 while((theIterator != inRangeList.end()) && CAAudioValueRange::IsStrictlyLessThan(*theIterator, inRange))
183 // put this range in the union
184 outUnion.push_back(*theIterator);
186 // go to the next one
187 std::advance(theIterator, 1);
190 if(theIterator != inRangeList.end())
192 if(!CAAudioValueRange::IsStrictlyGreaterThan(*theIterator, inRange))
194 // inRange intersects the range that theIterator points at, but might actually intersect several contiguous ranges
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);
200 // iterate until we find a range that is strictly greater than inRange
201 while((theGreaterIterator != inRangeList.end()) && !CAAudioValueRange::IsStrictlyGreaterThan(*theGreaterIterator, inRange))
203 // go to the next one
204 std::advance(theGreaterIterator, 1);
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);
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);
217 // add the coagulation to the union
218 outUnion.push_back(theCoagulation);
220 // adjust theIterator to point at the next range for processing
221 theIterator = theGreaterIterator;
222 std::advance(theIterator, 1);
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);
230 // we need to now copy the remaining higher ranges in inRangeList into the union
231 while(theIterator != inRangeList.end())
233 // put this range in the union
234 outUnion.push_back(*theIterator);
236 // go to the next one
237 std::advance(theIterator, 1);
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);
248 void CAAudioValueRange_ComputeIntersection(UInt32 inNumberRangeList1Items, AudioValueRange inRangeList1[], const CAAudioValueRangeList& inRangeList2, CAAudioValueRangeList& outIntersections)
250 outIntersections.clear();
251 for(UInt32 theRangeList1Index = 0; theRangeList1Index < inNumberRangeList1Items; ++theRangeList1Index)
253 for(CAAudioValueRangeList::const_iterator theRangeList2Iterator = inRangeList2.begin(); theRangeList2Iterator != inRangeList2.end(); std::advance(theRangeList2Iterator, 1))
255 AudioValueRange theIntersection;
256 if(CAAudioValueRange::Intersection(inRangeList1[theRangeList1Index], *theRangeList2Iterator, theIntersection))
258 outIntersections.push_back(theIntersection);