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