fSOUND


Quick Data

Header file:sound.h
Object name:fSOUND
Object index:1 of 1 Object

General Description

The fSOUND component is a wrapper for some of the multimedia system functions, which allows you to make the computer play some sounds and MIDI files. It's pretty basic, but sometimes useful.

During the last test, the MIDI sections of this class did not work. They kept giving me access violations. Your mileage may vary; but by all means have a go. If it doesn't work, you might have to 'just not use' those functions.

You may also need to link libwinmm.a to make the program compile - the exact filename will depend on your compiler (for example, under VisualC++ this would be mmsystem.lib or something like that).


Options for playing WAVE sounds


Name Description Value

wvReturnPlay When you play a wave file with this style, the sound will begin playing, and control will immediately return to the program that called it. SND_ASYNC
wvLoopPlay With this style, the sound that you start playing will play repeatedly until you stop it. Also, this function will immediately return control to the program (well, otherwise you can't stop the loop, can you?). SND_LOOP | SND_ASYNC
wvNoDefault With this flag, the function will not play the default sound if it can not find the file or data that you specified. Otherwise it will play the default sound, whatever that is. SND_NODEFAULT
wvIsPlaying This style will force the player function to return TRUE if there is a sound still playing, or FALSE otherwise. SND_NOSTOP
wvPlayReturn This style forces the function to play the specified sound, and then wait until it has finished before returning control back to the application. SND_SYNC

Methods for Wave Files


Prototype Description

bool PlayWaveResource(char* Name, int ID, int Flags); This function loads a wave file from the program's resources and plays it with the specified flags. Flags is one or more of the flags from the above table. This function returns TRUE if successful, or FALSE otherwise. Name specifies the name of the resource, or ID specifies the ID of the resource - please only use one or the other - ie. if you don't need Name, it should be NULL, and if you don't need ID, then it should be 0 (Zero).
bool PlayWaveResource(HINSTANCE Instance, char* Name, int ID, int Flags); This function is identical to the above function except for the addition of one parameter: Instance. Instance specifies the instance to load the resource from - this could easily be a DLL or another loaded module.
bool PlayWaveFile(char* FileName, int Flags); This function plays a WAVE file from a file on a disk. FileName specifies the filename to play from, and Flags is one or a combination of flags from the above table.
void StopWave(void); This function stops the wave file that might be currently playing. For example, if you started a loop, this function will stop it. Also, if you destroy this class, the sound will stop playing.

Methods for MIDI Files

To save repeatedly stating the same things, a few things should be mentioned here. Each of these functions have a parameter known as 'Alias'. That is merely a name by which the file that you are playing with is known as. You will use this name to refer to the file later on. It would be best if this name did not have spaces.

Finally, each of these functions return a bool value. It will be TRUE if the function was successful, or FALSE otherwise.


Prototype Description

bool LoadMIDIFile(char* Filename, char* Alias); This function loads the MIDI file specified by Filename.
bool SeekMIDIToStart(char* Alias); This function seeks a previously loaded MIDI file back to the beginning. Simple.
bool PlayMIDIFile(char* Alias); This function begins playing a previously loaded MIDI file.
bool StopMIDIFile(char* Alias); This function stops a previously started previously loaded MIDI file. In other words, it stops the MIDI file.
bool CloseMIDIFile(char* Alias); This function closes a MIDI file that you previously opened. Be sure to close a MIDI file when you are finished with it.

There are a few other MIDI functions, relating to obtaining the length of the MIDI file, but they are experimental, and have not worked so far. If you want to have a look, feel free to see sound.h.


Sample Code

The following snippet gives a basic idea of how to use this component.


//Firstly... code to play a WAVE file. Simple!
//First we need a sound object...
fSOUND Sound;

Sound.PlayWAVEFile("ding.wav", wvLoopPlay);
 //The above options make the file loop and makes the function
 //return control back to the application immediately.

//Now, when you get really, really sick of hearing 'ding'
//2 million times, call this function to make it stop!
Sound.StopWave();

//Now what about MIDI files? Not too hard, just like this...
Sound.LoadMIDIFile("canyon.mid", "MyMIDIAlias");
Sound.SeekMIDIToStart("MyMIDIAlias"); //Not essential, but recommended...
Sound.PlayMIDIFile("MyMIDIAlias");

//Now, stop it!
Sound.StopMIDIFile("MyMIDIAlias");

//Now I don't know too many MCI command codes, so that's why
//there isn't a Pause function or anything cool like that, so
//if anybody can help... please step forward!

Back to indexThe FreeFoote Foundation Classes Documentation