Android音视频框架 音量区间以及音频参数

参数介绍:

nbSteps:音量曲线的步长,为了便于计算,目前步长都是按100配置的,因此这个值一般都是100
indexInUi:对应流类型在上层UI设置的音量值 — 这就是上层设置下来的UI音量值,如果是music则在0~15之间
volIndexMin:对应流类型在上层UI的最小音量值
volIndexMax:对应流类型在上层UI的最大音量值
indexInUiPosition:用于确认上层UI设置的音量值落在底层音量曲线数组的哪个区间
mCurvePoints:此数组每个元素为一个CurvePoint对象,标识当前音量曲线的一个元素。以下图为例:mCurvePoints数组每个元素的mIndex依次为音量曲线的第一列值(1、33、66、100),每个mCurvePoints数组元素的mAttenuationInMb依次对应后面放大后的db值(-5800、-4000、-1700、0)

<< OLE Object: Picture (Device Independent Bitmap) >>

volIdx:上层UI设置的音量值对应底层音量曲线的index值(例如music流音量上层index区间为0~15,而底层音量曲线的index区间是1~100,则通过按比例计算将上层UI的当前音量值映射到底层音量曲线的index)


基本原理:

底层音量曲线在1~100这个index区间设置了几个点(目前4个),对每个点都设置对应的衰减分贝值db,两点之间设置的衰减分贝区间越大,则对应的音量等级声音大小变化越大。而上层UI有自己维护的音量区间(例如music流对应index区间为0~15),其设置的音量值会在底层通过按线性比例计算,看落在对应音量曲线的哪个区间,从而计算对应的db值。

举例:
例如使用speaker播放音乐,我们要调节music流音量为2级,则根据音量曲线计算的db值如下(使用的是DEFAULT_DEVICE_CATEGORY_SPEAKER_VOLUME_CURVE音量曲线):

volIdx = (100 * (2 - 0)) / (15 - 0) = 13; //落在mCurvePoints前二个数组元素之间(1 < 13 < 20)

indexInUiPosition = 1;

decibels

= (-5800 / 100.0f) + ((float)(13 - 1)) * ( ((-4000 / 100.0f) - (-5800 / 100.0f)) / ((float)(20 - 1)) );


相关code:

float VolumeCurve::volIndexToDb(int indexInUi, int volIndexMin, int volIndexMax) const
27{
28 ALOG_ASSERT(!mCurvePoints.isEmpty(), “Invalid volume curve”);
29 if (volIndexMin < 0 || volIndexMax < 0) {
30 // In order to let AudioService initialize the min and max, convention is to use -1
31 return NAN;
32 }
33 if (indexInUi < volIndexMin) {
34 // an index of 0 means mute request when volIndexMin > 0
35 if (indexInUi == 0) {
36 ALOGV(“VOLUME forcing mute for index 0 with min index %d”, volIndexMin);
37 return VOLUME_MIN_DB;
38 }
39 ALOGV(“VOLUME remapping index from %d to min index %d”, indexInUi, volIndexMin);
40 indexInUi = volIndexMin;
41 } else if (indexInUi > volIndexMax) {
42 ALOGV(“VOLUME remapping index from %d to max index %d”, indexInUi, volIndexMax);
43 indexInUi = volIndexMax;
44 }
45
46 size_t nbCurvePoints = mCurvePoints.size();
47 // the volume index in the UI is relative to the min and max volume indices for this stream
48 int nbSteps = 1 + mCurvePoints[nbCurvePoints - 1].mIndex - mCurvePoints[0].mIndex;
49 int volIdx = (nbSteps * (indexInUi - volIndexMin)) / (volIndexMax - volIndexMin);
50
51 // Where would this volume index been inserted in the curve point
52 size_t indexInUiPosition = mCurvePoints.orderOf(CurvePoint(volIdx, 0));
53 if (indexInUiPosition >= nbCurvePoints) {
54 //use last point of table
55 return mCurvePoints[nbCurvePoints - 1].mAttenuationInMb / 100.0f;
56 }
57 if (indexInUiPosition == 0) {
58 if (indexInUiPosition != mCurvePoints[0].mIndex) {
59 return VOLUME_MIN_DB; // out of bounds
60 }
61 return mCurvePoints[0].mAttenuationInMb / 100.0f;
62 }
63 // linear interpolation in the attenuation table in dB
64 float decibels = (mCurvePoints[indexInUiPosition - 1].mAttenuationInMb / 100.0f) +
65 ((float)(volIdx - mCurvePoints[indexInUiPosition - 1].mIndex)) *
66 ( ((mCurvePoints[indexInUiPosition].mAttenuationInMb / 100.0f) -
67 (mCurvePoints[indexInUiPosition - 1].mAttenuationInMb / 100.0f)) /
68 ((float)(mCurvePoints[indexInUiPosition].mIndex -
69 mCurvePoints[indexInUiPosition - 1].mIndex)) );
70
71 ALOGV(“VOLUME vol index=[%d %d %d], dB=[%.1f %.1f %.1f]”,
72 mCurvePoints[indexInUiPosition - 1].mIndex, volIdx,
73 mCurvePoints[indexInUiPosition].mIndex,
74 ((float)mCurvePoints[indexInUiPosition - 1].mAttenuationInMb / 100.0f), decibels,
75 ((float)mCurvePoints[indexInUiPosition].mAttenuationInMb / 100.0f));
76
77 return decibels;
78}


音频参数

相关路径:
device/xxx/xxx/rootdir/system/etc/parameter-framework/
audio_policy_engine_stream_volumes.xml
audio_policy_engine_default_stream_volumes.xml

音量衰减区间含义以及计算方法如4.1中所述,

 
53        system
54        0
55        7
56        
57            1,-3000
58            33,-2600
59            66,-2200
60            100,-1800
61        
62        
63            1,-2400
64            33,-1800
65            66,-1200
66            100,-600
67        
68        
69        
70        
71        
72    

你可能感兴趣的:(Android音视频框架 音量区间以及音频参数)