Limit convolution processing to the output ambisonic order
[openal-soft.git] / alc / backends / sdl2.cpp
blob303351406d53c6d9b38cbb7bd87eda9d1f6c448b
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 "backends/sdl2.h"
25 #include <cassert>
26 #include <cstdlib>
27 #include <cstring>
28 #include <string>
30 #include "AL/al.h"
32 #include "alcmain.h"
33 #include "alexcpt.h"
34 #include "almalloc.h"
35 #include "alu.h"
36 #include "logging.h"
38 #include <SDL2/SDL.h>
41 namespace {
43 #ifdef _WIN32
44 #define DEVNAME_PREFIX "OpenAL Soft on "
45 #else
46 #define DEVNAME_PREFIX ""
47 #endif
49 constexpr ALCchar defaultDeviceName[] = DEVNAME_PREFIX "Default Device";
51 struct Sdl2Backend final : public BackendBase {
52 Sdl2Backend(ALCdevice *device) noexcept : BackendBase{device} { }
53 ~Sdl2Backend() override;
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); }
59 void open(const ALCchar *name) override;
60 bool reset() override;
61 void start() override;
62 void stop() override;
64 SDL_AudioDeviceID mDeviceID{0u};
65 ALuint mFrameSize{0};
67 ALuint mFrequency{0u};
68 DevFmtChannels mFmtChans{};
69 DevFmtType mFmtType{};
70 ALuint mUpdateSize{0u};
72 DEF_NEWDEL(Sdl2Backend)
75 Sdl2Backend::~Sdl2Backend()
77 if(mDeviceID)
78 SDL_CloseAudioDevice(mDeviceID);
79 mDeviceID = 0;
82 void Sdl2Backend::audioCallback(Uint8 *stream, int len) noexcept
84 const auto ulen = static_cast<unsigned int>(len);
85 assert((ulen % mFrameSize) == 0);
86 mDevice->renderSamples(stream, ulen / mFrameSize, mDevice->channelsFromFmt());
89 void Sdl2Backend::open(const ALCchar *name)
91 SDL_AudioSpec want{}, have{};
93 want.freq = static_cast<int>(mDevice->Frequency);
94 switch(mDevice->FmtType)
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;
104 want.channels = (mDevice->FmtChans == DevFmtMono) ? 1 : 2;
105 want.samples = static_cast<Uint16>(mDevice->UpdateSize);
106 want.callback = &Sdl2Backend::audioCallbackC;
107 want.userdata = this;
109 /* Passing nullptr to SDL_OpenAudioDevice opens a default, which isn't
110 * necessarily the first in the list.
112 if(!name || strcmp(name, defaultDeviceName) == 0)
113 mDeviceID = SDL_OpenAudioDevice(nullptr, SDL_FALSE, &want, &have,
114 SDL_AUDIO_ALLOW_ANY_CHANGE);
115 else
117 const size_t prefix_len = strlen(DEVNAME_PREFIX);
118 if(strncmp(name, DEVNAME_PREFIX, prefix_len) == 0)
119 mDeviceID = SDL_OpenAudioDevice(name+prefix_len, SDL_FALSE, &want, &have,
120 SDL_AUDIO_ALLOW_ANY_CHANGE);
121 else
122 mDeviceID = SDL_OpenAudioDevice(name, SDL_FALSE, &want, &have,
123 SDL_AUDIO_ALLOW_ANY_CHANGE);
125 if(mDeviceID == 0)
126 throw al::backend_exception{ALC_INVALID_VALUE, "%s", SDL_GetError()};
128 mDevice->Frequency = static_cast<ALuint>(have.freq);
130 if(have.channels == 1)
131 mDevice->FmtChans = DevFmtMono;
132 else if(have.channels == 2)
133 mDevice->FmtChans = DevFmtStereo;
134 else
135 throw al::backend_exception{ALC_INVALID_VALUE, "Unhandled SDL channel count: %d",
136 int{have.channels}};
138 switch(have.format)
140 case AUDIO_U8: mDevice->FmtType = DevFmtUByte; break;
141 case AUDIO_S8: mDevice->FmtType = DevFmtByte; break;
142 case AUDIO_U16SYS: mDevice->FmtType = DevFmtUShort; break;
143 case AUDIO_S16SYS: mDevice->FmtType = DevFmtShort; break;
144 case AUDIO_S32SYS: mDevice->FmtType = DevFmtInt; break;
145 case AUDIO_F32SYS: mDevice->FmtType = DevFmtFloat; break;
146 default:
147 throw al::backend_exception{ALC_INVALID_VALUE, "Unhandled SDL format: 0x%04x",
148 have.format};
150 mDevice->UpdateSize = have.samples;
151 mDevice->BufferSize = have.samples * 2; /* SDL always (tries to) use two periods. */
153 mFrameSize = mDevice->frameSizeFromFmt();
154 mFrequency = mDevice->Frequency;
155 mFmtChans = mDevice->FmtChans;
156 mFmtType = mDevice->FmtType;
157 mUpdateSize = mDevice->UpdateSize;
159 mDevice->DeviceName = name ? name : defaultDeviceName;
162 bool Sdl2Backend::reset()
164 mDevice->Frequency = mFrequency;
165 mDevice->FmtChans = mFmtChans;
166 mDevice->FmtType = mFmtType;
167 mDevice->UpdateSize = mUpdateSize;
168 mDevice->BufferSize = mUpdateSize * 2;
169 setDefaultWFXChannelOrder();
170 return true;
173 void Sdl2Backend::start()
174 { SDL_PauseAudioDevice(mDeviceID, 0); }
176 void Sdl2Backend::stop()
177 { SDL_PauseAudioDevice(mDeviceID, 1); }
179 } // namespace
181 BackendFactory &SDL2BackendFactory::getFactory()
183 static SDL2BackendFactory factory{};
184 return factory;
187 bool SDL2BackendFactory::init()
188 { return (SDL_InitSubSystem(SDL_INIT_AUDIO) == 0); }
190 bool SDL2BackendFactory::querySupport(BackendType type)
191 { return type == BackendType::Playback; }
193 std::string SDL2BackendFactory::probe(BackendType type)
195 std::string outnames;
197 if(type != BackendType::Playback)
198 return outnames;
200 int num_devices{SDL_GetNumAudioDevices(SDL_FALSE)};
202 /* Includes null char. */
203 outnames.append(defaultDeviceName, sizeof(defaultDeviceName));
204 for(int i{0};i < num_devices;++i)
206 std::string name{DEVNAME_PREFIX};
207 name += SDL_GetAudioDeviceName(i, SDL_FALSE);
208 if(!name.empty())
209 outnames.append(name.c_str(), name.length()+1);
211 return outnames;
214 BackendPtr SDL2BackendFactory::createBackend(ALCdevice *device, BackendType type)
216 if(type == BackendType::Playback)
217 return BackendPtr{new Sdl2Backend{device}};
218 return nullptr;