Recognize more channel configurations from SDL
[openal-soft.git] / alc / backends / sdl2.cpp
blob04131c4bb2fd9080203a3fee6093c6b0c509a04a
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 "alnumeric.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 using namespace std::string_view_literals;
44 [[nodiscard]] constexpr auto getDefaultDeviceName() noexcept -> std::string_view
45 { return "Default Device"sv; }
47 struct Sdl2Backend final : public BackendBase {
48 Sdl2Backend(DeviceBase *device) noexcept : BackendBase{device} { }
49 ~Sdl2Backend() override;
51 void audioCallback(Uint8 *stream, int len) noexcept;
53 void open(std::string_view name) override;
54 bool reset() override;
55 void start() override;
56 void stop() override;
58 std::string mSDLName;
59 SDL_AudioDeviceID mDeviceID{0u};
60 uint mFrameSize{0};
63 Sdl2Backend::~Sdl2Backend()
65 if(mDeviceID)
66 SDL_CloseAudioDevice(mDeviceID);
67 mDeviceID = 0;
70 void Sdl2Backend::audioCallback(Uint8 *stream, int len) noexcept
72 const auto ulen = static_cast<unsigned int>(len);
73 assert((ulen % mFrameSize) == 0);
74 mDevice->renderSamples(stream, ulen / mFrameSize, mDevice->channelsFromFmt());
77 void Sdl2Backend::open(std::string_view name)
79 SDL_AudioSpec want{}, have{};
81 want.freq = static_cast<int>(mDevice->Frequency);
82 switch(mDevice->FmtType)
84 case DevFmtUByte: want.format = AUDIO_U8; break;
85 case DevFmtByte: want.format = AUDIO_S8; break;
86 case DevFmtUShort: want.format = AUDIO_U16SYS; break;
87 case DevFmtShort: want.format = AUDIO_S16SYS; break;
88 case DevFmtUInt: /* fall-through */
89 case DevFmtInt: want.format = AUDIO_S32SYS; break;
90 case DevFmtFloat: want.format = AUDIO_F32; break;
92 want.channels = static_cast<Uint8>(std::min<uint>(mDevice->channelsFromFmt(),
93 std::numeric_limits<Uint8>::max()));
94 want.samples = static_cast<Uint16>(std::min(mDevice->UpdateSize, 8192u));
95 want.callback = [](void *ptr, Uint8 *stream, int len) noexcept
96 { return static_cast<Sdl2Backend*>(ptr)->audioCallback(stream, len); };
97 want.userdata = this;
99 /* Passing nullptr to SDL_OpenAudioDevice opens a default, which isn't
100 * necessarily the first in the list.
102 const auto defaultDeviceName = getDefaultDeviceName();
103 if(name.empty() || name == defaultDeviceName)
105 name = defaultDeviceName;
106 mSDLName.clear();
107 mDeviceID = SDL_OpenAudioDevice(nullptr, SDL_FALSE, &want, &have,
108 SDL_AUDIO_ALLOW_ANY_CHANGE);
110 else
112 mSDLName = name;
113 mDeviceID = SDL_OpenAudioDevice(mSDLName.c_str(), SDL_FALSE, &want, &have,
114 SDL_AUDIO_ALLOW_ANY_CHANGE);
116 if(!mDeviceID)
117 throw al::backend_exception{al::backend_error::NoDevice, "%s", SDL_GetError()};
119 DevFmtType devtype{};
120 switch(have.format)
122 case AUDIO_U8: devtype = DevFmtUByte; break;
123 case AUDIO_S8: devtype = DevFmtByte; break;
124 case AUDIO_U16SYS: devtype = DevFmtUShort; break;
125 case AUDIO_S16SYS: devtype = DevFmtShort; break;
126 case AUDIO_S32SYS: devtype = DevFmtInt; break;
127 case AUDIO_F32SYS: devtype = DevFmtFloat; break;
128 default:
129 throw al::backend_exception{al::backend_error::DeviceError, "Unhandled SDL format: 0x%04x",
130 have.format};
133 mFrameSize = BytesFromDevFmt(devtype) * have.channels;
135 mDeviceName = name;
138 bool Sdl2Backend::reset()
140 if(mDeviceID)
141 SDL_CloseAudioDevice(mDeviceID);
142 mDeviceID = 0;
144 auto want = SDL_AudioSpec{};
145 want.freq = static_cast<int>(mDevice->Frequency);
146 switch(mDevice->FmtType)
148 case DevFmtUByte: want.format = AUDIO_U8; break;
149 case DevFmtByte: want.format = AUDIO_S8; break;
150 case DevFmtUShort: want.format = AUDIO_U16SYS; break;
151 case DevFmtShort: want.format = AUDIO_S16SYS; break;
152 case DevFmtUInt: [[fallthrough]];
153 case DevFmtInt: want.format = AUDIO_S32SYS; break;
154 case DevFmtFloat: want.format = AUDIO_F32; break;
156 want.channels = static_cast<Uint8>(std::min<uint>(mDevice->channelsFromFmt(),
157 std::numeric_limits<Uint8>::max()));
158 want.samples = static_cast<Uint16>(std::min(mDevice->UpdateSize, 8192u));
159 want.callback = [](void *ptr, Uint8 *stream, int len) noexcept
160 { return static_cast<Sdl2Backend*>(ptr)->audioCallback(stream, len); };
161 want.userdata = this;
163 auto have = SDL_AudioSpec{};
164 if(mSDLName.empty())
166 mDeviceID = SDL_OpenAudioDevice(nullptr, SDL_FALSE, &want, &have,
167 SDL_AUDIO_ALLOW_ANY_CHANGE);
169 else
171 mDeviceID = SDL_OpenAudioDevice(mSDLName.c_str(), SDL_FALSE, &want, &have,
172 SDL_AUDIO_ALLOW_ANY_CHANGE);
174 if(!mDeviceID)
175 throw al::backend_exception{al::backend_error::NoDevice, "%s", SDL_GetError()};
177 if(have.channels != mDevice->channelsFromFmt())
179 /* SDL guarantees these layouts for the given channel count. */
180 if(have.channels == 8)
181 mDevice->FmtChans = DevFmtX71;
182 else if(have.channels == 7)
183 mDevice->FmtChans = DevFmtX61;
184 else if(have.channels == 6)
185 mDevice->FmtChans = DevFmtX51;
186 else if(have.channels == 4)
187 mDevice->FmtChans = DevFmtQuad;
188 else if(have.channels >= 2)
189 mDevice->FmtChans = DevFmtStereo;
190 else if(have.channels == 1)
191 mDevice->FmtChans = DevFmtMono;
192 else
193 throw al::backend_exception{al::backend_error::DeviceError,
194 "Unhandled SDL channel count: %d", int{have.channels}};
195 mDevice->mAmbiOrder = 0;
198 switch(have.format)
200 case AUDIO_U8: mDevice->FmtType = DevFmtUByte; break;
201 case AUDIO_S8: mDevice->FmtType = DevFmtByte; break;
202 case AUDIO_U16SYS: mDevice->FmtType = DevFmtUShort; break;
203 case AUDIO_S16SYS: mDevice->FmtType = DevFmtShort; break;
204 case AUDIO_S32SYS: mDevice->FmtType = DevFmtInt; break;
205 case AUDIO_F32SYS: mDevice->FmtType = DevFmtFloat; break;
206 default:
207 throw al::backend_exception{al::backend_error::DeviceError, "Unhandled SDL format: 0x%04x",
208 have.format};
211 mFrameSize = BytesFromDevFmt(mDevice->FmtType) * have.channels;
213 if(have.freq < int{MinOutputRate})
214 throw al::backend_exception{al::backend_error::DeviceError,
215 "Unhandled SDL sample rate: %d", have.format};
217 mDevice->Frequency = static_cast<uint>(have.freq);
218 mDevice->UpdateSize = have.samples;
219 mDevice->BufferSize = std::max(have.size/mFrameSize, mDevice->UpdateSize*2u);
221 setDefaultWFXChannelOrder();
223 return true;
226 void Sdl2Backend::start()
227 { SDL_PauseAudioDevice(mDeviceID, 0); }
229 void Sdl2Backend::stop()
230 { SDL_PauseAudioDevice(mDeviceID, 1); }
232 } // namespace
234 BackendFactory &SDL2BackendFactory::getFactory()
236 static SDL2BackendFactory factory{};
237 return factory;
240 bool SDL2BackendFactory::init()
241 { return (SDL_InitSubSystem(SDL_INIT_AUDIO) == 0); }
243 bool SDL2BackendFactory::querySupport(BackendType type)
244 { return type == BackendType::Playback; }
246 auto SDL2BackendFactory::enumerate(BackendType type) -> std::vector<std::string>
248 std::vector<std::string> outnames;
250 if(type != BackendType::Playback)
251 return outnames;
253 int num_devices{SDL_GetNumAudioDevices(SDL_FALSE)};
254 if(num_devices <= 0)
255 return outnames;
257 outnames.reserve(static_cast<unsigned int>(num_devices)+1_uz);
258 outnames.emplace_back(getDefaultDeviceName());
259 for(int i{0};i < num_devices;++i)
261 if(const char *name = SDL_GetAudioDeviceName(i, SDL_FALSE))
262 outnames.emplace_back(name);
263 else
264 outnames.emplace_back("Unknown Device Name #"+std::to_string(i));
266 return outnames;
269 BackendPtr SDL2BackendFactory::createBackend(DeviceBase *device, BackendType type)
271 if(type == BackendType::Playback)
272 return BackendPtr{new Sdl2Backend{device}};
273 return nullptr;