From 8ff441c6ff42252d1292ea8989d86106563c6c18 Mon Sep 17 00:00:00 2001 From: lisyarus Date: Mon, 21 Sep 2020 11:41:13 +0300 Subject: [PATCH] Support audio effects --- libs/audio/CMakeLists.txt | 2 +- libs/audio/source/engine.cpp | 24 +++++++++++++++++++----- 2 files changed, 20 insertions(+), 6 deletions(-) diff --git a/libs/audio/CMakeLists.txt b/libs/audio/CMakeLists.txt index 3689f27f..cdca7459 100644 --- a/libs/audio/CMakeLists.txt +++ b/libs/audio/CMakeLists.txt @@ -5,4 +5,4 @@ file(GLOB_RECURSE PSEMEK_AUDIO_SOURCES RELATIVE "${CMAKE_CURRENT_SOURCE_DIR}" "s add_library(psemek-audio ${PSEMEK_AUDIO_HEADERS} ${PSEMEK_AUDIO_SOURCES}) target_include_directories(psemek-audio PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}/include") -target_link_libraries(psemek-audio PUBLIC psemek-sdl2 psemek-log psemek-util SDL2_mixer) +target_link_libraries(psemek-audio PUBLIC psemek-sdl2 psemek-log SDL2_mixer) diff --git a/libs/audio/source/engine.cpp b/libs/audio/source/engine.cpp index 5c1579b4..b1258ffb 100644 --- a/libs/audio/source/engine.cpp +++ b/libs/audio/source/engine.cpp @@ -1,8 +1,6 @@ #include #include #include -#include -#include #include #include @@ -71,6 +69,8 @@ namespace psemek::audio std::shared_ptr sample; bool loop; + std::vector> effects; + stream_impl(int channel, std::shared_ptr sample, bool loop) : channel(channel) , sample(sample) @@ -85,13 +85,27 @@ namespace psemek::audio void push_effect(std::shared_ptr e) override { - unused(e); - util::not_implemented(); + effects.push_back(e); + Mix_RegisterEffect(channel, &effect_func, nullptr, e.get()); } void clear_effects() override { - util::not_implemented(); + effects.clear(); + Mix_UnregisterAllEffects(channel); + } + + static void effect_func(int, void * data, int len, void * udata) + { + auto e = reinterpret_cast(udata); + + if ((len % 2) != 0) + { + log::error() << "Cannot apply " << e->name() << " effect: number of bytes not even"; + return; + } + + (*e)(reinterpret_cast(data), len / 2); } void start() override