Added mosra's suggestions

This commit is contained in:
2025-09-09 14:48:59 +03:00
parent ff1241fe36
commit 7c5d80cd31
5 changed files with 113 additions and 24 deletions

88
2 Normal file
View 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

View File

@@ -18,21 +18,21 @@ typedef Containers::Pointer<class Sound> SoundContainer;
typedef Containers::Pointer<class Listener> 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<void(Sound *)> 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;

View File

@@ -1,4 +1,5 @@
#include "ChargeAudio.hpp"
#include <Corrade/Containers/Containers.h>
#include <Corrade/Containers/Pointer.h>
#include <Corrade/Utility/Debug.h>
#include <Magnum/Math/Vector3.h>
@@ -20,9 +21,9 @@ Engine::Engine() {
Engine::~Engine() { ma_engine_uninit(&maEngine); }
Containers::Pointer<Sound> Engine::CreateSound(const std::string filepath,
SoundContainer Engine::CreateSound(const std::string filepath,
bool streamFile) {
return Containers::Pointer<Sound>(new Sound(
return SoundContainer(new Sound(
this,
[filepath, streamFile](Sound *sound) {
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));
}
Containers::Pointer<Listener> Engine::CreateListener() {
auto listener = Containers::Pointer<Listener>(new Listener());
ListenerContainer Engine::CreateListener() {
auto listener = ListenerContainer(new Listener());
listener->baseEngine = this;
listener->listenerID = listenerCounter++;
@@ -41,4 +42,4 @@ Containers::Pointer<Listener> 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); }

View File

@@ -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);

View File

@@ -26,15 +26,15 @@ Sound::Sound(Engine *engine, std::function<void(Sound *)> 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 *) {