From 908fa1ee606b80eec47c6fc70ce018308ef06e98 Mon Sep 17 00:00:00 2001 From: Kae <80987908+Novaenia@users.noreply.github.com> Date: Sat, 3 Aug 2024 11:54:08 +1000 Subject: [PATCH] Add libsamplerate, make Voice bitrate configurable --- source/CMakeLists.txt | 3 +++ source/application/StarMainApplication_sdl.cpp | 3 +++ source/frontend/StarVoice.cpp | 17 +++++++++++++++-- source/frontend/StarVoice.hpp | 1 + source/vcpkg-configuration.json | 2 +- source/vcpkg.json | 6 +++++- 6 files changed, 28 insertions(+), 4 deletions(-) diff --git a/source/CMakeLists.txt b/source/CMakeLists.txt index 2192700..d66fb9d 100644 --- a/source/CMakeLists.txt +++ b/source/CMakeLists.txt @@ -177,6 +177,7 @@ endif() message(STATUS "Using Lua API checks: ${STAR_LUA_APICHECK}") message(STATUS "Using jemalloc: ${STAR_USE_JEMALLOC}") message(STATUS "Using mimalloc: ${STAR_USE_MIMALLOC}") +message(STATUS "Using rpmalloc: ${STAR_USE_RPMALLOC}") # Set C defines and cmake variables based on the build settings we have now # determined... @@ -486,8 +487,10 @@ set(STAR_EXT_LIBS ${STAR_EXT_LIBS} if(STAR_BUILD_GUI) find_package(SDL2 CONFIG REQUIRED) + find_package(SampleRate CONFIG REQUIRED) set(STAR_EXT_GUI_LIBS + SampleRate::samplerate $ $,SDL2::SDL2,SDL2::SDL2-static> ) diff --git a/source/application/StarMainApplication_sdl.cpp b/source/application/StarMainApplication_sdl.cpp index c697a26..49d085a 100644 --- a/source/application/StarMainApplication_sdl.cpp +++ b/source/application/StarMainApplication_sdl.cpp @@ -271,6 +271,9 @@ public: #endif Logger::info("Application: Initializing SDL Audio"); +#if SDL_VERSION_ATLEAST(2, 0, 26) + SDL_SetHint(SDL_HINT_AUDIO_RESAMPLING_MODE, "fast"); +#endif if (SDL_InitSubSystem(SDL_INIT_AUDIO)) throw ApplicationException(strf("Couldn't initialize SDL Audio: {}", SDL_GetError())); diff --git a/source/frontend/StarVoice.cpp b/source/frontend/StarVoice.cpp index 0d56089..1328846 100644 --- a/source/frontend/StarVoice.cpp +++ b/source/frontend/StarVoice.cpp @@ -211,14 +211,26 @@ void Voice::loadJson(Json const& config, bool skipSave) { m_lastInputTime = 0; } + bool shouldResetEncoder = false; if (auto channelMode = config.optString("channelMode")) { if (change(m_channelMode, VoiceChannelModeNames.getLeft(*channelMode), changed)) { closeDevice(); - resetEncoder(); + shouldResetEncoder = true; resetDevice(); } } + // not saving this setting to disk, as it's just for audiophiles + // don't want someone fudging their bitrate from the intended defaults and forgetting + if (auto bitrate = config.opt("bitrate")) { + unsigned newBitrate = bitrate->canConvert(Json::Type::Int) + ? clamp((unsigned)bitrate->toUInt(), 6000u, 510000u) : 0; + shouldResetEncoder |= change(m_bitrate, newBitrate, changed); + } + + if (shouldResetEncoder) + resetEncoder(); + if (changed && !skipSave) scheduleSave(); } @@ -607,7 +619,8 @@ void Voice::resetEncoder() { int channels = encoderChannels(); MutexLocker locker(m_threadMutex); m_encoder.reset(createEncoder(channels)); - opus_encoder_ctl(m_encoder.get(), OPUS_SET_BITRATE(channels == 2 ? 50000 : 24000)); + int bitrate = m_bitrate > 0 ? (int)m_bitrate : (channels == 2 ? 50000 : 24000); + opus_encoder_ctl(m_encoder.get(), OPUS_SET_BITRATE(bitrate)); } void Voice::resetDevice() { diff --git a/source/frontend/StarVoice.hpp b/source/frontend/StarVoice.hpp index 8059dd4..c914d36 100644 --- a/source/frontend/StarVoice.hpp +++ b/source/frontend/StarVoice.hpp @@ -195,6 +195,7 @@ private: Maybe m_deviceName; VoiceInputMode m_inputMode; VoiceChannelMode m_channelMode; + unsigned m_bitrate = 0; ThreadFunction m_thread; Mutex m_threadMutex; diff --git a/source/vcpkg-configuration.json b/source/vcpkg-configuration.json index e74116d..98aa608 100644 --- a/source/vcpkg-configuration.json +++ b/source/vcpkg-configuration.json @@ -2,7 +2,7 @@ "$schema": "https://raw.githubusercontent.com/microsoft/vcpkg-tool/main/docs/vcpkg-configuration.schema.json", "default-registry": { "kind": "git", - "baseline": "f4456c1b974131b8467c7371a3c302b7f58a99f2", + "baseline": "1de2026f28ead93ff1773e6e680387643e914ea1", "repository": "https://github.com/microsoft/vcpkg" } } diff --git a/source/vcpkg.json b/source/vcpkg.json index f603aab..d397f03 100644 --- a/source/vcpkg.json +++ b/source/vcpkg.json @@ -2,7 +2,11 @@ "$schema": "https://raw.githubusercontent.com/microsoft/vcpkg-tool/main/docs/vcpkg.schema.json", "dependencies": [ "glew", - "sdl2", + "libsamplerate", + { + "name": "sdl2", + "features": [ "samplerate" ] + }, "libvorbis", "zlib", "freetype",