From 2aa61877b62b1fe53829b911f330396109122827 Mon Sep 17 00:00:00 2001 From: Rodrigo Delduca Date: Sun, 29 Sep 2024 14:53:12 -0300 Subject: [PATCH] Work in progress --- src/application.cpp | 6 +++--- src/audiodevice.cpp | 17 ----------------- src/soundfx.cpp | 34 +++++++++++++++++++++------------- src/soundfx.hpp | 8 +++++++- 4 files changed, 31 insertions(+), 34 deletions(-) diff --git a/src/application.cpp b/src/application.cpp index a87dab6..3b51c3b 100644 --- a/src/application.cpp +++ b/src/application.cpp @@ -16,9 +16,9 @@ application::application(int argc, char **argv) { UNUSED(argc); UNUSED(argv); - SDL_Init(SDL_INIT_AUDIO | SDL_INIT_GAMECONTROLLER | SDL_INIT_VIDEO); - SDL_EventState(SDL_CONTROLLERDEVICEADDED, SDL_ENABLE); - SDL_EventState(SDL_CONTROLLERDEVICEREMOVED, SDL_ENABLE); + SDL_Init(SDL_INIT_GAMECONTROLLER | SDL_INIT_VIDEO); + // SDL_EventState(SDL_CONTROLLERDEVICEADDED, SDL_ENABLE); + // SDL_EventState(SDL_CONTROLLERDEVICEREMOVED, SDL_ENABLE); PHYSFS_init(argv[0]); } diff --git a/src/audiodevice.cpp b/src/audiodevice.cpp index 31ac523..a41c9c9 100644 --- a/src/audiodevice.cpp +++ b/src/audiodevice.cpp @@ -24,19 +24,6 @@ audiodevice::audiodevice() : device(nullptr, [](ALCdevice *device) { } alcMakeContextCurrent(context.get()); - std::cout << "ALC device and context initialized successfully." << std::endl; - - // SDL_AudioSpec spec{}; - // spec.format = AUDIO_S16SYS; - // spec.freq = 44100; - // spec.channels = 2; - // spec.samples = 4096; - // _id = SDL_OpenAudioDevice(nullptr, 0, &spec, nullptr, 0); - - // if (_id == 0) { - // throw std::runtime_error( - // fmt::format("[SDL_OpenAudioDevice] error while opening audio device: {}", SDL_GetError())); - // } } audiodevice::~audiodevice() { @@ -45,8 +32,4 @@ audiodevice::~audiodevice() { alcMakeContextCurrent(nullptr); std::cout << "ALC context unset successfully." << std::endl; } - - /* SDL_CloseAudioDevice(0); */ } - -// uint32_t audiodevice::id() const { return _id; } diff --git a/src/soundfx.cpp b/src/soundfx.cpp index 0242c50..afc3cd3 100644 --- a/src/soundfx.cpp +++ b/src/soundfx.cpp @@ -4,19 +4,19 @@ using namespace audio; -static void callback(void *userdata, uint8_t *stream, int length) { - auto &buffer = static_cast(userdata)->buffer; +// static void callback(void *userdata, uint8_t *stream, int length) { +// auto &buffer = static_cast(userdata)->buffer; - if (buffer.empty()) { - SDL_memset(stream, 0, length); - return; - } +// if (buffer.empty()) { +// SDL_memset(stream, 0, length); +// return; +// } - const auto to_copy = std::min(static_cast(length), buffer.size()); - // SDL_memcpy(stream, buffer.data(), to_copy); - SDL_MixAudio(stream, buffer.data(), to_copy, SDL_MIX_MAXVOLUME); - buffer.erase(buffer.begin(), buffer.begin() + to_copy); -} +// const auto to_copy = std::min(static_cast(length), buffer.size()); +// // SDL_memcpy(stream, buffer.data(), to_copy); +// SDL_MixAudio(stream, buffer.data(), to_copy, SDL_MIX_MAXVOLUME); +// buffer.erase(buffer.begin(), buffer.begin() + to_copy); +// } static size_t ovPHYSFS_read(void *ptr, size_t size, size_t nmemb, void *source) { @@ -126,9 +126,17 @@ soundfx::soundfx(const std::shared_ptr audiodevice, } int32_t offset{0}; - const auto constexpr length = 1024 * 8; + const auto constexpr length = 1024 * 16; std::array array{0}; + const auto info = ov_info(vf.get(), -1); + if (!info) { + throw std::runtime_error(std::fmt("[ov_info] failed to retrieve OggVorbis info, error: {}", ov_strerror(info))); + } + + format = (info->channels == 1) ? AL_FORMAT_MONO16 : AL_FORMAT_STEREO16; + frequency = static_cast(info->rate); + #if SDL_BYTEORDER == SDL_LIL_ENDIAN const int bigendian = 0; #else @@ -145,7 +153,7 @@ soundfx::soundfx(const std::shared_ptr audiodevice, std::copy(array.begin(), array.begin() + offset, std::back_inserter(buffer)); } while (offset > 0); - UNUSED(callback); + // UNUSED(callback); } soundfx::~soundfx() {} diff --git a/src/soundfx.hpp b/src/soundfx.hpp index 35fc2d2..251806f 100644 --- a/src/soundfx.hpp +++ b/src/soundfx.hpp @@ -10,9 +10,15 @@ class soundfx { void play() const; - std::vector buffer; + // std::vector buffer; private: std::shared_ptr _audiodevice; + + // std::vector data; + ALuint buffer{}; + ALuint source{}; + ALenum format{}; + ALsizei frequency{}; }; }