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