From ed05668098188642a5885d1063bd714c2ea230cd Mon Sep 17 00:00:00 2001 From: Damiano Ricciardi Date: Sun, 18 Aug 2024 21:37:16 +0200 Subject: [PATCH 1/5] Alias pygame.Sound --- buildconfig/stubs/gen_stubs.py | 2 +- buildconfig/stubs/pygame/__init__.pyi | 2 +- docs/reST/ref/mixer.rst | 5 ++++- examples/aliens.py | 2 +- examples/audiocapture.py | 4 ++-- examples/chimp.py | 2 +- examples/sound.py | 2 +- examples/sound_array_demos.py | 4 ++-- src_py/__init__.py | 1 + test/mixer_test.py | 2 +- test/sndarray_test.py | 4 ++-- 11 files changed, 17 insertions(+), 13 deletions(-) diff --git a/buildconfig/stubs/gen_stubs.py b/buildconfig/stubs/gen_stubs.py index a762693f41..da679ac8f8 100644 --- a/buildconfig/stubs/gen_stubs.py +++ b/buildconfig/stubs/gen_stubs.py @@ -68,7 +68,7 @@ "_debug": ["print_debug_info"], "event": ["Event"], "font": ["Font"], - "mixer": ["Channel"], + "mixer": ["Sound", "Channel"], "time": ["Clock"], "joystick": ["Joystick"], "window": ["Window"], diff --git a/buildconfig/stubs/pygame/__init__.pyi b/buildconfig/stubs/pygame/__init__.pyi index 00690f3737..a8ecdd7f25 100644 --- a/buildconfig/stubs/pygame/__init__.pyi +++ b/buildconfig/stubs/pygame/__init__.pyi @@ -52,7 +52,7 @@ from .mask import Mask as Mask from ._debug import print_debug_info as print_debug_info from .event import Event as Event from .font import Font as Font -from .mixer import Channel as Channel +from .mixer import Sound as Sound, Channel as Channel from .time import Clock as Clock from .joystick import Joystick as Joystick from .window import Window as Window diff --git a/docs/reST/ref/mixer.rst b/docs/reST/ref/mixer.rst index 3de038983d..d349983fd6 100644 --- a/docs/reST/ref/mixer.rst +++ b/docs/reST/ref/mixer.rst @@ -375,6 +375,9 @@ The following file formats are supported :class:`pygame.mixer.Sound` keyword arguments and array interface support .. versionaddedold:: 2.0.1 pathlib.Path support on Python 3. + .. versionchanged:: 2.5.2 This class is also available through the ``pygame.Sound`` + alias. + .. method:: play | :sl:`begin sound playback` @@ -619,7 +622,7 @@ The following file formats are supported :: - sound = pygame.mixer.Sound("s.wav") + sound = pygame.Sound("s.wav") channel = s.play() # Sound plays at full volume by default sound.set_volume(0.9) # Now plays at 90% of full volume. sound.set_volume(0.6) # Now plays at 60% (previous value replaced). diff --git a/examples/aliens.py b/examples/aliens.py index 5d1fc65e08..fddb43df50 100644 --- a/examples/aliens.py +++ b/examples/aliens.py @@ -61,7 +61,7 @@ def load_sound(file): return None file = os.path.join(main_dir, "data", file) try: - sound = pygame.mixer.Sound(file) + sound = pygame.Sound(file) return sound except pygame.error: print(f"Warning, unable to load, {file}") diff --git a/examples/audiocapture.py b/examples/audiocapture.py index 3811a20567..c36c775cfd 100644 --- a/examples/audiocapture.py +++ b/examples/audiocapture.py @@ -70,8 +70,8 @@ def postmix_callback(postmix, audiomemoryview): time.sleep(5) -print("Turning data into a pygame.mixer.Sound") -sound = pygame.mixer.Sound(buffer=b"".join(sound_chunks)) +print("Turning data into a pygame.Sound") +sound = pygame.Sound(buffer=b"".join(sound_chunks)) print("playing back recorded sound") sound.play() diff --git a/examples/chimp.py b/examples/chimp.py index 1f0fecddfe..ea8d76e261 100644 --- a/examples/chimp.py +++ b/examples/chimp.py @@ -44,7 +44,7 @@ def play(self): return NoneSound() fullname = os.path.join(data_dir, name) - sound = pygame.mixer.Sound(fullname) + sound = pygame.Sound(fullname) return sound diff --git a/examples/sound.py b/examples/sound.py index fde2a45280..c0418813aa 100644 --- a/examples/sound.py +++ b/examples/sound.py @@ -25,7 +25,7 @@ def main(file_path=None): pygame.mixer.init(11025) # raises exception on fail # load the sound - sound = pygame.mixer.Sound(file_path) + sound = pygame.Sound(file_path) # start playing print("Playing Sound...") diff --git a/examples/sound_array_demos.py b/examples/sound_array_demos.py index 155a85ee76..700523b425 100644 --- a/examples/sound_array_demos.py +++ b/examples/sound_array_demos.py @@ -141,7 +141,7 @@ def main(): print(("-" * 30) + "\n") print("loading sound") - sound = pygame.mixer.Sound(os.path.join(main_dir, "data", "car_door.wav")) + sound = pygame.Sound(os.path.join(main_dir, "data", "car_door.wav")) print("-" * 30) print("start positions") @@ -195,7 +195,7 @@ def main(): while pygame.mixer.get_busy(): pygame.time.wait(200) - sound = pygame.mixer.Sound(os.path.join(main_dir, "data", "secosmic_lo.wav")) + sound = pygame.Sound(os.path.join(main_dir, "data", "secosmic_lo.wav")) t1 = time.time() sound3 = make_echo(sound, samples_per_second) diff --git a/src_py/__init__.py b/src_py/__init__.py index ac29abd3c8..bce9fe3ebb 100644 --- a/src_py/__init__.py +++ b/src_py/__init__.py @@ -273,6 +273,7 @@ def PixelArray(surface): # pylint: disable=unused-argument try: import pygame.mixer + from pygame.mixer import Sound from pygame.mixer import Channel except (ImportError, OSError): mixer = MissingModule("mixer", urgent=0) diff --git a/test/mixer_test.py b/test/mixer_test.py index 9b3551b834..5671f7c9f2 100644 --- a/test/mixer_test.py +++ b/test/mixer_test.py @@ -933,7 +933,7 @@ def todo_test_set_volume(self): # If the channel is playing a Sound on which set_volume() has also # been called, both calls are taken into account. For example: # - # sound = pygame.mixer.Sound("s.wav") + # sound = pygame.Sound("s.wav") # channel = s.play() # Sound plays at full volume by default # sound.set_volume(0.9) # Now plays at 90% of full volume. # sound.set_volume(0.6) # Now plays at 60% (previous value replaced). diff --git a/test/sndarray_test.py b/test/sndarray_test.py index a99171fc5a..1f67712f63 100644 --- a/test/sndarray_test.py +++ b/test/sndarray_test.py @@ -102,7 +102,7 @@ def check_sample(size, channels, test_data): __, sz, __ = pygame.mixer.get_init() if sz == size: zeroed = null_byte * ((abs(size) // 8) * len(test_data) * channels) - snd = pygame.mixer.Sound(buffer=zeroed) + snd = pygame.Sound(buffer=zeroed) samples = pygame.sndarray.samples(snd) self._assert_compatible(samples, size) ##print('X %s' % (samples.shape,)) @@ -146,7 +146,7 @@ def test_float32(self): self.skipTest("unsupported mixer configuration") arr = array([[0.0, -1.0], [-1.0, 0], [1.0, 0], [0, 1.0]], float32) - newsound = pygame.mixer.Sound(array=arr) + newsound = pygame.Sound(array=arr) pygame.mixer.quit() From 25c394eb97d9a0546ef20943f2ac47f1c2500572 Mon Sep 17 00:00:00 2001 From: Damiano Ricciardi Date: Mon, 19 Aug 2024 09:21:04 +0200 Subject: [PATCH 2/5] Revert example changes --- examples/aliens.py | 2 +- examples/audiocapture.py | 4 ++-- examples/chimp.py | 2 +- examples/sound.py | 2 +- examples/sound_array_demos.py | 2 +- test/mixer_test.py | 2 +- test/sndarray_test.py | 2 +- 7 files changed, 8 insertions(+), 8 deletions(-) diff --git a/examples/aliens.py b/examples/aliens.py index fddb43df50..5d1fc65e08 100644 --- a/examples/aliens.py +++ b/examples/aliens.py @@ -61,7 +61,7 @@ def load_sound(file): return None file = os.path.join(main_dir, "data", file) try: - sound = pygame.Sound(file) + sound = pygame.mixer.Sound(file) return sound except pygame.error: print(f"Warning, unable to load, {file}") diff --git a/examples/audiocapture.py b/examples/audiocapture.py index c36c775cfd..3811a20567 100644 --- a/examples/audiocapture.py +++ b/examples/audiocapture.py @@ -70,8 +70,8 @@ def postmix_callback(postmix, audiomemoryview): time.sleep(5) -print("Turning data into a pygame.Sound") -sound = pygame.Sound(buffer=b"".join(sound_chunks)) +print("Turning data into a pygame.mixer.Sound") +sound = pygame.mixer.Sound(buffer=b"".join(sound_chunks)) print("playing back recorded sound") sound.play() diff --git a/examples/chimp.py b/examples/chimp.py index ea8d76e261..1f0fecddfe 100644 --- a/examples/chimp.py +++ b/examples/chimp.py @@ -44,7 +44,7 @@ def play(self): return NoneSound() fullname = os.path.join(data_dir, name) - sound = pygame.Sound(fullname) + sound = pygame.mixer.Sound(fullname) return sound diff --git a/examples/sound.py b/examples/sound.py index c0418813aa..fde2a45280 100644 --- a/examples/sound.py +++ b/examples/sound.py @@ -25,7 +25,7 @@ def main(file_path=None): pygame.mixer.init(11025) # raises exception on fail # load the sound - sound = pygame.Sound(file_path) + sound = pygame.mixer.Sound(file_path) # start playing print("Playing Sound...") diff --git a/examples/sound_array_demos.py b/examples/sound_array_demos.py index 700523b425..fa0861ec36 100644 --- a/examples/sound_array_demos.py +++ b/examples/sound_array_demos.py @@ -141,7 +141,7 @@ def main(): print(("-" * 30) + "\n") print("loading sound") - sound = pygame.Sound(os.path.join(main_dir, "data", "car_door.wav")) + sound = pygame.mixer.Sound(os.path.join(main_dir, "data", "car_door.wav")) print("-" * 30) print("start positions") diff --git a/test/mixer_test.py b/test/mixer_test.py index 5671f7c9f2..9b3551b834 100644 --- a/test/mixer_test.py +++ b/test/mixer_test.py @@ -933,7 +933,7 @@ def todo_test_set_volume(self): # If the channel is playing a Sound on which set_volume() has also # been called, both calls are taken into account. For example: # - # sound = pygame.Sound("s.wav") + # sound = pygame.mixer.Sound("s.wav") # channel = s.play() # Sound plays at full volume by default # sound.set_volume(0.9) # Now plays at 90% of full volume. # sound.set_volume(0.6) # Now plays at 60% (previous value replaced). diff --git a/test/sndarray_test.py b/test/sndarray_test.py index 1f67712f63..ebc9a3e8fe 100644 --- a/test/sndarray_test.py +++ b/test/sndarray_test.py @@ -102,7 +102,7 @@ def check_sample(size, channels, test_data): __, sz, __ = pygame.mixer.get_init() if sz == size: zeroed = null_byte * ((abs(size) // 8) * len(test_data) * channels) - snd = pygame.Sound(buffer=zeroed) + snd = pygame.mixer.Sound(buffer=zeroed) samples = pygame.sndarray.samples(snd) self._assert_compatible(samples, size) ##print('X %s' % (samples.shape,)) From 783d45bc0a76f71ce256ba03b5e043c36639edac Mon Sep 17 00:00:00 2001 From: Damiano Ricciardi Date: Mon, 19 Aug 2024 09:22:17 +0200 Subject: [PATCH 3/5] Revert example in docs --- docs/reST/ref/mixer.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/reST/ref/mixer.rst b/docs/reST/ref/mixer.rst index d349983fd6..d23e2aa60a 100644 --- a/docs/reST/ref/mixer.rst +++ b/docs/reST/ref/mixer.rst @@ -622,7 +622,7 @@ The following file formats are supported :: - sound = pygame.Sound("s.wav") + sound = pygame.mixer.Sound("s.wav") channel = s.play() # Sound plays at full volume by default sound.set_volume(0.9) # Now plays at 90% of full volume. sound.set_volume(0.6) # Now plays at 60% (previous value replaced). From 675f0e3ecc6da13adaa5125c2424958a5dbb93d3 Mon Sep 17 00:00:00 2001 From: Damiano Ricciardi Date: Mon, 19 Aug 2024 09:23:08 +0200 Subject: [PATCH 4/5] Revert in one more example --- examples/sound_array_demos.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/sound_array_demos.py b/examples/sound_array_demos.py index fa0861ec36..155a85ee76 100644 --- a/examples/sound_array_demos.py +++ b/examples/sound_array_demos.py @@ -195,7 +195,7 @@ def main(): while pygame.mixer.get_busy(): pygame.time.wait(200) - sound = pygame.Sound(os.path.join(main_dir, "data", "secosmic_lo.wav")) + sound = pygame.mixer.Sound(os.path.join(main_dir, "data", "secosmic_lo.wav")) t1 = time.time() sound3 = make_echo(sound, samples_per_second) From 3447a196e5197e5c35f11b337cf63d1251159137 Mon Sep 17 00:00:00 2001 From: Damiano Ricciardi Date: Mon, 19 Aug 2024 09:24:02 +0200 Subject: [PATCH 5/5] Im about to go insane this vscode extension ain't telling me the full story every time --- test/sndarray_test.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/sndarray_test.py b/test/sndarray_test.py index ebc9a3e8fe..a99171fc5a 100644 --- a/test/sndarray_test.py +++ b/test/sndarray_test.py @@ -146,7 +146,7 @@ def test_float32(self): self.skipTest("unsupported mixer configuration") arr = array([[0.0, -1.0], [-1.0, 0], [1.0, 0], [0, 1.0]], float32) - newsound = pygame.Sound(array=arr) + newsound = pygame.mixer.Sound(array=arr) pygame.mixer.quit()