Skip to content

Commit f71f0c3

Browse files
committed
Address comments
1 parent d5e9157 commit f71f0c3

File tree

3 files changed

+52
-3
lines changed

3 files changed

+52
-3
lines changed

docs/reST/ref/mixer.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -515,6 +515,9 @@ The following file formats are supported
515515
original Sound. If the copy fails, a ``TypeError`` or :meth:`pygame.error`
516516
exception will be raised.
517517

518+
If copying a subclass of ``mixer.Sound``, an instance of the same subclass
519+
will be returned.
520+
518521
Also note that this functions as ``Sound.__copy__``.
519522

520523
.. versionadded:: 2.5.6

src_c/mixer.c

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -808,7 +808,7 @@ snd_copy(PyObject *self, PyObject *_null)
808808
MIXER_INIT_CHECK();
809809

810810
pgSoundObject *newSound =
811-
(pgSoundObject *)Py_TYPE(self)->tp_new(&pgSound_Type, NULL, NULL);
811+
(pgSoundObject *)Py_TYPE(self)->tp_new(Py_TYPE(self), NULL, NULL);
812812

813813
PyObject *kwargs = PyDict_New();
814814
PyObject *key = PyUnicode_FromString("buffer");
@@ -843,6 +843,15 @@ snd_copy(PyObject *self, PyObject *_null)
843843
return NULL;
844844
}
845845

846+
// Preserve original volume on the new chunk
847+
Mix_Chunk *orig = pgSound_AsChunk(self);
848+
Mix_Chunk *dst = pgSound_AsChunk((PyObject *)newSound);
849+
850+
if (orig && dst) {
851+
int vol = Mix_VolumeChunk(orig, -1);
852+
Mix_VolumeChunk(dst, vol);
853+
}
854+
846855
Py_DECREF(kwargs);
847856
return (PyObject *)newSound;
848857
}

test/mixer_test.py

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1331,6 +1331,10 @@ def __init__(self):
13311331
self.assertRaises(RuntimeError, incorrect.get_volume)
13321332

13331333
def test_snd_copy(self):
1334+
class SubSound(mixer.Sound):
1335+
def __init__(self, *args, **kwargs):
1336+
super().__init__(*args, **kwargs)
1337+
13341338
mixer.init()
13351339

13361340
filenames = [
@@ -1340,22 +1344,55 @@ def test_snd_copy(self):
13401344
"house_lo.opus",
13411345
"surfonasinewave.xm",
13421346
]
1347+
old_volumes = [0.1, 0.2, 0.5, 0.7, 1.0]
1348+
new_volumes = [0.2, 0.3, 0.7, 1.0, 0.1]
13431349
if pygame.mixer.get_sdl_mixer_version() >= (2, 6, 0):
13441350
filenames.append("house_lo.mp3")
13451351

1346-
for f in filenames:
1352+
for f, old_vol, new_vol in zip(filenames, old_volumes, new_volumes):
13471353
filename = example_path(os.path.join("data", f))
13481354
try:
13491355
sound = mixer.Sound(file=filename)
1356+
sound.set_volume(old_vol)
1357+
except pygame.error:
1358+
continue
1359+
sound_copy = sound.copy()
1360+
self.assertEqual(sound.get_length(), sound_copy.get_length())
1361+
self.assertEqual(sound.get_num_channels(), sound_copy.get_num_channels())
1362+
self.assertEqual(sound.get_volume(), sound_copy.get_volume())
1363+
self.assertEqual(sound.get_raw(), sound_copy.get_raw())
1364+
1365+
sound.set_volume(new_vol)
1366+
self.assertNotEqual(sound.get_volume(), sound_copy.get_volume())
1367+
1368+
del sound
1369+
1370+
# Test on the copy for playable sounds
1371+
channel = sound_copy.play()
1372+
if channel is None:
1373+
continue
1374+
self.assertTrue(channel.get_busy())
1375+
sound_copy.stop()
1376+
self.assertFalse(channel.get_busy())
1377+
sound_copy.play()
1378+
self.assertEqual(sound_copy.get_num_channels(), 1)
1379+
1380+
# Test copying a subclass of Sound
1381+
for f, old_vol, new_vol in zip(filenames, old_volumes, new_volumes):
1382+
filename = example_path(os.path.join("data", f))
1383+
try:
1384+
sound = SubSound(file=filename)
1385+
sound.set_volume(old_vol)
13501386
except pygame.error:
13511387
continue
13521388
sound_copy = sound.copy()
1389+
self.assertTrue(sound_copy, SubSound)
13531390
self.assertEqual(sound.get_length(), sound_copy.get_length())
13541391
self.assertEqual(sound.get_num_channels(), sound_copy.get_num_channels())
13551392
self.assertEqual(sound.get_volume(), sound_copy.get_volume())
13561393
self.assertEqual(sound.get_raw(), sound_copy.get_raw())
13571394

1358-
sound.set_volume(0.5)
1395+
sound.set_volume(new_vol)
13591396
self.assertNotEqual(sound.get_volume(), sound_copy.get_volume())
13601397

13611398
del sound

0 commit comments

Comments
 (0)