libSDL2pp/SDL2pp/AudioDevice.cc
Dmitry Marakasov 5abf3558be Move audio callback from AudioSpec into AudioDevice
Though this is not 100% compatible with SDL2, this makes API much
more consistent and less error prone. For example, you don't need
to store AudioSpec along with AudioDevice just to have your callback
lambda around, you don't need to copy AudioSpec from Wav file just
to fill in the callback (see wav demo), you are no more obliged to
take care of locking AudioDevice while replacing the callback.
2014-12-16 05:59:19 +03:00

121 lines
3.7 KiB
C++

/*
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/Audio.hh>
namespace SDL2pp {
void AudioDevice::SDLCallback(void *userdata, Uint8* stream, int len) {
AudioDevice* audiodevice = static_cast<AudioDevice*>(userdata);
audiodevice->callback_(stream, len);
}
AudioDevice::AudioDevice(const std::string& device, bool iscapture, const AudioSpec& spec, AudioDevice::AudioCallback&& callback) : callback_(std::move(callback)) {
SDL_AudioSpec spec_with_callback = *spec.Get();
if (callback_) {
spec_with_callback.callback = SDLCallback;
spec_with_callback.userdata = static_cast<void*>(this);
}
SDL_AudioSpec obtained;
if ((device_id_ = SDL_OpenAudioDevice(device.empty() ? nullptr : device.c_str(), iscapture ? 1 : 0, &spec_with_callback, &obtained, 0)) == 0)
throw Exception("SDL_OpenAudioDevice failed");
}
AudioDevice::AudioDevice(const std::string& device, bool iscapture, AudioSpec& spec, int allowed_changes, AudioDevice::AudioCallback&& callback) : callback_(std::move(callback)) {
SDL_AudioSpec spec_with_callback = *spec.Get();
if (callback_) {
spec_with_callback.callback = SDLCallback;
spec_with_callback.userdata = static_cast<void*>(this);
}
SDL_AudioSpec obtained;
if ((device_id_ = SDL_OpenAudioDevice(device.empty() ? nullptr : device.c_str(), iscapture ? 1 : 0, &spec_with_callback, &obtained, allowed_changes)) == 0)
throw Exception("SDL_OpenAudioDevice failed");
spec.MergeChanges(obtained);
}
AudioDevice::~AudioDevice() {
if (device_id_ != 0)
SDL_CloseAudioDevice(device_id_);
}
AudioDevice::AudioDevice(AudioDevice&& other) noexcept : device_id_(other.device_id_), callback_(std::move(other.callback_)) {
other.device_id_ = 0;
}
AudioDevice& AudioDevice::operator=(AudioDevice&& other) noexcept {
if (&other == this)
return *this;
if (device_id_)
SDL_CloseAudioDevice(device_id_);
device_id_ = other.device_id_;
callback_ = std::move(other.callback_);
other.device_id_ = 0;
return *this;
}
SDL_AudioDeviceID AudioDevice::Get() const {
return device_id_;
}
void AudioDevice::Pause(bool pause_on) {
SDL_PauseAudioDevice(device_id_, pause_on ? 1 : 0);
}
SDL_AudioStatus AudioDevice::GetStatus() const {
return SDL_GetAudioDeviceStatus(device_id_);
}
void AudioDevice::ChangeCallback(AudioDevice::AudioCallback&& callback) {
// make sure callback is not called while it's being replaced
LockHandle lock = Lock();
callback_ = std::move(callback);
}
AudioDevice::LockHandle AudioDevice::Lock() {
return LockHandle(this);
}
#ifdef SDL2PP_NEW_2_0_4
void AudioDevice::QueueAudio(const void* data, Uint32 len) {
if (SDL_QueueAudio(device_id_, data, len) == 0)
throw Exception("SDL_QueueAudio failed");
}
void AudioDevice::ClearQueuedAudio() {
SDL_ClearQueuedAudio(device_id_);
}
Uint32 GetQueuedAudioSize() const {
return SDL_GetQueuedAudioSize(device_id_);
}
#endif
}