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 // Calculate the new volume index
47 size_t nbCurvePoints = mCurvePoints.size();
48
49 int volIdx;
50 if (volIndexMin == volIndexMax) {
51 if (indexInUi == volIndexMin) {
52 volIdx = volIndexMin;
53 } else {
54 // This would result in a divide-by-zero below
55 ALOG_ASSERT(volIndexmin != volIndexMax, "Invalid volume index range & value: 0");
56 return NAN;
57 }
58 } else {
59 // interpolaate
60 // the volume index in the UI is relative to the min and max volume indices for this stream
61 int nbSteps = 1 + mCurvePoints[nbCurvePoints - 1].mIndex - mCurvePoints[0].mIndex;
62 volIdx = (nbSteps * (indexInUi - volIndexMin)) / (volIndexMax - volIndexMin);
63 }
64
65 // Where would this volume index been inserted in the curve point
66 size_t indexInUiPosition = mCurvePoints.orderOf(CurvePoint(volIdx, 0));
67 if (indexInUiPosition >= nbCurvePoints) {
68 //use last point of table
69 return mCurvePoints[nbCurvePoints - 1].mAttenuationInMb / 100.0f;
70 }
71 if (indexInUiPosition == 0) {
72 if (indexInUiPosition != mCurvePoints[0].mIndex) {
73 return VOLUME_MIN_DB; // out of bounds
74 }
75 return mCurvePoints[0].mAttenuationInMb / 100.0f;
76 }
77 // linear interpolation in the attenuation table in dB
78 float decibels = (mCurvePoints[indexInUiPosition - 1].mAttenuationInMb / 100.0f) +
79 ((float)(volIdx - mCurvePoints[indexInUiPosition - 1].mIndex)) *
80 ( ((mCurvePoints[indexInUiPosition].mAttenuationInMb / 100.0f) -
81 (mCurvePoints[indexInUiPosition - 1].mAttenuationInMb / 100.0f)) /
82 ((float)(mCurvePoints[indexInUiPosition].mIndex -
83 mCurvePoints[indexInUiPosition - 1].mIndex)) );
84
85 ALOGV("VOLUME vol index=[%d %d %d], dB=[%.1f %.1f %.1f]",
86 mCurvePoints[indexInUiPosition - 1].mIndex, volIdx,
87 mCurvePoints[indexInUiPosition].mIndex,
88 ((float)mCurvePoints[indexInUiPosition - 1].mAttenuationInMb / 100.0f), decibels,
89 ((float)mCurvePoints[indexInUiPosition].mAttenuationInMb / 100.0f));
90
91 return decibels;
92 }
93
dump(String8 * dst,int spaces,bool curvePoints) const94 void VolumeCurve::dump(String8 *dst, int spaces, bool curvePoints) const
95 {
96 if (!curvePoints) {
97 return;
98 }
99 dst->append(" {");
100 for (size_t i = 0; i < mCurvePoints.size(); i++) {
101 dst->appendFormat("%*s(%3d, %5d)", spaces, "", mCurvePoints[i].mIndex,
102 mCurvePoints[i].mAttenuationInMb);
103 dst->appendFormat(i == (mCurvePoints.size() - 1) ? " }\n" : ", ");
104 }
105 }
106
dump(String8 * dst,int spaces,bool curvePoints) const107 void VolumeCurves::dump(String8 *dst, int spaces, bool curvePoints) const
108 {
109 if (!curvePoints) {
110 // dst->appendFormat("%*s%02d %s %03d %03d ", spaces, "",
111 // mStream, mCanBeMuted ? "true " : "false", mIndexMin, mIndexMax);
112 dst->appendFormat("%*s Can be muted Index Min Index Max Index Cur [device : index]...\n",
113 spaces + 1, "");
114 dst->appendFormat("%*s %s %02d %02d ", spaces + 1, "",
115 mCanBeMuted ? "true " : "false", mIndexMin, mIndexMax);
116 for (const auto &pair : mIndexCur) {
117 dst->appendFormat("%04x : %02d, ", pair.first, pair.second);
118 }
119 dst->appendFormat("\n");
120 return;
121 }
122 std::string streamNames;
123 for (const auto &stream : mStreams) {
124 streamNames += android::toString(stream) + "("+std::to_string(stream)+") ";
125 }
126 dst->appendFormat("%*sVolume Curves Streams/Attributes, Curve points Streams for device"
127 " category (index, attenuation in millibel)\n", spaces, "");
128 dst->appendFormat("%*s Streams: %s \n", spaces, "", streamNames.c_str());
129 if (!mAttributes.empty()) dst->appendFormat("%*s Attributes:", spaces, "");
130 for (const auto &attributes : mAttributes) {
131 std::string attStr = attributes == defaultAttr ? "{ Any }" : android::toString(attributes);
132 dst->appendFormat("%*s %s\n", attributes == mAttributes.front() ? 0 : spaces + 13, "",
133 attStr.c_str());
134 }
135 for (size_t i = 0; i < size(); i++) {
136 std::string deviceCatLiteral;
137 DeviceCategoryConverter::toString(keyAt(i), deviceCatLiteral);
138 dst->appendFormat("%*s %s :", spaces, "", deviceCatLiteral.c_str());
139 valueAt(i)->dump(dst, 1, true);
140 }
141 }
142
143 } // namespace android
144