|
1 | 1 | /** |
2 | | - * Base class for volume transfer functions |
| 2 | + * Base class for volume transfer functions. |
| 3 | + * |
| 4 | + * Volume transfer functions convert between slider position (UI space) and |
| 5 | + * player volume (audio space). This allows different scaling behaviors like |
| 6 | + * linear or logarithmic (decibel-based) volume control. |
3 | 7 | */ |
4 | 8 | class VolumeTransfer { |
5 | 9 | /** |
6 | | - * Convert logarithmic (slider) to linear (tech) |
| 10 | + * Convert slider position to player volume. |
7 | 11 | * |
8 | | - * @param {number} logarithmic - Volume from 0-1 |
9 | | - * @return {number} Linear volume from 0-1 |
| 12 | + * @param {number} sliderPosition - Slider position from 0-1 |
| 13 | + * @return {number} Player volume from 0-1 |
10 | 14 | */ |
11 | | - toLinear(logarithmic) { |
| 15 | + sliderToVolume(sliderPosition) { |
12 | 16 | throw new Error('Must be implemented by subclass'); |
13 | 17 | } |
14 | 18 |
|
15 | 19 | /** |
16 | | - * Convert linear (tech) to logarithmic (slider) |
| 20 | + * Convert player volume to slider position. |
17 | 21 | * |
18 | | - * @param {number} linear - Volume from 0-1 |
19 | | - * @return {number} Logarithmic volume from 0-1 |
| 22 | + * @param {number} volume - Player volume from 0-1 |
| 23 | + * @return {number} Slider position from 0-1 |
20 | 24 | */ |
21 | | - toLogarithmic(linear) { |
| 25 | + volumeToSlider(volume) { |
22 | 26 | throw new Error('Must be implemented by subclass'); |
23 | 27 | } |
24 | | - |
25 | | - getName() { |
26 | | - return 'base'; |
27 | | - } |
28 | 28 | } |
29 | 29 |
|
30 | 30 | /** |
31 | | - * Linear transfer - no conversion (current default behavior) |
| 31 | + * Linear volume transfer - direct 1:1 mapping between slider and volume. |
| 32 | + * |
| 33 | + * This is the default behavior where moving the slider linearly adjusts |
| 34 | + * the volume linearly. Simple but may not match human perception of loudness. |
32 | 35 | */ |
33 | 36 | class LinearVolumeTransfer extends VolumeTransfer { |
34 | | - toLinear(value) { |
35 | | - return value; |
36 | | - } |
37 | | - |
38 | | - toLogarithmic(value) { |
39 | | - return value; |
| 37 | + /** |
| 38 | + * Convert slider position to player volume (1:1 mapping). |
| 39 | + * |
| 40 | + * @param {number} sliderPosition - Slider position from 0-1 |
| 41 | + * @return {number} Player volume from 0-1 |
| 42 | + */ |
| 43 | + sliderToVolume(sliderPosition) { |
| 44 | + return sliderPosition; |
40 | 45 | } |
41 | 46 |
|
42 | | - getName() { |
43 | | - return 'linear'; |
| 47 | + /** |
| 48 | + * Convert player volume to slider position (1:1 mapping). |
| 49 | + * |
| 50 | + * @param {number} volume - Player volume from 0-1 |
| 51 | + * @return {number} Slider position from 0-1 |
| 52 | + */ |
| 53 | + volumeToSlider(volume) { |
| 54 | + return volume; |
44 | 55 | } |
45 | 56 | } |
46 | 57 |
|
47 | 58 | /** |
48 | | - * Logarithmic transfer using decibel scaling |
| 59 | + * Logarithmic volume transfer using decibel scaling. |
| 60 | + * |
| 61 | + * Provides exponential volume changes as the slider moves linearly, which |
| 62 | + * better matches human perception of loudness. Uses decibel (dB) scaling |
| 63 | + * where volume = 10^(dB/20). |
| 64 | + * |
| 65 | + * @example |
| 66 | + * // With dbRange = 50: |
| 67 | + * // Slider at 0.5 → Volume ~0.032 (-30 dB) |
| 68 | + * // Slider at 0.75 → Volume ~0.178 (-15 dB) |
| 69 | + * // Slider at 1.0 → Volume 1.0 (0 dB) |
49 | 70 | */ |
50 | 71 | class LogarithmicVolumeTransfer extends VolumeTransfer { |
| 72 | + /** |
| 73 | + * Creates a logarithmic volume transfer function. |
| 74 | + * |
| 75 | + * @param {number} [dbRange=50] - The decibel range for the transfer function. |
| 76 | + * Larger values create a more dramatic curve. Typical range: 40-60 dB. |
| 77 | + */ |
51 | 78 | constructor(dbRange = 50) { |
52 | 79 | super(); |
53 | 80 | this.dbRange = dbRange; |
54 | 81 | this.offset = Math.pow(10, -dbRange / 20); |
55 | 82 | } |
56 | 83 |
|
57 | 84 | /** |
58 | | - * Convert logarithmic slider position to linear volume for tech |
| 85 | + * Convert slider position to player volume using logarithmic scaling. |
| 86 | + * |
| 87 | + * Applies exponential scaling so that linear slider movement produces |
| 88 | + * logarithmic volume changes, matching human loudness perception. |
59 | 89 | * |
60 | | - * @param {number} sliderPosition - Slider position (0-1) |
61 | | - * @return {number} Linear volume (0-1) |
| 90 | + * @param {number} sliderPosition - Slider position from 0-1 |
| 91 | + * @return {number} Player volume from 0-1 |
62 | 92 | */ |
63 | | - toLinear(sliderPosition) { |
64 | | - if (sliderPosition <= 0) { |
| 93 | + sliderToVolume(sliderPosition) { |
| 94 | + if (sliderPosition === 0) { |
65 | 95 | return 0; |
66 | 96 | } |
67 | | - |
68 | | - if (sliderPosition >= 1) { |
| 97 | + if (sliderPosition === 1) { |
69 | 98 | return 1; |
70 | 99 | } |
71 | 100 |
|
72 | 101 | const dB = sliderPosition * this.dbRange - this.dbRange; |
73 | | - const linear = Math.pow(10, dB / 20); |
74 | 102 |
|
75 | | - return linear; |
| 103 | + return Math.pow(10, dB / 20) * (1 + this.offset); |
76 | 104 | } |
77 | 105 |
|
78 | 106 | /** |
79 | | - * Convert linear volume from tech to logarithmic slider position |
| 107 | + * Convert player volume to slider position using logarithmic scaling. |
| 108 | + * |
| 109 | + * Inverse of sliderToVolume - converts linear volume back to the |
| 110 | + * corresponding logarithmic slider position. |
80 | 111 | * |
81 | | - * @param {number} linear - Linear volume (0-1) |
82 | | - * @return {number} Slider position (0-1) |
| 112 | + * @param {number} volume - Player volume from 0-1 |
| 113 | + * @return {number} Slider position from 0-1 |
83 | 114 | */ |
84 | | - toLogarithmic(linear) { |
85 | | - if (linear <= 0) { |
| 115 | + volumeToSlider(volume) { |
| 116 | + if (volume <= 0) { |
86 | 117 | return 0; |
87 | 118 | } |
88 | 119 |
|
89 | | - if (linear >= 1) { |
| 120 | + if (volume >= 1) { |
90 | 121 | return 1; |
91 | 122 | } |
92 | 123 |
|
93 | | - const dB = 20 * Math.log10(linear); |
| 124 | + const dB = 20 * Math.log10(volume); |
94 | 125 | const position = (dB + this.dbRange) / this.dbRange; |
95 | 126 |
|
96 | 127 | return position; |
97 | 128 | } |
98 | | - |
99 | | - getName() { |
100 | | - return 'logarithmic'; |
101 | | - } |
102 | 129 | } |
103 | 130 |
|
104 | 131 | export default VolumeTransfer; |
|
0 commit comments