Skip to content

Commit

Permalink
feat(audio): replace OpenAL audio device for Miniaudio backend (#1005)
Browse files Browse the repository at this point in the history
Co-Authored-By: João Miguel Nogueira <[email protected]>
  • Loading branch information
diogomsmiranda and Dageus committed Sep 29, 2024
1 parent 48bc4ab commit 58559da
Show file tree
Hide file tree
Showing 9 changed files with 353 additions and 351 deletions.
3 changes: 0 additions & 3 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,6 @@
[submodule "core/lib/glm"]
path = core/lib/glm
url = https://github.com/g-truc/glm.git
[submodule "core/lib/openal-soft"]
path = core/lib/openal-soft
url = https://github.com/kcat/openal-soft
[submodule "core/lib/stduuid"]
path = core/lib/stduuid
url = https://github.com/mariusbancila/stduuid.git
Expand Down
14 changes: 1 addition & 13 deletions core/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ if(WITH_GLFW)
option(GLFW_USE_SUBMODULE "Compile GLFW from source?" ON)
endif()

option(WITH_OPENAL "With OpenAL?" OFF)
option(GLM_USE_SUBMODULE "Compile GLM from source?" ON)

set(CUBOS_CORE_ECS_MAX_COMPONENTS "63" CACHE STRING "The maximum number of components registered in an ECS world.")
Expand Down Expand Up @@ -85,8 +84,7 @@ set(CUBOS_CORE_SOURCE
"src/gl/util.cpp"

"src/al/audio_device.cpp"
"src/al/oal_audio_device.cpp"
"src/al/oal_audio_device.hpp"
"src/al/miniaudio_device.cpp"

"src/ecs/entity/entity.cpp"
"src/ecs/entity/hash.cpp"
Expand Down Expand Up @@ -191,16 +189,6 @@ if(WITH_GLFW)
target_compile_definitions(cubos-core PRIVATE WITH_GLFW)
endif()

if(WITH_OPENAL)
set(ALSOFT_UTILS OFF CACHE BOOL "" FORCE)
set(ALSOFT_NO_CONFIG_UTIL OFF CACHE BOOL "" FORCE)
set(ALSOFT_EXAMPLES OFF CACHE BOOL "" FORCE)
add_subdirectory(lib/openal-soft)
target_include_directories(cubos-core PRIVATE lib/openal-soft/include)
target_link_libraries(cubos-core PRIVATE OpenAL)
target_compile_definitions(cubos-core PRIVATE WITH_OPENAL)
endif()

if(GLM_USE_SUBMODULE)
add_subdirectory(lib/glm SYSTEM)
else()
Expand Down
74 changes: 33 additions & 41 deletions core/include/cubos/core/al/audio_device.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include <vector>

#include <glm/glm.hpp>
#include <miniaudio.h>

#include <cubos/core/api.hpp>

Expand All @@ -32,15 +33,6 @@ namespace cubos::core::al
/// @ingroup core-al
using Source = std::shared_ptr<impl::Source>;

/// @brief Possible audio formats.
enum class Format
{
Mono8,
Mono16,
Stereo8,
Stereo16,
};

/// @brief Audio device interface used to wrap low-level audio rendering APIs.
class CUBOS_CORE_API AudioDevice
{
Expand All @@ -51,36 +43,40 @@ namespace cubos::core::al
/// @brief Forbid copy construction.
AudioDevice(const AudioDevice&) = delete;

/// @brief Creates an audio device from a given device @p specifier.
/// @brief Creates an audio device.
/// @see enumerateDevices()
/// @param specifier Device specifier (empty for default).
/// @return Audio device, or nullptr on failure.
static std::shared_ptr<AudioDevice> create(const std::string& specifier = "");

/// @brief Enumerates the available devices.
/// @param[out] devices Vector to fill with the available devices.
static void enumerateDevices(std::vector<std::string>& devices);
static std::shared_ptr<AudioDevice> create();

/// @brief Creates a new audio buffer
/// @param filePath File path to create buffer from.
/// @return Handle of the new buffer.
virtual Buffer createBuffer() = 0;
static Buffer createBuffer(const void* data, size_t dataSize);

/// @brief Creates a new audio source.
/// @return Handle of the new source.
virtual Source createSource() = 0;
static Source createSource();

/// @brief Enumerates the available devices.
/// @param[out] devices Vector to fill with the available devices.
static void enumerateDevices(std::vector<std::string>& devices);

/// @brief Sets the position of the listener.
/// @param position Position.
virtual void setListenerPosition(const glm::vec3& position) = 0;
/// @param listenerIndex Index of the listener
virtual void setListenerPosition(const glm::vec3& position, unsigned int listenerIndex = 0) = 0;

/// @brief Sets the orientation of the listener.
/// @param forward Forward direction of the listener.
/// @param up Up direction of the listener.
virtual void setListenerOrientation(const glm::vec3& forward, const glm::vec3& up) = 0;
/// @param listenerIndex Index of the listener
virtual void setListenerOrientation(const glm::vec3& forward, const glm::vec3& up,
unsigned int listenerIndex = 0) = 0;

/// @brief Sets the velocity of the listener. Used to implement the doppler effect.
/// @param velocity Velocity of the listener.
virtual void setListenerVelocity(const glm::vec3& velocity) = 0;
/// @param listenerIndex Index of the listener
virtual void setListenerVelocity(const glm::vec3& velocity, unsigned int listenerIndex = 0) = 0;
};

/// @brief Namespace to store the abstract types implemented by the audio device implementations.
Expand All @@ -90,14 +86,11 @@ namespace cubos::core::al
class CUBOS_CORE_API Buffer
{
public:
virtual ~Buffer() = default;
ma_decoder mDecoder;

/// @brief Fills the buffer with data.
/// @param format Audio format of the data.
/// @param size Size of the buffer in bytes.
/// @param data Buffer data.
/// @param frequency Audio frequency.
virtual void fill(Format format, std::size_t size, const void* data, std::size_t frequency) = 0;
virtual ~Buffer() = default;
virtual size_t getLength() = 0;
static std::shared_ptr<Buffer> create(const void* data, size_t dataSize);

protected:
Buffer() = default;
Expand All @@ -109,9 +102,12 @@ namespace cubos::core::al
public:
virtual ~Source() = default;

/// @brief Creates a source
static std::shared_ptr<Source> create();

/// @brief Sets the buffer to be played by the source.
/// @param buffer Buffer.
virtual void setBuffer(std::shared_ptr<Buffer> buffer) = 0;
virtual void setBuffer(cubos::core::al::Buffer buffer) = 0;

/// @brief Sets the position of the source, by default, in the world space.
/// @see setRelative() to change this behavior.
Expand Down Expand Up @@ -141,26 +137,22 @@ namespace cubos::core::al

/// @brief Sets the maximum distance at which the source is audible.
/// @param maxDistance Maximum distance.
virtual void setDistance(float maxDistance) = 0;
virtual void setMaxDistance(float maxDistance) = 0;

/// @brief Sets the cone angle of the source, in degrees. By default, 360.
/// @param coneAngle Angle, in degrees.
virtual void setConeAngle(float coneAngle) = 0;
/// @brief Sets the minimum distance at which the source starts to attenuate.
/// @param minDistance Minimum distance.
virtual void setMinDistance(float minDistance) = 0;

/// @brief Sets the cone gain of the source.
/// @todo Find out what this is.
/// @brief Sets the cone angle, in degrees. While also setting the outerGain.
/// @param innerAngle Outer angle, in degrees.
/// @param outerAngle Inner angle, in degrees.
/// @param coneGain Gain.
virtual void setConeGain(float coneGain) = 0;
virtual void setCone(float innerAngle, float outerAngle, float outerGain) = 0;

/// @brief Sets the cone direction of the source.
/// @param direction Direction.
virtual void setConeDirection(const glm::vec3& direction) = 0;

/// @brief Sets the distance under which the volume for the source would normally drop
/// by half.
/// @param referenceDistance Distance.
virtual void setReferenceDistance(float referenceDistance) = 0;

/// @brief Plays the source.
virtual void play() = 0;

Expand Down
77 changes: 77 additions & 0 deletions core/include/cubos/core/al/miniaudio_device.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
#pragma once

#include <cstddef>

#include <cubos/core/al/audio_device.hpp>

namespace cubos::core::al
{
class MiniaudioBuffer : public impl::Buffer
{
public:
~MiniaudioBuffer() override;
size_t getLength() override;

static std::shared_ptr<Buffer> create(const void* data, size_t dataSize)
{
return std::shared_ptr<cubos::core::al::MiniaudioBuffer>(new MiniaudioBuffer(data, dataSize));
}

protected:
MiniaudioBuffer(const void* data, size_t dataSize);
};

class MiniaudioSource : public impl::Source
{
public:
~MiniaudioSource() override;

static std::shared_ptr<Source> create()
{
return std::shared_ptr<cubos::core::al::MiniaudioSource>(new MiniaudioSource());
}

void setBuffer(cubos::core::al::Buffer buffer) override;
void setPosition(const glm::vec3& position) override;
void setVelocity(const glm::vec3& velocity) override;
void setGain(float gain) override;
void setPitch(float pitch) override;
void setLooping(bool looping) override;
void setRelative(bool relative) override;
void setMaxDistance(float maxDistance) override;
void setMinDistance(float minDistance) override;
void setCone(float innerAngle, float outerAngle, float outerGain) override;
void setConeDirection(const glm::vec3& direction) override;
void play() override;

protected:
MiniaudioSource();

private:
ma_sound mSound;
ma_engine mEngine;
};

/// Audio device implementation using miniaudio.
class MiniaudioDevice : public AudioDevice
{
public:
MiniaudioDevice();
~MiniaudioDevice() override;

static void enumerateDevices(std::vector<std::string>& devices);
static std::string getDefaultDevice();
static Buffer createBuffer(const void* data, size_t dataSize);
static Source createSource();

void setListenerPosition(const glm::vec3& position, ma_uint32 listenerIndex = 0) override;
void setListenerOrientation(const glm::vec3& forward, const glm::vec3& up,
ma_uint32 listenerIndex = 0) override;
void setListenerVelocity(const glm::vec3& velocity, ma_uint32 listenerIndex = 0) override;

private:
ma_context mContext;
ma_device mDevice;
ma_engine mEngine;
};
} // namespace cubos::core::al
1 change: 0 additions & 1 deletion core/lib/openal-soft
Submodule openal-soft deleted from d3875f
8 changes: 4 additions & 4 deletions core/src/al/audio_device.cpp
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
#include "oal_audio_device.hpp"
#include <cubos/core/al/miniaudio_device.hpp>

using namespace cubos::core::al;

std::shared_ptr<AudioDevice> AudioDevice::create(const std::string& specifier)
std::shared_ptr<AudioDevice> AudioDevice::create()
{
return std::make_shared<OALAudioDevice>(specifier);
return std::make_shared<MiniaudioDevice>();
}

void AudioDevice::enumerateDevices(std::vector<std::string>& devices)
{
OALAudioDevice::enumerateDevices(devices);
MiniaudioDevice::enumerateDevices(devices);
}
Loading

0 comments on commit 58559da

Please sign in to comment.