diff --git a/changes/changelog.d/1544.enhancement b/changes/changelog.d/1544.enhancement new file mode 100644 index 000000000..644fd1ccd --- /dev/null +++ b/changes/changelog.d/1544.enhancement @@ -0,0 +1 @@ +Changed volume dynamic range from 60dB to 40dB (fixes #1544) diff --git a/front/src/audio/volume.js b/front/src/audio/volume.js index 25f6c0ed2..c184a9767 100644 --- a/front/src/audio/volume.js +++ b/front/src/audio/volume.js @@ -1,31 +1,25 @@ - -// 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 diff --git a/front/tests/unit/specs/audio/volume.spec.js b/front/tests/unit/specs/audio/volume.spec.js index 61ac8e9c9..d10daf5ec 100644 --- a/front/tests/unit/specs/audio/volume.spec.js +++ b/front/tests/unit/specs/audio/volume.spec.js @@ -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 +})