How To: Music and sounds

In this tutorial you will learn how to work with the Audio sub system. You can set playback of any music in a section and escape the limits of the SMBX engine.

Note: Some features which are presented in this page (such as Audio namespace) are available since LunaLUA version v0.7.

Playing built-in sounds
You can play any available sounds by it's ID by using this function: This example will play the SMBX's sound with ID 23. The full list of built-in SMBX's sounds is listed here.

Playing custom sounds
You can play any custom sounds that you've place in your custom level folder:

When you will call this function, the sound file "my ultra-mega cool sfx.ogg" will be played once. You can use any files of supported formats.

You also can make advanced usage of sounds if you will use Mix_Chunk pointers: This example will play a "loco bell.ogg" file trice when this function will be called

Playing custom sound loops
This is an example SFX loop usage: This is a little example of a plane sound which flies above us and go out after 10 seconds.

Play music from another section
The simplest method of music playback usage is a using of musics from another sections

This function will play a configured music from 5th section

Set custom music on the fly and play it
You have the ability to play any available custom music file from your level's custom directory if you use a special function:

Stop music
You can stop playing of current music by setting the silence into the current section:

Fade In music
Now, let's play same music file with fade-in effect in 5 seconds: Adding one extra argument will give you the fade-in effect! The value is in milliseconds. Tip: When you setting the music ID into 0 (aka Silence), you'll run the fade-out effect.

Fade Out music
You can turn the silence beautifully! You only need to call the next function:

Example: Playlist
This is an example of usage multiple musics in one section

This is an example of shuffle music player for 2'nd section which plays each 20 seconds new music from play-list - an array which contain the list of music files which presented in the level custom folder.

Advanced way (for old LunaLua older than 0.7.2.3)
You have the ability to play any available custom music file from your level's custom directory if you use a special function:

Note 1: Some music formats supports extra arguments (for example, track number in the music file, or synthesizer flags, more detailed info and how to use here)

Note 2: To take working a custom musics by Lua code, you should set current music of target section to any default music and insert into the onLoad event function the call of special function:

This example seizing the music stream for 1'st section. When your character in 1'st section, you will have able to correctly use the musicOpen and any other functions.

Note: Don't forget to set to the target section any default music to confuse SMBX engine to take it think than "music is playing". This way will prevent appear of a bugs on switching between another applications.

Example: Playlist (for old LunaLua older than 0.7.2.3)
This is an example of usage multiple musics in one section

This is an example of shuffle music player for 2'nd section which plays each 20 seconds new music from play-list - an array which contain the list of music files which presented in the level custom folder.