-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Co-Authored-By: João Miguel Nogueira <[email protected]>
- Loading branch information
1 parent
58559da
commit a7bae01
Showing
5 changed files
with
130 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |