Skip to content

Commit

Permalink
Fixed crackling on some loud samples
Browse files Browse the repository at this point in the history
  • Loading branch information
Filoppi committed Jul 26, 2020
1 parent 562e014 commit 5dce69c
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 22 deletions.
12 changes: 6 additions & 6 deletions Source/Core/AudioCommon/AudioStretcher.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,22 +39,22 @@ void AudioStretcher::PushSamples(const s16* in, u32 num_in)
}
}

// This won't return any samples a lot of times as they are processed in batches
u32 AudioStretcher::GetStretchedSamples(s16* out, u32 num_out, bool pad)
{
// This won't return any samples a lot of times as they are processed in batches
u32 samples_received = m_sound_touch.receiveSamples(out, num_out);

if (!pad)
{
return samples_received;
}

if (samples_received != 0)
{
m_last_stretched_sample[0] = out[samples_received * 2 - 2];
m_last_stretched_sample[1] = out[samples_received * 2 - 1];
}

if (!pad)
{
return samples_received;
}

// Perform padding if we've run out of samples
for (u32 i = samples_received; i < num_out; ++i)
{
Expand Down
25 changes: 13 additions & 12 deletions Source/Core/AudioCommon/Mixer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

#include "Common/Logging/Log.h" //To delete and all the uses (or make debug log)
#include "VideoCommon/OnScreenDisplay.h" //To delete and all the uses
#pragma optimize("", off) //To delete
//#pragma optimize("", off) //To delete

Mixer::Mixer(u32 sample_rate)
: m_sample_rate(sample_rate), m_stretcher(sample_rate),
Expand Down Expand Up @@ -102,7 +102,7 @@ u32 Mixer::MixerFifo::Mix(s16* samples, u32 num_samples, bool stretching)
s32 lVolume = m_lVolume.load();
s32 rVolume = m_rVolume.load();

s16 s[NC]; // Padding samples
s32 s[NC]; // Padding samples
s[0] = m_last_output_samples[1];
s[1] = m_last_output_samples[1];

Expand Down Expand Up @@ -194,10 +194,10 @@ u32 Mixer::MixerFifo::Mix(s16* samples, u32 num_samples, bool stretching)
unsigned int current_sample = actual_samples_count * 2;
for (; current_sample < num_samples * 2; current_sample += 2)
{
samples[current_sample + 0] =
std::clamp(m_last_output_samples[0] + samples[current_sample + 0], -SHRT_MAX, SHRT_MAX);
samples[current_sample + 1] =
std::clamp(m_last_output_samples[1] + samples[current_sample + 1], -SHRT_MAX, SHRT_MAX);
samples[current_sample + 0] = std::clamp(
samples[current_sample + 0] + m_last_output_samples[0], SHRT_MIN, SHRT_MAX);
samples[current_sample + 1] = std::clamp(
samples[current_sample + 1] + m_last_output_samples[1], SHRT_MIN, SHRT_MAX);
}
}
//if (indexW > 8 && !m_constantly_pushed) OSD::AddMessage("last sample: " + std::to_string(m_last_output_samples[0]), 0U);
Expand All @@ -209,7 +209,7 @@ u32 Mixer::MixerFifo::Mix(s16* samples, u32 num_samples, bool stretching)

// Sounds better than linear interpolation
u32 Mixer::MixerFifo::CubicInterpolation(s16* samples, u32 num_samples, double rate, u32& indexR,
u32 indexW, s16& l_s, s16& r_s, s32 lVolume, s32 rVolume,
u32 indexW, s32& l_s, s32& r_s, s32 lVolume, s32 rVolume,
bool forwards)
{
// Coefficients copied from SoundTouch Copyright © Olli Parviainen 2001-2019
Expand Down Expand Up @@ -301,14 +301,14 @@ u32 Mixer::MixerFifo::CubicInterpolation(s16* samples, u32 num_samples, double r
y3 * interpolation_buffer[(indexR + 6 * direction + 1) & INDEX_MASK];
//To test left and right on HW source

l_s = (s16(std::round(l_s_f)) * lVolume) >> 8;
r_s = (s16(std::round(r_s_f)) * rVolume) >> 8;
l_s = (s32(std::round(l_s_f)) * lVolume) >> 8; //To round again post vol? //To use std::rint?
r_s = (s32(std::round(r_s_f)) * rVolume) >> 8;
// Clamp after adding to current sample, as if the cubic interpolation produced a sample over
// the limits and the current sample has the opposite sign, then we'd keep the excess value,
// while if it had the same sign, it would have been clamped anyway.
// We don't clamp to SHRT_MIN to keep the center at 0, though audio devices can take SHRT_MIN
samples[i * NC + 0] = std::clamp(samples[i * NC + 0] + l_s, -SHRT_MAX, SHRT_MAX);
samples[i * NC + 1] = std::clamp(samples[i * NC + 1] + r_s, -SHRT_MAX, SHRT_MAX);
// Instead of clamping to -SHORT_MAX like before to keep 0 centered, we use the full short range
samples[i * NC + 0] = std::clamp(samples[i * NC + 0] + l_s, SHRT_MIN, SHRT_MAX);
samples[i * NC + 1] = std::clamp(samples[i * NC + 1] + r_s, SHRT_MIN, SHRT_MAX);

++i;
}
Expand Down Expand Up @@ -597,6 +597,7 @@ u32 Mixer::MixSurround(float* samples, u32 num_samples)
if (available_samples != needed_samples)
{
ERROR_LOG(AUDIO, "Error decoding surround samples");
// This needs to do padding
return 0;
}

Expand Down
9 changes: 6 additions & 3 deletions Source/Core/AudioCommon/Mixer.h
Original file line number Diff line number Diff line change
Expand Up @@ -100,9 +100,11 @@ class Mixer final
u32 GetNextIndexR(u32 indexR) const;
void UpdatePush(double time);

// Returns the actual number of samples written. Outputs the last played sample for padding
// Returns the actual number of samples written. Outputs the last played sample for padding.
// We pass in in/out samples to cache the last calculated value. It's in int32 range because
// cubic interpolation can produce values over int16 and we don't want to lose the extra
u32 CubicInterpolation(s16* samples, u32 num_samples, double rate, u32& indexR, u32 indexW,
s16& l_s, s16& r_s, s32 lVolume, s32 rVolume, bool forwards = true);
s32& l_s, s32& r_s, s32 lVolume, s32 rVolume, bool forwards = true);

private:
Mixer* m_mixer;
Expand All @@ -128,7 +130,8 @@ class Mixer final
// Volume range is 0-256
std::atomic<s32> m_lVolume{256};
std::atomic<s32> m_rVolume{256};
s16 m_last_output_samples[NC]{};
// Se comment on CubicInterpolation()
s32 m_last_output_samples[NC]{};
double m_fract = -1.0;
double m_backwards_fract = -1.0;
//To review: if this was off, that mixer won't gather a buffer/latency, and it would always be on the brink of playback
Expand Down
2 changes: 1 addition & 1 deletion Source/Core/AudioCommon/SurroundDecoder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

#include "AudioCommon/Enums.h"
#include "AudioCommon/SurroundDecoder.h"
#pragma optimize("", off) //To delete
//#pragma optimize("", off) //To delete

namespace AudioCommon
{
Expand Down

0 comments on commit 5dce69c

Please sign in to comment.