Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
您现在的位置: Android教程網 >> Android系統教程 >  >> Android 技術 >> Android 開發實例

Android 音量控制曲線的計算原理

編輯:Android 開發實例

本文介紹 Android 音量控制曲線的計算方法。

由於人耳對聲音的聽感呈指數曲線型——小音量時較為敏感,隨著聲音加大而變得遲鈍,變化近似指數函數——為了讓聽感趨近線性,實務上會採用對數式曲線的電位器來達成。音量調節介面上的滑桿刻度是線性的,這正好為我們生成音量控制曲線提供了良好的對比基礎。以下就透過一個音量調節的場景,分析 Android 是如何控制音量的。

首先,我們按音量鍵把 media 音量逐級調到最大。STREAM_MUSIC 流的音量分為 15 級,透過 AudioManager 的 handleKeyDown 呼叫 adjustSuggestedStreamVolume 設定,一路往下:AudioService 的 adjustSuggestedStreamVolume 呼叫 adjustStreamVolume,透過訊息 MSG_SET_SYSTEM_VOLUME 呼叫 setSystemVolume,轉到 AudioSystem 的 setStreamVolumeIndex,再經 JNI 層呼叫本地層的 AudioSystem、AudioPolicyManagerService,最後來到 AudioPolicyManagerBase 的 setStreamVolumeIndex,由 checkAndSetVolume 呼叫 computeVolume。真正計算音量的地方是 volIndexToAmpl 函式,一起來看:

float AudioPolicyManagerBase::volIndexToAmpl(audio_devices_t device, const StreamDescriptor& streamDesc,
        int indexInUi)
{
    device_category deviceCategory = getDeviceCategory(device);
    const VolumeCurvePoint *curve = streamDesc.mVolumeCurve[deviceCategory];

    // the volume index in the UI is relative to the min and max volume indices for this stream type
    int nbSteps = 1 + curve[VOLMAX].mIndex -
            curve[VOLMIN].mIndex; // 計算預置曲線區間的範圍,這裡是(1-100)
    int volIdx = (nbSteps * (indexInUi - streamDesc.mIndexMin)) /
            (streamDesc.mIndexMax - streamDesc.mIndexMin); // 由傳進來的 UIIndex 換算成百分比 index

    // find what part of the curve this index volume belongs to
    int segment = 0;
    if (volIdx < curve[VOLMIN].mIndex) {           // out of bounds
        return 0.0f;
    } else if (volIdx < curve[VOLKNEE1].mIndex) {
        segment = 0;
    } else if (volIdx < curve[VOLKNEE2].mIndex) {
        segment = 1;
    } else if (volIdx <= curve[VOLMAX].mIndex) {
        segment = 2;
    } else {                                        // out of bounds
        return 1.0f;
    }

    // linear interpolation in the attenuation table in dB
    float decibels = curve[segment].mDBAttenuation +
            ((float)(volIdx - curve[segment].mIndex)) *
                ((curve[segment+1].mDBAttenuation -
                    curve[segment].mDBAttenuation) /
                ((float)(curve[segment+1].mIndex -
                    curve[segment].mIndex)));       // 計算衰減的分貝數

    float amplification = exp(decibels * 0.115129f); // exp(dB * ln(10) / 20)
    // 由指數公式計算出音量 amplification
    return amplification;
}

函式會先算出 UI 上線性刻度對應的百分比 index,再依 index 落在哪個區間(segment)決定曲線段,然後在該段內以 dB 做線性內插求得衰減分貝數,最後用 exp(dB × ln(10) / 20) 換算成乘上揚聲器輸出的放大倍數。計算後得到一個介於 0 到 1 之間的 float 音量值,再透過 mpClientInterface->setStreamVolume 設定到 audioflinger 的 mStreamTypes[stream].value,並在 prepareTracks_l 中把音量傳入 audiomixer 混音。

以下六個附表是 AudioPolicyManagerBase 中預置的音量曲線 dB 分布表:

表 1-1 default volume curve:刻度 1 / 33 / 66 / 100 對應衰減 -49.5 / -33.5 / -17.0 / 0.0 dB

表 1-2 default media volume curve:刻度 1 / 20 / 60 / 100 對應 -58.0 / -40.0 / -17.0 / 0.0 dB

表 1-3 speaker media volume curve:刻度 1 / 20 / 60 / 100 對應 -56.0 / -34.0 / -11.0 / 0.0 dB

表 1-4 speaker sonification volume curve:刻度 1 / 33 / 66 / 100 對應 -29.7 / -20.1 / -10.2 / 0.0 dB

表 1-5 default system volume curve:刻度 1 / 33 / 66 / 100 對應 -24.0 / -18.0 / -12.0 / -6.0 dB

表 1-6 headset system volume curve:刻度 1 / 33 / 66 / 100 對應 -30.0 / -26.0 / -22.0 / -18.0 dB

總結:透過按音量鍵這一場景,從 Java 層、media 本地層、AudioFlinger 服務層到硬體抽象層逐層分析音量調節的完成過程,歸納出從線性 UI Index 轉換為對數式人耳聽感的公式與預置區間表。依不同的硬體,我們可以自行預置合適的區間表,讓音量曲線更貼近實際聽感。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved