Playing background music

Music playback works similarly to the sound sample playback with one exception that there's only one music channel and it's controlled separately from the other mixing channels.

What's more, music playback can be moved into a specific position, so it can be used as a simple music player if that's your intention.

Getting ready

The music channel is allocated automatically, so there's no need to allocate more mixing channels to play music.

How to do it…

The first step in playing music is:

  1. To load a sound file into memory with the SDL.Mix_LoadMUS function. In this case, you can use a different set of file formats, such as WAV, MOD, MIDI, OGG, MP3, or even FLAC. This function returns a handle upon success similar to sound samples. If there's a problem loading sound files, it returns the nil value:
    local fileName = 'sound_file.MP3'
    local musicHandle = SDL.Mix_LoadMUS(fileName)
  2. From this moment, you can use various functions to control music. The first one, SDL.Mix_PlayMusic, is to start music playback. The following lines of code show how to play music twice.
    local loops = 1
    SDL.Mix_PlayMusic(musicHandle, loops)
  3. You can pause and resume music playback by calling the SDL.Mix_PauseMusic or SDL.Mix_ResumeMusic function without any parameters.
    SDL.Mix_PauseMusic()
    SDL.Mix_ResumeMusic()
  4. Music playback can be stopped in two ways. Either you stop the music immediately or slowly fade the volume down. For the first case, there is the SDL.Mix_HaltMusic function, and for the fade out effect, you can use the SDL.Mix_FadeOutMusic function:
    local time = 500 -- 500ms
    SDL.Mix_HaltMusic()
    SDl.Mix_FadeOutMusic(time)

    There is a fade-in effect for the music as well. This can be achieved with the SDL.Mix_FadeInMusic function:

    local loops = 0
    local time = 500 -- 500ms
    SDL.Mix_FadeInMusic(musicHandle, loops, time)
  5. The music position can be controlled with a set of two functions: SDL.Mix_RewindMusic and SDL.Mix_SetMusicPosition. You can either rewind the position to the beginning of the song or move to a specific time. The latter one can be tricky because SDL.Mix_SetMusicPosition will interpret the time value differently depending on the type of song. OGG files use the time value as a position from the beginning of the song in seconds. MP3 files use it as a relative position in seconds. And lastly, MOD files will cast the value into 16-bit unsigned integers that mark a pattern number.
    local position = 1.5 -- 1.5s
    SDL.Mix_RewindMusic(musicHandle)
    SDL.Mix_SetMusicPosition(position)
  6. Don't forget to free up the allocated resources after use with the SDL.Mix_FreeMUS function:
    SDL.Mix_FreeMUS(musicHandle)

    Tip

    Be careful not to use the sound sample handle in the music function by mistake as it could lead to an application crash!

How it works…

LuaSDL always reserves one special audio mixing channel for music playback. It's controlled by a separate set of functions, so it's guaranteed that sound samples won't disturb the music playback.

The SDL.Mix_LoadMUS function can decode other kinds of sound formats, such as MP3 and FLAC. You can't use these formats for sound playback because of the design decisions behind the SDL_Mixer library. These sound formats are better suited for streaming of audio data mainly because of the latency incorporated in audio decoding routines.

Handling of position change in music is not an easy problem to solve because you can easily disturb music playback by a sudden change in sound buffer content. Audio decoding works with chunks of sound data with various sizes depending on the used bit rate of the encoded audio file. That's one of the reasons why you can't use this feature with sound samples.

See also

  • The Playing sound samples recipe
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.188.190.175