2015-05-23 22:55:12 -06:00
|
|
|
// Copyright 2009 Dolphin Emulator Project
|
2021-07-04 19:22:19 -06:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
2009-07-05 20:10:26 -06:00
|
|
|
|
2014-02-18 18:11:52 -07:00
|
|
|
#include "AudioCommon/AudioCommon.h"
|
2021-10-03 14:40:59 -06:00
|
|
|
|
|
|
|
#include <fmt/chrono.h>
|
|
|
|
#include <fmt/format.h>
|
|
|
|
|
2014-02-17 03:18:15 -07:00
|
|
|
#include "AudioCommon/AlsaSoundStream.h"
|
2017-03-22 17:09:59 -06:00
|
|
|
#include "AudioCommon/CubebStream.h"
|
2014-02-17 03:18:15 -07:00
|
|
|
#include "AudioCommon/Mixer.h"
|
|
|
|
#include "AudioCommon/NullSoundStream.h"
|
|
|
|
#include "AudioCommon/OpenALStream.h"
|
|
|
|
#include "AudioCommon/OpenSLESStream.h"
|
|
|
|
#include "AudioCommon/PulseAudioStream.h"
|
2018-02-10 13:03:27 -07:00
|
|
|
#include "AudioCommon/WASAPIStream.h"
|
2015-09-28 09:57:16 -06:00
|
|
|
#include "Common/Common.h"
|
2014-02-17 03:18:15 -07:00
|
|
|
#include "Common/FileUtil.h"
|
2015-09-26 15:13:07 -06:00
|
|
|
#include "Common/Logging/Log.h"
|
2021-10-13 18:29:04 -06:00
|
|
|
#include "Core/Config/MainSettings.h"
|
2014-02-17 03:18:15 -07:00
|
|
|
#include "Core/ConfigManager.h"
|
2022-08-31 13:44:21 -06:00
|
|
|
#include "Core/System.h"
|
2009-07-05 20:10:26 -06:00
|
|
|
|
2019-05-31 05:07:14 -06:00
|
|
|
namespace AudioCommon
|
|
|
|
{
|
|
|
|
constexpr int AUDIO_VOLUME_MIN = 0;
|
|
|
|
constexpr int AUDIO_VOLUME_MAX = 100;
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2019-09-14 15:16:13 -06:00
|
|
|
static std::unique_ptr<SoundStream> CreateSoundStreamForBackend(std::string_view backend)
|
2009-03-28 18:48:46 -06:00
|
|
|
{
|
2017-03-22 17:09:59 -06:00
|
|
|
if (backend == BACKEND_CUBEB)
|
2019-09-14 15:16:13 -06:00
|
|
|
return std::make_unique<CubebStream>();
|
2021-08-07 17:23:58 -06:00
|
|
|
else if (backend == BACKEND_OPENAL && OpenALStream::IsValid())
|
2019-09-14 15:16:13 -06:00
|
|
|
return std::make_unique<OpenALStream>();
|
2017-02-04 16:57:42 -07:00
|
|
|
else if (backend == BACKEND_NULLSOUND)
|
2019-09-14 15:16:13 -06:00
|
|
|
return std::make_unique<NullSound>();
|
2021-08-07 17:23:58 -06:00
|
|
|
else if (backend == BACKEND_ALSA && AlsaSound::IsValid())
|
2019-09-14 15:16:13 -06:00
|
|
|
return std::make_unique<AlsaSound>();
|
2021-08-07 17:23:58 -06:00
|
|
|
else if (backend == BACKEND_PULSEAUDIO && PulseAudio::IsValid())
|
2019-09-14 15:16:13 -06:00
|
|
|
return std::make_unique<PulseAudio>();
|
2021-08-07 17:23:58 -06:00
|
|
|
else if (backend == BACKEND_OPENSLES && OpenSLESStream::IsValid())
|
2019-09-14 15:16:13 -06:00
|
|
|
return std::make_unique<OpenSLESStream>();
|
2021-08-07 17:23:58 -06:00
|
|
|
else if (backend == BACKEND_WASAPI && WASAPIStream::IsValid())
|
2019-09-14 15:16:13 -06:00
|
|
|
return std::make_unique<WASAPIStream>();
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2022-10-13 13:01:55 -06:00
|
|
|
void InitSoundStream(Core::System& system)
|
2019-09-14 15:16:13 -06:00
|
|
|
{
|
2021-10-13 18:29:04 -06:00
|
|
|
std::string backend = Config::Get(Config::MAIN_AUDIO_BACKEND);
|
2022-08-31 13:44:21 -06:00
|
|
|
std::unique_ptr<SoundStream> sound_stream = CreateSoundStreamForBackend(backend);
|
2019-09-14 15:16:13 -06:00
|
|
|
|
2022-08-31 13:44:21 -06:00
|
|
|
if (!sound_stream)
|
2019-09-14 15:16:13 -06:00
|
|
|
{
|
2020-10-21 11:32:25 -06:00
|
|
|
WARN_LOG_FMT(AUDIO, "Unknown backend {}, using {} instead.", backend, GetDefaultSoundBackend());
|
2019-09-14 15:16:13 -06:00
|
|
|
backend = GetDefaultSoundBackend();
|
2022-08-31 13:44:21 -06:00
|
|
|
sound_stream = CreateSoundStreamForBackend(backend);
|
2019-09-14 15:16:13 -06:00
|
|
|
}
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2022-08-31 13:44:21 -06:00
|
|
|
if (!sound_stream || !sound_stream->Init())
|
2013-10-19 03:27:57 -06:00
|
|
|
{
|
2020-10-21 11:32:25 -06:00
|
|
|
WARN_LOG_FMT(AUDIO, "Could not initialize backend {}, using {} instead.", backend,
|
|
|
|
BACKEND_NULLSOUND);
|
2022-08-31 13:44:21 -06:00
|
|
|
sound_stream = std::make_unique<NullSound>();
|
|
|
|
sound_stream->Init();
|
2013-10-19 03:27:57 -06:00
|
|
|
}
|
2022-08-31 13:44:21 -06:00
|
|
|
|
2022-10-13 13:01:55 -06:00
|
|
|
system.SetSoundStream(std::move(sound_stream));
|
2021-03-21 17:06:44 -06:00
|
|
|
}
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2022-10-13 13:01:55 -06:00
|
|
|
void PostInitSoundStream(Core::System& system)
|
2021-03-21 17:06:44 -06:00
|
|
|
{
|
2021-07-04 05:15:37 -06:00
|
|
|
// This needs to be called after AudioInterface::Init and SerialInterface::Init (for GBA devices)
|
|
|
|
// where input sample rates are set
|
2022-10-13 13:01:55 -06:00
|
|
|
UpdateSoundStream(system);
|
|
|
|
SetSoundStreamRunning(system, true);
|
2017-04-20 03:04:25 -06:00
|
|
|
|
2022-08-31 13:44:21 -06:00
|
|
|
if (Config::Get(Config::MAIN_DUMP_AUDIO) && !system.IsAudioDumpStarted())
|
2022-10-13 13:01:55 -06:00
|
|
|
StartAudioDump(system);
|
2009-03-28 02:57:34 -06:00
|
|
|
}
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2022-10-13 13:01:55 -06:00
|
|
|
void ShutdownSoundStream(Core::System& system)
|
2009-03-28 18:48:46 -06:00
|
|
|
{
|
2020-10-21 11:32:25 -06:00
|
|
|
INFO_LOG_FMT(AUDIO, "Shutting down sound stream");
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2022-08-31 13:44:21 -06:00
|
|
|
if (Config::Get(Config::MAIN_DUMP_AUDIO) && system.IsAudioDumpStarted())
|
2022-10-13 13:01:55 -06:00
|
|
|
StopAudioDump(system);
|
2016-07-31 10:35:08 -06:00
|
|
|
|
2022-10-13 13:01:55 -06:00
|
|
|
SetSoundStreamRunning(system, false);
|
2022-08-31 13:44:21 -06:00
|
|
|
system.SetSoundStream(nullptr);
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2020-10-21 11:32:25 -06:00
|
|
|
INFO_LOG_FMT(AUDIO, "Done shutting down sound stream");
|
2009-03-28 02:57:34 -06:00
|
|
|
}
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2017-02-04 18:15:38 -07:00
|
|
|
std::string GetDefaultSoundBackend()
|
|
|
|
{
|
|
|
|
std::string backend = BACKEND_NULLSOUND;
|
2017-02-08 13:29:27 -07:00
|
|
|
#if defined ANDROID
|
|
|
|
backend = BACKEND_OPENSLES;
|
|
|
|
#elif defined __linux__
|
2021-08-07 17:23:58 -06:00
|
|
|
if (AlsaSound::IsValid())
|
2017-02-04 18:15:38 -07:00
|
|
|
backend = BACKEND_ALSA;
|
2019-04-08 06:27:48 -06:00
|
|
|
#elif defined(__APPLE__) || defined(_WIN32)
|
2017-06-26 00:56:11 -06:00
|
|
|
backend = BACKEND_CUBEB;
|
2017-02-04 18:15:38 -07:00
|
|
|
#endif
|
|
|
|
return backend;
|
|
|
|
}
|
|
|
|
|
2019-03-18 05:22:27 -06:00
|
|
|
DPL2Quality GetDefaultDPL2Quality()
|
|
|
|
{
|
|
|
|
return DPL2Quality::High;
|
|
|
|
}
|
|
|
|
|
2009-03-28 18:48:46 -06:00
|
|
|
std::vector<std::string> GetSoundBackends()
|
|
|
|
{
|
|
|
|
std::vector<std::string> backends;
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2019-05-31 05:03:52 -06:00
|
|
|
backends.emplace_back(BACKEND_NULLSOUND);
|
|
|
|
backends.emplace_back(BACKEND_CUBEB);
|
2021-08-07 17:23:58 -06:00
|
|
|
if (AlsaSound::IsValid())
|
2019-05-31 05:03:52 -06:00
|
|
|
backends.emplace_back(BACKEND_ALSA);
|
2021-08-07 17:23:58 -06:00
|
|
|
if (PulseAudio::IsValid())
|
2019-05-31 05:03:52 -06:00
|
|
|
backends.emplace_back(BACKEND_PULSEAUDIO);
|
2021-08-07 17:23:58 -06:00
|
|
|
if (OpenALStream::IsValid())
|
2019-05-31 05:03:52 -06:00
|
|
|
backends.emplace_back(BACKEND_OPENAL);
|
2021-08-07 17:23:58 -06:00
|
|
|
if (OpenSLESStream::IsValid())
|
2019-05-31 05:03:52 -06:00
|
|
|
backends.emplace_back(BACKEND_OPENSLES);
|
2021-08-07 17:23:58 -06:00
|
|
|
if (WASAPIStream::IsValid())
|
2019-05-31 05:03:52 -06:00
|
|
|
backends.emplace_back(BACKEND_WASAPI);
|
2018-02-10 13:03:27 -07:00
|
|
|
|
2009-03-28 18:48:46 -06:00
|
|
|
return backends;
|
|
|
|
}
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2019-05-31 05:01:44 -06:00
|
|
|
bool SupportsDPL2Decoder(std::string_view backend)
|
2016-10-09 20:18:16 -06:00
|
|
|
{
|
|
|
|
#ifndef __APPLE__
|
|
|
|
if (backend == BACKEND_OPENAL)
|
|
|
|
return true;
|
|
|
|
#endif
|
2017-03-22 17:09:59 -06:00
|
|
|
if (backend == BACKEND_CUBEB)
|
|
|
|
return true;
|
2016-10-09 20:18:16 -06:00
|
|
|
if (backend == BACKEND_PULSEAUDIO)
|
|
|
|
return true;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-05-31 05:01:44 -06:00
|
|
|
bool SupportsLatencyControl(std::string_view backend)
|
2016-10-09 20:18:16 -06:00
|
|
|
{
|
2018-02-10 13:03:27 -07:00
|
|
|
return backend == BACKEND_OPENAL || backend == BACKEND_WASAPI;
|
2016-10-09 20:18:16 -06:00
|
|
|
}
|
|
|
|
|
2019-05-31 05:01:44 -06:00
|
|
|
bool SupportsVolumeChanges(std::string_view backend)
|
2016-10-09 20:18:16 -06:00
|
|
|
{
|
|
|
|
// FIXME: this one should ask the backend whether it supports it.
|
|
|
|
// but getting the backend from string etc. is probably
|
|
|
|
// too much just to enable/disable a stupid slider...
|
2019-09-14 15:08:08 -06:00
|
|
|
return backend == BACKEND_CUBEB || backend == BACKEND_OPENAL || backend == BACKEND_WASAPI;
|
2016-10-09 20:18:16 -06:00
|
|
|
}
|
|
|
|
|
2022-10-13 13:01:55 -06:00
|
|
|
void UpdateSoundStream(Core::System& system)
|
2013-01-16 18:16:56 -07:00
|
|
|
{
|
2022-08-31 13:44:21 -06:00
|
|
|
SoundStream* sound_stream = system.GetSoundStream();
|
|
|
|
|
|
|
|
if (sound_stream)
|
2013-01-16 18:16:56 -07:00
|
|
|
{
|
2021-10-13 18:29:04 -06:00
|
|
|
int volume = Config::Get(Config::MAIN_AUDIO_MUTED) ? 0 : Config::Get(Config::MAIN_AUDIO_VOLUME);
|
2022-08-31 13:44:21 -06:00
|
|
|
sound_stream->SetVolume(volume);
|
2013-01-16 18:16:56 -07:00
|
|
|
}
|
|
|
|
}
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2022-10-13 13:01:55 -06:00
|
|
|
void SetSoundStreamRunning(Core::System& system, bool running)
|
2014-03-27 18:56:05 -06:00
|
|
|
{
|
2022-08-31 13:44:21 -06:00
|
|
|
SoundStream* sound_stream = system.GetSoundStream();
|
|
|
|
|
|
|
|
if (!sound_stream)
|
2017-10-21 17:23:40 -06:00
|
|
|
return;
|
|
|
|
|
2022-08-31 13:44:21 -06:00
|
|
|
if (system.IsSoundStreamRunning() == running)
|
2017-10-21 16:12:05 -06:00
|
|
|
return;
|
2022-08-31 13:44:21 -06:00
|
|
|
system.SetSoundStreamRunning(running);
|
2017-10-21 16:12:05 -06:00
|
|
|
|
2022-08-31 13:44:21 -06:00
|
|
|
if (sound_stream->SetRunning(running))
|
2017-10-21 17:23:40 -06:00
|
|
|
return;
|
|
|
|
if (running)
|
2020-10-21 11:32:25 -06:00
|
|
|
ERROR_LOG_FMT(AUDIO, "Error starting stream.");
|
2017-10-21 17:23:40 -06:00
|
|
|
else
|
2020-10-21 11:32:25 -06:00
|
|
|
ERROR_LOG_FMT(AUDIO, "Error stopping stream.");
|
2014-03-27 18:56:05 -06:00
|
|
|
}
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2022-10-13 13:01:55 -06:00
|
|
|
void SendAIBuffer(Core::System& system, const short* samples, unsigned int num_samples)
|
2014-03-27 18:56:05 -06:00
|
|
|
{
|
2022-08-31 13:44:21 -06:00
|
|
|
SoundStream* sound_stream = system.GetSoundStream();
|
|
|
|
|
|
|
|
if (!sound_stream)
|
2014-03-27 18:56:05 -06:00
|
|
|
return;
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2022-08-31 13:44:21 -06:00
|
|
|
if (Config::Get(Config::MAIN_DUMP_AUDIO) && !system.IsAudioDumpStarted())
|
2022-10-13 13:01:55 -06:00
|
|
|
StartAudioDump(system);
|
2022-08-31 13:44:21 -06:00
|
|
|
else if (!Config::Get(Config::MAIN_DUMP_AUDIO) && system.IsAudioDumpStarted())
|
2022-10-13 13:01:55 -06:00
|
|
|
StopAudioDump(system);
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2022-08-31 13:44:21 -06:00
|
|
|
Mixer* mixer = sound_stream->GetMixer();
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2022-08-31 13:44:21 -06:00
|
|
|
if (mixer && samples)
|
2014-03-27 18:56:05 -06:00
|
|
|
{
|
2022-08-31 13:44:21 -06:00
|
|
|
mixer->PushSamples(samples, num_samples);
|
2014-03-27 18:56:05 -06:00
|
|
|
}
|
|
|
|
}
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2022-10-13 13:01:55 -06:00
|
|
|
void StartAudioDump(Core::System& system)
|
2014-10-15 18:03:31 -06:00
|
|
|
{
|
2022-08-31 13:44:21 -06:00
|
|
|
SoundStream* sound_stream = system.GetSoundStream();
|
|
|
|
|
2021-10-03 14:40:59 -06:00
|
|
|
std::time_t start_time = std::time(nullptr);
|
|
|
|
|
|
|
|
std::string path_prefix = File::GetUserPath(D_DUMPAUDIO_IDX) + SConfig::GetInstance().GetGameID();
|
|
|
|
|
|
|
|
std::string base_name =
|
2021-10-15 14:49:13 -06:00
|
|
|
fmt::format("{}_{:%Y-%m-%d_%H-%M-%S}", path_prefix, fmt::localtime(start_time));
|
2021-10-03 14:40:59 -06:00
|
|
|
|
|
|
|
const std::string audio_file_name_dtk = fmt::format("{}_dtkdump.wav", base_name);
|
|
|
|
const std::string audio_file_name_dsp = fmt::format("{}_dspdump.wav", base_name);
|
2014-10-15 18:03:31 -06:00
|
|
|
File::CreateFullPath(audio_file_name_dtk);
|
|
|
|
File::CreateFullPath(audio_file_name_dsp);
|
2022-08-31 13:44:21 -06:00
|
|
|
sound_stream->GetMixer()->StartLogDTKAudio(audio_file_name_dtk);
|
|
|
|
sound_stream->GetMixer()->StartLogDSPAudio(audio_file_name_dsp);
|
|
|
|
system.SetAudioDumpStarted(true);
|
2014-10-15 18:03:31 -06:00
|
|
|
}
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2022-10-13 13:01:55 -06:00
|
|
|
void StopAudioDump(Core::System& system)
|
2014-10-15 18:03:31 -06:00
|
|
|
{
|
2022-08-31 13:44:21 -06:00
|
|
|
SoundStream* sound_stream = system.GetSoundStream();
|
|
|
|
|
|
|
|
if (!sound_stream)
|
2017-10-21 17:23:40 -06:00
|
|
|
return;
|
2022-08-31 13:44:21 -06:00
|
|
|
sound_stream->GetMixer()->StopLogDTKAudio();
|
|
|
|
sound_stream->GetMixer()->StopLogDSPAudio();
|
|
|
|
system.SetAudioDumpStarted(false);
|
2014-10-15 18:03:31 -06:00
|
|
|
}
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2022-10-13 13:01:55 -06:00
|
|
|
void IncreaseVolume(Core::System& system, unsigned short offset)
|
2014-12-28 14:03:21 -07:00
|
|
|
{
|
2021-10-13 18:29:04 -06:00
|
|
|
Config::SetBaseOrCurrent(Config::MAIN_AUDIO_MUTED, false);
|
|
|
|
int currentVolume = Config::Get(Config::MAIN_AUDIO_VOLUME);
|
2014-12-28 14:03:21 -07:00
|
|
|
currentVolume += offset;
|
|
|
|
if (currentVolume > AUDIO_VOLUME_MAX)
|
|
|
|
currentVolume = AUDIO_VOLUME_MAX;
|
2021-10-13 18:29:04 -06:00
|
|
|
Config::SetBaseOrCurrent(Config::MAIN_AUDIO_VOLUME, currentVolume);
|
2022-10-13 13:01:55 -06:00
|
|
|
UpdateSoundStream(system);
|
2014-12-28 14:03:21 -07:00
|
|
|
}
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2022-10-13 13:01:55 -06:00
|
|
|
void DecreaseVolume(Core::System& system, unsigned short offset)
|
2014-12-28 14:03:21 -07:00
|
|
|
{
|
2021-10-13 18:29:04 -06:00
|
|
|
Config::SetBaseOrCurrent(Config::MAIN_AUDIO_MUTED, false);
|
|
|
|
int currentVolume = Config::Get(Config::MAIN_AUDIO_VOLUME);
|
2014-12-28 14:03:21 -07:00
|
|
|
currentVolume -= offset;
|
|
|
|
if (currentVolume < AUDIO_VOLUME_MIN)
|
|
|
|
currentVolume = AUDIO_VOLUME_MIN;
|
2021-10-13 18:29:04 -06:00
|
|
|
Config::SetBaseOrCurrent(Config::MAIN_AUDIO_VOLUME, currentVolume);
|
2022-10-13 13:01:55 -06:00
|
|
|
UpdateSoundStream(system);
|
2014-12-28 14:03:21 -07:00
|
|
|
}
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2022-10-13 13:01:55 -06:00
|
|
|
void ToggleMuteVolume(Core::System& system)
|
2014-12-28 14:03:21 -07:00
|
|
|
{
|
2021-10-13 18:29:04 -06:00
|
|
|
bool isMuted = Config::Get(Config::MAIN_AUDIO_MUTED);
|
|
|
|
Config::SetBaseOrCurrent(Config::MAIN_AUDIO_MUTED, !isMuted);
|
2022-10-13 13:01:55 -06:00
|
|
|
UpdateSoundStream(system);
|
2014-12-28 14:03:21 -07:00
|
|
|
}
|
2018-02-10 13:03:27 -07:00
|
|
|
} // namespace AudioCommon
|