Added mosra's suggestions
This commit is contained in:
88
2
Normal file
88
2
Normal file
@@ -0,0 +1,88 @@
|
|||||||
|
#ifndef CHARGE_AUDIO_BASE_H
|
||||||
|
#define CHARGE_AUDIO_BASE_H
|
||||||
|
#include <Corrade/Containers/Containers.h>
|
||||||
|
#include <Corrade/Containers/Optional.h>
|
||||||
|
#include <Corrade/Containers/Pointer.h>
|
||||||
|
#include <Magnum/Magnum.h>
|
||||||
|
#include <Magnum/Math/Vector.h>
|
||||||
|
#include <functional>
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
namespace ChargeAudio {
|
||||||
|
namespace _ma {
|
||||||
|
#include "miniaudio/miniaudio.h"
|
||||||
|
}
|
||||||
|
using namespace Corrade;
|
||||||
|
using namespace _ma;
|
||||||
|
|
||||||
|
typedef Containers::Optional<class Sound> SoundContainer;
|
||||||
|
typedef Containers::Optional<class Listener> 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<void(Sound *)> 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
|
@@ -18,21 +18,21 @@ typedef Containers::Pointer<class Sound> SoundContainer;
|
|||||||
typedef Containers::Pointer<class Listener> ListenerContainer;
|
typedef Containers::Pointer<class Listener> ListenerContainer;
|
||||||
class Sound {
|
class Sound {
|
||||||
public:
|
public:
|
||||||
enum SoundState { Idle, Playing, Paused, Finished };
|
enum class SoundState { Idle, Playing, Paused, Finished };
|
||||||
|
|
||||||
~Sound();
|
~Sound();
|
||||||
void Play();
|
void Play();
|
||||||
void Pause();
|
void Pause();
|
||||||
void Reset();
|
void Reset();
|
||||||
|
|
||||||
SoundState GetState();
|
const SoundState GetState();
|
||||||
float GetPlaybackTime();
|
const float GetPlaybackTime();
|
||||||
bool SetPlaybackTime(float time);
|
bool SetPlaybackTime(float time);
|
||||||
float GetDuration();
|
const float GetDuration();
|
||||||
void SetPosition(Magnum::Vector3 position);
|
void SetPosition(Magnum::Vector3 position);
|
||||||
Magnum::Vector3 GetPosition();
|
const Magnum::Vector3 GetPosition();
|
||||||
void SetVolume(float value);
|
void SetVolume(float value);
|
||||||
float GetVolume();
|
const float GetVolume();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
Sound(class Engine *engine, std::function<void(Sound *)> setupFunction,
|
Sound(class Engine *engine, std::function<void(Sound *)> setupFunction,
|
||||||
@@ -50,11 +50,11 @@ private:
|
|||||||
class Listener {
|
class Listener {
|
||||||
public:
|
public:
|
||||||
void SetEnabled(bool isEnabled);
|
void SetEnabled(bool isEnabled);
|
||||||
bool GetEnabled();
|
const bool GetEnabled();
|
||||||
void SetDirection(Magnum::Vector3 position);
|
void SetDirection(Magnum::Vector3 position);
|
||||||
Magnum::Vector3 GetDirection();
|
const Magnum::Vector3 GetDirection();
|
||||||
void SetPosition(Magnum::Vector3 position);
|
void SetPosition(Magnum::Vector3 position);
|
||||||
Magnum::Vector3 GetPosition();
|
const Magnum::Vector3 GetPosition();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
Listener();
|
Listener();
|
||||||
@@ -73,7 +73,7 @@ public:
|
|||||||
ListenerContainer CreateListener();
|
ListenerContainer CreateListener();
|
||||||
|
|
||||||
void SetVolume(float value);
|
void SetVolume(float value);
|
||||||
float GetVolume();
|
const float GetVolume();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
ma_engine maEngine;
|
ma_engine maEngine;
|
||||||
|
@@ -1,4 +1,5 @@
|
|||||||
#include "ChargeAudio.hpp"
|
#include "ChargeAudio.hpp"
|
||||||
|
#include <Corrade/Containers/Containers.h>
|
||||||
#include <Corrade/Containers/Pointer.h>
|
#include <Corrade/Containers/Pointer.h>
|
||||||
#include <Corrade/Utility/Debug.h>
|
#include <Corrade/Utility/Debug.h>
|
||||||
#include <Magnum/Math/Vector3.h>
|
#include <Magnum/Math/Vector3.h>
|
||||||
@@ -20,9 +21,9 @@ Engine::Engine() {
|
|||||||
|
|
||||||
Engine::~Engine() { ma_engine_uninit(&maEngine); }
|
Engine::~Engine() { ma_engine_uninit(&maEngine); }
|
||||||
|
|
||||||
Containers::Pointer<Sound> Engine::CreateSound(const std::string filepath,
|
SoundContainer Engine::CreateSound(const std::string filepath,
|
||||||
bool streamFile) {
|
bool streamFile) {
|
||||||
return Containers::Pointer<Sound>(new Sound(
|
return SoundContainer(new Sound(
|
||||||
this,
|
this,
|
||||||
[filepath, streamFile](Sound *sound) {
|
[filepath, streamFile](Sound *sound) {
|
||||||
sound->maConfig.pFilePath = filepath.c_str();
|
sound->maConfig.pFilePath = filepath.c_str();
|
||||||
@@ -31,8 +32,8 @@ Containers::Pointer<Sound> Engine::CreateSound(const std::string filepath,
|
|||||||
"Failed to create the sound from the file: " + filepath));
|
"Failed to create the sound from the file: " + filepath));
|
||||||
}
|
}
|
||||||
|
|
||||||
Containers::Pointer<Listener> Engine::CreateListener() {
|
ListenerContainer Engine::CreateListener() {
|
||||||
auto listener = Containers::Pointer<Listener>(new Listener());
|
auto listener = ListenerContainer(new Listener());
|
||||||
listener->baseEngine = this;
|
listener->baseEngine = this;
|
||||||
listener->listenerID = listenerCounter++;
|
listener->listenerID = listenerCounter++;
|
||||||
|
|
||||||
@@ -41,4 +42,4 @@ Containers::Pointer<Listener> Engine::CreateListener() {
|
|||||||
|
|
||||||
// Controls
|
// Controls
|
||||||
void Engine::SetVolume(float value) { ma_engine_set_volume(&maEngine, value); }
|
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); }
|
||||||
|
@@ -11,7 +11,7 @@ void Listener::SetEnabled(bool isEnabled) {
|
|||||||
ma_engine_listener_set_enabled(&baseEngine->maEngine, listenerID, 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);
|
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());
|
position.x(), position.y(), position.z());
|
||||||
}
|
}
|
||||||
|
|
||||||
Magnum::Vector3 Listener::GetDirection() {
|
const Magnum::Vector3 Listener::GetDirection() {
|
||||||
ma_vec3f dir =
|
ma_vec3f dir =
|
||||||
ma_engine_listener_get_direction(&baseEngine->maEngine, listenerID);
|
ma_engine_listener_get_direction(&baseEngine->maEngine, listenerID);
|
||||||
return Magnum::Vector3{dir.x, dir.y, dir.z};
|
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());
|
position.x(), position.y(), position.z());
|
||||||
}
|
}
|
||||||
|
|
||||||
Magnum::Vector3 Listener::GetPosition() {
|
const Magnum::Vector3 Listener::GetPosition() {
|
||||||
ma_vec3f pos =
|
ma_vec3f pos =
|
||||||
ma_engine_listener_get_position(&baseEngine->maEngine, listenerID);
|
ma_engine_listener_get_position(&baseEngine->maEngine, listenerID);
|
||||||
return Magnum::Vector3(pos.x, pos.y, pos.z);
|
return Magnum::Vector3(pos.x, pos.y, pos.z);
|
||||||
|
@@ -26,15 +26,15 @@ Sound::Sound(Engine *engine, std::function<void(Sound *)> setupFunction,
|
|||||||
}
|
}
|
||||||
|
|
||||||
Sound::~Sound() { ma_sound_uninit(&maSound); }
|
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;
|
float time;
|
||||||
ma_sound_get_length_in_seconds(&this->maSound, &time);
|
ma_sound_get_length_in_seconds(&this->maSound, &time);
|
||||||
return time;
|
return time;
|
||||||
}
|
}
|
||||||
|
|
||||||
float Sound::GetPlaybackTime() {
|
const float Sound::GetPlaybackTime() {
|
||||||
float time;
|
float time;
|
||||||
ma_sound_get_cursor_in_seconds(&this->maSound, &time);
|
ma_sound_get_cursor_in_seconds(&this->maSound, &time);
|
||||||
return time;
|
return time;
|
||||||
@@ -71,12 +71,12 @@ void Sound::Reset() { ma_sound_seek_to_pcm_frame(&maSound, 0); }
|
|||||||
void Sound::SetPosition(Magnum::Vector3 position) {
|
void Sound::SetPosition(Magnum::Vector3 position) {
|
||||||
ma_sound_set_position(&maSound, position.x(), position.y(), position.z());
|
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);
|
ma_vec3f pos = ma_sound_get_position(&maSound);
|
||||||
return Magnum::Vector3(pos.x, pos.y, pos.z);
|
return Magnum::Vector3(pos.x, pos.y, pos.z);
|
||||||
}
|
}
|
||||||
void Sound::SetVolume(float value) { ma_sound_set_volume(&maSound, value); }
|
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
|
// STATICs
|
||||||
void Sound::onSoundFinish(void *customData, ma_sound *) {
|
void Sound::onSoundFinish(void *customData, ma_sound *) {
|
||||||
|
Reference in New Issue
Block a user