Debian Codegraph @

Source/Core/AudioCommon/AudioCommon.cpp

raw symbols SCIP
1 // Copyright 2009 Dolphin Emulator Project
2 // SPDX-License-Identifier: GPL-2.0-or-later
3
4 #include "AudioCommon/AudioCommon.h"
5
6 #include <fmt/chrono.h>
7 #include <fmt/format.h>
8
9 #include "AudioCommon/AlsaSoundStream.h"
10 #include "AudioCommon/CubebStream.h"
11 #include "AudioCommon/Mixer.h"
12 #include "AudioCommon/NullSoundStream.h"
13 #include "AudioCommon/OpenALStream.h"
14 #include "AudioCommon/OpenSLESStream.h"
15 #include "AudioCommon/PulseAudioStream.h"
16 #include "AudioCommon/WASAPIStream.h"
17 #include "Common/FileUtil.h"
18 #include "Common/Logging/Log.h"
19 #include "Common/TimeUtil.h"
20 #include "Core/Config/MainSettings.h"
21 #include "Core/ConfigManager.h"
22 #include "Core/System.h"
23
24 namespace AudioCommon
25 {
26 constexpr int AUDIO_VOLUME_MIN = 0;
27 constexpr int AUDIO_VOLUME_MAX = 100;
28
29 static std::unique_ptr<SoundStream> CreateSoundStreamForBackend(std::string_view backend)
30 {
31 if (backend == BACKEND_CUBEB && CubebStream::IsValid())
32 return std::make_unique<CubebStream>();
33 else if (backend == BACKEND_OPENAL && OpenALStream::IsValid())
34 return std::make_unique<OpenALStream>();
35 else if (backend == BACKEND_NULLSOUND)
36 return std::make_unique<NullSound>();
37 else if (backend == BACKEND_ALSA && AlsaSound::IsValid())
38 return std::make_unique<AlsaSound>();
39 else if (backend == BACKEND_PULSEAUDIO && PulseAudio::IsValid())
40 return std::make_unique<PulseAudio>();
41 else if (backend == BACKEND_OPENSLES && OpenSLESStream::IsValid())
42 return std::make_unique<OpenSLESStream>();
43 else if (backend == BACKEND_WASAPI && WASAPIStream::IsValid())
44 return std::make_unique<WASAPIStream>();
45 return {};
46 }
47
48 void InitSoundStream(Core::System& system)
49 {
50 std::string backend = Config::Get(Config::MAIN_AUDIO_BACKEND);
51 std::unique_ptr<SoundStream> sound_stream = CreateSoundStreamForBackend(backend);
52
53 if (!sound_stream)
54 {
55 WARN_LOG_FMT(AUDIO, "Unknown backend {}, using {} instead.", backend, GetDefaultSoundBackend());
56 backend = GetDefaultSoundBackend();
57 sound_stream = CreateSoundStreamForBackend(backend);
58 }
59
60 if (!sound_stream || !sound_stream->Init())
61 {
62 WARN_LOG_FMT(AUDIO, "Could not initialize backend {}, using {} instead.", backend,
63 BACKEND_NULLSOUND);
64 sound_stream = std::make_unique<NullSound>();
65 sound_stream->Init();
66 }
67
68 system.SetSoundStream(std::move(sound_stream));
69 }
70
71 void PostInitSoundStream(Core::System& system)
72 {
73 // This needs to be called after AudioInterface::Init and SerialInterface::Init (for GBA devices)
74 // where input sample rates are set
75 UpdateSoundStream(system);
76 SetSoundStreamRunning(system, true);
77
78 if (Config::Get(Config::MAIN_DUMP_AUDIO) && !system.IsAudioDumpStarted())
79 StartAudioDump(system);
80 }
81
82 void ShutdownSoundStream(Core::System& system)
83 {
84 INFO_LOG_FMT(AUDIO, "Shutting down sound stream");
85
86 if (Config::Get(Config::MAIN_DUMP_AUDIO) && system.IsAudioDumpStarted())
87 StopAudioDump(system);
88
89 SetSoundStreamRunning(system, false);
90 system.SetSoundStream(nullptr);
91
92 INFO_LOG_FMT(AUDIO, "Done shutting down sound stream");
93 }
94
95 std::string GetDefaultSoundBackend()
96 {
97 #if defined(ANDROID)
98 return BACKEND_OPENSLES;
99 #else
100 if (CubebStream::IsValid())
101 return BACKEND_CUBEB;
102 #endif
103
104 #if defined(__linux__)
105 if (AlsaSound::IsValid())
106 return BACKEND_ALSA;
107 #endif
108
109 return BACKEND_NULLSOUND;
110 }
111
112 DPL2Quality GetDefaultDPL2Quality()
113 {
114 return DPL2Quality::High;
115 }
116
117 std::vector<std::string> GetSoundBackends()
118 {
119 std::vector<std::string> backends;
120
121 backends.emplace_back(BACKEND_NULLSOUND);
122 if (CubebStream::IsValid())
123 backends.emplace_back(BACKEND_CUBEB);
124 if (AlsaSound::IsValid())
125 backends.emplace_back(BACKEND_ALSA);
126 if (PulseAudio::IsValid())
127 backends.emplace_back(BACKEND_PULSEAUDIO);
128 if (OpenALStream::IsValid())
129 backends.emplace_back(BACKEND_OPENAL);
130 if (OpenSLESStream::IsValid())
131 backends.emplace_back(BACKEND_OPENSLES);
132 if (WASAPIStream::IsValid())
133 backends.emplace_back(BACKEND_WASAPI);
134
135 return backends;
136 }
137
138 bool SupportsDPL2Decoder(std::string_view backend)
139 {
140 #ifndef __APPLE__
141 if (backend == BACKEND_OPENAL)
142 return true;
143 #endif
144 if (backend == BACKEND_CUBEB)
145 return true;
146 if (backend == BACKEND_PULSEAUDIO)
147 return true;
148 return false;
149 }
150
151 bool SupportsLatencyControl(std::string_view backend)
152 {
153 return backend == BACKEND_OPENAL || backend == BACKEND_WASAPI;
154 }
155
156 bool SupportsVolumeChanges(std::string_view backend)
157 {
158 // FIXME: this one should ask the backend whether it supports it.
159 // but getting the backend from string etc. is probably
160 // too much just to enable/disable a stupid slider...
161 return backend == BACKEND_CUBEB || backend == BACKEND_OPENAL || backend == BACKEND_WASAPI;
162 }
163
164 void UpdateSoundStream(Core::System& system)
165 {
166 SoundStream* sound_stream = system.GetSoundStream();
167
168 if (sound_stream)
169 {
170 const int volume =
171 Config::Get(Config::MAIN_AUDIO_MUTED) ? 0 : Config::Get(Config::MAIN_AUDIO_VOLUME);
172 sound_stream->SetVolume(volume);
173 }
174 }
175
176 void SetSoundStreamRunning(Core::System& system, bool running)
177 {
178 SoundStream* sound_stream = system.GetSoundStream();
179
180 if (!sound_stream)
181 return;
182
183 if (system.IsSoundStreamRunning() == running)
184 return;
185 system.SetSoundStreamRunning(running);
186
187 if (sound_stream->SetRunning(running))
188 return;
189 if (running)
190 ERROR_LOG_FMT(AUDIO, "Error starting stream.");
191 else
192 ERROR_LOG_FMT(AUDIO, "Error stopping stream.");
193 }
194
195 void SendAIBuffer(Core::System& system, const short* samples, unsigned int num_samples)
196 {
197 const SoundStream* const sound_stream = system.GetSoundStream();
198
199 if (!sound_stream)
200 return;
201
202 if (Config::Get(Config::MAIN_DUMP_AUDIO) && !system.IsAudioDumpStarted())
203 StartAudioDump(system);
204 else if (!Config::Get(Config::MAIN_DUMP_AUDIO) && system.IsAudioDumpStarted())
205 StopAudioDump(system);
206
207 Mixer* mixer = sound_stream->GetMixer();
208
209 if (mixer && samples)
210 {
211 mixer->PushSamples(samples, num_samples);
212 }
213 }
214
215 void StartAudioDump(Core::System& system)
216 {
217 const SoundStream* const sound_stream = system.GetSoundStream();
218
219 const std::time_t start_time = std::time(nullptr);
220
221 std::string path_prefix = File::GetUserPath(D_DUMPAUDIO_IDX) + SConfig::GetInstance().GetGameID();
222
223 const auto local_time = Common::LocalTime(start_time);
224 if (!local_time)
225 return;
226
227 std::string base_name = fmt::format("{}_{:%Y-%m-%d_%H-%M-%S}", path_prefix, *local_time);
228
229 const std::string audio_file_name_dtk = fmt::format("{}_dtkdump.wav", base_name);
230 const std::string audio_file_name_dsp = fmt::format("{}_dspdump.wav", base_name);
231 File::CreateFullPath(audio_file_name_dtk);
232 File::CreateFullPath(audio_file_name_dsp);
233 sound_stream->GetMixer()->StartLogDTKAudio(audio_file_name_dtk);
234 sound_stream->GetMixer()->StartLogDSPAudio(audio_file_name_dsp);
235 system.SetAudioDumpStarted(true);
236 }
237
238 void StopAudioDump(Core::System& system)
239 {
240 const SoundStream* const sound_stream = system.GetSoundStream();
241
242 if (!sound_stream)
243 return;
244 sound_stream->GetMixer()->StopLogDTKAudio();
245 sound_stream->GetMixer()->StopLogDSPAudio();
246 system.SetAudioDumpStarted(false);
247 }
248
249 void IncreaseVolume(Core::System& system, unsigned short offset)
250 {
251 Config::SetBaseOrCurrent(Config::MAIN_AUDIO_MUTED, false);
252 int currentVolume = Config::Get(Config::MAIN_AUDIO_VOLUME);
253 currentVolume += offset;
254 if (currentVolume > AUDIO_VOLUME_MAX)
255 currentVolume = AUDIO_VOLUME_MAX;
256 Config::SetBaseOrCurrent(Config::MAIN_AUDIO_VOLUME, currentVolume);
257 UpdateSoundStream(system);
258 }
259
260 void DecreaseVolume(Core::System& system, unsigned short offset)
261 {
262 Config::SetBaseOrCurrent(Config::MAIN_AUDIO_MUTED, false);
263 int currentVolume = Config::Get(Config::MAIN_AUDIO_VOLUME);
264 currentVolume -= offset;
265 if (currentVolume < AUDIO_VOLUME_MIN)
266 currentVolume = AUDIO_VOLUME_MIN;
267 Config::SetBaseOrCurrent(Config::MAIN_AUDIO_VOLUME, currentVolume);
268 UpdateSoundStream(system);
269 }
270
271 void ToggleMuteVolume(Core::System& system)
272 {
273 const bool isMuted = Config::Get(Config::MAIN_AUDIO_MUTED);
274 Config::SetBaseOrCurrent(Config::MAIN_AUDIO_MUTED, !isMuted);
275 UpdateSoundStream(system);
276 }
277 } // namespace AudioCommon