1 /*
2  * Copyright (C) 2015 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.android.cts.verifier.audio;
18 
19 public class AudioBandSpecs {
20     double mFreqStart;
21     double mFreqStop;
22     double mRippleStartTop;
23     double mRippleStartBottom;
24 
25     double mRippleStopTop;
26     double mRippleStopBottom;
27 
28     double mOffset;
29 
AudioBandSpecs(double fStart, double fStop, double startTop, double startBottom, double stopTop, double stopBottom)30     public AudioBandSpecs(double fStart, double fStop, double startTop, double startBottom,
31             double stopTop, double stopBottom) {
32         initFreq(fStart, fStop);
33         initRipple(startTop, startBottom, stopTop, stopBottom);
34         setOffset(0);
35     }
36 
initRipple(double startTop, double startBottom, double stopTop, double stopBottom)37     public void initRipple(double startTop, double startBottom, double stopTop, double stopBottom) {
38         mRippleStartTop = startTop;
39         mRippleStartBottom = startBottom;
40         mRippleStopTop = stopTop;
41         mRippleStopBottom = stopBottom;
42         // note: top should be >= bottom, but no check is done here.
43     }
44 
initFreq(double fStart, double fStop)45     public void initFreq(double fStart, double fStop) {
46         mFreqStart = fStart;
47         mFreqStop = fStop;
48     }
49 
setOffset(double offset)50     public void setOffset(double offset) {
51         mOffset = offset;
52     }
53 
54     /**
55      * Check if the given point is in bounds in this band.
56      */
isInBounds(double freq, double value)57     public boolean isInBounds(double freq, double value) {
58         if (freq < mFreqStart || freq > mFreqStop) {
59             return false;
60         }
61 
62         double d = mFreqStop - mFreqStart;
63         if (d <= 0) {
64             return false;
65         }
66 
67         double e = freq - mFreqStart;
68         double vTop = (e / d) * (mRippleStopTop - mRippleStartTop) + mRippleStartTop + mOffset;
69         if (value > vTop) {
70             return false;
71         }
72 
73         double vBottom = (e / d) * (mRippleStopBottom - mRippleStartBottom) + mRippleStartBottom
74                 + mOffset;
75 
76         if (value < vBottom) {
77             return false;
78         }
79         return true;
80     }
81 
toString()82     public String toString() {
83         StringBuilder sb = new StringBuilder();
84         sb.append(String.format("Freq %.1f - %.1f |", mFreqStart, mFreqStop));
85         sb.append(String.format("start [%.1f : %.1f] |", mRippleStartTop, mRippleStartBottom));
86         sb.append(String.format("stop  [%.1f : %.1f] |", mRippleStopTop, mRippleStopBottom));
87         sb.append(String.format("offset %.1f", mOffset));
88         return sb.toString();
89     }
90 }