MCS273 Introduction to C++ Programming (January 2013)

irrKlang Tutorial

In order to use sound in your program, you will be using external libraries to provide sound playback functions. There are many out there but irrKlang offers a free library for C++ that you can use.

The irrKlang libraries are already installed on Mac OS X in the Olin lab so all you need to download is the header files for your programs. I made this available in the "Useful Links" section.

Once you download the header files, you want the directory structure to look like this:

project directory
|
+-- irrKlang
|   |
|   irrKlang header files here.
|
|
+-- cccfiles
|   |
|   ccc header and obj files here.
|
|
your Makefile and source codes here.

Sound Example

First, you need to include a few header files for the sound to work.
#include <stdio.h>
#include <irrKlang.h>
#include <conio.h>
Then, you want to use irrklang name space so that you do not have to include irrklang:: every time you use functions provided by the irrKlang library.
using namespace irrklang;
Once inside your main funciton, you need to define and initialize the sound engine to use.
ISoundEngine* engine = createIrrKlangDevice();

// error check on sound engine
if (!engine) {
  cerr << "Could not startup engine" << endl;
  return 1;
}
To play sounds, you can use the function play2D in the following manner.

engine->play2D(<sound file name>)

OR

engine->play2D(<sound file name>, true)

If you supply only the sound file name, it will play that sound only once. If you use play2D with the second parameter true, it will keep playing the sound file repeatedly until the program ends.

For example, engine->play2D("bell.wav") will play bell.wav file once while engine->play2D("bell.wav", true) will play bell.wav repeatedly until your program terminates.

The sound file formats you can use are:

  • RIFF WAVE (*.wav)
  • Ogg Vorbis (*.ogg)
  • MPEG-1 Audio Layer 3 (*.mp3)
  • Free Lossless Audio Codec (*.flac)
  • Amiga Modules (*.mod)
  • Impulse Tracker (*.it)
  • Scream Tracker 3 (*.s3d)
  • Fast Tracker 2 (*.xm)

At the end of the code, make sure you deallocate the sound engine gracefully by putting:

engine->drop();

Compiling

Unfortunately, irrKlang only provides 32-bit libraries. Therefore, we need to force g++ to compile the code in 32-bit mode. You can add -m32 option to g++ to compile your code in 32-bit. In addition, you now have two subdirectories where you want to include files from. You can repeat -I option for g++ for this purpose.