|
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). |
49 | 64 | */ |
50 | 65 | class LogarithmicVolumeTransfer extends VolumeTransfer { |
| 66 | + /** |
| 67 | + * Creates a logarithmic volume transfer function. |
| 68 | + * |
| 69 | + * @param {number} [dbRange=50] - The decibel range for the transfer function. |
| 70 | + * Larger values create a more dramatic curve. Typical range: 40-60 dB. |
| 71 | + */ |
51 | 72 | constructor(dbRange = 50) { |
52 | 73 | super(); |
53 | 74 | this.dbRange = dbRange; |
54 | 75 | this.offset = Math.pow(10, -dbRange / 20); |
55 | 76 | } |
56 | 77 |
|
57 | 78 | /** |
58 | | - * Convert logarithmic slider position to linear volume for tech |
| 79 | + * Convert slider position to player volume using logarithmic scaling. |
| 80 | + * |
| 81 | + * Applies exponential scaling so that linear slider movement produces |
| 82 | + * logarithmic volume changes, matching human loudness perception. |
59 | 83 | * |
60 | | - * @param {number} sliderPosition - Slider position (0-1) |
61 | | - * @return {number} Linear volume (0-1) |
| 84 | + * @param {number} sliderPosition - Slider position from 0-1 |
| 85 | + * @return {number} Player volume from 0-1 |
62 | 86 | */ |
63 | | - toLinear(sliderPosition) { |
64 | | - if (sliderPosition <= 0) { |
| 87 | + sliderToVolume(sliderPosition) { |
| 88 | + if (sliderPosition === 0) { |
65 | 89 | return 0; |
66 | 90 | } |
67 | | - |
68 | | - if (sliderPosition >= 1) { |
| 91 | + if (sliderPosition === 1) { |
69 | 92 | return 1; |
70 | 93 | } |
71 | 94 |
|
72 | 95 | const dB = sliderPosition * this.dbRange - this.dbRange; |
73 | | - const linear = Math.pow(10, dB / 20); |
74 | 96 |
|
75 | | - return linear; |
| 97 | + return Math.pow(10, dB / 20) * (1 + this.offset); |
76 | 98 | } |
77 | 99 |
|
78 | 100 | /** |
79 | | - * Convert linear volume from tech to logarithmic slider position |
| 101 | + * Convert player volume to slider position using logarithmic scaling. |
| 102 | + * |
| 103 | + * Inverse of sliderToVolume - converts linear volume back to the |
| 104 | + * corresponding logarithmic slider position. |
80 | 105 | * |
81 | | - * @param {number} linear - Linear volume (0-1) |
82 | | - * @return {number} Slider position (0-1) |
| 106 | + * @param {number} volume - Player volume from 0-1 |
| 107 | + * @return {number} Slider position from 0-1 |
83 | 108 | */ |
84 | | - toLogarithmic(linear) { |
85 | | - if (linear <= 0) { |
| 109 | + volumeToSlider(volume) { |
| 110 | + if (volume <= 0) { |
86 | 111 | return 0; |
87 | 112 | } |
88 | 113 |
|
89 | | - if (linear >= 1) { |
| 114 | + if (volume >= 1) { |
90 | 115 | return 1; |
91 | 116 | } |
92 | 117 |
|
93 | | - const dB = 20 * Math.log10(linear); |
| 118 | + const dB = 20 * Math.log10(volume); |
94 | 119 | const position = (dB + this.dbRange) / this.dbRange; |
95 | 120 |
|
96 | 121 | return position; |
97 | 122 | } |
98 | | - |
99 | | - getName() { |
100 | | - return 'logarithmic'; |
101 | | - } |
102 | 123 | } |
103 | 124 |
|
104 | 125 | export default VolumeTransfer; |
|
0 commit comments