Jamoma API  0.6.0.a19
CAAudioValueRange.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 "CAAudioValueRange.h"
47 
48 // Standard Library
49 #include <algorithm>
50 
51 //==================================================================================================
52 // CAAudioValueRange
53 //==================================================================================================
54 
55 Float64 CAAudioValueRange::PickCommonSampleRate(const AudioValueRange& inRange)
56 {
57  // This routine will pick a "common" sample rate from the give range of rates or the maximum
58  // if no common rates can be found. It assumes that inRange contains a continuous range of
59  // sample rates.
60  Float64 theAnswer = inRange.mMaximum;
61 
62  if(ContainsValue(inRange, 44100.0))
63  {
64  theAnswer = 44100.0;
65  }
66  else if(ContainsValue(inRange, 48000.0))
67  {
68  theAnswer = 48000.0;
69  }
70  else if(ContainsValue(inRange, 96000.0))
71  {
72  theAnswer = 96000.0;
73  }
74  else if(ContainsValue(inRange, 88200.0))
75  {
76  theAnswer = 88200.0;
77  }
78  else if(ContainsValue(inRange, 64000.0))
79  {
80  theAnswer = 64000.0;
81  }
82  else if(ContainsValue(inRange, 32000.0))
83  {
84  theAnswer = 32000.0;
85  }
86  else if(ContainsValue(inRange, 24000.0))
87  {
88  theAnswer = 24000.0;
89  }
90  else if(ContainsValue(inRange, 22050.0))
91  {
92  theAnswer = 22050.0;
93  }
94  else if(ContainsValue(inRange, 16000.0))
95  {
96  theAnswer = 16000.0;
97  }
98  else if(ContainsValue(inRange, 12000.0))
99  {
100  theAnswer = 12000.0;
101  }
102  else if(ContainsValue(inRange, 11025.0))
103  {
104  theAnswer = 11025.0;
105  }
106  else if(ContainsValue(inRange, 8000.0))
107  {
108  theAnswer = 8000.0;
109  }
110 
111  return theAnswer;
112 }
113 
114 bool CAAudioValueRange::Intersection(const AudioValueRange& x, const AudioValueRange& y, AudioValueRange& outRange)
115 {
116  bool isNonEmpty;
117  if(!IsStrictlyLessThan(x, y) && !IsStrictlyGreaterThan(x, y))
118  {
119  outRange.mMinimum = std::max(x.mMinimum, y.mMinimum);
120  outRange.mMaximum = std::min(x.mMaximum, y.mMaximum);
121  isNonEmpty = true;
122  }
123  else
124  {
125  outRange.mMinimum = 0;
126  outRange.mMaximum = 0;
127  isNonEmpty = false;
128  }
129  return isNonEmpty;
130 }
131 
132 bool CAAudioValueRange::Union(const AudioValueRange& x, const AudioValueRange& y, AudioValueRange& outRange)
133 {
134  bool isDisjoint;
135  if(!IsStrictlyLessThan(x, y) && !IsStrictlyGreaterThan(x, y))
136  {
137  outRange.mMinimum = std::min(x.mMinimum, y.mMinimum);
138  outRange.mMaximum = std::max(x.mMaximum, y.mMaximum);
139  isDisjoint = false;
140  }
141  else
142  {
143  outRange.mMinimum = 0;
144  outRange.mMaximum = 0;
145  isDisjoint = true;
146  }
147  return isDisjoint;
148 }
149 
150 void CAAudioValueRange_ComputeUnion(const AudioValueRange& inRange, const CAAudioValueRangeList& inRangeList, CAAudioValueRangeList& outUnion)
151 {
152  // this method assumes that the ranges in inRangeList are disjoint and that they are sorted from low to high and
153  outUnion.clear();
154 
155  // start at the beginning of inRangeList
156  CAAudioValueRangeList::const_iterator theIterator = inRangeList.begin();
157 
158  // iterate through inRangeList and add all the ranges that are strictly less than inRange
159  while((theIterator != inRangeList.end()) && CAAudioValueRange::IsStrictlyLessThan(*theIterator, inRange))
160  {
161  // put this range in the union
162  outUnion.push_back(*theIterator);
163 
164  // go to the next one
165  std::advance(theIterator, 1);
166  }
167 
168  if(theIterator != inRangeList.end())
169  {
170  if(!CAAudioValueRange::IsStrictlyGreaterThan(*theIterator, inRange))
171  {
172  // inRange intersects the range that theIterator points at, but might actually intersect several contiguous ranges
173 
174  // initialize the starting point, noting that we can skip the current one since we already know it's in the intersection
175  CAAudioValueRangeList::const_iterator theGreaterIterator = theIterator;
176  std::advance(theGreaterIterator, 1);
177 
178  // iterate until we find a range that is strictly greater than inRange
179  while((theGreaterIterator != inRangeList.end()) && !CAAudioValueRange::IsStrictlyGreaterThan(*theGreaterIterator, inRange))
180  {
181  // go to the next one
182  std::advance(theGreaterIterator, 1);
183  }
184 
185  // theGreaterIterator now points at either one past the highest range in the intersection or the end of the vector
186  // Either way, we have to adjust it to point at the true highest range in the intersection
187  std::advance(theGreaterIterator, -1);
188 
189  // now theIterator points at the lowest range in the intersection and theGreaterIterator points at the highest
190  // so we can compute the coagulated range
191  AudioValueRange theCoagulation;
192  theCoagulation.mMinimum = std::min(theIterator->mMinimum, inRange.mMinimum);
193  theCoagulation.mMaximum = std::max(theGreaterIterator->mMaximum, inRange.mMaximum);
194 
195  // add the coagulation to the union
196  outUnion.push_back(theCoagulation);
197 
198  // adjust theIterator to point at the next range for processing
199  theIterator = theGreaterIterator;
200  std::advance(theIterator, 1);
201  }
202  else
203  {
204  // the range theIterator points at is strictly greater than inRange, so insert inRange in front of it and we're done
205  outUnion.push_back(inRange);
206  }
207 
208  // we need to now copy the remaining higher ranges in inRangeList into the union
209  while(theIterator != inRangeList.end())
210  {
211  // put this range in the union
212  outUnion.push_back(*theIterator);
213 
214  // go to the next one
215  std::advance(theIterator, 1);
216  }
217  }
218  else
219  {
220  // inRange is larger than all of the ranges in inRangeList, so just add it onto the end of the union and we're done
221  // This is also the case if inRangeList is empty
222  outUnion.push_back(inRange);
223  }
224 }
225 
226 void CAAudioValueRange_ComputeIntersection(const AudioValueRange& inRange, const CAAudioValueRangeList& inRangeList, CAAudioValueRangeList& outIntersections)
227 {
228  outIntersections.clear();
229  // iterate through the list and compute the intersections
230  CAAudioValueRangeList::const_iterator theIterator = inRangeList.begin();
231  while(theIterator != inRangeList.end())
232  {
233  // figure out if the range intersects
234  AudioValueRange theIntersection;
235  if(CAAudioValueRange::Intersection(inRange, *theIterator, theIntersection))
236  {
237  // it does, so add the intersection to the return list
238  outIntersections.push_back(theIntersection);
239  }
240 
241  // go to the next one
242  std::advance(theIterator, 1);
243  }
244 }