Here are modified pyGame SOUND modules working with Blender 2.49b

Hi,
Problem:
PyGame sound in 2.49b comes with a delay of 0.5sec after it has been triggered.
Solution:
Recently I was able to modified pyGame and make it work properly with Blender 2.49b.
If you want to use it you will need to extract this pyGame folder and replace the original SDL.dll with this SDL.dll (or blender 2.45 SDL)in your Blender main folder.
That’s all. Restart Blender and your pyGame sounds should work just fine.

P.S.I made this archives very quickly, so I might have forgotten some files. Hopefully not.

Some specific Operating System only tips, I guess?!

pyGame folder is for all OS’es, SDL.dll is win only…I think, but you can use the SDL…from blender.2.45 instead.

Ok, thank you!

thank you i will try

That’s all. Restart Blender and your pyGame sounds should work just fine.
only sound?

only sound?

What do you mean? I think it’s not possible to use other pyGame modules with blender ge.(expl:you can not render the pyGame sprites in BGE)

I was thinking that you implemented the whole pyGame thing to the Blender Engine,

so thats why im confused as i read ‘sound should work’ .
thats all. so to keep things clear, this only the sound ^^?

greets equal

so to keep things clear, this only the sound ^^?

Yes! This fix is only for the pyGame sound module - “mixer”

P.S. I’ll change the title of this thread to be more clear.

sorry, can you give me specific changes you made? I don’t understand what was the problem before :confused: That’s because I never used pygame sound…

But I’m thinking of using pygame for sound, but I don’t know what you fixed and what’s the difference…

Thanks!

pyGame sound in 2.49b comes with a delay of 0.5sec after it has been triggered.
This modified files should fix the problem+you should copy the 2.45 SDL.dll and replace it in your 2.49b folder.

…because I never used pygame sound…

I strongly suggest you to learn and use it! PyGame sound module provides to Blender professionally handled sound management. I doubt that, Blender2.5 sound system will be more advanced, than pyGame sound, but who knows.

p.s. It is kind of “ugly” fix, but it works.

Im getting an error.

“The procedure entry point PyImport_ImportModuleLevel could not be located in the dynamic link library python26.dll.”

Can you help me with this? I’m desperate to get pygame sounds working in blender!

Can you post an example blend file. It is possible that, you are missing some files.

I’ve been using your build for a while now but I’ve been getting quite a lot of crashes using pyGame. (crash when playing a certain track twice for example)
I’ll re-install Blender to see if that’ll fix the problem. (I’ve been using a modified Blender 2.49b that supported OGG sounds but it increased loading times and I didn’t want that, so I switched to pyGame and I am still glad that you posted this solution to the trigger-lag)
I’ll keep you informed.

I’m glad, that this mod is useful for you.

've been using your build for a while now but I’ve been getting quite a lot of crashes using pyGame. (crash when playing a certain track twice for example
I’m using official blender 2.49b build and it is quite stable. Never had any of this problems.

That’s odd since the re-install didn’t fix the problem. Hmmm. It could be the Python library files I copied into the binary directory of my game. (all game files are seperated into several dirs, I keep the binaries in one too, makes relative paths easier to set too, lol it helps a lot)

Hmm, how you handle the sound files in python?.. 'cause this may be the issue.
I strongly suggest this architecture(for initializing pygame sounds) :


import GameLogic as g
import pygame.mixer

<i># initializing the PyGame mixer</i>
pygame.mixer.init()

g.Mixer = pygame.mixer
<i># Set number of max used sound channels(means that 12 different sounds can be played at once)</i>
g.Mixer.set_num_channels(12)
#--------------------------------------------------------------------------------

g.SoundPath="some/sound_dir/path/" #<i> define external sound directory

</i>#<i> Set the music channel and sound file</i>
g.Mixer.music.load(g.SoundPath+"musicFile.ogg")
g.Mixer.music.play(-1) <i>#---loop the music forever</i>
pygame.mixer.music.set_volume(.35) <i># set the music channel volume</i>
#---------------------------------------------------------------------------------
g.Sounds = {} <i># create empty dictionary</i>

<i># Fill the dictionary with pointers to the sound files</i>
g.Sounds["sound1"] = pygame.mixer.Sound(g.SoundPath+"soundFile1.ogg")
g.Sounds["sound2"] =  pygame.mixer.Sound(g.SoundPath+"soundFile2.ogg")

And then use the g.Sounds dictionary form anywhere like this:


g.Sounds["sound1"].set_volume(0.5) <i># Set the sound volume</i>
LOOPS=3 #<i> Sound loops count</i>
<i># Play the sound</i>
g.Mixer.find_channel().play(g.Sounds["sound1"],LOOPS)

Important Using g.Mixer.find_channel().play('.....') instead of g.Mixer.play('....') is essential for playing more than one sound at once. Otherwise the sounds will overlap or you will here only one of them. find_channel() - automatically finds an empty sound channel and plays the sound there.

P.S. Ohh, and one last very important thing. when you quiting the game after pressing ‘P’ in blender…the game quits alright. But the pygame mixer is still running as a background process. What you need to do is add this line to your ‘quit game’ script:

g.Mixer.quit()
or
import pygame.mixer
pygame.mixer.quit()

I am trying to keep the sound entries seperated from the initialization file.
The problem is that I will have to re-import the pygame.mixer library and “GameLogic as g”
Anyone know a way to fix that?

The sound scripts are located in /Scripts/sound/
The sounds are located in /Sounds/

init.txt

import GameLogic as g
import pygame.mixer

# initializing the PyGame mixer
pygame.mixer.init()

g.Mixer = pygame.mixer
# Set number of max used sound channels(means that 12 different sounds can be played at once)
g.Mixer.set_num_channels(12)
#--------------------------------------------------------------------------------

g.SoundPath="../Sounds/" # define external sound directory

# Set the music channel and sound file
g.Mixer.music.load(g.SoundPath+"testogg.ogg")
g.Mixer.music.play(-1) #---loop the music forever
pygame.mixer.music.set_volume(1.0) # set the music channel volume
#---------------------------------------------------------------------------------
g.Sounds = {} # create empty dictionary

ambient_3.txt

import pygame.mixer

GameLogic.Sounds["ambient_3"] = pygame.mixer.Sound(GameLogic.SoundPath+"/ambient/sci-fi - ambient 03.wav")
GameLogic.Sounds["ambient_3"].set_volume(0.5) # Set the sound volume
LOOPS=1 # Sound loops count
# Play the sound
GameLogic.Mixer.find_channel().play(GameLogic.Sounds["ambient_3"],LOOPS)

You have this definition in the init file:

g.Mixer = pygame.mixer

So in ambient_3.txt you can use instead of :

import GameLogic
import pygame.mixer
GameLogic.Sounds[“ambient_3”] = pygame.mixer.Sound(GameLogic.SoundPath+“/ambient/sci-fi - ambient 03.wav”)

this:
import GameLogic as g

g.Sounds[“ambient_3”] = g.Mixer.Sound(GameLogic.SoundPath+“/ambient/sci-fi - ambient 03.wav”)

So you dont have to import the mixer again.

BUT the nicer way to manage sounds is what I’d done in Krum.
Write a sound manager class that holds all the methods you need to manage the sounds(play,stop,frame sounds, randomize…etc).
And then just call some method with a parameter from anywhere you want.

Example (not real):
soundSystem.py:

import GameLogic

def playRandomSound(soundLib):
randomSoundFile=random(5)
GameLogic.Mixer.find_channel().play(GameLogic.Sounds[soundLib[randomSoundFile])

And than you just call this method from anywhere you want, like this:

import soundSystem
import GameLogic as g
soundSystem.playRandomSound(g.StepsLibrary)

P.S. this code is not real so it might not work in the python api, but that is the general idea.

Thanks a lot for the help. I’ve setup a sound system script like you did.
Now I just have to figure out again how to make the sensors stop triggering the scripts twice.
I found a solution once but I forgot how I fixed it.

The sensor triggers twice, on and off. Tap doesn’t seem to fix the problem.
The sound should be fired only once but it’s played twice since the trigger outputs ‘True’ twice.

It also sounds muffled: http://www.youtube.com/watch?v=auVJBEaEx5I

FIXED: Muffle issue fixed. I forgot that the Sound module only plays mono sounds. My sound was stereo.