smk::Audio Class Reference

#include <Audio.hpp>

Description

Used to Initialize OpenAL. It is required to use it in the main() function. Coucou

Example

int main() {
// Init OpenAL.
smk::Audio audio;
// Load a sound file.
auto sound_buffer = smk::SoundBuffer(asset::water_mp3);
// Create a sound source.
auto sound = smk::Sound(sound_buffer);
// Play the sound.
sound.Play();
[...]
}
A sound file loaded in memory.
Definition: SoundBuffer.hpp:33
Represent a sound being played.
Definition: Sound.hpp:31
Examples
sound.cpp.

Definition at line 32 of file Audio.hpp.

Static Public Member Functions

static bool Initialized ()
 

Constructor & Destructor Documentation

◆ Audio()

smk::Audio::Audio ( )

Definition at line 35 of file Audio.cpp.

35 {
36 if (g_ref_count++) {
37 return;
38 }
39 std::vector<std::string> devices;
40 GetDevices(devices);
41 std::cout << "Audio devices found " << devices.size() << ":" << std::endl;
42
43 for (auto& it : devices) {
44 std::cout << "* " << it << std::endl;
45 }
46
47 std::cout << std::endl;
48
49 g_audio_device = alcOpenDevice(devices[0].c_str());
50
51 if (!g_audio_device) {
52 std::cerr << "Failed to get an OpenAL device. Please check you have some "
53 "backend configured while building your application. For "
54 "instance PulseAudio with libpulse-dev"
55 << std::endl;
56 return;
57 }
58
59 g_audio_context = alcCreateContext(g_audio_device, nullptr);
60 if (!g_audio_context) {
61 std::cerr << "Failed to get an OpenAL context" << std::endl;
62 return;
63 }
64
65 if (!alcMakeContextCurrent(g_audio_context)) {
66 std::cerr << "Failed to make the OpenAL context active" << std::endl;
67 return;
68 }
69}

◆ ~Audio()

smk::Audio::~Audio ( )

Definition at line 71 of file Audio.cpp.

71 {
72 if (--g_ref_count) {
73 return;
74 }
75 // Destroy the context
76 alcMakeContextCurrent(nullptr);
77 if (g_audio_context) {
78 alcDestroyContext(g_audio_context);
79 g_audio_context = nullptr;
80 }
81
82 // Destroy the device
83 if (g_audio_device) {
84 alcCloseDevice(g_audio_device);
85 g_audio_device = nullptr;
86 }
87}

Member Function Documentation

◆ Initialized()

bool smk::Audio::Initialized ( )
static
Returns
true if there is at least one Audio class instanciated.

Definition at line 91 of file Audio.cpp.

91 {
92 return g_ref_count;
93}

The documentation for this class was generated from the following files: