Skip to content

Commit

Permalink
Follow SDL's behaviour on negative values and values greater than the…
Browse files Browse the repository at this point in the history
… max + add tests for that
  • Loading branch information
Matiiss committed Sep 5, 2024
1 parent 865db9a commit b465075
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 2 deletions.
8 changes: 6 additions & 2 deletions src_c/mixer.c
Original file line number Diff line number Diff line change
Expand Up @@ -756,7 +756,9 @@ snd_set_volume(PyObject *self, PyObject *args)

MIXER_INIT_CHECK();

((pgSoundObject *)self)->volume = volume;
if (volume >= 0.0f) {
((pgSoundObject *)self)->volume = MIN(volume, 1.0f);
}

Mix_VolumeChunk(chunk, (int)(volume * 128));
Py_RETURN_NONE;
Expand Down Expand Up @@ -1275,7 +1277,9 @@ chan_set_volume(PyObject *self, PyObject *args)
volume = 1.0f;
}

channeldata[channelnum].volume = volume;
if (volume >= 0.0f) {
channeldata[channelnum].volume = MIN(volume, 1.0f);
}

#ifdef Py_DEBUG
result =
Expand Down
38 changes: 38 additions & 0 deletions test/mixer_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -921,6 +921,25 @@ def test_set_volume(self):
ch.set_volume(0.0)
self.assertAlmostEqual(ch.get_volume(), 0.0)

ch.set_volume(0.5)
self.assertAlmostEqual(ch.get_volume(), 0.5)
ch.set_volume(-0.1)
self.assertAlmostEqual(ch.get_volume(), 0.5)
ch.set_volume(-5)
self.assertAlmostEqual(ch.get_volume(), 0.5)

ch.set_volume(0.99)
self.assertAlmostEqual(ch.get_volume(), 0.99)
ch.set_volume(1.0)
self.assertAlmostEqual(ch.get_volume(), 1.0)
ch.set_volume(1.1)
self.assertAlmostEqual(ch.get_volume(), 1.0)
ch.set_volume(3)
self.assertAlmostEqual(ch.get_volume(), 1.0)

ch.set_volume(-0.5)
self.assertAlmostEqual(ch.get_volume(), 1.0)

for volume_1000x in range(0, 1_000 + 1):
set_volume = volume_1000x / 1_000

Expand Down Expand Up @@ -1271,6 +1290,25 @@ def test_set_volume_exact(self):
snd.set_volume(0.0)
self.assertAlmostEqual(snd.get_volume(), 0.0)

snd.set_volume(0.5)
self.assertAlmostEqual(snd.get_volume(), 0.5)
snd.set_volume(-0.1)
self.assertAlmostEqual(snd.get_volume(), 0.5)
snd.set_volume(-5)
self.assertAlmostEqual(snd.get_volume(), 0.5)

snd.set_volume(0.99)
self.assertAlmostEqual(snd.get_volume(), 0.99)
snd.set_volume(1.0)
self.assertAlmostEqual(snd.get_volume(), 1.0)
snd.set_volume(1.1)
self.assertAlmostEqual(snd.get_volume(), 1.0)
snd.set_volume(3)
self.assertAlmostEqual(snd.get_volume(), 1.0)

snd.set_volume(-0.5)
self.assertAlmostEqual(snd.get_volume(), 1.0)

for volume_1000x in range(0, 1_000 + 1):
set_volume = volume_1000x / 1_000

Expand Down

0 comments on commit b465075

Please sign in to comment.