diff --git a/src/audio/audio.cpp b/src/audio/audio.cpp index dd209812..8974d46a 100644 --- a/src/audio/audio.cpp +++ b/src/audio/audio.cpp @@ -1,7 +1,7 @@ // // Lol Engine // -// Copyright © 2010—2016 Sam Hocevar +// Copyright © 2010—2019 Sam Hocevar // // Lol Engine is free software. It comes without any warranty, to // the extent permitted by applicable law. You can redistribute it @@ -12,6 +12,9 @@ #include +#include +#include + #if LOL_USE_SDL_MIXER # if HAVE_SDL2_SDL_H # include @@ -30,99 +33,86 @@ struct audio_streamer int m_channel; std::function m_callback; #if defined LOL_USE_SDL_MIXER - array m_empty; // SDL keeps a reference to this + std::vector m_empty; // SDL keeps a reference to this Mix_Chunk *m_chunk; #endif }; -array g_streamers; +std::unordered_set> audio::m_streamers; /* * Public audio class */ +#if defined LOL_USE_SDL_MIXER void audio::init() { -#if defined LOL_USE_SDL_MIXER Mix_OpenAudio(22050, AUDIO_S16, 2, 1024); set_channels(8); -#endif } void audio::set_channels(int channels) { -#if defined LOL_USE_SDL_MIXER Mix_AllocateChannels(channels); -#else - UNUSED(channels); -#endif } void audio::set_volume(int channel, int volume) { -#if defined LOL_USE_SDL_MIXER Mix_Volume(channel, volume); -#else - UNUSED(channel, volume); -#endif } void audio::mute_all() { -#if defined LOL_USE_SDL_MIXER - Mix_Volume(-1,0); -#endif + Mix_Volume(-1, 0); } void audio::unmute_all() { -#if defined LOL_USE_SDL_MIXER - Mix_Volume(-1,MIX_MAX_VOLUME); -#endif + Mix_Volume(-1, MIX_MAX_VOLUME); } int audio::start_streaming(std::function const &f) { -#if defined LOL_USE_SDL_MIXER static auto trampoline = [](int, void *stream, int bytes, void *udata) { auto s = (audio_streamer *)udata; s->m_callback(stream, bytes); }; - audio_streamer *s = new audio_streamer(); - g_streamers.push(s); + std::shared_ptr s = std::make_shared(); + m_streamers.insert(s); s->m_empty.resize(1024); - s->m_chunk = Mix_QuickLoad_RAW(s->m_empty.data(), s->m_empty.bytes()); + s->m_chunk = Mix_QuickLoad_RAW(s->m_empty.data(), s->m_empty.size() * sizeof(uint8_t)); s->m_channel = Mix_PlayChannel(-1, s->m_chunk, -1); s->m_callback = f; - Mix_RegisterEffect(s->m_channel, trampoline, nullptr, s); + Mix_RegisterEffect(s->m_channel, trampoline, nullptr, s.get()); return s->m_channel; -#else - UNUSED(f); - return -1; -#endif } -void audio::stop_streaming(int channel) +void audio::stop_streaming(int stream) { -#if defined LOL_USE_SDL_MIXER - for (int i = 0; i < g_streamers.count(); ++i) + for (auto streamer : m_streamers) { - if (g_streamers[i]->m_channel == channel) + if (streamer->m_channel == stream) { - Mix_HaltChannel(channel); - delete g_streamers[i]; - g_streamers.remove(i); - return; + Mix_HaltChannel(stream); + m_streamers.erase(streamer); + break; } } +} + #else - UNUSED(channel); +void audio::init() {} +void audio::set_channels(int) {} +void audio::set_volume(int, int) {} +void audio::mute_all() {} +void audio::unmute_all() {} +int audio::start_streaming(std::function const &) { return -1; } +void audio::stop_streaming(int) {} #endif -} } /* namespace lol */ diff --git a/src/lol/audio/audio.h b/src/lol/audio/audio.h index d176f1fa..33a0f9be 100644 --- a/src/lol/audio/audio.h +++ b/src/lol/audio/audio.h @@ -18,6 +18,7 @@ // Helper functions to set up the audio device. // +#include #include namespace lol @@ -38,6 +39,7 @@ public: private: audio() {} + static std::unordered_set> m_streamers; }; } /* namespace lol */