Debian Codegraph @

Source/Core/AudioCommon/CubebStream.cpp

raw symbols SCIP
1 // Copyright 2017 Dolphin Emulator Project
2 // SPDX-License-Identifier: GPL-2.0-or-later
3
4 #include "AudioCommon/CubebStream.h"
5
6 #include <cubeb/cubeb.h>
7
8 #include "AudioCommon/CubebUtils.h"
9 #include "Common/CommonTypes.h"
10 #include "Common/Logging/Log.h"
11 #include "Core/Config/MainSettings.h"
12 #include "Core/System.h"
13
14 #ifdef _WIN32
15 #include <Objbase.h>
16 #endif
17
18 // ~10 ms - needs to be at least 240 for surround
19 constexpr u32 BUFFER_SAMPLES = 512;
20
21 long CubebStream::DataCallback(cubeb_stream* stream, void* user_data, const void* /*input_buffer*/,
22 void* output_buffer, long num_frames)
23 {
24 const auto* const self = static_cast<CubebStream*>(user_data);
25
26 if (self->m_stereo)
27 self->m_mixer->Mix(static_cast<short*>(output_buffer), num_frames);
28 else
29 self->m_mixer->MixSurround(static_cast<float*>(output_buffer), num_frames);
30
31 return num_frames;
32 }
33
34 void CubebStream::StateCallback(cubeb_stream* stream, void* user_data, cubeb_state state)
35 {
36 }
37
38 long CubebStream::WiimoteDataCallback(cubeb_stream* stream, void* user_data,
39 const void* /*input_buffer*/, void* output_buffer,
40 long num_frames)
41 {
42 const auto* data = static_cast<const WiimoteStreamData*>(user_data);
43 data->self->m_mixer->MixWiimoteSpeaker(data->wiimote_index, static_cast<short*>(output_buffer),
44 num_frames);
45 return num_frames;
46 }
47
48 void CubebStream::WiimoteStateCallback(cubeb_stream* stream, void* user_data, cubeb_state state)
49 {
50 }
51
52 CubebStream::CubebStream()
53 #ifdef _WIN32
54 : m_work_queue("Cubeb Worker")
55 {
56 m_work_queue.PushBlocking([this] {
57 const auto result = CoInitializeEx(nullptr, COINIT_MULTITHREADED | COINIT_DISABLE_OLE1DDE);
58 m_coinit_success = result == S_OK;
59 m_should_couninit = result == S_OK || result == S_FALSE;
60 });
61 }
62 #else
63 = default;
64 #endif
65
66 bool CubebStream::Init()
67 {
68 bool return_value = false;
69
70 #ifdef _WIN32
71 if (!m_coinit_success)
72 return false;
73 m_work_queue.PushBlocking([this, &return_value] {
74 #endif
75 m_ctx = CubebUtils::GetContext();
76 if (m_ctx)
77 {
78 m_stereo = !Config::ShouldUseDPL2Decoder();
79
80 cubeb_stream_params params{};
81 params.rate = m_mixer->GetSampleRate();
82 if (m_stereo)
83 {
84 params.channels = 2;
85 params.format = CUBEB_SAMPLE_S16NE;
86 params.layout = CUBEB_LAYOUT_STEREO;
87 }
88 else
89 {
90 params.channels = 6;
91 params.format = CUBEB_SAMPLE_FLOAT32NE;
92 params.layout = CUBEB_LAYOUT_3F2_LFE;
93 }
94
95 u32 minimum_latency = 0;
96 if (cubeb_get_min_latency(m_ctx.get(), &params, &minimum_latency) != CUBEB_OK)
97 ERROR_LOG_FMT(AUDIO, "Error getting minimum latency");
98 INFO_LOG_FMT(AUDIO, "Minimum latency: {} frames", minimum_latency);
99
100 return_value =
101 cubeb_stream_init(m_ctx.get(), &m_stream, "Dolphin Audio Output", nullptr, nullptr,
102 nullptr, &params, std::max(BUFFER_SAMPLES, minimum_latency),
103 DataCallback, StateCallback, this) == CUBEB_OK;
104
105 // Create per-wiimote streams for audio routing (Wii games only, when enabled)
106 if (return_value && Core::System::GetInstance().IsWii() &&
107 Config::Get(Config::MAIN_WIIMOTE_AUDIO_ROUTING_ENABLED))
108 {
109 static const char* const WIIMOTE_STREAM_NAMES[4] = {
110 "Dolphin Wiimote 1 Audio", "Dolphin Wiimote 2 Audio", "Dolphin Wiimote 3 Audio",
111 "Dolphin Wiimote 4 Audio"};
112
113 cubeb_stream_params wiimote_params{};
114 wiimote_params.rate = m_mixer->GetSampleRate();
115 wiimote_params.channels = 2;
116 wiimote_params.format = CUBEB_SAMPLE_S16NE;
117 wiimote_params.layout = CUBEB_LAYOUT_STEREO;
118
119 for (std::size_t i = 0; i < m_wiimote_streams.size(); ++i)
120 {
121 if (!Config::Get(Config::MAIN_WIIMOTE_AUDIO_OUTPUT_ENABLED[i]))
122 continue;
123
124 const std::string device_id_str =
125 Config::Get(Config::MAIN_WIIMOTE_AUDIO_OUTPUT_DEVICE[i]);
126 const cubeb_devid output_devid =
127 device_id_str.empty() ?
128 nullptr :
129 static_cast<cubeb_devid>(CubebUtils::GetOutputDeviceById(device_id_str));
130
131 u32 wiimote_min_latency = 0;
132 cubeb_get_min_latency(m_ctx.get(), &wiimote_params, &wiimote_min_latency);
133
134 m_wiimote_stream_data[i] = {this, i};
135 const int result = cubeb_stream_init(
136 m_ctx.get(), &m_wiimote_streams[i], WIIMOTE_STREAM_NAMES[i], nullptr, nullptr,
137 output_devid, &wiimote_params, std::max(BUFFER_SAMPLES, wiimote_min_latency),
138 WiimoteDataCallback, WiimoteStateCallback, &m_wiimote_stream_data[i]);
139
140 if (result != CUBEB_OK)
141 {
142 ERROR_LOG_FMT(AUDIO, "Failed to create Cubeb stream for Wiimote {} audio routing",
143 i + 1);
144 m_wiimote_streams[i] = nullptr;
145 }
146 }
147 }
148 }
149
150 #ifdef _WIN32
151 });
152 #endif
153
154 return return_value;
155 }
156
157 bool CubebStream::SetRunning(bool running)
158 {
159 bool return_value = false;
160
161 #ifdef _WIN32
162 if (!m_coinit_success)
163 return false;
164 m_work_queue.PushBlocking([this, running, &return_value] {
165 #endif
166 if (running)
167 {
168 return_value = cubeb_stream_start(m_stream) == CUBEB_OK;
169 for (auto& ws : m_wiimote_streams)
170 {
171 if (ws)
172 cubeb_stream_start(ws);
173 }
174 }
175 else
176 {
177 return_value = cubeb_stream_stop(m_stream) == CUBEB_OK;
178 for (auto& ws : m_wiimote_streams)
179 {
180 if (ws)
181 cubeb_stream_stop(ws);
182 }
183 }
184 #ifdef _WIN32
185 });
186 #endif
187
188 return return_value;
189 }
190
191 CubebStream::~CubebStream()
192 {
193 #ifdef _WIN32
194 m_work_queue.PushBlocking([this] {
195 #endif
196 cubeb_stream_stop(m_stream);
197 cubeb_stream_destroy(m_stream);
198 for (auto& ws : m_wiimote_streams)
199 {
200 if (ws)
201 {
202 cubeb_stream_stop(ws);
203 cubeb_stream_destroy(ws);
204 ws = nullptr;
205 }
206 }
207 #ifdef _WIN32
208 if (m_should_couninit)
209 {
210 m_should_couninit = false;
211 CoUninitialize();
212 }
213 m_coinit_success = false;
214 });
215 #endif
216 m_ctx.reset();
217 }
218
219 void CubebStream::SetVolume(int volume)
220 {
221 #ifdef _WIN32
222 if (!m_coinit_success)
223 return;
224 m_work_queue.PushBlocking([this, volume] {
225 #endif
226 cubeb_stream_set_volume(m_stream, volume / 100.0f);
227 #ifdef _WIN32
228 });
229 #endif
230 }