Skip to content

Commit

Permalink
Add 'Use Alternative Audio Hook' option, enable by default
Browse files Browse the repository at this point in the history
  • Loading branch information
DarthMew committed Sep 30, 2023
1 parent 3767447 commit afb6bc7
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 8 deletions.
2 changes: 2 additions & 0 deletions src/core/settings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,8 @@ void Settings::Load(SettingsInterface& si)
apply_game_settings = si.GetBoolValue("Main", "ApplyGameSettings", true);
runahead_frames = static_cast<u32>(si.GetIntValue("Main", "RunaheadFrameCount", 0));

audio_fast_hook = si.GetBoolValue("Audio", "FastHook", true);

cpu_execution_mode =
ParseCPUExecutionMode(
si.GetStringValue("CPU", "ExecutionMode", GetCPUExecutionModeName(DEFAULT_CPU_EXECUTION_MODE)).c_str())
Expand Down
2 changes: 2 additions & 0 deletions src/core/settings.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ struct Settings

ConsoleRegion region = ConsoleRegion::Auto;

bool audio_fast_hook = true;

CPUExecutionMode cpu_execution_mode = CPUExecutionMode::Interpreter;
u32 cpu_overclock_numerator = 1;
u32 cpu_overclock_denominator = 1;
Expand Down
32 changes: 24 additions & 8 deletions src/libretro/libretro_audio_stream.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,32 @@ LibretroAudioStream::LibretroAudioStream() = default;

LibretroAudioStream::~LibretroAudioStream() = default;

void LibretroAudioStream::UploadToFrontend()
{
std::array<SampleType, MaxSamples> output_buffer;
u32 total_samples = 0;
while (const auto num_samples = m_buffer.GetContiguousSize()) {
const auto write_pos = output_buffer.begin() + total_samples;
std::copy_n(m_buffer.GetReadPointer(), num_samples, write_pos);
m_buffer.Remove(num_samples);
total_samples += num_samples;
}
g_retro_audio_sample_batch_callback(output_buffer.data(), total_samples / AUDIO_CHANNELS);
}

void LibretroAudioStream::FramesAvailable()
{
for (;;)
if (!g_settings.audio_fast_hook)
{
const u32 num_samples = m_buffer.GetContiguousSize();
if (num_samples == 0)
break;
for (;;)
{
const u32 num_samples = m_buffer.GetContiguousSize();
if (num_samples == 0)
break;

const u32 num_frames = num_samples / AUDIO_CHANNELS;
g_retro_audio_sample_batch_callback(m_buffer.GetReadPointer(), num_frames);
m_buffer.Remove(num_samples);
const u32 num_frames = num_samples / AUDIO_CHANNELS;
g_retro_audio_sample_batch_callback(m_buffer.GetReadPointer(), num_frames);
m_buffer.Remove(num_samples);
}
}
}
}
2 changes: 2 additions & 0 deletions src/libretro/libretro_audio_stream.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ class LibretroAudioStream final : public AudioStream
LibretroAudioStream();
~LibretroAudioStream();

void UploadToFrontend();

protected:
void FramesAvailable() override;
};
13 changes: 13 additions & 0 deletions src/libretro/libretro_core_options.h
Original file line number Diff line number Diff line change
Expand Up @@ -2034,6 +2034,19 @@ struct retro_core_option_v2_definition option_defs_us[] = {
{NULL, NULL},
},
"false"},
{"swanstation_Audio_FastHook",
"Use Alternative Audio Hook (Restart)",
NULL,
"Use a faster and more efficient to submit audio samples to the frontend. Mostly safe to enable, but may hang "
"for a select few games that rely on the old method to function. Requires the core to be restarted to apply.",
NULL,
"advanced",
{
{"true", "Enabled"},
{"false", "Disabled"},
{NULL, NULL},
},
"true"},
{NULL, NULL, NULL, NULL, NULL, NULL, {{NULL, NULL}}, NULL},
};

Expand Down
12 changes: 12 additions & 0 deletions src/libretro/libretro_host_interface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -697,6 +697,12 @@ void LibretroHostInterface::retro_run_frame()
UpdateGeometry();

m_display->Render();

if (g_settings.audio_fast_hook)
{
auto* const audio_stream = dynamic_cast<LibretroAudioStream*>(m_audio_stream.get());
audio_stream->UploadToFrontend();
}
}

unsigned LibretroHostInterface::retro_get_region()
Expand Down Expand Up @@ -1153,6 +1159,12 @@ void LibretroHostInterface::UpdateSettings()

g_settings.gpu_use_software_renderer_for_readbacks = old_settings.gpu_use_software_renderer_for_readbacks;
}

if (g_settings.audio_fast_hook != old_settings.audio_fast_hook)
{
ReportFormattedMessage("Changing audio hook will apply on core reload.");
g_settings.audio_fast_hook = old_settings.audio_fast_hook;
}
}

CheckForSettingsChanges(old_settings);
Expand Down

1 comment on commit afb6bc7

@DarthMew
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Re-added 0d46e46 as an option.

Please sign in to comment.