aboutsummaryrefslogtreecommitdiff
path: root/src/crepe/facade
diff options
context:
space:
mode:
authorLoek Le Blansch <loek@pipeframe.xyz>2024-11-13 12:33:59 +0100
committerLoek Le Blansch <loek@pipeframe.xyz>2024-11-13 12:33:59 +0100
commit9d58e897fb68ab8dd001a5085cde2fae4634c274 (patch)
treeb70c33c6343be56a536c0b639435a45a1d2429ed /src/crepe/facade
parent87c74782e486647c46f9ca4d2f857094006e563c (diff)
move some files from `loek/tests` to `loek/audio`
Diffstat (limited to 'src/crepe/facade')
-rw-r--r--src/crepe/facade/Sound.cpp30
-rw-r--r--src/crepe/facade/Sound.h14
-rw-r--r--src/crepe/facade/SoundContext.cpp5
-rw-r--r--src/crepe/facade/SoundContext.h5
4 files changed, 31 insertions, 23 deletions
diff --git a/src/crepe/facade/Sound.cpp b/src/crepe/facade/Sound.cpp
index b8ea71a..648ec81 100644
--- a/src/crepe/facade/Sound.cpp
+++ b/src/crepe/facade/Sound.cpp
@@ -1,24 +1,26 @@
-#include <memory>
-
-#include "../Asset.h"
#include "../util/log.h"
#include "Sound.h"
#include "SoundContext.h"
using namespace crepe;
-using namespace std;
-Sound::Sound(SoundContext & ctx) : context(ctx) { dbg_trace(); }
+Sound::Sound(std::unique_ptr<Asset> res) {
+ dbg_trace();
+ this->load(std::move(res));
+}
+
+Sound::Sound(const char * src) {
+ dbg_trace();
+ this->load(std::make_unique<Asset>(src));
+}
-unique_ptr<Resource> Sound::clone(const Asset & src) const {
- auto instance = make_unique<Sound>(*this);
- instance->sample.load(src.canonical());
- return instance;
+void Sound::load(std::unique_ptr<Asset> res) {
+ this->sample.load(res->canonical());
}
void Sound::play() {
- SoundContext & ctx = this->context;
+ SoundContext & ctx = SoundContext::get_instance();
if (ctx.engine.getPause(this->handle)) {
// resume if paused
ctx.engine.setPause(this->handle, false);
@@ -30,13 +32,13 @@ void Sound::play() {
}
void Sound::pause() {
- SoundContext & ctx = this->context;
+ SoundContext & ctx = SoundContext::get_instance();
if (ctx.engine.getPause(this->handle)) return;
ctx.engine.setPause(this->handle, true);
}
void Sound::rewind() {
- SoundContext & ctx = this->context;
+ SoundContext & ctx = SoundContext::get_instance();
if (!ctx.engine.isValidVoiceHandle(this->handle)) return;
ctx.engine.seek(this->handle, 0);
}
@@ -44,7 +46,7 @@ void Sound::rewind() {
void Sound::set_volume(float volume) {
this->volume = volume;
- SoundContext & ctx = this->context;
+ SoundContext & ctx = SoundContext::get_instance();
if (!ctx.engine.isValidVoiceHandle(this->handle)) return;
ctx.engine.setVolume(this->handle, this->volume);
}
@@ -52,7 +54,7 @@ void Sound::set_volume(float volume) {
void Sound::set_looping(bool looping) {
this->looping = looping;
- SoundContext & ctx = this->context;
+ SoundContext & ctx = SoundContext::get_instance();
if (!ctx.engine.isValidVoiceHandle(this->handle)) return;
ctx.engine.setLooping(this->handle, this->looping);
}
diff --git a/src/crepe/facade/Sound.h b/src/crepe/facade/Sound.h
index e5b2f19..183bd7c 100644
--- a/src/crepe/facade/Sound.h
+++ b/src/crepe/facade/Sound.h
@@ -4,13 +4,11 @@
#include <soloud/soloud.h>
#include <soloud/soloud_wav.h>
-#include "../Resource.h"
+#include "../Asset.h"
namespace crepe {
-class SoundContext;
-
-class Sound : public Resource {
+class Sound {
public:
/**
* \brief Pause this sample
@@ -66,13 +64,15 @@ public:
bool get_looping() const { return this->looping; }
public:
- Sound(SoundContext & ctx);
- std::unique_ptr<Resource> clone(const Asset & src) const override;
+ Sound(const char * src);
+ Sound(std::unique_ptr<Asset> res);
+
+private:
+ void load(std::unique_ptr<Asset> res);
private:
SoLoud::Wav sample;
SoLoud::handle handle;
- SoundContext & context;
float volume = 1.0f;
bool looping = false;
diff --git a/src/crepe/facade/SoundContext.cpp b/src/crepe/facade/SoundContext.cpp
index b5f3db3..5e5a3a9 100644
--- a/src/crepe/facade/SoundContext.cpp
+++ b/src/crepe/facade/SoundContext.cpp
@@ -4,6 +4,11 @@
using namespace crepe;
+SoundContext & SoundContext::get_instance() {
+ static SoundContext instance;
+ return instance;
+}
+
SoundContext::SoundContext() {
dbg_trace();
engine.init();
diff --git a/src/crepe/facade/SoundContext.h b/src/crepe/facade/SoundContext.h
index 8d9e396..d3123d2 100644
--- a/src/crepe/facade/SoundContext.h
+++ b/src/crepe/facade/SoundContext.h
@@ -7,10 +7,12 @@
namespace crepe {
class SoundContext {
-public:
+private:
SoundContext();
virtual ~SoundContext();
+ // singleton
+ static SoundContext & get_instance();
SoundContext(const SoundContext &) = delete;
SoundContext(SoundContext &&) = delete;
SoundContext & operator=(const SoundContext &) = delete;
@@ -18,7 +20,6 @@ public:
private:
SoLoud::Soloud engine;
- //! Sound directly calls methods on \c engine
friend class Sound;
};