Skip to content

Commit

Permalink
Merge pull request #14711 from unknownbrackets/reverb-volume
Browse files Browse the repository at this point in the history
Sas: Add option to control reverb volume
  • Loading branch information
hrydgard authored Aug 10, 2021
2 parents 379f075 + ff9b9f7 commit d62899e
Show file tree
Hide file tree
Showing 9 changed files with 30 additions and 10 deletions.
3 changes: 2 additions & 1 deletion Core/Config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -904,7 +904,8 @@ static ConfigSetting soundSettings[] = {
ConfigSetting("Enable", &g_Config.bEnableSound, true, true, true),
ConfigSetting("AudioBackend", &g_Config.iAudioBackend, 0, true, true),
ConfigSetting("ExtraAudioBuffering", &g_Config.bExtraAudioBuffering, false, true, false),
ConfigSetting("GlobalVolume", &g_Config.iGlobalVolume, VOLUME_MAX, true, true),
ConfigSetting("GlobalVolume", &g_Config.iGlobalVolume, VOLUME_FULL, true, true),
ConfigSetting("ReverbVolume", &g_Config.iReverbVolume, VOLUME_FULL, true, true),
ConfigSetting("AltSpeedVolume", &g_Config.iAltSpeedVolume, -1, true, true),
ConfigSetting("AudioDevice", &g_Config.sAudioDevice, "", true, false),
ConfigSetting("AutoAudioDevice", &g_Config.bAutoAudioDevice, true, true, false),
Expand Down
1 change: 1 addition & 0 deletions Core/Config.h
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,7 @@ struct Config {
bool bEnableSound;
int iAudioBackend;
int iGlobalVolume;
int iReverbVolume;
int iAltSpeedVolume;
bool bExtraAudioBuffering; // For bluetooth
std::string sAudioDevice;
Expand Down
2 changes: 1 addition & 1 deletion Core/ConfigValues.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ const int PSP_MODEL_FAT = 0;
const int PSP_MODEL_SLIM = 1;
const int PSP_DEFAULT_FIRMWARE = 660;
static const int8_t VOLUME_OFF = 0;
static const int8_t VOLUME_MAX = 10;
static const int8_t VOLUME_FULL = 10;

enum class CPUCore {
INTERPRETER = 0,
Expand Down
2 changes: 1 addition & 1 deletion Core/HW/SasAudio.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -680,7 +680,7 @@ void SasInstance::ApplyWaveformEffect() {
sendBufferDownsampled[i * 2 + 1] = clamp_s16(sendBuffer[i * 4 + 1]);
}

// Volume max is 0x1000, while our factor is up to 0x8000. Shifting right by 3 fixes that.
// Volume max is 0x1000, while our factor is up to 0x8000. Shifting left by 3 fixes that.
reverb_.ProcessReverb(sendBufferProcessed, sendBufferDownsampled, grainSize / 2, waveformEffect.leftVol << 3, waveformEffect.rightVol << 3);
}

Expand Down
15 changes: 13 additions & 2 deletions Core/HW/SasReverb.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
#include <cstdint>
#include <cstring>

#include "Common/Math/math_util.h"
#include "Core/Config.h"
#include "Core/HW/SasReverb.h"
#include "Core/Util/AudioFormat.h"

Expand Down Expand Up @@ -219,6 +221,15 @@ void SasReverb::ProcessReverb(int16_t *output, const int16_t *input, size_t inpu
return;
}

const uint8_t reverbVolume = Clamp(g_Config.iReverbVolume, 0, 25);
// Standard volume is 10, which pairs with a normal shift of 15.
const uint8_t finalShift = 25 - reverbVolume;
if (reverbVolume == 0) {
// Force to zero output, which is not the same as "Off."
memset(output, 0, inputSize * 4);
return;
}

const SasReverbData &d = presets[preset_];

// We put this on the stack instead of in the object to let the compiler optimize better (avoid mem r/w).
Expand Down Expand Up @@ -255,8 +266,8 @@ void SasReverb::ProcessReverb(int16_t *output, const int16_t *input, size_t inpu
b[d.mRAPF2] = clamp_s16(Rout - (d.vAPF2*b[(d.mRAPF2 - d.dAPF2)] >> 15));
Rout = b[(d.mRAPF2 - d.dAPF2)] + (b[d.mRAPF2] * d.vAPF2 >> 15);
// ___Output to Mixer(Output volume multiplied with input from APF2)___________
output[i * 4 + 0] = clamp_s16(Lout * volLeft >> 15);
output[i * 4 + 1] = clamp_s16(Rout * volRight >> 15);
output[i * 4 + 0] = clamp_s16((Lout * volLeft) >> finalShift);
output[i * 4 + 1] = clamp_s16((Rout * volRight) >> finalShift);
output[i * 4 + 2] = 0;
output[i * 4 + 3] = 0;

Expand Down
4 changes: 2 additions & 2 deletions Core/HW/StereoResampler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -149,12 +149,12 @@ inline void ClampBufferToS16WithVolume(s16 *out, const s32 *in, size_t size) {
}
}

if (volume >= VOLUME_MAX) {
if (volume >= VOLUME_FULL) {
ClampBufferToS16<false>(out, in, size, 0);
} else if (volume <= VOLUME_OFF) {
memset(out, 0, size * sizeof(s16));
} else {
ClampBufferToS16<true>(out, in, size, VOLUME_MAX - (s8)volume);
ClampBufferToS16<true>(out, in, size, VOLUME_FULL - (s8)volume);
}
}

Expand Down
8 changes: 6 additions & 2 deletions UI/GameSettingsScreen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -609,15 +609,19 @@ void GameSettingsScreen::CreateViews() {

audioSettings->Add(new CheckBox(&g_Config.bEnableSound, a->T("Enable Sound")));

PopupSliderChoice *volume = audioSettings->Add(new PopupSliderChoice(&g_Config.iGlobalVolume, VOLUME_OFF, VOLUME_MAX, a->T("Global volume"), screenManager()));
PopupSliderChoice *volume = audioSettings->Add(new PopupSliderChoice(&g_Config.iGlobalVolume, VOLUME_OFF, VOLUME_FULL, a->T("Global volume"), screenManager()));
volume->SetEnabledPtr(&g_Config.bEnableSound);
volume->SetZeroLabel(a->T("Mute"));

PopupSliderChoice *altVolume = audioSettings->Add(new PopupSliderChoice(&g_Config.iAltSpeedVolume, VOLUME_OFF, VOLUME_MAX, a->T("Alternate speed volume"), screenManager()));
PopupSliderChoice *altVolume = audioSettings->Add(new PopupSliderChoice(&g_Config.iAltSpeedVolume, VOLUME_OFF, VOLUME_FULL, a->T("Alternate speed volume"), screenManager()));
altVolume->SetEnabledPtr(&g_Config.bEnableSound);
altVolume->SetZeroLabel(a->T("Mute"));
altVolume->SetNegativeDisable(a->T("Use global volume"));

PopupSliderChoice *reverbVolume = audioSettings->Add(new PopupSliderChoice(&g_Config.iReverbVolume, VOLUME_OFF, 2 * VOLUME_FULL, a->T("Reverb volume"), screenManager()));
reverbVolume->SetEnabledPtr(&g_Config.bEnableSound);
reverbVolume->SetZeroLabel(a->T("Disabled"));

// Hide the backend selector in UWP builds (we only support XAudio2 there).
#if PPSSPP_PLATFORM(WINDOWS) && !PPSSPP_PLATFORM(UWP)
if (IsVistaOrHigher()) {
Expand Down
2 changes: 2 additions & 0 deletions headless/Headless.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -405,6 +405,8 @@ int main(int argc, const char* argv[])
g_Config.sMACAddress = "12:34:56:78:9A:BC";
g_Config.iFirmwareVersion = PSP_DEFAULT_FIRMWARE;
g_Config.iPSPModel = PSP_MODEL_SLIM;
g_Config.iGlobalVolume = VOLUME_FULL;
g_Config.iReverbVolume = VOLUME_FULL;

#ifdef _WIN32
g_Config.internalDataDirectory.clear();
Expand Down
3 changes: 2 additions & 1 deletion libretro/libretro.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -420,7 +420,8 @@ void retro_init(void)
// libretro does its own timing, so this should stay CONTINUOUS.
g_Config.iUnthrottleMode = (int)UnthrottleMode::CONTINUOUS;
g_Config.bMemStickInserted = true;
g_Config.iGlobalVolume = VOLUME_MAX - 1;
g_Config.iGlobalVolume = VOLUME_FULL - 1;
g_Config.iReverbVolume = VOLUME_FULL;
g_Config.iAltSpeedVolume = -1;
g_Config.bEnableSound = true;
g_Config.iCwCheatRefreshRate = 60;
Expand Down

0 comments on commit d62899e

Please sign in to comment.