Luanti 5.10.0-dev
 
Loading...
Searching...
No Matches
al_helpers.h
Go to the documentation of this file.
1// Luanti
2// SPDX-License-Identifier: LGPL-2.1-or-later
3// Copyright (C) 2022 DS
4// Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
5// Copyright (C) 2011 Sebastian 'Bahamada' Rühl
6// Copyright (C) 2011 Cyriaque 'Cisoun' Skrapits <cysoun@gmail.com>
7// Copyright (C) 2011 Giuseppe Bilotta <giuseppe.bilotta@gmail.com>
8
9#pragma once
10
11#include "log.h"
12#include "util/basic_macros.h"
13#include "irr_v3d.h"
14
15#if defined(_WIN32)
16 #include <al.h>
17 #include <alc.h>
18 //#include <alext.h>
19#elif defined(__APPLE__)
20 #define OPENAL_DEPRECATED
21 #include <OpenAL/al.h>
22 #include <OpenAL/alc.h>
23 //#include <OpenAL/alext.h>
24#else
25 #include <AL/al.h>
26 #include <AL/alc.h>
27 #include <AL/alext.h>
28#endif
29
30#include <utility>
31
32namespace sound {
33
34inline const char *getAlErrorString(ALenum err) noexcept
35{
36 switch (err) {
37 case AL_NO_ERROR:
38 return "no error";
39 case AL_INVALID_NAME:
40 return "invalid name";
41 case AL_INVALID_ENUM:
42 return "invalid enum";
43 case AL_INVALID_VALUE:
44 return "invalid value";
45 case AL_INVALID_OPERATION:
46 return "invalid operation";
47 case AL_OUT_OF_MEMORY:
48 return "out of memory";
49 default:
50 return "<unknown OpenAL error>";
51 }
52}
53
54inline ALenum warn_if_al_error(const char *desc)
55{
56 ALenum err = alGetError();
57 if (err == AL_NO_ERROR)
58 return err;
59 warningstream << "[OpenAL Error] " << desc << ": " << getAlErrorString(err)
60 << std::endl;
61 return err;
62}
63
69inline v3f swap_handedness(v3f v) noexcept
70{
71 return v3f(-v.X, v.Y, v.Z);
72}
73
78{
79 RAIIALSoundBuffer() noexcept = default;
80 explicit RAIIALSoundBuffer(ALuint buffer) noexcept : m_buffer(buffer) {};
81
82 ~RAIIALSoundBuffer() noexcept { reset(0); }
83
85
86 RAIIALSoundBuffer(RAIIALSoundBuffer &&other) noexcept : m_buffer(other.release()) {}
88
89 ALuint get() noexcept { return m_buffer; }
90
91 ALuint release() noexcept { return std::exchange(m_buffer, 0); }
92
93 void reset(ALuint buf) noexcept;
94
95 static RAIIALSoundBuffer generate() noexcept;
96
97private:
98 // According to openal specification:
99 // > Deleting buffer name 0 is a legal NOP.
100 //
101 // and:
102 // > [...] the NULL buffer (i.e., 0) which can always be queued.
103 ALuint m_buffer = 0;
104};
105
106} // namespace sound
#define DISABLE_CLASS_COPY(C)
Definition basic_macros.h:26
core::vector3df v3f
Definition irr_v3d.h:11
thread_local LogStream warningstream
Definition al_extensions.cpp:11
const char * getAlErrorString(ALenum err) noexcept
Definition al_helpers.h:34
ALenum warn_if_al_error(const char *desc)
Definition al_helpers.h:54
v3f swap_handedness(v3f v) noexcept
Transforms vectors from a left-handed coordinate system to a right-handed one and vice-versa.
Definition al_helpers.h:69
RAII wrapper for openal sound buffers.
Definition al_helpers.h:78
ALuint release() noexcept
Definition al_helpers.h:91
RAIIALSoundBuffer() noexcept=default
static RAIIALSoundBuffer generate() noexcept
Definition al_helpers.cpp:34
~RAIIALSoundBuffer() noexcept
Definition al_helpers.h:82
ALuint get() noexcept
Definition al_helpers.h:89
ALuint m_buffer
Definition al_helpers.h:103
void reset(ALuint buf) noexcept
Definition al_helpers.cpp:24
RAIIALSoundBuffer & operator=(RAIIALSoundBuffer &&other) noexcept
Definition al_helpers.cpp:17