Support audio effects

This commit is contained in:
Nikita Lisitsa 2020-09-21 11:41:13 +03:00
parent 6549c2f907
commit 8ff441c6ff
2 changed files with 20 additions and 6 deletions

View file

@ -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)

View file

@ -1,8 +1,6 @@
#include <psemek/audio/engine.hpp>
#include <psemek/sdl2/init.hpp>
#include <psemek/log/log.hpp>
#include <psemek/util/not_implemented.hpp>
#include <psemek/util/unused.hpp>
#include <SDL2/SDL.h>
#include <SDL2/SDL_mixer.h>
@ -71,6 +69,8 @@ namespace psemek::audio
std::shared_ptr<sample_impl> sample;
bool loop;
std::vector<std::shared_ptr<effect>> effects;
stream_impl(int channel, std::shared_ptr<sample_impl> sample, bool loop)
: channel(channel)
, sample(sample)
@ -85,13 +85,27 @@ namespace psemek::audio
void push_effect(std::shared_ptr<effect> 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<effect *>(udata);
if ((len % 2) != 0)
{
log::error() << "Cannot apply " << e->name() << " effect: number of bytes not even";
return;
}
(*e)(reinterpret_cast<std::int16_t *>(data), len / 2);
}
void start() override