2
song = "choy_san_dou.ogg"
pygame.init()
pygame.display.set_mode((200,100))
pygame.mixer.init()
pygame.mixer.music.load(song)
pygame.mixer.music.set_volume(0.1)
pygame.mixer.music.play(0)

I am unable to find a way to fade in the volume when the song is loaded, means to have the volume of the song slowly increase until a max volume instead of blasting it off full volume from the very beginning. How to do it?

Another problem is this, I must declare

pygame.display.set_mode((200,100))

or the sound won't play. The program is actually running because after time of the sound has elapsed, the subsequent steps in the program continues. Why is it this way? I am testing it on windows pc, with the intention of transferring it to a raspberry pi later.

sam
  • 115
  • 2
  • 10
  • Hi @LPK, This is not what I want, is exactly the opposite. Fade in. – sam Jul 20 '16 at 00:44
  • @sam: It seems you can use the option of play function. Are you searching for this: http://gamedev.stackexchange.com/questions/62023/crossfade-between-two-music-tracks-in-pygame ? – Hoàng Long Jul 20 '16 at 01:20
  • @HoàngLong, this i think is the one I am looking for, but I don't quite understand the example given there, since I am very new in programming. Mind to explain and show how in code? Why need to define in a class? – sam Jul 20 '16 at 01:46
  • Sorry, I'm also a pygame newbie. I don't understand your question. What class is it? The answer in the above question clearly gives `play(loops=0, maxtime=0, fade_ms=0)`. You just need to specify fade_ms and it should work... Note that it use Sound, not Music. – Hoàng Long Jul 20 '16 at 02:20
  • If you see the way they use it in http://stackoverflow.com/questions/18733213/fade-between-two-music-tracks-in-progress-in-pygame, you will see that it will specify class in the very beginning of the program. Even if you refer to the http://www.pygame.org/docs/ref/mixer.html you see the below: class pygame.mixer.Sound. But after some trial and error, themesong = pygame.mixer.Sound(song) themesong.play(loops=0, maxtime=0, fade_ms=10000) is able to do it. Just that I do not totally understand the reasoning behind. And what's the difference of using Sound and Music? – sam Jul 20 '16 at 03:50

1 Answers1

2

The pygame.mixer module has a Sound class (doc: http://www.pygame.org/docs/ref/mixer.html) that takes a file name as input and converts the file contents to a Sound object. Sound objects have a method play() where you can specify an amount of time where the Sound is faded in (you likely want to make it much shorter than the actual length of the Sound). I suggest you read the documentation to get the gist of how pygame.mixer works. If you want to alter the Sound object, such as adding fade-in or fade-out, there's the pygame.sndarray module that can convert a Sound object to a numpy array and vice versa, though you'll need to use numpy or scipy to do stuff with that array (I recommend the free Anaconda package).
The difference between the music and the mixer module is that the music module STREAMS data to playback, while the mixer module handles data saved to main memory.

BatWannaBe
  • 4,330
  • 1
  • 14
  • 23