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
26 #define WIN32_LEAN_AND_MEAN
55 #include <string_view>
65 #include "al/auxeffectslot.h"
66 #include "al/buffer.h"
68 #include "al/effect.h"
69 #include "al/filter.h"
70 #include "al/source.h"
71 #include "alc/events.h"
75 #include "alnumbers.h"
76 #include "alnumeric.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"
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"
112 #include "backends/pipewire.h"
115 #include "backends/jack.h"
117 #ifdef HAVE_PULSEAUDIO
118 #include "backends/pulseaudio.h"
121 #include "backends/alsa.h"
124 #include "backends/wasapi.h"
126 #ifdef HAVE_COREAUDIO
127 #include "backends/coreaudio.h"
130 #include "backends/opensl.h"
133 #include "backends/oboe.h"
136 #include "backends/solaris.h"
139 #include "backends/sndio.h"
142 #include "backends/oss.h"
145 #include "backends/dsound.h"
148 #include "backends/winmm.h"
150 #ifdef HAVE_PORTAUDIO
151 #include "backends/portaudio.h"
154 #include "backends/sdl2.h"
157 #include "backends/otherio.h"
160 #include "backends/wave.h"
164 #include "al/eax/api.h"
165 #include "al/eax/globals.h"
169 /************************************************
170 * Library initialization
171 ************************************************/
172 #if defined(_WIN32) && !defined(AL_LIBTYPE_STATIC)
173 BOOL APIENTRY
DllMain(HINSTANCE module
, DWORD reason
, LPVOID
/*reserved*/)
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
);
189 using namespace std::string_view_literals
;
190 using std::chrono::seconds
;
191 using std::chrono::nanoseconds
;
194 using float2
= std::array
<float,2>;
197 /************************************************
199 ************************************************/
202 BackendFactory
& (*getFactory
)();
205 std::array BackendList
{
207 BackendInfo
{"pipewire", PipeWireBackendFactory::getFactory
},
209 #ifdef HAVE_PULSEAUDIO
210 BackendInfo
{"pulse", PulseBackendFactory::getFactory
},
213 BackendInfo
{"wasapi", WasapiBackendFactory::getFactory
},
215 #ifdef HAVE_COREAUDIO
216 BackendInfo
{"core", CoreAudioBackendFactory::getFactory
},
219 BackendInfo
{"oboe", OboeBackendFactory::getFactory
},
222 BackendInfo
{"opensl", OSLBackendFactory::getFactory
},
225 BackendInfo
{"alsa", AlsaBackendFactory::getFactory
},
228 BackendInfo
{"solaris", SolarisBackendFactory::getFactory
},
231 BackendInfo
{"sndio", SndIOBackendFactory::getFactory
},
234 BackendInfo
{"oss", OSSBackendFactory::getFactory
},
237 BackendInfo
{"jack", JackBackendFactory::getFactory
},
240 BackendInfo
{"dsound", DSoundBackendFactory::getFactory
},
243 BackendInfo
{"winmm", WinMMBackendFactory::getFactory
},
245 #ifdef HAVE_PORTAUDIO
246 BackendInfo
{"port", PortBackendFactory::getFactory
},
249 BackendInfo
{"sdl2", SDL2BackendFactory::getFactory
},
252 BackendInfo
{"otherio", OtherIOBackendFactory::getFactory
},
255 BackendInfo
{"null", NullBackendFactory::getFactory
},
257 BackendInfo
{"wave", WaveBackendFactory::getFactory
},
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"; }
275 [[nodiscard
]] constexpr auto GetDevicePrefix() noexcept
{ return "OpenAL Soft on "sv
; }
277 [[nodiscard
]] constexpr auto GetDevicePrefix() noexcept
{ return std::string_view
{}; }
280 /************************************************
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
305 bool SuspendDefers
{true};
307 /* Initial seed for dithering. */
308 constexpr uint DitherRNGSeed
{22222u};
311 /************************************************
313 ************************************************/
314 [[nodiscard
]] constexpr auto GetNoDeviceExtList() noexcept
-> std::string_view
316 return "ALC_ENUMERATE_ALL_EXT "
317 "ALC_ENUMERATION_EXT "
319 "ALC_EXT_direct_context "
321 "ALC_EXT_thread_local_context "
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 "
334 "ALC_EXT_direct_context "
335 "ALC_EXT_disconnect "
337 "ALC_EXT_thread_local_context "
338 "ALC_SOFT_device_clock "
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 /************************************************
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
;
378 gLogLevel
= static_cast<LogLevel
>(lvl
);
382 if(const auto logfile
= al::getenv(L
"ALSOFT_LOGFILE"))
384 FILE *logf
{_wfopen(logfile
->c_str(), L
"wt")};
385 if(logf
) gLogFile
= logf
;
388 auto u8name
= wstr_to_utf8(*logfile
);
389 ERR("Failed to open log file '%s'\n", u8name
.c_str());
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());
401 TRACE("Initializing library v%s-%s %s\n", ALSOFT_VERSION
, ALSOFT_GIT_COMMIT_HASH
,
405 if(std::size(BackendList
) < 1)
409 const al::span
<const BackendInfo
> infos
{BackendList
};
410 names
= infos
[0].name
;
411 for(const auto &backend
: infos
.subspan
<1>())
414 names
+= backend
.name
;
417 TRACE("Supported backends: %s\n", names
.c_str());
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");
429 ERR("Unhandled context suspend behavior setting: \"%s\"\n", suspendmode
->c_str());
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
;
443 capfilter
|= CPU_CAP_NEON
;
445 if(auto cpuopt
= ConfigValueStr({}, {}, "disable-cpu-exts"sv
))
447 std::string_view cpulist
{*cpuopt
};
448 if(al::case_compare(cpulist
, "all"sv
) == 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
] == ',')
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);
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
;
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
;
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
;
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))
555 traperr
= al::getenv("ALSOFT_TRAP_AL_ERROR");
557 TrapALError
= al::case_compare(*traperr
, "true"sv
) == 0
558 || strtol(traperr
->c_str(), nullptr, 0) == 1;
560 TrapALError
= GetConfigValueBool({}, {}, "trap-al-error"sv
, false);
562 traperr
= al::getenv("ALSOFT_TRAP_ALC_ERROR");
564 TrapALCError
= al::case_compare(*traperr
, "true"sv
) == 0
565 || strtol(traperr
->c_str(), nullptr, 0) == 1;
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
);
581 auto backendlist_cur
= BackendList
.begin();
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
);
591 if(nextpos
< drvlist
.size())
594 while(nextpos
< drvlist
.size() && drvlist
[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);
610 /* HACK: For backwards compatibility, convert backend references of
611 * mmdevapi to wasapi. This should eventually be removed.
613 if(entry
== "mmdevapi"sv
)
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
)
625 BackendListEnd
= std::move(this_backend
+1, BackendListEnd
, this_backend
);
627 backendlist_cur
= std::rotate(backendlist_cur
, this_backend
, this_backend
+1);
631 BackendListEnd
= backendlist_cur
;
634 auto init_backend
= [](BackendInfo
&backend
) -> void
636 if(PlaybackFactory
&& CaptureFactory
)
639 BackendFactory
&factory
= backend
.getFactory();
642 WARN("Failed to initialize backend \"%s\"\n", backend
.name
);
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();
663 WARN("No playback backend available!\n");
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
);
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.");
699 eax_g_is_enabled
= true;
701 if((DisabledEffects
.test(EAXREVERB_EFFECT
) || DisabledEffects
.test(CHORUS_EFFECT
))
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" : "");
714 inline void InitConfig()
715 { std::call_once(alc_config_once
, [](){alc_initconfig();}); }
718 /************************************************
720 ************************************************/
721 void ProbeAllDevicesList()
725 std::lock_guard
<std::recursive_mutex
> listlock
{ListLock
};
728 decltype(alcAllDevicesArray
){}.swap(alcAllDevicesArray
);
729 decltype(alcAllDevicesList
){}.swap(alcAllDevicesList
);
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()
749 std::lock_guard
<std::recursive_mutex
> listlock
{ListLock
};
752 decltype(alcCaptureDeviceArray
){}.swap(alcCaptureDeviceArray
);
753 decltype(alcCaptureDeviceList
){}.swap(alcCaptureDeviceList
);
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
;
776 const ALCint
*attrEnd
{attribs
};
778 attrEnd
+= 2; /* NOLINT(cppcoreguidelines-pro-bounds-pointer-arithmetic) */
779 attrSpan
= {attribs
, attrEnd
};
784 struct DevFmtPair
{ DevFmtChannels chans
; DevFmtType type
; };
785 std::optional
<DevFmtPair
> DecomposeDevFormat(ALenum format
)
789 DevFmtChannels channels
;
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
};
837 std::optional
<DevFmtType
> DevFmtTypeFromEnum(ALCenum 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
);
852 ALCenum
EnumFromDevFmt(DevFmtType 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
)
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
);
882 ALCenum
EnumFromDevFmt(DevFmtChannels 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. */
896 case DevFmtX3D71
: break;
898 throw std::runtime_error
{"Invalid DevFmtChannels: "+std::to_string(int(channels
))};
901 std::optional
<DevAmbiLayout
> DevAmbiLayoutFromEnum(ALCenum 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
);
911 ALCenum
EnumFromDevAmbi(DevAmbiLayout 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
)
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
);
932 ALCenum
EnumFromDevAmbi(DevAmbiScaling 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/
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
};
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
;
1083 buffer_size
= period_size
* uint
{DefaultNumUpdates
};
1085 if(auto typeopt
= device
->configValue
<std::string
>({}, "sample-type"))
1088 std::string_view name
;
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
);
1108 opttype
= iter
->type
;
1110 if(auto chanopt
= device
->configValue
<std::string
>({}, "channels"))
1113 std::string_view name
;
1114 DevFmtChannels chans
;
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
);
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
;
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
;
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
;
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]);
1217 case ATTRIBUTE(ALC_FORMAT_TYPE_SOFT
)
1218 if(device
->Type
== DeviceType::Loopback
)
1219 opttype
= DevFmtTypeFromEnum(attrList
[attrIdx
+ 1]);
1222 case ATTRIBUTE(ALC_FREQUENCY
)
1223 freqAttr
= attrList
[attrIdx
+ 1];
1226 case ATTRIBUTE(ALC_AMBISONIC_LAYOUT_SOFT
)
1227 if(device
->Type
== DeviceType::Loopback
)
1228 optlayout
= DevAmbiLayoutFromEnum(attrList
[attrIdx
+ 1]);
1231 case ATTRIBUTE(ALC_AMBISONIC_SCALING_SOFT
)
1232 if(device
->Type
== DeviceType::Loopback
)
1233 optscale
= DevAmbiScalingFromEnum(attrList
[attrIdx
+ 1]);
1236 case ATTRIBUTE(ALC_AMBISONIC_ORDER_SOFT
)
1237 if(device
->Type
== DeviceType::Loopback
)
1238 aorder
= static_cast<uint
>(attrList
[attrIdx
+ 1]);
1241 case ATTRIBUTE(ALC_MONO_SOURCES
)
1242 numMono
= static_cast<uint
>(attrList
[attrIdx
+ 1]);
1243 if(numMono
> INT_MAX
) numMono
= 0;
1246 case ATTRIBUTE(ALC_STEREO_SOURCES
)
1247 numStereo
= static_cast<uint
>(attrList
[attrIdx
+ 1]);
1248 if(numStereo
> INT_MAX
) numStereo
= 0;
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
});
1257 case ATTRIBUTE(ALC_HRTF_SOFT
)
1258 if(attrList
[attrIdx
+ 1] == ALC_FALSE
)
1260 else if(attrList
[attrIdx
+ 1] == ALC_TRUE
)
1262 else if(attrList
[attrIdx
+ 1] == ALC_DONT_CARE_SOFT
)
1263 opthrtf
= std::nullopt
;
1266 case ATTRIBUTE(ALC_HRTF_ID_SOFT
)
1267 hrtf_id
= attrList
[attrIdx
+ 1];
1270 case ATTRIBUTE(ALC_OUTPUT_LIMITER_SOFT
)
1271 if(attrList
[attrIdx
+ 1] == ALC_FALSE
)
1273 else if(attrList
[attrIdx
+ 1] == ALC_TRUE
)
1275 else if(attrList
[attrIdx
+ 1] == ALC_DONT_CARE_SOFT
)
1276 optlimit
= std::nullopt
;
1279 case ATTRIBUTE(ALC_OUTPUT_MODE_SOFT
)
1280 outmode
= attrList
[attrIdx
+ 1];
1284 TRACE("0x%04X = %d (0x%x)\n", attrList
[attrIdx
],
1285 attrList
[attrIdx
+ 1], attrList
[attrIdx
+ 1]);
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
)
1305 return ALC_INVALID_VALUE
;
1307 else if(*optchans
== DevFmtStereo
)
1312 stereomode
= StereoEncoding::Hrtf
;
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
);
1335 stereomode
= StereoEncoding::Hrtf
;
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
;
1355 case OutputMode::Uhj2
:
1356 optchans
= DevFmtStereo
;
1357 stereomode
= StereoEncoding::Uhj
;
1359 case OutputMode::Hrtf
:
1360 optchans
= DevFmtStereo
;
1361 stereomode
= StereoEncoding::Hrtf
;
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;
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
);
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
};
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);
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;
1560 if(!device
->mUhjEncoder
)
1561 device
->RealOut
.RemixMap
= StereoDownmix
;
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)};
1582 switch(device
->FmtType
)
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");
1608 TRACE("Dithering enabled (%d-bit, %g)\n", float2int(std::log2(device
->DitherDepth
)+0.5f
)+1,
1609 device
->DitherDepth
);
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).
1620 switch(device
->FmtType
)
1634 if(!optlimit
.value_or(false))
1635 TRACE("Output limiter disabled\n");
1638 float thrshld
{1.0f
};
1639 switch(device
->FmtType
)
1643 thrshld
= 127.0f
/ 128.0f
;
1647 thrshld
= 32767.0f
/ 32768.0f
;
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(),
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
};
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(),
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
);
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
};
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
1763 DecrementRef(send
.Slot
->ref
);
1764 send
.Slot
= nullptr;
1767 send
.HFReference
= LowPassFreqRef
;
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)
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
);
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
);
1822 device
->mDeviceState
= DeviceState::Configured
;
1823 if(!device
->Flags
.test(DevicePaused
))
1826 auto backend
= device
->Backend
.get();
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
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
))
1861 /* Clear any pending voice changes and reallocate voices to get a
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
))
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
);
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
)
1897 return DeviceRef
{*iter
};
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
)
1913 return ContextRef
{*iter
};
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()};
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
);
1951 /* DebugBreak() will cause an exception if there is no debugger */
1952 if(IsDebuggerPresent())
1954 #elif defined(SIGTRAP)
1960 device
->LastError
.store(errorCode
);
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
)};
1982 alcSetError(nullptr, ALC_INVALID_CONTEXT
);
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");
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
)};
2005 alcSetError(nullptr, ALC_INVALID_CONTEXT
);
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");
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};
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();
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();
2050 std::lock_guard
<std::mutex
> statelock
{dev
->StateLock
};
2051 value
= dev
->mDeviceName
.c_str();
2056 ProbeAllDevicesList();
2057 value
= alcAllDevicesList
.c_str();
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
);
2068 std::lock_guard
<std::mutex
> statelock
{dev
->StateLock
};
2069 value
= dev
->mDeviceName
.c_str();
2074 ProbeCaptureDeviceList();
2075 value
= alcCaptureDeviceList
.c_str();
2079 /* Default devices are always first in the list */
2080 case ALC_DEFAULT_DEVICE_SPECIFIER
:
2081 value
= GetDefaultName();
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();
2093 alcDefaultAllDevicesSpecifier
= alcAllDevicesArray
.front();
2094 value
= alcDefaultAllDevicesSpecifier
.c_str();
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();
2107 alcCaptureDefaultDeviceSpecifier
= alcCaptureDeviceArray
.front();
2108 value
= alcCaptureDefaultDeviceSpecifier
.c_str();
2112 case ALC_EXTENSIONS
:
2113 if(VerifyDevice(Device
))
2114 value
= GetExtensionList().data();
2116 value
= GetNoDeviceExtList().data();
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() : "");
2126 alcSetError(nullptr, ALC_INVALID_DEVICE
);
2130 alcSetError(VerifyDevice(Device
).get(), ALC_INVALID_ENUM
);
2138 static size_t GetIntegerv(ALCdevice
*device
, ALCenum param
, const al::span
<int> values
)
2142 alcSetError(device
, ALC_INVALID_VALUE
);
2150 case ALC_MAJOR_VERSION
:
2151 values
[0] = alcMajorVersion
;
2153 case ALC_MINOR_VERSION
:
2154 values
[0] = alcMinorVersion
;
2157 case ALC_EFX_MAJOR_VERSION
:
2158 values
[0] = alcEFXMajorVersion
;
2160 case ALC_EFX_MINOR_VERSION
:
2161 values
[0] = alcEFXMinorVersion
;
2163 case ALC_MAX_AUXILIARY_SENDS
:
2164 values
[0] = MaxSendCount
;
2167 case ALC_ATTRIBUTES_SIZE
:
2168 case ALC_ALL_ATTRIBUTES
:
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
);
2185 alcSetError(nullptr, ALC_INVALID_ENUM
);
2190 std::lock_guard
<std::mutex
> statelock
{device
->StateLock
};
2191 if(device
->Type
== DeviceType::Capture
)
2193 static constexpr int MaxCaptureAttributes
{9};
2196 case ALC_ATTRIBUTES_SIZE
:
2197 values
[0] = MaxCaptureAttributes
;
2199 case ALC_ALL_ATTRIBUTES
:
2200 if(values
.size() >= MaxCaptureAttributes
)
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
);
2212 assert(i
== MaxCaptureAttributes
);
2215 alcSetError(device
, ALC_INVALID_VALUE
);
2218 case ALC_MAJOR_VERSION
:
2219 values
[0] = alcMajorVersion
;
2221 case ALC_MINOR_VERSION
:
2222 values
[0] = alcMinorVersion
;
2225 case ALC_CAPTURE_SAMPLES
:
2226 values
[0] = static_cast<int>(device
->Backend
->availableSamples());
2230 values
[0] = device
->Connected
.load(std::memory_order_acquire
);
2234 alcSetError(device
, ALC_INVALID_ENUM
);
2240 auto NumAttrsForDevice
= [](const ALCdevice
*aldev
) noexcept
-> uint8_t
2242 if(aldev
->Type
== DeviceType::Loopback
&& aldev
->FmtChans
== DevFmtAmbi3D
)
2248 case ALC_ATTRIBUTES_SIZE
:
2249 values
[0] = NumAttrsForDevice(device
);
2252 case ALC_ALL_ATTRIBUTES
:
2253 if(values
.size() >= NumAttrsForDevice(device
))
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
;
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());
2321 assert(i
== NumAttrsForDevice(device
));
2324 alcSetError(device
, ALC_INVALID_VALUE
);
2327 case ALC_MAJOR_VERSION
:
2328 values
[0] = alcMajorVersion
;
2331 case ALC_MINOR_VERSION
:
2332 values
[0] = alcMinorVersion
;
2335 case ALC_EFX_MAJOR_VERSION
:
2336 values
[0] = alcEFXMajorVersion
;
2339 case ALC_EFX_MINOR_VERSION
:
2340 values
[0] = alcEFXMinorVersion
;
2344 values
[0] = static_cast<int>(device
->Frequency
);
2348 if(device
->Type
== DeviceType::Loopback
)
2350 alcSetError(device
, ALC_INVALID_DEVICE
);
2353 values
[0] = static_cast<int>(device
->Frequency
/ device
->UpdateSize
);
2357 if(device
->Type
== DeviceType::Loopback
)
2359 alcSetError(device
, ALC_INVALID_DEVICE
);
2362 values
[0] = ALC_FALSE
;
2365 case ALC_FORMAT_CHANNELS_SOFT
:
2366 if(device
->Type
!= DeviceType::Loopback
)
2368 alcSetError(device
, ALC_INVALID_DEVICE
);
2371 values
[0] = EnumFromDevFmt(device
->FmtChans
);
2374 case ALC_FORMAT_TYPE_SOFT
:
2375 if(device
->Type
!= DeviceType::Loopback
)
2377 alcSetError(device
, ALC_INVALID_DEVICE
);
2380 values
[0] = EnumFromDevFmt(device
->FmtType
);
2383 case ALC_AMBISONIC_LAYOUT_SOFT
:
2384 if(device
->Type
!= DeviceType::Loopback
|| device
->FmtChans
!= DevFmtAmbi3D
)
2386 alcSetError(device
, ALC_INVALID_DEVICE
);
2389 values
[0] = EnumFromDevAmbi(device
->mAmbiLayout
);
2392 case ALC_AMBISONIC_SCALING_SOFT
:
2393 if(device
->Type
!= DeviceType::Loopback
|| device
->FmtChans
!= DevFmtAmbi3D
)
2395 alcSetError(device
, ALC_INVALID_DEVICE
);
2398 values
[0] = EnumFromDevAmbi(device
->mAmbiScale
);
2401 case ALC_AMBISONIC_ORDER_SOFT
:
2402 if(device
->Type
!= DeviceType::Loopback
|| device
->FmtChans
!= DevFmtAmbi3D
)
2404 alcSetError(device
, ALC_INVALID_DEVICE
);
2407 values
[0] = static_cast<int>(device
->mAmbiOrder
);
2410 case ALC_MONO_SOURCES
:
2411 values
[0] = static_cast<int>(device
->NumMonoSources
);
2414 case ALC_STEREO_SOURCES
:
2415 values
[0] = static_cast<int>(device
->NumStereoSources
);
2418 case ALC_MAX_AUXILIARY_SENDS
:
2419 values
[0] = static_cast<int>(device
->NumAuxSends
);
2423 values
[0] = device
->Connected
.load(std::memory_order_acquire
);
2427 values
[0] = (device
->mHrtf
? ALC_TRUE
: ALC_FALSE
);
2430 case ALC_HRTF_STATUS_SOFT
:
2431 values
[0] = device
->mHrtfStatus
;
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()}));
2440 case ALC_OUTPUT_LIMITER_SOFT
:
2441 values
[0] = device
->Limiter
? ALC_TRUE
: ALC_FALSE
;
2444 case ALC_MAX_AMBISONIC_ORDER_SOFT
:
2445 values
[0] = MaxAmbiOrder
;
2448 case ALC_OUTPUT_MODE_SOFT
:
2449 values
[0] = static_cast<ALCenum
>(device
->getOutputMode1());
2453 alcSetError(device
, ALC_INVALID_ENUM
);
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
);
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
);
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());
2484 auto NumAttrsForDevice
= [](ALCdevice
*aldev
) noexcept
-> size_t
2486 if(aldev
->Type
== DeviceType::Loopback
&& aldev
->FmtChans
== DevFmtAmbi3D
)
2490 std::lock_guard
<std::mutex
> statelock
{dev
->StateLock
};
2493 case ALC_ATTRIBUTES_SIZE
:
2494 valuespan
[0] = static_cast<ALCint64SOFT
>(NumAttrsForDevice(dev
.get()));
2497 case ALC_ALL_ATTRIBUTES
:
2498 if(valuespan
.size() < NumAttrsForDevice(dev
.get()))
2499 alcSetError(dev
.get(), ALC_INVALID_VALUE
);
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
;
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());
2567 case ALC_DEVICE_CLOCK_SOFT
:
2569 uint samplecount
, refcount
;
2570 nanoseconds basecount
;
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();
2582 case ALC_DEVICE_LATENCY_SOFT
:
2583 valuespan
[0] = GetClockLatency(dev
.get(), dev
->Backend
.get()).Latency
.count();
2586 case ALC_DEVICE_CLOCK_LATENCY_SOFT
:
2588 alcSetError(dev
.get(), ALC_INVALID_VALUE
);
2591 ClockLatency clock
{GetClockLatency(dev
.get(), dev
->Backend
.get())};
2592 valuespan
[0] = clock
.ClockTime
.count();
2593 valuespan
[1] = clock
.Latency
.count();
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());
2606 ALC_API ALCboolean ALC_APIENTRY
alcIsExtensionPresent(ALCdevice
*device
, const ALCchar
*extName
) noexcept
2608 DeviceRef dev
{VerifyDevice(device
)};
2611 alcSetError(dev
.get(), ALC_INVALID_VALUE
);
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
])))
2624 matchpos
= extlist
.find(tofind
, matchpos
+1);
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
2637 DeviceRef dev
{VerifyDevice(device
)};
2638 alcSetError(dev
.get(), ALC_INVALID_VALUE
);
2643 if(eax_g_is_enabled
)
2645 for(const auto &func
: eaxFunctions
)
2647 if(strcmp(func
.funcName
, funcName
) == 0)
2648 return func
.address
;
2652 for(const auto &func
: alcFunctions
)
2654 if(strcmp(func
.funcName
, funcName
) == 0)
2655 return func
.address
;
2661 ALC_API ALCenum ALC_APIENTRY
alcGetEnumValue(ALCdevice
*device
, const ALCchar
*enumName
) noexcept
2665 DeviceRef dev
{VerifyDevice(device
)};
2666 alcSetError(dev
.get(), ALC_INVALID_VALUE
);
2671 if(eax_g_is_enabled
)
2673 for(const auto &enm
: eaxEnumerations
)
2675 if(strcmp(enm
.enumName
, enumName
) == 0)
2680 for(const auto &enm
: alcEnumerations
)
2682 if(strcmp(enm
.enumName
, enumName
) == 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
))
2701 alcSetError(dev
.get(), ALC_INVALID_DEVICE
);
2704 std::unique_lock
<std::mutex
> statelock
{dev
->StateLock
};
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
);
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]);
2727 auto context
= ContextRef
{new(std::nothrow
) ALCcontext
{dev
, ctxflags
}};
2730 alcSetError(dev
.get(), ALC_OUT_OF_MEMORY
);
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
);
2742 const float db
{std::clamp(valf
, -24.0f
, 24.0f
)};
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/
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
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();
2775 auto iter
= std::lower_bound(ContextList
.cbegin(), ContextList
.cend(), context
.get());
2776 ContextList
.emplace(iter
, context
.get());
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());
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
)
2801 alcSetError(nullptr, ALC_INVALID_CONTEXT
);
2805 /* Hold a reference to this context so it remains valid until the ListLock
2808 ContextRef ctx
{*iter
};
2809 ContextList
.erase(iter
);
2811 ALCdevice
*Device
{ctx
->mALDevice
.get()};
2813 std::lock_guard
<std::mutex
> statelock
{Device
->StateLock
};
2818 ALC_API
auto ALC_APIENTRY
alcGetCurrentContext() noexcept
-> ALCcontext
*
2820 ALCcontext
*Context
{ALCcontext::getThreadContext()};
2821 if(!Context
) Context
= ALCcontext::sGlobalContext
.load();
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 */
2835 ctx
= VerifyContext(context
);
2838 alcSetError(nullptr, ALC_INVALID_CONTEXT
);
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. */
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 */
2871 ctx
= VerifyContext(context
);
2874 alcSetError(nullptr, ALC_INVALID_CONTEXT
);
2878 /* context's reference count is already incremented */
2879 ContextRef old
{ALCcontext::getThreadContext()};
2880 ALCcontext::setThreadContext(ctx
.release());
2886 ALC_API ALCdevice
* ALC_APIENTRY
alcGetContextsDevice(ALCcontext
*Context
) noexcept
2888 ContextRef ctx
{VerifyContext(Context
)};
2891 alcSetError(nullptr, ALC_INVALID_CONTEXT
);
2894 return ctx
->mALDevice
.get();
2898 ALC_API ALCdevice
* ALC_APIENTRY
alcOpenDevice(const ALCchar
*deviceName
) noexcept
2902 if(!PlaybackFactory
)
2904 alcSetError(nullptr, ALC_INVALID_VALUE
);
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
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
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)
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());
2938 TRACE("Opening default playback device\n");
2940 const uint DefaultSends
{
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
}};
2950 WARN("Failed to create playback device handle\n");
2951 alcSetError(nullptr, ALC_OUT_OF_MEMORY
);
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
;
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
);
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
);
3022 if((*iter
)->Type
== DeviceType::Capture
)
3024 alcSetError(*iter
, ALC_INVALID_DEVICE
);
3028 /* Erase the device, and any remaining contexts left on it, from their
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
);
3047 for(ContextRef
&context
: orphanctxs
)
3049 WARN("Releasing orphaned context %p\n", voidp
{context
.get()});
3054 if(dev
->mDeviceState
== DeviceState::Playing
)
3056 dev
->Backend
->stop();
3057 dev
->mDeviceState
= DeviceState::Configured
;
3064 /************************************************
3065 * ALC capture functions
3066 ************************************************/
3067 ALC_API ALCdevice
* ALC_APIENTRY
alcCaptureOpenDevice(const ALCchar
*deviceName
, ALCuint frequency
, ALCenum format
, ALCsizei samples
) noexcept
3073 alcSetError(nullptr, ALC_INVALID_VALUE
);
3079 alcSetError(nullptr, ALC_INVALID_VALUE
);
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)
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());
3099 TRACE("Opening default capture device\n");
3101 DeviceRef device
{new(std::nothrow
) ALCdevice
{DeviceType::Capture
}};
3104 WARN("Failed to create capture device handle\n");
3105 alcSetError(nullptr, ALC_OUT_OF_MEMORY
);
3109 auto decompfmt
= DecomposeDevFormat(format
);
3112 alcSetError(nullptr, ALC_INVALID_ENUM
);
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
);
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
);
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
);
3164 if((*iter
)->Type
!= DeviceType::Capture
)
3166 alcSetError(*iter
, ALC_INVALID_DEVICE
);
3170 DeviceRef dev
{*iter
};
3171 DeviceList
.erase(iter
);
3174 std::lock_guard
<std::mutex
> statelock
{dev
->StateLock
};
3175 if(dev
->mDeviceState
== DeviceState::Playing
)
3177 dev
->Backend
->stop();
3178 dev
->mDeviceState
= DeviceState::Configured
;
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
);
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
)
3200 auto backend
= dev
->Backend
.get();
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
);
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
);
3237 if(samples
< 0 || (samples
> 0 && buffer
== nullptr))
3239 alcSetError(dev
.get(), ALC_INVALID_VALUE
);
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
);
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
3268 /* Make sure the device name, if specified, is us. */
3269 if(deviceName
&& strcmp(deviceName
, GetDefaultName()) != 0)
3271 alcSetError(nullptr, ALC_INVALID_VALUE
);
3275 const uint DefaultSends
{
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
}};
3285 WARN("Failed to create loopback device handle\n");
3286 alcSetError(nullptr, ALC_OUT_OF_MEMORY
);
3290 device
->SourcesMax
= 256;
3291 device
->AuxiliaryEffectSlotMax
= 64;
3292 device
->NumAuxSends
= DefaultSends
;
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
;
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
);
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
);
3338 alcSetError(dev
.get(), ALC_INVALID_VALUE
);
3341 if(DevFmtTypeFromEnum(type
).has_value() && DevFmtChannelsFromEnum(channels
).has_value()
3342 && freq
>= int{MinOutputRate
} && freq
<= int{MaxOutputRate
})
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
]]
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
);
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
);
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
);
3402 std::lock_guard
<std::mutex
> statelock
{dev
->StateLock
};
3403 if(!dev
->Flags
.test(DevicePaused
))
3405 if(dev
->mDeviceState
< DeviceState::Configured
)
3407 WARN("Cannot resume unconfigured device\n");
3408 alcSetError(dev
.get(), ALC_INVALID_DEVICE
);
3411 if(!dev
->Connected
.load())
3413 WARN("Cannot resume a disconnected device\n");
3414 alcSetError(dev
.get(), ALC_INVALID_DEVICE
);
3417 dev
->Flags
.reset(DevicePaused
);
3418 if(dev
->mContexts
.load()->empty())
3422 auto backend
= dev
->Backend
.get();
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
);
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
);
3457 alcSetError(dev
.get(), ALC_INVALID_ENUM
);
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
)
3472 alcSetError(dev
.get(), ALC_INVALID_DEVICE
);
3475 std::lock_guard
<std::mutex
> statelock
{dev
->StateLock
};
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
)
3504 alcSetError(dev
.get(), ALC_INVALID_DEVICE
);
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
);
3519 if(al::case_compare(devname
, GetDefaultName()) == 0)
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
};
3534 dev
->Backend
->stop();
3535 dev
->mDeviceState
= DeviceState::Configured
;
3538 BackendPtr newbackend
;
3540 newbackend
= PlaybackFactory
->createBackend(dev
.get(), BackendType::Playback
);
3541 newbackend
->open(devname
);
3543 catch(al::backend_exception
&e
) {
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
)
3554 auto backend
= dev
->Backend
.get();
3556 dev
->mDeviceState
= DeviceState::Playing
;
3558 catch(al::backend_exception
&be
) {
3559 ERR("%s\n", be
.what());
3560 dev
->handleDisconnect("%s", be
.what());
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
));
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
);
3619 WARN("Invalid event type: 0x%04x\n", eventType
);
3620 alcSetError(nullptr, ALC_INVALID_ENUM
);
3624 auto supported
= alc::EventSupport::NoSupport
;
3627 case ALC_PLAYBACK_DEVICE_SOFT
:
3629 supported
= PlaybackFactory
->queryEventSupport(*etype
, BackendType::Playback
);
3630 return al::to_underlying(supported
);
3632 case ALC_CAPTURE_DEVICE_SOFT
:
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
);