Skip to content

Commit

Permalink
chore(audio): refactor code in audio scope
Browse files Browse the repository at this point in the history
  • Loading branch information
diogomsmiranda committed Oct 3, 2024
1 parent 74ea18e commit 3b170fa
Show file tree
Hide file tree
Showing 7 changed files with 134 additions and 161 deletions.
28 changes: 11 additions & 17 deletions core/include/cubos/core/al/audio_device.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
#include <vector>

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

#include <cubos/core/api.hpp>

Expand Down Expand Up @@ -48,34 +47,35 @@ namespace cubos::core::al
/// @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 filePath File path to create buffer from.
/// @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.
/// @return Handle of the new buffer.
static Buffer createBuffer(const void* data, size_t dataSize);
virtual Buffer createBuffer(const void* data, size_t dataSize) = 0;

/// @brief Creates a new audio source.
/// @return Handle of the new source.
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);
virtual Source createSource() = 0;

/// @brief Sets the position of the listener.
/// @param position Position.
/// @param listenerIndex Index of the listener
/// @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.
/// @param listenerIndex Index of the listener
/// @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.
/// @param listenerIndex Index of the listener
/// @param listenerIndex Index of the listener.
virtual void setListenerVelocity(const glm::vec3& velocity, unsigned int listenerIndex = 0) = 0;
};

Expand All @@ -86,11 +86,8 @@ namespace cubos::core::al
class CUBOS_CORE_API Buffer
{
public:
ma_decoder mDecoder;

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 @@ -102,9 +99,6 @@ 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(cubos::core::al::Buffer buffer) = 0;
Expand Down
52 changes: 4 additions & 48 deletions core/include/cubos/core/al/miniaudio_device.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,56 +2,12 @@

#include <cstddef>

#include <miniaudio.h>

#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
{
Expand All @@ -61,9 +17,9 @@ namespace cubos::core::al

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

Buffer createBuffer(const void* data, size_t dataSize) override;
Source createSource() override;
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;
Expand Down
Loading

0 comments on commit 3b170fa

Please sign in to comment.