Modern games can no longer have no sound, so the audio engine has become an indispensable part of the game engine. This is an article about the modern audio engine.Article(Http://hard.zol.com.cn/labs/2003/0520/60986.shtml). fmod audio engine (http://www.fmod.org) is a very good audio engine, its use is also relatively simple, below do some simple introduction:
I. Basic preparations
It is free. You can download APIs and other files from their websites. Then, you need to add header files and library files, as shown below (C/C ++ ):
Fmodvc. Lib is used for Microsoft Visual C ++ and codewarrior.
Fmodbc. Lib for borland
Fmodwc. Lib for Watcom
Fmodcc. Lib for LCC-Win32
Libfmod. A for mingw and cygwin
Fmod-3-7.lib for GCC
(Reference: http://www.gamedev.net/reference/articles/article2098.asp)
After that, you only need to add the fmod. h header file and then use it.
Ii. Start Using
1. Initialization
Initialization is required before playing a sound. It is very simple:
Fsound_init (44100, 32, 0 );
The first parameter is the output Hz, and the second parameter is the maximum number of software channels, regardless of the CPU burden. The third parameter can be set to 0 if some labels are not set.
2. Basic Knowledge
Fmod divides audio into two types: Sound and music. the former is like :. moD ,. s3m ,. XM ,. it ,. mid ,. RMI ,. sgt or. FSB
The latter, such :. wav ,. MP2 ,. MP3 ,. ogg or. raw, etc. they use different functions for processing. you can use the stream after sampling. however, small files are generally sampled and can be played multiple times but occupy memory. large files use stream mode to reduce memory consumption.
3. Play Music
First, define a variable of the fmusic_module type as the file handle. Then it can be implemented through the fmusic API, such:
Mount a file:
Handle = fmusic_loadsong ("yourfilename ");
Fmusic_playsong (handle );
Volume Control: fmusic_setmastervolume (handle, 255); the following parameters are 0 ~ The greater the value, the greater the sound.
Pause: fmusic_setpaused (handle, true );
Re-start: fmusic_setpaused (handle, false );
Loop playback: fmusic_setlooping (handle, true );
Stop playing: fmusic_stopsong (handle );
Release Audio memory: fmusic_freesong (handle );
The following is an example in command mode:
# Include <conio. h>
# Include "INC/fmod. H"
Fmusic_module * handle;
Int main ()
{
// Initialization
Fsound_init (44100, 32, 0 );
// Installation example
Handle = fmusic_loadsong ("Canyon. Mid ");
// Play only once
// Disable loop playback when playing a MIDI file
Fmusic_setlooping (handle, false );
// Play
Fmusic_playsong (handle );
// End with one click
While (! _ Kbhit ())
{
}
// Release
Fmusic_freesong (handle );
Fsound_close ();
}
4. Playing Sound
4.1 Sample Method
Define the fsound_sample type variables, and then use the fsound series functions, such:
File Installation:
Handle = fsound_sample_load (0, "yourfilename", 0); // parameters other than file names are used for multi-sample or other
Fsound_playsound (0, handle );
Set the volume: fsound_setvolume (handle, 255 );
Listen: fsound_setpaused (handle, true );
Start again: fsound_setpaused (handle, false );
Stop: fsound_stopsound (handle );
Release: fsound_sample_free (handle );
The following is a simple example:
# Include <conio. h>
# Include "INC/fmod. H"
Fsound_sample * handle;
Int main ()
{
// Initialization
Fsound_init (44100, 32, 0 );
// Load and play
Handle = fsound_sample_load (0, "sample.mp3", 0, 0, 0 );
Fsound_playsound (0, handle );
// End with one click
While (! _ Kbhit ())
{
}
// Release
Fsound_sample_free (handle );
Fsound_close ();
}
4.2 stream mode
First define a fsound_stream type variable, and then:
Mount a file:
Handle = fsound_stream_open ("yourfilename", 0, 0, 0 );
Fsound_stream_play (0, handle );
Tip: The methods before version 3.7 are different.
Stop: fsound_stream_stop (handle );
Release: fsound_stream_close (handle );
Others are the same as above. Below is a simple example:
# Include <conio. h>
# Include "INC/fmod. H"
Fsound_stream * handle;
Void main ()
{
// Init fmod sound system
Fsound_init (44100, 32, 0 );
// Load and play sample
Handle = fsound_stream_open ("sample.mp3", 0, 0, 0 );
Fsound_stream_play (0, handle );
// Wait until the users hits a key to end the app
While (! _ Kbhit ())
{
}
// Clean up
Fsound_stream_close (handle );
Fsound_close ();
}
5. Disable
Fsound_close ();
Refer:
A Quick Guide to fmod by Joachim Rohde (http://www.gamedev.net/reference/articles/article2098.asp)
Fmod Wiki (http://www.devmaster.net/wiki/FMod)