All about audio covers the background information you will need to get started with audio in your SwinGame. This will introduce you to the file formats supported, along with general information about loading and playing music and sound effects.
Getting Started with Audio
Music and sound effects make a huge difference to game play experience. SwinGame makes it easy for you to have both music and sound effects in your games.
Before you get started with audio you will need to get some sound files that you want played in your game. The following table outlines the file formats that SwinGame supports. A tick in the sound effect column means that the format can be used for sound effects, similarly a tick in the music column means it can be used for music.
Sound/Music Whats the difference?
Sound effects should be used for short sounds you play in response to some action or event in your game. These will be loaded into memory before they are played.
Music can be longer, and is usually played constantly while the game runs. Unlike sound effects, music is not loaded in its entirety before it starts playing. Instead the music data is read as it is played.
Basics of Sound
Before using sounds in SwinGame you must first call OpenAudio, this procedure opens the audio device which allows you to play sounds through SwinGame.
Once SwinGame's audio is open you can start to load and play music and sound effects. To load a sound effect you can use either the Load Sound Effect or Load Sound Effect Named functions. Similarly, there is the Load Music and Load Music Named functions to load music.
To play music there is the Play Music procedure, this can be passed either music data or the name of the music to play (this is the name you gave it when you loaded it). Similarly you can use Play Sound Effect to play sound effects. This can be passed the sound effect or the name you loaded the sound as.
Sound is played until the file stops or until it is manually stopped in your code using Stop Music or Stop Sound Effect. The sound effect or music will play in the background while your program's code continues to run.
On Windows a graphics window needs to be displayed before audio will play.
Multiple sounds can be played at one time using SwinGame, these sounds can be mixed using channels. Each channel can only play one SoundEffect at a time. So every call to PlaySoundEffect will cause a channel that is not currently playing any sound to play the SoundEffect. When there are no free channels SwinGame will stop a channel from playing it's current SoundEffect and play the new SoundEffect from the start.
SwinGame will automatically allocate as many channels as it can for the system it's running on.
Music is different from SoundEffects in that Music has it's own reserved channel that only Music can be played on. Music, like SoundEffects will continue to be played until either the Music is finished or StopMusic is called.
Playing Multiple Music Files
Since Music has one channel to itself, if you call PlayMusic when Music is already playing the currently playing Music will be stopped and the new Music will start playing from the beginning.