aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--lib/soloud/CMakeLists.txt2
-rw-r--r--mwe/dynlink/exec/CMakeLists.txt10
-rw-r--r--mwe/dynlink/exec/main.c3
-rw-r--r--mwe/dynlink/lib/CMakeLists.txt16
-rw-r--r--mwe/dynlink/lib/test.c (renamed from mwe/dynlink/lib/lib.c)0
-rw-r--r--mwe/dynlink/lib/test.h (renamed from mwe/dynlink/lib/lib.h)0
-rw-r--r--src/CMakeLists.txt22
-rw-r--r--src/crepe/CMakeLists.txt14
-rw-r--r--src/crepe/Sound.cpp38
-rw-r--r--src/crepe/Sound.h69
-rw-r--r--src/crepe/SoundSystem.cpp21
-rw-r--r--src/crepe/SoundSystem.h27
-rw-r--r--src/crepe/api/AudioSource.cpp25
-rw-r--r--src/crepe/api/AudioSource.h42
-rw-r--r--src/crepe/api/CMakeLists.txt11
-rw-r--r--src/crepe/api/Component.h11
-rw-r--r--src/crepe/api/Resource.cpp19
-rw-r--r--src/crepe/api/Resource.h25
-rw-r--r--src/crepe/main.cpp3
-rw-r--r--src/crepe/util/CMakeLists.txt9
-rw-r--r--src/crepe/util/color.h42
-rw-r--r--src/crepe/util/log.cpp51
-rw-r--r--src/crepe/util/log.h33
-rw-r--r--src/dummy_audio.cpp41
-rw-r--r--test/CMakeLists.txt6
-rw-r--r--test/audio.cpp29
26 files changed, 557 insertions, 12 deletions
diff --git a/lib/soloud/CMakeLists.txt b/lib/soloud/CMakeLists.txt
index 8433281..aaccd96 100644
--- a/lib/soloud/CMakeLists.txt
+++ b/lib/soloud/CMakeLists.txt
@@ -9,7 +9,7 @@ add_subdirectory(../sdl2 sdl2)
project(soloud C CXX)
-add_library(soloud STATIC
+add_library(soloud SHARED
src/src/audiosource/ay/chipplayer.cpp
src/src/audiosource/ay/sndbuffer.cpp
src/src/audiosource/ay/sndchip.cpp
diff --git a/mwe/dynlink/exec/CMakeLists.txt b/mwe/dynlink/exec/CMakeLists.txt
index 5335f0f..465b45e 100644
--- a/mwe/dynlink/exec/CMakeLists.txt
+++ b/mwe/dynlink/exec/CMakeLists.txt
@@ -5,12 +5,20 @@ set(CMAKE_CXX_STANDARD 20)
project(main C CXX)
+add_executable(main main.c)
+
# Since we have the source code for the test library, we might as well let
# CMake use it so it automatically picks up the interface headers and compiles
# the library for us. The same can be achieved manually, but is more code.
add_subdirectory(../lib test)
-add_executable(main main.c)
+# include(ExternalProject)
+# ExternalProject_Add(test_ext
+# SOURCE_DIR ../../lib
+# CMAKE_ARGS -DCMAKE_INSTALL_PREFIX="${CMAKE_BINARY_DIR}/ext"
+# )
+# add_dependencies(main test_ext)
+# find_package(test REQUIRED)
# Make sure ld.so (linux) looks in the same folder as the final executable for
# the .so dependency
diff --git a/mwe/dynlink/exec/main.c b/mwe/dynlink/exec/main.c
index 2bbdc20..ea55feb 100644
--- a/mwe/dynlink/exec/main.c
+++ b/mwe/dynlink/exec/main.c
@@ -1,7 +1,8 @@
-#include <lib.h>
+#include <test.h>
int main() {
library_function();
return 0;
}
+
diff --git a/mwe/dynlink/lib/CMakeLists.txt b/mwe/dynlink/lib/CMakeLists.txt
index 2d15353..7efc91a 100644
--- a/mwe/dynlink/lib/CMakeLists.txt
+++ b/mwe/dynlink/lib/CMakeLists.txt
@@ -5,6 +5,20 @@ set(CMAKE_CXX_STANDARD 20)
project(lib C CXX)
-add_library(test SHARED lib.c)
+add_library(test SHARED)
+
target_include_directories(test SYSTEM INTERFACE .)
+target_sources(test PUBLIC
+ test.c
+)
+
+target_sources(test PUBLIC FILE_SET HEADERS FILES
+ test.h
+)
+
+install(
+ TARGETS test
+ FILE_SET HEADERS DESTINATION include
+)
+
diff --git a/mwe/dynlink/lib/lib.c b/mwe/dynlink/lib/test.c
index c7a78e4..c7a78e4 100644
--- a/mwe/dynlink/lib/lib.c
+++ b/mwe/dynlink/lib/test.c
diff --git a/mwe/dynlink/lib/lib.h b/mwe/dynlink/lib/test.h
index 093eadb..093eadb 100644
--- a/mwe/dynlink/lib/lib.h
+++ b/mwe/dynlink/lib/test.h
diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt
index 0090188..232d330 100644
--- a/src/CMakeLists.txt
+++ b/src/CMakeLists.txt
@@ -8,9 +8,29 @@ set(CMAKE_EXPORT_COMPILE_COMMANDS 1)
set(CMAKE_BUILD_TYPE Debug)
add_compile_definitions(DEBUG)
+add_subdirectory(../lib/soloud soloud)
+
project(crepe C CXX)
-add_executable(main)
+add_library(crepe SHARED)
+
+target_include_directories(crepe
+ PUBLIC SYSTEM INTERFACE .
+)
+
+# TODO: libraries should be linked as PRIVATE
+target_link_libraries(crepe
+ PUBLIC soloud
+)
add_subdirectory(crepe)
+install(
+ TARGETS crepe
+ FILE_SET HEADERS DESTINATION include/crepe
+)
+
+
+add_executable(dummy_audio dummy_audio.cpp)
+target_link_libraries(dummy_audio PUBLIC crepe)
+
diff --git a/src/crepe/CMakeLists.txt b/src/crepe/CMakeLists.txt
index 392b7d7..9f7c91c 100644
--- a/src/crepe/CMakeLists.txt
+++ b/src/crepe/CMakeLists.txt
@@ -1,3 +1,13 @@
-target_sources(main PUBLIC
- main.cpp
+target_sources(crepe PUBLIC
+ Sound.cpp
+ SoundSystem.cpp
)
+
+target_sources(crepe PUBLIC FILE_SET HEADERS FILES
+ Sound.h
+ SoundSystem.h
+)
+
+add_subdirectory(api)
+add_subdirectory(util)
+
diff --git a/src/crepe/Sound.cpp b/src/crepe/Sound.cpp
new file mode 100644
index 0000000..09ffd5f
--- /dev/null
+++ b/src/crepe/Sound.cpp
@@ -0,0 +1,38 @@
+#include "util/log.h"
+
+#include "Sound.h"
+#include "SoundSystem.h"
+
+using namespace crepe;
+
+Sound::Sound(std::unique_ptr<api::Resource> res) {
+ dbg_trace();
+ this->load(std::move(res));
+}
+
+Sound::Sound(const char * src) {
+ dbg_trace();
+ this->load(std::make_unique<api::Resource>(src));
+}
+
+void Sound::load(std::unique_ptr<api::Resource> res) {
+ this->sample.load(this->res->canonical());
+}
+
+void Sound::play() {
+ SoundSystem & system = SoundSystem::instance();
+ if (system.engine.getPause(this->handle)) {
+ // resume if paused
+ system.engine.setPause(this->handle, false);
+ } else {
+ // or start new sound
+ this->handle = system.engine.play(this->sample);
+ }
+}
+
+void Sound::pause() {
+ SoundSystem & system = SoundSystem::instance();
+ if (system.engine.getPause(this->handle)) return;
+ system.engine.setPause(this->handle, true);
+}
+
diff --git a/src/crepe/Sound.h b/src/crepe/Sound.h
new file mode 100644
index 0000000..163c5b4
--- /dev/null
+++ b/src/crepe/Sound.h
@@ -0,0 +1,69 @@
+#pragma once
+
+#include <soloud.h>
+#include <soloud_wav.h>
+
+#include <memory>
+
+#include "api/Resource.h"
+
+namespace crepe {
+
+class Sound {
+public:
+ /**
+ * \brief Pause this sample
+ *
+ * Pauses this sound if it is playing, or does nothing if it is already
+ * paused. The playhead position is saved, such that calling \c play() after
+ * this function makes the sound resume.
+ */
+ void pause();
+ /**
+ * \brief Play this sample
+ *
+ * Resume playback if this sound is paused, or start from the beginning of
+ * the sample.
+ *
+ * \note This class only saves a reference to the most recent 'voice' of this
+ * sound. Calling \c play() while the sound is already playing causes
+ * multiple instances of the sample to play simultaniously. The sample
+ * started last is the one that is controlled afterwards.
+ */
+ void play();
+ /**
+ * \brief Reset playhead position
+ *
+ * Resets the playhead position so that calling \c play() after this function
+ * makes it play from the start of the sample. If the sound is not paused
+ * before calling this function, this function will stop playback.
+ */
+ void rewind();
+ /**
+ * \brief Set playback volume / gain
+ *
+ * \param volume Volume (0 = muted, 1 = full volume)
+ */
+ void set_volume(float volume);
+ /**
+ * \brief Set looping behavior for this sample
+ *
+ * \param looping Looping behavior (false = one-shot, true = loop)
+ */
+ void set_looping(bool looping);
+
+public:
+ Sound(const char * src);
+ Sound(std::unique_ptr<api::Resource> res);
+
+private:
+ void load(std::unique_ptr<api::Resource> res);
+
+private:
+ std::unique_ptr<api::Resource> res;
+ SoLoud::Wav sample;
+ SoLoud::handle handle;
+};
+
+}
+
diff --git a/src/crepe/SoundSystem.cpp b/src/crepe/SoundSystem.cpp
new file mode 100644
index 0000000..00f874c
--- /dev/null
+++ b/src/crepe/SoundSystem.cpp
@@ -0,0 +1,21 @@
+#include "util/log.h"
+
+#include "SoundSystem.h"
+
+using namespace crepe;
+
+SoundSystem & SoundSystem::instance() {
+ static SoundSystem instance;
+ return instance;
+}
+
+SoundSystem::SoundSystem() {
+ dbg_trace();
+ engine.init();
+}
+
+SoundSystem::~SoundSystem() {
+ dbg_trace();
+ engine.deinit();
+}
+
diff --git a/src/crepe/SoundSystem.h b/src/crepe/SoundSystem.h
new file mode 100644
index 0000000..da3927a
--- /dev/null
+++ b/src/crepe/SoundSystem.h
@@ -0,0 +1,27 @@
+#pragma once
+
+#include <soloud.h>
+
+#include "Sound.h"
+
+namespace crepe {
+
+class SoundSystem {
+private:
+ SoundSystem();
+ virtual ~SoundSystem();
+
+ // singleton
+ static SoundSystem & instance();
+ SoundSystem(const SoundSystem &) = delete;
+ SoundSystem(SoundSystem &&) = delete;
+ SoundSystem &operator=(const SoundSystem &) = delete;
+ SoundSystem &operator=(SoundSystem &&) = delete;
+
+private:
+ SoLoud::Soloud engine;
+ friend class Sound;
+};
+
+}
+
diff --git a/src/crepe/api/AudioSource.cpp b/src/crepe/api/AudioSource.cpp
new file mode 100644
index 0000000..cbde79f
--- /dev/null
+++ b/src/crepe/api/AudioSource.cpp
@@ -0,0 +1,25 @@
+#include "AudioSource.h"
+
+#include "../Sound.h"
+#include <memory>
+
+using namespace crepe::api;
+
+AudioSource::AudioSource(std::unique_ptr<Resource> audio_clip) {
+ this->_sound = std::make_unique<crepe::Sound>(std::move(audio_clip));
+}
+
+void AudioSource::play() {
+ return this->play(false);
+}
+
+void AudioSource::play(bool looping) {
+ this->_sound->set_looping(looping);
+ this->_sound->play();
+}
+
+void AudioSource::stop() {
+ this->_sound->pause();
+ this->_sound->rewind();
+}
+
diff --git a/src/crepe/api/AudioSource.h b/src/crepe/api/AudioSource.h
new file mode 100644
index 0000000..6a038be
--- /dev/null
+++ b/src/crepe/api/AudioSource.h
@@ -0,0 +1,42 @@
+#pragma once
+
+#include <memory>
+
+#include "Component.h"
+#include "Resource.h"
+
+namespace crepe {
+class Sound;
+}
+
+namespace crepe::api {
+
+//! Audio source component
+class AudioSource : Component {
+public:
+ AudioSource(std::unique_ptr<Resource> audio_clip);
+ virtual ~AudioSource() = default;
+
+public:
+ //! Start or resume this audio source
+ void play();
+ void play(bool looping);
+ //! Stop this audio source
+ void stop();
+
+public:
+ //! Sample file location
+ std::unique_ptr<Resource> audio_clip;
+ //! TODO: ?????
+ bool play_on_awake;
+ //! Repeat the current audio clip during playback
+ bool loop;
+ //! Normalized volume (0.0 - 1.0)
+ float volume;
+
+private:
+ std::unique_ptr<crepe::Sound> _sound;
+};
+
+}
+
diff --git a/src/crepe/api/CMakeLists.txt b/src/crepe/api/CMakeLists.txt
new file mode 100644
index 0000000..feb03ef
--- /dev/null
+++ b/src/crepe/api/CMakeLists.txt
@@ -0,0 +1,11 @@
+target_sources(crepe PUBLIC
+ # AudioSource.cpp
+ Resource.cpp
+)
+
+target_sources(crepe PUBLIC FILE_SET HEADERS FILES
+ AudioSource.h
+ Component.h
+ Resource.h
+)
+
diff --git a/src/crepe/api/Component.h b/src/crepe/api/Component.h
new file mode 100644
index 0000000..2abb461
--- /dev/null
+++ b/src/crepe/api/Component.h
@@ -0,0 +1,11 @@
+#pragma once
+
+namespace crepe::api {
+
+class Component {
+public:
+ bool active;
+};
+
+}
+
diff --git a/src/crepe/api/Resource.cpp b/src/crepe/api/Resource.cpp
new file mode 100644
index 0000000..6bb081d
--- /dev/null
+++ b/src/crepe/api/Resource.cpp
@@ -0,0 +1,19 @@
+#include <filesystem>
+
+#include "Resource.h"
+
+using namespace crepe::api;
+
+Resource::Resource(const std::string & src) {
+ this->src = std::filesystem::canonical(src);
+ this->file = std::ifstream(this->src, std::ios::in | std::ios::binary);
+}
+
+const std::istream & Resource::read() {
+ return this->file;
+}
+
+const char * Resource::canonical() {
+ return this->src.c_str();
+}
+
diff --git a/src/crepe/api/Resource.h b/src/crepe/api/Resource.h
new file mode 100644
index 0000000..2b62ff9
--- /dev/null
+++ b/src/crepe/api/Resource.h
@@ -0,0 +1,25 @@
+#pragma once
+
+#include <string>
+#include <fstream>
+#include <iostream>
+
+namespace crepe::api {
+
+class Resource {
+public:
+ Resource(const std::string & src);
+
+public:
+ //! Get an input stream to the contents of this resource
+ const std::istream & read();
+ //! Get the canonical path to this resource
+ const char * canonical();
+
+private:
+ std::string src;
+ std::ifstream file;
+};
+
+}
+
diff --git a/src/crepe/main.cpp b/src/crepe/main.cpp
deleted file mode 100644
index 8e9a184..0000000
--- a/src/crepe/main.cpp
+++ /dev/null
@@ -1,3 +0,0 @@
-#include <stdio.h>
-
-int main() { printf("Hello World!\n"); }
diff --git a/src/crepe/util/CMakeLists.txt b/src/crepe/util/CMakeLists.txt
new file mode 100644
index 0000000..100f028
--- /dev/null
+++ b/src/crepe/util/CMakeLists.txt
@@ -0,0 +1,9 @@
+target_sources(crepe PUBLIC
+ log.cpp
+)
+
+target_sources(crepe PUBLIC FILE_SET HEADERS FILES
+ color.h
+ log.h
+)
+
diff --git a/src/crepe/util/color.h b/src/crepe/util/color.h
new file mode 100644
index 0000000..1af6c8f
--- /dev/null
+++ b/src/crepe/util/color.h
@@ -0,0 +1,42 @@
+#pragma once
+
+namespace crepe::util::color {
+
+constexpr const char * RESET = "\e[0m";
+
+constexpr const char * FG_BLACK = "\e[30m";
+constexpr const char * FG_RED = "\e[31m";
+constexpr const char * FG_GREEN = "\e[32m";
+constexpr const char * FG_YELLOW = "\e[33m";
+constexpr const char * FG_BLUE = "\e[34m";
+constexpr const char * FG_MAGENTA = "\e[35m";
+constexpr const char * FG_CYAN = "\e[36m";
+constexpr const char * FG_WHITE = "\e[37m";
+constexpr const char * BG_BLACK = "\e[40m";
+constexpr const char * BG_RED = "\e[41m";
+constexpr const char * BG_GREEN = "\e[42m";
+constexpr const char * BG_YELLOW = "\e[43m";
+constexpr const char * BG_BLUE = "\e[44m";
+constexpr const char * BG_MAGENTA = "\e[45m";
+constexpr const char * BG_CYAN = "\e[46m";
+constexpr const char * BG_WHITE = "\e[47m";
+
+constexpr const char * FG_BLACK_BRIGHT = "\e[90m";
+constexpr const char * FG_RED_BRIGHT = "\e[91m";
+constexpr const char * FG_GREEN_BRIGHT = "\e[92m";
+constexpr const char * FG_YELLOW_BRIGHT = "\e[93m";
+constexpr const char * FG_BLUE_BRIGHT = "\e[94m";
+constexpr const char * FG_MAGENTA_BRIGHT = "\e[95m";
+constexpr const char * FG_CYAN_BRIGHT = "\e[96m";
+constexpr const char * FG_WHITE_BRIGHT = "\e[97m";
+constexpr const char * BG_BLACK_BRIGHT = "\e[100m";
+constexpr const char * BG_RED_BRIGHT = "\e[101m";
+constexpr const char * BG_GREEN_BRIGHT = "\e[102m";
+constexpr const char * BG_YELLOW_BRIGHT = "\e[103m";
+constexpr const char * BG_BLUE_BRIGHT = "\e[104m";
+constexpr const char * BG_MAGENTA_BRIGHT = "\e[105m";
+constexpr const char * BG_CYAN_BRIGHT = "\e[106m";
+constexpr const char * BG_WHITE_BRIGHT = "\e[107m";
+
+}
+
diff --git a/src/crepe/util/log.cpp b/src/crepe/util/log.cpp
new file mode 100644
index 0000000..796df49
--- /dev/null
+++ b/src/crepe/util/log.cpp
@@ -0,0 +1,51 @@
+#include <cstdarg>
+#include <cstdio>
+#include <cstdlib>
+#include <string>
+
+#include "log.h"
+
+using namespace crepe::util;
+
+static const char * const LOG_PREFIX[] = {
+ [log_level::debug] = "[DBG] ",
+ [log_level::info] = "[INFO] ",
+ [log_level::warning] = "[WARN] ",
+ [log_level::error] = "[ERR] ",
+};
+
+static void va_logf(enum log_level level, va_list args, const std::string fmt) {
+ va_list args_copy;
+ va_copy(args_copy, args);
+
+ // prepend log level and ensure newline
+ std::string format_fixed = LOG_PREFIX[level] + fmt;
+ if (!format_fixed.ends_with("\n")) format_fixed += "\n";
+
+ size_t sz = vsnprintf(NULL, 0, format_fixed.c_str(), args_copy) + 1;
+ char * msg = (char *) malloc(sz);
+ va_end(args_copy);
+
+ vsnprintf(msg, sz, format_fixed.c_str(), args);
+
+ // TODO: also log to file or smth
+ printf("%s", msg);
+ fflush(stdout);
+
+ free(msg);
+}
+
+void crepe::util::logf(const char * fmt, ...) {
+ va_list args;
+ va_start(args, fmt);
+ va_logf(crepe::util::log_level::debug, args, fmt);
+ va_end(args);
+}
+
+void crepe::util::logf(log_level level, const char * fmt, ...) {
+ va_list args;
+ va_start(args, fmt);
+ va_logf(level, args, fmt);
+ va_end(args);
+}
+
diff --git a/src/crepe/util/log.h b/src/crepe/util/log.h
new file mode 100644
index 0000000..5295cb9
--- /dev/null
+++ b/src/crepe/util/log.h
@@ -0,0 +1,33 @@
+#pragma once
+
+// allow user to disable debug macros
+#ifndef CREPE_DISABLE_MACROS
+
+#include "color.h"
+
+// utility macros
+#define _crepe_logf_here(fmt, ...) \
+ crepe::util::logf(util::log_level::debug, "%s%s (%s:%d)" fmt "\n", \
+ crepe::util::color::FG_WHITE, \
+ __PRETTY_FUNCTION__, \
+ __FILE_NAME__, \
+ __LINE__, \
+ crepe::util::color::RESET, \
+ __VA_ARGS__)
+
+#define dbg_logf(fmt, ...) _crepe_logf_here(": " fmt, __VA_ARGS__)
+#define dbg_log(str) _crepe_logf_here(": %s", str)
+#define dbg_trace() _crepe_logf_here("%s", "")
+
+#endif
+
+namespace crepe::util {
+
+enum log_level { debug, info, warning, error, };
+
+void logf(const char * fmt, ...);
+void logf(enum log_level level, const char * fmt, ...);
+
+}
+
+
diff --git a/src/dummy_audio.cpp b/src/dummy_audio.cpp
new file mode 100644
index 0000000..1249076
--- /dev/null
+++ b/src/dummy_audio.cpp
@@ -0,0 +1,41 @@
+#include "crepe/util/log.h"
+#include "crepe/Sound.h"
+
+#include <chrono>
+#include <thread>
+
+using namespace crepe;
+using namespace std;
+using namespace std::chrono_literals;
+using std::make_unique;
+
+int main() {
+ dbg_trace();
+
+ auto bgm = Sound("../mwe/audio/bgm.ogg");
+ auto sfx1 = Sound("../mwe/audio/sfx1.wav");
+ auto sfx2 = Sound("../mwe/audio/sfx2.wav");
+ auto sfx3 = Sound("../mwe/audio/sfx3.wav");
+
+ bgm.play();
+
+ // play each sample sequentially
+ this_thread::sleep_for(500ms);
+ sfx1.play();
+ this_thread::sleep_for(500ms);
+ sfx2.play();
+ bgm.pause();
+ this_thread::sleep_for(500ms);
+ sfx3.play();
+ bgm.play();
+ this_thread::sleep_for(500ms);
+
+ // play all samples simultaniously
+ sfx1.play();
+ sfx2.play();
+ sfx3.play();
+ this_thread::sleep_for(1000ms);
+
+ return 0;
+}
+
diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt
index 26aa460..f015570 100644
--- a/test/CMakeLists.txt
+++ b/test/CMakeLists.txt
@@ -9,13 +9,15 @@ set(CMAKE_BUILD_TYPE Debug)
project(test C CXX)
add_subdirectory(../lib/googletest googletest)
+add_subdirectory(../src crepe)
add_executable(test
dummy.cpp
+ # audio.cpp
)
target_link_libraries(test
- gtest_main
- # TODO: add crepe engine
+ PRIVATE gtest_main
+ PUBLIC crepe # TODO: this does not work properly
)
diff --git a/test/audio.cpp b/test/audio.cpp
new file mode 100644
index 0000000..a415919
--- /dev/null
+++ b/test/audio.cpp
@@ -0,0 +1,29 @@
+#include <gtest/gtest.h>
+#include <memory>
+
+#include <crepe/api/Resource.h>
+#include <crepe/api/AudioSource.h>
+
+#include <chrono>
+#include <thread>
+
+using namespace std;
+using namespace std::chrono_literals;
+
+using namespace crepe::api;
+
+// TODO: mock internal audio class
+
+TEST(audio, play) {
+ auto res = std::make_unique<Resource>("../mwe/audio/bgm.ogg");
+ auto bgm = AudioSource(std::move(res));
+
+ bgm.play();
+
+ this_thread::sleep_for(2s);
+
+ bgm.stop();
+
+ ASSERT_TRUE(true);
+}
+