Introduction to Game Programming with C++ (Wordware Game Developers Library)

20.2 FMOD

In this section we'll examine a method to play various media files like MP3 and the fantastic OGG format using the FMOD library.

FMOD is a cross-platform music and audio library. It's not free for commercial development, however, and the price varies depending on a developer's product and situation. The FMOD SDK can be downloaded for free from http://www.fmod.org/.

Figure 20.1

Some of FMOD's features and properties are listed below. Supported Platforms

Supported Audio File Formats

20.2.1 Installing and Configuring

Once downloaded, FMOD is easy to install using the wizard, which installs FMOD to the computer. All the libraries and documentation will be stored in the FMOD folder. To use FMOD, the fmod.hpp file must be included in projects using the #include preprocessor directive; for example, #include<fmod.hpp>. For Visual Studio, the library fmodex_vc.lib should be linked, and for Code::Blocks and Dev C++, the libfmodex.a library should be linked.

20.2.2 Playing Sound and Music

The process of playing a sound file in FMOD is quite simple. The following sample program demonstrates how a sound file can be played.

FMOD_RESULT result; FMOD::Sound *sound; FMOD::Channel *channel; FMOD::System *system; result = FMOD::System_Create(&system); system->init(200, FMOD_INIT_NORMAL, 0); system->createSound("sample.mp3", FMOD_DEFAULT, 0, &sound); system->playSound(FMOD_CHANNEL_FREE, sound, false, 0);

That's all there is to it. The above code creates two important objects: FMOD::Sound and FMOD::System. FMOD::System represents the actual sound playing hardware attached to the computer, and it acts like the media player controls on a stereo, allowing programmers to start, stop, and pause playback of music. The FMOD::Sound class encapsulates the actual music file, in this case sample.mp3. Naturally, this could be any other sound file in a valid format. Finally, the playSound method of FMOD::System requires a sound to play, a channel to play in, and a flag to determine whether the sound should be repeated.

Категории