From ec914e895ff6d7bf2d19e8c8785e5d8bfd38629f Mon Sep 17 00:00:00 2001 From: Chris Robinson Date: Mon, 25 Dec 2023 05:21:42 -0800 Subject: Use standard methods for rounding --- alc/alc.cpp | 6 +++--- alc/backends/pipewire.cpp | 9 +++++---- examples/alffplay.cpp | 4 ++-- utils/makemhr/loadsofa.cpp | 2 +- 4 files changed, 11 insertions(+), 10 deletions(-) diff --git a/alc/alc.cpp b/alc/alc.cpp index d944cf0c..e9d3aed7 100644 --- a/alc/alc.cpp +++ b/alc/alc.cpp @@ -1025,7 +1025,7 @@ ALCenum UpdateDeviceParams(ALCdevice *device, const int *attrList) optsrate = clampu(*freqopt, MinOutputRate, MaxOutputRate); const double scale{static_cast(*optsrate) / double{DefaultOutputRate}}; - period_size = static_cast(period_size*scale + 0.5); + period_size = static_cast(std::lround(period_size * scale)); } if(auto persizeopt = device->configValue({}, "period_size")) @@ -1330,8 +1330,8 @@ ALCenum UpdateDeviceParams(ALCdevice *device, const int *attrList) freqAttr = clampi(freqAttr, MinOutputRate, MaxOutputRate); const double scale{static_cast(freqAttr) / oldrate}; - period_size = static_cast(period_size*scale + 0.5); - buffer_size = static_cast(buffer_size*scale + 0.5); + period_size = static_cast(std::lround(period_size * scale)); + buffer_size = static_cast(std::lround(buffer_size * scale)); optsrate = static_cast(freqAttr); } } diff --git a/alc/backends/pipewire.cpp b/alc/backends/pipewire.cpp index 2e6e20cc..c31f943e 100644 --- a/alc/backends/pipewire.cpp +++ b/alc/backends/pipewire.cpp @@ -1629,11 +1629,12 @@ bool PipeWirePlayback::reset() { /* Scale the update size if the sample rate changes. */ const double scale{static_cast(match->mSampleRate) / mDevice->Frequency}; - const double numbufs{static_cast(mDevice->BufferSize)/mDevice->UpdateSize}; + const double updatesize{std::round(mDevice->UpdateSize * scale)}; + const double buffersize{std::round(mDevice->BufferSize * scale)}; + mDevice->Frequency = match->mSampleRate; - mDevice->UpdateSize = static_cast(clampd(mDevice->UpdateSize*scale + 0.5, - 64.0, 8192.0)); - mDevice->BufferSize = static_cast(numbufs*mDevice->UpdateSize + 0.5); + mDevice->UpdateSize = static_cast(clampd(updatesize, 64.0, 8192.0)); + mDevice->BufferSize = static_cast(maxd(buffersize, 128.0)); } if(!mDevice->Flags.test(ChannelsRequest) && match->mChannels != InvalidChannelConfig) mDevice->FmtChans = match->mChannels; diff --git a/examples/alffplay.cpp b/examples/alffplay.cpp index 890ecedb..a7633bfa 100644 --- a/examples/alffplay.cpp +++ b/examples/alffplay.cpp @@ -1509,9 +1509,9 @@ void VideoState::updateVideo(SDL_Window *screen, SDL_Renderer *renderer, bool re { double aspect_ratio = av_q2d(frame->sample_aspect_ratio); if(aspect_ratio >= 1.0) - frame_width = static_cast(frame_width*aspect_ratio + 0.5); + frame_width = static_cast(std::lround(frame_width * aspect_ratio)); else if(aspect_ratio > 0.0) - frame_height = static_cast(frame_height/aspect_ratio + 0.5); + frame_height = static_cast(std::lround(frame_height / aspect_ratio)); } SDL_SetWindowSize(screen, frame_width, frame_height); } diff --git a/utils/makemhr/loadsofa.cpp b/utils/makemhr/loadsofa.cpp index c29cb45c..b2038c77 100644 --- a/utils/makemhr/loadsofa.cpp +++ b/utils/makemhr/loadsofa.cpp @@ -460,7 +460,7 @@ bool LoadSofaFile(const char *filename, const uint numThreads, const uint fftSiz /* Assume a default head radius of 9cm. */ hData->mRadius = 0.09; - hData->mIrRate = static_cast(GetSampleRate(sofaHrtf.get()) + 0.5f); + hData->mIrRate = static_cast(std::lround(GetSampleRate(sofaHrtf.get()))); if(!hData->mIrRate) return false; -- cgit v1.2.3