From 018692edd0a1b8ce4ef239161b790aa8f8447a70 Mon Sep 17 00:00:00 2001 From: lightningterror <18107717+lightningterror@users.noreply.github.com> Date: Sun, 15 Jan 2023 01:30:54 +0100 Subject: [PATCH] SPU2: Cleanup SndOut.cpp const, casts, constexpr, initializations. --- pcsx2/SPU2/SndOut.cpp | 64 +++++++++++++++++++++---------------------- 1 file changed, 32 insertions(+), 32 deletions(-) diff --git a/pcsx2/SPU2/SndOut.cpp b/pcsx2/SPU2/SndOut.cpp index a6088e0b8b..5191bb36e9 100644 --- a/pcsx2/SPU2/SndOut.cpp +++ b/pcsx2/SPU2/SndOut.cpp @@ -1,5 +1,5 @@ /* PCSX2 - PS2 Emulator for PCs - * Copyright (C) 2002-2020 PCSX2 Dev Team + * Copyright (C) 2002-2023 PCSX2 Dev Team * * PCSX2 is free software: you can redistribute it and/or modify it under the terms * of the GNU Lesser General Public License as published by the Free Software Found- @@ -32,8 +32,8 @@ StereoOut32::StereoOut32(const StereoOut16& src) } StereoOut32::StereoOut32(const StereoOutFloat& src) - : Left((s32)(src.Left * 2147483647.0f)) - , Right((s32)(src.Right * 2147483647.0f)) + : Left(static_cast(src.Left * 2147483647.0f)) + , Right(static_cast(src.Right * 2147483647.0f)) { } @@ -221,8 +221,8 @@ void SndBuffer::_WriteSamples_Safe(StereoOut32* bData, int nSamples) // WARNING: This code assumes there's only ONE writing process. if ((m_size - m_wpos) < nSamples) { - int b1 = m_size - m_wpos; - int b2 = nSamples - b1; + const int b1 = m_size - m_wpos; + const int b2 = nSamples - b1; _WriteSamples_Internal(bData, b1); _WriteSamples_Internal(bData + b1, b2); @@ -238,8 +238,8 @@ void SndBuffer::_ReadSamples_Safe(StereoOut32* bData, int nSamples) // WARNING: This code assumes there's only ONE reading process. if ((m_size - m_rpos) < nSamples) { - int b1 = m_size - m_rpos; - int b2 = nSamples - b1; + const int b1 = m_size - m_rpos; + const int b2 = nSamples - b1; _ReadSamples_Internal(bData, b1); _ReadSamples_Internal(bData + b1, b2); @@ -270,7 +270,7 @@ void SndBuffer::ReadSamples(T* bData, int nSamples) // This will cause one brief hiccup that can never exceed the user's // set buffer length in duration. - int quietSamples; + int quietSamples = 0; if (CheckUnderrunStatus(nSamples, quietSamples)) { pxAssume(nSamples <= SndOutPacketSize); @@ -286,7 +286,7 @@ void SndBuffer::ReadSamples(T* bData, int nSamples) bData[i].ResampleFrom(m_buffer[i + m_rpos]); // Second part - int b2 = nSamples - b1; + const int b2 = nSamples - b1; for (int i = 0; i < b2; i++) bData[i + b1].ResampleFrom(m_buffer[i]); @@ -336,7 +336,7 @@ void SndBuffer::_WriteSamples(StereoOut32* bData, int nSamples) // The older portion of the buffer is discarded rather than incoming data, // so that the overall audio synchronization is better. - int free = m_size - _GetApproximateDataInBuffer(); // -1, but the <= handles that + const int free = m_size - _GetApproximateDataInBuffer(); // -1, but the <= handles that if (free <= nSamples) { // Disabled since the lock-free queue can't handle changing the read end from the write thread @@ -345,7 +345,7 @@ void SndBuffer::_WriteSamples(StereoOut32* bData, int nSamples) // Dump samples from the read portion of the buffer instead of dropping // the newly written stuff. - s32 comp; + s32 comp = 0; if( SynchMode == 0 ) // TimeStrech on { @@ -513,12 +513,12 @@ float SndBuffer::GetStatusPct() // Get the buffer status of the output driver too, so that we can // obtain a more accurate overall buffer status. - int drvempty = s_output_module->GetEmptySampleCount(); // / 2; + const int drvempty = s_output_module->GetEmptySampleCount(); // / 2; //ConLog( "Data %d >>> driver: %d predict: %d\n", m_data, drvempty, m_predictData ); - int data = _GetApproximateDataInBuffer(); - float result = (float)(data + m_predictData - drvempty) - (m_size / 16); + const int data = _GetApproximateDataInBuffer(); + float result = static_cast(data + m_predictData - drvempty) - (m_size / 16); result /= (m_size / 16); return result; } @@ -584,8 +584,8 @@ static float addToAvg(float val) avg_fullness[nextAvgPos] = val; nextAvgPos = (nextAvgPos + 1U) % AVERAGING_BUFFER_SIZE; - unsigned int actualWindow = std::min(available, AVERAGING_WINDOW); - unsigned int first = (nextAvgPos - actualWindow + AVERAGING_BUFFER_SIZE) % AVERAGING_BUFFER_SIZE; + const unsigned int actualWindow = std::min(available, AVERAGING_WINDOW); + const unsigned int first = (nextAvgPos - actualWindow + AVERAGING_BUFFER_SIZE) % AVERAGING_BUFFER_SIZE; // Possible optimization: if we know that actualWindow hasn't changed since // last invocation, we could calculate the running average in O(1) instead of O(N) @@ -613,9 +613,9 @@ static bool IsInRange(const T& val, const T& min, const T& max) void SndBuffer::UpdateTempoChangeSoundTouch2() { - long targetSamplesReservoir = 48 * EmuConfig.SPU2.Latency; //48000*SndOutLatencyMS/1000 + const long targetSamplesReservoir = 48 * EmuConfig.SPU2.Latency; //48000*SndOutLatencyMS/1000 //base aim at buffer filled % - float baseTargetFullness = (double)targetSamplesReservoir; ///(double)m_size;//0.05; + float baseTargetFullness = static_cast(targetSamplesReservoir); ///(double)m_size;//0.05; //state vars static bool inside_hysteresis; //=false; @@ -630,8 +630,8 @@ void SndBuffer::UpdateTempoChangeSoundTouch2() dynamicTargetFullness = baseTargetFullness; } - int data = _GetApproximateDataInBuffer(); - float bufferFullness = (float)data; ///(float)m_size; + const int data = _GetApproximateDataInBuffer(); + const float bufferFullness = static_cast(data); ///(float)m_size; #ifdef NEWSTRETCHER_USE_DYNAMIC_TUNING { //test current iterations/sec every 0.5s, and change algo params accordingly if different than previous IPS more than 30% @@ -657,13 +657,13 @@ void SndBuffer::UpdateTempoChangeSoundTouch2() #endif //Algorithm params: (threshold params (hysteresis), etc) - const float hys_ok_factor = 1.04f; - const float hys_bad_factor = 1.2f; - int hys_min_ok_count = std::clamp((int)(50.0 * (float)targetIPS / 750.0), 2, 100); //consecutive iterations within hys_ok before going to 1:1 mode - int compensationDivider = std::clamp((int)(100.0 * (float)targetIPS / 750), 15, 150); + constexpr float hys_ok_factor = 1.04f; + constexpr float hys_bad_factor = 1.2f; + const int hys_min_ok_count = std::clamp((int)(50.0 * (float)targetIPS / 750.0), 2, 100); //consecutive iterations within hys_ok before going to 1:1 mode + const int compensationDivider = std::clamp((int)(100.0 * (float)targetIPS / 750), 15, 150); float tempoAdjust = bufferFullness / dynamicTargetFullness; - float avgerage = addToAvg(tempoAdjust); + const float avgerage = addToAvg(tempoAdjust); tempoAdjust = avgerage; // Dampen the adjustment to avoid overshoots (this means the average will compensate to the other side). @@ -733,8 +733,8 @@ void SndBuffer::UpdateTempoChangeSoundTouch2() void SndBuffer::UpdateTempoChangeSoundTouch() { - float statusPct = GetStatusPct(); - float pctChange = statusPct - lastPct; + const float statusPct = GetStatusPct(); + const float pctChange = statusPct - lastPct; float tempoChange; float emergencyAdj = 0; @@ -783,8 +783,8 @@ void SndBuffer::UpdateTempoChangeSoundTouch() // Prediction based on the buffer fill status: - const float statusWeight = 2.99f; - const float statusRange = 0.068f; + constexpr float statusWeight = 2.99f; + constexpr float statusRange = 0.068f; // "non-emergency" deadzone: In this area stretching will be strongly discouraged. // Note: due tot he nature of timestretch latency, it's always a wee bit harder to @@ -830,7 +830,7 @@ void SndBuffer::UpdateTempoChangeSoundTouch() // is usually better/safer) if (newTempo < 0.970f || newTempo > 1.045f) { - cTempo = (float)newcee; + cTempo = static_cast(newcee); if (newTempo < 0.10f) newTempo = 0.10f; @@ -842,7 +842,7 @@ void SndBuffer::UpdateTempoChangeSoundTouch() else if (cTempo > 7.5f) cTempo = 7.5f; - pSoundTouch->setTempo(eTempo = (float)newTempo); + pSoundTouch->setTempo(eTempo = static_cast(newTempo)); /*ConLog("* SPU2: [Nominal %d%%] [Emergency: %d%%] (baseTempo: %d%% ) (newTempo: %d%%) (buffer: %d%%)\n", //(relation < 0.0) ? "Normalize" : "", @@ -875,7 +875,7 @@ void SndBuffer::UpdateTempoChangeSoundTouch() extern uint TickInterval; void SndBuffer::UpdateTempoChangeAsyncMixing() { - float statusPct = GetStatusPct(); + const float statusPct = GetStatusPct(); lastPct = statusPct; if (statusPct < -0.1f)