Added Listeners instead of single engine listener

This commit is contained in:
2025-09-05 12:09:33 +03:00
parent ee83818de2
commit a40c0665ac
4 changed files with 48 additions and 17 deletions

View File

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

View File

@@ -30,20 +30,35 @@ private:
friend class Engine; friend class Engine;
}; };
class Listener {
public:
void SetPosition(Magnum::Vector3 position);
Magnum::Vector3 GetPosition();
private:
Listener();
class Engine *baseEngine;
ma_uint64 listenerID;
friend class Engine;
};
class Engine { class Engine {
public: public:
Engine(); Engine();
~Engine(); ~Engine();
Containers::Pointer<Sound> CreateSound(std::string filepath);
void SetPosition(Magnum::Vector3 position); // Creating tools
Magnum::Vector3 GetPosition(); Containers::Pointer<Sound> CreateSound(std::string filepath);
Containers::Pointer<Listener> CreateListener();
void SetVolume(float value); void SetVolume(float value);
float GetVolume(); float GetVolume();
private: private:
ma_engine maEngine; ma_engine maEngine;
ma_result maResponse; ma_result maResponse;
ma_uint64 listenerCounter = 0;
friend class Listener;
}; };
} // namespace ChargeAudio } // namespace ChargeAudio

View File

@@ -32,21 +32,17 @@ Containers::Pointer<Sound> Engine::CreateSound(std::string filepath) {
throw new std::runtime_error( throw new std::runtime_error(
"Failed to create the sound from file. Check STDERR for more info."); "Failed to create the sound from file. Check STDERR for more info.");
} }
return sound; return sound;
} }
Containers::Pointer<Listener> Engine::CreateListener() {
auto listener = Containers::Pointer<Listener>(new Listener());
listener->baseEngine = this;
listener->listenerID = listenerCounter++;
return listener;
}
// Controls // 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); } void Engine::SetVolume(float value) { ma_engine_set_volume(&maEngine, value); }
float Engine::GetVolume() { return ma_engine_get_volume(&maEngine); } float Engine::GetVolume() { return ma_engine_get_volume(&maEngine); }

19
src/Listener.cpp Normal file
View File

@@ -0,0 +1,19 @@
#include "ChargeAudio.hpp"
#include <Magnum/Math/Vector3.h>
using namespace ChargeAudio;
Listener::Listener() {}
// Controls
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);
;
}