diff --git a/src/ChargeAudio.hpp b/src/ChargeAudio.hpp index dc3e23a..422a4a5 100644 --- a/src/ChargeAudio.hpp +++ b/src/ChargeAudio.hpp @@ -3,6 +3,7 @@ #include #include #include +#include #include namespace ChargeAudio { @@ -27,8 +28,8 @@ public: float GetVolume(); private: - Sound(class Engine *engine); - void init(std::string additionalErrorMessage = ""); + Sound(class Engine *engine, std::function setupFunction, + std::string additionalErrorMessage = ""); static void onSoundFinish(void *customData, ma_sound *); class Engine *baseEngine; @@ -61,7 +62,8 @@ public: ~Engine(); // Creating tools - Containers::Pointer CreateSound(std::string filepath); + Containers::Pointer CreateSound(std::string filepath, + bool streamFile = false); Containers::Pointer CreateListener(); void SetVolume(float value); diff --git a/src/Engine.cpp b/src/Engine.cpp index 0477456..a3844cc 100644 --- a/src/Engine.cpp +++ b/src/Engine.cpp @@ -20,14 +20,15 @@ Engine::Engine() { Engine::~Engine() { ma_engine_uninit(&maEngine); } -Containers::Pointer Engine::CreateSound(const std::string filepath) { - auto sound = Containers::Pointer(new Sound(this)); - sound->maConfig.pFilePath = filepath.c_str(); - sound->maConfig.flags = 0; - sound->maConfig.pInitialAttachment = NULL; - sound->maConfig.pDoneFence = NULL; - sound->init("Failed to create the sound from the file: " + filepath); - return sound; +Containers::Pointer Engine::CreateSound(const std::string filepath, + bool streamFile) { + return Containers::Pointer(new Sound( + this, + [filepath, streamFile](Sound *sound) { + sound->maConfig.pFilePath = filepath.c_str(); + sound->maConfig.flags = (streamFile ? MA_SOUND_FLAG_STREAM : 0); + }, + "Failed to create the sound from the file: " + filepath)); } Containers::Pointer Engine::CreateListener() { diff --git a/src/Sound.cpp b/src/Sound.cpp index b7ad0bd..1390865 100644 --- a/src/Sound.cpp +++ b/src/Sound.cpp @@ -1,20 +1,18 @@ #include "ChargeAudio.hpp" #include #include + +#include #include using namespace ChargeAudio; -Sound::Sound(Engine *engine) : baseEngine(engine) { +Sound::Sound(Engine *engine, std::function setupFunction, + std::string additionalErrorMessage) + : baseEngine(engine) { maConfig = ma_sound_config_init_2(&baseEngine->maEngine); maConfig.endCallback = Sound::onSoundFinish; maConfig.pEndCallbackUserData = this; -} - -Sound::~Sound() { ma_sound_uninit(&maSound); } - -Sound::SoundState Sound::GetState() { return state; } - -void Sound::init(std::string additionalErrorMessage) { + setupFunction(this); ma_result maResponse = ma_sound_init_ex(&baseEngine->maEngine, &maConfig, &maSound); if (maResponse != MA_SUCCESS) { @@ -26,6 +24,9 @@ void Sound::init(std::string additionalErrorMessage) { } } +Sound::~Sound() { ma_sound_uninit(&maSound); } +Sound::SoundState Sound::GetState() { return state; } + // Controls void Sound::Play() { ma_sound_start(&maSound);