From 01b99eff98e6f752f82bc018052d1a3a1286dd65 Mon Sep 17 00:00:00 2001 From: "Unknown W. Brackets" Date: Sat, 9 Oct 2021 08:17:55 -0700 Subject: [PATCH 1/2] Audio: Cleanup buffer wrap code. This is a bit more straight-forward. --- Core/HW/StereoResampler.cpp | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/Core/HW/StereoResampler.cpp b/Core/HW/StereoResampler.cpp index 1705191eef..3f0f973a0b 100644 --- a/Core/HW/StereoResampler.cpp +++ b/Core/HW/StereoResampler.cpp @@ -118,7 +118,8 @@ inline void ClampBufferToS16(s16 *out, const s32 *in, size_t size, s8 volShift) size -= 8; } #elif PPSSPP_ARCH(ARM_NEON) - int16x4_t signedVolShift = vdup_n_s16 (-volShift); // Can only dynamic-shift right, but by a signed integer + // Dynamic shifts can only be left, but it's signed - negate to shift right. + int16x4_t signedVolShift = vdup_n_s16(-volShift); while (size >= 8) { int32x4_t in1 = vld1q_s32(in); int32x4_t in2 = vld1q_s32(in + 4); @@ -281,10 +282,11 @@ void StereoResampler::PushSamples(const s32 *samples, unsigned int numSamples) { return; } - int over_bytes = numSamples * 4 - (m_maxBufsize * 2 - (indexW & INDEX_MASK)) * sizeof(short); - if (over_bytes > 0) { - ClampBufferToS16WithVolume(&m_buffer[indexW & INDEX_MASK], samples, (numSamples * 4 - over_bytes) / 2); - ClampBufferToS16WithVolume(&m_buffer[0], samples + (numSamples * 4 - over_bytes) / sizeof(short), over_bytes / 2); + // Check if we need to roll over to the start of the buffer during the copy. + int indexW_left_samples = m_maxBufsize * 2 - (indexW & INDEX_MASK); + if (numSamples * 2 > indexW_left_samples) { + ClampBufferToS16WithVolume(&m_buffer[indexW & INDEX_MASK], samples, indexW_left_samples); + ClampBufferToS16WithVolume(&m_buffer[0], samples + indexW_left_samples, numSamples * 2 - indexW_left_samples); } else { ClampBufferToS16WithVolume(&m_buffer[indexW & INDEX_MASK], samples, numSamples * 2); } From b77695c760ba8dde18f94f2e87f56546e30bfa57 Mon Sep 17 00:00:00 2001 From: "Unknown W. Brackets" Date: Sat, 9 Oct 2021 08:24:54 -0700 Subject: [PATCH 2/2] Audio: Protect against time skew. It might be possible a clock is adjusting backwards slightly after a time update, and this could've caused sz to become negative. Based on crash reports. --- UI/BackgroundAudio.cpp | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/UI/BackgroundAudio.cpp b/UI/BackgroundAudio.cpp index 82d7bf586d..5db580ffcd 100644 --- a/UI/BackgroundAudio.cpp +++ b/UI/BackgroundAudio.cpp @@ -344,7 +344,10 @@ int BackgroundAudio::Play() { } double now = time_now_d(); - int sz = lastPlaybackTime_ <= 0.0 ? 44100 / 60 : (int)((now - lastPlaybackTime_) * 44100); + int sz = 44100 / 60; + if (lastPlaybackTime_ > 0.0 && lastPlaybackTime_ >= now) { + sz = (int)((now - lastPlaybackTime_) * 44100); + } sz = std::min(BUFSIZE / 2, sz); if (at3Reader_) { if (at3Reader_->Read(buffer, sz)) {