From a2bda63ae56c51dea7b09f6bd3bd74a4541fce17 Mon Sep 17 00:00:00 2001 From: scawful Date: Sat, 20 Apr 2024 07:47:57 -0400 Subject: [PATCH] Add Controller::LoadAudioDevice and cleanup audio device OnExit --- src/app/core/controller.cc | 20 ++++++++++++++++++++ src/app/core/controller.h | 2 ++ 2 files changed, 22 insertions(+) diff --git a/src/app/core/controller.cc b/src/app/core/controller.cc index e9b28b25..bc40ef17 100644 --- a/src/app/core/controller.cc +++ b/src/app/core/controller.cc @@ -121,6 +121,7 @@ absl::Status Controller::OnEntry() { RETURN_IF_ERROR(CreateSDL_Window()) RETURN_IF_ERROR(CreateRenderer()) RETURN_IF_ERROR(CreateGuiContext()) + RETURN_IF_ERROR(LoadAudioDevice()) InitializeKeymap(); master_editor_.SetupScreen(renderer_); active_ = true; @@ -187,6 +188,8 @@ void Controller::DoRender() const { void Controller::OnExit() { master_editor_.Shutdown(); Mix_CloseAudio(); + SDL_PauseAudioDevice(audio_device_, 1); + SDL_CloseAudioDevice(audio_device_); ImGui_ImplSDLRenderer2_Shutdown(); ImGui_ImplSDL2_Shutdown(); ImGui::DestroyContext(); @@ -325,6 +328,23 @@ absl::Status Controller::LoadFontFamilies() const { return absl::OkStatus(); } +absl::Status Controller::LoadAudioDevice() { + SDL_AudioSpec want, have; + SDL_memset(&want, 0, sizeof(want)); + want.freq = audio_frequency_; + want.format = AUDIO_S16; + want.channels = 2; + want.samples = 2048; + want.callback = NULL; // Uses the queue + audio_device_ = SDL_OpenAudioDevice(NULL, 0, &want, &have, 0); + if (audio_device_ == 0) { + return absl::InternalError( + absl::StrFormat("Failed to open audio: %s\n", SDL_GetError())); + } + SDL_PauseAudioDevice(audio_device_, 0); + return absl::OkStatus(); +} + } // namespace core } // namespace app } // namespace yaze \ No newline at end of file diff --git a/src/app/core/controller.h b/src/app/core/controller.h index 7ee69ada..58973607 100644 --- a/src/app/core/controller.h +++ b/src/app/core/controller.h @@ -57,12 +57,14 @@ class Controller : public ExperimentFlags { absl::Status CreateRenderer(); absl::Status CreateGuiContext(); absl::Status LoadFontFamilies() const; + absl::Status LoadAudioDevice(); void CloseWindow() { active_ = false; } friend int ::main(int argc, char **argv); bool active_; int wanted_samples_; + int audio_frequency_ = 48000; int16_t *audio_buffer_; editor::MasterEditor master_editor_; SDL_AudioDeviceID audio_device_;