Compare commits
4 Commits
ee83818de2
...
64b5a8af29
Author | SHA1 | Date | |
---|---|---|---|
64b5a8af29 | |||
76b53b5f71 | |||
67e423a77d | |||
a40c0665ac |
@@ -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
|
||||||
|
@@ -13,20 +13,40 @@ using namespace Corrade;
|
|||||||
using namespace _ma;
|
using namespace _ma;
|
||||||
class Sound {
|
class Sound {
|
||||||
public:
|
public:
|
||||||
|
enum SoundState { Idle, Playing, Paused, Finished };
|
||||||
|
|
||||||
~Sound();
|
~Sound();
|
||||||
void Play();
|
void Play();
|
||||||
void Pause();
|
void Pause();
|
||||||
void Reset();
|
void Reset();
|
||||||
|
|
||||||
|
SoundState GetState();
|
||||||
void SetPosition(Magnum::Vector3 position);
|
void SetPosition(Magnum::Vector3 position);
|
||||||
Magnum::Vector3 GetPosition();
|
Magnum::Vector3 GetPosition();
|
||||||
void SetVolume(float value);
|
void SetVolume(float value);
|
||||||
float GetVolume();
|
float GetVolume();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
Sound();
|
Sound(class Engine *engine);
|
||||||
|
static void onSoundFinish(void *customData, ma_sound *);
|
||||||
|
|
||||||
class Engine *baseEngine;
|
class Engine *baseEngine;
|
||||||
ma_sound maSound;
|
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;
|
friend class Engine;
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -34,16 +54,20 @@ 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;
|
||||||
|
friend class Sound;
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace ChargeAudio
|
} // namespace ChargeAudio
|
||||||
|
@@ -21,11 +21,12 @@ Engine::Engine() {
|
|||||||
Engine::~Engine() { ma_engine_uninit(&maEngine); }
|
Engine::~Engine() { ma_engine_uninit(&maEngine); }
|
||||||
|
|
||||||
Containers::Pointer<Sound> Engine::CreateSound(std::string filepath) {
|
Containers::Pointer<Sound> Engine::CreateSound(std::string filepath) {
|
||||||
auto sound = Containers::Pointer<Sound>(new Sound());
|
auto sound = Containers::Pointer<Sound>(new Sound(this));
|
||||||
sound->baseEngine = this;
|
|
||||||
|
|
||||||
maResponse = ma_sound_init_from_file(&maEngine, filepath.c_str(), 0, NULL,
|
maResponse = ma_sound_init_from_file(&maEngine, filepath.c_str(), 0, NULL,
|
||||||
NULL, &sound->maSound);
|
NULL, &sound->maSound);
|
||||||
|
sound->maSound.endCallback = Sound::onSoundFinish;
|
||||||
|
sound->maSound.pEndCallbackUserData = sound.get();
|
||||||
|
|
||||||
if (maResponse != MA_SUCCESS) {
|
if (maResponse != MA_SUCCESS) {
|
||||||
Utility::Error{} << "Failed to create the sound from the file: "
|
Utility::Error{} << "Failed to create the sound from the file: "
|
||||||
<< filepath.c_str() << " (" << maResponse << ")";
|
<< filepath.c_str() << " (" << maResponse << ")";
|
||||||
@@ -36,17 +37,14 @@ Containers::Pointer<Sound> Engine::CreateSound(std::string filepath) {
|
|||||||
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); }
|
||||||
|
25
src/Listener.cpp
Normal file
25
src/Listener.cpp
Normal 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);
|
||||||
|
}
|
@@ -3,12 +3,23 @@
|
|||||||
#include <Magnum/Math/Vector3.h>
|
#include <Magnum/Math/Vector3.h>
|
||||||
|
|
||||||
using namespace ChargeAudio;
|
using namespace ChargeAudio;
|
||||||
Sound::Sound() {}
|
Sound::Sound(Engine *engine) : baseEngine(engine) {}
|
||||||
|
|
||||||
Sound::~Sound() { ma_sound_uninit(&maSound); }
|
Sound::~Sound() { ma_sound_uninit(&maSound); }
|
||||||
|
|
||||||
|
Sound::SoundState Sound::GetState() { return state; }
|
||||||
|
|
||||||
// Controls
|
// Controls
|
||||||
void Sound::Play() { ma_sound_start(&maSound); }
|
void Sound::Play() {
|
||||||
void Sound::Pause() { ma_sound_stop(&maSound); }
|
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::Reset() { ma_sound_seek_to_pcm_frame(&maSound, 0); }
|
||||||
|
|
||||||
void Sound::SetPosition(Magnum::Vector3 position) {
|
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); }
|
void Sound::SetVolume(float value) { ma_sound_set_volume(&maSound, value); }
|
||||||
float Sound::GetVolume() { return ma_sound_get_volume(&maSound); }
|
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;
|
||||||
|
}
|
||||||
|
Reference in New Issue
Block a user