Compare commits

...

4 Commits

Author SHA1 Message Date
cat
64b5a8af29 Detection for SoundState::Finished 2025-09-05 17:30:12 +03:00
cat
76b53b5f71 Sound has states on it now 2025-09-05 12:21:40 +03:00
cat
67e423a77d Added enabling to listeners 2025-09-05 12:16:47 +03:00
cat
a40c0665ac Added Listeners instead of single engine listener 2025-09-05 12:09:33 +03:00
5 changed files with 88 additions and 23 deletions

View File

@@ -19,8 +19,9 @@ pkg_check_modules(AVCODEC REQUIRED libavcodec)
pkg_check_modules(AVUTIL REQUIRED libavutil)
pkg_check_modules(SWRESAMPLE REQUIRED libswresample)
add_library(ChargeAudio SHARED "src/ChargeAudio.hpp" "src/Engine.cpp"
"src/Sound.cpp" "lib/miniaudio/miniaudio.c")
add_library(
ChargeAudio SHARED "src/ChargeAudio.hpp" "src/Engine.cpp" "src/Sound.cpp"
"src/Listener.cpp" "lib/miniaudio/miniaudio.c")
target_link_libraries(
ChargeAudio

View File

@@ -13,20 +13,40 @@ using namespace Corrade;
using namespace _ma;
class Sound {
public:
enum SoundState { Idle, Playing, Paused, Finished };
~Sound();
void Play();
void Pause();
void Reset();
SoundState GetState();
void SetPosition(Magnum::Vector3 position);
Magnum::Vector3 GetPosition();
void SetVolume(float value);
float GetVolume();
private:
Sound();
Sound(class Engine *engine);
static void onSoundFinish(void *customData, ma_sound *);
class Engine *baseEngine;
ma_sound maSound;
SoundState state = SoundState::Idle;
friend class Engine;
};
class Listener {
public:
void SetEnabled(bool isEnabled);
bool GetEnabled();
void SetPosition(Magnum::Vector3 position);
Magnum::Vector3 GetPosition();
private:
Listener();
class Engine *baseEngine;
ma_uint32 listenerID;
friend class Engine;
};
@@ -34,16 +54,20 @@ class Engine {
public:
Engine();
~Engine();
Containers::Pointer<Sound> CreateSound(std::string filepath);
void SetPosition(Magnum::Vector3 position);
Magnum::Vector3 GetPosition();
// Creating tools
Containers::Pointer<Sound> CreateSound(std::string filepath);
Containers::Pointer<Listener> CreateListener();
void SetVolume(float value);
float GetVolume();
private:
ma_engine maEngine;
ma_result maResponse;
ma_uint64 listenerCounter = 0;
friend class Listener;
friend class Sound;
};
} // namespace ChargeAudio

View File

@@ -21,11 +21,12 @@ Engine::Engine() {
Engine::~Engine() { ma_engine_uninit(&maEngine); }
Containers::Pointer<Sound> Engine::CreateSound(std::string filepath) {
auto sound = Containers::Pointer<Sound>(new Sound());
sound->baseEngine = this;
auto sound = Containers::Pointer<Sound>(new Sound(this));
maResponse = ma_sound_init_from_file(&maEngine, filepath.c_str(), 0, NULL,
NULL, &sound->maSound);
sound->maSound.endCallback = Sound::onSoundFinish;
sound->maSound.pEndCallbackUserData = sound.get();
if (maResponse != MA_SUCCESS) {
Utility::Error{} << "Failed to create the sound from the file: "
<< filepath.c_str() << " (" << maResponse << ")";
@@ -36,17 +37,14 @@ Containers::Pointer<Sound> Engine::CreateSound(std::string filepath) {
return sound;
}
Containers::Pointer<Listener> Engine::CreateListener() {
auto listener = Containers::Pointer<Listener>(new Listener());
listener->baseEngine = this;
listener->listenerID = listenerCounter++;
return listener;
}
// Controls
void Engine::SetPosition(Magnum::Vector3 position) {
ma_engine_listener_set_position(&maEngine, 0, position.x(), position.y(),
position.z());
}
Magnum::Vector3 Engine::GetPosition() {
ma_vec3f pos = ma_engine_listener_get_position(&maEngine, 0);
return Magnum::Vector3(pos.x, pos.y, pos.z);
;
}
void Engine::SetVolume(float value) { ma_engine_set_volume(&maEngine, value); }
float Engine::GetVolume() { return ma_engine_get_volume(&maEngine); }

25
src/Listener.cpp Normal file
View File

@@ -0,0 +1,25 @@
#include "ChargeAudio.hpp"
#include <Magnum/Math/Vector3.h>
using namespace ChargeAudio;
Listener::Listener() {}
// Controls
void Listener::SetEnabled(bool isEnabled) {
ma_engine_listener_set_enabled(&baseEngine->maEngine, listenerID, isEnabled);
}
bool Listener::GetEnabled() {
return ma_engine_listener_is_enabled(&baseEngine->maEngine, listenerID);
}
void Listener::SetPosition(Magnum::Vector3 position) {
ma_engine_listener_set_position(&baseEngine->maEngine, listenerID,
position.x(), position.y(), position.z());
}
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

@@ -3,12 +3,23 @@
#include <Magnum/Math/Vector3.h>
using namespace ChargeAudio;
Sound::Sound() {}
Sound::Sound(Engine *engine) : baseEngine(engine) {}
Sound::~Sound() { ma_sound_uninit(&maSound); }
Sound::SoundState Sound::GetState() { return state; }
// Controls
void Sound::Play() { ma_sound_start(&maSound); }
void Sound::Pause() { ma_sound_stop(&maSound); }
void Sound::Play() {
ma_sound_start(&maSound);
state = Sound::SoundState::Playing;
}
void Sound::Pause() {
ma_sound_stop(&maSound);
state = Sound::SoundState::Paused;
}
void Sound::Reset() { ma_sound_seek_to_pcm_frame(&maSound, 0); }
void Sound::SetPosition(Magnum::Vector3 position) {
@@ -20,3 +31,9 @@ Magnum::Vector3 Sound::GetPosition() {
}
void Sound::SetVolume(float value) { ma_sound_set_volume(&maSound, value); }
float Sound::GetVolume() { return ma_sound_get_volume(&maSound); }
// STATICs
void Sound::onSoundFinish(void *customData, ma_sound *) {
auto sound = static_cast<Sound *>(customData);
sound->state = SoundState::Finished;
}