From a25c84932c9b61731e029ff5d50a73166366abc3 Mon Sep 17 00:00:00 2001 From: Dmitry Marakasov Date: Fri, 28 Aug 2015 23:04:09 +0300 Subject: [PATCH] Add stub Mixer class --- CMakeLists.txt | 2 + SDL2pp/Mixer.cc | 51 ++++++++++++++++++++ SDL2pp/Mixer.hh | 115 ++++++++++++++++++++++++++++++++++++++++++++++ SDL2pp/SDL2pp.hh | 1 + examples/mixer.cc | 4 +- 5 files changed, 172 insertions(+), 1 deletion(-) create mode 100644 SDL2pp/Mixer.cc create mode 100644 SDL2pp/Mixer.hh diff --git a/CMakeLists.txt b/CMakeLists.txt index 1dbd83b..724c022 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -110,6 +110,7 @@ SET(LIBRARY_SOURCES SDL2pp/AudioSpec.cc SDL2pp/Chunk.cc SDL2pp/Exception.cc + SDL2pp/Mixer.cc SDL2pp/Point.cc SDL2pp/RWops.cc SDL2pp/Rect.cc @@ -130,6 +131,7 @@ SET(LIBRARY_HEADERS SDL2pp/Chunk.hh SDL2pp/ContainerRWops.hh SDL2pp/Exception.hh + SDL2pp/Mixer.hh SDL2pp/Optional.hh SDL2pp/Point.hh SDL2pp/RWops.hh diff --git a/SDL2pp/Mixer.cc b/SDL2pp/Mixer.cc new file mode 100644 index 0000000..32901ef --- /dev/null +++ b/SDL2pp/Mixer.cc @@ -0,0 +1,51 @@ +/* + libSDL2pp - C++11 bindings/wrapper for SDL2 + Copyright (C) 2015 Dmitry Marakasov + + This software is provided 'as-is', without any express or implied + warranty. In no event will the authors be held liable for any damages + arising from the use of this software. + + Permission is granted to anyone to use this software for any purpose, + including commercial applications, and to alter it and redistribute it + freely, subject to the following restrictions: + + 1. The origin of this software must not be misrepresented; you must not + claim that you wrote the original software. If you use this software + in a product, an acknowledgment in the product documentation would be + appreciated but is not required. + 2. Altered source versions must be plainly marked as such, and must not be + misrepresented as being the original software. + 3. This notice may not be removed or altered from any source distribution. +*/ + +#include +#include + +namespace SDL2pp { + +Mixer::Mixer(int frequency, Uint16 format, int channels, int chunksize) : open_(true) { + if (Mix_OpenAudio(frequency, format, channels, chunksize) != 0) + throw Exception("Mix_OpenAudio"); +} + +Mixer::~Mixer() { + if (open_) + Mix_CloseAudio(); +} + +Mixer::Mixer(Mixer&& other) noexcept : open_(other.open_) { + other.open_ = false; +} + +Mixer& Mixer::operator=(Mixer&& other) noexcept { + if (&other == this) + return *this; + if (open_) + Mix_CloseAudio(); + open_ = other.open_; + other.open_ = false; + return *this; +} + +} diff --git a/SDL2pp/Mixer.hh b/SDL2pp/Mixer.hh new file mode 100644 index 0000000..6b52c08 --- /dev/null +++ b/SDL2pp/Mixer.hh @@ -0,0 +1,115 @@ +/* + libSDL2pp - C++11 bindings/wrapper for SDL2 + Copyright (C) 2015 Dmitry Marakasov + + This software is provided 'as-is', without any express or implied + warranty. In no event will the authors be held liable for any damages + arising from the use of this software. + + Permission is granted to anyone to use this software for any purpose, + including commercial applications, and to alter it and redistribute it + freely, subject to the following restrictions: + + 1. The origin of this software must not be misrepresented; you must not + claim that you wrote the original software. If you use this software + in a product, an acknowledgment in the product documentation would be + appreciated but is not required. + 2. Altered source versions must be plainly marked as such, and must not be + misrepresented as being the original software. + 3. This notice may not be removed or altered from any source distribution. +*/ + +#ifndef SDL2PP_MIXER_HH +#define SDL2PP_MIXER_HH + +#include + +#include + +namespace SDL2pp { + +class RWops; + +//////////////////////////////////////////////////////////// +/// \brief SDL_mixer's audio mixer +/// +/// \ingroup mixer +/// +/// \headerfile SDL2pp/Mixer.hh +/// +/// This class represents open SDL_mixer audio device. Object +/// of this class must be constructed before creating any +/// SDL2pp:Chunk's. +/// +//////////////////////////////////////////////////////////// +class Mixer { +private: + bool open_; + +public: + //////////////////////////////////////////////////////////// + /// \brief Construct a mixer and open an audio device + /// + /// \param[in] frequency Output sampling frequency in samples + /// per second (Hz). You might use + /// MIX_DEFAULT_FREQUENCY(22050) since that + /// is a good value for most games + /// \param[in] format Output sample format + /// \param[in] channels Number of sound channels in output. Set + /// to 2 for stereo, 1 for mono. This has + /// nothing to do with mixing channels + /// \param[in] chunksize Bytes used per output sample + /// + /// \throws SDL2pp::Exception + /// + /// \see https://www.libsdl.org/projects/SDL_mixer/docs/SDL_mixer.html#SEC11 + /// + //////////////////////////////////////////////////////////// + Mixer(int frequency, Uint16 format, int channels, int chunksize); + + //////////////////////////////////////////////////////////// + /// \brief Destructor + /// + /// \see https://www.libsdl.org/projects/SDL_mixer/docs/SDL_mixer.html#SEC12 + /// + //////////////////////////////////////////////////////////// + ~Mixer(); + + //////////////////////////////////////////////////////////// + /// \brief Move constructor + /// + /// \param[in] other SDL2pp::Mixer object to move data from + /// + //////////////////////////////////////////////////////////// + Mixer(Mixer&& other) noexcept; + + //////////////////////////////////////////////////////////// + /// \brief Move assignment operator + /// + /// \param[in] other SDL2pp::Mixer object to move data from + /// + /// \returns Reference to self + /// + //////////////////////////////////////////////////////////// + Mixer& operator=(Mixer&& other) noexcept; + + //////////////////////////////////////////////////////////// + /// \brief Deleted copy constructor + /// + /// This class is not copyable + /// + //////////////////////////////////////////////////////////// + Mixer(const Mixer& other) = delete; + + //////////////////////////////////////////////////////////// + /// \brief Deleted assignment operator + /// + /// This class is not copyable + /// + //////////////////////////////////////////////////////////// + Mixer& operator=(const Mixer& other) = delete; +}; + +} + +#endif diff --git a/SDL2pp/SDL2pp.hh b/SDL2pp/SDL2pp.hh index 46c10b0..c24954a 100644 --- a/SDL2pp/SDL2pp.hh +++ b/SDL2pp/SDL2pp.hh @@ -124,6 +124,7 @@ //////////////////////////////////////////////////////////// #ifdef SDL2PP_WITH_MIXER # include +# include # include #endif diff --git a/examples/mixer.cc b/examples/mixer.cc index 2f06827..43ab0ba 100644 --- a/examples/mixer.cc +++ b/examples/mixer.cc @@ -26,13 +26,15 @@ #include #include +#include #include using namespace SDL2pp; int main() try { SDL sdl(SDL_INIT_AUDIO); - SDLMixer mixer(MIX_INIT_OGG); + SDLMixer mixerlib(MIX_INIT_OGG); + Mixer mixer(MIX_DEFAULT_FREQUENCY, MIX_DEFAULT_FORMAT, 2, 4096); // currently fails as audio device hasn't been opened Chunk chunk(TESTDATA_DIR "/test.ogg");