Add stub Mixer class

This commit is contained in:
Dmitry Marakasov 2015-08-28 23:04:09 +03:00
parent 94f4a3d7aa
commit a25c84932c
5 changed files with 172 additions and 1 deletions

View File

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

51
SDL2pp/Mixer.cc Normal file
View File

@ -0,0 +1,51 @@
/*
libSDL2pp - C++11 bindings/wrapper for SDL2
Copyright (C) 2015 Dmitry Marakasov <amdmi3@amdmi3.ru>
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 <SDL2pp/Mixer.hh>
#include <SDL2pp/Exception.hh>
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;
}
}

115
SDL2pp/Mixer.hh Normal file
View File

@ -0,0 +1,115 @@
/*
libSDL2pp - C++11 bindings/wrapper for SDL2
Copyright (C) 2015 Dmitry Marakasov <amdmi3@amdmi3.ru>
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 <string>
#include <SDL2/SDL_mixer.h>
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

View File

@ -124,6 +124,7 @@
////////////////////////////////////////////////////////////
#ifdef SDL2PP_WITH_MIXER
# include <SDL2pp/Chunk.hh>
# include <SDL2pp/Mixer.hh>
# include <SDL2pp/SDLMixer.hh>
#endif

View File

@ -26,13 +26,15 @@
#include <SDL2pp/SDL.hh>
#include <SDL2pp/SDLMixer.hh>
#include <SDL2pp/Mixer.hh>
#include <SDL2pp/Chunk.hh>
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");