Skip to content

Commit

Permalink
Add audio interface. Make resource loading function pass shared point…
Browse files Browse the repository at this point in the history
…er to deserialize context.
  • Loading branch information
cjhoward committed Dec 24, 2023
1 parent 08f6cd1 commit 9b2e330
Show file tree
Hide file tree
Showing 56 changed files with 1,518 additions and 190 deletions.
17 changes: 16 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,15 @@ FetchContent_Declare(tinyexr
GIT_REPOSITORY https://github.com/syoyo/tinyexr.git
GIT_TAG 6e8cac308cdf4d717078f3f37c4aa39bf3b356b4 # v1.0.7
)
FetchContent_Declare(ogg
GIT_REPOSITORY https://github.com/xiph/ogg.git
GIT_TAG e1774cd77f471443541596e09078e78fdc342e4f # v1.3.5
OVERRIDE_FIND_PACKAGE
)
FetchContent_Declare(vorbis
GIT_REPOSITORY https://github.com/xiph/vorbis.git
GIT_TAG 0657aee69dec8508a0011f47f3b69d7538e9d262 # v1.3.7
)

# Configure cxxopts
set(CXXOPTS_BUILD_EXAMPLES OFF)
Expand Down Expand Up @@ -173,6 +182,9 @@ add_compile_definitions(
TINYEXR_USE_STB_ZLIB=1
)

# Configure Ogg
set(BUILD_TESTING OFF)

# Make dependencies available
FetchContent_MakeAvailable(
cxxopts
Expand All @@ -186,6 +198,8 @@ FetchContent_MakeAvailable(
SDL2
stb
tinyexr
ogg
vorbis
)

# Build dr_wav static library
Expand All @@ -195,7 +209,7 @@ add_library(dr_wav STATIC ${dr_libs_BINARY_DIR}/src/dr_wav.c)
target_compile_definitions(dr_wav
PRIVATE
DR_WAV_IMPLEMENTATION
DR_WAV_NO_CONVERSION_API
# DR_WAV_NO_CONVERSION_API
DR_WAV_NO_STDIO
)
target_include_directories(dr_wav
Expand Down Expand Up @@ -391,6 +405,7 @@ target_link_libraries(${PROJECT_NAME}
physfs-static
freetype
${OPENGL_gl_LIBRARY}
vorbisfile
)

# Determine data output directory
Expand Down
16 changes: 16 additions & 0 deletions src/engine/audio/audio.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// SPDX-FileCopyrightText: 2023 C. J. Howard
// SPDX-License-Identifier: GPL-3.0-or-later

#ifndef ANTKEEPER_AUDIO_HPP
#define ANTKEEPER_AUDIO_HPP

#include <engine/audio/listener.hpp>
#include <engine/audio/playback-state.hpp>
#include <engine/audio/sound-que.hpp>
#include <engine/audio/sound-system.hpp>
#include <engine/audio/sound-wave.hpp>

/// Audio interface.
namespace audio {}

#endif // ANTKEEPER_AUDIO_HPP
60 changes: 60 additions & 0 deletions src/engine/audio/listener.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
// SPDX-FileCopyrightText: 2023 C. J. Howard
// SPDX-License-Identifier: GPL-3.0-or-later

#include <engine/audio/listener.hpp>
#include <AL/al.h>

namespace audio {

listener::listener()
{}

void listener::set_position(const math::fvec3& position)
{
if (m_position != position)
{
m_position = position;
alListenerfv(AL_POSITION, m_position.data());
}
}

void listener::set_orientation(const math::fquat& orientation)
{
if (m_orientation != orientation)
{
m_orientation = orientation;

// Calculate basis vectors
const math::fvec3 basis[2] =
{
// Forward direction vector
m_orientation * math::fvec3{ 0.0f, 0.0f, -1.0f},

// Up direciton vector
m_orientation * math::fvec3{ 0.0f, 1.0f, 0.0f}
};

static_assert(sizeof(basis) == sizeof(float) * 6);
alListenerfv(AL_ORIENTATION, basis[0].data());
}
}

void listener::set_velocity(const math::fvec3& velocity)
{
if (m_velocity != velocity)
{
m_velocity = velocity;
alListenerfv(AL_VELOCITY, m_velocity.data());
}
}

void listener::set_gain(float gain)
{
if (m_gain != gain)
{
m_gain = gain;
alListenerf(AL_GAIN, m_gain);
}
}

} // namespace audio
94 changes: 94 additions & 0 deletions src/engine/audio/listener.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
// SPDX-FileCopyrightText: 2023 C. J. Howard
// SPDX-License-Identifier: GPL-3.0-or-later

#ifndef ANTKEEPER_AUDIO_LISTENER_HPP
#define ANTKEEPER_AUDIO_LISTENER_HPP

#include <engine/math/vector.hpp>
#include <engine/math/quaternion.hpp>

namespace audio {

class sound_system;

/**
* Sound listener.
*/
class listener
{
public:
/** Constructs a listener. */
listener();

/** Destructs a listener. */
~listener() = default;

listener(const listener&) = delete;
listener(listener&&) = delete;
listener& operator=(const listener&) = delete;
listener& operator=(listener&&) = delete;

/**
* Sets the position of the listener.
*
* @param position Position of the listener.
*/
void set_position(const math::fvec3& position);

/**
* Sets the orientation of the listener.
*
* @param orientation Orientation of the listener.
*/
void set_orientation(const math::fquat& orientation);

/**
* Sets the velocity of the listener.
*
* @param velocity Position of the listener.
*/
void set_velocity(const math::fvec3& velocity);

/**
* Sets the gain of the listener.
*
* @param gain Gain of the listener.
*/
void set_gain(float gain);

/** Returns the position of the listener. */
[[nodiscard]] inline constexpr const auto& get_position() const noexcept
{
return m_position;
}

/** Returns the orientation of the listener. */
[[nodiscard]] inline constexpr const auto& get_orientation() const noexcept
{
return m_orientation;
}

/** Returns the velocity of the listener. */
[[nodiscard]] inline constexpr const auto& get_velocity() const noexcept
{
return m_velocity;
}

/** Returns the gain of the listener. */
[[nodiscard]] inline constexpr auto get_gain() const noexcept
{
return m_gain;
}

private:
friend class sound_system;

math::fvec3 m_position{};
math::fquat m_orientation{math::identity<math::fquat>};
math::fvec3 m_velocity{};
float m_gain{1.0f};
};

} // namespace audio

#endif // ANTKEEPER_AUDIO_LISTENER_HPP
24 changes: 24 additions & 0 deletions src/engine/audio/playback-state.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// SPDX-FileCopyrightText: 2023 C. J. Howard
// SPDX-License-Identifier: GPL-3.0-or-later

#ifndef ANTKEEPER_AUDIO_PLAYBACK_STATE_HPP
#define ANTKEEPER_AUDIO_PLAYBACK_STATE_HPP

namespace audio {

/** Playback statse of a sound queue. */
enum class playback_state
{
/** Sound que is stopped. */
stopped,

/** Sound que is playing. */
playing,

/** Sound que is paused. */
paused
};

} // namespace audio

#endif // ANTKEEPER_AUDIO_PLAYBACK_STATE_HPP

0 comments on commit 9b2e330

Please sign in to comment.