Skip to content

Commit

Permalink
Audio: Add mute/unmute and volume shortcuts
Browse files Browse the repository at this point in the history
Also add auto repeat to volume shortcuts
  • Loading branch information
Megamouse committed Jan 2, 2025
1 parent 799cb79 commit fb237dd
Show file tree
Hide file tree
Showing 15 changed files with 127 additions and 28 deletions.
39 changes: 39 additions & 0 deletions rpcs3/Emu/Audio/audio_utils.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#include "stdafx.h"
#include "audio_utils.h"
#include "Emu/system_config.h"
#include "Emu/System.h"
#include "Emu/IdManager.h"
#include "Emu/RSX/Overlays/overlay_message.h"

namespace audio
{
f32 get_volume()
{
return g_fxo->get<audio_fxo>().audio_muted ? 0.0f : g_cfg.audio.volume / 100.0f;
}

void toggle_mute()
{
audio_fxo& fxo = g_fxo->get<audio_fxo>();
fxo.audio_muted = !fxo.audio_muted;
Emu.GetCallbacks().update_emu_settings();

rsx::overlays::queue_message(fxo.audio_muted ? localized_string_id::AUDIO_MUTED : localized_string_id::AUDIO_UNMUTED, 3'000'000);
}

void change_volume(s32 delta)
{
// Ignore if muted
if (g_fxo->get<audio_fxo>().audio_muted) return;

const s32 old_volume = g_cfg.audio.volume;
const s32 new_volume = old_volume + delta;

if (old_volume == new_volume) return;

g_cfg.audio.volume.set(std::clamp<s32>(new_volume, g_cfg.audio.volume.min, g_cfg.audio.volume.max));
Emu.GetCallbacks().update_emu_settings();

rsx::overlays::queue_message(get_localized_string(localized_string_id::AUDIO_CHANGED, fmt::format("%d%%", g_cfg.audio.volume.get()).c_str()), 3'000'000);
}
}
14 changes: 14 additions & 0 deletions rpcs3/Emu/Audio/audio_utils.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#pragma once

namespace audio
{
struct audio_fxo
{
atomic_t<bool> audio_muted {false};
};

f32 get_volume();

void toggle_mute();
void change_volume(s32 delta);
}
1 change: 1 addition & 0 deletions rpcs3/Emu/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ target_sources(rpcs3_emu PRIVATE
# Audio
target_sources(rpcs3_emu PRIVATE
Audio/audio_resampler.cpp
Audio/audio_utils.cpp
Audio/AudioDumper.cpp
Audio/AudioBackend.cpp
Audio/Cubeb/CubebBackend.cpp
Expand Down
3 changes: 2 additions & 1 deletion rpcs3/Emu/Cell/Modules/cellAudio.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "stdafx.h"
#include "Emu/System.h"
#include "Emu/system_config.h"
#include "Emu/Audio/audio_utils.h"
#include "Emu/Cell/PPUModule.h"
#include "Emu/Cell/lv2/sys_process.h"
#include "Emu/Cell/lv2/sys_event.h"
Expand Down Expand Up @@ -1041,7 +1042,7 @@ void cell_audio_thread::mix(float* out_buffer, s32 offset)
constexpr u32 out_channels = static_cast<u32>(channels);
constexpr u32 out_buffer_sz = out_channels * AUDIO_BUFFER_SAMPLES;

const float master_volume = g_cfg.audio.volume / 100.0f;
const float master_volume = audio::get_volume();

// Reset out_buffer
std::memset(out_buffer, 0, out_buffer_sz * sizeof(float));
Expand Down
9 changes: 5 additions & 4 deletions rpcs3/Emu/Cell/lv2/sys_rsxaudio.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include "Emu/IdManager.h"
#include "Emu/System.h"
#include "Emu/system_config.h"
#include "Emu//Audio/audio_utils.h"
#include "Emu//Cell/Modules/cellAudioOut.h"
#include "util/video_provider.h"

Expand Down Expand Up @@ -1308,11 +1309,11 @@ rsxaudio_backend_thread::rsxaudio_backend_thread()
{
new_emu_cfg = get_emu_cfg();

const u64 new_vol = g_cfg.audio.volume;
const f32 new_vol = audio::get_volume();

callback_cfg.atomic_op([&](callback_config& val)
{
val.target_volume = static_cast<u16>(new_vol / 100.0 * callback_config::VOL_NOMINAL);
val.target_volume = static_cast<u16>(new_vol * callback_config::VOL_NOMINAL);
val.initial_volume = val.current_volume;
});
}
Expand All @@ -1332,11 +1333,11 @@ void rsxaudio_backend_thread::update_emu_cfg()
{
std::unique_lock lock(state_update_m);
const emu_audio_cfg _new_emu_cfg = get_emu_cfg();
const u64 new_vol = g_cfg.audio.volume;
const f32 new_vol = audio::get_volume();

callback_cfg.atomic_op([&](callback_config& val)
{
val.target_volume = static_cast<u16>(new_vol / 100.0 * callback_config::VOL_NOMINAL);
val.target_volume = static_cast<u16>(new_vol * callback_config::VOL_NOMINAL);
val.initial_volume = val.current_volume;
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ namespace rsx
{
m_dim_background = std::make_unique<overlay_element>();
m_dim_background->set_size(virtual_width, virtual_height);
m_dim_background->back_color.a = 0.5f;
m_dim_background->back_color.a = 0.9f;

m_list = std::make_unique<list_view>(virtual_width - 2 * 20, 540);
m_list->set_pos(20, 85);
Expand Down
5 changes: 5 additions & 0 deletions rpcs3/Emu/localized_string_id.h
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,10 @@ enum class localized_string_id
HOME_MENU_TROPHY_GRADE_GOLD,
HOME_MENU_TROPHY_GRADE_PLATINUM,

AUDIO_MUTED,
AUDIO_UNMUTED,
AUDIO_CHANGED,

PROGRESS_DIALOG_PROGRESS,
PROGRESS_DIALOG_PROGRESS_ANALYZING,
PROGRESS_DIALOG_REMAINING,
Expand All @@ -303,6 +307,7 @@ enum class localized_string_id
EMULATION_PAUSED_RESUME_WITH_START,
EMULATION_RESUMING,
EMULATION_FROZEN,

SAVESTATE_FAILED_DUE_TO_VDEC,
SAVESTATE_FAILED_DUE_TO_SAVEDATA,
SAVESTATE_FAILED_DUE_TO_SPU,
Expand Down
2 changes: 2 additions & 0 deletions rpcs3/emucore.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
<ClCompile Include="Crypto\decrypt_binaries.cpp" />
<ClCompile Include="Crypto\unzip.cpp" />
<ClCompile Include="Emu\Audio\audio_resampler.cpp" />
<ClCompile Include="Emu\Audio\audio_utils.cpp" />
<ClCompile Include="Emu\Audio\FAudio\FAudioBackend.cpp">
<ExcludedFromBuild>true</ExcludedFromBuild>
</ClCompile>
Expand Down Expand Up @@ -530,6 +531,7 @@
<ClInclude Include="Crypto\unzip.h" />
<ClInclude Include="Emu\Audio\audio_resampler.h" />
<ClInclude Include="Emu\Audio\audio_device_enumerator.h" />
<ClInclude Include="Emu\Audio\audio_utils.h" />
<ClInclude Include="Emu\Audio\FAudio\FAudioBackend.h">
<ExcludedFromBuild>true</ExcludedFromBuild>
</ClInclude>
Expand Down
6 changes: 6 additions & 0 deletions rpcs3/emucore.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -1324,6 +1324,9 @@
<ClCompile Include="Emu\RSX\Overlays\Trophies\overlay_trophy_list_dialog.cpp">
<Filter>Emu\GPU\RSX\Overlays\Trophies</Filter>
</ClCompile>
<ClCompile Include="Emu\Audio\audio_utils.cpp">
<Filter>Emu\Audio</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="Crypto\aes.h">
Expand Down Expand Up @@ -2677,6 +2680,9 @@
<ClInclude Include="Emu\RSX\Overlays\Trophies\overlay_trophy_list_dialog.h">
<Filter>Emu\GPU\RSX\Overlays\Trophies</Filter>
</ClInclude>
<ClInclude Include="Emu\Audio\audio_utils.h">
<Filter>Emu\Audio</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<None Include="Emu\RSX\Program\GLSLSnippets\GPUDeswizzle.glsl">
Expand Down
16 changes: 16 additions & 0 deletions rpcs3/rpcs3qt/gs_frame.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include "Emu/system_config.h"
#include "Emu/system_progress.hpp"
#include "Emu/IdManager.h"
#include "Emu/Audio/audio_utils.h"
#include "Emu/Cell/Modules/cellScreenshot.h"
#include "Emu/Cell/Modules/cellVideoOut.h"
#include "Emu/Cell/Modules/cellAudio.h"
Expand Down Expand Up @@ -358,6 +359,21 @@ void gs_frame::handle_shortcut(gui::shortcuts::shortcut shortcut_key, const QKey
pad::g_home_menu_requested = true;
break;
}
case gui::shortcuts::shortcut::gw_mute_unmute:
{
audio::toggle_mute();
break;
}
case gui::shortcuts::shortcut::gw_volume_up:
{
audio::change_volume(5);
break;
}
case gui::shortcuts::shortcut::gw_volume_down:
{
audio::change_volume(-5);
break;
}
default:
{
break;
Expand Down
3 changes: 2 additions & 1 deletion rpcs3/rpcs3qt/gui_application.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include "_discord_utils.h"
#endif

#include "Emu/Audio/audio_utils.h"
#include "Emu/Io/Null/null_camera_handler.h"
#include "Emu/Io/Null/null_music_handler.h"
#include "Emu/vfs_config.h"
Expand Down Expand Up @@ -621,7 +622,7 @@ void gui_application::InitializeCallbacks()
// Create a new sound effect. Re-using the same object seems to be broken for some users starting with Qt 6.6.3.
std::unique_ptr<QSoundEffect> sound_effect = std::make_unique<QSoundEffect>();
sound_effect->setSource(QUrl::fromLocalFile(QString::fromStdString(path)));
sound_effect->setVolume(g_cfg.audio.volume * 0.01f);
sound_effect->setVolume(audio::get_volume());
sound_effect->play();

m_sound_effects.push_back(std::move(sound_effect));
Expand Down
3 changes: 3 additions & 0 deletions rpcs3/rpcs3qt/localized_emu.h
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,9 @@ class localized_emu : public QObject
case localized_string_id::HOME_MENU_TROPHY_GRADE_SILVER: return tr("Silver", "Trophy type");
case localized_string_id::HOME_MENU_TROPHY_GRADE_GOLD: return tr("Gold", "Trophy type");
case localized_string_id::HOME_MENU_TROPHY_GRADE_PLATINUM: return tr("Platinum", "Trophy type");
case localized_string_id::AUDIO_MUTED: return tr("Audio muted", "Audio");
case localized_string_id::AUDIO_UNMUTED: return tr("Audio unmuted", "Audio");
case localized_string_id::AUDIO_CHANGED: return tr("Volume changed to %0", "Audio").arg(std::forward<Args>(args)...);
case localized_string_id::PROGRESS_DIALOG_PROGRESS: return tr("Progress:");
case localized_string_id::PROGRESS_DIALOG_PROGRESS_ANALYZING: return tr("Progress: analyzing...");
case localized_string_id::PROGRESS_DIALOG_REMAINING: return tr("remaining");
Expand Down
2 changes: 1 addition & 1 deletion rpcs3/rpcs3qt/shortcut_handler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ shortcut_handler::shortcut_handler(gui::shortcuts::shortcut_handler_id handler_i

const QKeySequence key_sequence = sc_settings.get_key_sequence(info, gui_settings);
QShortcut* shortcut = new QShortcut(key_sequence, parent);
shortcut->setAutoRepeat(false);
shortcut->setAutoRepeat(info.allow_auto_repeat);

shortcut_key_info key_info{};
key_info.shortcut = shortcut;
Expand Down
46 changes: 26 additions & 20 deletions rpcs3/rpcs3qt/shortcut_settings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ void fmt_class_string<shortcut>::format(std::string& out, u64 arg)
case shortcut::gw_frame_limit: return "gw_frame_limit";
case shortcut::gw_toggle_mouse_and_keyboard: return "gw_toggle_mouse_and_keyboard";
case shortcut::gw_home_menu: return "gw_home_menu";
case shortcut::gw_mute_unmute: return "gw_mute_unmute";
case shortcut::gw_volume_up: return "gw_volume_up";
case shortcut::gw_volume_down: return "gw_volume_down";
case shortcut::count: return "count";
}

Expand All @@ -53,26 +56,29 @@ void fmt_class_string<shortcut_handler_id>::format(std::string& out, u64 arg)

shortcut_settings::shortcut_settings()
: shortcut_map({
{ shortcut::mw_start, shortcut_info{ "main_window_start", tr("Start"), "Ctrl+E", shortcut_handler_id::main_window } },
{ shortcut::mw_stop, shortcut_info{ "main_window_stop", tr("Stop"), "Ctrl+S", shortcut_handler_id::main_window } },
{ shortcut::mw_pause, shortcut_info{ "main_window_pause", tr("Pause"), "Ctrl+P", shortcut_handler_id::main_window } },
{ shortcut::mw_restart, shortcut_info{ "main_window_restart", tr("Restart"), "Ctrl+R", shortcut_handler_id::main_window } },
{ shortcut::mw_toggle_fullscreen, shortcut_info{ "main_window_toggle_fullscreen", tr("Toggle Fullscreen"), "Alt+Return", shortcut_handler_id::main_window } },
{ shortcut::mw_exit_fullscreen, shortcut_info{ "main_window_exit_fullscreen", tr("Exit Fullscreen"), "Esc", shortcut_handler_id::main_window } },
{ shortcut::mw_refresh, shortcut_info{ "main_window_refresh", tr("Refresh"), "Ctrl+F5", shortcut_handler_id::main_window } },
{ shortcut::gw_toggle_fullscreen, shortcut_info{ "game_window_toggle_fullscreen", tr("Toggle Fullscreen"), "Alt+Return", shortcut_handler_id::game_window } },
{ shortcut::gw_exit_fullscreen, shortcut_info{ "game_window_exit_fullscreen", tr("Exit Fullscreen"), "Esc", shortcut_handler_id::game_window } },
{ shortcut::gw_log_mark, shortcut_info{ "game_window_log_mark", tr("Add Log Mark"), "Alt+L", shortcut_handler_id::game_window } },
{ shortcut::gw_mouse_lock, shortcut_info{ "game_window_mouse_lock", tr("Mouse lock"), "Ctrl+L", shortcut_handler_id::game_window } },
{ shortcut::gw_toggle_recording, shortcut_info{ "game_window_toggle_recording", tr("Start/Stop Recording"), "F11", shortcut_handler_id::game_window } },
{ shortcut::gw_screenshot, shortcut_info{ "game_window_screenshot", tr("Screenshot"), "F12", shortcut_handler_id::game_window } },
{ shortcut::gw_pause_play, shortcut_info{ "game_window_pause_play", tr("Pause/Play"), "Ctrl+P", shortcut_handler_id::game_window } },
{ shortcut::gw_savestate, shortcut_info{ "game_window_savestate", tr("Savestate"), "Ctrl+S", shortcut_handler_id::game_window } },
{ shortcut::gw_restart, shortcut_info{ "game_window_restart", tr("Restart"), "Ctrl+R", shortcut_handler_id::game_window } },
{ shortcut::gw_rsx_capture, shortcut_info{ "game_window_rsx_capture", tr("RSX Capture"), "Alt+C", shortcut_handler_id::game_window } },
{ shortcut::gw_frame_limit, shortcut_info{ "game_window_frame_limit", tr("Toggle Framelimit"), "Ctrl+F10", shortcut_handler_id::game_window } },
{ shortcut::gw_toggle_mouse_and_keyboard, shortcut_info{ "game_window_toggle_mouse_and_keyboard", tr("Toggle Keyboard"), "Ctrl+F11", shortcut_handler_id::game_window } },
{ shortcut::gw_home_menu, shortcut_info{ "gw_home_menu", tr("Open Home Menu"), "Shift+F10", shortcut_handler_id::game_window } },
{ shortcut::mw_start, shortcut_info{ "main_window_start", tr("Start"), "Ctrl+E", shortcut_handler_id::main_window, false } },
{ shortcut::mw_stop, shortcut_info{ "main_window_stop", tr("Stop"), "Ctrl+S", shortcut_handler_id::main_window, false } },
{ shortcut::mw_pause, shortcut_info{ "main_window_pause", tr("Pause"), "Ctrl+P", shortcut_handler_id::main_window, false } },
{ shortcut::mw_restart, shortcut_info{ "main_window_restart", tr("Restart"), "Ctrl+R", shortcut_handler_id::main_window, false } },
{ shortcut::mw_toggle_fullscreen, shortcut_info{ "main_window_toggle_fullscreen", tr("Toggle Fullscreen"), "Alt+Return", shortcut_handler_id::main_window, false } },
{ shortcut::mw_exit_fullscreen, shortcut_info{ "main_window_exit_fullscreen", tr("Exit Fullscreen"), "Esc", shortcut_handler_id::main_window, false } },
{ shortcut::mw_refresh, shortcut_info{ "main_window_refresh", tr("Refresh"), "Ctrl+F5", shortcut_handler_id::main_window, false } },
{ shortcut::gw_toggle_fullscreen, shortcut_info{ "game_window_toggle_fullscreen", tr("Toggle Fullscreen"), "Alt+Return", shortcut_handler_id::game_window, false } },
{ shortcut::gw_exit_fullscreen, shortcut_info{ "game_window_exit_fullscreen", tr("Exit Fullscreen"), "Esc", shortcut_handler_id::game_window, false } },
{ shortcut::gw_log_mark, shortcut_info{ "game_window_log_mark", tr("Add Log Mark"), "Alt+L", shortcut_handler_id::game_window, false } },
{ shortcut::gw_mouse_lock, shortcut_info{ "game_window_mouse_lock", tr("Mouse lock"), "Ctrl+L", shortcut_handler_id::game_window, false } },
{ shortcut::gw_toggle_recording, shortcut_info{ "game_window_toggle_recording", tr("Start/Stop Recording"), "F11", shortcut_handler_id::game_window, false } },
{ shortcut::gw_screenshot, shortcut_info{ "game_window_screenshot", tr("Screenshot"), "F12", shortcut_handler_id::game_window, false } },
{ shortcut::gw_pause_play, shortcut_info{ "game_window_pause_play", tr("Pause/Play"), "Ctrl+P", shortcut_handler_id::game_window, false } },
{ shortcut::gw_savestate, shortcut_info{ "game_window_savestate", tr("Savestate"), "Ctrl+S", shortcut_handler_id::game_window, false } },
{ shortcut::gw_restart, shortcut_info{ "game_window_restart", tr("Restart"), "Ctrl+R", shortcut_handler_id::game_window, false } },
{ shortcut::gw_rsx_capture, shortcut_info{ "game_window_rsx_capture", tr("RSX Capture"), "Alt+C", shortcut_handler_id::game_window, false } },
{ shortcut::gw_frame_limit, shortcut_info{ "game_window_frame_limit", tr("Toggle Framelimit"), "Ctrl+F10", shortcut_handler_id::game_window, false } },
{ shortcut::gw_toggle_mouse_and_keyboard, shortcut_info{ "game_window_toggle_mouse_and_keyboard", tr("Toggle Keyboard"), "Ctrl+F11", shortcut_handler_id::game_window, false } },
{ shortcut::gw_home_menu, shortcut_info{ "gw_home_menu", tr("Open Home Menu"), "Shift+F10", shortcut_handler_id::game_window, false } },
{ shortcut::gw_mute_unmute, shortcut_info{ "gw_mute_unmute", tr("Mute/Unmute Audio"), "Shift+M", shortcut_handler_id::game_window, false } },
{ shortcut::gw_volume_up, shortcut_info{ "gw_volume_up", tr("Volume Up"), "Shift++", shortcut_handler_id::game_window, true } },
{ shortcut::gw_volume_down, shortcut_info{ "gw_volume_down", tr("Volume Down"), "Shift+-", shortcut_handler_id::game_window, true } },
})
{
}
Expand Down
4 changes: 4 additions & 0 deletions rpcs3/rpcs3qt/shortcut_settings.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ namespace gui
gw_frame_limit,
gw_toggle_mouse_and_keyboard,
gw_home_menu,
gw_mute_unmute,
gw_volume_up,
gw_volume_down,

count
};
Expand All @@ -49,6 +52,7 @@ struct shortcut_info
QString localized_name;
QString key_sequence;
gui::shortcuts::shortcut_handler_id handler_id{};
bool allow_auto_repeat = false;
};

class shortcut_settings : public QObject
Expand Down

0 comments on commit fb237dd

Please sign in to comment.