Minetest 5.10.0-dev
 
Loading...
Searching...
No Matches
al_helpers.h
Go to the documentation of this file.
1/*
2Minetest
3Copyright (C) 2022 DS
4Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
5OpenAL support based on work by:
6Copyright (C) 2011 Sebastian 'Bahamada' Rühl
7Copyright (C) 2011 Cyriaque 'Cisoun' Skrapits <cysoun@gmail.com>
8Copyright (C) 2011 Giuseppe Bilotta <giuseppe.bilotta@gmail.com>
9
10This program is free software; you can redistribute it and/or modify
11it under the terms of the GNU Lesser General Public License as published by
12the Free Software Foundation; either version 2.1 of the License, or
13(at your option) any later version.
14
15This program is distributed in the hope that it will be useful,
16but WITHOUT ANY WARRANTY; without even the implied warranty of
17MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18GNU Lesser General Public License for more details.
19
20You should have received a copy of the GNU Lesser General Public License along
21with this program; if not, write to the Free Software Foundation, Inc.,
2251 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23*/
24
25#pragma once
26
27#include "log.h"
28#include "util/basic_macros.h"
29#include "irr_v3d.h"
30
31#if defined(_WIN32)
32 #include <al.h>
33 #include <alc.h>
34 //#include <alext.h>
35#elif defined(__APPLE__)
36 #define OPENAL_DEPRECATED
37 #include <OpenAL/al.h>
38 #include <OpenAL/alc.h>
39 //#include <OpenAL/alext.h>
40#else
41 #include <AL/al.h>
42 #include <AL/alc.h>
43 #include <AL/alext.h>
44#endif
45
46#include <utility>
47
48namespace sound {
49
50inline const char *getAlErrorString(ALenum err) noexcept
51{
52 switch (err) {
53 case AL_NO_ERROR:
54 return "no error";
55 case AL_INVALID_NAME:
56 return "invalid name";
57 case AL_INVALID_ENUM:
58 return "invalid enum";
59 case AL_INVALID_VALUE:
60 return "invalid value";
61 case AL_INVALID_OPERATION:
62 return "invalid operation";
63 case AL_OUT_OF_MEMORY:
64 return "out of memory";
65 default:
66 return "<unknown OpenAL error>";
67 }
68}
69
70inline ALenum warn_if_al_error(const char *desc)
71{
72 ALenum err = alGetError();
73 if (err == AL_NO_ERROR)
74 return err;
75 warningstream << "[OpenAL Error] " << desc << ": " << getAlErrorString(err)
76 << std::endl;
77 return err;
78}
79
85inline v3f swap_handedness(v3f v) noexcept
86{
87 return v3f(-v.X, v.Y, v.Z);
88}
89
94{
95 RAIIALSoundBuffer() noexcept = default;
96 explicit RAIIALSoundBuffer(ALuint buffer) noexcept : m_buffer(buffer) {};
97
98 ~RAIIALSoundBuffer() noexcept { reset(0); }
99
101
102 RAIIALSoundBuffer(RAIIALSoundBuffer &&other) noexcept : m_buffer(other.release()) {}
104
105 ALuint get() noexcept { return m_buffer; }
106
107 ALuint release() noexcept { return std::exchange(m_buffer, 0); }
108
109 void reset(ALuint buf) noexcept;
110
111 static RAIIALSoundBuffer generate() noexcept;
112
113private:
114 // According to openal specification:
115 // > Deleting buffer name 0 is a legal NOP.
116 //
117 // and:
118 // > [...] the NULL buffer (i.e., 0) which can always be queued.
119 ALuint m_buffer = 0;
120};
121
122} // namespace sound
#define DISABLE_CLASS_COPY(C)
Definition basic_macros.h:35
core::vector3df v3f
Definition irr_v3d.h:26
thread_local LogStream warningstream
Definition al_extensions.cpp:26
const char * getAlErrorString(ALenum err) noexcept
Definition al_helpers.h:50
ALenum warn_if_al_error(const char *desc)
Definition al_helpers.h:70
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:85
RAII wrapper for openal sound buffers.
Definition al_helpers.h:94
ALuint release() noexcept
Definition al_helpers.h:107
RAIIALSoundBuffer() noexcept=default
static RAIIALSoundBuffer generate() noexcept
Definition al_helpers.cpp:50
~RAIIALSoundBuffer() noexcept
Definition al_helpers.h:98
ALuint get() noexcept
Definition al_helpers.h:105
ALuint m_buffer
Definition al_helpers.h:119
void reset(ALuint buf) noexcept
Definition al_helpers.cpp:40
RAIIALSoundBuffer & operator=(RAIIALSoundBuffer &&other) noexcept
Definition al_helpers.cpp:33