| 1 | // Copyright 2008 Dolphin Emulator Project |
| 2 | // SPDX-License-Identifier: GPL-2.0-or-later |
| 3 | |
| 4 | #ifdef _WIN32 |
| 5 | |
| 6 | #include "AudioCommon/OpenALStream.h" |
| 7 | |
| 8 | #include <windows.h> |
| 9 | #include <climits> |
| 10 | #include <cstring> |
| 11 | #include <thread> |
| 12 | |
| 13 | #include "Common/Logging/Log.h" |
| 14 | #include "Common/MsgHandler.h" |
| 15 | #include "Common/Thread.h" |
| 16 | #include "Core/Config/MainSettings.h" |
| 17 | |
| 18 | static HMODULE s_openal_dll = nullptr; |
| 19 | |
| 20 | #define OPENAL_API_VISIT(X) \ |
| 21 | X(alBufferData) \ |
| 22 | X(alcCloseDevice) \ |
| 23 | X(alcCreateContext) \ |
| 24 | X(alcDestroyContext) \ |
| 25 | X(alcGetContextsDevice) \ |
| 26 | X(alcGetCurrentContext) \ |
| 27 | X(alcGetString) \ |
| 28 | X(alcIsExtensionPresent) \ |
| 29 | X(alcMakeContextCurrent) \ |
| 30 | X(alcOpenDevice) \ |
| 31 | X(alDeleteBuffers) \ |
| 32 | X(alDeleteSources) \ |
| 33 | X(alGenBuffers) \ |
| 34 | X(alGenSources) \ |
| 35 | X(alGetError) \ |
| 36 | X(alGetSourcei) \ |
| 37 | X(alGetString) \ |
| 38 | X(alIsExtensionPresent) \ |
| 39 | X(alSourcef) \ |
| 40 | X(alSourcei) \ |
| 41 | X(alSourcePlay) \ |
| 42 | X(alSourceQueueBuffers) \ |
| 43 | X(alSourceStop) \ |
| 44 | X(alSourceUnqueueBuffers) |
| 45 | |
| 46 | // Create func_t function pointer type and declare a nullptr-initialized static variable of that |
| 47 | // type named "pfunc". |
| 48 | #define DYN_FUNC_DECLARE(func) \ |
| 49 | typedef decltype(&func) func##_t; \ |
| 50 | static func##_t p##func = nullptr; |
| 51 | |
| 52 | // Attempt to load the function from the given module handle. |
| 53 | #define OPENAL_FUNC_LOAD(func) \ |
| 54 | p##func = (func##_t)::GetProcAddress(s_openal_dll, #func); \ |
| 55 | if (!p##func) \ |
| 56 | { \ |
| 57 | return false; \ |
| 58 | } |
| 59 | |
| 60 | OPENAL_API_VISIT(DYN_FUNC_DECLARE); |
| 61 | |
| 62 | static bool InitFunctions() |
| 63 | { |
| 64 | OPENAL_API_VISIT(OPENAL_FUNC_LOAD); |
| 65 | return true; |
| 66 | } |
| 67 | |
| 68 | static bool InitLibrary() |
| 69 | { |
| 70 | if (s_openal_dll) |
| 71 | return true; |
| 72 | |
| 73 | s_openal_dll = ::LoadLibrary(TEXT("openal32.dll")); |
| 74 | if (!s_openal_dll) |
| 75 | return false; |
| 76 | |
| 77 | if (!InitFunctions()) |
| 78 | { |
| 79 | FreeLibrary(s_openal_dll); |
| 80 | s_openal_dll = nullptr; |
| 81 | return false; |
| 82 | } |
| 83 | |
| 84 | return true; |
| 85 | } |
| 86 | |
| 87 | bool OpenALStream::IsValid() |
| 88 | { |
| 89 | return InitLibrary(); |
| 90 | } |
| 91 | |
| 92 | // |
| 93 | // AyuanX: Spec says OpenAL1.1 is thread safe already |
| 94 | // |
| 95 | bool OpenALStream::Init() |
| 96 | { |
| 97 | if (!palcIsExtensionPresent(nullptr, "ALC_ENUMERATION_EXT")) |
| 98 | { |
| 99 | PanicAlertFmtT("OpenAL: can't find sound devices"); |
| 100 | return false; |
| 101 | } |
| 102 | |
| 103 | const char* default_device_dame = palcGetString(nullptr, ALC_DEFAULT_DEVICE_SPECIFIER); |
| 104 | INFO_LOG_FMT(AUDIO, "Found OpenAL device {}", default_device_dame); |
| 105 | |
| 106 | ALCdevice* device = palcOpenDevice(default_device_dame); |
| 107 | if (!device) |
| 108 | { |
| 109 | PanicAlertFmtT("OpenAL: can't open device {0}", default_device_dame); |
| 110 | return false; |
| 111 | } |
| 112 | |
| 113 | ALCcontext* context = palcCreateContext(device, nullptr); |
| 114 | if (!context) |
| 115 | { |
| 116 | palcCloseDevice(device); |
| 117 | PanicAlertFmtT("OpenAL: can't create context for device {0}", default_device_dame); |
| 118 | return false; |
| 119 | } |
| 120 | |
| 121 | palcMakeContextCurrent(context); |
| 122 | m_run_thread.Set(); |
| 123 | m_thread = std::thread(&OpenALStream::SoundLoop, this); |
| 124 | return true; |
| 125 | } |
| 126 | |
| 127 | OpenALStream::~OpenALStream() |
| 128 | { |
| 129 | m_run_thread.Clear(); |
| 130 | m_thread.join(); |
| 131 | |
| 132 | palSourceStop(m_source); |
| 133 | palSourcei(m_source, AL_BUFFER, 0); |
| 134 | |
| 135 | // Clean up buffers and sources |
| 136 | palDeleteSources(1, &m_source); |
| 137 | m_source = 0; |
| 138 | palDeleteBuffers(OAL_BUFFERS, m_buffers.data()); |
| 139 | |
| 140 | ALCcontext* context = palcGetCurrentContext(); |
| 141 | ALCdevice* device = palcGetContextsDevice(context); |
| 142 | |
| 143 | palcMakeContextCurrent(nullptr); |
| 144 | palcDestroyContext(context); |
| 145 | palcCloseDevice(device); |
| 146 | } |
| 147 | |
| 148 | void OpenALStream::SetVolume(int volume) |
| 149 | { |
| 150 | m_volume = (float)volume / 100.0f; |
| 151 | |
| 152 | if (m_source) |
| 153 | palSourcef(m_source, AL_GAIN, m_volume); |
| 154 | } |
| 155 | |
| 156 | bool OpenALStream::SetRunning(bool running) |
| 157 | { |
| 158 | if (running) |
| 159 | { |
| 160 | palSourcePlay(m_source); |
| 161 | } |
| 162 | else |
| 163 | { |
| 164 | palSourceStop(m_source); |
| 165 | } |
| 166 | return true; |
| 167 | } |
| 168 | |
| 169 | static ALenum CheckALError(const char* desc) |
| 170 | { |
| 171 | const ALenum err = palGetError(); |
| 172 | |
| 173 | if (err != AL_NO_ERROR) |
| 174 | { |
| 175 | std::string type; |
| 176 | |
| 177 | switch (err) |
| 178 | { |
| 179 | case AL_INVALID_NAME: |
| 180 | type = "AL_INVALID_NAME"; |
| 181 | break; |
| 182 | case AL_INVALID_ENUM: |
| 183 | type = "AL_INVALID_ENUM"; |
| 184 | break; |
| 185 | case AL_INVALID_VALUE: |
| 186 | type = "AL_INVALID_VALUE"; |
| 187 | break; |
| 188 | case AL_INVALID_OPERATION: |
| 189 | type = "AL_INVALID_OPERATION"; |
| 190 | break; |
| 191 | case AL_OUT_OF_MEMORY: |
| 192 | type = "AL_OUT_OF_MEMORY"; |
| 193 | break; |
| 194 | default: |
| 195 | type = "UNKNOWN_ERROR"; |
| 196 | break; |
| 197 | } |
| 198 | |
| 199 | ERROR_LOG_FMT(AUDIO, "Error {}: {:08x} {}", desc, err, type); |
| 200 | } |
| 201 | |
| 202 | return err; |
| 203 | } |
| 204 | |
| 205 | static bool IsCreativeXFi() |
| 206 | { |
| 207 | return strstr(palGetString(AL_RENDERER), "X-Fi") != nullptr; |
| 208 | } |
| 209 | |
| 210 | void OpenALStream::SoundLoop() |
| 211 | { |
| 212 | Common::SetCurrentThreadName("Audio thread - openal"); |
| 213 | |
| 214 | const bool float32_capable = palIsExtensionPresent("AL_EXT_float32") != 0; |
| 215 | const bool surround_capable = palIsExtensionPresent("AL_EXT_MCFORMATS") || IsCreativeXFi(); |
| 216 | bool use_surround = Config::ShouldUseDPL2Decoder() && surround_capable; |
| 217 | |
| 218 | // As there is no extension to check for 32-bit fixed point support |
| 219 | // and we know that only a X-Fi with hardware OpenAL supports it, |
| 220 | // we just check if one is being used. |
| 221 | const bool fixed32_capable = IsCreativeXFi(); |
| 222 | |
| 223 | const u32 frequency = m_mixer->GetSampleRate(); |
| 224 | |
| 225 | u32 frames_per_buffer; |
| 226 | // Can't have zero samples per buffer |
| 227 | if (Config::Get(Config::MAIN_AUDIO_LATENCY) > 0) |
| 228 | { |
| 229 | frames_per_buffer = frequency / 1000 * Config::Get(Config::MAIN_AUDIO_LATENCY) / OAL_BUFFERS; |
| 230 | } |
| 231 | else |
| 232 | { |
| 233 | frames_per_buffer = frequency / 1000 * 1 / OAL_BUFFERS; |
| 234 | } |
| 235 | |
| 236 | if (frames_per_buffer > OAL_MAX_FRAMES) |
| 237 | { |
| 238 | frames_per_buffer = OAL_MAX_FRAMES; |
| 239 | } |
| 240 | |
| 241 | INFO_LOG_FMT(AUDIO, "Using {} buffers, each with {} audio frames for a total of {}.", OAL_BUFFERS, |
| 242 | frames_per_buffer, frames_per_buffer * OAL_BUFFERS); |
| 243 | |
| 244 | // Should we make these larger just in case the mixer ever sends more samples |
| 245 | // than what we request? |
| 246 | m_realtime_buffer.resize(frames_per_buffer * STEREO_CHANNELS); |
| 247 | m_source = 0; |
| 248 | |
| 249 | // Clear error state before querying or else we get false positives. |
| 250 | ALenum err = palGetError(); |
| 251 | |
| 252 | // Generate some AL Buffers for streaming |
| 253 | palGenBuffers(OAL_BUFFERS, (ALuint*)m_buffers.data()); |
| 254 | err = CheckALError("generating buffers"); |
| 255 | |
| 256 | // Generate a Source to playback the Buffers |
| 257 | palGenSources(1, &m_source); |
| 258 | err = CheckALError("generating sources"); |
| 259 | |
| 260 | // Set the default sound volume as saved in the config file. |
| 261 | palSourcef(m_source, AL_GAIN, m_volume); |
| 262 | |
| 263 | // TODO: Error handling |
| 264 | // ALenum err = alGetError(); |
| 265 | |
| 266 | unsigned int next_buffer = 0; |
| 267 | unsigned int num_buffers_queued = 0; |
| 268 | ALint state = 0; |
| 269 | |
| 270 | while (m_run_thread.IsSet()) |
| 271 | { |
| 272 | // Block until we have a free buffer |
| 273 | int num_buffers_processed; |
| 274 | palGetSourcei(m_source, AL_BUFFERS_PROCESSED, &num_buffers_processed); |
| 275 | if (num_buffers_queued == OAL_BUFFERS && !num_buffers_processed) |
| 276 | { |
| 277 | std::this_thread::sleep_for(std::chrono::milliseconds(1)); |
| 278 | continue; |
| 279 | } |
| 280 | |
| 281 | // Remove the Buffer from the Queue. |
| 282 | if (num_buffers_processed) |
| 283 | { |
| 284 | std::array<ALuint, OAL_BUFFERS> unqueued_buffer_ids; |
| 285 | palSourceUnqueueBuffers(m_source, num_buffers_processed, unqueued_buffer_ids.data()); |
| 286 | err = CheckALError("unqueuing buffers"); |
| 287 | |
| 288 | num_buffers_queued -= num_buffers_processed; |
| 289 | } |
| 290 | |
| 291 | const unsigned int min_frames = frames_per_buffer; |
| 292 | |
| 293 | if (use_surround) |
| 294 | { |
| 295 | std::array<float, OAL_MAX_FRAMES * SURROUND_CHANNELS> dpl2; |
| 296 | const u32 rendered_frames = static_cast<u32>(m_mixer->MixSurround(dpl2.data(), min_frames)); |
| 297 | |
| 298 | if (rendered_frames < min_frames) |
| 299 | continue; |
| 300 | |
| 301 | if (float32_capable) |
| 302 | { |
| 303 | palBufferData(m_buffers[next_buffer], AL_FORMAT_51CHN32, dpl2.data(), |
| 304 | rendered_frames * FRAME_SURROUND_FLOAT, frequency); |
| 305 | } |
| 306 | else if (fixed32_capable) |
| 307 | { |
| 308 | std::array<int, OAL_MAX_FRAMES * SURROUND_CHANNELS> surround_int32; |
| 309 | |
| 310 | for (u32 i = 0; i < rendered_frames * SURROUND_CHANNELS; ++i) |
| 311 | { |
| 312 | dpl2[i] = dpl2[i] * std::numeric_limits<int>::max(); |
| 313 | if (dpl2[i] > std::numeric_limits<int>::max()) |
| 314 | surround_int32[i] = std::numeric_limits<int>::max(); |
| 315 | else if (dpl2[i] < std::numeric_limits<int>::min()) |
| 316 | surround_int32[i] = std::numeric_limits<int>::min(); |
| 317 | else |
| 318 | surround_int32[i] = static_cast<int>(dpl2[i]); |
| 319 | } |
| 320 | |
| 321 | palBufferData(m_buffers[next_buffer], AL_FORMAT_51CHN32, surround_int32.data(), |
| 322 | rendered_frames * FRAME_SURROUND_INT32, frequency); |
| 323 | } |
| 324 | else |
| 325 | { |
| 326 | std::array<short, OAL_MAX_FRAMES * SURROUND_CHANNELS> surround_short; |
| 327 | |
| 328 | for (u32 i = 0; i < rendered_frames * SURROUND_CHANNELS; ++i) |
| 329 | { |
| 330 | dpl2[i] = dpl2[i] * std::numeric_limits<short>::max(); |
| 331 | if (dpl2[i] > std::numeric_limits<short>::max()) |
| 332 | surround_short[i] = std::numeric_limits<short>::max(); |
| 333 | else if (dpl2[i] < std::numeric_limits<short>::min()) |
| 334 | surround_short[i] = std::numeric_limits<short>::min(); |
| 335 | else |
| 336 | surround_short[i] = static_cast<short>(dpl2[i]); |
| 337 | } |
| 338 | |
| 339 | palBufferData(m_buffers[next_buffer], AL_FORMAT_51CHN16, surround_short.data(), |
| 340 | rendered_frames * FRAME_SURROUND_SHORT, frequency); |
| 341 | } |
| 342 | |
| 343 | err = CheckALError("buffering data"); |
| 344 | if (err == AL_INVALID_ENUM) |
| 345 | { |
| 346 | // 5.1 is not supported by the host, fallback to stereo |
| 347 | WARN_LOG_FMT( |
| 348 | AUDIO, "Unable to set 5.1 surround mode. Updating OpenAL Soft might fix this issue."); |
| 349 | use_surround = false; |
| 350 | } |
| 351 | } |
| 352 | else |
| 353 | { |
| 354 | const u32 rendered_frames = |
| 355 | static_cast<u32>(m_mixer->Mix(m_realtime_buffer.data(), min_frames)); |
| 356 | |
| 357 | if (!rendered_frames) |
| 358 | continue; |
| 359 | |
| 360 | palBufferData(m_buffers[next_buffer], AL_FORMAT_STEREO16, m_realtime_buffer.data(), |
| 361 | rendered_frames * FRAME_STEREO_SHORT, frequency); |
| 362 | } |
| 363 | |
| 364 | palSourceQueueBuffers(m_source, 1, &m_buffers[next_buffer]); |
| 365 | err = CheckALError("queuing buffers"); |
| 366 | |
| 367 | num_buffers_queued++; |
| 368 | next_buffer = (next_buffer + 1) % OAL_BUFFERS; |
| 369 | |
| 370 | palGetSourcei(m_source, AL_SOURCE_STATE, &state); |
| 371 | if (state != AL_PLAYING) |
| 372 | { |
| 373 | // Buffer underrun occurred, resume playback |
| 374 | palSourcePlay(m_source); |
| 375 | err = CheckALError("occurred resuming playback"); |
| 376 | } |
| 377 | } |
| 378 | } |
| 379 | |
| 380 | #endif // _WIN32 |