mirror of
https://github.com/libSDL2pp/libSDL2pp.git
synced 2025-08-03 10:55:57 -04:00
Switch AudioDevice to Optional
No compatibility here, as API is broken anyway (Optional<std::string> ctor won't accept const char*)
This commit is contained in:
parent
6d0213810b
commit
3796a6d246
@ -31,7 +31,7 @@ void AudioDevice::SDLCallback(void *userdata, Uint8* stream, int len) {
|
||||
audiodevice->callback_(stream, len);
|
||||
}
|
||||
|
||||
AudioDevice::AudioDevice(const std::string& device, bool iscapture, const AudioSpec& spec, AudioDevice::AudioCallback&& callback) {
|
||||
AudioDevice::AudioDevice(const Optional<std::string>& device, bool iscapture, const AudioSpec& spec, AudioDevice::AudioCallback&& callback) {
|
||||
SDL_AudioSpec spec_with_callback = *spec.Get();
|
||||
if (callback) {
|
||||
spec_with_callback.callback = SDLCallback;
|
||||
@ -39,13 +39,13 @@ AudioDevice::AudioDevice(const std::string& device, bool iscapture, const AudioS
|
||||
}
|
||||
SDL_AudioSpec obtained;
|
||||
|
||||
if ((device_id_ = SDL_OpenAudioDevice(device.empty() ? nullptr : device.c_str(), iscapture ? 1 : 0, &spec_with_callback, &obtained, 0)) == 0)
|
||||
if ((device_id_ = SDL_OpenAudioDevice(device ? device->c_str() : nullptr, iscapture ? 1 : 0, &spec_with_callback, &obtained, 0)) == 0)
|
||||
throw Exception("SDL_OpenAudioDevice failed");
|
||||
|
||||
callback_ = std::move(callback);
|
||||
}
|
||||
|
||||
AudioDevice::AudioDevice(const std::string& device, bool iscapture, AudioSpec& spec, int allowed_changes, AudioDevice::AudioCallback&& callback) {
|
||||
AudioDevice::AudioDevice(const Optional<std::string>& device, bool iscapture, AudioSpec& spec, int allowed_changes, AudioDevice::AudioCallback&& callback) {
|
||||
SDL_AudioSpec spec_with_callback = *spec.Get();
|
||||
if (callback) {
|
||||
spec_with_callback.callback = SDLCallback;
|
||||
@ -53,7 +53,7 @@ AudioDevice::AudioDevice(const std::string& device, bool iscapture, AudioSpec& s
|
||||
}
|
||||
SDL_AudioSpec obtained;
|
||||
|
||||
if ((device_id_ = SDL_OpenAudioDevice(device.empty() ? nullptr : device.c_str(), iscapture ? 1 : 0, &spec_with_callback, &obtained, allowed_changes)) == 0)
|
||||
if ((device_id_ = SDL_OpenAudioDevice(device ? device->c_str() : nullptr, iscapture ? 1 : 0, &spec_with_callback, &obtained, allowed_changes)) == 0)
|
||||
throw Exception("SDL_OpenAudioDevice failed");
|
||||
|
||||
spec.MergeChanges(obtained);
|
||||
|
@ -27,6 +27,7 @@
|
||||
|
||||
#include <SDL2/SDL_audio.h>
|
||||
|
||||
#include <SDL2pp/Optional.hh>
|
||||
#include <SDL2pp/Config.hh>
|
||||
|
||||
namespace SDL2pp {
|
||||
@ -64,8 +65,8 @@ private:
|
||||
static void SDLCallback(void *userdata, Uint8* stream, int len);
|
||||
|
||||
public:
|
||||
AudioDevice(const std::string& device, bool iscapture, const AudioSpec& spec, AudioCallback&& callback = AudioCallback());
|
||||
AudioDevice(const std::string& device, bool iscapture, AudioSpec& spec, int allowed_changes, AudioCallback&& callback = AudioCallback());
|
||||
AudioDevice(const Optional<std::string>& device, bool iscapture, const AudioSpec& spec, AudioCallback&& callback = AudioCallback());
|
||||
AudioDevice(const Optional<std::string>& device, bool iscapture, AudioSpec& spec, int allowed_changes, AudioCallback&& callback = AudioCallback());
|
||||
virtual ~AudioDevice();
|
||||
|
||||
AudioDevice(const AudioDevice& other) = delete;
|
||||
|
@ -41,7 +41,7 @@ int Run() {
|
||||
AudioSpec spec(samplerate, AUDIO_S16SYS, 1, 4096);
|
||||
|
||||
// Open audio device
|
||||
AudioDevice dev("", 0, spec, [&nsample, frequency, samplerate](Uint8* stream, int len) {
|
||||
AudioDevice dev(NullOpt, 0, spec, [&nsample, frequency, samplerate](Uint8* stream, int len) {
|
||||
// fill provided buffer with sine wave
|
||||
for (Uint8* ptr = stream; ptr < stream + len; ptr += 2)
|
||||
*(Uint16*)ptr = (Uint16)(32766.0f * sin(nsample++ / (float)samplerate * frequency));
|
||||
|
@ -39,7 +39,7 @@ int Run() {
|
||||
Uint8* wav_pos = wav.GetBuffer();
|
||||
|
||||
// Open audio device
|
||||
AudioDevice dev("", 0, wav.GetSpec(), [&wav, &wav_pos](Uint8* stream, int len) {
|
||||
AudioDevice dev(NullOpt, 0, wav.GetSpec(), [&wav, &wav_pos](Uint8* stream, int len) {
|
||||
// Fill provided buffer with wave contents
|
||||
Uint8* stream_pos = stream;
|
||||
Uint8* stream_end = stream + len;
|
||||
|
Loading…
x
Reference in New Issue
Block a user