diff --git a/2 b/2 new file mode 100644 index 0000000..49d72c0 --- /dev/null +++ b/2 @@ -0,0 +1,88 @@ +#ifndef CHARGE_AUDIO_BASE_H +#define CHARGE_AUDIO_BASE_H +#include +#include +#include +#include +#include +#include +#include + +namespace ChargeAudio { +namespace _ma { +#include "miniaudio/miniaudio.h" +} +using namespace Corrade; +using namespace _ma; + +typedef Containers::Optional SoundContainer; +typedef Containers::Optional ListenerContainer; +class Sound { +public: + enum class SoundState { Idle, Playing, Paused, Finished }; + + ~Sound(); + void Play(); + void Pause(); + void Reset(); + + const SoundState GetState(); + const float GetPlaybackTime(); + bool SetPlaybackTime(float time); + const float GetDuration(); + void SetPosition(Magnum::Vector3 position); + const Magnum::Vector3 GetPosition(); + void SetVolume(float value); + const float GetVolume(); + +private: + Sound(class Engine *engine, std::function setupFunction, + std::string additionalErrorMessage = ""); + static void onSoundFinish(void *customData, ma_sound *); + + class Engine *baseEngine; + ma_sound maSound; + ma_sound_config maConfig; + SoundState state = SoundState::Idle; + + friend class Engine; +}; + +class Listener { +public: + void SetEnabled(bool isEnabled); + const bool GetEnabled(); + void SetDirection(Magnum::Vector3 position); + const Magnum::Vector3 GetDirection(); + void SetPosition(Magnum::Vector3 position); + const Magnum::Vector3 GetPosition(); + +private: + Listener(); + class Engine *baseEngine; + ma_uint32 listenerID; + friend class Engine; +}; + +class Engine { +public: + Engine(); + ~Engine(); + + // Creating tools + SoundContainer CreateSound(std::string filepath, bool streamFile = false); + ListenerContainer CreateListener(); + + void SetVolume(float value); + const float GetVolume(); + +private: + ma_engine maEngine; + ma_result maResponse; + ma_uint64 listenerCounter = 0; + friend class Listener; + friend class Sound; +}; + +} // namespace ChargeAudio +#endif diff --git a/src/ChargeAudio.hpp b/src/ChargeAudio.hpp index a469966..fa54826 100644 --- a/src/ChargeAudio.hpp +++ b/src/ChargeAudio.hpp @@ -18,21 +18,21 @@ typedef Containers::Pointer SoundContainer; typedef Containers::Pointer ListenerContainer; class Sound { public: - enum SoundState { Idle, Playing, Paused, Finished }; + enum class SoundState { Idle, Playing, Paused, Finished }; ~Sound(); void Play(); void Pause(); void Reset(); - SoundState GetState(); - float GetPlaybackTime(); + const SoundState GetState(); + const float GetPlaybackTime(); bool SetPlaybackTime(float time); - float GetDuration(); + const float GetDuration(); void SetPosition(Magnum::Vector3 position); - Magnum::Vector3 GetPosition(); + const Magnum::Vector3 GetPosition(); void SetVolume(float value); - float GetVolume(); + const float GetVolume(); private: Sound(class Engine *engine, std::function setupFunction, @@ -50,11 +50,11 @@ private: class Listener { public: void SetEnabled(bool isEnabled); - bool GetEnabled(); + const bool GetEnabled(); void SetDirection(Magnum::Vector3 position); - Magnum::Vector3 GetDirection(); + const Magnum::Vector3 GetDirection(); void SetPosition(Magnum::Vector3 position); - Magnum::Vector3 GetPosition(); + const Magnum::Vector3 GetPosition(); private: Listener(); @@ -73,7 +73,7 @@ public: ListenerContainer CreateListener(); void SetVolume(float value); - float GetVolume(); + const float GetVolume(); private: ma_engine maEngine; diff --git a/src/Engine.cpp b/src/Engine.cpp index a3844cc..6bc9495 100644 --- a/src/Engine.cpp +++ b/src/Engine.cpp @@ -1,4 +1,5 @@ #include "ChargeAudio.hpp" +#include #include #include #include @@ -20,9 +21,9 @@ Engine::Engine() { Engine::~Engine() { ma_engine_uninit(&maEngine); } -Containers::Pointer Engine::CreateSound(const std::string filepath, - bool streamFile) { - return Containers::Pointer(new Sound( +SoundContainer Engine::CreateSound(const std::string filepath, + bool streamFile) { + return SoundContainer(new Sound( this, [filepath, streamFile](Sound *sound) { sound->maConfig.pFilePath = filepath.c_str(); @@ -31,8 +32,8 @@ Containers::Pointer Engine::CreateSound(const std::string filepath, "Failed to create the sound from the file: " + filepath)); } -Containers::Pointer Engine::CreateListener() { - auto listener = Containers::Pointer(new Listener()); +ListenerContainer Engine::CreateListener() { + auto listener = ListenerContainer(new Listener()); listener->baseEngine = this; listener->listenerID = listenerCounter++; @@ -41,4 +42,4 @@ Containers::Pointer Engine::CreateListener() { // Controls void Engine::SetVolume(float value) { ma_engine_set_volume(&maEngine, value); } -float Engine::GetVolume() { return ma_engine_get_volume(&maEngine); } +const float Engine::GetVolume() { return ma_engine_get_volume(&maEngine); } diff --git a/src/Listener.cpp b/src/Listener.cpp index 5ba991c..8f886ef 100644 --- a/src/Listener.cpp +++ b/src/Listener.cpp @@ -11,7 +11,7 @@ void Listener::SetEnabled(bool isEnabled) { ma_engine_listener_set_enabled(&baseEngine->maEngine, listenerID, isEnabled); } -bool Listener::GetEnabled() { +const bool Listener::GetEnabled() { return ma_engine_listener_is_enabled(&baseEngine->maEngine, listenerID); } @@ -20,7 +20,7 @@ void Listener::SetDirection(Magnum::Vector3 position) { position.x(), position.y(), position.z()); } -Magnum::Vector3 Listener::GetDirection() { +const Magnum::Vector3 Listener::GetDirection() { ma_vec3f dir = ma_engine_listener_get_direction(&baseEngine->maEngine, listenerID); return Magnum::Vector3{dir.x, dir.y, dir.z}; @@ -31,7 +31,7 @@ void Listener::SetPosition(Magnum::Vector3 position) { position.x(), position.y(), position.z()); } -Magnum::Vector3 Listener::GetPosition() { +const Magnum::Vector3 Listener::GetPosition() { ma_vec3f pos = ma_engine_listener_get_position(&baseEngine->maEngine, listenerID); return Magnum::Vector3(pos.x, pos.y, pos.z); diff --git a/src/Sound.cpp b/src/Sound.cpp index 6bfc552..b8d2023 100644 --- a/src/Sound.cpp +++ b/src/Sound.cpp @@ -26,15 +26,15 @@ Sound::Sound(Engine *engine, std::function setupFunction, } Sound::~Sound() { ma_sound_uninit(&maSound); } -Sound::SoundState Sound::GetState() { return state; } +const Sound::SoundState Sound::GetState() { return state; } -float Sound::GetDuration() { +const float Sound::GetDuration() { float time; ma_sound_get_length_in_seconds(&this->maSound, &time); return time; } -float Sound::GetPlaybackTime() { +const float Sound::GetPlaybackTime() { float time; ma_sound_get_cursor_in_seconds(&this->maSound, &time); return time; @@ -71,12 +71,12 @@ void Sound::Reset() { ma_sound_seek_to_pcm_frame(&maSound, 0); } void Sound::SetPosition(Magnum::Vector3 position) { ma_sound_set_position(&maSound, position.x(), position.y(), position.z()); } -Magnum::Vector3 Sound::GetPosition() { +const Magnum::Vector3 Sound::GetPosition() { ma_vec3f pos = ma_sound_get_position(&maSound); return Magnum::Vector3(pos.x, pos.y, pos.z); } void Sound::SetVolume(float value) { ma_sound_set_volume(&maSound, value); } -float Sound::GetVolume() { return ma_sound_get_volume(&maSound); } +const float Sound::GetVolume() { return ma_sound_get_volume(&maSound); } // STATICs void Sound::onSoundFinish(void *customData, ma_sound *) {