diff --git a/changes/changelog.d/1222.enhancement b/changes/changelog.d/1222.enhancement new file mode 100644 index 000000000..c896000e8 --- /dev/null +++ b/changes/changelog.d/1222.enhancement @@ -0,0 +1 @@ +Logarithmic scale for volume slider (#1222) \ No newline at end of file diff --git a/front/src/audio/volume.js b/front/src/audio/volume.js new file mode 100644 index 000000000..25f6c0ed2 --- /dev/null +++ b/front/src/audio/volume.js @@ -0,0 +1,32 @@ + +// 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 + +function toLinearVolumeScale(v) { + // Or as approximation: + // return Math.pow(v, 4) + if (v == 0.0) { + return 0.0 + } + + return Math.min(Math.exp(PARAM_B * v) / PARAM_A, 1.0) +} + +function toLogarithmicVolumeScale(v) { + // Or as approximation: + // return Math.exp(Math.log(v) / 4) + if (v == 0.0) { + return 0.0 + } + + return Math.log(v * PARAM_A) / PARAM_B +} + +exports.toLinearVolumeScale = toLinearVolumeScale +exports.toLogarithmicVolumeScale = toLogarithmicVolumeScale diff --git a/front/src/components/audio/VolumeControl.vue b/front/src/components/audio/VolumeControl.vue index 4635d9763..47a2a9594 100644 --- a/front/src/components/audio/VolumeControl.vue +++ b/front/src/components/audio/VolumeControl.vue @@ -29,7 +29,7 @@ @@ -38,6 +38,7 @@