Skip to content
Permalink
Browse files
Merge pull request #11165 from AdmiralCurtiss/audiocommon-pass-system
AudioCommon: Pass Core::System to AudioCommon functions.
  • Loading branch information
lioncash committed Nov 23, 2022
2 parents 1469055 + 1c63349 commit 0ef6d30
Show file tree
Hide file tree
Showing 8 changed files with 55 additions and 54 deletions.
@@ -45,7 +45,7 @@ static std::unique_ptr<SoundStream> CreateSoundStreamForBackend(std::string_view
return {};
}

void InitSoundStream()
void InitSoundStream(Core::System& system)
{
std::string backend = Config::Get(Config::MAIN_AUDIO_BACKEND);
std::unique_ptr<SoundStream> sound_stream = CreateSoundStreamForBackend(backend);
@@ -65,32 +65,28 @@ void InitSoundStream()
sound_stream->Init();
}

Core::System::GetInstance().SetSoundStream(std::move(sound_stream));
system.SetSoundStream(std::move(sound_stream));
}

void PostInitSoundStream()
void PostInitSoundStream(Core::System& system)
{
auto& system = Core::System::GetInstance();

// This needs to be called after AudioInterface::Init and SerialInterface::Init (for GBA devices)
// where input sample rates are set
UpdateSoundStream();
SetSoundStreamRunning(true);
UpdateSoundStream(system);
SetSoundStreamRunning(system, true);

if (Config::Get(Config::MAIN_DUMP_AUDIO) && !system.IsAudioDumpStarted())
StartAudioDump();
StartAudioDump(system);
}

void ShutdownSoundStream()
void ShutdownSoundStream(Core::System& system)
{
auto& system = Core::System::GetInstance();

INFO_LOG_FMT(AUDIO, "Shutting down sound stream");

if (Config::Get(Config::MAIN_DUMP_AUDIO) && system.IsAudioDumpStarted())
StopAudioDump();
StopAudioDump(system);

SetSoundStreamRunning(false);
SetSoundStreamRunning(system, false);
system.SetSoundStream(nullptr);

INFO_LOG_FMT(AUDIO, "Done shutting down sound stream");
@@ -161,9 +157,8 @@ bool SupportsVolumeChanges(std::string_view backend)
return backend == BACKEND_CUBEB || backend == BACKEND_OPENAL || backend == BACKEND_WASAPI;
}

void UpdateSoundStream()
void UpdateSoundStream(Core::System& system)
{
auto& system = Core::System::GetInstance();
SoundStream* sound_stream = system.GetSoundStream();

if (sound_stream)
@@ -173,9 +168,8 @@ void UpdateSoundStream()
}
}

void SetSoundStreamRunning(bool running)
void SetSoundStreamRunning(Core::System& system, bool running)
{
auto& system = Core::System::GetInstance();
SoundStream* sound_stream = system.GetSoundStream();

if (!sound_stream)
@@ -193,18 +187,17 @@ void SetSoundStreamRunning(bool running)
ERROR_LOG_FMT(AUDIO, "Error stopping stream.");
}

void SendAIBuffer(const short* samples, unsigned int num_samples)
void SendAIBuffer(Core::System& system, const short* samples, unsigned int num_samples)
{
auto& system = Core::System::GetInstance();
SoundStream* sound_stream = system.GetSoundStream();

if (!sound_stream)
return;

if (Config::Get(Config::MAIN_DUMP_AUDIO) && !system.IsAudioDumpStarted())
StartAudioDump();
StartAudioDump(system);
else if (!Config::Get(Config::MAIN_DUMP_AUDIO) && system.IsAudioDumpStarted())
StopAudioDump();
StopAudioDump(system);

Mixer* mixer = sound_stream->GetMixer();

@@ -214,9 +207,8 @@ void SendAIBuffer(const short* samples, unsigned int num_samples)
}
}

void StartAudioDump()
void StartAudioDump(Core::System& system)
{
auto& system = Core::System::GetInstance();
SoundStream* sound_stream = system.GetSoundStream();

std::time_t start_time = std::time(nullptr);
@@ -235,9 +227,8 @@ void StartAudioDump()
system.SetAudioDumpStarted(true);
}

void StopAudioDump()
void StopAudioDump(Core::System& system)
{
auto& system = Core::System::GetInstance();
SoundStream* sound_stream = system.GetSoundStream();

if (!sound_stream)
@@ -247,32 +238,32 @@ void StopAudioDump()
system.SetAudioDumpStarted(false);
}

void IncreaseVolume(unsigned short offset)
void IncreaseVolume(Core::System& system, unsigned short offset)
{
Config::SetBaseOrCurrent(Config::MAIN_AUDIO_MUTED, false);
int currentVolume = Config::Get(Config::MAIN_AUDIO_VOLUME);
currentVolume += offset;
if (currentVolume > AUDIO_VOLUME_MAX)
currentVolume = AUDIO_VOLUME_MAX;
Config::SetBaseOrCurrent(Config::MAIN_AUDIO_VOLUME, currentVolume);
UpdateSoundStream();
UpdateSoundStream(system);
}

void DecreaseVolume(unsigned short offset)
void DecreaseVolume(Core::System& system, unsigned short offset)
{
Config::SetBaseOrCurrent(Config::MAIN_AUDIO_MUTED, false);
int currentVolume = Config::Get(Config::MAIN_AUDIO_VOLUME);
currentVolume -= offset;
if (currentVolume < AUDIO_VOLUME_MIN)
currentVolume = AUDIO_VOLUME_MIN;
Config::SetBaseOrCurrent(Config::MAIN_AUDIO_VOLUME, currentVolume);
UpdateSoundStream();
UpdateSoundStream(system);
}

void ToggleMuteVolume()
void ToggleMuteVolume(Core::System& system)
{
bool isMuted = Config::Get(Config::MAIN_AUDIO_MUTED);
Config::SetBaseOrCurrent(Config::MAIN_AUDIO_MUTED, !isMuted);
UpdateSoundStream();
UpdateSoundStream(system);
}
} // namespace AudioCommon
@@ -13,23 +13,28 @@

class Mixer;

namespace Core
{
class System;
}

namespace AudioCommon
{
void InitSoundStream();
void PostInitSoundStream();
void ShutdownSoundStream();
void InitSoundStream(Core::System& system);
void PostInitSoundStream(Core::System& system);
void ShutdownSoundStream(Core::System& system);
std::string GetDefaultSoundBackend();
std::vector<std::string> GetSoundBackends();
DPL2Quality GetDefaultDPL2Quality();
bool SupportsDPL2Decoder(std::string_view backend);
bool SupportsLatencyControl(std::string_view backend);
bool SupportsVolumeChanges(std::string_view backend);
void UpdateSoundStream();
void SetSoundStreamRunning(bool running);
void SendAIBuffer(const short* samples, unsigned int num_samples);
void StartAudioDump();
void StopAudioDump();
void IncreaseVolume(unsigned short offset);
void DecreaseVolume(unsigned short offset);
void ToggleMuteVolume();
void UpdateSoundStream(Core::System& system);
void SetSoundStreamRunning(Core::System& system, bool running);
void SendAIBuffer(Core::System& system, const short* samples, unsigned int num_samples);
void StartAudioDump(Core::System& system);
void StopAudioDump(Core::System& system);
void IncreaseVolume(Core::System& system, unsigned short offset);
void DecreaseVolume(Core::System& system, unsigned short offset);
void ToggleMuteVolume(Core::System& system);
} // namespace AudioCommon
@@ -448,7 +448,7 @@ static void FifoPlayerThread(const std::optional<std::string>& savestate_path,
// See the BootManager.cpp file description for a complete call schedule.
static void EmuThread(std::unique_ptr<BootParameters> boot, WindowSystemInfo wsi)
{
const Core::System& system = Core::System::GetInstance();
Core::System& system = Core::System::GetInstance();
const SConfig& core_parameter = SConfig::GetInstance();
CallOnStateChangedCallbacks(State::Starting);
Common::ScopeGuard flag_guard{[] {
@@ -505,8 +505,8 @@ static void EmuThread(std::unique_ptr<BootParameters> boot, WindowSystemInfo wsi
Movie::Init(*boot);
Common::ScopeGuard movie_guard{&Movie::Shutdown};

AudioCommon::InitSoundStream();
Common::ScopeGuard audio_guard{&AudioCommon::ShutdownSoundStream};
AudioCommon::InitSoundStream(system);
Common::ScopeGuard audio_guard([&system] { AudioCommon::ShutdownSoundStream(system); });

HW::Init(NetPlay::IsNetPlayRunning() ? &(boot_session_data.GetNetplaySettings()->sram) : nullptr);

@@ -558,7 +558,7 @@ static void EmuThread(std::unique_ptr<BootParameters> boot, WindowSystemInfo wsi
// it's now ok to initialize any custom textures
HiresTexture::Update();

AudioCommon::PostInitSoundStream();
AudioCommon::PostInitSoundStream(system);

// The hardware is initialized.
s_hardware_initialized = true;
@@ -14,6 +14,7 @@
#include "Core/Host.h"
#include "Core/PowerPC/GDBStub.h"
#include "Core/PowerPC/PowerPC.h"
#include "Core/System.h"
#include "VideoCommon/Fifo.h"

namespace CPU
@@ -193,7 +194,7 @@ static void RunAdjacentSystems(bool running)
Fifo::EmulatorState(running);
// Core is responsible for shutting down the sound stream.
if (s_state != State::PowerDown)
AudioCommon::SetSoundStreamRunning(running);
AudioCommon::SetSoundStreamRunning(Core::System::GetInstance(), running);
}

void Stop()
@@ -414,7 +414,8 @@ void RegisterMMIO(MMIO::Mapping* mmio, u32 base)
mmio->Register(
base | AUDIO_DMA_CONTROL_LEN, MMIO::DirectRead<u16>(&state.audio_dma.AudioDMAControl.Hex),
MMIO::ComplexWrite<u16>([](u32, u16 val) {
auto& state = Core::System::GetInstance().GetDSPState().GetData();
auto& system = Core::System::GetInstance();
auto& state = system.GetDSPState().GetData();
bool already_enabled = state.audio_dma.AudioDMAControl.Enable;
state.audio_dma.AudioDMAControl.Hex = val;

@@ -522,7 +523,7 @@ void UpdateAudioDMA()
// external audio fifo in the emulator, to be mixed with the disc
// streaming output.
void* address = Memory::GetPointer(state.audio_dma.current_source_address);
AudioCommon::SendAIBuffer(reinterpret_cast<short*>(address), 8);
AudioCommon::SendAIBuffer(system, reinterpret_cast<short*>(address), 8);

if (state.audio_dma.remaining_blocks_count != 0)
{
@@ -540,7 +541,7 @@ void UpdateAudioDMA()
}
else
{
AudioCommon::SendAIBuffer(&zero_samples[0], 8);
AudioCommon::SendAIBuffer(system, &zero_samples[0], 8);
}
}

@@ -30,6 +30,7 @@
#include "Core/IOS/USB/Bluetooth/BTBase.h"
#include "Core/IOS/USB/Bluetooth/BTReal.h"
#include "Core/State.h"
#include "Core/System.h"
#include "Core/WiiUtils.h"

#ifdef HAS_LIBMGBA
@@ -353,7 +354,7 @@ void HotkeyScheduler::Run()

if (IsHotkey(HK_VOLUME_TOGGLE_MUTE))
{
AudioCommon::ToggleMuteVolume();
AudioCommon::ToggleMuteVolume(Core::System::GetInstance());
ShowVolume();
}

@@ -36,6 +36,7 @@
#include "Core/IOS/IOS.h"
#include "Core/NetPlayClient.h"
#include "Core/NetPlayServer.h"
#include "Core/System.h"

#include "DolphinQt/Host.h"
#include "DolphinQt/QtUtils/QueueOnObject.h"
@@ -384,13 +385,13 @@ void Settings::SetVolume(int volume)

void Settings::IncreaseVolume(int volume)
{
AudioCommon::IncreaseVolume(volume);
AudioCommon::IncreaseVolume(Core::System::GetInstance(), volume);
emit VolumeChanged(GetVolume());
}

void Settings::DecreaseVolume(int volume)
{
AudioCommon::DecreaseVolume(volume);
AudioCommon::DecreaseVolume(Core::System::GetInstance(), volume);
emit VolumeChanged(GetVolume());
}

@@ -23,6 +23,7 @@

#include "Core/Config/MainSettings.h"
#include "Core/Core.h"
#include "Core/System.h"

#include "DolphinQt/Config/SettingsWindow.h"
#include "DolphinQt/Settings.h"
@@ -328,7 +329,7 @@ void AudioPane::SaveSettings()
Config::SetBaseOrCurrent(Config::MAIN_WASAPI_DEVICE, device);
#endif

AudioCommon::UpdateSoundStream();
AudioCommon::UpdateSoundStream(Core::System::GetInstance());
}

void AudioPane::OnDspChanged()

0 comments on commit 0ef6d30

Please sign in to comment.