Skip to content

Commit

Permalink
feat(audio): Add AudioPlugin
Browse files Browse the repository at this point in the history
  • Loading branch information
diogomsmiranda committed Oct 2, 2024
1 parent 52949f6 commit 2b9a02d
Show file tree
Hide file tree
Showing 10 changed files with 148 additions and 4 deletions.
3 changes: 3 additions & 0 deletions engine/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ set(CUBOS_ENGINE_SOURCE
"src/utils/free_camera/plugin.cpp"
"src/utils/free_camera/controller.cpp"

"src/audio/plugin.cpp"
"src/audio/source.cpp"
"src/audio/listener.cpp"
"src/audio/audio.cpp"
"src/audio/bridge.cpp"

Expand Down
2 changes: 1 addition & 1 deletion engine/include/cubos/engine/audio/audio.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ namespace cubos::engine
std::shared_ptr<cubos::core::al::impl::Buffer> mData; // Raw data of the audio
size_t mLength; // Audio length in seconds TODO: add getter in audio

explicit Audio(std::shared_ptr<cubos::core::al::MiniaudioContext>, core::memory::Stream& stream);
explicit Audio(std::shared_ptr<cubos::core::al::AudioContext>, core::memory::Stream& stream);
Audio(Audio&& other) noexcept;
~Audio();
};
Expand Down
4 changes: 2 additions & 2 deletions engine/include/cubos/engine/audio/bridge.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ namespace cubos::engine
class AudioBridge : public FileBridge
{
public:
std::shared_ptr<cubos::core::al::MiniaudioContext> mContext;
std::shared_ptr<cubos::core::al::AudioContext> mContext;

/// @brief Constructs a bridge.
AudioBridge(std::shared_ptr<cubos::core::al::MiniaudioContext> context)
AudioBridge(std::shared_ptr<cubos::core::al::AudioContext> context)
: FileBridge(core::reflection::reflect<Audio>())
, mContext(context)
{
Expand Down
22 changes: 22 additions & 0 deletions engine/include/cubos/engine/audio/listener.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/// @file
/// @brief Component @ref cubos::engine::AudioListener.
/// @ingroup audio-plugin

#pragma once

#include <cubos/core/reflection/reflect.hpp>

#include <cubos/engine/api.hpp>

namespace cubos::engine
{
/// @brief Component which adds an AudioListener to the entitiy
/// @ingroup audio-plugin

struct CUBOS_ENGINE_API AudioListener
{
CUBOS_REFLECT;

bool active; // is the listener active (assigned to an entity)
};
} // namespace cubos::engine
40 changes: 40 additions & 0 deletions engine/include/cubos/engine/audio/plugin.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/// @dir
/// @brief @ref audio-plugin plugin directory.

/// @file
/// @brief Plugin entry point.
/// @ingroup audio-plugin

#pragma once

#include <cubos/engine/audio/audio.hpp>
#include <cubos/engine/audio/listener.hpp>
#include <cubos/engine/audio/source.hpp>
#include <cubos/engine/prelude.hpp>

namespace cubos::engine
{
/// @defgroup audio-plugin Audio
/// @ingroup engine
/// @brief Adds audio management to @b Cubos
///
/// ## Settings
/// - `audio.listenerCount` - number of listeners per audio device (default: `4`).
///
/// ## Dependencies
/// - @ref settings-plugin

/// @brief Initializes the audio manager (after @ref settingsTag).
CUBOS_ENGINE_API extern Tag audioInitTag;

/// @brief Systems which add bridges to the audio manager should be tagged with this.
CUBOS_ENGINE_API extern Tag audioBridgeTag;

/// @brief Startup systems which load audios should be tagged with this.
CUBOS_ENGINE_API extern Tag audioTag;

/// @brief Plugin entry function.
/// @param cubos @b Cubos main class.
/// @ingroup assets-plugin
CUBOS_ENGINE_API void audioPlugin(Cubos& cubos);
} // namespace cubos::engine
20 changes: 20 additions & 0 deletions engine/include/cubos/engine/audio/source.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/// @file
/// @brief Component @ref cubos::engine::AudioSource.
/// @ingroup audio-plugin

#pragma once

#include <cubos/core/reflection/reflect.hpp>

#include <cubos/engine/api.hpp>

namespace cubos::engine
{
/// @brief Component which adds an AudioSource to the entitiy
/// @ingroup audio-plugin

struct CUBOS_ENGINE_API AudioSource
{
CUBOS_REFLECT;
};
} // namespace cubos::engine
2 changes: 1 addition & 1 deletion engine/src/audio/audio.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ CUBOS_REFLECT_IMPL(cubos::engine::Audio)
return core::reflection::Type::create("cubos::engine::Audio");
}

cubos::engine::Audio::Audio(std::shared_ptr<cubos::core::al::MiniaudioContext> context, core::memory::Stream& stream)
cubos::engine::Audio::Audio(std::shared_ptr<cubos::core::al::AudioContext> context, core::memory::Stream& stream)
{
stream.seek(0, cubos::core::memory::SeekOrigin::End);
size_t streamSize = stream.tell();
Expand Down
11 changes: 11 additions & 0 deletions engine/src/audio/listener.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#include <cubos/core/ecs/reflection.hpp>
#include <cubos/core/reflection/external/primitives.hpp>

#include <cubos/engine/audio/listener.hpp>

CUBOS_REFLECT_IMPL(cubos::engine::AudioListener)
{
return core::ecs::TypeBuilder<AudioListener>("cubos::engine::AudioListener")
.withField("active", &AudioListener::active)
.build();
}
39 changes: 39 additions & 0 deletions engine/src/audio/plugin.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#include <cubos/core/al/audio_context.hpp>

#include <cubos/engine/assets/plugin.hpp>
#include <cubos/engine/audio/bridge.hpp>
#include <cubos/engine/audio/plugin.hpp>
#include <cubos/engine/settings/plugin.hpp>

CUBOS_DEFINE_TAG(cubos::engine::audioInitTag);
CUBOS_DEFINE_TAG(cubos::engine::audioBridgeTag);
CUBOS_DEFINE_TAG(cubos::engine::audioTag);

using cubos::core::al::AudioContext;

void cubos::engine::audioPlugin(Cubos& cubos)
{
cubos.depends(settingsPlugin);

cubos.component<AudioSource>();
cubos.component<AudioListener>();

cubos.startupTag(audioTag);
cubos.startupTag(audioInitTag).after(settingsTag);
cubos.startupTag(audioBridgeTag).after(audioInitTag).before(audioTag);

cubos.startupSystem("initalize Audio plugin").tagged(audioInitTag).call([](Assets& assets, Settings& settings) {
auto audioContext = AudioContext::create();

unsigned int listenerCount = static_cast<unsigned int>(
settings.getInteger("audio.listener.count", static_cast<int>(MA_ENGINE_MAX_LISTENERS)));

auto audioDevice = audioContext->createDevice("MiniAudioDevice", listenerCount);

auto bridge = std::make_shared<AudioBridge>(audioContext);
assets.registerBridge(".wav", bridge);
assets.registerBridge(".mp3", bridge);
assets.registerBridge(".flac", bridge);
assets.registerBridge(".ogg", bridge);
});
}
9 changes: 9 additions & 0 deletions engine/src/audio/source.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#include <cubos/core/ecs/reflection.hpp>
#include <cubos/core/reflection/external/primitives.hpp>

#include <cubos/engine/audio/source.hpp>

CUBOS_REFLECT_IMPL(cubos::engine::AudioSource)
{
return core::ecs::TypeBuilder<AudioSource>("cubos::engine::AudioSource").build();
}

0 comments on commit 2b9a02d

Please sign in to comment.