Audio

From SDL.NET

Table of contents

Introduction

SDL.NET's audio is comprised of a number of different classes for playing sound and music file formats. This tutorial just goes over the features provided with audio in the SDL.NET library. For a live demo, please see the Audio Example. Since SDL.NET uses SDL_mixer and SMPEG for playing audio, it has a strong list of capable formats:

  • Microsoft WAVE files
  • Creative Labs VOC files
  • MIDI files via Timidity
  • MikMod: .MOD .S3M .IT .XM.
  • Ogg Vorbis streams
  • SDL.NET does not support MP3 format because the underlying C library, SDL_mixer, does not officially support it

These are split into being either Sound or Music. Sound being simple wav files and Music being midi, mod and ogg files.

Sound

To load a sound file from the disk, you have to first create the sound object:

Sound bang = new Sound("bang.wav");

To play the sound you use:

bang.Play();  // Play sound once.
bang.Play(4); // Play sound four times.

When you play a sound, it returns the channel that it's playing on. This means you can customize the playing sound even more:

Channel channel = bang.Play();
channel.SetPanning(0, 255); // Set the panning of the sound on the right.
channel.Pause();  // Pause the sound
channel.Resume(); // Continue the sound

The maximum number of channels determines the number samples which can be played concurrently, by default the mixer is setup with a maximum of 8 channels. To change the number of channels allocated, set the Mixer Channels Allocated property:

Mixer.ChannelsAllocated = 1000; // Set the maximum number of channels to 1000

Each time the Sample.Play() method is called, a free channel is allocated to the sample before it is played (i.e. if channel 1,2 & 3 are currently playing but channel 4 is free, the sample will be allocated to channel 4 and played) - If the same sample is played multiple times additional channels will be allocated to accommodate this and the sample will be played concurrently multiple times. (FYI - This is different to the MS DirectX concept of secondary buffers, where calling the Play method on a secondary buffer would have no audible effect if the buffer is already playing)


Music

The use of music is much the same as using the sound class.

Music techno = new Music("techno.ogg");
techno.Play(true);    // Play forever

The music class has many features:

techno.Volume = 150;  // Turn down the volume
techno.FadeOut(2000); // Fade out for 2 seconds

Queueing music is a very nice feature when you want to play through a list of music files:

Music.EnableMusicFinishedCallback();

Music rock = new Music("rock.mid");
techno.QueuedMusic = rock;

rock.QueuedMusic = techno;

This means that the rock will play after techno and will play rock again after the techno finishes. We gotta mix up those beats.

Dictionaries

Uses the sound and music classes are very nice, but what happens if you have hundreds of different sounds or hundreds of music tracks and want to manage them all at once. You can turn to the SoundDictionary and MusicDictionary classes. These are basically dictionary collections that help you store any amount of sounds and music by using a key-value system.

// Start the dictionary
SoundDictionary sounds = new SoundDictionary ();

// Add the sounds
sounds.Add( "boing", new Sound("boing.wav") );
sounds.Add( "explosion", new Sound("explosion.wav") );

// Play some of the sounds
sounds["boing"].Play();     // Play the boing
sounds["explosion"].Play(); // Play the explosion!

The management of a MusicDictionary is very much the same:

MusicDictionary music = new MusicDictionary();

music.Add("rock", new Music("rock.mid") );
music.Add("techno.ogg"); // Add the music using the key as the filename

// Play the music
music["rock"].Play();
music["techno.ogg"].Play();

Another feature to the music dictionary is that you can dynamically create a music queue list by calling the creatively named CreateQueueList() method. This will automatically setup all queueing for all music within the collection.

Events

There are two events that can be triggered when audio finishes:

  • ChannelFinishedEventHandler
  • MusicFinishedEventHandler

These can be used to dynamically load a music file when the music finishes, or to start playing a new sound when one sound stops.

Conclusion

Now that you know of the classes included in the SDL.NET for audio, you can put them to good use. The Audio Example is an available demo of the sound and music classes in action.

See Also