MCPcopy Create free account
hub / github.com/OpenLoco/OpenLoco / openDevice

Function openDevice

src/Audio/src/AudioEngine.cpp:101–157  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

99 }
100
101 bool openDevice(const std::string& name)
102 {
103 closeDevice();
104
105 _alcDevice = name.empty() ? alcOpenDevice(nullptr) : alcOpenDevice(name.c_str());
106 if (_alcDevice == nullptr)
107 {
108 Logging::error("Failed to open OpenAL device '{}'", name.empty() ? "default" : name);
109 return false;
110 }
111
112 constexpr ALCint attrs[] = {
113 ALC_HRTF_SOFT, ALC_FALSE, 0
114 };
115
116 _alcContext = alcCreateContext(_alcDevice, attrs);
117 if (_alcContext == nullptr)
118 {
119 Logging::error("Failed to create OpenAL context");
120 alcCloseDevice(_alcDevice);
121 _alcDevice = nullptr;
122 return false;
123 }
124
125 if (!alcMakeContextCurrent(_alcContext))
126 {
127 Logging::error("Failed to make OpenAL context current");
128 alcDestroyContext(_alcContext);
129 alcCloseDevice(_alcDevice);
130 _alcContext = nullptr;
131 _alcDevice = nullptr;
132 return false;
133 }
134
135 Logging::info("OpenAL {}, Vendor: {}, Renderer: {}, initialized.", alGetString(AL_VERSION), alGetString(AL_VENDOR), alGetString(AL_RENDERER));
136
137 // Need to disable this for debug builds, there is a bug in OpenAL that triggers an iterator check, but no release has been made yet.
138 // https://github.com/kcat/openal-soft/issues/1238
139 // It is functional for release builds so we just skip it for debug. Once a fixed version of OpenAL is released we can remove this workaround.
140#ifdef NDEBUG
141 _reverbAvailable = alcIsExtensionPresent(_alcDevice, "ALC_EXT_EFX") == ALC_TRUE;
142#else
143 _reverbAvailable = false;
144#endif
145 if (_reverbAvailable)
146 {
147 alGenEffects(1, &_reverbEffect);
148 alEffecti(_reverbEffect, AL_EFFECT_TYPE, AL_EFFECT_REVERB);
149
150 alGenAuxiliaryEffectSlots(1, &_reverbSlot);
151 alAuxiliaryEffectSloti(_reverbSlot, AL_EFFECTSLOT_EFFECT, static_cast<ALint>(_reverbEffect));
152
153 Logging::info("OpenAL EFX reverb initialized.");
154 }
155
156 return true;
157 }
158

Callers 1

initialiseDSoundFunction · 0.85

Calls 4

closeDeviceFunction · 0.85
errorFunction · 0.85
infoFunction · 0.85
emptyMethod · 0.45

Tested by

no test coverage detected