Skip to content

Commit 6e7a153

Browse files
authored
Document how volume is stored internally for (get|set)_volume functions and methods (#3091)
* document the internal state of sound/music volume * formatting (of course)
1 parent 44b3483 commit 6e7a153

File tree

2 files changed

+34
-0
lines changed

2 files changed

+34
-0
lines changed

docs/reST/ref/mixer.rst

+28
Original file line numberDiff line numberDiff line change
@@ -438,6 +438,25 @@ The following file formats are supported
438438
| If value < 0.0, the volume will not be changed
439439
| If value > 1.0, the volume will be set to 1.0
440440
441+
.. note::
442+
The values are internally converted and kept as integer values in range [0, 128], which means
443+
that ``get_volume()`` may return a different volume than it was set to. For example,
444+
445+
>>> sound.set_volume(0.1)
446+
>>> sound.get_volume()
447+
0.09375
448+
449+
This is because when you ``set_volume(0.1)``, the volume is internally calculated like so
450+
451+
>>> int(0.1 * 128)
452+
12
453+
454+
This means that some of the precision is lost, so when you retrieve it again using ``get_volume()``,
455+
it is converted back to a ``float`` using that integer
456+
457+
>>> 12 / 128
458+
0.09375
459+
441460
.. ## Sound.set_volume ##
442461
443462
.. method:: get_volume
@@ -447,6 +466,9 @@ The following file formats are supported
447466
448467
Return a value from 0.0 to 1.0 (inclusive) representing the volume for this Sound.
449468

469+
.. note::
470+
See :func:`Sound.set_volume` for more information regarding the returned value
471+
450472
.. ## Sound.get_volume ##
451473
452474
.. method:: get_num_channels
@@ -627,6 +649,9 @@ The following file formats are supported
627649
sound.set_volume(0.6) # Now plays at 60% (previous value replaced).
628650
channel.set_volume(0.5) # Now plays at 30% (0.6 * 0.5).
629651

652+
.. note::
653+
See :func:`Sound.set_volume` for more information regarding how the value is stored internally
654+
630655
.. ## Channel.set_volume ##
631656
632657
.. method:: get_volume
@@ -640,6 +665,9 @@ The following file formats are supported
640665
:meth:`Channel.set_volume`. The Sound object also has its own volume
641666
which is mixed with the channel.
642667

668+
.. note::
669+
See :func:`Sound.set_volume` for more information regarding the returned value
670+
643671
.. ## Channel.get_volume ##
644672
645673
.. method:: get_busy

docs/reST/ref/music.rst

+6
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,9 @@ For a complete list of supported file formats, see the :mod:`pygame.mixer` doc p
158158
volume will remain set at the current level. If the ``volume`` argument
159159
is greater than ``1.0``, the volume will be set to ``1.0``.
160160

161+
.. note::
162+
See :func:`mixer.Sound.set_volume()<pygame.mixer.Sound.set_volume>` for more information regarding how the value is stored internally
163+
161164
.. ## pygame.mixer.music.set_volume ##
162165
163166
.. function:: get_volume
@@ -168,6 +171,9 @@ For a complete list of supported file formats, see the :mod:`pygame.mixer` doc p
168171
Returns the current volume for the mixer. The value will be between ``0.0``
169172
and ``1.0``.
170173

174+
.. note::
175+
See :func:`mixer.Sound.set_volume()<pygame.mixer.Sound.set_volume>` for more information regarding the returned value
176+
171177
.. ## pygame.mixer.music.get_volume ##
172178
173179
.. function:: get_busy

0 commit comments

Comments
 (0)