Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How would I be able to pan a sound in pygame?

Basically, what I want to accomplish here, is a way to pan the sound while the game is running. I'd like to make the volumes (left and right) change depending on the position of the player. For now I've got a simple code that I thought that would be a good test:

pygame.mixer.init()

self.sound = pygame.mixer.Sound("System Of A Down - DDevil #06.mp3")
print("I could get here")
self.music = self.sound.play()

self.music.set_volume(1.0, 0)

First, I tried something similar but with pygame.mixer.music, but I came to realize that there's no way to change the volumes separately this way, or so I think, then I changed to the code presented here. Now it seems like the file is not able to be loaded, my guess is that the file is too big to be treated as a sound in pygame. Any idea of how I'd be able to make this work?

like image 578
Matheus Fernando Avatar asked Jan 18 '26 12:01

Matheus Fernando


2 Answers

You can pan on the Channel like this:

from os import split, join
import pygame
import pygame.examples.aliens
pygame.init()
# get path to an example punch.wav file that comes with pygame.
sound_path = join(split(pygame.examples.aliens.__file__)[0], 'data', 'punch.wav')
sound = pygame.mixer.Sound(sound_path)
# mixer can mix several sounds together at once.
# Find a free channel to play the sound on.
channel = pygame.mixer.find_channel()
# pan volume full loudness on the left, and silent on right.
channel.set_volume(1.0, 0.0)
channel.play(sound)

https://www.pygame.org/docs/ref/mixer.html#pygame.mixer.Channel

like image 132
René Dudfield Avatar answered Jan 20 '26 03:01

René Dudfield



It may be worth looking into a seperate audio library for this. In general i'd recommend PortAudio (which is C), but using the python bindings for it provided by PyAudio. This will give you much more control over the exact audio stream.

To simplify this even further, there is a library known as PyDub which is built ontop of PyAudio for a high level interface (it even has a specific pan method!).


from pydub import AudioSegment
from pydub.playback import play

backgroundMusic = AudioSegment.from_wav("music.wav")

# pan the audio 15% to the right
panned_right = backgroundMusic.pan(+0.15)

# pan the audio 50% to the left
panned_left = backgroundMusic.pan(-0.50)

#Play audio
while True:
    try:
       play(panned_left)
      #play(panned_right)

If this is too slow or doesn't provide effective real-time implementations then I would definately give PyAudio a go because you will also learn a lot more about audio processing in the process!

PS. if you do use PyAudio make sure to check out the callback techniques so that the game you are running can continue to run in parallel using different threads.

like image 34
WoodyDev Avatar answered Jan 20 '26 04:01

WoodyDev



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!