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