0
\$\begingroup\$

My intention is to have two music tracks, which are similar in nature, fade between each other at various times. When such a fade occurs, one music track should fade from full volume to muted in a short period of time, and, simultaneously, the other track should fade from 0 to 100 and continue playing from there. They must be able to do this dynamically at any time - when a certain action occurs, the fade will occur and the new track will start playing at the same time index that the other one left off at.

This might be plausible by either using volume manipulation or by starting and stopping the music (however, it appears that only a "fadeout" option exists, and there is a lack of a "fadein" option). How can I do this? What is the best method, if any, that exists? If it is impossible using Pygame, alternatives to Pygame are acceptable.

\$\endgroup\$
  • \$\begingroup\$ Isn't this a cross-post? \$\endgroup\$ – pradyunsg Mar 4 '14 at 9:49
  • \$\begingroup\$ @Schoolboy Yes, I did so before I knew that was bad, got away with it, and had it work out rather usefully. All the answers on both sites contributed into me finally solving my problem, so it worked out great. \$\endgroup\$ – Southpaw Hare Mar 4 '14 at 14:35
3
\$\begingroup\$

There is a "fadein" option, it just isn't a function. From the pygame.mixer.Sound documentation:

play()

begin sound playback

play(loops=0, maxtime=0, fade_ms=0) -> Channel

...

The fade_ms argument will make the sound start playing at 0 volume and fade up to full volume over the time given. The sample may end before the fade-in is complete.

\$\endgroup\$
  • \$\begingroup\$ Is it also possible to start from a given manual position using this method? \$\endgroup\$ – Southpaw Hare Sep 11 '13 at 13:19
  • \$\begingroup\$ You mean in the middle of a track? I suppose not. The only way is to pause and then play, which is not really what you're after. \$\endgroup\$ – bradur Sep 11 '13 at 15:10
1
\$\begingroup\$

There is no easy method with pygame.

pygame uses sdl_mixer and shares its limitations. sdl_mixer only supports one streamed, music channel. You could use sound effects instead and manage your own sound samples and channels, but it's a lot of work:

  • Track the progress of each piece of music, including the channel it was played on
  • Without streaming, you have to preload the entire music file into memory. It's a lot because we're talking about uncompressed audio, you're looking at many 10s of MB per minute, depending on sampling rate and bit depth.

So really, you're rolling a crossfade library from scratch.

If you limit your requirements so that you crossfade only at the end/start of tracks, you could create a transition music, and play music1, transition, music2 in that order. It's still annoying to do though.

For more details see this thread: http://forums.indiegamer.com/showthread.php?12641-Cross-fading-with-SDL_mixer

\$\endgroup\$
  • \$\begingroup\$ Unfortunately, there is no "start" or "end" to the pieces - they must loop indefinitely, and when one piece fades in, it must resume at the same place that the other piece stopped at, making a seamless transition. \$\endgroup\$ – Southpaw Hare Sep 11 '13 at 13:18

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.