Audio Manager

About


Akel’s audio manager is currently only able to play 2D sounds and only supports the formats supported by the SND files library in 16 bits and 8 bits. However, it is extremely easy to use. You just load an audio file into a buffer, play it whenever you want and free it at the end. The audio manager is simply a static wrapper around an OpenAL module.

Syntax


class CustomComponent : public Ak::Component
{
    public:
        void CustomComponent::onAttach() override
        {
            sound = Ak::AudioManager::loadSound("../sound.wav");
        }

        void CustomComponent::update() override
        {
            Ak::AudioManager::playSound(sound);
        }

        void CustomComponent::onQuit() override
        {
            Ak::AudioManager::freeSound(sound);
        }

    private:
        Ak::audioFile sound;
};

class App : Ak::Application
{
    public:
        App() : Ak::Application("my app")
        {
            add_component<Ak::AudioManager>(); // add it before using it
            add_component<CustomComponent>();
        }
};

Audio Files


The audio manager has a type specific to audio files which is Ak::audioFile. This is a buffer containing information about a sound loaded from an audio file. It is advisable to use this type with the Akel audio manager for simplicity and compatibility.

// Declaration
using audioFile = ALuint;

// Usage
Ak::audioFile my_sound = Ak::AudioManager::loadSound("sound.wav");

Member functions


Return type Function Specifiers
AudioManager()
~AudioManager()
void onAttach() override
void onQuit() override
Ak::audioFile loadSound(std::string filename) static
void playSound(audioFile sound) static
void freeSound(audioFile sound) static
void newSource() static
void freeSource(int index) static
void switch_to_source(int index) static

Constructor


Creates a new AudioManager.

// Prototype
AudioManager() = delete;

// Usage

// You can't use it because it is a deleted method.
// AudioManager is used like a static class.
// It is init automatically by Akel at the begenning of your program.

Destructor


Deletes the AudioManager.

// Prototype
~AudioManager() = delete;

// Usage

// You can't use it because it is a deleted method.
// AudioManager is used like a static class.
// It is deleted automatically by Akel at the end of your program.

onAttach()


Inits the AudioManager.

// Prototype
void onAttach() override;

// Usage

// You are not supposed to use this method.
// It is init by Akel when adding it as a component

onQuit()


Shutdowns the AudioManager.

// Prototype
void onQuit() override;

// Usage

// You are not supposed to use this method.
// It is called by Akel at the end of the program.

loadSound(std::string filename)


Loads a new sound from the sound file given.

// Prototype
static void loadSound(std::string filename);

// Usage
Ak::audioFile my_sound = Ak::AudioManager::loadSound("../sound.wav");

playSound(Ak::audiofile sound)


Plays the sound given with its source.

// Prototype
static void playSound(audiofile sound);

// Usage
Ak::AudioManager::playSound(my_sound);

freeSound(Ak::audiofile sound)


Frees the sound given.

// Prototype
static void freeSound(audiofile sound);

// Usage
Ak::AudioManager::freeSound(my_sound);

newSource()


Creates a new source from which a sound can be played.

// Prototype
static void newSource();

// Usage
Ak::AudioManager::newSource();

freeSource(int index)


Frees an existing source corresponding to the given index.

// Prototype
static void freeSource(int index);

// Usage
Ak::AudioManager::freeSource(2); // frees the third source (it starts from 0)

switch_to_source(int index)


Switches to the source corresponding to the given index.

// Prototype
static void switch_to_source(int index);

// Usage
Ak::AudioManager::switch_to_source(2); // switches to the third source (it starts from 0)