Android音量控制曲线

  摘要:本文介绍了android音量的控制曲线的计算方法。

  由于人耳对声音的听感具指数曲线型,也就是对小音量时比较敏感,随着声音的加大其听感随之变的不敏感,其变化近似指数函数曲线的形式。为了使听感变的近似直线的变化,人们在实践中就采用了音量变化近似对数式曲线型的电位器来实现这个目的。对比法产生音量控制曲线与最终扬声器输出的声压有关,当然您也可以根据扬声器的输出功率来进行比对,但功率终究不如电压来的方便。音量调节框的UI滑动条的刻度是线性的,这样就给我们生成音量控制曲线打下了很好的对比基础。下面我们就来通过一个音量调节的场景来分析Android是如何控制音量的。

  首先,我们按音量调节键使得media音量逐级增加到最大。STREAM_MUSIC流的音量分为15级,通过AudioManger的handleKeyDown函数调用adjustSuggestedStreamVolume设置,一路找下去,发现在AudioService中adjustSuggestedStreamVolume然后调用adjustStreamVolume,通过消息MSG_SET_SYSTEM_VOLUME(4.1中MSG_SET_DEVICE_VOLUME)调用setSystemVolume(4.1中setDeviceVolume),转到AudioSystem中的setStreamVolumeIndex,再通过jni层调用本地层的AudioSystem调用AudioPolicymanagerService,最后到AudiopolicyManagerBase的setStreamVolumeIndex,接下来的由checkAndSetVolume调用computeVolume,马上就要到真相大白的时候了。volIndexToAmpl函数时真正计算音量的地方,我们一起来分析这个函数

 

 1 float AudioPolicyManagerBase::volIndexToAmpl(audio_devices_t device, const StreamDescriptor& streamDesc,
 2         int indexInUi)
 3 {
 4     device_category deviceCategory = getDeviceCategory(device);
 5     const VolumeCurvePoint *curve = streamDesc.mVolumeCurve[deviceCategory];
 6 
 7     // the volume index in the UI is relative to the min and max volume indices for this stream type
 8     int nbSteps = 1 + curve[VOLMAX].mIndex -
 9             curve[VOLMIN].mIndex;//计算预置的曲线区间的范围,这里是(1-100)
10     ALOGI("VOLUME vol indexInUi=%d, nbSteps=%d, mIndexMin=%d, mIndexMax=%d",indexInUi,nbSteps,streamDesc.mIndexMin,streamDesc.mIndexMax);
11     int volIdx = (nbSteps * (indexInUi - streamDesc.mIndexMin)) /
12             (streamDesc.mIndexMax - streamDesc.mIndexMin);//(由传进来的UIIndex计算百分比的index,比如现在是第一级 100*(1-0)/(15-0)=6)
13 
14     // find what part of the curve this index volume belongs to, or if it's out of bounds
15     int segment = 0;
16     if (volIdx < curve[VOLMIN].mIndex) {         // out of bounds
17         return 0.0f;
18     } else if (volIdx < curve[VOLKNEE1].mIndex) {
19         segment = 0;
20     } else if (volIdx < curve[VOLKNEE2].mIndex) {
21         segment = 1;
22     } else if (volIdx <= curve[VOLMAX].mIndex) {
23         segment = 2;
24     } else {                                                               // out of bounds
25         return 1.0f;
26     }
27 //第一极6是在区间VOLKNEE1之间,其区间表是在AudioPolicyManager初始化的时候就已经加载,因此它对应的segment为0
28     // linear interpolation in the attenuation table in dB
29     float decibels = curve[segment].mDBAttenuation +
30             ((float)(volIdx - curve[segment].mIndex)) *
31                 ( (curve[segment+1].mDBAttenuation -
32                         curve[segment].mDBAttenuation) /
33                     ((float)(curve[segment+1].mIndex -
34                             curve[segment].mIndex)) );
35 //计算衰减分贝数 curve[0].db + 该区间每一级index对应的db*index数  
36     float amplification = exp( decibels * 0.115129f); // exp( dB * ln(10) / 20 )
37//由指数公式计算出音量amplification db = 20log(V/Vmax)  linearToLog Vmax是一个参考值
38     ALOGI("VOLUME vol index=[%d %d %d], dB=[%.1f %.1f %.1f] ampl=%.5f",
39             curve[segment].mIndex, volIdx,
40             curve[segment+1].mIndex,
41             curve[segment].mDBAttenuation,
42             decibels,
43             curve[segment+1].mDBAttenuation,
44             amplification);
45 
46     return amplification;
47 }

    然后通过以上函数计算后得到一个0-1之间的float音量值,最后通过mpClientInterface->setStreamVolume设置到audioflinger中的mStreamTypes[stream].value,在prepareTracks_l中将音量值传入到audiomixer中混合。至此音量调节的全过程介绍完毕,下面六个附表是在AudioPolicyManagerBase中预置的六个音量曲线db分布表

音量刻度 1 33 66 100
输出衰减量db -49.5 -33.5 -17.0 0.0

表1-1 default volume curve

音量刻度 1 20 60 100
输出衰减量db -58.0 -40.0 -17.0 0.0

表1-2 default media volume curve

音量刻度 1 20 60 100
输出衰减量db -56.0 -34.0 -11.0 0.0

表1-3 speaker media volume curve

音量刻度 1 33 66 100
输出衰减量db -29.7 -20.1 -10.2 0.0

表1-4 speaker sonification volume curve

音量刻度 1 33 66 100
输出衰减量db -24.0 -18.0 -12.0 -6.0

表1-5 default system volume curve

音量刻度 1 33 66 100
输出衰减量db -30.0 -26.0 -22.0 -18.0

表1-6 headset system volume curve

  总结:通过调节音量键这一调节音量的场景,从java层,media本地层,AudioFlinger服务层,硬件抽象层等四层分析音量调节函数是如何完成一个音量调节任务的。总结了从线性UI的Index如何转化为对数关系的人耳的听觉转换的公式,以及预置的区间表,根据不同的硬件,我们可以自己预置适当的区间表,使得音量曲线更符合我们的听感。

posted @ 2012-09-27 19:07  cerberspace  阅读(5948)  评论(6编辑  收藏  举报