irrKlang:     About     Features    Tutorials    Download    irrKlang Pro    FAQ    License    Forum    API    API.NET    Buy now

irrKlang Tutorial: Sound Effects (C++)

Let's start

Include the irrKlang headers and other input/output stuff needed to print and get user input from the console. And as exlained in the first tutorial, we use the namespace irrklang and link to the irrKlang.dll file.

#if defined(WIN32)
  #include <conio.h>
  #include "../common/conio.h"

#include <stdio.h>
#include <stdlib.h>
#include <irrKlang.h>
using namespace irrklang;

#pragma comment(lib, "irrKlang.lib") // link with irrKlang.dll

Now let's start with the irrKlang 3D sound engine example 05, demonstrating sound effects. Simply startup the engine using using createIrrKlangDevice() with default options/parameters.

int main(int argc, const char** argv)
  // start the sound engine with default parameters
  ISoundEngine* engine = createIrrKlangDevice();

  if (!engine)
    return 0; // error starting up the engine

Now we play a .xm file as music here. Note that the last parameter named 'enableSoundEffects' has been set to 'true' here. If this is not done, sound effects cannot be used with this sound. After this, we print some help text and start a loop which reads user keyboard input.

  ISound* music = engine->play2D("../../media/MF-W-90.XM",
    true, false, true, ESM_AUTO_DETECT, true);

  // Print some help text and start the display loop

  printf("\nSound effects example. Keys:\n");
  printf("\nESCAPE: quit\n");
  printf("w: enable/disable waves reverb\n");
  printf("d: enable/disable distortion\n");
  printf("e: enable/disable echo\n");
  printf("a: disable all effects\n");

  while(true) // endless loop until user exits
    int key = getch();

    if (key == 27)
      break; // user pressed ESCAPE key
      // user maybe pressed an effects key,
      // now enable or disable a sound effect.

We get a pointer to the ISoundEffectControl interface, but this only exists if the sound driver supports sound effects and if the sound was started setting the 'enableSoundeffects' flag to 'true' as we did above. This pointer is only valid as long as we don't call music->drop() and delete the music with this.

    ISoundEffectControl* fx = 0;
    if (music)
      fx = music->getSoundEffectControl();

    if (!fx)
      // some sound devices do not support sound effects.
      printf("This device or sound does not support sound effects.\n");

Now we disable or enable the sound effects of the music depending on what key the user pressed. Note that every enableXXXSoundEffect() method also accepts a lot of parameters, so it is easily possible to influence the details of the effect. If the sound effect is already active, it is also possible to simply call the enableXXXSoundEffect() method again to just change the effect parameters, although we aren't doing this here.

    case 'd':
      if (fx->isDistortionSoundEffectEnabled())

    case 'e':
      if (fx->isEchoSoundEffectEnabled())

    case 'w':
      if (fx->isWavesReverbSoundEffectEnabled())

    case 'a':

The program is now nearly finished, just shut down the engine and release the music memory now.

  // don't forget to release the resources
  if (music)
    music->drop(); // release music stream.

  engine->drop(); // delete Engine

That's it.
Download tutorial source and binary (included in the SDK)

Back to the tutorial overview

Copyright© 2021 Ambiera e.U. all rights reserved.
Contact | Imprint | Products | Privacy Policy | Terms and Conditions |