Add Wav class

This commit is contained in:
Dmitry Marakasov 2014-11-30 00:48:08 +03:00
parent 646380520f
commit 950829f234
6 changed files with 142 additions and 0 deletions

View File

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

View File

@ -34,5 +34,6 @@
#include <SDL2pp/Point.hh>
#include <SDL2pp/RWops.hh>
#include <SDL2pp/ExtraRWops.hh>
#include <SDL2pp/Wav.hh>
#endif

76
SDL2pp/Wav.cc Normal file
View File

@ -0,0 +1,76 @@
/*
libSDL2pp - C++ wrapper for libSDL2
Copyright (C) 2014 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/Exception.hh>
#include <SDL2pp/RWops.hh>
#include <SDL2pp/Wav.hh>
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_;
}
}

57
SDL2pp/Wav.hh Normal file
View File

@ -0,0 +1,57 @@
/*
libSDL2pp - C++ wrapper for libSDL2
Copyright (C) 2014 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_WAV_HH
#define SDL2PP_WAV_HH
#include <SDL2pp/Audio.hh>
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

View File

@ -12,6 +12,7 @@ SET(HEADER_TESTS
header_sdl
header_sdl2pp
header_texture
header_wav
header_window
)

5
tests/header_wav.cc Normal file
View File

@ -0,0 +1,5 @@
#include <SDL2pp/Wav.hh>
int main() {
return 0;
}