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
31 #include "alnumeric.h"
32 #include "core/device.h"
33 #include "core/logging.h"
41 #define DEVNAME_PREFIX "OpenAL Soft on "
43 #define DEVNAME_PREFIX ""
46 constexpr char defaultDeviceName
[] = DEVNAME_PREFIX
"Default Device";
48 struct Sdl2Backend final
: public BackendBase
{
49 Sdl2Backend(DeviceBase
*device
) noexcept
: BackendBase
{device
} { }
50 ~Sdl2Backend() override
;
52 void audioCallback(Uint8
*stream
, int len
) noexcept
;
53 static void audioCallbackC(void *ptr
, Uint8
*stream
, int len
) noexcept
54 { static_cast<Sdl2Backend
*>(ptr
)->audioCallback(stream
, len
); }
56 void open(const char *name
) override
;
57 bool reset() override
;
58 void start() override
;
61 SDL_AudioDeviceID mDeviceID
{0u};
65 DevFmtChannels mFmtChans
{};
66 DevFmtType mFmtType
{};
69 DEF_NEWDEL(Sdl2Backend
)
72 Sdl2Backend::~Sdl2Backend()
75 SDL_CloseAudioDevice(mDeviceID
);
79 void Sdl2Backend::audioCallback(Uint8
*stream
, int len
) noexcept
81 const auto ulen
= static_cast<unsigned int>(len
);
82 assert((ulen
% mFrameSize
) == 0);
83 mDevice
->renderSamples(stream
, ulen
/ mFrameSize
, mDevice
->channelsFromFmt());
86 void Sdl2Backend::open(const char *name
)
88 SDL_AudioSpec want
{}, have
{};
90 want
.freq
= static_cast<int>(mDevice
->Frequency
);
91 switch(mDevice
->FmtType
)
93 case DevFmtUByte
: want
.format
= AUDIO_U8
; break;
94 case DevFmtByte
: want
.format
= AUDIO_S8
; break;
95 case DevFmtUShort
: want
.format
= AUDIO_U16SYS
; break;
96 case DevFmtShort
: want
.format
= AUDIO_S16SYS
; break;
97 case DevFmtUInt
: /* fall-through */
98 case DevFmtInt
: want
.format
= AUDIO_S32SYS
; break;
99 case DevFmtFloat
: want
.format
= AUDIO_F32
; break;
101 want
.channels
= (mDevice
->FmtChans
== DevFmtMono
) ? 1 : 2;
102 want
.samples
= static_cast<Uint16
>(minu(mDevice
->UpdateSize
, 8192));
103 want
.callback
= &Sdl2Backend::audioCallbackC
;
104 want
.userdata
= this;
106 /* Passing nullptr to SDL_OpenAudioDevice opens a default, which isn't
107 * necessarily the first in the list.
109 SDL_AudioDeviceID devid
;
110 if(!name
|| strcmp(name
, defaultDeviceName
) == 0)
111 devid
= SDL_OpenAudioDevice(nullptr, SDL_FALSE
, &want
, &have
, SDL_AUDIO_ALLOW_ANY_CHANGE
);
114 const size_t prefix_len
= strlen(DEVNAME_PREFIX
);
115 if(strncmp(name
, DEVNAME_PREFIX
, prefix_len
) == 0)
116 devid
= SDL_OpenAudioDevice(name
+prefix_len
, SDL_FALSE
, &want
, &have
,
117 SDL_AUDIO_ALLOW_ANY_CHANGE
);
119 devid
= SDL_OpenAudioDevice(name
, SDL_FALSE
, &want
, &have
, SDL_AUDIO_ALLOW_ANY_CHANGE
);
122 throw al::backend_exception
{al::backend_error::NoDevice
, "%s", SDL_GetError()};
124 DevFmtChannels devchans
{};
125 if(have
.channels
>= 2)
126 devchans
= DevFmtStereo
;
127 else if(have
.channels
== 1)
128 devchans
= DevFmtMono
;
131 SDL_CloseAudioDevice(devid
);
132 throw al::backend_exception
{al::backend_error::DeviceError
,
133 "Unhandled SDL channel count: %d", int{have
.channels
}};
136 DevFmtType devtype
{};
139 case AUDIO_U8
: devtype
= DevFmtUByte
; break;
140 case AUDIO_S8
: devtype
= DevFmtByte
; break;
141 case AUDIO_U16SYS
: devtype
= DevFmtUShort
; break;
142 case AUDIO_S16SYS
: devtype
= DevFmtShort
; break;
143 case AUDIO_S32SYS
: devtype
= DevFmtInt
; break;
144 case AUDIO_F32SYS
: devtype
= DevFmtFloat
; break;
146 SDL_CloseAudioDevice(devid
);
147 throw al::backend_exception
{al::backend_error::DeviceError
, "Unhandled SDL format: 0x%04x",
152 SDL_CloseAudioDevice(mDeviceID
);
155 mFrameSize
= BytesFromDevFmt(devtype
) * have
.channels
;
156 mFrequency
= static_cast<uint
>(have
.freq
);
157 mFmtChans
= devchans
;
159 mUpdateSize
= have
.samples
;
161 mDevice
->DeviceName
= name
? name
: defaultDeviceName
;
164 bool Sdl2Backend::reset()
166 mDevice
->Frequency
= mFrequency
;
167 mDevice
->FmtChans
= mFmtChans
;
168 mDevice
->FmtType
= mFmtType
;
169 mDevice
->UpdateSize
= mUpdateSize
;
170 mDevice
->BufferSize
= mUpdateSize
* 2; /* SDL always (tries to) use two periods. */
171 setDefaultWFXChannelOrder();
175 void Sdl2Backend::start()
176 { SDL_PauseAudioDevice(mDeviceID
, 0); }
178 void Sdl2Backend::stop()
179 { SDL_PauseAudioDevice(mDeviceID
, 1); }
183 BackendFactory
&SDL2BackendFactory::getFactory()
185 static SDL2BackendFactory factory
{};
189 bool SDL2BackendFactory::init()
190 { return (SDL_InitSubSystem(SDL_INIT_AUDIO
) == 0); }
192 bool SDL2BackendFactory::querySupport(BackendType type
)
193 { return type
== BackendType::Playback
; }
195 std::string
SDL2BackendFactory::probe(BackendType type
)
197 std::string outnames
;
199 if(type
!= BackendType::Playback
)
202 int num_devices
{SDL_GetNumAudioDevices(SDL_FALSE
)};
204 /* Includes null char. */
205 outnames
.append(defaultDeviceName
, sizeof(defaultDeviceName
));
206 for(int i
{0};i
< num_devices
;++i
)
208 std::string name
{DEVNAME_PREFIX
};
209 name
+= SDL_GetAudioDeviceName(i
, SDL_FALSE
);
211 outnames
.append(name
.c_str(), name
.length()+1);
216 BackendPtr
SDL2BackendFactory::createBackend(DeviceBase
*device
, BackendType type
)
218 if(type
== BackendType::Playback
)
219 return BackendPtr
{new Sdl2Backend
{device
}};