Game Development Reference
In-Depth Information
The
SoundSource
structure stores information about the loaded sounds. It
will only ever be used internally by the sound manager so the structure exists
inside the
SoundManager
class. When sound data is loaded in OpenAL an in-
teger will be returned; this is used to keep a reference of the sound for when it
needs to be played. The sound identifier maps a sound's name onto the sound
data in memory. The dictionary will be a big table of all the sounds available to
the game with an easy English language string to identify each one.
To load a sound file the
using System.IO;
statement needs to added to the
top of the file. The
LoadSound
function will fill up the
_soundIdentifier
dictionary with game sounds.
public void LoadSound(string soundId, string path)
{
// Generate a buffer.
int buffer = -1;
Al.alGenBuffers(1, out buffer);
int errorCode = Al.alGetError();
System.Diagnostics.Debug.Assert(errorCode == Al.AL_NO_ERROR);
int format;
float frequency;
int size;
System.Diagnostics.Debug.Assert(File.Exists(path));
IntPtr data = Alut.alutLoadMemoryFromFile(path, out format, out size,
out frequency);
System.Diagnostics.Debug.Assert(data != IntPtr.Zero);
// Load wav data into the generated buffer.
Al.alBufferData(buffer, format, data, size, (int)frequency);
// Everything seems ok, add it to the library.
_soundIdentifier.Add(soundId, new SoundSource(buffer, path));
}
The
LoadSound
method first generates a buffer. This buffer is an area in
memory where the sound data from the disk will be stored. The OpenAL utility
function
alutLoadMemoryFromFile
is used to read the data for a .wav into
memory. The memory containing the file data is then put into the buffer along
with the format, size, and frequency data that was also read in. This buffer is then
put into our dictionary using the
soundId
to identify it later.
Search WWH ::
Custom Search