Skip to content

Commit

Permalink
feat(audio): Add Sound asset (#230)
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 58559da commit 31b51bd
Show file tree
Hide file tree
Showing 5 changed files with 130 additions and 0 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/audio.cpp"
"src/audio/bridge.cpp"

"src/assets/plugin.cpp"
"src/assets/assets.cpp"
"src/assets/bridge.cpp"
Expand Down
27 changes: 27 additions & 0 deletions engine/include/cubos/engine/audio/audio.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/// @file
/// @brief Class @ref cubos::engine::Audio.
/// @ingroup audio-plugin
#pragma once

#include <cubos/core/al/miniaudio_device.hpp>
#include <cubos/core/memory/stream.hpp>
#include <cubos/core/reflection/reflect.hpp>

#include <cubos/engine/assets/asset.hpp>

namespace cubos::engine
{
/// @brief Asset containing raw Audio data.
///
/// @ingroup audio-plugin
struct CUBOS_ENGINE_API Audio
{
CUBOS_REFLECT;
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(core::memory::Stream& stream);
Audio(Audio&& other) noexcept;
~Audio();
};
} // namespace cubos::engine
30 changes: 30 additions & 0 deletions engine/include/cubos/engine/audio/bridge.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/// @file
/// @brief Class @ref cubos::engine::AudioBridge.
/// @ingroup audio-plugin

#pragma once

#include <cubos/engine/assets/bridges/file.hpp>
#include <cubos/engine/audio/audio.hpp>

namespace cubos::engine
{
/// @brief Bridge which loads and saves @ref Sound assets.
///
/// Uses the format specified in @ref Audio::loadFrom and @ref Audio::writeTo
///
/// @ingroup audio-plugin
class AudioBridge : public FileBridge
{
public:
/// @brief Constructs a bridge.
AudioBridge()
: FileBridge(core::reflection::reflect<Audio>())
{
}

protected:
bool loadFromFile(Assets& assets, const AnyAsset& handle, core::memory::Stream& stream) override;
bool saveToFile(const Assets& assets, const AnyAsset& handle, core::memory::Stream& stream) override;
};
} // namespace cubos::engine
44 changes: 44 additions & 0 deletions engine/src/audio/audio.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#include <cubos/core/log.hpp>
#include <cubos/core/reflection/external/cstring.hpp>
#include <cubos/core/reflection/type.hpp>

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

CUBOS_REFLECT_IMPL(cubos::engine::Audio)
{
return core::reflection::Type::create("cubos::engine::Audio");
}

cubos::engine::Audio::Audio(core::memory::Stream& stream)
{
stream.seek(0, cubos::core::memory::SeekOrigin::End);
size_t streamSize = stream.tell();
void* contents = operator new(sizeof(char) * streamSize);
stream.seek(0, cubos::core::memory::SeekOrigin::Begin);
if (stream.read((char*)contents, streamSize) < streamSize)
{
CUBOS_ERROR("Couldn't load audio: Read less than stream lenght");
// TODO: add audio buffer here
mData = nullptr;
operator delete(contents);
return;
};

mData = cubos::core::al::MiniaudioDevice::createBuffer(contents, (size_t)streamSize);
if (mData == nullptr)
{
CUBOS_ERROR("Couldn't create audio buffer");
}
operator delete(contents);
}

cubos::engine::Audio::~Audio()
{
// no need to delete buffer thanks to shared_ptr
}

cubos::engine::Audio::Audio(Audio&& other) noexcept
: mData(other.mData)
{
other.mData = nullptr;
}
26 changes: 26 additions & 0 deletions engine/src/audio/bridge.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#include <cubos/core/log.hpp>

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

using namespace cubos::engine;

bool AudioBridge::loadFromFile(Assets& assets, const AnyAsset& handle, core::memory::Stream& stream)
{
Audio audio{stream};
if (audio.mData == nullptr)
{
return false;
}
assets.store(handle, std::move(audio));
return true;
}
bool AudioBridge::saveToFile(const Assets& assets, const AnyAsset& handle, core::memory::Stream& stream)
{
(void)assets;
(void)handle;
(void)stream;
CUBOS_ERROR("Saving audios is currently unsupported");
return false;
}

0 comments on commit 31b51bd

Please sign in to comment.