Give unique IDs to debug messages
[openal-soft.git] / alc / alc.cpp
blobddead59e1519cf85f98b7840e7b0b3b30407933b
1 /**
2 * OpenAL cross platform audio library
3 * Copyright (C) 1999-2007 by authors.
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 * Or go to http://www.gnu.org/copyleft/lgpl.html
21 #include "config.h"
23 #include "version.h"
25 #ifdef _WIN32
26 #define WIN32_LEAN_AND_MEAN
27 #include <windows.h>
28 #endif
30 #include <algorithm>
31 #include <array>
32 #include <atomic>
33 #include <bitset>
34 #include <cassert>
35 #include <cctype>
36 #include <chrono>
37 #include <cinttypes>
38 #include <climits>
39 #include <cmath>
40 #include <csignal>
41 #include <cstddef>
42 #include <cstdio>
43 #include <cstdlib>
44 #include <cstring>
45 #include <exception>
46 #include <functional>
47 #include <iterator>
48 #include <limits>
49 #include <memory>
50 #include <mutex>
51 #include <new>
52 #include <optional>
53 #include <stdexcept>
54 #include <string>
55 #include <string_view>
56 #include <tuple>
57 #include <utility>
58 #include <vector>
60 #include "AL/al.h"
61 #include "AL/alc.h"
62 #include "AL/alext.h"
63 #include "AL/efx.h"
65 #include "al/auxeffectslot.h"
66 #include "al/buffer.h"
67 #include "al/debug.h"
68 #include "al/effect.h"
69 #include "al/filter.h"
70 #include "al/source.h"
71 #include "alc/events.h"
72 #include "albit.h"
73 #include "alconfig.h"
74 #include "almalloc.h"
75 #include "alnumbers.h"
76 #include "alnumeric.h"
77 #include "alspan.h"
78 #include "alstring.h"
79 #include "alu.h"
80 #include "atomic.h"
81 #include "context.h"
82 #include "core/ambidefs.h"
83 #include "core/bformatdec.h"
84 #include "core/bs2b.h"
85 #include "core/context.h"
86 #include "core/cpu_caps.h"
87 #include "core/devformat.h"
88 #include "core/device.h"
89 #include "core/effects/base.h"
90 #include "core/effectslot.h"
91 #include "core/filters/nfc.h"
92 #include "core/helpers.h"
93 #include "core/mastering.h"
94 #include "core/fpu_ctrl.h"
95 #include "core/logging.h"
96 #include "core/uhjfilter.h"
97 #include "core/voice.h"
98 #include "core/voice_change.h"
99 #include "device.h"
100 #include "effects/base.h"
101 #include "export_list.h"
102 #include "flexarray.h"
103 #include "inprogext.h"
104 #include "intrusive_ptr.h"
105 #include "opthelpers.h"
106 #include "strutils.h"
108 #include "backends/base.h"
109 #include "backends/null.h"
110 #include "backends/loopback.h"
111 #ifdef HAVE_PIPEWIRE
112 #include "backends/pipewire.h"
113 #endif
114 #ifdef HAVE_JACK
115 #include "backends/jack.h"
116 #endif
117 #ifdef HAVE_PULSEAUDIO
118 #include "backends/pulseaudio.h"
119 #endif
120 #ifdef HAVE_ALSA
121 #include "backends/alsa.h"
122 #endif
123 #ifdef HAVE_WASAPI
124 #include "backends/wasapi.h"
125 #endif
126 #ifdef HAVE_COREAUDIO
127 #include "backends/coreaudio.h"
128 #endif
129 #ifdef HAVE_OPENSL
130 #include "backends/opensl.h"
131 #endif
132 #ifdef HAVE_OBOE
133 #include "backends/oboe.h"
134 #endif
135 #ifdef HAVE_SOLARIS
136 #include "backends/solaris.h"
137 #endif
138 #ifdef HAVE_SNDIO
139 #include "backends/sndio.h"
140 #endif
141 #ifdef HAVE_OSS
142 #include "backends/oss.h"
143 #endif
144 #ifdef HAVE_DSOUND
145 #include "backends/dsound.h"
146 #endif
147 #ifdef HAVE_WINMM
148 #include "backends/winmm.h"
149 #endif
150 #ifdef HAVE_PORTAUDIO
151 #include "backends/portaudio.h"
152 #endif
153 #ifdef HAVE_SDL2
154 #include "backends/sdl2.h"
155 #endif
156 #ifdef HAVE_OTHERIO
157 #include "backends/otherio.h"
158 #endif
159 #ifdef HAVE_WAVE
160 #include "backends/wave.h"
161 #endif
163 #ifdef ALSOFT_EAX
164 #include "al/eax/api.h"
165 #include "al/eax/globals.h"
166 #endif
169 /************************************************
170 * Library initialization
171 ************************************************/
172 #if defined(_WIN32) && !defined(AL_LIBTYPE_STATIC)
173 BOOL APIENTRY DllMain(HINSTANCE module, DWORD reason, LPVOID /*reserved*/)
175 switch(reason)
177 case DLL_PROCESS_ATTACH:
178 /* Pin the DLL so we won't get unloaded until the process terminates */
179 GetModuleHandleExW(GET_MODULE_HANDLE_EX_FLAG_PIN | GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS,
180 reinterpret_cast<WCHAR*>(module), &module);
181 break;
183 return TRUE;
185 #endif
187 namespace {
189 using namespace std::string_view_literals;
190 using std::chrono::seconds;
191 using std::chrono::nanoseconds;
193 using voidp = void*;
194 using float2 = std::array<float,2>;
197 /************************************************
198 * Backends
199 ************************************************/
200 struct BackendInfo {
201 const char *name;
202 BackendFactory& (*getFactory)();
205 std::array BackendList{
206 #ifdef HAVE_PIPEWIRE
207 BackendInfo{"pipewire", PipeWireBackendFactory::getFactory},
208 #endif
209 #ifdef HAVE_PULSEAUDIO
210 BackendInfo{"pulse", PulseBackendFactory::getFactory},
211 #endif
212 #ifdef HAVE_WASAPI
213 BackendInfo{"wasapi", WasapiBackendFactory::getFactory},
214 #endif
215 #ifdef HAVE_COREAUDIO
216 BackendInfo{"core", CoreAudioBackendFactory::getFactory},
217 #endif
218 #ifdef HAVE_OBOE
219 BackendInfo{"oboe", OboeBackendFactory::getFactory},
220 #endif
221 #ifdef HAVE_OPENSL
222 BackendInfo{"opensl", OSLBackendFactory::getFactory},
223 #endif
224 #ifdef HAVE_ALSA
225 BackendInfo{"alsa", AlsaBackendFactory::getFactory},
226 #endif
227 #ifdef HAVE_SOLARIS
228 BackendInfo{"solaris", SolarisBackendFactory::getFactory},
229 #endif
230 #ifdef HAVE_SNDIO
231 BackendInfo{"sndio", SndIOBackendFactory::getFactory},
232 #endif
233 #ifdef HAVE_OSS
234 BackendInfo{"oss", OSSBackendFactory::getFactory},
235 #endif
236 #ifdef HAVE_JACK
237 BackendInfo{"jack", JackBackendFactory::getFactory},
238 #endif
239 #ifdef HAVE_DSOUND
240 BackendInfo{"dsound", DSoundBackendFactory::getFactory},
241 #endif
242 #ifdef HAVE_WINMM
243 BackendInfo{"winmm", WinMMBackendFactory::getFactory},
244 #endif
245 #ifdef HAVE_PORTAUDIO
246 BackendInfo{"port", PortBackendFactory::getFactory},
247 #endif
248 #ifdef HAVE_SDL2
249 BackendInfo{"sdl2", SDL2BackendFactory::getFactory},
250 #endif
251 #ifdef HAVE_OTHERIO
252 BackendInfo{"otherio", OtherIOBackendFactory::getFactory},
253 #endif
255 BackendInfo{"null", NullBackendFactory::getFactory},
256 #ifdef HAVE_WAVE
257 BackendInfo{"wave", WaveBackendFactory::getFactory},
258 #endif
261 BackendFactory *PlaybackFactory{};
262 BackendFactory *CaptureFactory{};
265 [[nodiscard]] constexpr auto GetNoErrorString() noexcept { return "No Error"; }
266 [[nodiscard]] constexpr auto GetInvalidDeviceString() noexcept { return "Invalid Device"; }
267 [[nodiscard]] constexpr auto GetInvalidContextString() noexcept { return "Invalid Context"; }
268 [[nodiscard]] constexpr auto GetInvalidEnumString() noexcept { return "Invalid Enum"; }
269 [[nodiscard]] constexpr auto GetInvalidValueString() noexcept { return "Invalid Value"; }
270 [[nodiscard]] constexpr auto GetOutOfMemoryString() noexcept { return "Out of Memory"; }
272 [[nodiscard]] constexpr auto GetDefaultName() noexcept { return "OpenAL Soft\0"; }
274 #ifdef _WIN32
275 [[nodiscard]] constexpr auto GetDevicePrefix() noexcept { return "OpenAL Soft on "sv; }
276 #else
277 [[nodiscard]] constexpr auto GetDevicePrefix() noexcept { return std::string_view{}; }
278 #endif
280 /************************************************
281 * Global variables
282 ************************************************/
284 /* Enumerated device names */
285 std::vector<std::string> alcAllDevicesArray;
286 std::vector<std::string> alcCaptureDeviceArray;
287 std::string alcAllDevicesList;
288 std::string alcCaptureDeviceList;
290 /* Default is always the first in the list */
291 std::string alcDefaultAllDevicesSpecifier;
292 std::string alcCaptureDefaultDeviceSpecifier;
294 std::atomic<ALCenum> LastNullDeviceError{ALC_NO_ERROR};
296 /* Flag to trap ALC device errors */
297 bool TrapALCError{false};
299 /* One-time configuration init control */
300 std::once_flag alc_config_once{};
302 /* Flag to specify if alcSuspendContext/alcProcessContext should defer/process
303 * updates.
305 bool SuspendDefers{true};
307 /* Initial seed for dithering. */
308 constexpr uint DitherRNGSeed{22222u};
311 /************************************************
312 * ALC information
313 ************************************************/
314 [[nodiscard]] constexpr auto GetNoDeviceExtList() noexcept -> std::string_view
316 return "ALC_ENUMERATE_ALL_EXT "
317 "ALC_ENUMERATION_EXT "
318 "ALC_EXT_CAPTURE "
319 "ALC_EXT_direct_context "
320 "ALC_EXT_EFX "
321 "ALC_EXT_thread_local_context "
322 "ALC_SOFT_loopback "
323 "ALC_SOFT_loopback_bformat "
324 "ALC_SOFT_reopen_device "
325 "ALC_SOFT_system_events"sv;
327 [[nodiscard]] constexpr auto GetExtensionList() noexcept -> std::string_view
329 return "ALC_ENUMERATE_ALL_EXT "
330 "ALC_ENUMERATION_EXT "
331 "ALC_EXT_CAPTURE "
332 "ALC_EXT_debug "
333 "ALC_EXT_DEDICATED "
334 "ALC_EXT_direct_context "
335 "ALC_EXT_disconnect "
336 "ALC_EXT_EFX "
337 "ALC_EXT_thread_local_context "
338 "ALC_SOFT_device_clock "
339 "ALC_SOFT_HRTF "
340 "ALC_SOFT_loopback "
341 "ALC_SOFT_loopback_bformat "
342 "ALC_SOFT_output_limiter "
343 "ALC_SOFT_output_mode "
344 "ALC_SOFT_pause_device "
345 "ALC_SOFT_reopen_device "
346 "ALC_SOFT_system_events"sv;
349 constexpr int alcMajorVersion{1};
350 constexpr int alcMinorVersion{1};
352 constexpr int alcEFXMajorVersion{1};
353 constexpr int alcEFXMinorVersion{0};
356 using DeviceRef = al::intrusive_ptr<ALCdevice>;
359 /************************************************
360 * Device lists
361 ************************************************/
362 std::vector<ALCdevice*> DeviceList;
363 std::vector<ALCcontext*> ContextList;
365 std::recursive_mutex ListLock;
368 void alc_initconfig()
370 if(auto loglevel = al::getenv("ALSOFT_LOGLEVEL"))
372 long lvl = strtol(loglevel->c_str(), nullptr, 0);
373 if(lvl >= static_cast<long>(LogLevel::Trace))
374 gLogLevel = LogLevel::Trace;
375 else if(lvl <= static_cast<long>(LogLevel::Disable))
376 gLogLevel = LogLevel::Disable;
377 else
378 gLogLevel = static_cast<LogLevel>(lvl);
381 #ifdef _WIN32
382 if(const auto logfile = al::getenv(L"ALSOFT_LOGFILE"))
384 FILE *logf{_wfopen(logfile->c_str(), L"wt")};
385 if(logf) gLogFile = logf;
386 else
388 auto u8name = wstr_to_utf8(*logfile);
389 ERR("Failed to open log file '%s'\n", u8name.c_str());
392 #else
393 if(const auto logfile = al::getenv("ALSOFT_LOGFILE"))
395 FILE *logf{fopen(logfile->c_str(), "wt")};
396 if(logf) gLogFile = logf;
397 else ERR("Failed to open log file '%s'\n", logfile->c_str());
399 #endif
401 TRACE("Initializing library v%s-%s %s\n", ALSOFT_VERSION, ALSOFT_GIT_COMMIT_HASH,
402 ALSOFT_GIT_BRANCH);
404 std::string names;
405 if(std::size(BackendList) < 1)
406 names = "(none)";
407 else
409 const al::span<const BackendInfo> infos{BackendList};
410 names = infos[0].name;
411 for(const auto &backend : infos.subspan<1>())
413 names += ", ";
414 names += backend.name;
417 TRACE("Supported backends: %s\n", names.c_str());
419 ReadALConfig();
421 if(auto suspendmode = al::getenv("__ALSOFT_SUSPEND_CONTEXT"))
423 if(al::case_compare(*suspendmode, "ignore"sv) == 0)
425 SuspendDefers = false;
426 TRACE("Selected context suspend behavior, \"ignore\"\n");
428 else
429 ERR("Unhandled context suspend behavior setting: \"%s\"\n", suspendmode->c_str());
432 int capfilter{0};
433 #if defined(HAVE_SSE4_1)
434 capfilter |= CPU_CAP_SSE | CPU_CAP_SSE2 | CPU_CAP_SSE3 | CPU_CAP_SSE4_1;
435 #elif defined(HAVE_SSE3)
436 capfilter |= CPU_CAP_SSE | CPU_CAP_SSE2 | CPU_CAP_SSE3;
437 #elif defined(HAVE_SSE2)
438 capfilter |= CPU_CAP_SSE | CPU_CAP_SSE2;
439 #elif defined(HAVE_SSE)
440 capfilter |= CPU_CAP_SSE;
441 #endif
442 #ifdef HAVE_NEON
443 capfilter |= CPU_CAP_NEON;
444 #endif
445 if(auto cpuopt = ConfigValueStr({}, {}, "disable-cpu-exts"sv))
447 std::string_view cpulist{*cpuopt};
448 if(al::case_compare(cpulist, "all"sv) == 0)
449 capfilter = 0;
450 else while(!cpulist.empty())
452 auto nextpos = std::min(cpulist.find(','), cpulist.size());
453 auto entry = cpulist.substr(0, nextpos);
455 while(nextpos < cpulist.size() && cpulist[nextpos] == ',')
456 ++nextpos;
457 cpulist.remove_prefix(nextpos);
459 while(!entry.empty() && std::isspace(entry.front()))
460 entry.remove_prefix(1);
461 while(!entry.empty() && std::isspace(entry.back()))
462 entry.remove_suffix(1);
463 if(entry.empty())
464 continue;
466 if(al::case_compare(entry, "sse"sv) == 0)
467 capfilter &= ~CPU_CAP_SSE;
468 else if(al::case_compare(entry, "sse2"sv) == 0)
469 capfilter &= ~CPU_CAP_SSE2;
470 else if(al::case_compare(entry, "sse3"sv) == 0)
471 capfilter &= ~CPU_CAP_SSE3;
472 else if(al::case_compare(entry, "sse4.1"sv) == 0)
473 capfilter &= ~CPU_CAP_SSE4_1;
474 else if(al::case_compare(entry, "neon"sv) == 0)
475 capfilter &= ~CPU_CAP_NEON;
476 else
477 WARN("Invalid CPU extension \"%.*s\"\n", al::sizei(entry), entry.data());
480 if(auto cpuopt = GetCPUInfo())
482 if(!cpuopt->mVendor.empty() || !cpuopt->mName.empty())
484 TRACE("Vendor ID: \"%s\"\n", cpuopt->mVendor.c_str());
485 TRACE("Name: \"%s\"\n", cpuopt->mName.c_str());
487 const int caps{cpuopt->mCaps};
488 TRACE("Extensions:%s%s%s%s%s%s\n",
489 ((capfilter&CPU_CAP_SSE) ? ((caps&CPU_CAP_SSE) ? " +SSE" : " -SSE") : ""),
490 ((capfilter&CPU_CAP_SSE2) ? ((caps&CPU_CAP_SSE2) ? " +SSE2" : " -SSE2") : ""),
491 ((capfilter&CPU_CAP_SSE3) ? ((caps&CPU_CAP_SSE3) ? " +SSE3" : " -SSE3") : ""),
492 ((capfilter&CPU_CAP_SSE4_1) ? ((caps&CPU_CAP_SSE4_1) ? " +SSE4.1" : " -SSE4.1") : ""),
493 ((capfilter&CPU_CAP_NEON) ? ((caps&CPU_CAP_NEON) ? " +NEON" : " -NEON") : ""),
494 ((!capfilter) ? " -none-" : ""));
495 CPUCapFlags = caps & capfilter;
498 if(auto priopt = ConfigValueInt({}, {}, "rt-prio"sv))
499 RTPrioLevel = *priopt;
500 if(auto limopt = ConfigValueBool({}, {}, "rt-time-limit"sv))
501 AllowRTTimeLimit = *limopt;
504 CompatFlagBitset compatflags{};
505 auto checkflag = [](const char *envname, const std::string_view optname) -> bool
507 if(auto optval = al::getenv(envname))
509 return al::case_compare(*optval, "true"sv) == 0
510 || strtol(optval->c_str(), nullptr, 0) == 1;
512 return GetConfigValueBool({}, "game_compat", optname, false);
514 sBufferSubDataCompat = checkflag("__ALSOFT_ENABLE_SUB_DATA_EXT", "enable-sub-data-ext"sv);
515 compatflags.set(CompatFlags::ReverseX, checkflag("__ALSOFT_REVERSE_X", "reverse-x"sv));
516 compatflags.set(CompatFlags::ReverseY, checkflag("__ALSOFT_REVERSE_Y", "reverse-y"sv));
517 compatflags.set(CompatFlags::ReverseZ, checkflag("__ALSOFT_REVERSE_Z", "reverse-z"sv));
519 aluInit(compatflags, ConfigValueFloat({}, "game_compat"sv, "nfc-scale"sv).value_or(1.0f));
521 Voice::InitMixer(ConfigValueStr({}, {}, "resampler"sv));
523 if(auto uhjfiltopt = ConfigValueStr({}, "uhj"sv, "decode-filter"sv))
525 if(al::case_compare(*uhjfiltopt, "fir256"sv) == 0)
526 UhjDecodeQuality = UhjQualityType::FIR256;
527 else if(al::case_compare(*uhjfiltopt, "fir512"sv) == 0)
528 UhjDecodeQuality = UhjQualityType::FIR512;
529 else if(al::case_compare(*uhjfiltopt, "iir"sv) == 0)
530 UhjDecodeQuality = UhjQualityType::IIR;
531 else
532 WARN("Unsupported uhj/decode-filter: %s\n", uhjfiltopt->c_str());
534 if(auto uhjfiltopt = ConfigValueStr({}, "uhj"sv, "encode-filter"sv))
536 if(al::case_compare(*uhjfiltopt, "fir256"sv) == 0)
537 UhjEncodeQuality = UhjQualityType::FIR256;
538 else if(al::case_compare(*uhjfiltopt, "fir512"sv) == 0)
539 UhjEncodeQuality = UhjQualityType::FIR512;
540 else if(al::case_compare(*uhjfiltopt, "iir"sv) == 0)
541 UhjEncodeQuality = UhjQualityType::IIR;
542 else
543 WARN("Unsupported uhj/encode-filter: %s\n", uhjfiltopt->c_str());
546 if(auto traperr = al::getenv("ALSOFT_TRAP_ERROR"); traperr
547 && (al::case_compare(*traperr, "true"sv) == 0
548 || std::strtol(traperr->c_str(), nullptr, 0) == 1))
550 TrapALError = true;
551 TrapALCError = true;
553 else
555 traperr = al::getenv("ALSOFT_TRAP_AL_ERROR");
556 if(traperr)
557 TrapALError = al::case_compare(*traperr, "true"sv) == 0
558 || strtol(traperr->c_str(), nullptr, 0) == 1;
559 else
560 TrapALError = GetConfigValueBool({}, {}, "trap-al-error"sv, false);
562 traperr = al::getenv("ALSOFT_TRAP_ALC_ERROR");
563 if(traperr)
564 TrapALCError = al::case_compare(*traperr, "true"sv) == 0
565 || strtol(traperr->c_str(), nullptr, 0) == 1;
566 else
567 TrapALCError = GetConfigValueBool({}, {}, "trap-alc-error"sv, false);
570 if(auto boostopt = ConfigValueFloat({}, "reverb"sv, "boost"sv))
572 const float valf{std::isfinite(*boostopt) ? std::clamp(*boostopt, -24.0f, 24.0f) : 0.0f};
573 ReverbBoost *= std::pow(10.0f, valf / 20.0f);
576 auto BackendListEnd = BackendList.end();
577 auto devopt = al::getenv("ALSOFT_DRIVERS");
578 if(!devopt) devopt = ConfigValueStr({}, {}, "drivers"sv);
579 if(devopt)
581 auto backendlist_cur = BackendList.begin();
583 bool endlist{true};
584 std::string_view drvlist{*devopt};
585 while(!drvlist.empty())
587 auto nextpos = std::min(drvlist.find(','), drvlist.size());
588 auto entry = drvlist.substr(0, nextpos);
590 endlist = true;
591 if(nextpos < drvlist.size())
593 endlist = false;
594 while(nextpos < drvlist.size() && drvlist[nextpos] == ',')
595 ++nextpos;
597 drvlist.remove_prefix(nextpos);
599 while(!entry.empty() && std::isspace(entry.front()))
600 entry.remove_prefix(1);
601 const bool delitem{!entry.empty() && entry.front() == '-'};
602 if(delitem) entry.remove_prefix(1);
604 while(!entry.empty() && std::isspace(entry.back()))
605 entry.remove_suffix(1);
606 if(entry.empty())
607 continue;
609 #ifdef HAVE_WASAPI
610 /* HACK: For backwards compatibility, convert backend references of
611 * mmdevapi to wasapi. This should eventually be removed.
613 if(entry == "mmdevapi"sv)
614 entry = "wasapi"sv;
615 #endif
617 auto find_backend = [entry](const BackendInfo &backend) -> bool
618 { return entry == backend.name; };
619 auto this_backend = std::find_if(BackendList.begin(), BackendListEnd, find_backend);
621 if(this_backend == BackendListEnd)
622 continue;
624 if(delitem)
625 BackendListEnd = std::move(this_backend+1, BackendListEnd, this_backend);
626 else
627 backendlist_cur = std::rotate(backendlist_cur, this_backend, this_backend+1);
630 if(endlist)
631 BackendListEnd = backendlist_cur;
634 auto init_backend = [](BackendInfo &backend) -> void
636 if(PlaybackFactory && CaptureFactory)
637 return;
639 BackendFactory &factory = backend.getFactory();
640 if(!factory.init())
642 WARN("Failed to initialize backend \"%s\"\n", backend.name);
643 return;
646 TRACE("Initialized backend \"%s\"\n", backend.name);
647 if(!PlaybackFactory && factory.querySupport(BackendType::Playback))
649 PlaybackFactory = &factory;
650 TRACE("Added \"%s\" for playback\n", backend.name);
652 if(!CaptureFactory && factory.querySupport(BackendType::Capture))
654 CaptureFactory = &factory;
655 TRACE("Added \"%s\" for capture\n", backend.name);
658 std::for_each(BackendList.begin(), BackendListEnd, init_backend);
660 LoopbackBackendFactory::getFactory().init();
662 if(!PlaybackFactory)
663 WARN("No playback backend available!\n");
664 if(!CaptureFactory)
665 WARN("No capture backend available!\n");
667 if(auto exclopt = ConfigValueStr({}, {}, "excludefx"sv))
669 std::string_view exclude{*exclopt};
670 while(!exclude.empty())
672 const auto nextpos = exclude.find(',');
673 const auto entry = exclude.substr(0, nextpos);
674 exclude.remove_prefix((nextpos < exclude.size()) ? nextpos+1 : exclude.size());
676 std::for_each(gEffectList.cbegin(), gEffectList.cend(),
677 [entry](const EffectList &effectitem) noexcept
679 if(entry == std::data(effectitem.name))
680 DisabledEffects.set(effectitem.type);
685 InitEffect(&ALCcontext::sDefaultEffect);
686 auto defrevopt = al::getenv("ALSOFT_DEFAULT_REVERB");
687 if(!defrevopt) defrevopt = ConfigValueStr({}, {}, "default-reverb"sv);
688 if(defrevopt) LoadReverbPreset(*defrevopt, &ALCcontext::sDefaultEffect);
690 #ifdef ALSOFT_EAX
692 if(const auto eax_enable_opt = ConfigValueBool({}, "eax", "enable"))
694 eax_g_is_enabled = *eax_enable_opt;
695 if(!eax_g_is_enabled)
696 TRACE("%s\n", "EAX disabled by a configuration.");
698 else
699 eax_g_is_enabled = true;
701 if((DisabledEffects.test(EAXREVERB_EFFECT) || DisabledEffects.test(CHORUS_EFFECT))
702 && eax_g_is_enabled)
704 eax_g_is_enabled = false;
705 TRACE("EAX disabled because %s disabled.\n",
706 (DisabledEffects.test(EAXREVERB_EFFECT) && DisabledEffects.test(CHORUS_EFFECT))
707 ? "EAXReverb and Chorus are" :
708 DisabledEffects.test(EAXREVERB_EFFECT) ? "EAXReverb is" :
709 DisabledEffects.test(CHORUS_EFFECT) ? "Chorus is" : "");
712 #endif // ALSOFT_EAX
714 inline void InitConfig()
715 { std::call_once(alc_config_once, [](){alc_initconfig();}); }
718 /************************************************
719 * Device enumeration
720 ************************************************/
721 void ProbeAllDevicesList()
723 InitConfig();
725 std::lock_guard<std::recursive_mutex> listlock{ListLock};
726 if(!PlaybackFactory)
728 decltype(alcAllDevicesArray){}.swap(alcAllDevicesArray);
729 decltype(alcAllDevicesList){}.swap(alcAllDevicesList);
731 else
733 alcAllDevicesArray = PlaybackFactory->enumerate(BackendType::Playback);
734 if(const auto prefix = GetDevicePrefix(); !prefix.empty())
735 std::for_each(alcAllDevicesArray.begin(), alcAllDevicesArray.end(),
736 [prefix](std::string &name) { name.insert(0, prefix); });
738 decltype(alcAllDevicesList){}.swap(alcAllDevicesList);
739 if(alcAllDevicesArray.empty())
740 alcAllDevicesList += '\0';
741 else for(auto &devname : alcAllDevicesArray)
742 alcAllDevicesList.append(devname) += '\0';
745 void ProbeCaptureDeviceList()
747 InitConfig();
749 std::lock_guard<std::recursive_mutex> listlock{ListLock};
750 if(!CaptureFactory)
752 decltype(alcCaptureDeviceArray){}.swap(alcCaptureDeviceArray);
753 decltype(alcCaptureDeviceList){}.swap(alcCaptureDeviceList);
755 else
757 alcCaptureDeviceArray = CaptureFactory->enumerate(BackendType::Capture);
758 if(const auto prefix = GetDevicePrefix(); !prefix.empty())
759 std::for_each(alcCaptureDeviceArray.begin(), alcCaptureDeviceArray.end(),
760 [prefix](std::string &name) { name.insert(0, prefix); });
762 decltype(alcCaptureDeviceList){}.swap(alcCaptureDeviceList);
763 if(alcCaptureDeviceArray.empty())
764 alcCaptureDeviceList += '\0';
765 else for(auto &devname : alcCaptureDeviceArray)
766 alcCaptureDeviceList.append(devname) += '\0';
771 al::span<const ALCint> SpanFromAttributeList(const ALCint *attribs) noexcept
773 al::span<const ALCint> attrSpan;
774 if(attribs)
776 const ALCint *attrEnd{attribs};
777 while(*attrEnd != 0)
778 attrEnd += 2; /* NOLINT(cppcoreguidelines-pro-bounds-pointer-arithmetic) */
779 attrSpan = {attribs, attrEnd};
781 return attrSpan;
784 struct DevFmtPair { DevFmtChannels chans; DevFmtType type; };
785 std::optional<DevFmtPair> DecomposeDevFormat(ALenum format)
787 struct FormatType {
788 ALenum format;
789 DevFmtChannels channels;
790 DevFmtType type;
792 static constexpr std::array list{
793 FormatType{AL_FORMAT_MONO8, DevFmtMono, DevFmtUByte},
794 FormatType{AL_FORMAT_MONO16, DevFmtMono, DevFmtShort},
795 FormatType{AL_FORMAT_MONO_I32, DevFmtMono, DevFmtInt},
796 FormatType{AL_FORMAT_MONO_FLOAT32, DevFmtMono, DevFmtFloat},
798 FormatType{AL_FORMAT_STEREO8, DevFmtStereo, DevFmtUByte},
799 FormatType{AL_FORMAT_STEREO16, DevFmtStereo, DevFmtShort},
800 FormatType{AL_FORMAT_STEREO_I32, DevFmtStereo, DevFmtInt},
801 FormatType{AL_FORMAT_STEREO_FLOAT32, DevFmtStereo, DevFmtFloat},
803 FormatType{AL_FORMAT_QUAD8, DevFmtQuad, DevFmtUByte},
804 FormatType{AL_FORMAT_QUAD16, DevFmtQuad, DevFmtShort},
805 FormatType{AL_FORMAT_QUAD32, DevFmtQuad, DevFmtFloat},
806 FormatType{AL_FORMAT_QUAD_I32, DevFmtQuad, DevFmtInt},
807 FormatType{AL_FORMAT_QUAD_FLOAT32, DevFmtQuad, DevFmtFloat},
809 FormatType{AL_FORMAT_51CHN8, DevFmtX51, DevFmtUByte},
810 FormatType{AL_FORMAT_51CHN16, DevFmtX51, DevFmtShort},
811 FormatType{AL_FORMAT_51CHN32, DevFmtX51, DevFmtFloat},
812 FormatType{AL_FORMAT_51CHN_I32, DevFmtX51, DevFmtInt},
813 FormatType{AL_FORMAT_51CHN_FLOAT32, DevFmtX51, DevFmtFloat},
815 FormatType{AL_FORMAT_61CHN8, DevFmtX61, DevFmtUByte},
816 FormatType{AL_FORMAT_61CHN16, DevFmtX61, DevFmtShort},
817 FormatType{AL_FORMAT_61CHN32, DevFmtX61, DevFmtFloat},
818 FormatType{AL_FORMAT_61CHN_I32, DevFmtX61, DevFmtInt},
819 FormatType{AL_FORMAT_61CHN_FLOAT32, DevFmtX61, DevFmtFloat},
821 FormatType{AL_FORMAT_71CHN8, DevFmtX71, DevFmtUByte},
822 FormatType{AL_FORMAT_71CHN16, DevFmtX71, DevFmtShort},
823 FormatType{AL_FORMAT_71CHN32, DevFmtX71, DevFmtFloat},
824 FormatType{AL_FORMAT_71CHN_I32, DevFmtX71, DevFmtInt},
825 FormatType{AL_FORMAT_71CHN_FLOAT32, DevFmtX71, DevFmtFloat},
828 for(const auto &item : list)
830 if(item.format == format)
831 return DevFmtPair{item.channels, item.type};
834 return std::nullopt;
837 std::optional<DevFmtType> DevFmtTypeFromEnum(ALCenum type)
839 switch(type)
841 case ALC_BYTE_SOFT: return DevFmtByte;
842 case ALC_UNSIGNED_BYTE_SOFT: return DevFmtUByte;
843 case ALC_SHORT_SOFT: return DevFmtShort;
844 case ALC_UNSIGNED_SHORT_SOFT: return DevFmtUShort;
845 case ALC_INT_SOFT: return DevFmtInt;
846 case ALC_UNSIGNED_INT_SOFT: return DevFmtUInt;
847 case ALC_FLOAT_SOFT: return DevFmtFloat;
849 WARN("Unsupported format type: 0x%04x\n", type);
850 return std::nullopt;
852 ALCenum EnumFromDevFmt(DevFmtType type)
854 switch(type)
856 case DevFmtByte: return ALC_BYTE_SOFT;
857 case DevFmtUByte: return ALC_UNSIGNED_BYTE_SOFT;
858 case DevFmtShort: return ALC_SHORT_SOFT;
859 case DevFmtUShort: return ALC_UNSIGNED_SHORT_SOFT;
860 case DevFmtInt: return ALC_INT_SOFT;
861 case DevFmtUInt: return ALC_UNSIGNED_INT_SOFT;
862 case DevFmtFloat: return ALC_FLOAT_SOFT;
864 throw std::runtime_error{"Invalid DevFmtType: "+std::to_string(int(type))};
867 std::optional<DevFmtChannels> DevFmtChannelsFromEnum(ALCenum channels)
869 switch(channels)
871 case ALC_MONO_SOFT: return DevFmtMono;
872 case ALC_STEREO_SOFT: return DevFmtStereo;
873 case ALC_QUAD_SOFT: return DevFmtQuad;
874 case ALC_5POINT1_SOFT: return DevFmtX51;
875 case ALC_6POINT1_SOFT: return DevFmtX61;
876 case ALC_7POINT1_SOFT: return DevFmtX71;
877 case ALC_BFORMAT3D_SOFT: return DevFmtAmbi3D;
879 WARN("Unsupported format channels: 0x%04x\n", channels);
880 return std::nullopt;
882 ALCenum EnumFromDevFmt(DevFmtChannels channels)
884 switch(channels)
886 case DevFmtMono: return ALC_MONO_SOFT;
887 case DevFmtStereo: return ALC_STEREO_SOFT;
888 case DevFmtQuad: return ALC_QUAD_SOFT;
889 case DevFmtX51: return ALC_5POINT1_SOFT;
890 case DevFmtX61: return ALC_6POINT1_SOFT;
891 case DevFmtX71: return ALC_7POINT1_SOFT;
892 case DevFmtAmbi3D: return ALC_BFORMAT3D_SOFT;
893 /* FIXME: Shouldn't happen. */
894 case DevFmtX714:
895 case DevFmtX7144:
896 case DevFmtX3D71: break;
898 throw std::runtime_error{"Invalid DevFmtChannels: "+std::to_string(int(channels))};
901 std::optional<DevAmbiLayout> DevAmbiLayoutFromEnum(ALCenum layout)
903 switch(layout)
905 case ALC_FUMA_SOFT: return DevAmbiLayout::FuMa;
906 case ALC_ACN_SOFT: return DevAmbiLayout::ACN;
908 WARN("Unsupported ambisonic layout: 0x%04x\n", layout);
909 return std::nullopt;
911 ALCenum EnumFromDevAmbi(DevAmbiLayout layout)
913 switch(layout)
915 case DevAmbiLayout::FuMa: return ALC_FUMA_SOFT;
916 case DevAmbiLayout::ACN: return ALC_ACN_SOFT;
918 throw std::runtime_error{"Invalid DevAmbiLayout: "+std::to_string(int(layout))};
921 std::optional<DevAmbiScaling> DevAmbiScalingFromEnum(ALCenum scaling)
923 switch(scaling)
925 case ALC_FUMA_SOFT: return DevAmbiScaling::FuMa;
926 case ALC_SN3D_SOFT: return DevAmbiScaling::SN3D;
927 case ALC_N3D_SOFT: return DevAmbiScaling::N3D;
929 WARN("Unsupported ambisonic scaling: 0x%04x\n", scaling);
930 return std::nullopt;
932 ALCenum EnumFromDevAmbi(DevAmbiScaling scaling)
934 switch(scaling)
936 case DevAmbiScaling::FuMa: return ALC_FUMA_SOFT;
937 case DevAmbiScaling::SN3D: return ALC_SN3D_SOFT;
938 case DevAmbiScaling::N3D: return ALC_N3D_SOFT;
940 throw std::runtime_error{"Invalid DevAmbiScaling: "+std::to_string(int(scaling))};
944 /* Downmixing channel arrays, to map a device format's missing channels to
945 * existing ones. Based on what PipeWire does, though simplified.
947 constexpr float inv_sqrt2f{static_cast<float>(1.0 / al::numbers::sqrt2)};
948 constexpr std::array FrontStereo3dB{
949 InputRemixMap::TargetMix{FrontLeft, inv_sqrt2f},
950 InputRemixMap::TargetMix{FrontRight, inv_sqrt2f}
952 constexpr std::array FrontStereo6dB{
953 InputRemixMap::TargetMix{FrontLeft, 0.5f},
954 InputRemixMap::TargetMix{FrontRight, 0.5f}
956 constexpr std::array SideStereo3dB{
957 InputRemixMap::TargetMix{SideLeft, inv_sqrt2f},
958 InputRemixMap::TargetMix{SideRight, inv_sqrt2f}
960 constexpr std::array BackStereo3dB{
961 InputRemixMap::TargetMix{BackLeft, inv_sqrt2f},
962 InputRemixMap::TargetMix{BackRight, inv_sqrt2f}
964 constexpr std::array FrontLeft3dB{InputRemixMap::TargetMix{FrontLeft, inv_sqrt2f}};
965 constexpr std::array FrontRight3dB{InputRemixMap::TargetMix{FrontRight, inv_sqrt2f}};
966 constexpr std::array SideLeft0dB{InputRemixMap::TargetMix{SideLeft, 1.0f}};
967 constexpr std::array SideRight0dB{InputRemixMap::TargetMix{SideRight, 1.0f}};
968 constexpr std::array BackLeft0dB{InputRemixMap::TargetMix{BackLeft, 1.0f}};
969 constexpr std::array BackRight0dB{InputRemixMap::TargetMix{BackRight, 1.0f}};
970 constexpr std::array BackCenter3dB{InputRemixMap::TargetMix{BackCenter, inv_sqrt2f}};
972 constexpr std::array StereoDownmix{
973 InputRemixMap{FrontCenter, FrontStereo3dB},
974 InputRemixMap{SideLeft, FrontLeft3dB},
975 InputRemixMap{SideRight, FrontRight3dB},
976 InputRemixMap{BackLeft, FrontLeft3dB},
977 InputRemixMap{BackRight, FrontRight3dB},
978 InputRemixMap{BackCenter, FrontStereo6dB},
980 constexpr std::array QuadDownmix{
981 InputRemixMap{FrontCenter, FrontStereo3dB},
982 InputRemixMap{SideLeft, BackLeft0dB},
983 InputRemixMap{SideRight, BackRight0dB},
984 InputRemixMap{BackCenter, BackStereo3dB},
986 constexpr std::array X51Downmix{
987 InputRemixMap{BackLeft, SideLeft0dB},
988 InputRemixMap{BackRight, SideRight0dB},
989 InputRemixMap{BackCenter, SideStereo3dB},
991 constexpr std::array X61Downmix{
992 InputRemixMap{BackLeft, BackCenter3dB},
993 InputRemixMap{BackRight, BackCenter3dB},
995 constexpr std::array X71Downmix{
996 InputRemixMap{BackCenter, BackStereo3dB},
1000 std::unique_ptr<Compressor> CreateDeviceLimiter(const ALCdevice *device, const float threshold)
1002 static constexpr bool AutoKnee{true};
1003 static constexpr bool AutoAttack{true};
1004 static constexpr bool AutoRelease{true};
1005 static constexpr bool AutoPostGain{true};
1006 static constexpr bool AutoDeclip{true};
1007 static constexpr float LookAheadTime{0.001f};
1008 static constexpr float HoldTime{0.002f};
1009 static constexpr float PreGainDb{0.0f};
1010 static constexpr float PostGainDb{0.0f};
1011 static constexpr float Ratio{std::numeric_limits<float>::infinity()};
1012 static constexpr float KneeDb{0.0f};
1013 static constexpr float AttackTime{0.02f};
1014 static constexpr float ReleaseTime{0.2f};
1016 return Compressor::Create(device->RealOut.Buffer.size(), static_cast<float>(device->Frequency),
1017 AutoKnee, AutoAttack, AutoRelease, AutoPostGain, AutoDeclip, LookAheadTime, HoldTime,
1018 PreGainDb, PostGainDb, threshold, Ratio, KneeDb, AttackTime, ReleaseTime);
1022 * Updates the device's base clock time with however many samples have been
1023 * done. This is used so frequency changes on the device don't cause the time
1024 * to jump forward or back. Must not be called while the device is running/
1025 * mixing.
1027 inline void UpdateClockBase(ALCdevice *device)
1029 const auto mixLock = device->getWriteMixLock();
1031 auto samplesDone = device->mSamplesDone.load(std::memory_order_relaxed);
1032 auto clockBase = device->mClockBase.load(std::memory_order_relaxed);
1034 clockBase += nanoseconds{seconds{samplesDone}} / device->Frequency;
1035 device->mClockBase.store(clockBase, std::memory_order_relaxed);
1036 device->mSamplesDone.store(0, std::memory_order_relaxed);
1040 * Updates device parameters according to the attribute list (caller is
1041 * responsible for holding the list lock).
1043 ALCenum UpdateDeviceParams(ALCdevice *device, const al::span<const int> attrList)
1045 if(attrList.empty() && device->Type == DeviceType::Loopback)
1047 WARN("Missing attributes for loopback device\n");
1048 return ALC_INVALID_VALUE;
1051 uint numMono{device->NumMonoSources};
1052 uint numStereo{device->NumStereoSources};
1053 uint numSends{device->NumAuxSends};
1054 std::optional<StereoEncoding> stereomode;
1055 std::optional<bool> optlimit;
1056 std::optional<uint> optsrate;
1057 std::optional<DevFmtChannels> optchans;
1058 std::optional<DevFmtType> opttype;
1059 std::optional<DevAmbiLayout> optlayout;
1060 std::optional<DevAmbiScaling> optscale;
1061 uint period_size{DefaultUpdateSize};
1062 uint buffer_size{DefaultUpdateSize * DefaultNumUpdates};
1063 int hrtf_id{-1};
1064 uint aorder{0u};
1066 if(device->Type != DeviceType::Loopback)
1068 /* Get default settings from the user configuration */
1070 if(auto freqopt = device->configValue<uint>({}, "frequency"))
1072 optsrate = std::clamp<uint>(*freqopt, MinOutputRate, MaxOutputRate);
1074 const double scale{static_cast<double>(*optsrate) / double{DefaultOutputRate}};
1075 period_size = static_cast<uint>(std::lround(period_size * scale));
1078 if(auto persizeopt = device->configValue<uint>({}, "period_size"))
1079 period_size = std::clamp(*persizeopt, 64u, 8192u);
1080 if(auto numperopt = device->configValue<uint>({}, "periods"))
1081 buffer_size = std::clamp(*numperopt, 2u, 16u) * period_size;
1082 else
1083 buffer_size = period_size * uint{DefaultNumUpdates};
1085 if(auto typeopt = device->configValue<std::string>({}, "sample-type"))
1087 struct TypeMap {
1088 std::string_view name;
1089 DevFmtType type;
1091 constexpr std::array typelist{
1092 TypeMap{"int8"sv, DevFmtByte },
1093 TypeMap{"uint8"sv, DevFmtUByte },
1094 TypeMap{"int16"sv, DevFmtShort },
1095 TypeMap{"uint16"sv, DevFmtUShort},
1096 TypeMap{"int32"sv, DevFmtInt },
1097 TypeMap{"uint32"sv, DevFmtUInt },
1098 TypeMap{"float32"sv, DevFmtFloat },
1101 const ALCchar *fmt{typeopt->c_str()};
1102 auto iter = std::find_if(typelist.begin(), typelist.end(),
1103 [svfmt=std::string_view{fmt}](const TypeMap &entry) -> bool
1104 { return al::case_compare(entry.name, svfmt) == 0; });
1105 if(iter == typelist.end())
1106 ERR("Unsupported sample-type: %s\n", fmt);
1107 else
1108 opttype = iter->type;
1110 if(auto chanopt = device->configValue<std::string>({}, "channels"))
1112 struct ChannelMap {
1113 std::string_view name;
1114 DevFmtChannels chans;
1115 uint8_t order;
1117 constexpr std::array chanlist{
1118 ChannelMap{"mono"sv, DevFmtMono, 0},
1119 ChannelMap{"stereo"sv, DevFmtStereo, 0},
1120 ChannelMap{"quad"sv, DevFmtQuad, 0},
1121 ChannelMap{"surround51"sv, DevFmtX51, 0},
1122 ChannelMap{"surround61"sv, DevFmtX61, 0},
1123 ChannelMap{"surround71"sv, DevFmtX71, 0},
1124 ChannelMap{"surround714"sv, DevFmtX714, 0},
1125 ChannelMap{"surround7144"sv, DevFmtX7144, 0},
1126 ChannelMap{"surround3d71"sv, DevFmtX3D71, 0},
1127 ChannelMap{"surround51rear"sv, DevFmtX51, 0},
1128 ChannelMap{"ambi1"sv, DevFmtAmbi3D, 1},
1129 ChannelMap{"ambi2"sv, DevFmtAmbi3D, 2},
1130 ChannelMap{"ambi3"sv, DevFmtAmbi3D, 3},
1133 const ALCchar *fmt{chanopt->c_str()};
1134 auto iter = std::find_if(chanlist.begin(), chanlist.end(),
1135 [svfmt=std::string_view{fmt}](const ChannelMap &entry) -> bool
1136 { return al::case_compare(entry.name, svfmt) == 0; });
1137 if(iter == chanlist.end())
1138 ERR("Unsupported channels: %s\n", fmt);
1139 else
1141 optchans = iter->chans;
1142 aorder = iter->order;
1145 if(auto ambiopt = device->configValue<std::string>({}, "ambi-format"sv))
1147 if(al::case_compare(*ambiopt, "fuma"sv) == 0)
1149 optlayout = DevAmbiLayout::FuMa;
1150 optscale = DevAmbiScaling::FuMa;
1152 else if(al::case_compare(*ambiopt, "acn+fuma"sv) == 0)
1154 optlayout = DevAmbiLayout::ACN;
1155 optscale = DevAmbiScaling::FuMa;
1157 else if(al::case_compare(*ambiopt, "ambix"sv) == 0
1158 || al::case_compare(*ambiopt, "acn+sn3d"sv) == 0)
1160 optlayout = DevAmbiLayout::ACN;
1161 optscale = DevAmbiScaling::SN3D;
1163 else if(al::case_compare(*ambiopt, "acn+n3d"sv) == 0)
1165 optlayout = DevAmbiLayout::ACN;
1166 optscale = DevAmbiScaling::N3D;
1168 else
1169 ERR("Unsupported ambi-format: %s\n", ambiopt->c_str());
1172 if(auto hrtfopt = device->configValue<std::string>({}, "hrtf"sv))
1174 WARN("general/hrtf is deprecated, please use stereo-encoding instead\n");
1176 if(al::case_compare(*hrtfopt, "true"sv) == 0)
1177 stereomode = StereoEncoding::Hrtf;
1178 else if(al::case_compare(*hrtfopt, "false"sv) == 0)
1180 if(!stereomode || *stereomode == StereoEncoding::Hrtf)
1181 stereomode = StereoEncoding::Default;
1183 else if(al::case_compare(*hrtfopt, "auto"sv) != 0)
1184 ERR("Unexpected hrtf value: %s\n", hrtfopt->c_str());
1188 if(auto encopt = device->configValue<std::string>({}, "stereo-encoding"sv))
1190 if(al::case_compare(*encopt, "basic"sv) == 0 || al::case_compare(*encopt, "panpot"sv) == 0)
1191 stereomode = StereoEncoding::Basic;
1192 else if(al::case_compare(*encopt, "uhj") == 0)
1193 stereomode = StereoEncoding::Uhj;
1194 else if(al::case_compare(*encopt, "hrtf") == 0)
1195 stereomode = StereoEncoding::Hrtf;
1196 else
1197 ERR("Unexpected stereo-encoding: %s\n", encopt->c_str());
1200 // Check for app-specified attributes
1201 if(!attrList.empty())
1203 ALenum outmode{ALC_ANY_SOFT};
1204 std::optional<bool> opthrtf;
1205 int freqAttr{};
1207 #define ATTRIBUTE(a) a: TRACE("%s = %d\n", #a, attrList[attrIdx + 1]);
1208 for(size_t attrIdx{0};attrIdx < attrList.size();attrIdx+=2)
1210 switch(attrList[attrIdx])
1212 case ATTRIBUTE(ALC_FORMAT_CHANNELS_SOFT)
1213 if(device->Type == DeviceType::Loopback)
1214 optchans = DevFmtChannelsFromEnum(attrList[attrIdx + 1]);
1215 break;
1217 case ATTRIBUTE(ALC_FORMAT_TYPE_SOFT)
1218 if(device->Type == DeviceType::Loopback)
1219 opttype = DevFmtTypeFromEnum(attrList[attrIdx + 1]);
1220 break;
1222 case ATTRIBUTE(ALC_FREQUENCY)
1223 freqAttr = attrList[attrIdx + 1];
1224 break;
1226 case ATTRIBUTE(ALC_AMBISONIC_LAYOUT_SOFT)
1227 if(device->Type == DeviceType::Loopback)
1228 optlayout = DevAmbiLayoutFromEnum(attrList[attrIdx + 1]);
1229 break;
1231 case ATTRIBUTE(ALC_AMBISONIC_SCALING_SOFT)
1232 if(device->Type == DeviceType::Loopback)
1233 optscale = DevAmbiScalingFromEnum(attrList[attrIdx + 1]);
1234 break;
1236 case ATTRIBUTE(ALC_AMBISONIC_ORDER_SOFT)
1237 if(device->Type == DeviceType::Loopback)
1238 aorder = static_cast<uint>(attrList[attrIdx + 1]);
1239 break;
1241 case ATTRIBUTE(ALC_MONO_SOURCES)
1242 numMono = static_cast<uint>(attrList[attrIdx + 1]);
1243 if(numMono > INT_MAX) numMono = 0;
1244 break;
1246 case ATTRIBUTE(ALC_STEREO_SOURCES)
1247 numStereo = static_cast<uint>(attrList[attrIdx + 1]);
1248 if(numStereo > INT_MAX) numStereo = 0;
1249 break;
1251 case ATTRIBUTE(ALC_MAX_AUXILIARY_SENDS)
1252 numSends = static_cast<uint>(attrList[attrIdx + 1]);
1253 if(numSends > uint{std::numeric_limits<int>::max()}) numSends = 0;
1254 else numSends = std::min(numSends, uint{MaxSendCount});
1255 break;
1257 case ATTRIBUTE(ALC_HRTF_SOFT)
1258 if(attrList[attrIdx + 1] == ALC_FALSE)
1259 opthrtf = false;
1260 else if(attrList[attrIdx + 1] == ALC_TRUE)
1261 opthrtf = true;
1262 else if(attrList[attrIdx + 1] == ALC_DONT_CARE_SOFT)
1263 opthrtf = std::nullopt;
1264 break;
1266 case ATTRIBUTE(ALC_HRTF_ID_SOFT)
1267 hrtf_id = attrList[attrIdx + 1];
1268 break;
1270 case ATTRIBUTE(ALC_OUTPUT_LIMITER_SOFT)
1271 if(attrList[attrIdx + 1] == ALC_FALSE)
1272 optlimit = false;
1273 else if(attrList[attrIdx + 1] == ALC_TRUE)
1274 optlimit = true;
1275 else if(attrList[attrIdx + 1] == ALC_DONT_CARE_SOFT)
1276 optlimit = std::nullopt;
1277 break;
1279 case ATTRIBUTE(ALC_OUTPUT_MODE_SOFT)
1280 outmode = attrList[attrIdx + 1];
1281 break;
1283 default:
1284 TRACE("0x%04X = %d (0x%x)\n", attrList[attrIdx],
1285 attrList[attrIdx + 1], attrList[attrIdx + 1]);
1286 break;
1289 #undef ATTRIBUTE
1291 if(device->Type == DeviceType::Loopback)
1293 if(!optchans || !opttype)
1294 return ALC_INVALID_VALUE;
1295 if(freqAttr < int{MinOutputRate} || freqAttr > int{MaxOutputRate})
1296 return ALC_INVALID_VALUE;
1297 if(*optchans == DevFmtAmbi3D)
1299 if(!optlayout || !optscale)
1300 return ALC_INVALID_VALUE;
1301 if(aorder < 1 || aorder > MaxAmbiOrder)
1302 return ALC_INVALID_VALUE;
1303 if((*optlayout == DevAmbiLayout::FuMa || *optscale == DevAmbiScaling::FuMa)
1304 && aorder > 3)
1305 return ALC_INVALID_VALUE;
1307 else if(*optchans == DevFmtStereo)
1309 if(opthrtf)
1311 if(*opthrtf)
1312 stereomode = StereoEncoding::Hrtf;
1313 else
1315 if(stereomode.value_or(StereoEncoding::Hrtf) == StereoEncoding::Hrtf)
1316 stereomode = StereoEncoding::Default;
1320 if(outmode == ALC_STEREO_BASIC_SOFT)
1321 stereomode = StereoEncoding::Basic;
1322 else if(outmode == ALC_STEREO_UHJ_SOFT)
1323 stereomode = StereoEncoding::Uhj;
1324 else if(outmode == ALC_STEREO_HRTF_SOFT)
1325 stereomode = StereoEncoding::Hrtf;
1328 optsrate = static_cast<uint>(freqAttr);
1330 else
1332 if(opthrtf)
1334 if(*opthrtf)
1335 stereomode = StereoEncoding::Hrtf;
1336 else
1338 if(stereomode.value_or(StereoEncoding::Hrtf) == StereoEncoding::Hrtf)
1339 stereomode = StereoEncoding::Default;
1343 if(outmode != ALC_ANY_SOFT)
1345 using OutputMode = ALCdevice::OutputMode;
1346 switch(OutputMode(outmode))
1348 case OutputMode::Any: break;
1349 case OutputMode::Mono: optchans = DevFmtMono; break;
1350 case OutputMode::Stereo: optchans = DevFmtStereo; break;
1351 case OutputMode::StereoBasic:
1352 optchans = DevFmtStereo;
1353 stereomode = StereoEncoding::Basic;
1354 break;
1355 case OutputMode::Uhj2:
1356 optchans = DevFmtStereo;
1357 stereomode = StereoEncoding::Uhj;
1358 break;
1359 case OutputMode::Hrtf:
1360 optchans = DevFmtStereo;
1361 stereomode = StereoEncoding::Hrtf;
1362 break;
1363 case OutputMode::Quad: optchans = DevFmtQuad; break;
1364 case OutputMode::X51: optchans = DevFmtX51; break;
1365 case OutputMode::X61: optchans = DevFmtX61; break;
1366 case OutputMode::X71: optchans = DevFmtX71; break;
1370 if(freqAttr)
1372 uint oldrate = optsrate.value_or(DefaultOutputRate);
1373 freqAttr = std::clamp<int>(freqAttr, MinOutputRate, MaxOutputRate);
1375 const double scale{static_cast<double>(freqAttr) / oldrate};
1376 period_size = static_cast<uint>(std::lround(period_size * scale));
1377 buffer_size = static_cast<uint>(std::lround(buffer_size * scale));
1378 optsrate = static_cast<uint>(freqAttr);
1382 /* If a context is already running on the device, stop playback so the
1383 * device attributes can be updated.
1385 if(device->mDeviceState == DeviceState::Playing)
1387 device->Backend->stop();
1388 device->mDeviceState = DeviceState::Unprepared;
1391 UpdateClockBase(device);
1394 if(device->mDeviceState == DeviceState::Playing)
1395 return ALC_NO_ERROR;
1397 device->mDeviceState = DeviceState::Unprepared;
1398 device->AvgSpeakerDist = 0.0f;
1399 device->mNFCtrlFilter = NfcFilter{};
1400 device->mUhjEncoder = nullptr;
1401 device->AmbiDecoder = nullptr;
1402 device->Bs2b = nullptr;
1403 device->PostProcess = nullptr;
1405 device->Limiter = nullptr;
1406 device->ChannelDelays = nullptr;
1408 std::fill(std::begin(device->HrtfAccumData), std::end(device->HrtfAccumData), float2{});
1410 device->Dry.AmbiMap.fill(BFChannelConfig{});
1411 device->Dry.Buffer = {};
1412 std::fill(std::begin(device->NumChannelsPerOrder), std::end(device->NumChannelsPerOrder), 0u);
1413 device->RealOut.RemixMap = {};
1414 device->RealOut.ChannelIndex.fill(InvalidChannelIndex);
1415 device->RealOut.Buffer = {};
1416 device->MixBuffer.clear();
1417 device->MixBuffer.shrink_to_fit();
1419 UpdateClockBase(device);
1420 device->FixedLatency = nanoseconds::zero();
1422 device->DitherDepth = 0.0f;
1423 device->DitherSeed = DitherRNGSeed;
1425 device->mHrtfStatus = ALC_HRTF_DISABLED_SOFT;
1427 /*************************************************************************
1428 * Update device format request
1431 if(device->Type == DeviceType::Loopback)
1433 device->Frequency = *optsrate;
1434 device->FmtChans = *optchans;
1435 device->FmtType = *opttype;
1436 if(device->FmtChans == DevFmtAmbi3D)
1438 device->mAmbiOrder = aorder;
1439 device->mAmbiLayout = *optlayout;
1440 device->mAmbiScale = *optscale;
1442 device->Flags.set(FrequencyRequest).set(ChannelsRequest).set(SampleTypeRequest);
1444 else
1446 device->FmtType = opttype.value_or(DevFmtTypeDefault);
1447 device->FmtChans = optchans.value_or(DevFmtChannelsDefault);
1448 device->mAmbiOrder = 0;
1449 device->BufferSize = buffer_size;
1450 device->UpdateSize = period_size;
1451 device->Frequency = optsrate.value_or(DefaultOutputRate);
1452 device->Flags.set(FrequencyRequest, optsrate.has_value())
1453 .set(ChannelsRequest, optchans.has_value())
1454 .set(SampleTypeRequest, opttype.has_value());
1456 if(device->FmtChans == DevFmtAmbi3D)
1458 device->mAmbiOrder = std::clamp(aorder, 1u, uint{MaxAmbiOrder});
1459 device->mAmbiLayout = optlayout.value_or(DevAmbiLayout::Default);
1460 device->mAmbiScale = optscale.value_or(DevAmbiScaling::Default);
1461 if(device->mAmbiOrder > 3
1462 && (device->mAmbiLayout == DevAmbiLayout::FuMa
1463 || device->mAmbiScale == DevAmbiScaling::FuMa))
1465 ERR("FuMa is incompatible with %d%s order ambisonics (up to 3rd order only)\n",
1466 device->mAmbiOrder, GetCounterSuffix(device->mAmbiOrder));
1467 device->mAmbiOrder = 3;
1472 TRACE("Pre-reset: %s%s, %s%s, %s%uhz, %u / %u buffer\n",
1473 device->Flags.test(ChannelsRequest)?"*":"", DevFmtChannelsString(device->FmtChans),
1474 device->Flags.test(SampleTypeRequest)?"*":"", DevFmtTypeString(device->FmtType),
1475 device->Flags.test(FrequencyRequest)?"*":"", device->Frequency,
1476 device->UpdateSize, device->BufferSize);
1478 const uint oldFreq{device->Frequency};
1479 const DevFmtChannels oldChans{device->FmtChans};
1480 const DevFmtType oldType{device->FmtType};
1481 try {
1482 auto backend = device->Backend.get();
1483 if(!backend->reset())
1484 throw al::backend_exception{al::backend_error::DeviceError, "Device reset failure"};
1486 catch(std::exception &e) {
1487 ERR("Device error: %s\n", e.what());
1488 device->handleDisconnect("%s", e.what());
1489 return ALC_INVALID_DEVICE;
1492 if(device->FmtChans != oldChans && device->Flags.test(ChannelsRequest))
1494 ERR("Failed to set %s, got %s instead\n", DevFmtChannelsString(oldChans),
1495 DevFmtChannelsString(device->FmtChans));
1496 device->Flags.reset(ChannelsRequest);
1498 if(device->FmtType != oldType && device->Flags.test(SampleTypeRequest))
1500 ERR("Failed to set %s, got %s instead\n", DevFmtTypeString(oldType),
1501 DevFmtTypeString(device->FmtType));
1502 device->Flags.reset(SampleTypeRequest);
1504 if(device->Frequency != oldFreq && device->Flags.test(FrequencyRequest))
1506 WARN("Failed to set %uhz, got %uhz instead\n", oldFreq, device->Frequency);
1507 device->Flags.reset(FrequencyRequest);
1510 TRACE("Post-reset: %s, %s, %uhz, %u / %u buffer\n",
1511 DevFmtChannelsString(device->FmtChans), DevFmtTypeString(device->FmtType),
1512 device->Frequency, device->UpdateSize, device->BufferSize);
1514 if(device->Type != DeviceType::Loopback)
1516 if(auto modeopt = device->configValue<std::string>({}, "stereo-mode"))
1518 if(al::case_compare(*modeopt, "headphones"sv) == 0)
1519 device->Flags.set(DirectEar);
1520 else if(al::case_compare(*modeopt, "speakers"sv) == 0)
1521 device->Flags.reset(DirectEar);
1522 else if(al::case_compare(*modeopt, "auto"sv) != 0)
1523 ERR("Unexpected stereo-mode: %s\n", modeopt->c_str());
1527 aluInitRenderer(device, hrtf_id, stereomode);
1529 /* Calculate the max number of sources, and split them between the mono and
1530 * stereo count given the requested number of stereo sources.
1532 if(auto srcsopt = device->configValue<uint>({}, "sources"sv))
1534 if(*srcsopt <= 0) numMono = 256;
1535 else numMono = std::max(*srcsopt, 16u);
1537 else
1539 numMono = std::min(numMono, std::numeric_limits<int>::max()-numStereo);
1540 numMono = std::max(numMono+numStereo, 256u);
1542 numStereo = std::min(numStereo, numMono);
1543 numMono -= numStereo;
1544 device->SourcesMax = numMono + numStereo;
1545 device->NumMonoSources = numMono;
1546 device->NumStereoSources = numStereo;
1548 if(auto sendsopt = device->configValue<uint>({}, "sends"sv))
1549 numSends = std::min(numSends, std::clamp(*sendsopt, 0u, uint{MaxSendCount}));
1550 device->NumAuxSends = numSends;
1552 TRACE("Max sources: %d (%d + %d), effect slots: %d, sends: %d\n",
1553 device->SourcesMax, device->NumMonoSources, device->NumStereoSources,
1554 device->AuxiliaryEffectSlotMax, device->NumAuxSends);
1556 switch(device->FmtChans)
1558 case DevFmtMono: break;
1559 case DevFmtStereo:
1560 if(!device->mUhjEncoder)
1561 device->RealOut.RemixMap = StereoDownmix;
1562 break;
1563 case DevFmtQuad: device->RealOut.RemixMap = QuadDownmix; break;
1564 case DevFmtX51: device->RealOut.RemixMap = X51Downmix; break;
1565 case DevFmtX61: device->RealOut.RemixMap = X61Downmix; break;
1566 case DevFmtX71: device->RealOut.RemixMap = X71Downmix; break;
1567 case DevFmtX714: device->RealOut.RemixMap = X71Downmix; break;
1568 case DevFmtX7144: device->RealOut.RemixMap = X71Downmix; break;
1569 case DevFmtX3D71: device->RealOut.RemixMap = X51Downmix; break;
1570 case DevFmtAmbi3D: break;
1573 size_t sample_delay{0};
1574 if(auto *encoder{device->mUhjEncoder.get()})
1575 sample_delay += encoder->getDelay();
1577 if(device->getConfigValueBool({}, "dither"sv, true))
1579 int depth{device->configValue<int>({}, "dither-depth"sv).value_or(0)};
1580 if(depth <= 0)
1582 switch(device->FmtType)
1584 case DevFmtByte:
1585 case DevFmtUByte:
1586 depth = 8;
1587 break;
1588 case DevFmtShort:
1589 case DevFmtUShort:
1590 depth = 16;
1591 break;
1592 case DevFmtInt:
1593 case DevFmtUInt:
1594 case DevFmtFloat:
1595 break;
1599 if(depth > 0)
1601 depth = std::clamp(depth, 2, 24);
1602 device->DitherDepth = std::pow(2.0f, static_cast<float>(depth-1));
1605 if(!(device->DitherDepth > 0.0f))
1606 TRACE("Dithering disabled\n");
1607 else
1608 TRACE("Dithering enabled (%d-bit, %g)\n", float2int(std::log2(device->DitherDepth)+0.5f)+1,
1609 device->DitherDepth);
1611 if(!optlimit)
1612 optlimit = device->configValue<bool>({}, "output-limiter");
1614 /* If the gain limiter is unset, use the limiter for integer-based output
1615 * (where samples must be clamped), and don't for floating-point (which can
1616 * take unclamped samples).
1618 if(!optlimit)
1620 switch(device->FmtType)
1622 case DevFmtByte:
1623 case DevFmtUByte:
1624 case DevFmtShort:
1625 case DevFmtUShort:
1626 case DevFmtInt:
1627 case DevFmtUInt:
1628 optlimit = true;
1629 break;
1630 case DevFmtFloat:
1631 break;
1634 if(!optlimit.value_or(false))
1635 TRACE("Output limiter disabled\n");
1636 else
1638 float thrshld{1.0f};
1639 switch(device->FmtType)
1641 case DevFmtByte:
1642 case DevFmtUByte:
1643 thrshld = 127.0f / 128.0f;
1644 break;
1645 case DevFmtShort:
1646 case DevFmtUShort:
1647 thrshld = 32767.0f / 32768.0f;
1648 break;
1649 case DevFmtInt:
1650 case DevFmtUInt:
1651 case DevFmtFloat:
1652 break;
1654 if(device->DitherDepth > 0.0f)
1655 thrshld -= 1.0f / device->DitherDepth;
1657 const float thrshld_dB{std::log10(thrshld) * 20.0f};
1658 auto limiter = CreateDeviceLimiter(device, thrshld_dB);
1660 sample_delay += limiter->getLookAhead();
1661 device->Limiter = std::move(limiter);
1662 TRACE("Output limiter enabled, %.4fdB limit\n", thrshld_dB);
1665 /* Convert the sample delay from samples to nanosamples to nanoseconds. */
1666 sample_delay = std::min<size_t>(sample_delay, std::numeric_limits<int>::max());
1667 device->FixedLatency += nanoseconds{seconds{sample_delay}} / device->Frequency;
1668 TRACE("Fixed device latency: %" PRId64 "ns\n", int64_t{device->FixedLatency.count()});
1670 FPUCtl mixer_mode{};
1671 auto reset_context = [device](ContextBase *ctxbase)
1673 auto *context = static_cast<ALCcontext*>(ctxbase);
1675 std::unique_lock<std::mutex> proplock{context->mPropLock};
1676 std::unique_lock<std::mutex> slotlock{context->mEffectSlotLock};
1678 /* Clear out unused effect slot clusters. */
1679 auto slot_cluster_not_in_use = [](ContextBase::EffectSlotCluster &clusterptr) -> bool
1681 return std::none_of(clusterptr->begin(), clusterptr->end(),
1682 std::mem_fn(&EffectSlot::InUse));
1684 auto slotcluster_end = std::remove_if(context->mEffectSlotClusters.begin(),
1685 context->mEffectSlotClusters.end(), slot_cluster_not_in_use);
1686 context->mEffectSlotClusters.erase(slotcluster_end, context->mEffectSlotClusters.end());
1688 /* Free all wet buffers. Any in use will be reallocated with an updated
1689 * configuration in aluInitEffectPanning.
1691 auto clear_wetbuffers = [](ContextBase::EffectSlotCluster &clusterptr)
1693 auto clear_buffer = [](EffectSlot &slot)
1695 slot.mWetBuffer.clear();
1696 slot.mWetBuffer.shrink_to_fit();
1697 slot.Wet.Buffer = {};
1699 std::for_each(clusterptr->begin(), clusterptr->end(), clear_buffer);
1701 std::for_each(context->mEffectSlotClusters.begin(), context->mEffectSlotClusters.end(),
1702 clear_wetbuffers);
1704 if(ALeffectslot *slot{context->mDefaultSlot.get()})
1706 auto *slotbase = slot->mSlot;
1707 aluInitEffectPanning(slotbase, context);
1709 if(auto *props = slotbase->Update.exchange(nullptr, std::memory_order_relaxed))
1710 AtomicReplaceHead(context->mFreeEffectSlotProps, props);
1712 EffectState *state{slot->Effect.State.get()};
1713 state->mOutTarget = device->Dry.Buffer;
1714 state->deviceUpdate(device, slot->Buffer);
1715 slot->mPropsDirty = true;
1718 if(EffectSlotArray *curarray{context->mActiveAuxSlots.load(std::memory_order_relaxed)})
1719 std::fill(curarray->begin()+ptrdiff_t(curarray->size()>>1), curarray->end(), nullptr);
1720 auto reset_slots = [device,context](EffectSlotSubList &sublist)
1722 uint64_t usemask{~sublist.FreeMask};
1723 while(usemask)
1725 const auto idx = static_cast<uint>(al::countr_zero(usemask));
1726 auto &slot = (*sublist.EffectSlots)[idx];
1727 usemask &= ~(1_u64 << idx);
1729 auto *slotbase = slot.mSlot;
1730 aluInitEffectPanning(slotbase, context);
1732 if(auto *props = slotbase->Update.exchange(nullptr, std::memory_order_relaxed))
1733 AtomicReplaceHead(context->mFreeEffectSlotProps, props);
1735 EffectState *state{slot.Effect.State.get()};
1736 state->mOutTarget = device->Dry.Buffer;
1737 state->deviceUpdate(device, slot.Buffer);
1738 slot.mPropsDirty = true;
1741 std::for_each(context->mEffectSlotList.begin(), context->mEffectSlotList.end(),
1742 reset_slots);
1744 /* Clear all effect slot props to let them get allocated again. */
1745 context->mEffectSlotPropClusters.clear();
1746 context->mFreeEffectSlotProps.store(nullptr, std::memory_order_relaxed);
1747 slotlock.unlock();
1749 std::unique_lock<std::mutex> srclock{context->mSourceLock};
1750 const uint num_sends{device->NumAuxSends};
1751 auto reset_sources = [num_sends](SourceSubList &sublist)
1753 uint64_t usemask{~sublist.FreeMask};
1754 while(usemask)
1756 const auto idx = static_cast<uint>(al::countr_zero(usemask));
1757 auto &source = (*sublist.Sources)[idx];
1758 usemask &= ~(1_u64 << idx);
1760 auto clear_send = [](ALsource::SendData &send) -> void
1762 if(send.Slot)
1763 DecrementRef(send.Slot->ref);
1764 send.Slot = nullptr;
1765 send.Gain = 1.0f;
1766 send.GainHF = 1.0f;
1767 send.HFReference = LowPassFreqRef;
1768 send.GainLF = 1.0f;
1769 send.LFReference = HighPassFreqRef;
1771 const auto sends = al::span{source.Send}.subspan(num_sends);
1772 std::for_each(sends.begin(), sends.end(), clear_send);
1774 source.mPropsDirty = true;
1777 std::for_each(context->mSourceList.begin(), context->mSourceList.end(), reset_sources);
1779 auto reset_voice = [device,num_sends,context](Voice *voice)
1781 /* Clear extraneous property set sends. */
1782 const auto sendparams = al::span{voice->mProps.Send}.subspan(num_sends);
1783 std::fill(sendparams.begin(), sendparams.end(), VoiceProps::SendData{});
1785 std::fill(voice->mSend.begin()+num_sends, voice->mSend.end(), Voice::TargetData{});
1786 auto clear_wetparams = [num_sends](Voice::ChannelData &chandata)
1788 const auto wetparams = al::span{chandata.mWetParams}.subspan(num_sends);
1789 std::fill(wetparams.begin(), wetparams.end(), SendParams{});
1791 std::for_each(voice->mChans.begin(), voice->mChans.end(), clear_wetparams);
1793 if(VoicePropsItem *props{voice->mUpdate.exchange(nullptr, std::memory_order_relaxed)})
1794 AtomicReplaceHead(context->mFreeVoiceProps, props);
1796 /* Force the voice to stopped if it was stopping. */
1797 Voice::State vstate{Voice::Stopping};
1798 voice->mPlayState.compare_exchange_strong(vstate, Voice::Stopped,
1799 std::memory_order_acquire, std::memory_order_acquire);
1800 if(voice->mSourceID.load(std::memory_order_relaxed) == 0u)
1801 return;
1803 voice->prepare(device);
1805 const auto voicespan = context->getVoicesSpan();
1806 std::for_each(voicespan.begin(), voicespan.end(), reset_voice);
1808 /* Clear all voice props to let them get allocated again. */
1809 context->mVoicePropClusters.clear();
1810 context->mFreeVoiceProps.store(nullptr, std::memory_order_relaxed);
1811 srclock.unlock();
1813 context->mPropsDirty = false;
1814 UpdateContextProps(context);
1815 UpdateAllEffectSlotProps(context);
1816 UpdateAllSourceProps(context);
1818 auto ctxspan = al::span{*device->mContexts.load()};
1819 std::for_each(ctxspan.begin(), ctxspan.end(), reset_context);
1820 mixer_mode.leave();
1822 device->mDeviceState = DeviceState::Configured;
1823 if(!device->Flags.test(DevicePaused))
1825 try {
1826 auto backend = device->Backend.get();
1827 backend->start();
1828 device->mDeviceState = DeviceState::Playing;
1830 catch(al::backend_exception& e) {
1831 ERR("%s\n", e.what());
1832 device->handleDisconnect("%s", e.what());
1833 return ALC_INVALID_DEVICE;
1835 TRACE("Post-start: %s, %s, %uhz, %u / %u buffer\n",
1836 DevFmtChannelsString(device->FmtChans), DevFmtTypeString(device->FmtType),
1837 device->Frequency, device->UpdateSize, device->BufferSize);
1840 return ALC_NO_ERROR;
1844 * Updates device parameters as above, and also first clears the disconnected
1845 * status, if set.
1847 bool ResetDeviceParams(ALCdevice *device, const al::span<const int> attrList)
1849 /* If the device was disconnected, reset it since we're opened anew. */
1850 if(!device->Connected.load(std::memory_order_relaxed)) UNLIKELY
1852 /* Make sure disconnection is finished before continuing on. */
1853 std::ignore = device->waitForMix();
1855 for(ContextBase *ctxbase : *device->mContexts.load(std::memory_order_acquire))
1857 auto *ctx = static_cast<ALCcontext*>(ctxbase);
1858 if(!ctx->mStopVoicesOnDisconnect.load(std::memory_order_acquire))
1859 continue;
1861 /* Clear any pending voice changes and reallocate voices to get a
1862 * clean restart.
1864 std::lock_guard<std::mutex> sourcelock{ctx->mSourceLock};
1865 auto *vchg = ctx->mCurrentVoiceChange.load(std::memory_order_acquire);
1866 while(auto *next = vchg->mNext.load(std::memory_order_acquire))
1867 vchg = next;
1868 ctx->mCurrentVoiceChange.store(vchg, std::memory_order_release);
1870 ctx->mVoicePropClusters.clear();
1871 ctx->mFreeVoiceProps.store(nullptr, std::memory_order_relaxed);
1873 ctx->mVoiceClusters.clear();
1874 ctx->allocVoices(std::max<size_t>(256,
1875 ctx->mActiveVoiceCount.load(std::memory_order_relaxed)));
1878 device->Connected.store(true);
1881 ALCenum err{UpdateDeviceParams(device, attrList)};
1882 if(err == ALC_NO_ERROR) LIKELY return ALC_TRUE;
1884 alcSetError(device, err);
1885 return ALC_FALSE;
1889 /** Checks if the device handle is valid, and returns a new reference if so. */
1890 DeviceRef VerifyDevice(ALCdevice *device)
1892 std::lock_guard<std::recursive_mutex> listlock{ListLock};
1893 auto iter = std::lower_bound(DeviceList.begin(), DeviceList.end(), device);
1894 if(iter != DeviceList.end() && *iter == device)
1896 (*iter)->add_ref();
1897 return DeviceRef{*iter};
1899 return nullptr;
1904 * Checks if the given context is valid, returning a new reference to it if so.
1906 ContextRef VerifyContext(ALCcontext *context)
1908 std::lock_guard<std::recursive_mutex> listlock{ListLock};
1909 auto iter = std::lower_bound(ContextList.begin(), ContextList.end(), context);
1910 if(iter != ContextList.end() && *iter == context)
1912 (*iter)->add_ref();
1913 return ContextRef{*iter};
1915 return nullptr;
1918 } // namespace
1920 FORCE_ALIGN void ALC_APIENTRY alsoft_set_log_callback(LPALSOFTLOGCALLBACK callback, void *userptr) noexcept
1922 al_set_log_callback(callback, userptr);
1925 /** Returns a new reference to the currently active context for this thread. */
1926 ContextRef GetContextRef() noexcept
1928 ALCcontext *context{ALCcontext::getThreadContext()};
1929 if(context)
1930 context->add_ref();
1931 else
1933 while(ALCcontext::sGlobalContextLock.exchange(true, std::memory_order_acquire)) {
1934 /* Wait to make sure another thread isn't trying to change the
1935 * current context and bring its refcount to 0.
1938 context = ALCcontext::sGlobalContext.load(std::memory_order_acquire);
1939 if(context) LIKELY context->add_ref();
1940 ALCcontext::sGlobalContextLock.store(false, std::memory_order_release);
1942 return ContextRef{context};
1945 void alcSetError(ALCdevice *device, ALCenum errorCode)
1947 WARN("Error generated on device %p, code 0x%04x\n", voidp{device}, errorCode);
1948 if(TrapALCError)
1950 #ifdef _WIN32
1951 /* DebugBreak() will cause an exception if there is no debugger */
1952 if(IsDebuggerPresent())
1953 DebugBreak();
1954 #elif defined(SIGTRAP)
1955 raise(SIGTRAP);
1956 #endif
1959 if(device)
1960 device->LastError.store(errorCode);
1961 else
1962 LastNullDeviceError.store(errorCode);
1965 /************************************************
1966 * Standard ALC functions
1967 ************************************************/
1969 ALC_API ALCenum ALC_APIENTRY alcGetError(ALCdevice *device) noexcept
1971 DeviceRef dev{VerifyDevice(device)};
1972 if(dev) return dev->LastError.exchange(ALC_NO_ERROR);
1973 return LastNullDeviceError.exchange(ALC_NO_ERROR);
1977 ALC_API void ALC_APIENTRY alcSuspendContext(ALCcontext *context) noexcept
1979 ContextRef ctx{VerifyContext(context)};
1980 if(!ctx)
1982 alcSetError(nullptr, ALC_INVALID_CONTEXT);
1983 return;
1986 if(ctx->mContextFlags.test(ContextFlags::DebugBit)) UNLIKELY
1987 ctx->debugMessage(DebugSource::API, DebugType::Portability, 0, DebugSeverity::Medium,
1988 "alcSuspendContext behavior is not portable -- some implementations suspend all "
1989 "rendering, some only defer property changes, and some are completely no-op; consider "
1990 "using alcDevicePauseSOFT to suspend all rendering, or alDeferUpdatesSOFT to only "
1991 "defer property changes");
1993 if(SuspendDefers)
1995 std::lock_guard<std::mutex> proplock{ctx->mPropLock};
1996 ctx->deferUpdates();
2000 ALC_API void ALC_APIENTRY alcProcessContext(ALCcontext *context) noexcept
2002 ContextRef ctx{VerifyContext(context)};
2003 if(!ctx)
2005 alcSetError(nullptr, ALC_INVALID_CONTEXT);
2006 return;
2009 if(ctx->mContextFlags.test(ContextFlags::DebugBit)) UNLIKELY
2010 ctx->debugMessage(DebugSource::API, DebugType::Portability, 1, DebugSeverity::Medium,
2011 "alcProcessContext behavior is not portable -- some implementations resume rendering, "
2012 "some apply deferred property changes, and some are completely no-op; consider using "
2013 "alcDeviceResumeSOFT to resume rendering, or alProcessUpdatesSOFT to apply deferred "
2014 "property changes");
2016 if(SuspendDefers)
2018 std::lock_guard<std::mutex> proplock{ctx->mPropLock};
2019 ctx->processUpdates();
2024 ALC_API const ALCchar* ALC_APIENTRY alcGetString(ALCdevice *Device, ALCenum param) noexcept
2026 const ALCchar *value{nullptr};
2028 switch(param)
2030 case ALC_NO_ERROR: value = GetNoErrorString(); break;
2031 case ALC_INVALID_ENUM: value = GetInvalidEnumString(); break;
2032 case ALC_INVALID_VALUE: value = GetInvalidValueString(); break;
2033 case ALC_INVALID_DEVICE: value = GetInvalidDeviceString(); break;
2034 case ALC_INVALID_CONTEXT: value = GetInvalidContextString(); break;
2035 case ALC_OUT_OF_MEMORY: value = GetOutOfMemoryString(); break;
2037 case ALC_DEVICE_SPECIFIER:
2038 value = GetDefaultName();
2039 break;
2041 case ALC_ALL_DEVICES_SPECIFIER:
2042 if(DeviceRef dev{VerifyDevice(Device)})
2044 if(dev->Type == DeviceType::Capture)
2045 alcSetError(dev.get(), ALC_INVALID_ENUM);
2046 else if(dev->Type == DeviceType::Loopback)
2047 value = GetDefaultName();
2048 else
2050 std::lock_guard<std::mutex> statelock{dev->StateLock};
2051 value = dev->mDeviceName.c_str();
2054 else
2056 ProbeAllDevicesList();
2057 value = alcAllDevicesList.c_str();
2059 break;
2061 case ALC_CAPTURE_DEVICE_SPECIFIER:
2062 if(DeviceRef dev{VerifyDevice(Device)})
2064 if(dev->Type != DeviceType::Capture)
2065 alcSetError(dev.get(), ALC_INVALID_ENUM);
2066 else
2068 std::lock_guard<std::mutex> statelock{dev->StateLock};
2069 value = dev->mDeviceName.c_str();
2072 else
2074 ProbeCaptureDeviceList();
2075 value = alcCaptureDeviceList.c_str();
2077 break;
2079 /* Default devices are always first in the list */
2080 case ALC_DEFAULT_DEVICE_SPECIFIER:
2081 value = GetDefaultName();
2082 break;
2084 case ALC_DEFAULT_ALL_DEVICES_SPECIFIER:
2085 if(alcAllDevicesList.empty())
2086 ProbeAllDevicesList();
2088 /* Copy first entry as default. */
2089 if(alcAllDevicesArray.empty())
2090 value = GetDefaultName();
2091 else
2093 alcDefaultAllDevicesSpecifier = alcAllDevicesArray.front();
2094 value = alcDefaultAllDevicesSpecifier.c_str();
2096 break;
2098 case ALC_CAPTURE_DEFAULT_DEVICE_SPECIFIER:
2099 if(alcCaptureDeviceList.empty())
2100 ProbeCaptureDeviceList();
2102 /* Copy first entry as default. */
2103 if(alcCaptureDeviceArray.empty())
2104 value = GetDefaultName();
2105 else
2107 alcCaptureDefaultDeviceSpecifier = alcCaptureDeviceArray.front();
2108 value = alcCaptureDefaultDeviceSpecifier.c_str();
2110 break;
2112 case ALC_EXTENSIONS:
2113 if(VerifyDevice(Device))
2114 value = GetExtensionList().data();
2115 else
2116 value = GetNoDeviceExtList().data();
2117 break;
2119 case ALC_HRTF_SPECIFIER_SOFT:
2120 if(DeviceRef dev{VerifyDevice(Device)})
2122 std::lock_guard<std::mutex> statelock{dev->StateLock};
2123 value = (dev->mHrtf ? dev->mHrtfName.c_str() : "");
2125 else
2126 alcSetError(nullptr, ALC_INVALID_DEVICE);
2127 break;
2129 default:
2130 alcSetError(VerifyDevice(Device).get(), ALC_INVALID_ENUM);
2131 break;
2134 return value;
2138 static size_t GetIntegerv(ALCdevice *device, ALCenum param, const al::span<int> values)
2140 if(values.empty())
2142 alcSetError(device, ALC_INVALID_VALUE);
2143 return 0;
2146 if(!device)
2148 switch(param)
2150 case ALC_MAJOR_VERSION:
2151 values[0] = alcMajorVersion;
2152 return 1;
2153 case ALC_MINOR_VERSION:
2154 values[0] = alcMinorVersion;
2155 return 1;
2157 case ALC_EFX_MAJOR_VERSION:
2158 values[0] = alcEFXMajorVersion;
2159 return 1;
2160 case ALC_EFX_MINOR_VERSION:
2161 values[0] = alcEFXMinorVersion;
2162 return 1;
2163 case ALC_MAX_AUXILIARY_SENDS:
2164 values[0] = MaxSendCount;
2165 return 1;
2167 case ALC_ATTRIBUTES_SIZE:
2168 case ALC_ALL_ATTRIBUTES:
2169 case ALC_FREQUENCY:
2170 case ALC_REFRESH:
2171 case ALC_SYNC:
2172 case ALC_MONO_SOURCES:
2173 case ALC_STEREO_SOURCES:
2174 case ALC_CAPTURE_SAMPLES:
2175 case ALC_FORMAT_CHANNELS_SOFT:
2176 case ALC_FORMAT_TYPE_SOFT:
2177 case ALC_AMBISONIC_LAYOUT_SOFT:
2178 case ALC_AMBISONIC_SCALING_SOFT:
2179 case ALC_AMBISONIC_ORDER_SOFT:
2180 case ALC_MAX_AMBISONIC_ORDER_SOFT:
2181 alcSetError(nullptr, ALC_INVALID_DEVICE);
2182 return 0;
2184 default:
2185 alcSetError(nullptr, ALC_INVALID_ENUM);
2187 return 0;
2190 std::lock_guard<std::mutex> statelock{device->StateLock};
2191 if(device->Type == DeviceType::Capture)
2193 static constexpr int MaxCaptureAttributes{9};
2194 switch(param)
2196 case ALC_ATTRIBUTES_SIZE:
2197 values[0] = MaxCaptureAttributes;
2198 return 1;
2199 case ALC_ALL_ATTRIBUTES:
2200 if(values.size() >= MaxCaptureAttributes)
2202 size_t i{0};
2203 values[i++] = ALC_MAJOR_VERSION;
2204 values[i++] = alcMajorVersion;
2205 values[i++] = ALC_MINOR_VERSION;
2206 values[i++] = alcMinorVersion;
2207 values[i++] = ALC_CAPTURE_SAMPLES;
2208 values[i++] = static_cast<int>(device->Backend->availableSamples());
2209 values[i++] = ALC_CONNECTED;
2210 values[i++] = device->Connected.load(std::memory_order_relaxed);
2211 values[i++] = 0;
2212 assert(i == MaxCaptureAttributes);
2213 return i;
2215 alcSetError(device, ALC_INVALID_VALUE);
2216 return 0;
2218 case ALC_MAJOR_VERSION:
2219 values[0] = alcMajorVersion;
2220 return 1;
2221 case ALC_MINOR_VERSION:
2222 values[0] = alcMinorVersion;
2223 return 1;
2225 case ALC_CAPTURE_SAMPLES:
2226 values[0] = static_cast<int>(device->Backend->availableSamples());
2227 return 1;
2229 case ALC_CONNECTED:
2230 values[0] = device->Connected.load(std::memory_order_acquire);
2231 return 1;
2233 default:
2234 alcSetError(device, ALC_INVALID_ENUM);
2236 return 0;
2239 /* render device */
2240 auto NumAttrsForDevice = [](const ALCdevice *aldev) noexcept -> uint8_t
2242 if(aldev->Type == DeviceType::Loopback && aldev->FmtChans == DevFmtAmbi3D)
2243 return 37;
2244 return 31;
2246 switch(param)
2248 case ALC_ATTRIBUTES_SIZE:
2249 values[0] = NumAttrsForDevice(device);
2250 return 1;
2252 case ALC_ALL_ATTRIBUTES:
2253 if(values.size() >= NumAttrsForDevice(device))
2255 size_t i{0};
2256 values[i++] = ALC_MAJOR_VERSION;
2257 values[i++] = alcMajorVersion;
2258 values[i++] = ALC_MINOR_VERSION;
2259 values[i++] = alcMinorVersion;
2260 values[i++] = ALC_EFX_MAJOR_VERSION;
2261 values[i++] = alcEFXMajorVersion;
2262 values[i++] = ALC_EFX_MINOR_VERSION;
2263 values[i++] = alcEFXMinorVersion;
2265 values[i++] = ALC_FREQUENCY;
2266 values[i++] = static_cast<int>(device->Frequency);
2267 if(device->Type != DeviceType::Loopback)
2269 values[i++] = ALC_REFRESH;
2270 values[i++] = static_cast<int>(device->Frequency / device->UpdateSize);
2272 values[i++] = ALC_SYNC;
2273 values[i++] = ALC_FALSE;
2275 else
2277 if(device->FmtChans == DevFmtAmbi3D)
2279 values[i++] = ALC_AMBISONIC_LAYOUT_SOFT;
2280 values[i++] = EnumFromDevAmbi(device->mAmbiLayout);
2282 values[i++] = ALC_AMBISONIC_SCALING_SOFT;
2283 values[i++] = EnumFromDevAmbi(device->mAmbiScale);
2285 values[i++] = ALC_AMBISONIC_ORDER_SOFT;
2286 values[i++] = static_cast<int>(device->mAmbiOrder);
2289 values[i++] = ALC_FORMAT_CHANNELS_SOFT;
2290 values[i++] = EnumFromDevFmt(device->FmtChans);
2292 values[i++] = ALC_FORMAT_TYPE_SOFT;
2293 values[i++] = EnumFromDevFmt(device->FmtType);
2296 values[i++] = ALC_MONO_SOURCES;
2297 values[i++] = static_cast<int>(device->NumMonoSources);
2299 values[i++] = ALC_STEREO_SOURCES;
2300 values[i++] = static_cast<int>(device->NumStereoSources);
2302 values[i++] = ALC_MAX_AUXILIARY_SENDS;
2303 values[i++] = static_cast<int>(device->NumAuxSends);
2305 values[i++] = ALC_HRTF_SOFT;
2306 values[i++] = (device->mHrtf ? ALC_TRUE : ALC_FALSE);
2308 values[i++] = ALC_HRTF_STATUS_SOFT;
2309 values[i++] = device->mHrtfStatus;
2311 values[i++] = ALC_OUTPUT_LIMITER_SOFT;
2312 values[i++] = device->Limiter ? ALC_TRUE : ALC_FALSE;
2314 values[i++] = ALC_MAX_AMBISONIC_ORDER_SOFT;
2315 values[i++] = MaxAmbiOrder;
2317 values[i++] = ALC_OUTPUT_MODE_SOFT;
2318 values[i++] = static_cast<ALCenum>(device->getOutputMode1());
2320 values[i++] = 0;
2321 assert(i == NumAttrsForDevice(device));
2322 return i;
2324 alcSetError(device, ALC_INVALID_VALUE);
2325 return 0;
2327 case ALC_MAJOR_VERSION:
2328 values[0] = alcMajorVersion;
2329 return 1;
2331 case ALC_MINOR_VERSION:
2332 values[0] = alcMinorVersion;
2333 return 1;
2335 case ALC_EFX_MAJOR_VERSION:
2336 values[0] = alcEFXMajorVersion;
2337 return 1;
2339 case ALC_EFX_MINOR_VERSION:
2340 values[0] = alcEFXMinorVersion;
2341 return 1;
2343 case ALC_FREQUENCY:
2344 values[0] = static_cast<int>(device->Frequency);
2345 return 1;
2347 case ALC_REFRESH:
2348 if(device->Type == DeviceType::Loopback)
2350 alcSetError(device, ALC_INVALID_DEVICE);
2351 return 0;
2353 values[0] = static_cast<int>(device->Frequency / device->UpdateSize);
2354 return 1;
2356 case ALC_SYNC:
2357 if(device->Type == DeviceType::Loopback)
2359 alcSetError(device, ALC_INVALID_DEVICE);
2360 return 0;
2362 values[0] = ALC_FALSE;
2363 return 1;
2365 case ALC_FORMAT_CHANNELS_SOFT:
2366 if(device->Type != DeviceType::Loopback)
2368 alcSetError(device, ALC_INVALID_DEVICE);
2369 return 0;
2371 values[0] = EnumFromDevFmt(device->FmtChans);
2372 return 1;
2374 case ALC_FORMAT_TYPE_SOFT:
2375 if(device->Type != DeviceType::Loopback)
2377 alcSetError(device, ALC_INVALID_DEVICE);
2378 return 0;
2380 values[0] = EnumFromDevFmt(device->FmtType);
2381 return 1;
2383 case ALC_AMBISONIC_LAYOUT_SOFT:
2384 if(device->Type != DeviceType::Loopback || device->FmtChans != DevFmtAmbi3D)
2386 alcSetError(device, ALC_INVALID_DEVICE);
2387 return 0;
2389 values[0] = EnumFromDevAmbi(device->mAmbiLayout);
2390 return 1;
2392 case ALC_AMBISONIC_SCALING_SOFT:
2393 if(device->Type != DeviceType::Loopback || device->FmtChans != DevFmtAmbi3D)
2395 alcSetError(device, ALC_INVALID_DEVICE);
2396 return 0;
2398 values[0] = EnumFromDevAmbi(device->mAmbiScale);
2399 return 1;
2401 case ALC_AMBISONIC_ORDER_SOFT:
2402 if(device->Type != DeviceType::Loopback || device->FmtChans != DevFmtAmbi3D)
2404 alcSetError(device, ALC_INVALID_DEVICE);
2405 return 0;
2407 values[0] = static_cast<int>(device->mAmbiOrder);
2408 return 1;
2410 case ALC_MONO_SOURCES:
2411 values[0] = static_cast<int>(device->NumMonoSources);
2412 return 1;
2414 case ALC_STEREO_SOURCES:
2415 values[0] = static_cast<int>(device->NumStereoSources);
2416 return 1;
2418 case ALC_MAX_AUXILIARY_SENDS:
2419 values[0] = static_cast<int>(device->NumAuxSends);
2420 return 1;
2422 case ALC_CONNECTED:
2423 values[0] = device->Connected.load(std::memory_order_acquire);
2424 return 1;
2426 case ALC_HRTF_SOFT:
2427 values[0] = (device->mHrtf ? ALC_TRUE : ALC_FALSE);
2428 return 1;
2430 case ALC_HRTF_STATUS_SOFT:
2431 values[0] = device->mHrtfStatus;
2432 return 1;
2434 case ALC_NUM_HRTF_SPECIFIERS_SOFT:
2435 device->enumerateHrtfs();
2436 values[0] = static_cast<int>(std::min(device->mHrtfList.size(),
2437 size_t{std::numeric_limits<int>::max()}));
2438 return 1;
2440 case ALC_OUTPUT_LIMITER_SOFT:
2441 values[0] = device->Limiter ? ALC_TRUE : ALC_FALSE;
2442 return 1;
2444 case ALC_MAX_AMBISONIC_ORDER_SOFT:
2445 values[0] = MaxAmbiOrder;
2446 return 1;
2448 case ALC_OUTPUT_MODE_SOFT:
2449 values[0] = static_cast<ALCenum>(device->getOutputMode1());
2450 return 1;
2452 default:
2453 alcSetError(device, ALC_INVALID_ENUM);
2455 return 0;
2458 ALC_API void ALC_APIENTRY alcGetIntegerv(ALCdevice *device, ALCenum param, ALCsizei size, ALCint *values) noexcept
2460 DeviceRef dev{VerifyDevice(device)};
2461 if(size <= 0 || values == nullptr)
2462 alcSetError(dev.get(), ALC_INVALID_VALUE);
2463 else
2464 GetIntegerv(dev.get(), param, {values, static_cast<uint>(size)});
2467 ALC_API void ALC_APIENTRY alcGetInteger64vSOFT(ALCdevice *device, ALCenum pname, ALCsizei size, ALCint64SOFT *values) noexcept
2469 DeviceRef dev{VerifyDevice(device)};
2470 if(size <= 0 || values == nullptr)
2472 alcSetError(dev.get(), ALC_INVALID_VALUE);
2473 return;
2475 const auto valuespan = al::span{values, static_cast<uint>(size)};
2476 if(!dev || dev->Type == DeviceType::Capture)
2478 auto ivals = std::vector<int>(valuespan.size());
2479 if(size_t got{GetIntegerv(dev.get(), pname, ivals)})
2480 std::copy_n(ivals.cbegin(), got, valuespan.begin());
2481 return;
2483 /* render device */
2484 auto NumAttrsForDevice = [](ALCdevice *aldev) noexcept -> size_t
2486 if(aldev->Type == DeviceType::Loopback && aldev->FmtChans == DevFmtAmbi3D)
2487 return 41;
2488 return 35;
2490 std::lock_guard<std::mutex> statelock{dev->StateLock};
2491 switch(pname)
2493 case ALC_ATTRIBUTES_SIZE:
2494 valuespan[0] = static_cast<ALCint64SOFT>(NumAttrsForDevice(dev.get()));
2495 break;
2497 case ALC_ALL_ATTRIBUTES:
2498 if(valuespan.size() < NumAttrsForDevice(dev.get()))
2499 alcSetError(dev.get(), ALC_INVALID_VALUE);
2500 else
2502 size_t i{0};
2503 valuespan[i++] = ALC_FREQUENCY;
2504 valuespan[i++] = dev->Frequency;
2506 if(dev->Type != DeviceType::Loopback)
2508 valuespan[i++] = ALC_REFRESH;
2509 valuespan[i++] = dev->Frequency / dev->UpdateSize;
2511 valuespan[i++] = ALC_SYNC;
2512 valuespan[i++] = ALC_FALSE;
2514 else
2516 valuespan[i++] = ALC_FORMAT_CHANNELS_SOFT;
2517 valuespan[i++] = EnumFromDevFmt(dev->FmtChans);
2519 valuespan[i++] = ALC_FORMAT_TYPE_SOFT;
2520 valuespan[i++] = EnumFromDevFmt(dev->FmtType);
2522 if(dev->FmtChans == DevFmtAmbi3D)
2524 valuespan[i++] = ALC_AMBISONIC_LAYOUT_SOFT;
2525 valuespan[i++] = EnumFromDevAmbi(dev->mAmbiLayout);
2527 valuespan[i++] = ALC_AMBISONIC_SCALING_SOFT;
2528 valuespan[i++] = EnumFromDevAmbi(dev->mAmbiScale);
2530 valuespan[i++] = ALC_AMBISONIC_ORDER_SOFT;
2531 valuespan[i++] = dev->mAmbiOrder;
2535 valuespan[i++] = ALC_MONO_SOURCES;
2536 valuespan[i++] = dev->NumMonoSources;
2538 valuespan[i++] = ALC_STEREO_SOURCES;
2539 valuespan[i++] = dev->NumStereoSources;
2541 valuespan[i++] = ALC_MAX_AUXILIARY_SENDS;
2542 valuespan[i++] = dev->NumAuxSends;
2544 valuespan[i++] = ALC_HRTF_SOFT;
2545 valuespan[i++] = (dev->mHrtf ? ALC_TRUE : ALC_FALSE);
2547 valuespan[i++] = ALC_HRTF_STATUS_SOFT;
2548 valuespan[i++] = dev->mHrtfStatus;
2550 valuespan[i++] = ALC_OUTPUT_LIMITER_SOFT;
2551 valuespan[i++] = dev->Limiter ? ALC_TRUE : ALC_FALSE;
2553 ClockLatency clock{GetClockLatency(dev.get(), dev->Backend.get())};
2554 valuespan[i++] = ALC_DEVICE_CLOCK_SOFT;
2555 valuespan[i++] = clock.ClockTime.count();
2557 valuespan[i++] = ALC_DEVICE_LATENCY_SOFT;
2558 valuespan[i++] = clock.Latency.count();
2560 valuespan[i++] = ALC_OUTPUT_MODE_SOFT;
2561 valuespan[i++] = al::to_underlying(device->getOutputMode1());
2563 valuespan[i++] = 0;
2565 break;
2567 case ALC_DEVICE_CLOCK_SOFT:
2569 uint samplecount, refcount;
2570 nanoseconds basecount;
2571 do {
2572 refcount = dev->waitForMix();
2573 basecount = dev->mClockBase.load(std::memory_order_relaxed);
2574 samplecount = dev->mSamplesDone.load(std::memory_order_relaxed);
2575 std::atomic_thread_fence(std::memory_order_acquire);
2576 } while(refcount != dev->mMixCount.load(std::memory_order_relaxed));
2577 basecount += nanoseconds{seconds{samplecount}} / dev->Frequency;
2578 valuespan[0] = basecount.count();
2580 break;
2582 case ALC_DEVICE_LATENCY_SOFT:
2583 valuespan[0] = GetClockLatency(dev.get(), dev->Backend.get()).Latency.count();
2584 break;
2586 case ALC_DEVICE_CLOCK_LATENCY_SOFT:
2587 if(size < 2)
2588 alcSetError(dev.get(), ALC_INVALID_VALUE);
2589 else
2591 ClockLatency clock{GetClockLatency(dev.get(), dev->Backend.get())};
2592 valuespan[0] = clock.ClockTime.count();
2593 valuespan[1] = clock.Latency.count();
2595 break;
2597 default:
2598 auto ivals = std::vector<int>(valuespan.size());
2599 if(size_t got{GetIntegerv(dev.get(), pname, ivals)})
2600 std::copy_n(ivals.cbegin(), got, valuespan.begin());
2601 break;
2606 ALC_API ALCboolean ALC_APIENTRY alcIsExtensionPresent(ALCdevice *device, const ALCchar *extName) noexcept
2608 DeviceRef dev{VerifyDevice(device)};
2609 if(!extName)
2611 alcSetError(dev.get(), ALC_INVALID_VALUE);
2612 return ALC_FALSE;
2615 const std::string_view tofind{extName};
2616 const auto extlist = dev ? GetExtensionList() : GetNoDeviceExtList();
2617 auto matchpos = extlist.find(tofind);
2618 while(matchpos != std::string_view::npos)
2620 const auto endpos = matchpos + tofind.size();
2621 if((matchpos == 0 || std::isspace(extlist[matchpos-1]))
2622 && (endpos == extlist.size() || std::isspace(extlist[endpos])))
2623 return ALC_TRUE;
2624 matchpos = extlist.find(tofind, matchpos+1);
2626 return ALC_FALSE;
2630 ALCvoid* ALC_APIENTRY alcGetProcAddress2(ALCdevice *device, const ALCchar *funcName) noexcept
2631 { return alcGetProcAddress(device, funcName); }
2633 ALC_API ALCvoid* ALC_APIENTRY alcGetProcAddress(ALCdevice *device, const ALCchar *funcName) noexcept
2635 if(!funcName)
2637 DeviceRef dev{VerifyDevice(device)};
2638 alcSetError(dev.get(), ALC_INVALID_VALUE);
2639 return nullptr;
2642 #ifdef ALSOFT_EAX
2643 if(eax_g_is_enabled)
2645 for(const auto &func : eaxFunctions)
2647 if(strcmp(func.funcName, funcName) == 0)
2648 return func.address;
2651 #endif
2652 for(const auto &func : alcFunctions)
2654 if(strcmp(func.funcName, funcName) == 0)
2655 return func.address;
2657 return nullptr;
2661 ALC_API ALCenum ALC_APIENTRY alcGetEnumValue(ALCdevice *device, const ALCchar *enumName) noexcept
2663 if(!enumName)
2665 DeviceRef dev{VerifyDevice(device)};
2666 alcSetError(dev.get(), ALC_INVALID_VALUE);
2667 return 0;
2670 #ifdef ALSOFT_EAX
2671 if(eax_g_is_enabled)
2673 for(const auto &enm : eaxEnumerations)
2675 if(strcmp(enm.enumName, enumName) == 0)
2676 return enm.value;
2679 #endif
2680 for(const auto &enm : alcEnumerations)
2682 if(strcmp(enm.enumName, enumName) == 0)
2683 return enm.value;
2686 return 0;
2690 ALC_API ALCcontext* ALC_APIENTRY alcCreateContext(ALCdevice *device, const ALCint *attrList) noexcept
2692 /* Explicitly hold the list lock while taking the StateLock in case the
2693 * device is asynchronously destroyed, to ensure this new context is
2694 * properly cleaned up after being made.
2696 std::unique_lock<std::recursive_mutex> listlock{ListLock};
2697 DeviceRef dev{VerifyDevice(device)};
2698 if(!dev || dev->Type == DeviceType::Capture || !dev->Connected.load(std::memory_order_relaxed))
2700 listlock.unlock();
2701 alcSetError(dev.get(), ALC_INVALID_DEVICE);
2702 return nullptr;
2704 std::unique_lock<std::mutex> statelock{dev->StateLock};
2705 listlock.unlock();
2707 dev->LastError.store(ALC_NO_ERROR);
2709 const auto attrSpan = SpanFromAttributeList(attrList);
2710 ALCenum err{UpdateDeviceParams(dev.get(), attrSpan)};
2711 if(err != ALC_NO_ERROR)
2713 alcSetError(dev.get(), err);
2714 return nullptr;
2717 ContextFlagBitset ctxflags{0};
2718 for(size_t i{0};i < attrSpan.size();i+=2)
2720 if(attrSpan[i] == ALC_CONTEXT_FLAGS_EXT)
2722 ctxflags = static_cast<ALuint>(attrSpan[i+1]);
2723 break;
2727 auto context = ContextRef{new(std::nothrow) ALCcontext{dev, ctxflags}};
2728 if(!context)
2730 alcSetError(dev.get(), ALC_OUT_OF_MEMORY);
2731 return nullptr;
2733 context->init();
2735 if(auto volopt = dev->configValue<float>({}, "volume-adjust"))
2737 const float valf{*volopt};
2738 if(!std::isfinite(valf))
2739 ERR("volume-adjust must be finite: %f\n", valf);
2740 else
2742 const float db{std::clamp(valf, -24.0f, 24.0f)};
2743 if(db != valf)
2744 WARN("volume-adjust clamped: %f, range: +/-%f\n", valf, 24.0f);
2745 context->mGainBoost = std::pow(10.0f, db/20.0f);
2746 TRACE("volume-adjust gain: %f\n", context->mGainBoost);
2751 using ContextArray = al::FlexArray<ContextBase*>;
2753 /* Allocate a new context array, which holds 1 more than the current/
2754 * old array.
2756 auto *oldarray = device->mContexts.load();
2757 auto newarray = ContextArray::Create(oldarray->size() + 1);
2759 /* Copy the current/old context handles to the new array, appending the
2760 * new context.
2762 auto iter = std::copy(oldarray->begin(), oldarray->end(), newarray->begin());
2763 *iter = context.get();
2765 /* Store the new context array in the device. Wait for any current mix
2766 * to finish before deleting the old array.
2768 auto prevarray = dev->mContexts.exchange(std::move(newarray));
2769 std::ignore = dev->waitForMix();
2771 statelock.unlock();
2774 listlock.lock();
2775 auto iter = std::lower_bound(ContextList.cbegin(), ContextList.cend(), context.get());
2776 ContextList.emplace(iter, context.get());
2777 listlock.unlock();
2780 if(ALeffectslot *slot{context->mDefaultSlot.get()})
2782 ALenum sloterr{slot->initEffect(0, ALCcontext::sDefaultEffect.type,
2783 ALCcontext::sDefaultEffect.Props, context.get())};
2784 if(sloterr == AL_NO_ERROR)
2785 slot->updateProps(context.get());
2786 else
2787 ERR("Failed to initialize the default effect\n");
2790 TRACE("Created context %p\n", voidp{context.get()});
2791 return context.release();
2794 ALC_API void ALC_APIENTRY alcDestroyContext(ALCcontext *context) noexcept
2796 std::unique_lock<std::recursive_mutex> listlock{ListLock};
2797 auto iter = std::lower_bound(ContextList.begin(), ContextList.end(), context);
2798 if(iter == ContextList.end() || *iter != context)
2800 listlock.unlock();
2801 alcSetError(nullptr, ALC_INVALID_CONTEXT);
2802 return;
2805 /* Hold a reference to this context so it remains valid until the ListLock
2806 * is released.
2808 ContextRef ctx{*iter};
2809 ContextList.erase(iter);
2811 ALCdevice *Device{ctx->mALDevice.get()};
2813 std::lock_guard<std::mutex> statelock{Device->StateLock};
2814 ctx->deinit();
2818 ALC_API auto ALC_APIENTRY alcGetCurrentContext() noexcept -> ALCcontext*
2820 ALCcontext *Context{ALCcontext::getThreadContext()};
2821 if(!Context) Context = ALCcontext::sGlobalContext.load();
2822 return Context;
2825 /** Returns the currently active thread-local context. */
2826 ALC_API auto ALC_APIENTRY alcGetThreadContext() noexcept -> ALCcontext*
2827 { return ALCcontext::getThreadContext(); }
2829 ALC_API ALCboolean ALC_APIENTRY alcMakeContextCurrent(ALCcontext *context) noexcept
2831 /* context must be valid or nullptr */
2832 ContextRef ctx;
2833 if(context)
2835 ctx = VerifyContext(context);
2836 if(!ctx)
2838 alcSetError(nullptr, ALC_INVALID_CONTEXT);
2839 return ALC_FALSE;
2842 /* Release this reference (if any) to store it in the GlobalContext
2843 * pointer. Take ownership of the reference (if any) that was previously
2844 * stored there, and let the reference go.
2846 while(ALCcontext::sGlobalContextLock.exchange(true, std::memory_order_acquire)) {
2847 /* Wait to make sure another thread isn't getting or trying to change
2848 * the current context as its refcount is decremented.
2851 ctx = ContextRef{ALCcontext::sGlobalContext.exchange(ctx.release())};
2852 ALCcontext::sGlobalContextLock.store(false, std::memory_order_release);
2854 /* Take ownership of the thread-local context reference (if any), clearing
2855 * the storage to null.
2857 ctx = ContextRef{ALCcontext::getThreadContext()};
2858 if(ctx) ALCcontext::setThreadContext(nullptr);
2859 /* Reset (decrement) the previous thread-local reference. */
2861 return ALC_TRUE;
2864 /** Makes the given context the active context for the current thread. */
2865 ALC_API ALCboolean ALC_APIENTRY alcSetThreadContext(ALCcontext *context) noexcept
2867 /* context must be valid or nullptr */
2868 ContextRef ctx;
2869 if(context)
2871 ctx = VerifyContext(context);
2872 if(!ctx)
2874 alcSetError(nullptr, ALC_INVALID_CONTEXT);
2875 return ALC_FALSE;
2878 /* context's reference count is already incremented */
2879 ContextRef old{ALCcontext::getThreadContext()};
2880 ALCcontext::setThreadContext(ctx.release());
2882 return ALC_TRUE;
2886 ALC_API ALCdevice* ALC_APIENTRY alcGetContextsDevice(ALCcontext *Context) noexcept
2888 ContextRef ctx{VerifyContext(Context)};
2889 if(!ctx)
2891 alcSetError(nullptr, ALC_INVALID_CONTEXT);
2892 return nullptr;
2894 return ctx->mALDevice.get();
2898 ALC_API ALCdevice* ALC_APIENTRY alcOpenDevice(const ALCchar *deviceName) noexcept
2900 InitConfig();
2902 if(!PlaybackFactory)
2904 alcSetError(nullptr, ALC_INVALID_VALUE);
2905 return nullptr;
2908 std::string_view devname{deviceName ? deviceName : ""};
2909 if(!devname.empty())
2911 TRACE("Opening playback device \"%.*s\"\n", al::sizei(devname), devname.data());
2912 if(al::case_compare(devname, GetDefaultName()) == 0
2913 #ifdef _WIN32
2914 /* Some old Windows apps hardcode these expecting OpenAL to use a
2915 * specific audio API, even when they're not enumerated. Creative's
2916 * router effectively ignores them too.
2918 || al::case_compare(devname, "DirectSound3D"sv) == 0
2919 || al::case_compare(devname, "DirectSound"sv) == 0
2920 || al::case_compare(devname, "MMSYSTEM"sv) == 0
2921 #endif
2922 /* Some old Linux apps hardcode configuration strings that were
2923 * supported by the OpenAL SI. We can't really do anything useful
2924 * with them, so just ignore.
2926 || al::starts_with(devname, "'("sv)
2927 || al::case_compare(devname, "openal-soft"sv) == 0)
2928 devname = {};
2929 else
2931 const auto prefix = GetDevicePrefix();
2932 if(!prefix.empty() && devname.size() > prefix.size()
2933 && al::starts_with(devname, prefix))
2934 devname = devname.substr(prefix.size());
2937 else
2938 TRACE("Opening default playback device\n");
2940 const uint DefaultSends{
2941 #ifdef ALSOFT_EAX
2942 eax_g_is_enabled ? uint{EAX_MAX_FXSLOTS} :
2943 #endif // ALSOFT_EAX
2944 uint{DefaultSendCount}
2947 DeviceRef device{new(std::nothrow) ALCdevice{DeviceType::Playback}};
2948 if(!device)
2950 WARN("Failed to create playback device handle\n");
2951 alcSetError(nullptr, ALC_OUT_OF_MEMORY);
2952 return nullptr;
2955 /* Set output format */
2956 device->FmtChans = DevFmtChannelsDefault;
2957 device->FmtType = DevFmtTypeDefault;
2958 device->Frequency = DefaultOutputRate;
2959 device->UpdateSize = DefaultUpdateSize;
2960 device->BufferSize = DefaultUpdateSize * DefaultNumUpdates;
2962 device->SourcesMax = 256;
2963 device->NumStereoSources = 1;
2964 device->NumMonoSources = device->SourcesMax - device->NumStereoSources;
2965 device->AuxiliaryEffectSlotMax = 64;
2966 device->NumAuxSends = DefaultSends;
2968 try {
2969 auto backend = PlaybackFactory->createBackend(device.get(), BackendType::Playback);
2970 std::lock_guard<std::recursive_mutex> listlock{ListLock};
2971 backend->open(devname);
2972 device->mDeviceName = std::string{GetDevicePrefix()}+backend->mDeviceName;
2973 device->Backend = std::move(backend);
2975 catch(al::backend_exception &e) {
2976 WARN("Failed to open playback device: %s\n", e.what());
2977 alcSetError(nullptr, (e.errorCode() == al::backend_error::OutOfMemory)
2978 ? ALC_OUT_OF_MEMORY : ALC_INVALID_VALUE);
2979 return nullptr;
2982 auto checkopt = [&device](const char *envname, const std::string_view optname)
2984 if(auto optval = al::getenv(envname)) return optval;
2985 return device->configValue<std::string>("game_compat", optname);
2987 if(auto overrideopt = checkopt("__ALSOFT_VENDOR_OVERRIDE", "vendor-override"sv))
2989 device->mVendorOverride = std::move(*overrideopt);
2990 TRACE("Overriding vendor string: \"%s\"\n", device->mVendorOverride.c_str());
2992 if(auto overrideopt = checkopt("__ALSOFT_VERSION_OVERRIDE", "version-override"sv))
2994 device->mVersionOverride = std::move(*overrideopt);
2995 TRACE("Overriding version string: \"%s\"\n", device->mVersionOverride.c_str());
2997 if(auto overrideopt = checkopt("__ALSOFT_RENDERER_OVERRIDE", "renderer-override"sv))
2999 device->mRendererOverride = std::move(*overrideopt);
3000 TRACE("Overriding renderer string: \"%s\"\n", device->mRendererOverride.c_str());
3004 std::lock_guard<std::recursive_mutex> listlock{ListLock};
3005 auto iter = std::lower_bound(DeviceList.cbegin(), DeviceList.cend(), device.get());
3006 DeviceList.emplace(iter, device.get());
3009 TRACE("Created device %p, \"%s\"\n", voidp{device.get()}, device->mDeviceName.c_str());
3010 return device.release();
3013 ALC_API ALCboolean ALC_APIENTRY alcCloseDevice(ALCdevice *device) noexcept
3015 std::unique_lock<std::recursive_mutex> listlock{ListLock};
3016 auto iter = std::lower_bound(DeviceList.begin(), DeviceList.end(), device);
3017 if(iter == DeviceList.end() || *iter != device)
3019 alcSetError(nullptr, ALC_INVALID_DEVICE);
3020 return ALC_FALSE;
3022 if((*iter)->Type == DeviceType::Capture)
3024 alcSetError(*iter, ALC_INVALID_DEVICE);
3025 return ALC_FALSE;
3028 /* Erase the device, and any remaining contexts left on it, from their
3029 * respective lists.
3031 DeviceRef dev{*iter};
3032 DeviceList.erase(iter);
3034 std::unique_lock<std::mutex> statelock{dev->StateLock};
3035 std::vector<ContextRef> orphanctxs;
3036 for(ContextBase *ctx : *dev->mContexts.load())
3038 auto ctxiter = std::lower_bound(ContextList.begin(), ContextList.end(), ctx);
3039 if(ctxiter != ContextList.end() && *ctxiter == ctx)
3041 orphanctxs.emplace_back(*ctxiter);
3042 ContextList.erase(ctxiter);
3045 listlock.unlock();
3047 for(ContextRef &context : orphanctxs)
3049 WARN("Releasing orphaned context %p\n", voidp{context.get()});
3050 context->deinit();
3052 orphanctxs.clear();
3054 if(dev->mDeviceState == DeviceState::Playing)
3056 dev->Backend->stop();
3057 dev->mDeviceState = DeviceState::Configured;
3060 return ALC_TRUE;
3064 /************************************************
3065 * ALC capture functions
3066 ************************************************/
3067 ALC_API ALCdevice* ALC_APIENTRY alcCaptureOpenDevice(const ALCchar *deviceName, ALCuint frequency, ALCenum format, ALCsizei samples) noexcept
3069 InitConfig();
3071 if(!CaptureFactory)
3073 alcSetError(nullptr, ALC_INVALID_VALUE);
3074 return nullptr;
3077 if(samples <= 0)
3079 alcSetError(nullptr, ALC_INVALID_VALUE);
3080 return nullptr;
3083 std::string_view devname{deviceName ? deviceName : ""};
3084 if(!devname.empty())
3086 TRACE("Opening capture device \"%.*s\"\n", al::sizei(devname), devname.data());
3087 if(al::case_compare(devname, GetDefaultName()) == 0
3088 || al::case_compare(devname, "openal-soft"sv) == 0)
3089 devname = {};
3090 else
3092 const auto prefix = GetDevicePrefix();
3093 if(!prefix.empty() && devname.size() > prefix.size()
3094 && al::starts_with(devname, prefix))
3095 devname = devname.substr(prefix.size());
3098 else
3099 TRACE("Opening default capture device\n");
3101 DeviceRef device{new(std::nothrow) ALCdevice{DeviceType::Capture}};
3102 if(!device)
3104 WARN("Failed to create capture device handle\n");
3105 alcSetError(nullptr, ALC_OUT_OF_MEMORY);
3106 return nullptr;
3109 auto decompfmt = DecomposeDevFormat(format);
3110 if(!decompfmt)
3112 alcSetError(nullptr, ALC_INVALID_ENUM);
3113 return nullptr;
3116 device->Frequency = frequency;
3117 device->FmtChans = decompfmt->chans;
3118 device->FmtType = decompfmt->type;
3119 device->Flags.set(FrequencyRequest);
3120 device->Flags.set(ChannelsRequest);
3121 device->Flags.set(SampleTypeRequest);
3123 device->UpdateSize = static_cast<uint>(samples);
3124 device->BufferSize = static_cast<uint>(samples);
3126 TRACE("Capture format: %s, %s, %uhz, %u / %u buffer\n", DevFmtChannelsString(device->FmtChans),
3127 DevFmtTypeString(device->FmtType), device->Frequency, device->UpdateSize,
3128 device->BufferSize);
3130 try {
3131 auto backend = CaptureFactory->createBackend(device.get(), BackendType::Capture);
3132 std::lock_guard<std::recursive_mutex> listlock{ListLock};
3133 backend->open(devname);
3134 device->mDeviceName = std::string{GetDevicePrefix()}+backend->mDeviceName;
3135 device->Backend = std::move(backend);
3137 catch(al::backend_exception &e) {
3138 WARN("Failed to open capture device: %s\n", e.what());
3139 alcSetError(nullptr, (e.errorCode() == al::backend_error::OutOfMemory)
3140 ? ALC_OUT_OF_MEMORY : ALC_INVALID_VALUE);
3141 return nullptr;
3145 std::lock_guard<std::recursive_mutex> listlock{ListLock};
3146 auto iter = std::lower_bound(DeviceList.cbegin(), DeviceList.cend(), device.get());
3147 DeviceList.emplace(iter, device.get());
3149 device->mDeviceState = DeviceState::Configured;
3151 TRACE("Created capture device %p, \"%s\"\n", voidp{device.get()}, device->mDeviceName.c_str());
3152 return device.release();
3155 ALC_API ALCboolean ALC_APIENTRY alcCaptureCloseDevice(ALCdevice *device) noexcept
3157 std::unique_lock<std::recursive_mutex> listlock{ListLock};
3158 auto iter = std::lower_bound(DeviceList.begin(), DeviceList.end(), device);
3159 if(iter == DeviceList.end() || *iter != device)
3161 alcSetError(nullptr, ALC_INVALID_DEVICE);
3162 return ALC_FALSE;
3164 if((*iter)->Type != DeviceType::Capture)
3166 alcSetError(*iter, ALC_INVALID_DEVICE);
3167 return ALC_FALSE;
3170 DeviceRef dev{*iter};
3171 DeviceList.erase(iter);
3172 listlock.unlock();
3174 std::lock_guard<std::mutex> statelock{dev->StateLock};
3175 if(dev->mDeviceState == DeviceState::Playing)
3177 dev->Backend->stop();
3178 dev->mDeviceState = DeviceState::Configured;
3181 return ALC_TRUE;
3184 ALC_API void ALC_APIENTRY alcCaptureStart(ALCdevice *device) noexcept
3186 DeviceRef dev{VerifyDevice(device)};
3187 if(!dev || dev->Type != DeviceType::Capture)
3189 alcSetError(dev.get(), ALC_INVALID_DEVICE);
3190 return;
3193 std::lock_guard<std::mutex> statelock{dev->StateLock};
3194 if(!dev->Connected.load(std::memory_order_acquire)
3195 || dev->mDeviceState < DeviceState::Configured)
3196 alcSetError(dev.get(), ALC_INVALID_DEVICE);
3197 else if(dev->mDeviceState != DeviceState::Playing)
3199 try {
3200 auto backend = dev->Backend.get();
3201 backend->start();
3202 dev->mDeviceState = DeviceState::Playing;
3204 catch(al::backend_exception& e) {
3205 ERR("%s\n", e.what());
3206 dev->handleDisconnect("%s", e.what());
3207 alcSetError(dev.get(), ALC_INVALID_DEVICE);
3212 ALC_API void ALC_APIENTRY alcCaptureStop(ALCdevice *device) noexcept
3214 DeviceRef dev{VerifyDevice(device)};
3215 if(!dev || dev->Type != DeviceType::Capture)
3216 alcSetError(dev.get(), ALC_INVALID_DEVICE);
3217 else
3219 std::lock_guard<std::mutex> statelock{dev->StateLock};
3220 if(dev->mDeviceState == DeviceState::Playing)
3222 dev->Backend->stop();
3223 dev->mDeviceState = DeviceState::Configured;
3228 ALC_API void ALC_APIENTRY alcCaptureSamples(ALCdevice *device, ALCvoid *buffer, ALCsizei samples) noexcept
3230 DeviceRef dev{VerifyDevice(device)};
3231 if(!dev || dev->Type != DeviceType::Capture)
3233 alcSetError(dev.get(), ALC_INVALID_DEVICE);
3234 return;
3237 if(samples < 0 || (samples > 0 && buffer == nullptr))
3239 alcSetError(dev.get(), ALC_INVALID_VALUE);
3240 return;
3242 if(samples < 1)
3243 return;
3245 std::lock_guard<std::mutex> statelock{dev->StateLock};
3246 BackendBase *backend{dev->Backend.get()};
3248 const auto usamples = static_cast<uint>(samples);
3249 if(usamples > backend->availableSamples())
3251 alcSetError(dev.get(), ALC_INVALID_VALUE);
3252 return;
3255 backend->captureSamples(static_cast<std::byte*>(buffer), usamples);
3259 /************************************************
3260 * ALC loopback functions
3261 ************************************************/
3263 /** Open a loopback device, for manual rendering. */
3264 ALC_API ALCdevice* ALC_APIENTRY alcLoopbackOpenDeviceSOFT(const ALCchar *deviceName) noexcept
3266 InitConfig();
3268 /* Make sure the device name, if specified, is us. */
3269 if(deviceName && strcmp(deviceName, GetDefaultName()) != 0)
3271 alcSetError(nullptr, ALC_INVALID_VALUE);
3272 return nullptr;
3275 const uint DefaultSends{
3276 #ifdef ALSOFT_EAX
3277 eax_g_is_enabled ? uint{EAX_MAX_FXSLOTS} :
3278 #endif // ALSOFT_EAX
3279 uint{DefaultSendCount}
3282 DeviceRef device{new(std::nothrow) ALCdevice{DeviceType::Loopback}};
3283 if(!device)
3285 WARN("Failed to create loopback device handle\n");
3286 alcSetError(nullptr, ALC_OUT_OF_MEMORY);
3287 return nullptr;
3290 device->SourcesMax = 256;
3291 device->AuxiliaryEffectSlotMax = 64;
3292 device->NumAuxSends = DefaultSends;
3294 //Set output format
3295 device->BufferSize = 0;
3296 device->UpdateSize = 0;
3298 device->Frequency = DefaultOutputRate;
3299 device->FmtChans = DevFmtChannelsDefault;
3300 device->FmtType = DevFmtTypeDefault;
3302 device->NumStereoSources = 1;
3303 device->NumMonoSources = device->SourcesMax - device->NumStereoSources;
3305 try {
3306 auto backend = LoopbackBackendFactory::getFactory().createBackend(device.get(),
3307 BackendType::Playback);
3308 backend->open("Loopback");
3309 device->mDeviceName = std::string{GetDevicePrefix()}+backend->mDeviceName;
3310 device->Backend = std::move(backend);
3312 catch(al::backend_exception &e) {
3313 WARN("Failed to open loopback device: %s\n", e.what());
3314 alcSetError(nullptr, (e.errorCode() == al::backend_error::OutOfMemory)
3315 ? ALC_OUT_OF_MEMORY : ALC_INVALID_VALUE);
3316 return nullptr;
3320 std::lock_guard<std::recursive_mutex> listlock{ListLock};
3321 auto iter = std::lower_bound(DeviceList.cbegin(), DeviceList.cend(), device.get());
3322 DeviceList.emplace(iter, device.get());
3325 TRACE("Created loopback device %p\n", voidp{device.get()});
3326 return device.release();
3330 * Determines if the loopback device supports the given format for rendering.
3332 ALC_API ALCboolean ALC_APIENTRY alcIsRenderFormatSupportedSOFT(ALCdevice *device, ALCsizei freq, ALCenum channels, ALCenum type) noexcept
3334 DeviceRef dev{VerifyDevice(device)};
3335 if(!dev || dev->Type != DeviceType::Loopback)
3336 alcSetError(dev.get(), ALC_INVALID_DEVICE);
3337 else if(freq <= 0)
3338 alcSetError(dev.get(), ALC_INVALID_VALUE);
3339 else
3341 if(DevFmtTypeFromEnum(type).has_value() && DevFmtChannelsFromEnum(channels).has_value()
3342 && freq >= int{MinOutputRate} && freq <= int{MaxOutputRate})
3343 return ALC_TRUE;
3346 return ALC_FALSE;
3350 * Renders some samples into a buffer, using the format last set by the
3351 * attributes given to alcCreateContext.
3353 #if defined(__GNUC__) && defined(__i386__)
3354 /* Needed on x86-32 even without SSE codegen, since the mixer may still use SSE
3355 * and GCC assumes the stack is aligned (x86-64 ABI guarantees alignment).
3357 [[gnu::force_align_arg_pointer]]
3358 #endif
3359 ALC_API void ALC_APIENTRY alcRenderSamplesSOFT(ALCdevice *device, ALCvoid *buffer, ALCsizei samples) noexcept
3361 if(!device || device->Type != DeviceType::Loopback) UNLIKELY
3362 alcSetError(device, ALC_INVALID_DEVICE);
3363 else if(samples < 0 || (samples > 0 && buffer == nullptr)) UNLIKELY
3364 alcSetError(device, ALC_INVALID_VALUE);
3365 else
3366 device->renderSamples(buffer, static_cast<uint>(samples), device->channelsFromFmt());
3370 /************************************************
3371 * ALC DSP pause/resume functions
3372 ************************************************/
3374 /** Pause the DSP to stop audio processing. */
3375 ALC_API void ALC_APIENTRY alcDevicePauseSOFT(ALCdevice *device) noexcept
3377 DeviceRef dev{VerifyDevice(device)};
3378 if(!dev || dev->Type != DeviceType::Playback)
3379 alcSetError(dev.get(), ALC_INVALID_DEVICE);
3380 else
3382 std::lock_guard<std::mutex> statelock{dev->StateLock};
3383 if(dev->mDeviceState == DeviceState::Playing)
3385 dev->Backend->stop();
3386 dev->mDeviceState = DeviceState::Configured;
3388 dev->Flags.set(DevicePaused);
3392 /** Resume the DSP to restart audio processing. */
3393 ALC_API void ALC_APIENTRY alcDeviceResumeSOFT(ALCdevice *device) noexcept
3395 DeviceRef dev{VerifyDevice(device)};
3396 if(!dev || dev->Type != DeviceType::Playback)
3398 alcSetError(dev.get(), ALC_INVALID_DEVICE);
3399 return;
3402 std::lock_guard<std::mutex> statelock{dev->StateLock};
3403 if(!dev->Flags.test(DevicePaused))
3404 return;
3405 if(dev->mDeviceState < DeviceState::Configured)
3407 WARN("Cannot resume unconfigured device\n");
3408 alcSetError(dev.get(), ALC_INVALID_DEVICE);
3409 return;
3411 if(!dev->Connected.load())
3413 WARN("Cannot resume a disconnected device\n");
3414 alcSetError(dev.get(), ALC_INVALID_DEVICE);
3415 return;
3417 dev->Flags.reset(DevicePaused);
3418 if(dev->mContexts.load()->empty())
3419 return;
3421 try {
3422 auto backend = dev->Backend.get();
3423 backend->start();
3424 dev->mDeviceState = DeviceState::Playing;
3426 catch(al::backend_exception& e) {
3427 ERR("%s\n", e.what());
3428 dev->handleDisconnect("%s", e.what());
3429 alcSetError(dev.get(), ALC_INVALID_DEVICE);
3430 return;
3432 TRACE("Post-resume: %s, %s, %uhz, %u / %u buffer\n",
3433 DevFmtChannelsString(dev->FmtChans), DevFmtTypeString(dev->FmtType),
3434 dev->Frequency, dev->UpdateSize, dev->BufferSize);
3438 /************************************************
3439 * ALC HRTF functions
3440 ************************************************/
3442 /** Gets a string parameter at the given index. */
3443 ALC_API const ALCchar* ALC_APIENTRY alcGetStringiSOFT(ALCdevice *device, ALCenum paramName, ALCsizei index) noexcept
3445 DeviceRef dev{VerifyDevice(device)};
3446 if(!dev || dev->Type == DeviceType::Capture)
3447 alcSetError(dev.get(), ALC_INVALID_DEVICE);
3448 else switch(paramName)
3450 case ALC_HRTF_SPECIFIER_SOFT:
3451 if(index >= 0 && static_cast<uint>(index) < dev->mHrtfList.size())
3452 return dev->mHrtfList[static_cast<uint>(index)].c_str();
3453 alcSetError(dev.get(), ALC_INVALID_VALUE);
3454 break;
3456 default:
3457 alcSetError(dev.get(), ALC_INVALID_ENUM);
3458 break;
3461 return nullptr;
3464 /** Resets the given device output, using the specified attribute list. */
3465 ALC_API ALCboolean ALC_APIENTRY alcResetDeviceSOFT(ALCdevice *device, const ALCint *attribs) noexcept
3467 std::unique_lock<std::recursive_mutex> listlock{ListLock};
3468 DeviceRef dev{VerifyDevice(device)};
3469 if(!dev || dev->Type == DeviceType::Capture)
3471 listlock.unlock();
3472 alcSetError(dev.get(), ALC_INVALID_DEVICE);
3473 return ALC_FALSE;
3475 std::lock_guard<std::mutex> statelock{dev->StateLock};
3476 listlock.unlock();
3478 /* Force the backend to stop mixing first since we're resetting. Also reset
3479 * the connected state so lost devices can attempt recover.
3481 if(dev->mDeviceState == DeviceState::Playing)
3483 dev->Backend->stop();
3484 dev->mDeviceState = DeviceState::Configured;
3487 return ResetDeviceParams(dev.get(), SpanFromAttributeList(attribs)) ? ALC_TRUE : ALC_FALSE;
3491 /************************************************
3492 * ALC device reopen functions
3493 ************************************************/
3495 /** Reopens the given device output, using the specified name and attribute list. */
3496 FORCE_ALIGN ALCboolean ALC_APIENTRY alcReopenDeviceSOFT(ALCdevice *device,
3497 const ALCchar *deviceName, const ALCint *attribs) noexcept
3499 std::unique_lock<std::recursive_mutex> listlock{ListLock};
3500 DeviceRef dev{VerifyDevice(device)};
3501 if(!dev || dev->Type != DeviceType::Playback)
3503 listlock.unlock();
3504 alcSetError(dev.get(), ALC_INVALID_DEVICE);
3505 return ALC_FALSE;
3507 std::lock_guard<std::mutex> statelock{dev->StateLock};
3509 std::string_view devname{deviceName ? deviceName : ""};
3510 if(!devname.empty())
3512 if(devname.length() >= size_t{std::numeric_limits<int>::max()})
3514 ERR("Device name too long (%zu >= %d)\n", devname.length(),
3515 std::numeric_limits<int>::max());
3516 alcSetError(dev.get(), ALC_INVALID_VALUE);
3517 return ALC_FALSE;
3519 if(al::case_compare(devname, GetDefaultName()) == 0)
3520 devname = {};
3521 else
3523 const auto prefix = GetDevicePrefix();
3524 if(!prefix.empty() && devname.size() > prefix.size()
3525 && al::starts_with(devname, prefix))
3526 devname = devname.substr(prefix.size());
3530 /* Force the backend device to stop first since we're opening another one. */
3531 const bool wasPlaying{dev->mDeviceState == DeviceState::Playing};
3532 if(wasPlaying)
3534 dev->Backend->stop();
3535 dev->mDeviceState = DeviceState::Configured;
3538 BackendPtr newbackend;
3539 try {
3540 newbackend = PlaybackFactory->createBackend(dev.get(), BackendType::Playback);
3541 newbackend->open(devname);
3543 catch(al::backend_exception &e) {
3544 listlock.unlock();
3545 newbackend = nullptr;
3547 WARN("Failed to reopen playback device: %s\n", e.what());
3548 alcSetError(dev.get(), (e.errorCode() == al::backend_error::OutOfMemory)
3549 ? ALC_OUT_OF_MEMORY : ALC_INVALID_VALUE);
3551 if(dev->Connected.load(std::memory_order_relaxed) && wasPlaying)
3553 try {
3554 auto backend = dev->Backend.get();
3555 backend->start();
3556 dev->mDeviceState = DeviceState::Playing;
3558 catch(al::backend_exception &be) {
3559 ERR("%s\n", be.what());
3560 dev->handleDisconnect("%s", be.what());
3563 return ALC_FALSE;
3565 listlock.unlock();
3566 dev->mDeviceName = std::string{GetDevicePrefix()}+newbackend->mDeviceName;
3567 dev->Backend = std::move(newbackend);
3568 dev->mDeviceState = DeviceState::Unprepared;
3569 TRACE("Reopened device %p, \"%s\"\n", voidp{dev.get()}, dev->mDeviceName.c_str());
3571 std::string{}.swap(dev->mVendorOverride);
3572 std::string{}.swap(dev->mVersionOverride);
3573 std::string{}.swap(dev->mRendererOverride);
3574 auto checkopt = [&dev](const char *envname, const std::string_view optname)
3576 if(auto optval = al::getenv(envname)) return optval;
3577 return dev->configValue<std::string>("game_compat", optname);
3579 if(auto overrideopt = checkopt("__ALSOFT_VENDOR_OVERRIDE", "vendor-override"sv))
3581 dev->mVendorOverride = std::move(*overrideopt);
3582 TRACE("Overriding vendor string: \"%s\"\n", dev->mVendorOverride.c_str());
3584 if(auto overrideopt = checkopt("__ALSOFT_VERSION_OVERRIDE", "version-override"sv))
3586 dev->mVersionOverride = std::move(*overrideopt);
3587 TRACE("Overriding version string: \"%s\"\n", dev->mVersionOverride.c_str());
3589 if(auto overrideopt = checkopt("__ALSOFT_RENDERER_OVERRIDE", "renderer-override"sv))
3591 dev->mRendererOverride = std::move(*overrideopt);
3592 TRACE("Overriding renderer string: \"%s\"\n", dev->mRendererOverride.c_str());
3595 /* Always return true even if resetting fails. It shouldn't fail, but this
3596 * is primarily to avoid confusion by the app seeing the function return
3597 * false while the device is on the new output anyway. We could try to
3598 * restore the old backend if this fails, but the configuration would be
3599 * changed with the new backend and would need to be reset again with the
3600 * old one, and the provided attributes may not be appropriate or desirable
3601 * for the old device.
3603 * In this way, we essentially act as if the function succeeded, but
3604 * immediately disconnects following it.
3606 ResetDeviceParams(dev.get(), SpanFromAttributeList(attribs));
3607 return ALC_TRUE;
3610 /************************************************
3611 * ALC event query functions
3612 ************************************************/
3614 FORCE_ALIGN ALCenum ALC_APIENTRY alcEventIsSupportedSOFT(ALCenum eventType, ALCenum deviceType) noexcept
3616 auto etype = alc::GetEventType(eventType);
3617 if(!etype)
3619 WARN("Invalid event type: 0x%04x\n", eventType);
3620 alcSetError(nullptr, ALC_INVALID_ENUM);
3621 return ALC_FALSE;
3624 auto supported = alc::EventSupport::NoSupport;
3625 switch(deviceType)
3627 case ALC_PLAYBACK_DEVICE_SOFT:
3628 if(PlaybackFactory)
3629 supported = PlaybackFactory->queryEventSupport(*etype, BackendType::Playback);
3630 return al::to_underlying(supported);
3632 case ALC_CAPTURE_DEVICE_SOFT:
3633 if(CaptureFactory)
3634 supported = CaptureFactory->queryEventSupport(*etype, BackendType::Capture);
3635 return al::to_underlying(supported);
3637 WARN("Invalid device type: 0x%04x\n", deviceType);
3638 alcSetError(nullptr, ALC_INVALID_ENUM);
3639 return ALC_FALSE;