Bug 1052923 - Fix the volume conversion in millibels. r=padenot

This commit is contained in:
Gabriele Svelto 2014-08-15 11:36:49 +02:00
parent 9615e6d114
commit 6457d628ad

View File

@ -745,6 +745,7 @@ opensl_stream_set_volume(cubeb_stream * stm, float volume)
{
SLresult res;
SLmillibel max_level, millibels;
float unclamped_millibels;
res = (*stm->volume)->GetMaxVolumeLevel(stm->volume, &max_level);
@ -752,12 +753,15 @@ opensl_stream_set_volume(cubeb_stream * stm, float volume)
return CUBEB_ERROR;
}
millibels = SL_MILLIBEL_MIN + (float)(max_level - SL_MILLIBEL_MIN) * volume;
/* millibels are 100*dB, so the conversion from the volume's linear amplitude
* is 100 * 20 * log(volume). However we clamp the resulting value before
* passing it to lroundf() in order to prevent it from silently returning an
* erroneous value when the unclamped value exceeds the size of a long. */
unclamped_millibels = 100.0f * 20.0f * log10f(fmaxf(volume, 0.0f));
unclamped_millibels = fmaxf(unclamped_millibels, SL_MILLIBEL_MIN);
unclamped_millibels = fminf(unclamped_millibels, max_level);
/* clamp to supported range */
if (millibels > max_level) {
millibels = max_level;
}
millibels = lroundf(unclamped_millibels);
res = (*stm->volume)->SetVolumeLevel(stm->volume, millibels);