Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit ffbc80f5 authored by Eric Laurent's avatar Eric Laurent
Browse files

audio policy: volume in dBs

Volumes are now stored and exchanged in dBs to be
consistent with the volume tables.

They are converted if needed when applied to the target
gain controller.

Change-Id: I916cbb0bbe65c916444b8c65a2bdca9645c58399
parent c75307b7
Loading
Loading
Loading
Loading
+21 −1
Original line number Diff line number Diff line
@@ -18,6 +18,10 @@

#include <system/audio.h>
#include <utils/Log.h>
#include <math.h>

// Absolute min volume in dB (can be represented in single precision normal float value)
#define VOLUME_MIN_DB (-758)

class VolumeCurvePoint
{
@@ -32,7 +36,7 @@ public:
    /**
     * 4 points to define the volume attenuation curve, each characterized by the volume
     * index (from 0 to 100) at which they apply, and the attenuation in dB at that index.
     * we use 100 steps to avoid rounding errors when computing the volume in volIndexToAmpl()
     * we use 100 steps to avoid rounding errors when computing the volume in volIndexToDb()
     *
     * @todo shall become configurable
     */
@@ -134,4 +138,20 @@ public:
        }
    }

    static inline float DbToAmpl(float decibels)
    {
        if (decibels <= VOLUME_MIN_DB) {
            return 0.0f;
        }
        return exp( decibels * 0.115129f); // exp( dB * ln(10) / 20 )
    }

    static inline float AmplToDb(float amplification)
    {
        if (amplification == 0) {
            return VOLUME_MIN_DB;
        }
        return 20 * log10(amplification);
    }

};
+1 −1
Original line number Diff line number Diff line
@@ -74,7 +74,7 @@ public:
    audio_patch_handle_t mPatchHandle;
    uint32_t mRefCount[AUDIO_STREAM_CNT]; // number of streams of each type using this output
    nsecs_t mStopTime[AUDIO_STREAM_CNT];
    float mCurVolume[AUDIO_STREAM_CNT];   // current stream volume
    float mCurVolume[AUDIO_STREAM_CNT];   // current stream volume in dB
    int mMuteCount[AUDIO_STREAM_CNT];     // mute request counter
    bool mStrategyMutedByDevice[NUM_STRATEGIES]; // strategies muted because of incompatible
                                        // device selection. See checkDeviceMuteStrategies()
+4 −2
Original line number Diff line number Diff line
@@ -21,6 +21,7 @@
#include "AudioOutputDescriptor.h"
#include "IOProfile.h"
#include "AudioGain.h"
#include "Volume.h"
#include "HwModule.h"
#include <media/AudioPolicy.h>

@@ -354,11 +355,12 @@ bool SwAudioOutputDescriptor::setVolume(float volume,
    if (changed) {
        // Force VOICE_CALL to track BLUETOOTH_SCO stream volume when bluetooth audio is
        // enabled
        float volume = Volume::DbToAmpl(mCurVolume[stream]);
        if (stream == AUDIO_STREAM_BLUETOOTH_SCO) {
            mClientInterface->setStreamVolume(
                    AUDIO_STREAM_VOICE_CALL, mCurVolume[stream], mIoHandle, delayMs);
                    AUDIO_STREAM_VOICE_CALL, volume, mIoHandle, delayMs);
        }
        mClientInterface->setStreamVolume(stream, mCurVolume[stream], mIoHandle, delayMs);
        mClientInterface->setStreamVolume(stream, volume, mIoHandle, delayMs);
    }
    return changed;
}
+3 −3
Original line number Diff line number Diff line
@@ -134,16 +134,16 @@ public:
                                              audio_policy_dev_state_t state) = 0;

    /**
     * Translate a volume index given by the UI to an amplification value for a stream type
     * Translate a volume index given by the UI to an amplification value in dB for a stream type
     * and a device category.
     *
     * @param[in] deviceCategory for which the conversion is requested.
     * @param[in] stream type for which the conversion is requested.
     * @param[in] indexInUi index received from the UI to be translated.
     *
     * @return amplification value matching the UI index for this given device and stream.
     * @return amplification value in dB matching the UI index for this given device and stream.
     */
    virtual float volIndexToAmpl(Volume::device_category deviceCategory, audio_stream_type_t stream,
    virtual float volIndexToDb(Volume::device_category deviceCategory, audio_stream_type_t stream,
                                 int indexInUi) = 0;

    /**
+3 −2
Original line number Diff line number Diff line
@@ -63,13 +63,14 @@ status_t Engine::initCheck()
    return (mApmObserver != NULL) ?  NO_ERROR : NO_INIT;
}

float Engine::volIndexToAmpl(Volume::device_category category, audio_stream_type_t streamType,
float Engine::volIndexToDb(Volume::device_category category, audio_stream_type_t streamType,
                             int indexInUi)
{
    const StreamDescriptor &streamDesc = mApmObserver->getStreamDescriptors().valueAt(streamType);
    return Gains::volIndexToAmpl(category, streamDesc, indexInUi);
    return Gains::volIndexToDb(category, streamDesc, indexInUi);
}


status_t Engine::initStreamVolume(audio_stream_type_t stream, int indexMin, int indexMax)
{
    ALOGV("initStreamVolume() stream %d, min %d, max %d", stream , indexMin, indexMax);
Loading