Fix volume override for applets (i2s2/csnd)

This feature repurposes the functionality used for the camera shutter sound.
As such, it interferes with it:
    - shutter volume is set to the override instead of its default 100% value
    - due to implementation details, having the shutter sound effect play will
      make this feature stop working until the volume override is reapplied by
      going back to this menu

Closes #2072
This commit is contained in:
TuxSH 2024-10-19 23:53:44 +02:00
parent 684c95c7e8
commit 9b8a95a1de

View File

@ -389,24 +389,55 @@ void SysConfigMenu_ToggleCardIfPower(void)
static Result SysConfigMenu_ApplyVolumeOverride(void)
{
// Credit: profi200
u8 tmp;
// This feature repurposes the functionality used for the camera shutter sound.
// As such, it interferes with it:
// - shutter volume is set to the override instead of its default 100% value
// - due to implementation details, having the shutter sound effect play will
// make this feature stop working until the volume override is reapplied by
// going back to this menu
// Original credit: profi200
u8 i2s1Mux;
u8 i2s2Mux;
Result res = cdcChkInit();
if (R_SUCCEEDED(res)) res = CDCCHK_ReadRegisters2(0, 116, &tmp, 1); // CDC_REG_VOL_MICDET_PIN_SAR_ADC
if (currVolumeSliderOverride >= 0)
tmp &= ~0x80;
else
tmp |= 0x80;
if (R_SUCCEEDED(res)) res = CDCCHK_WriteRegisters2(0, 116, &tmp, 1);
if (R_SUCCEEDED(res)) res = CDCCHK_ReadRegisters2(0, 116, &i2s1Mux, 1); // used for shutter sound in TWL mode, and all GBA/DSi/3DS application
if (R_SUCCEEDED(res)) res = CDCCHK_ReadRegisters2(100, 49, &i2s2Mux, 1); // used for shutter sound in CTR mode and CTR mode library applets
if (currVolumeSliderOverride >= 0) {
s8 calculated = -128 + (((float)currVolumeSliderOverride/100.f) * 108);
if (calculated > -20)
res = -1; // Just in case
s8 volumes[2] = {calculated, calculated}; // Volume in 0.5 dB steps. -128 (muted) to 48. Do not go above -20 (100%).
if (R_SUCCEEDED(res)) res = CDCCHK_WriteRegisters2(0, 65, volumes, 2); // CDC_REG_DAC_L_VOLUME_CTRL, CDC_REG_DAC_R_VOLUME_CTRL
if (currVolumeSliderOverride >= 0)
{
i2s1Mux &= ~0x80;
i2s2Mux |= 0x20;
}
else
{
i2s1Mux |= 0x80;
i2s2Mux &= ~0x20;
}
s8 i2s1Volume;
s8 i2s2Volume;
if (currVolumeSliderOverride >= 0)
{
i2s1Volume = -128 + (((float)currVolumeSliderOverride/100.f) * 108);
i2s2Volume = i2s1Volume;
}
else
{
// Restore shutter sound volumes. This sould be sourced from cfg,
// however the values are the same everwhere
i2s1Volume = -3; // -1.5 dB (115.7%, only used by TWL applications when taking photos)
i2s2Volume = -20; // -10 dB (100%)
}
// Write volume overrides values before writing to the pinmux registers
if (R_SUCCEEDED(res)) res = CDCCHK_WriteRegisters2(0, 65, &i2s1Volume, 1); // CDC_REG_DAC_L_VOLUME_CTRL
if (R_SUCCEEDED(res)) res = CDCCHK_WriteRegisters2(0, 66, &i2s1Volume, 1); // CDC_REG_DAC_R_VOLUME_CTRL
if (R_SUCCEEDED(res)) res = CDCCHK_WriteRegisters2(100, 123, &i2s2Volume, 1);
if (R_SUCCEEDED(res)) res = CDCCHK_WriteRegisters2(0, 116, &i2s1Mux, 1);
if (R_SUCCEEDED(res)) res = CDCCHK_WriteRegisters2(100, 49, &i2s2Mux, 1);
cdcChkExit();
return res;