Commit ffc9109a authored by JuniorJPDJ's avatar JuniorJPDJ Committed by Georg Krause
Browse files

Change volume dynamic range from 60dB to 40dB

also makes it easily configurable in future if needed
changes volume calculation algo to more precise and easy to understand (dB definition)
parent f1f6a77d
Changed volume dynamic range from 60dB to 40dB (fixes #1544)
// Provides functions to convert between linear and logarithmic volume scales.
// The logarithmic volume from the UI is converted to a linear volume with a
// logarithmic function like exp(b*x)/a.
// Compare https://www.dr-lex.be/info-stuff/volumecontrols.html for how the
// values for a and b got derived.
const PARAM_A = 1000
const PARAM_B = Math.log(1000) // ~ 6.908
const DYNAMIC_RANGE = 40 // dB
function toLinearVolumeScale(v) {
// Or as approximation:
// return Math.pow(v, 4)
if (v == 0.0) {
if (v <= 0.0) {
return 0.0
}
return Math.min(Math.exp(PARAM_B * v) / PARAM_A, 1.0)
// (1.0; 0.0) -> (0; -DYNAMIC_RANGE) dB
let dB = (v-1)*DYNAMIC_RANGE
return Math.pow(10, dB / 20)
}
function toLogarithmicVolumeScale(v) {
// Or as approximation:
// return Math.exp(Math.log(v) / 4)
if (v == 0.0) {
if (v <= 0.0) {
return 0.0
}
return Math.log(v * PARAM_A) / PARAM_B
let dB = 20 * Math.log10(v)
// (0; -DYNAMIC_RANGE) [dB] -> (1.0; 0.0)
return 1 - (dB / -DYNAMIC_RANGE)
}
exports.toLinearVolumeScale = toLinearVolumeScale
......
......@@ -8,10 +8,6 @@ describe('store/auth', () => {
expect(toLinearVolumeScale(0.0)).to.equal(0.0)
})
describe('it should have logarithmic scale', () => {
expect(2 * toLinearVolumeScale(0.5)).to.be.closeTo(toLinearVolumeScale(0.6), 0.001)
})
describe('it should return full volume', () => {
expect(toLogarithmicVolumeScale(1.0)).to.be.closeTo(1.0, 0.001)
})
......@@ -22,13 +18,8 @@ describe('store/auth', () => {
expect(toLogarithmicVolumeScale(0.0)).to.equal(0.0)
})
describe('it should have logarithmic scale', () => {
expect(toLogarithmicVolumeScale(0.6)).to.be.closeTo(0.9261, 0.001)
expect(toLogarithmicVolumeScale(0.7)).to.be.closeTo(0.9483, 0.001)
})
describe('it should return full volume', () => {
expect(toLogarithmicVolumeScale(1.0)).to.be.closeTo(1.0, 0.001)
})
})
})
\ No newline at end of file
})
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment