Make code a little more concise; should have done this in the last commit.

This commit is contained in:
Stephen Anthony 2022-06-05 11:10:44 -02:30
parent 0503927ec1
commit 3baf404873
1 changed files with 27 additions and 22 deletions

View File

@ -16,6 +16,11 @@
//============================================================================ //============================================================================
#include <cmath> #include <cmath>
using std::abs;
using std::max;
using std::min;
using std::pow;
using std::round;
#include "JitterEmulation.hxx" #include "JitterEmulation.hxx"
@ -37,24 +42,24 @@ void JitterEmulation::setSensitivity(Int32 sensitivity)
myLastFrameScanlines = myLastFrameVsyncCycles = myUnstableCount = myJitter = 0; myLastFrameScanlines = myLastFrameVsyncCycles = myUnstableCount = myJitter = 0;
mySensitivity = BSPF::clamp(sensitivity, MIN_SENSITIVITY, MAX_SENSITIVITY); mySensitivity = BSPF::clamp(sensitivity, MIN_SENSITIVITY, MAX_SENSITIVITY);
const float factor = std::pow(static_cast<float>(mySensitivity - MIN_SENSITIVITY) / (MAX_SENSITIVITY - MIN_SENSITIVITY), 1.5); const float factor = pow(static_cast<float>(mySensitivity - MIN_SENSITIVITY) / (MAX_SENSITIVITY - MIN_SENSITIVITY), 1.5);
myScanlineDelta = std::round(MAX_SCANLINE_DELTA - (MAX_SCANLINE_DELTA - MIN_SCANLINE_DELTA) * factor); myScanlineDelta = round(MAX_SCANLINE_DELTA - (MAX_SCANLINE_DELTA - MIN_SCANLINE_DELTA) * factor);
myVsyncCycles = std::round(MIN_VSYNC_CYCLES + (MAX_VSYNC_CYCLES - MIN_VSYNC_CYCLES) * factor); myVsyncCycles = round(MIN_VSYNC_CYCLES + (MAX_VSYNC_CYCLES - MIN_VSYNC_CYCLES) * factor);
myVsyncDelta1 = std::round(MAX_VSYNC_DELTA_1 - (MAX_VSYNC_DELTA_1 - MIN_VSYNC_DELTA_1) * factor); myVsyncDelta1 = round(MAX_VSYNC_DELTA_1 - (MAX_VSYNC_DELTA_1 - MIN_VSYNC_DELTA_1) * factor);
#ifdef VSYNC_LINE_JITTER #ifdef VSYNC_LINE_JITTER
myVsyncDelta2 = std::round(MIN_VSYNC_DELTA_2 + (MAX_VSYNC_DELTA_2 - MIN_VSYNC_DELTA_2) * factor); myVsyncDelta2 = round(MIN_VSYNC_DELTA_2 + (MAX_VSYNC_DELTA_2 - MIN_VSYNC_DELTA_2) * factor);
#endif #endif
myUnstableFrames = std::round(MAX_UNSTABLE_FRAMES - (MAX_UNSTABLE_FRAMES - MIN_UNSTABLE_FRAMES) * factor); myUnstableFrames = round(MAX_UNSTABLE_FRAMES - (MAX_UNSTABLE_FRAMES - MIN_UNSTABLE_FRAMES) * factor);
myJitterLines = std::round(MIN_JITTER_LINES + (MAX_JITTER_LINES - MIN_JITTER_LINES) * factor); myJitterLines = round(MIN_JITTER_LINES + (MAX_JITTER_LINES - MIN_JITTER_LINES) * factor);
myVsyncLines = std::round(MIN_VSYNC_LINES + (MAX_VSYNC_LINES - MIN_VSYNC_LINES) * factor); myVsyncLines = round(MIN_VSYNC_LINES + (MAX_VSYNC_LINES - MIN_VSYNC_LINES) * factor);
} }
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
void JitterEmulation::frameComplete(Int32 scanlineCount, Int32 vsyncCycles) void JitterEmulation::frameComplete(Int32 scanlineCount, Int32 vsyncCycles)
{ {
#ifdef DEBUG_BUILD #ifdef DEBUG_BUILD
const int vsyncLines = std::round((vsyncCycles - 2) / 76.0); const int vsyncLines = round((vsyncCycles - 2) / 76.0);
cerr << "TV jitter " << myJitter << " - " << scanlineCount << ", " << vsyncCycles << ", " << vsyncLines << endl; cerr << "TV jitter " << myJitter << " - " << scanlineCount << ", " << vsyncCycles << ", " << vsyncLines << endl;
#endif #endif
@ -63,11 +68,11 @@ void JitterEmulation::frameComplete(Int32 scanlineCount, Int32 vsyncCycles)
const bool vsyncCyclesStable = vsyncCycles >= myVsyncCycles; const bool vsyncCyclesStable = vsyncCycles >= myVsyncCycles;
// Handle inconsistency of vsync cycles and around half lines // Handle inconsistency of vsync cycles and around half lines
#ifdef VSYNC_LINE_JITTER #ifdef VSYNC_LINE_JITTER
const Int32 minLines = std::round((vsyncCycles - 2 - myVsyncDelta2) / 76.0); const Int32 minLines = round((vsyncCycles - 2 - myVsyncDelta2) / 76.0);
const Int32 maxLines = std::round((vsyncCycles - 2 + myVsyncDelta2) / 76.0); const Int32 maxLines = round((vsyncCycles - 2 + myVsyncDelta2) / 76.0);
const Int32 minLastLines = std::round((myLastFrameVsyncCycles - 2 - myVsyncDelta2) / 76.0); const Int32 minLastLines = round((myLastFrameVsyncCycles - 2 - myVsyncDelta2) / 76.0);
const Int32 maxLastLines = std::round((myLastFrameVsyncCycles - 2 + myVsyncDelta2) / 76.0); const Int32 maxLastLines = round((myLastFrameVsyncCycles - 2 + myVsyncDelta2) / 76.0);
const bool vsyncLinesStable = std::abs(vsyncCycles - myLastFrameVsyncCycles) < myVsyncDelta1 const bool vsyncLinesStable = abs(vsyncCycles - myLastFrameVsyncCycles) < myVsyncDelta1
&& minLines == maxLastLines && maxLines == minLastLines; && minLines == maxLastLines && maxLines == minLastLines;
#else #else
const bool vsyncLinesStable = abs(vsyncCycles - myLastFrameVsyncCycles) < myVsyncDelta1; const bool vsyncLinesStable = abs(vsyncCycles - myLastFrameVsyncCycles) < myVsyncDelta1;
@ -85,14 +90,14 @@ void JitterEmulation::frameComplete(Int32 scanlineCount, Int32 vsyncCycles)
&& abs(myJitter) < static_cast<Int32>(myRandom.next() % myJitterLines)) && abs(myJitter) < static_cast<Int32>(myRandom.next() % myJitterLines))
{ {
// Repeated invalid frames cause randomly repeated jitter // Repeated invalid frames cause randomly repeated jitter
myJitter = std::max(std::min(scanlineDifference, myJitterLines), -myYStart); myJitter = max(min(scanlineDifference, myJitterLines), -myYStart);
} }
} }
if(!vsyncCyclesStable) if(!vsyncCyclesStable)
{ {
// If VSYNC length is too low, the frame rolls permanently down, speed depending on missing cycles // If VSYNC length is too low, the frame rolls permanently down, speed depending on missing cycles
const Int32 jitter = std::max( const Int32 jitter = max(
std::min<Int32>(std::round(scanlineCount * (1 - static_cast<float>(vsyncCycles) / myVsyncCycles)), min<Int32>(round(scanlineCount * (1 - static_cast<float>(vsyncCycles) / myVsyncCycles)),
myJitterLines), myJitterLines),
myJitterRecovery + 1); // Roll at least one scanline myJitterRecovery + 1); // Roll at least one scanline
@ -109,7 +114,7 @@ void JitterEmulation::frameComplete(Int32 scanlineCount, Int32 vsyncCycles)
myJitter += vsyncCycles > myLastFrameVsyncCycles ? myVsyncLines : -myVsyncLines; myJitter += vsyncCycles > myLastFrameVsyncCycles ? myVsyncLines : -myVsyncLines;
#endif #endif
} }
myJitter = std::max(myJitter, -myYStart); myJitter = max(myJitter, -myYStart);
} }
} }
else else
@ -118,9 +123,9 @@ void JitterEmulation::frameComplete(Int32 scanlineCount, Int32 vsyncCycles)
// Only recover during stable frames // Only recover during stable frames
if(myJitter > 0) if(myJitter > 0)
myJitter = std::max(myJitter - myJitterRecovery, 0); myJitter = max(myJitter - myJitterRecovery, 0);
else if(myJitter < 0) else if(myJitter < 0)
myJitter = std::min(myJitter + myJitterRecovery, 0); myJitter = min(myJitter + myJitterRecovery, 0);
} }
myLastFrameScanlines = scanlineCount; myLastFrameScanlines = scanlineCount;
@ -142,7 +147,7 @@ bool JitterEmulation::save(Serializer& out) const
} }
catch(...) catch(...)
{ {
cerr << "ERROR: JitterEmulation::save" << std::endl; cerr << "ERROR: JitterEmulation::save" << endl;
return false; return false;
} }
@ -165,7 +170,7 @@ bool JitterEmulation::load(Serializer& in)
} }
catch (...) catch (...)
{ {
cerr << "ERROR: JitterEmulation::load" << std::endl; cerr << "ERROR: JitterEmulation::load" << endl;
return false; return false;
} }