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"
|
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"
|
2014-02-17 03:18:15 -07:00
|
|
|
#include "Core/ConfigManager.h"
|
2013-01-16 18:16:56 -07:00
|
|
|
|
|
|
|
// This shouldn't be a global, at least not here.
|
2016-07-31 10:35:08 -06:00
|
|
|
std::unique_ptr<SoundStream> g_sound_stream;
|
2009-07-05 20:10:26 -06:00
|
|
|
|
2019-05-31 05:07:14 -06:00
|
|
|
namespace AudioCommon
|
|
|
|
{
|
2014-10-15 18:03:31 -06:00
|
|
|
static bool s_audio_dump_start = false;
|
2017-10-21 16:12:05 -06:00
|
|
|
static bool s_sound_stream_running = false;
|
2014-10-15 18:03:31 -06:00
|
|
|
|
2019-05-31 05:07:14 -06:00
|
|
|
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 {};
|
|
|
|
}
|
|
|
|
|
|
|
|
void InitSoundStream()
|
|
|
|
{
|
|
|
|
std::string backend = SConfig::GetInstance().sBackend;
|
|
|
|
g_sound_stream = CreateSoundStreamForBackend(backend);
|
|
|
|
|
|
|
|
if (!g_sound_stream)
|
|
|
|
{
|
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();
|
|
|
|
g_sound_stream = CreateSoundStreamForBackend(GetDefaultSoundBackend());
|
|
|
|
}
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2017-10-21 17:23:40 -06:00
|
|
|
if (!g_sound_stream || !g_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);
|
2016-07-31 10:35:08 -06:00
|
|
|
g_sound_stream = std::make_unique<NullSound>();
|
2019-10-09 08:09:16 -06:00
|
|
|
g_sound_stream->Init();
|
2013-10-19 03:27:57 -06:00
|
|
|
}
|
2021-03-21 17:06:44 -06:00
|
|
|
}
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2021-03-21 17:06:44 -06:00
|
|
|
void PostInitSoundStream()
|
|
|
|
{
|
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
|
2017-04-20 03:04:25 -06:00
|
|
|
UpdateSoundStream();
|
2017-10-21 17:23:40 -06:00
|
|
|
SetSoundStreamRunning(true);
|
2017-04-20 03:04:25 -06:00
|
|
|
|
2016-07-31 10:35:08 -06:00
|
|
|
if (SConfig::GetInstance().m_DumpAudio && !s_audio_dump_start)
|
|
|
|
StartAudioDump();
|
2009-03-28 02:57:34 -06:00
|
|
|
}
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2009-03-28 18:48:46 -06:00
|
|
|
void ShutdownSoundStream()
|
|
|
|
{
|
2020-10-21 11:32:25 -06:00
|
|
|
INFO_LOG_FMT(AUDIO, "Shutting down sound stream");
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2017-10-21 17:23:40 -06:00
|
|
|
if (SConfig::GetInstance().m_DumpAudio && s_audio_dump_start)
|
|
|
|
StopAudioDump();
|
2016-07-31 10:35:08 -06:00
|
|
|
|
2017-10-21 17:23:40 -06:00
|
|
|
SetSoundStreamRunning(false);
|
|
|
|
g_sound_stream.reset();
|
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
|
|
|
}
|
|
|
|
|
2013-01-16 18:16:56 -07:00
|
|
|
void UpdateSoundStream()
|
|
|
|
{
|
2014-09-29 19:35:57 -06:00
|
|
|
if (g_sound_stream)
|
2013-01-16 18:16:56 -07:00
|
|
|
{
|
2014-12-28 14:03:21 -07:00
|
|
|
int volume = SConfig::GetInstance().m_IsMuted ? 0 : SConfig::GetInstance().m_Volume;
|
|
|
|
g_sound_stream->SetVolume(volume);
|
2013-01-16 18:16:56 -07:00
|
|
|
}
|
|
|
|
}
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2017-10-21 13:35:58 -06:00
|
|
|
void SetSoundStreamRunning(bool running)
|
2014-03-27 18:56:05 -06:00
|
|
|
{
|
2017-10-21 17:23:40 -06:00
|
|
|
if (!g_sound_stream)
|
|
|
|
return;
|
|
|
|
|
2017-10-21 16:12:05 -06:00
|
|
|
if (s_sound_stream_running == running)
|
|
|
|
return;
|
|
|
|
s_sound_stream_running = running;
|
|
|
|
|
2017-10-21 17:23:40 -06:00
|
|
|
if (g_sound_stream->SetRunning(running))
|
|
|
|
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
|
|
|
|
2016-07-31 17:14:20 -06:00
|
|
|
void SendAIBuffer(const short* samples, unsigned int num_samples)
|
2014-03-27 18:56:05 -06:00
|
|
|
{
|
2014-09-29 19:35:57 -06:00
|
|
|
if (!g_sound_stream)
|
2014-03-27 18:56:05 -06:00
|
|
|
return;
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2014-10-15 18:03:31 -06:00
|
|
|
if (SConfig::GetInstance().m_DumpAudio && !s_audio_dump_start)
|
|
|
|
StartAudioDump();
|
|
|
|
else if (!SConfig::GetInstance().m_DumpAudio && s_audio_dump_start)
|
|
|
|
StopAudioDump();
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2017-06-26 15:41:12 -06:00
|
|
|
Mixer* pMixer = g_sound_stream->GetMixer();
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2014-03-27 18:56:05 -06:00
|
|
|
if (pMixer && samples)
|
|
|
|
{
|
|
|
|
pMixer->PushSamples(samples, num_samples);
|
|
|
|
}
|
|
|
|
}
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2014-10-15 18:03:31 -06:00
|
|
|
void StartAudioDump()
|
|
|
|
{
|
|
|
|
std::string audio_file_name_dtk = File::GetUserPath(D_DUMPAUDIO_IDX) + "dtkdump.wav";
|
|
|
|
std::string audio_file_name_dsp = File::GetUserPath(D_DUMPAUDIO_IDX) + "dspdump.wav";
|
|
|
|
File::CreateFullPath(audio_file_name_dtk);
|
|
|
|
File::CreateFullPath(audio_file_name_dsp);
|
|
|
|
g_sound_stream->GetMixer()->StartLogDTKAudio(audio_file_name_dtk);
|
|
|
|
g_sound_stream->GetMixer()->StartLogDSPAudio(audio_file_name_dsp);
|
|
|
|
s_audio_dump_start = true;
|
|
|
|
}
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2014-10-15 18:03:31 -06:00
|
|
|
void StopAudioDump()
|
|
|
|
{
|
2017-10-21 17:23:40 -06:00
|
|
|
if (!g_sound_stream)
|
|
|
|
return;
|
2014-10-15 18:03:31 -06:00
|
|
|
g_sound_stream->GetMixer()->StopLogDTKAudio();
|
|
|
|
g_sound_stream->GetMixer()->StopLogDSPAudio();
|
|
|
|
s_audio_dump_start = false;
|
|
|
|
}
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2014-12-28 14:03:21 -07:00
|
|
|
void IncreaseVolume(unsigned short offset)
|
|
|
|
{
|
|
|
|
SConfig::GetInstance().m_IsMuted = false;
|
|
|
|
int& currentVolume = SConfig::GetInstance().m_Volume;
|
|
|
|
currentVolume += offset;
|
|
|
|
if (currentVolume > AUDIO_VOLUME_MAX)
|
|
|
|
currentVolume = AUDIO_VOLUME_MAX;
|
|
|
|
UpdateSoundStream();
|
|
|
|
}
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2014-12-28 14:03:21 -07:00
|
|
|
void DecreaseVolume(unsigned short offset)
|
|
|
|
{
|
|
|
|
SConfig::GetInstance().m_IsMuted = false;
|
|
|
|
int& currentVolume = SConfig::GetInstance().m_Volume;
|
|
|
|
currentVolume -= offset;
|
|
|
|
if (currentVolume < AUDIO_VOLUME_MIN)
|
|
|
|
currentVolume = AUDIO_VOLUME_MIN;
|
|
|
|
UpdateSoundStream();
|
|
|
|
}
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2014-12-28 14:03:21 -07:00
|
|
|
void ToggleMuteVolume()
|
|
|
|
{
|
|
|
|
bool& isMuted = SConfig::GetInstance().m_IsMuted;
|
|
|
|
isMuted = !isMuted;
|
|
|
|
UpdateSoundStream();
|
|
|
|
}
|
2018-02-10 13:03:27 -07:00
|
|
|
} // namespace AudioCommon
|