From 8a4d4b63751c13c86d74906e85fc401f437d2039 Mon Sep 17 00:00:00 2001 From: obligaron Date: Mon, 13 Dec 2021 22:04:20 +0100 Subject: [PATCH] Add OptionEntries for Audio --- Source/options.cpp | 34 +++++++++++++++++++++++++--------- Source/options.h | 8 ++++---- Source/sound.cpp | 4 ++-- Source/storm/storm_svid.cpp | 2 +- Source/utils/soundsample.cpp | 4 ++-- 5 files changed, 34 insertions(+), 18 deletions(-) diff --git a/Source/options.cpp b/Source/options.cpp index 4d4f963f5..0ae179dbe 100644 --- a/Source/options.cpp +++ b/Source/options.cpp @@ -252,6 +252,19 @@ void OptionSharewareChanged() gbIsSpawn = *sgOptions.StartUp.shareware; } +void OptionAudioChanged() +{ + effects_cleanup_sfx(); + music_stop(); + snd_deinit(); + snd_init(); + music_start(TMUSIC_INTRO); + if (gbRunGame) + sound_init(); + else + ui_sound_init(); +} + } // namespace void SetIniValue(const char *sectionName, const char *keyName, const char *value, int len) @@ -294,11 +307,6 @@ void LoadOptions() sgOptions.Audio.nSoundVolume = GetIniInt("Audio", "Sound Volume", VOLUME_MAX); sgOptions.Audio.nMusicVolume = GetIniInt("Audio", "Music Volume", VOLUME_MAX); - sgOptions.Audio.nSampleRate = GetIniInt("Audio", "Sample Rate", DEFAULT_AUDIO_SAMPLE_RATE); - sgOptions.Audio.nChannels = GetIniInt("Audio", "Channels", DEFAULT_AUDIO_CHANNELS); - sgOptions.Audio.nBufferSize = GetIniInt("Audio", "Buffer Size", DEFAULT_AUDIO_BUFFER_SIZE); - sgOptions.Audio.nResamplingQuality = GetIniInt("Audio", "Resampling Quality", DEFAULT_AUDIO_RESAMPLING_QUALITY); - sgOptions.Graphics.nGammaCorrection = GetIniInt("Graphics", "Gamma Correction", 100); #if SDL_VERSION_ATLEAST(2, 0, 0) sgOptions.Graphics.bHardwareCursor = GetIniBool("Graphics", "Hardware Cursor", HardwareCursorDefault()); @@ -348,10 +356,6 @@ void SaveOptions() SetIniValue("Audio", "Sound Volume", sgOptions.Audio.nSoundVolume); SetIniValue("Audio", "Music Volume", sgOptions.Audio.nMusicVolume); - SetIniValue("Audio", "Sample Rate", sgOptions.Audio.nSampleRate); - SetIniValue("Audio", "Channels", sgOptions.Audio.nChannels); - SetIniValue("Audio", "Buffer Size", sgOptions.Audio.nBufferSize); - SetIniValue("Audio", "Resampling Quality", sgOptions.Audio.nResamplingQuality); SetIniValue("Graphics", "Gamma Correction", sgOptions.Graphics.nGammaCorrection); #if SDL_VERSION_ATLEAST(2, 0, 0) SetIniValue("Graphics", "Hardware Cursor", sgOptions.Graphics.bHardwareCursor); @@ -605,7 +609,15 @@ AudioOptions::AudioOptions() , walkingSound("Walking Sound", OptionEntryFlags::None, N_("Walking Sound"), N_("Player emits sound when walking."), true) , autoEquipSound("Auto Equip Sound", OptionEntryFlags::None, N_("Auto Equip Sound"), N_("Automatically equipping items on pickup emits the equipment sound."), false) , itemPickupSound("Item Pickup Sound", OptionEntryFlags::None, N_("Item Pickup Sound"), N_("Picking up items emits the items pickup sound."), false) + , sampleRate("Sample Rate", OptionEntryFlags::CantChangeInGame, N_("Sample Rate"), N_("Output sample rate (Hz)."), DEFAULT_AUDIO_SAMPLE_RATE, { 22050, 44100, 48000 }) + , channels("Channels", OptionEntryFlags::CantChangeInGame, N_("Channels"), N_("Number of output channels."), DEFAULT_AUDIO_CHANNELS, { 1, 2 }) + , bufferSize("Buffer Size", OptionEntryFlags::CantChangeInGame, N_("Buffer Size"), N_("Buffer size (number of frames per channel)."), DEFAULT_AUDIO_BUFFER_SIZE, { 1024, 2048, 5120 }) + , resamplingQuality("Resampling Quality", OptionEntryFlags::CantChangeInGame, N_("Resampling Quality"), N_("Quality of the resampler, from 0 (lowest) to 10 (highest)."), DEFAULT_AUDIO_RESAMPLING_QUALITY, { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }) { + sampleRate.SetValueChangedCallback(OptionAudioChanged); + channels.SetValueChangedCallback(OptionAudioChanged); + bufferSize.SetValueChangedCallback(OptionAudioChanged); + resamplingQuality.SetValueChangedCallback(OptionAudioChanged); } std::vector AudioOptions::GetEntries() { @@ -613,6 +625,10 @@ std::vector AudioOptions::GetEntries() &walkingSound, &autoEquipSound, &itemPickupSound, + &sampleRate, + &channels, + &bufferSize, + &resamplingQuality, }; } diff --git a/Source/options.h b/Source/options.h index 99b89b717..af808363b 100644 --- a/Source/options.h +++ b/Source/options.h @@ -360,13 +360,13 @@ struct AudioOptions : OptionCategoryBase { OptionEntryBoolean itemPickupSound; /** @brief Output sample rate (Hz) */ - std::uint32_t nSampleRate; + OptionEntryInt sampleRate; /** @brief The number of output channels (1 or 2) */ - std::uint8_t nChannels; + OptionEntryInt channels; /** @brief Buffer size (number of frames per channel) */ - std::uint32_t nBufferSize; + OptionEntryInt bufferSize; /** @brief Quality of the resampler, from 0 (lowest) to 10 (highest) */ - std::uint8_t nResamplingQuality; + OptionEntryInt resamplingQuality; }; struct GraphicsOptions : OptionCategoryBase { diff --git a/Source/sound.cpp b/Source/sound.cpp index fe20b684c..17a77f595 100644 --- a/Source/sound.cpp +++ b/Source/sound.cpp @@ -56,7 +56,7 @@ void LoadMusic(SDL_RWops *handle) handle = SDL_RWFromConstMem(musicBuffer.get(), bytestoread); #endif music.emplace(handle, std::make_unique(), - std::make_unique(sgOptions.Audio.nResamplingQuality), /*closeRw=*/true); + std::make_unique(*sgOptions.Audio.resamplingQuality), /*closeRw=*/true); } void CleanupMusic() @@ -201,7 +201,7 @@ void snd_init() // Initialize the SDL_audiolib library. Set the output sample rate to // 22kHz, the audio format to 16-bit signed, use 2 output channels // (stereo), and a 2KiB output buffer. - if (!Aulib::init(sgOptions.Audio.nSampleRate, AUDIO_S16, sgOptions.Audio.nChannels, sgOptions.Audio.nBufferSize)) { + if (!Aulib::init(*sgOptions.Audio.sampleRate, AUDIO_S16, *sgOptions.Audio.channels, *sgOptions.Audio.bufferSize)) { LogError(LogCategory::Audio, "Failed to initialize audio (Aulib::init): {}", SDL_GetError()); return; } diff --git a/Source/storm/storm_svid.cpp b/Source/storm/storm_svid.cpp index 66bf5f136..77c170436 100644 --- a/Source/storm/storm_svid.cpp +++ b/Source/storm/storm_svid.cpp @@ -263,7 +263,7 @@ bool SVidPlayBegin(const char *filename, int flags) auto decoder = std::make_unique(audioInfo.nChannels, audioInfo.sampleRate); SVidAudioDecoder = decoder.get(); SVidAudioStream.emplace(/*rwops=*/nullptr, std::move(decoder), - std::make_unique(sgOptions.Audio.nResamplingQuality), /*closeRw=*/false); + std::make_unique(*sgOptions.Audio.resamplingQuality), /*closeRw=*/false); const float volume = static_cast(sgOptions.Audio.nSoundVolume - VOLUME_MIN) / -VOLUME_MIN; SVidAudioStream->setVolume(volume); if (!SVidAudioStream->open()) { diff --git a/Source/utils/soundsample.cpp b/Source/utils/soundsample.cpp index 034b88f45..50ea6450f 100644 --- a/Source/utils/soundsample.cpp +++ b/Source/utils/soundsample.cpp @@ -123,7 +123,7 @@ int SoundSample::SetChunkStream(std::string filePath) } stream_ = std::make_unique(handle, std::make_unique(), - std::make_unique(sgOptions.Audio.nResamplingQuality), /*closeRw=*/true); + std::make_unique(*sgOptions.Audio.resamplingQuality), /*closeRw=*/true); if (!stream_->open()) { stream_ = nullptr; LogError(LogCategory::Audio, "Aulib::Stream::open (from SoundSample::SetChunkStream): {}", SDL_GetError()); @@ -143,7 +143,7 @@ int SoundSample::SetChunk(ArraySharedPtr fileData, std::size_t dwB } stream_ = std::make_unique(buf, std::make_unique(), - std::make_unique(sgOptions.Audio.nResamplingQuality), /*closeRw=*/true); + std::make_unique(*sgOptions.Audio.resamplingQuality), /*closeRw=*/true); if (!stream_->open()) { stream_ = nullptr; file_data_ = nullptr;