oxygine sound - oxygine/oxygine-framework GitHub Wiki

Sounds

oxygine-sound is extension for oxygine-framework. It is based on OpenAL library and could be used for playing ogg files. It supports streaming for long sound tracks and using separate thread for decoding and reading ogg files.

mp3 and wav file formats are not supported

Usage

Include headers:

#include "oxygine-sound.h"

Initialize sound system with 16 channels:

SoundSystem::create()->init(16);
SoundPlayer::initialize();

SoundPlayer::initialize registers new type of resource "sound". It should be called before loading sounds.

Add your ogg tracks to resources file, lets call it "sounds.xml":

<?xml version="1.0"?>
<resources>
	<set path = "sounds" />	

 	<sound file="sfx1.ogg" />
	<sound file="sfx2.ogg" />
	<sound file="voice1.ogg"/>
	<sound file="music.ogg" streaming="true"/>

</resources>

you could have multiple resources files

  • If sound is too short if would be automaticaly decoded to buffer when loading.
  • If sound is longer than about 5 seconds it would be decoded step by step from memory when playing.
  • streaming=true attribute means that sound would be loaded from file and decoded on the fly when playing.

Load resources:

Resources resources;
resources.loadXML("sounds.xml");

Next step is declaring SoundPlayer instance:

SoundPlayer sfxPlayer;

SoundPlayer is a container for group of sounds playing right now. You could declare multiple instances of SoundPlayer and control them individually:

SoundPlayer sfxPlayer;
SoundPlayer musicPlayer;

...

sfxPlayer.setVolume(1.0f);
musicPlayer.setVolume(0.5f);

Update SoundSystem and SoundPlayer instances each frame:

void example_update()
{
	SoundSystem::get()->update();
	sfxPlayer.update();
	musicPlayer.update();
}

Play single sound:

sfxPlayer.play(resources.get("sfx1"));

Play looped music with fade in 1.5 seconds:

musicPlayer.play(resources.get("music"), PlayOptions().loop().fade(1500));

You could save handle to playing sound instance and use it for control:

spSoundInstance music = musicPlayer.play(resources.get("music"));	

void levelCompleted()
{
	music->fadeOut();
}

Or you could control group of sounds belonging to SoundPlayer:

sfxPlayer.pause();
...
sfxPlayer.stop();

You could subscribe to events:

spSoundInstance music = musicPlayer.play(resources.get("music"));
music->setDoneCallback(CLOSURE(this, &SomeClass::musicDone));

void SomeClass::musicDone(Event*)
{
	musicPlayer.play(resources.get("music2"));
} 

Don't forget to cleanup everything before exit:

musicPlayer.stop();
sfxPlayer.stop();
SoundPlayer::free();
SoundSystem::free();

If you don' want to declare sounds in the resources xml file

ResSound *clickRes = ResSound::create("sounds/button_click.ogg", false);
...

sfxPlayer.play(clickRes);
⚠️ **GitHub.com Fallback** ⚠️