Skip to content

Commit 42f9239

Browse files
committed
document the internal state of sound/music volume
1 parent b0d45d1 commit 42f9239

File tree

2 files changed

+34
-0
lines changed

2 files changed

+34
-0
lines changed

docs/reST/ref/mixer.rst

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -438,6 +438,25 @@ The following file formats are supported
438438

439439
| If value < 0.0, the volume will not be changed
440440
| If value > 1.0, the volume will be set to 1.0
441+
442+
.. note::
443+
The values are internally converted and kept as integer values in range [0, 128], which means
444+
that ``get_volume()`` may return a different volume than it was set to. For example,
445+
446+
>>> sound.set_volume(0.1)
447+
>>> sound.get_volume()
448+
0.09375
449+
450+
This is because when you ``set_volume(0.1)``, the volume is internally calculated like so
451+
452+
>>> int(0.1 * 128)
453+
12
454+
455+
This means that some of the precision is lost, so when you retrieve it again using ``get_volume()``,
456+
it is converted back to a ``float`` using that integer
457+
458+
>>> 12 / 128
459+
0.09375
441460

442461
.. ## Sound.set_volume ##
443462
@@ -448,6 +467,9 @@ The following file formats are supported
448467
449468
Return a value from 0.0 to 1.0 (inclusive) representing the volume for this Sound.
450469

470+
.. note::
471+
See :func:`Sound.set_volume` for more information regarding the returned value
472+
451473
.. ## Sound.get_volume ##
452474
453475
.. method:: get_num_channels
@@ -627,6 +649,9 @@ The following file formats are supported
627649
sound.set_volume(0.9) # Now plays at 90% of full volume.
628650
sound.set_volume(0.6) # Now plays at 60% (previous value replaced).
629651
channel.set_volume(0.5) # Now plays at 30% (0.6 * 0.5).
652+
653+
.. note::
654+
See :func:`Sound.set_volume` for more information regarding how the value is stored internally
630655

631656
.. ## Channel.set_volume ##
632657
@@ -641,6 +666,9 @@ The following file formats are supported
641666
:meth:`Channel.set_volume`. The Sound object also has its own volume
642667
which is mixed with the channel.
643668

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

docs/reST/ref/music.rst

Lines changed: 6 additions & 0 deletions
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)