Skip to content

Commit

Permalink
feat(audio): Add AudioContext and MiniaudioContext
Browse files Browse the repository at this point in the history
Co-Authored-By: João Miguel Nogueira <[email protected]>
  • Loading branch information
Dageus authored and diogomsmiranda committed Oct 3, 2024
1 parent 3b170fa commit 107ae19
Show file tree
Hide file tree
Showing 11 changed files with 200 additions and 144 deletions.
4 changes: 2 additions & 2 deletions core/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,8 @@ set(CUBOS_CORE_SOURCE
"src/gl/ogl_render_device.cpp"
"src/gl/util.cpp"

"src/al/audio_device.cpp"
"src/al/miniaudio_device.cpp"
"src/al/audio_context.cpp"
"src/al/miniaudio_context.cpp"

"src/ecs/entity/entity.cpp"
"src/ecs/entity/hash.cpp"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/// @file
/// @brief Class @ref cubos::core::al::AudioDevice and related types.
/// @brief Class @ref cubos::core::al::AudioContext and related types.
/// @ingroup core-al

#pragma once
Expand Down Expand Up @@ -36,21 +36,11 @@ namespace cubos::core::al
class CUBOS_CORE_API AudioDevice
{
public:
AudioDevice() = default;
virtual ~AudioDevice() = default;

/// @brief Forbid copy construction.
AudioDevice(const AudioDevice&) = delete;

/// @brief Creates an audio device.
/// @see enumerateDevices()
/// @return Audio device, or nullptr on failure.
static std::shared_ptr<AudioDevice> create();

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

/// @brief Creates a new audio buffer
/// @param data Data to be written to the buffer, cubos currently supports .wav, .mp3 and .flac files
/// @param datSize Size of the data to be written.
Expand All @@ -77,6 +67,30 @@ namespace cubos::core::al
/// @param velocity Velocity of the listener.
/// @param listenerIndex Index of the listener.
virtual void setListenerVelocity(const glm::vec3& velocity, unsigned int listenerIndex = 0) = 0;

protected:
AudioDevice() = default;
};

/// @brief Audio context that contains audio devices;
class CUBOS_CORE_API AudioContext
{
public:
AudioContext() = default;
virtual ~AudioContext() = default;

/// @brief Creates an audio context.
/// @return AudioContext, or nullptr on failure.
static std::shared_ptr<AudioContext> create();

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

/// @brief Creates a new audio device
/// @param specifier The specifier of the audio device, used to identify it
/// @return Handle of the new device
virtual std::shared_ptr<AudioDevice> createDevice(const std::string& specifier) = 0;
};

/// @brief Namespace to store the abstract types implemented by the audio device implementations.
Expand Down
26 changes: 26 additions & 0 deletions core/include/cubos/core/al/miniaudio_context.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#pragma once

#include <cstddef>

#include <miniaudio.h>

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

namespace cubos::core::al
{
/// Audio device implementation using miniaudio.
class MiniaudioContext : public AudioContext
{
public:
MiniaudioContext();
~MiniaudioContext() override;

std::shared_ptr<AudioDevice> createDevice(const std::string& specifier) override;

static void enumerateDevices(std::vector<std::string>& devices);
static std::string getDefaultDevice();

private:
ma_context mContext;
};
} // namespace cubos::core::al
33 changes: 0 additions & 33 deletions core/include/cubos/core/al/miniaudio_device.hpp

This file was deleted.

13 changes: 13 additions & 0 deletions core/src/al/audio_context.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#include <cubos/core/al/miniaudio_context.hpp>

using namespace cubos::core::al;

std::shared_ptr<AudioContext> AudioContext::create()
{
return std::make_shared<MiniaudioContext>();
}

void AudioContext::enumerateDevices(std::vector<std::string>& devices)
{
MiniaudioContext::enumerateDevices(devices);
}
13 changes: 0 additions & 13 deletions core/src/al/audio_device.cpp

This file was deleted.

Loading

0 comments on commit 107ae19

Please sign in to comment.