Skip to content

Commit 9b2e330

Browse files
committed
Add audio interface. Make resource loading function pass shared pointer to deserialize context.
1 parent 08f6cd1 commit 9b2e330

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

56 files changed

+1518
-190
lines changed

CMakeLists.txt

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,15 @@ FetchContent_Declare(tinyexr
9999
GIT_REPOSITORY https://github.com/syoyo/tinyexr.git
100100
GIT_TAG 6e8cac308cdf4d717078f3f37c4aa39bf3b356b4 # v1.0.7
101101
)
102+
FetchContent_Declare(ogg
103+
GIT_REPOSITORY https://github.com/xiph/ogg.git
104+
GIT_TAG e1774cd77f471443541596e09078e78fdc342e4f # v1.3.5
105+
OVERRIDE_FIND_PACKAGE
106+
)
107+
FetchContent_Declare(vorbis
108+
GIT_REPOSITORY https://github.com/xiph/vorbis.git
109+
GIT_TAG 0657aee69dec8508a0011f47f3b69d7538e9d262 # v1.3.7
110+
)
102111

103112
# Configure cxxopts
104113
set(CXXOPTS_BUILD_EXAMPLES OFF)
@@ -173,6 +182,9 @@ add_compile_definitions(
173182
TINYEXR_USE_STB_ZLIB=1
174183
)
175184

185+
# Configure Ogg
186+
set(BUILD_TESTING OFF)
187+
176188
# Make dependencies available
177189
FetchContent_MakeAvailable(
178190
cxxopts
@@ -186,6 +198,8 @@ FetchContent_MakeAvailable(
186198
SDL2
187199
stb
188200
tinyexr
201+
ogg
202+
vorbis
189203
)
190204

191205
# Build dr_wav static library
@@ -195,7 +209,7 @@ add_library(dr_wav STATIC ${dr_libs_BINARY_DIR}/src/dr_wav.c)
195209
target_compile_definitions(dr_wav
196210
PRIVATE
197211
DR_WAV_IMPLEMENTATION
198-
DR_WAV_NO_CONVERSION_API
212+
# DR_WAV_NO_CONVERSION_API
199213
DR_WAV_NO_STDIO
200214
)
201215
target_include_directories(dr_wav
@@ -391,6 +405,7 @@ target_link_libraries(${PROJECT_NAME}
391405
physfs-static
392406
freetype
393407
${OPENGL_gl_LIBRARY}
408+
vorbisfile
394409
)
395410

396411
# Determine data output directory

src/engine/audio/audio.hpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// SPDX-FileCopyrightText: 2023 C. J. Howard
2+
// SPDX-License-Identifier: GPL-3.0-or-later
3+
4+
#ifndef ANTKEEPER_AUDIO_HPP
5+
#define ANTKEEPER_AUDIO_HPP
6+
7+
#include <engine/audio/listener.hpp>
8+
#include <engine/audio/playback-state.hpp>
9+
#include <engine/audio/sound-que.hpp>
10+
#include <engine/audio/sound-system.hpp>
11+
#include <engine/audio/sound-wave.hpp>
12+
13+
/// Audio interface.
14+
namespace audio {}
15+
16+
#endif // ANTKEEPER_AUDIO_HPP

src/engine/audio/listener.cpp

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
// SPDX-FileCopyrightText: 2023 C. J. Howard
2+
// SPDX-License-Identifier: GPL-3.0-or-later
3+
4+
#include <engine/audio/listener.hpp>
5+
#include <AL/al.h>
6+
7+
namespace audio {
8+
9+
listener::listener()
10+
{}
11+
12+
void listener::set_position(const math::fvec3& position)
13+
{
14+
if (m_position != position)
15+
{
16+
m_position = position;
17+
alListenerfv(AL_POSITION, m_position.data());
18+
}
19+
}
20+
21+
void listener::set_orientation(const math::fquat& orientation)
22+
{
23+
if (m_orientation != orientation)
24+
{
25+
m_orientation = orientation;
26+
27+
// Calculate basis vectors
28+
const math::fvec3 basis[2] =
29+
{
30+
// Forward direction vector
31+
m_orientation * math::fvec3{ 0.0f, 0.0f, -1.0f},
32+
33+
// Up direciton vector
34+
m_orientation * math::fvec3{ 0.0f, 1.0f, 0.0f}
35+
};
36+
37+
static_assert(sizeof(basis) == sizeof(float) * 6);
38+
alListenerfv(AL_ORIENTATION, basis[0].data());
39+
}
40+
}
41+
42+
void listener::set_velocity(const math::fvec3& velocity)
43+
{
44+
if (m_velocity != velocity)
45+
{
46+
m_velocity = velocity;
47+
alListenerfv(AL_VELOCITY, m_velocity.data());
48+
}
49+
}
50+
51+
void listener::set_gain(float gain)
52+
{
53+
if (m_gain != gain)
54+
{
55+
m_gain = gain;
56+
alListenerf(AL_GAIN, m_gain);
57+
}
58+
}
59+
60+
} // namespace audio

src/engine/audio/listener.hpp

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
// SPDX-FileCopyrightText: 2023 C. J. Howard
2+
// SPDX-License-Identifier: GPL-3.0-or-later
3+
4+
#ifndef ANTKEEPER_AUDIO_LISTENER_HPP
5+
#define ANTKEEPER_AUDIO_LISTENER_HPP
6+
7+
#include <engine/math/vector.hpp>
8+
#include <engine/math/quaternion.hpp>
9+
10+
namespace audio {
11+
12+
class sound_system;
13+
14+
/**
15+
* Sound listener.
16+
*/
17+
class listener
18+
{
19+
public:
20+
/** Constructs a listener. */
21+
listener();
22+
23+
/** Destructs a listener. */
24+
~listener() = default;
25+
26+
listener(const listener&) = delete;
27+
listener(listener&&) = delete;
28+
listener& operator=(const listener&) = delete;
29+
listener& operator=(listener&&) = delete;
30+
31+
/**
32+
* Sets the position of the listener.
33+
*
34+
* @param position Position of the listener.
35+
*/
36+
void set_position(const math::fvec3& position);
37+
38+
/**
39+
* Sets the orientation of the listener.
40+
*
41+
* @param orientation Orientation of the listener.
42+
*/
43+
void set_orientation(const math::fquat& orientation);
44+
45+
/**
46+
* Sets the velocity of the listener.
47+
*
48+
* @param velocity Position of the listener.
49+
*/
50+
void set_velocity(const math::fvec3& velocity);
51+
52+
/**
53+
* Sets the gain of the listener.
54+
*
55+
* @param gain Gain of the listener.
56+
*/
57+
void set_gain(float gain);
58+
59+
/** Returns the position of the listener. */
60+
[[nodiscard]] inline constexpr const auto& get_position() const noexcept
61+
{
62+
return m_position;
63+
}
64+
65+
/** Returns the orientation of the listener. */
66+
[[nodiscard]] inline constexpr const auto& get_orientation() const noexcept
67+
{
68+
return m_orientation;
69+
}
70+
71+
/** Returns the velocity of the listener. */
72+
[[nodiscard]] inline constexpr const auto& get_velocity() const noexcept
73+
{
74+
return m_velocity;
75+
}
76+
77+
/** Returns the gain of the listener. */
78+
[[nodiscard]] inline constexpr auto get_gain() const noexcept
79+
{
80+
return m_gain;
81+
}
82+
83+
private:
84+
friend class sound_system;
85+
86+
math::fvec3 m_position{};
87+
math::fquat m_orientation{math::identity<math::fquat>};
88+
math::fvec3 m_velocity{};
89+
float m_gain{1.0f};
90+
};
91+
92+
} // namespace audio
93+
94+
#endif // ANTKEEPER_AUDIO_LISTENER_HPP

src/engine/audio/playback-state.hpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// SPDX-FileCopyrightText: 2023 C. J. Howard
2+
// SPDX-License-Identifier: GPL-3.0-or-later
3+
4+
#ifndef ANTKEEPER_AUDIO_PLAYBACK_STATE_HPP
5+
#define ANTKEEPER_AUDIO_PLAYBACK_STATE_HPP
6+
7+
namespace audio {
8+
9+
/** Playback statse of a sound queue. */
10+
enum class playback_state
11+
{
12+
/** Sound que is stopped. */
13+
stopped,
14+
15+
/** Sound que is playing. */
16+
playing,
17+
18+
/** Sound que is paused. */
19+
paused
20+
};
21+
22+
} // namespace audio
23+
24+
#endif // ANTKEEPER_AUDIO_PLAYBACK_STATE_HPP

0 commit comments

Comments
 (0)