1. Volume series definition
The maximum volume Max_stream_volume is defined in Audioservice.java, and the phone's settings property can overwrite it.
2. Volume initialization
Initstreamvolume into the audiopolicymanagerbase streamdescriptor mstreams[audiosystem::num_stream_types];
3. Set the master volume
How does the master volume work?
Final volume = Master Volume * Stream volume
4. Set the stream volume
The Setstreamvolumeindex function, in Audiopolicy, uses the Volindextoampl to convert the index integer to the amplitude ratio of the float type, which is the amplitude/reference amplitude.
The specific method is: through the input index to find the corresponding sound pressure value of db, and then the following formula to calculate the amplifier, this value is the amplitude ratio.
A line of code in the function Volindextoampl
FLOAT amplification = exp (decibels * 0.115129f);
That's the formula.
By multiplying this value by the amplitude of the source, the adjusted volume is obtained. This is also the principle of digital gain adjustment.
After this value is obtained, the global variable mstreamtypes is deposited into the Audioflinger, which is Mstreamtypes[stream].volume =value.
When thread tries to play a sound, it does so in preparetracks_l:
int32_t VL = t->prevvolume[0]; int32_t VR = t->prevvolume[1]; Const int32_t Vlinc =t->volumeinc[0]; Const int32_t Vrinc =t->volumeinc[1]; Do { *out++ + = (VL >> +) * (int32_t) *in++; *out++ + = (VR >> +) * (int32_t) *in++; VL + = Vlinc; VR + = Vrinc; } while (--framecount);
When the system is muted, it is simple to set the following parameters to 0:
VL = VR = 0;VLF = VRF = VAF = 0.
Setting parameters for Audiomixer
Maudiomixer->setparameter (name, PARAM,AUDIOMIXER::VOLUME0, &VLF);
Maudiomixer->setparameter (name, param,audiomixer::volume1, &VRF);
So finally, the volume is set by audiomixer actually multiplying VOLUME0 and VOLUME1.
As in Track__16bitsstereo
int32_t VL = t->prevvolume[0]; int32_t VR = t->prevvolume[1]; Const int32_t Vlinc =t->volumeinc[0]; Const int32_t Vrinc =t->volumeinc[1]; Do { *out++ + = (VL >> +) * (int32_t) *in++; *out++ + = (VR >> +) * (int32_t) *in++; VL + = Vlinc; VR + = Vrinc; } while (--framecount);
Android Volume Setup process dry version