Don't call log10 with values too close to 0
[openal-soft.git] / alc / backends / sdl2.cpp
blob8dc319f96891e72039ef237c4f1070598b495869
1 /**
2 * OpenAL cross platform audio library
3 * Copyright (C) 2018 by authors.
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 * Or go to http://www.gnu.org/copyleft/lgpl.html
21 #include "config.h"
23 #include "sdl2.h"
25 #include <cassert>
26 #include <cstdlib>
27 #include <cstring>
28 #include <string>
29 #include <string_view>
31 #include "alstring.h"
32 #include "core/device.h"
34 _Pragma("GCC diagnostic push")
35 _Pragma("GCC diagnostic ignored \"-Wold-style-cast\"")
36 #include "SDL.h"
37 _Pragma("GCC diagnostic pop")
40 namespace {
42 #ifdef _WIN32
43 #define DEVNAME_PREFIX "OpenAL Soft on "
44 #else
45 #define DEVNAME_PREFIX ""
46 #endif
48 constexpr auto getDevicePrefix() noexcept -> std::string_view { return DEVNAME_PREFIX; }
49 constexpr auto getDefaultDeviceName() noexcept -> std::string_view
50 { return DEVNAME_PREFIX "Default Device"; }
52 struct Sdl2Backend final : public BackendBase {
53 Sdl2Backend(DeviceBase *device) noexcept : BackendBase{device} { }
54 ~Sdl2Backend() override;
56 void audioCallback(Uint8 *stream, int len) noexcept;
58 void open(std::string_view name) override;
59 bool reset() override;
60 void start() override;
61 void stop() override;
63 std::string mDeviceName;
64 SDL_AudioDeviceID mDeviceID{0u};
65 uint mFrameSize{0};
68 Sdl2Backend::~Sdl2Backend()
70 if(mDeviceID)
71 SDL_CloseAudioDevice(mDeviceID);
72 mDeviceID = 0;
75 void Sdl2Backend::audioCallback(Uint8 *stream, int len) noexcept
77 const auto ulen = static_cast<unsigned int>(len);
78 assert((ulen % mFrameSize) == 0);
79 mDevice->renderSamples(stream, ulen / mFrameSize, mDevice->channelsFromFmt());
82 void Sdl2Backend::open(std::string_view name)
84 SDL_AudioSpec want{}, have{};
86 want.freq = static_cast<int>(mDevice->Frequency);
87 switch(mDevice->FmtType)
89 case DevFmtUByte: want.format = AUDIO_U8; break;
90 case DevFmtByte: want.format = AUDIO_S8; break;
91 case DevFmtUShort: want.format = AUDIO_U16SYS; break;
92 case DevFmtShort: want.format = AUDIO_S16SYS; break;
93 case DevFmtUInt: /* fall-through */
94 case DevFmtInt: want.format = AUDIO_S32SYS; break;
95 case DevFmtFloat: want.format = AUDIO_F32; break;
97 want.channels = static_cast<Uint8>(std::min<uint>(mDevice->channelsFromFmt(),
98 std::numeric_limits<Uint8>::max()));
99 want.samples = static_cast<Uint16>(std::min(mDevice->UpdateSize, 8192u));
100 want.callback = [](void *ptr, Uint8 *stream, int len) noexcept
101 { return static_cast<Sdl2Backend*>(ptr)->audioCallback(stream, len); };
102 want.userdata = this;
104 /* Passing nullptr to SDL_OpenAudioDevice opens a default, which isn't
105 * necessarily the first in the list.
107 const auto defaultDeviceName = getDefaultDeviceName();
108 if(name.empty() || name == defaultDeviceName)
110 name = defaultDeviceName;
111 mDeviceName.clear();
112 mDeviceID = SDL_OpenAudioDevice(nullptr, SDL_FALSE, &want, &have,
113 SDL_AUDIO_ALLOW_ANY_CHANGE);
115 else
117 const auto namePrefix = getDevicePrefix();
118 if(name.size() >= namePrefix.size() && al::starts_with(name, namePrefix))
120 mDeviceName = name.substr(namePrefix.size());
121 mDeviceID = SDL_OpenAudioDevice(mDeviceName.c_str(), SDL_FALSE, &want, &have,
122 SDL_AUDIO_ALLOW_ANY_CHANGE);
124 else
126 mDeviceName = name;
127 mDeviceID = SDL_OpenAudioDevice(mDeviceName.c_str(), SDL_FALSE, &want, &have,
128 SDL_AUDIO_ALLOW_ANY_CHANGE);
131 if(!mDeviceID)
132 throw al::backend_exception{al::backend_error::NoDevice, "%s", SDL_GetError()};
134 DevFmtType devtype{};
135 switch(have.format)
137 case AUDIO_U8: devtype = DevFmtUByte; break;
138 case AUDIO_S8: devtype = DevFmtByte; break;
139 case AUDIO_U16SYS: devtype = DevFmtUShort; break;
140 case AUDIO_S16SYS: devtype = DevFmtShort; break;
141 case AUDIO_S32SYS: devtype = DevFmtInt; break;
142 case AUDIO_F32SYS: devtype = DevFmtFloat; break;
143 default:
144 throw al::backend_exception{al::backend_error::DeviceError, "Unhandled SDL format: 0x%04x",
145 have.format};
148 mFrameSize = BytesFromDevFmt(devtype) * have.channels;
150 mDevice->DeviceName = name;
153 bool Sdl2Backend::reset()
155 if(mDeviceID)
156 SDL_CloseAudioDevice(mDeviceID);
157 mDeviceID = 0;
159 auto want = SDL_AudioSpec{};
160 want.freq = static_cast<int>(mDevice->Frequency);
161 switch(mDevice->FmtType)
163 case DevFmtUByte: want.format = AUDIO_U8; break;
164 case DevFmtByte: want.format = AUDIO_S8; break;
165 case DevFmtUShort: want.format = AUDIO_U16SYS; break;
166 case DevFmtShort: want.format = AUDIO_S16SYS; break;
167 case DevFmtUInt: [[fallthrough]];
168 case DevFmtInt: want.format = AUDIO_S32SYS; break;
169 case DevFmtFloat: want.format = AUDIO_F32; break;
171 want.channels = static_cast<Uint8>(std::min<uint>(mDevice->channelsFromFmt(),
172 std::numeric_limits<Uint8>::max()));
173 want.samples = static_cast<Uint16>(std::min(mDevice->UpdateSize, 8192u));
174 want.callback = [](void *ptr, Uint8 *stream, int len) noexcept
175 { return static_cast<Sdl2Backend*>(ptr)->audioCallback(stream, len); };
176 want.userdata = this;
178 auto have = SDL_AudioSpec{};
179 if(mDeviceName.empty())
181 mDeviceID = SDL_OpenAudioDevice(nullptr, SDL_FALSE, &want, &have,
182 SDL_AUDIO_ALLOW_ANY_CHANGE);
184 else
186 mDeviceID = SDL_OpenAudioDevice(mDeviceName.c_str(), SDL_FALSE, &want, &have,
187 SDL_AUDIO_ALLOW_ANY_CHANGE);
189 if(!mDeviceID)
190 throw al::backend_exception{al::backend_error::NoDevice, "%s", SDL_GetError()};
192 if(have.channels != mDevice->channelsFromFmt())
194 if(have.channels < 1)
195 throw al::backend_exception{al::backend_error::DeviceError,
196 "Unhandled SDL channel count: %d", int{have.channels}};
197 if(have.channels == 1)
198 mDevice->FmtChans = DevFmtMono;
199 else
200 mDevice->FmtChans = DevFmtStereo;
201 mDevice->mAmbiOrder = 0;
204 switch(have.format)
206 case AUDIO_U8: mDevice->FmtType = DevFmtUByte; break;
207 case AUDIO_S8: mDevice->FmtType = DevFmtByte; break;
208 case AUDIO_U16SYS: mDevice->FmtType = DevFmtUShort; break;
209 case AUDIO_S16SYS: mDevice->FmtType = DevFmtShort; break;
210 case AUDIO_S32SYS: mDevice->FmtType = DevFmtInt; break;
211 case AUDIO_F32SYS: mDevice->FmtType = DevFmtFloat; break;
212 default:
213 throw al::backend_exception{al::backend_error::DeviceError, "Unhandled SDL format: 0x%04x",
214 have.format};
217 mFrameSize = BytesFromDevFmt(mDevice->FmtType) * have.channels;
219 if(have.freq < int{MinOutputRate})
220 throw al::backend_exception{al::backend_error::DeviceError,
221 "Unhandled SDL sample rate: %d", have.format};
223 mDevice->Frequency = static_cast<uint>(have.freq);
224 mDevice->UpdateSize = have.samples;
225 mDevice->BufferSize = std::max(have.size/mFrameSize, mDevice->UpdateSize*2u);
227 setDefaultWFXChannelOrder();
229 return true;
232 void Sdl2Backend::start()
233 { SDL_PauseAudioDevice(mDeviceID, 0); }
235 void Sdl2Backend::stop()
236 { SDL_PauseAudioDevice(mDeviceID, 1); }
238 } // namespace
240 BackendFactory &SDL2BackendFactory::getFactory()
242 static SDL2BackendFactory factory{};
243 return factory;
246 bool SDL2BackendFactory::init()
247 { return (SDL_InitSubSystem(SDL_INIT_AUDIO) == 0); }
249 bool SDL2BackendFactory::querySupport(BackendType type)
250 { return type == BackendType::Playback; }
252 auto SDL2BackendFactory::enumerate(BackendType type) -> std::vector<std::string>
254 std::vector<std::string> outnames;
256 if(type != BackendType::Playback)
257 return outnames;
259 int num_devices{SDL_GetNumAudioDevices(SDL_FALSE)};
260 if(num_devices <= 0)
261 return outnames;
263 outnames.reserve(static_cast<unsigned int>(num_devices));
264 outnames.emplace_back(getDefaultDeviceName());
265 for(int i{0};i < num_devices;++i)
267 std::string outname{getDevicePrefix()};
268 if(const char *name = SDL_GetAudioDeviceName(i, SDL_FALSE))
269 outname += name;
270 else
271 outname += "Unknown Device Name #"+std::to_string(i);
272 outnames.emplace_back(std::move(outname));
274 return outnames;
277 BackendPtr SDL2BackendFactory::createBackend(DeviceBase *device, BackendType type)
279 if(type == BackendType::Playback)
280 return BackendPtr{new Sdl2Backend{device}};
281 return nullptr;