From 950829f2344efe892d3ce426bd4e920402aadeb8 Mon Sep 17 00:00:00 2001 From: Dmitry Marakasov Date: Sun, 30 Nov 2014 00:48:08 +0300 Subject: [PATCH] Add Wav class --- CMakeLists.txt | 2 ++ SDL2pp/SDL2pp.hh | 1 + SDL2pp/Wav.cc | 76 ++++++++++++++++++++++++++++++++++++++++++++ SDL2pp/Wav.hh | 57 +++++++++++++++++++++++++++++++++ tests/CMakeLists.txt | 1 + tests/header_wav.cc | 5 +++ 6 files changed, 142 insertions(+) create mode 100644 SDL2pp/Wav.cc create mode 100644 SDL2pp/Wav.hh create mode 100644 tests/header_wav.cc diff --git a/CMakeLists.txt b/CMakeLists.txt index 96f0c5a..f6da54a 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -60,6 +60,7 @@ SET(LIBRARY_SOURCES SDL2pp/SDL.cc SDL2pp/Texture.cc SDL2pp/TextureLock.cc + SDL2pp/Wav.cc SDL2pp/Window.cc ) @@ -74,6 +75,7 @@ SET(LIBRARY_HEADERS SDL2pp/SDL.hh SDL2pp/SDL2pp.hh SDL2pp/Texture.hh + SDL2pp/Wav.hh SDL2pp/Window.hh ) diff --git a/SDL2pp/SDL2pp.hh b/SDL2pp/SDL2pp.hh index 19c063e..8fa7c19 100644 --- a/SDL2pp/SDL2pp.hh +++ b/SDL2pp/SDL2pp.hh @@ -34,5 +34,6 @@ #include #include #include +#include #endif diff --git a/SDL2pp/Wav.cc b/SDL2pp/Wav.cc new file mode 100644 index 0000000..76b062b --- /dev/null +++ b/SDL2pp/Wav.cc @@ -0,0 +1,76 @@ +/* + libSDL2pp - C++ wrapper for libSDL2 + Copyright (C) 2014 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 + +#include + +namespace SDL2pp { + +Wav::Wav(const std::string& file) { + if (SDL_LoadWAV(file.c_str(), &spec_, &audio_buffer_, &audio_length_) == nullptr) + throw Exception("SDL_LoadWAV failed"); +} + +Wav::Wav(RWops& rwops) { + if (SDL_LoadWAV_RW(rwops.Get(), 0, &spec_, &audio_buffer_, &audio_length_) == nullptr) + throw Exception("SDL_LoadWAV failed"); +} + +Wav::~Wav() { + if (audio_buffer_ != nullptr) + SDL_FreeWAV(audio_buffer_); +} + +Wav::Wav(Wav&& other) : audio_buffer_(other.audio_buffer_), audio_length_(other.audio_length_), spec_(std::move(other.spec_)) { + other.audio_buffer_ = nullptr; + other.audio_length_ = 0; +} + +Wav& Wav::operator=(Wav&& other) { + spec_ = std::move(other.spec_); + audio_buffer_ = other.audio_buffer_; + audio_length_ = other.audio_length_; + + other.audio_buffer_ = nullptr; + other.audio_length_ = 0; + + return *this; +} + +Uint32 Wav::GetLength() const { + return audio_length_; +} + +Uint8* Wav::GetBuffer() { + return audio_buffer_; +} + +const Uint8* Wav::GetBuffer() const { + return audio_buffer_; +} + +const AudioSpec& Wav::GetSpec() const { + return spec_; +} + +} diff --git a/SDL2pp/Wav.hh b/SDL2pp/Wav.hh new file mode 100644 index 0000000..51638fc --- /dev/null +++ b/SDL2pp/Wav.hh @@ -0,0 +1,57 @@ +/* + libSDL2pp - C++ wrapper for libSDL2 + Copyright (C) 2014 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_WAV_HH +#define SDL2PP_WAV_HH + +#include + +namespace SDL2pp { + +class RWops; + +class Wav { +private: + Uint8* audio_buffer_; + Uint32 audio_length_; + + AudioSpec spec_; + +public: + Wav(const std::string& file); + Wav(RWops& rwops); + ~Wav(); + + Wav(Wav&& other); + Wav& operator=(Wav&& other); + Wav(const Wav& other) = delete; + Wav& operator=(const Wav& other) = delete; + + Uint32 GetLength() const; + Uint8* GetBuffer(); + const Uint8* GetBuffer() const; + + const AudioSpec& GetSpec() const; +}; + +} + +#endif diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index c06bd0e..d2811b1 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -12,6 +12,7 @@ SET(HEADER_TESTS header_sdl header_sdl2pp header_texture + header_wav header_window ) diff --git a/tests/header_wav.cc b/tests/header_wav.cc new file mode 100644 index 0000000..4c997b9 --- /dev/null +++ b/tests/header_wav.cc @@ -0,0 +1,5 @@ +#include + +int main() { + return 0; +}