• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #define LOG_TAG "APM::VolumeCurve"
18 //#define LOG_NDEBUG 0
19 
20 #include "VolumeCurve.h"
21 #include "TypeConverter.h"
22 #include <media/TypeConverter.h>
23 
24 namespace android {
25 
volIndexToDb(int indexInUi,int volIndexMin,int volIndexMax) const26 float VolumeCurve::volIndexToDb(int indexInUi, int volIndexMin, int volIndexMax) const
27 {
28     ALOG_ASSERT(!mCurvePoints.isEmpty(), "Invalid volume curve");
29     if (volIndexMin < 0 || volIndexMax < 0) {
30         // In order to let AudioService initialize the min and max, convention is to use -1
31         return NAN;
32     }
33     if (indexInUi < volIndexMin) {
34         // an index of 0 means mute request when volIndexMin > 0
35         if (indexInUi == 0) {
36             ALOGV("VOLUME forcing mute for index 0 with min index %d", volIndexMin);
37             return VOLUME_MIN_DB;
38         }
39         ALOGV("VOLUME remapping index from %d to min index %d", indexInUi, volIndexMin);
40         indexInUi = volIndexMin;
41     } else if (indexInUi > volIndexMax) {
42         ALOGV("VOLUME remapping index from %d to max index %d", indexInUi, volIndexMax);
43         indexInUi = volIndexMax;
44     }
45 
46     size_t nbCurvePoints = mCurvePoints.size();
47     // the volume index in the UI is relative to the min and max volume indices for this stream
48     int nbSteps = 1 + mCurvePoints[nbCurvePoints - 1].mIndex - mCurvePoints[0].mIndex;
49     int volIdx = (nbSteps * (indexInUi - volIndexMin)) / (volIndexMax - volIndexMin);
50 
51     // Where would this volume index been inserted in the curve point
52     size_t indexInUiPosition = mCurvePoints.orderOf(CurvePoint(volIdx, 0));
53     if (indexInUiPosition >= nbCurvePoints) {
54         //use last point of table
55         return mCurvePoints[nbCurvePoints - 1].mAttenuationInMb / 100.0f;
56     }
57     if (indexInUiPosition == 0) {
58         if (indexInUiPosition != mCurvePoints[0].mIndex) {
59             return VOLUME_MIN_DB; // out of bounds
60         }
61         return mCurvePoints[0].mAttenuationInMb / 100.0f;
62     }
63     // linear interpolation in the attenuation table in dB
64     float decibels = (mCurvePoints[indexInUiPosition - 1].mAttenuationInMb / 100.0f) +
65             ((float)(volIdx - mCurvePoints[indexInUiPosition - 1].mIndex)) *
66                 ( ((mCurvePoints[indexInUiPosition].mAttenuationInMb / 100.0f) -
67                         (mCurvePoints[indexInUiPosition - 1].mAttenuationInMb / 100.0f)) /
68                     ((float)(mCurvePoints[indexInUiPosition].mIndex -
69                             mCurvePoints[indexInUiPosition - 1].mIndex)) );
70 
71     ALOGV("VOLUME vol index=[%d %d %d], dB=[%.1f %.1f %.1f]",
72             mCurvePoints[indexInUiPosition - 1].mIndex, volIdx,
73             mCurvePoints[indexInUiPosition].mIndex,
74             ((float)mCurvePoints[indexInUiPosition - 1].mAttenuationInMb / 100.0f), decibels,
75             ((float)mCurvePoints[indexInUiPosition].mAttenuationInMb / 100.0f));
76 
77     return decibels;
78 }
79 
dump(String8 * dst,int spaces,bool curvePoints) const80 void VolumeCurve::dump(String8 *dst, int spaces, bool curvePoints) const
81 {
82     if (!curvePoints) {
83         return;
84     }
85     dst->append(" {");
86     for (size_t i = 0; i < mCurvePoints.size(); i++) {
87         dst->appendFormat("%*s(%3d, %5d)", spaces, "", mCurvePoints[i].mIndex,
88                           mCurvePoints[i].mAttenuationInMb);
89         dst->appendFormat(i == (mCurvePoints.size() - 1) ? " }\n" : ", ");
90     }
91 }
92 
dump(String8 * dst,int spaces,bool curvePoints) const93 void VolumeCurves::dump(String8 *dst, int spaces, bool curvePoints) const
94 {
95     if (!curvePoints) {
96 //        dst->appendFormat("%*s%02d      %s         %03d        %03d        ", spaces, "",
97 //                          mStream, mCanBeMuted ? "true " : "false", mIndexMin, mIndexMax);
98         dst->appendFormat("%*s Can be muted  Index Min  Index Max  Index Cur [device : index]...\n",
99                           spaces + 1, "");
100         dst->appendFormat("%*s %s         %02d         %02d         ", spaces + 1, "",
101                           mCanBeMuted ? "true " : "false", mIndexMin, mIndexMax);
102         for (const auto &pair : mIndexCur) {
103             dst->appendFormat("%04x : %02d, ", pair.first, pair.second);
104         }
105         dst->appendFormat("\n");
106         return;
107     }
108     std::string streamNames;
109     for (const auto &stream : mStreams) {
110         streamNames += android::toString(stream) + "("+std::to_string(stream)+") ";
111     }
112     dst->appendFormat("%*sVolume Curves Streams/Attributes, Curve points Streams for device"
113                       " category (index, attenuation in millibel)\n", spaces, "");
114     dst->appendFormat("%*s Streams: %s \n", spaces, "", streamNames.c_str());
115     if (!mAttributes.empty()) dst->appendFormat("%*s Attributes:", spaces, "");
116     for (const auto &attributes : mAttributes) {
117         std::string attStr = attributes == defaultAttr ? "{ Any }" : android::toString(attributes);
118         dst->appendFormat("%*s %s\n", attributes == mAttributes.front() ? 0 : spaces + 13, "",
119                           attStr.c_str());
120     }
121     for (size_t i = 0; i < size(); i++) {
122         std::string deviceCatLiteral;
123         DeviceCategoryConverter::toString(keyAt(i), deviceCatLiteral);
124         dst->appendFormat("%*s %s :", spaces, "", deviceCatLiteral.c_str());
125         valueAt(i)->dump(dst, 1, true);
126     }
127 }
128 
129 } // namespace android
130