]> git.tdb.fi Git - ext/openal.git/blob - alc/backends/sdl2.cpp
Import OpenAL Soft 1.23.1 sources
[ext/openal.git] / alc / backends / sdl2.cpp
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.
8  *
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.
13  *
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
19  */
20
21 #include "config.h"
22
23 #include "sdl2.h"
24
25 #include <cassert>
26 #include <cstdlib>
27 #include <cstring>
28 #include <string>
29
30 #include "almalloc.h"
31 #include "alnumeric.h"
32 #include "core/device.h"
33 #include "core/logging.h"
34
35 _Pragma("GCC diagnostic push")
36 _Pragma("GCC diagnostic ignored \"-Wold-style-cast\"")
37 #include "SDL.h"
38 _Pragma("GCC diagnostic pop")
39
40
41 namespace {
42
43 #ifdef _WIN32
44 #define DEVNAME_PREFIX "OpenAL Soft on "
45 #else
46 #define DEVNAME_PREFIX ""
47 #endif
48
49 constexpr char defaultDeviceName[] = DEVNAME_PREFIX "Default Device";
50
51 struct Sdl2Backend final : public BackendBase {
52     Sdl2Backend(DeviceBase *device) noexcept : BackendBase{device} { }
53     ~Sdl2Backend() override;
54
55     void audioCallback(Uint8 *stream, int len) noexcept;
56     static void audioCallbackC(void *ptr, Uint8 *stream, int len) noexcept
57     { static_cast<Sdl2Backend*>(ptr)->audioCallback(stream, len); }
58
59     void open(const char *name) override;
60     bool reset() override;
61     void start() override;
62     void stop() override;
63
64     SDL_AudioDeviceID mDeviceID{0u};
65     uint mFrameSize{0};
66
67     uint mFrequency{0u};
68     DevFmtChannels mFmtChans{};
69     DevFmtType     mFmtType{};
70     uint mUpdateSize{0u};
71
72     DEF_NEWDEL(Sdl2Backend)
73 };
74
75 Sdl2Backend::~Sdl2Backend()
76 {
77     if(mDeviceID)
78         SDL_CloseAudioDevice(mDeviceID);
79     mDeviceID = 0;
80 }
81
82 void Sdl2Backend::audioCallback(Uint8 *stream, int len) noexcept
83 {
84     const auto ulen = static_cast<unsigned int>(len);
85     assert((ulen % mFrameSize) == 0);
86     mDevice->renderSamples(stream, ulen / mFrameSize, mDevice->channelsFromFmt());
87 }
88
89 void Sdl2Backend::open(const char *name)
90 {
91     SDL_AudioSpec want{}, have{};
92
93     want.freq = static_cast<int>(mDevice->Frequency);
94     switch(mDevice->FmtType)
95     {
96     case DevFmtUByte: want.format = AUDIO_U8; break;
97     case DevFmtByte: want.format = AUDIO_S8; break;
98     case DevFmtUShort: want.format = AUDIO_U16SYS; break;
99     case DevFmtShort: want.format = AUDIO_S16SYS; break;
100     case DevFmtUInt: /* fall-through */
101     case DevFmtInt: want.format = AUDIO_S32SYS; break;
102     case DevFmtFloat: want.format = AUDIO_F32; break;
103     }
104     want.channels = (mDevice->FmtChans == DevFmtMono) ? 1 : 2;
105     want.samples = static_cast<Uint16>(minu(mDevice->UpdateSize, 8192));
106     want.callback = &Sdl2Backend::audioCallbackC;
107     want.userdata = this;
108
109     /* Passing nullptr to SDL_OpenAudioDevice opens a default, which isn't
110      * necessarily the first in the list.
111      */
112     SDL_AudioDeviceID devid;
113     if(!name || strcmp(name, defaultDeviceName) == 0)
114         devid = SDL_OpenAudioDevice(nullptr, SDL_FALSE, &want, &have, SDL_AUDIO_ALLOW_ANY_CHANGE);
115     else
116     {
117         const size_t prefix_len = strlen(DEVNAME_PREFIX);
118         if(strncmp(name, DEVNAME_PREFIX, prefix_len) == 0)
119             devid = SDL_OpenAudioDevice(name+prefix_len, SDL_FALSE, &want, &have,
120                 SDL_AUDIO_ALLOW_ANY_CHANGE);
121         else
122             devid = SDL_OpenAudioDevice(name, SDL_FALSE, &want, &have, SDL_AUDIO_ALLOW_ANY_CHANGE);
123     }
124     if(!devid)
125         throw al::backend_exception{al::backend_error::NoDevice, "%s", SDL_GetError()};
126
127     DevFmtChannels devchans{};
128     if(have.channels >= 2)
129         devchans = DevFmtStereo;
130     else if(have.channels == 1)
131         devchans = DevFmtMono;
132     else
133     {
134         SDL_CloseAudioDevice(devid);
135         throw al::backend_exception{al::backend_error::DeviceError,
136             "Unhandled SDL channel count: %d", int{have.channels}};
137     }
138
139     DevFmtType devtype{};
140     switch(have.format)
141     {
142     case AUDIO_U8:     devtype = DevFmtUByte;  break;
143     case AUDIO_S8:     devtype = DevFmtByte;   break;
144     case AUDIO_U16SYS: devtype = DevFmtUShort; break;
145     case AUDIO_S16SYS: devtype = DevFmtShort;  break;
146     case AUDIO_S32SYS: devtype = DevFmtInt;    break;
147     case AUDIO_F32SYS: devtype = DevFmtFloat;  break;
148     default:
149         SDL_CloseAudioDevice(devid);
150         throw al::backend_exception{al::backend_error::DeviceError, "Unhandled SDL format: 0x%04x",
151             have.format};
152     }
153
154     if(mDeviceID)
155         SDL_CloseAudioDevice(mDeviceID);
156     mDeviceID = devid;
157
158     mFrameSize = BytesFromDevFmt(devtype) * have.channels;
159     mFrequency = static_cast<uint>(have.freq);
160     mFmtChans = devchans;
161     mFmtType = devtype;
162     mUpdateSize = have.samples;
163
164     mDevice->DeviceName = name ? name : defaultDeviceName;
165 }
166
167 bool Sdl2Backend::reset()
168 {
169     mDevice->Frequency = mFrequency;
170     mDevice->FmtChans = mFmtChans;
171     mDevice->FmtType = mFmtType;
172     mDevice->UpdateSize = mUpdateSize;
173     mDevice->BufferSize = mUpdateSize * 2; /* SDL always (tries to) use two periods. */
174     setDefaultWFXChannelOrder();
175     return true;
176 }
177
178 void Sdl2Backend::start()
179 { SDL_PauseAudioDevice(mDeviceID, 0); }
180
181 void Sdl2Backend::stop()
182 { SDL_PauseAudioDevice(mDeviceID, 1); }
183
184 } // namespace
185
186 BackendFactory &SDL2BackendFactory::getFactory()
187 {
188     static SDL2BackendFactory factory{};
189     return factory;
190 }
191
192 bool SDL2BackendFactory::init()
193 { return (SDL_InitSubSystem(SDL_INIT_AUDIO) == 0); }
194
195 bool SDL2BackendFactory::querySupport(BackendType type)
196 { return type == BackendType::Playback; }
197
198 std::string SDL2BackendFactory::probe(BackendType type)
199 {
200     std::string outnames;
201
202     if(type != BackendType::Playback)
203         return outnames;
204
205     int num_devices{SDL_GetNumAudioDevices(SDL_FALSE)};
206
207     /* Includes null char. */
208     outnames.append(defaultDeviceName, sizeof(defaultDeviceName));
209     for(int i{0};i < num_devices;++i)
210     {
211         std::string name{DEVNAME_PREFIX};
212         name += SDL_GetAudioDeviceName(i, SDL_FALSE);
213         if(!name.empty())
214             outnames.append(name.c_str(), name.length()+1);
215     }
216     return outnames;
217 }
218
219 BackendPtr SDL2BackendFactory::createBackend(DeviceBase *device, BackendType type)
220 {
221     if(type == BackendType::Playback)
222         return BackendPtr{new Sdl2Backend{device}};
223     return nullptr;
224 }