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
23 namespace android {
24
volIndexToDb(int indexInUi,int volIndexMin,int volIndexMax) const25 float VolumeCurve::volIndexToDb(int indexInUi, int volIndexMin, int volIndexMax) const
26 {
27 ALOG_ASSERT(!mCurvePoints.isEmpty(), "Invalid volume curve");
28
29 size_t nbCurvePoints = mCurvePoints.size();
30 // the volume index in the UI is relative to the min and max volume indices for this stream
31 int nbSteps = 1 + mCurvePoints[nbCurvePoints - 1].mIndex - mCurvePoints[0].mIndex;
32 if (indexInUi < volIndexMin) {
33 ALOGV("VOLUME remapping index from %d to min index %d", indexInUi, volIndexMin);
34 indexInUi = volIndexMin;
35 } else if (indexInUi > volIndexMax) {
36 ALOGV("VOLUME remapping index from %d to max index %d", indexInUi, volIndexMax);
37 indexInUi = volIndexMax;
38 }
39 int volIdx = (nbSteps * (indexInUi - volIndexMin)) / (volIndexMax - volIndexMin);
40
41 // Where would this volume index been inserted in the curve point
42 size_t indexInUiPosition = mCurvePoints.orderOf(CurvePoint(volIdx, 0));
43 if (indexInUiPosition >= nbCurvePoints) {
44 //use last point of table
45 return mCurvePoints[nbCurvePoints - 1].mAttenuationInMb / 100.0f;
46 }
47 if (indexInUiPosition == 0) {
48 if (indexInUiPosition != mCurvePoints[0].mIndex) {
49 return VOLUME_MIN_DB; // out of bounds
50 }
51 return mCurvePoints[0].mAttenuationInMb / 100.0f;
52 }
53 // linear interpolation in the attenuation table in dB
54 float decibels = (mCurvePoints[indexInUiPosition - 1].mAttenuationInMb / 100.0f) +
55 ((float)(volIdx - mCurvePoints[indexInUiPosition - 1].mIndex)) *
56 ( ((mCurvePoints[indexInUiPosition].mAttenuationInMb / 100.0f) -
57 (mCurvePoints[indexInUiPosition - 1].mAttenuationInMb / 100.0f)) /
58 ((float)(mCurvePoints[indexInUiPosition].mIndex -
59 mCurvePoints[indexInUiPosition - 1].mIndex)) );
60
61 ALOGV("VOLUME mDeviceCategory %d, mStreamType %d vol index=[%d %d %d], dB=[%.1f %.1f %.1f]",
62 mDeviceCategory, mStreamType,
63 mCurvePoints[indexInUiPosition - 1].mIndex, volIdx,
64 mCurvePoints[indexInUiPosition].mIndex,
65 ((float)mCurvePoints[indexInUiPosition - 1].mAttenuationInMb / 100.0f), decibels,
66 ((float)mCurvePoints[indexInUiPosition].mAttenuationInMb / 100.0f));
67
68 return decibels;
69 }
70
dump(int fd) const71 void VolumeCurve::dump(int fd) const
72 {
73 const size_t SIZE = 256;
74 char buffer[SIZE];
75 String8 result;
76 snprintf(buffer, SIZE, " {");
77 result.append(buffer);
78 for (size_t i = 0; i < mCurvePoints.size(); i++) {
79 snprintf(buffer, SIZE, "(%3d, %5d)",
80 mCurvePoints[i].mIndex, mCurvePoints[i].mAttenuationInMb);
81 result.append(buffer);
82 result.append(i == (mCurvePoints.size() - 1) ? " }\n" : ", ");
83 }
84 write(fd, result.string(), result.size());
85 }
86
dump(int fd,int spaces=0,bool curvePoints) const87 void VolumeCurvesForStream::dump(int fd, int spaces = 0, bool curvePoints) const
88 {
89 const size_t SIZE = 256;
90 char buffer[SIZE];
91 String8 result;
92
93 if (!curvePoints) {
94 snprintf(buffer, SIZE, "%s %02d %02d ",
95 mCanBeMuted ? "true " : "false", mIndexMin, mIndexMax);
96 result.append(buffer);
97 for (size_t i = 0; i < mIndexCur.size(); i++) {
98 snprintf(buffer, SIZE, "%04x : %02d, ", mIndexCur.keyAt(i), mIndexCur.valueAt(i));
99 result.append(buffer);
100 }
101 result.append("\n");
102 write(fd, result.string(), result.size());
103 return;
104 }
105
106 for (size_t i = 0; i < size(); i++) {
107 std::string deviceCatLiteral;
108 DeviceCategoryConverter::toString(keyAt(i), deviceCatLiteral);
109 snprintf(buffer, SIZE, "%*s %s :",
110 spaces, "", deviceCatLiteral.c_str());
111 write(fd, buffer, strlen(buffer));
112 valueAt(i)->dump(fd);
113 }
114 result.append("\n");
115 write(fd, result.string(), result.size());
116 }
117
dump(int fd) const118 status_t VolumeCurvesCollection::dump(int fd) const
119 {
120 const size_t SIZE = 256;
121 char buffer[SIZE];
122
123 snprintf(buffer, SIZE, "\nStreams dump:\n");
124 write(fd, buffer, strlen(buffer));
125 snprintf(buffer, SIZE,
126 " Stream Can be muted Index Min Index Max Index Cur [device : index]...\n");
127 write(fd, buffer, strlen(buffer));
128 for (size_t i = 0; i < size(); i++) {
129 snprintf(buffer, SIZE, " %02zu ", i);
130 write(fd, buffer, strlen(buffer));
131 valueAt(i).dump(fd);
132 }
133 snprintf(buffer, SIZE, "\nVolume Curves for Use Cases (aka Stream types) dump:\n");
134 write(fd, buffer, strlen(buffer));
135 for (size_t i = 0; i < size(); i++) {
136 std::string streamTypeLiteral;
137 StreamTypeConverter::toString(keyAt(i), streamTypeLiteral);
138 snprintf(buffer, SIZE,
139 " %s (%02zu): Curve points for device category (index, attenuation in millibel)\n",
140 streamTypeLiteral.c_str(), i);
141 write(fd, buffer, strlen(buffer));
142 valueAt(i).dump(fd, 2, true);
143 }
144
145 return NO_ERROR;
146 }
147
148 } // namespace android
149