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
23 #include "backends/sdl2.h"
43 #define DEVNAME_PREFIX "OpenAL Soft on "
45 #define DEVNAME_PREFIX ""
48 constexpr ALCchar defaultDeviceName
[] = DEVNAME_PREFIX
"Default Device";
50 struct Sdl2Backend final
: public BackendBase
{
51 Sdl2Backend(ALCdevice
*device
) noexcept
: BackendBase
{device
} { }
52 ~Sdl2Backend() override
;
54 static void audioCallbackC(void *ptr
, Uint8
*stream
, int len
);
55 void audioCallback(Uint8
*stream
, int len
);
57 ALCenum
open(const ALCchar
*name
) override
;
58 bool reset() override
;
59 bool start() override
;
62 void unlock() override
;
64 SDL_AudioDeviceID mDeviceID
{0u};
67 ALuint mFrequency
{0u};
68 DevFmtChannels mFmtChans
{};
69 DevFmtType mFmtType
{};
70 ALuint mUpdateSize
{0u};
72 DEF_NEWDEL(Sdl2Backend
)
75 Sdl2Backend::~Sdl2Backend()
78 SDL_CloseAudioDevice(mDeviceID
);
82 void Sdl2Backend::audioCallbackC(void *ptr
, Uint8
*stream
, int len
)
83 { static_cast<Sdl2Backend
*>(ptr
)->audioCallback(stream
, len
); }
85 void Sdl2Backend::audioCallback(Uint8
*stream
, int len
)
87 const auto ulen
= static_cast<unsigned int>(len
);
88 assert((ulen
% mFrameSize
) == 0);
89 aluMixData(mDevice
, stream
, ulen
/ mFrameSize
);
92 ALCenum
Sdl2Backend::open(const ALCchar
*name
)
94 SDL_AudioSpec want
{}, have
{};
96 want
.freq
= static_cast<int>(mDevice
->Frequency
);
97 switch(mDevice
->FmtType
)
99 case DevFmtUByte
: want
.format
= AUDIO_U8
; break;
100 case DevFmtByte
: want
.format
= AUDIO_S8
; break;
101 case DevFmtUShort
: want
.format
= AUDIO_U16SYS
; break;
102 case DevFmtShort
: want
.format
= AUDIO_S16SYS
; break;
103 case DevFmtUInt
: /* fall-through */
104 case DevFmtInt
: want
.format
= AUDIO_S32SYS
; break;
105 case DevFmtFloat
: want
.format
= AUDIO_F32
; break;
107 want
.channels
= (mDevice
->FmtChans
== DevFmtMono
) ? 1 : 2;
108 want
.samples
= static_cast<Uint16
>(mDevice
->UpdateSize
);
109 want
.callback
= &Sdl2Backend::audioCallbackC
;
110 want
.userdata
= this;
112 /* Passing nullptr to SDL_OpenAudioDevice opens a default, which isn't
113 * necessarily the first in the list.
115 if(!name
|| strcmp(name
, defaultDeviceName
) == 0)
116 mDeviceID
= SDL_OpenAudioDevice(nullptr, SDL_FALSE
, &want
, &have
,
117 SDL_AUDIO_ALLOW_ANY_CHANGE
);
120 const size_t prefix_len
= strlen(DEVNAME_PREFIX
);
121 if(strncmp(name
, DEVNAME_PREFIX
, prefix_len
) == 0)
122 mDeviceID
= SDL_OpenAudioDevice(name
+prefix_len
, SDL_FALSE
, &want
, &have
,
123 SDL_AUDIO_ALLOW_ANY_CHANGE
);
125 mDeviceID
= SDL_OpenAudioDevice(name
, SDL_FALSE
, &want
, &have
,
126 SDL_AUDIO_ALLOW_ANY_CHANGE
);
129 return ALC_INVALID_VALUE
;
131 mDevice
->Frequency
= static_cast<ALuint
>(have
.freq
);
132 if(have
.channels
== 1)
133 mDevice
->FmtChans
= DevFmtMono
;
134 else if(have
.channels
== 2)
135 mDevice
->FmtChans
= DevFmtStereo
;
138 ERR("Got unhandled SDL channel count: %d\n", int{have
.channels
});
139 return ALC_INVALID_VALUE
;
143 case AUDIO_U8
: mDevice
->FmtType
= DevFmtUByte
; break;
144 case AUDIO_S8
: mDevice
->FmtType
= DevFmtByte
; break;
145 case AUDIO_U16SYS
: mDevice
->FmtType
= DevFmtUShort
; break;
146 case AUDIO_S16SYS
: mDevice
->FmtType
= DevFmtShort
; break;
147 case AUDIO_S32SYS
: mDevice
->FmtType
= DevFmtInt
; break;
148 case AUDIO_F32SYS
: mDevice
->FmtType
= DevFmtFloat
; break;
150 ERR("Got unsupported SDL format: 0x%04x\n", have
.format
);
151 return ALC_INVALID_VALUE
;
153 mDevice
->UpdateSize
= have
.samples
;
154 mDevice
->BufferSize
= have
.samples
* 2; /* SDL always (tries to) use two periods. */
156 mFrameSize
= mDevice
->frameSizeFromFmt();
157 mFrequency
= mDevice
->Frequency
;
158 mFmtChans
= mDevice
->FmtChans
;
159 mFmtType
= mDevice
->FmtType
;
160 mUpdateSize
= mDevice
->UpdateSize
;
162 mDevice
->DeviceName
= name
? name
: defaultDeviceName
;
166 bool Sdl2Backend::reset()
168 mDevice
->Frequency
= mFrequency
;
169 mDevice
->FmtChans
= mFmtChans
;
170 mDevice
->FmtType
= mFmtType
;
171 mDevice
->UpdateSize
= mUpdateSize
;
172 mDevice
->BufferSize
= mUpdateSize
* 2;
173 SetDefaultWFXChannelOrder(mDevice
);
177 bool Sdl2Backend::start()
179 SDL_PauseAudioDevice(mDeviceID
, 0);
183 void Sdl2Backend::stop()
184 { SDL_PauseAudioDevice(mDeviceID
, 1); }
186 void Sdl2Backend::lock()
187 { SDL_LockAudioDevice(mDeviceID
); }
189 void Sdl2Backend::unlock()
190 { SDL_UnlockAudioDevice(mDeviceID
); }
194 BackendFactory
&SDL2BackendFactory::getFactory()
196 static SDL2BackendFactory factory
{};
200 bool SDL2BackendFactory::init()
201 { return (SDL_InitSubSystem(SDL_INIT_AUDIO
) == 0); }
203 bool SDL2BackendFactory::querySupport(BackendType type
)
204 { return type
== BackendType::Playback
; }
206 void SDL2BackendFactory::probe(DevProbe type
, std::string
*outnames
)
208 if(type
!= DevProbe::Playback
)
211 int num_devices
{SDL_GetNumAudioDevices(SDL_FALSE
)};
213 /* Includes null char. */
214 outnames
->append(defaultDeviceName
, sizeof(defaultDeviceName
));
215 for(int i
{0};i
< num_devices
;++i
)
217 std::string name
{DEVNAME_PREFIX
};
218 name
+= SDL_GetAudioDeviceName(i
, SDL_FALSE
);
220 outnames
->append(name
.c_str(), name
.length()+1);
224 BackendPtr
SDL2BackendFactory::createBackend(ALCdevice
*device
, BackendType type
)
226 if(type
== BackendType::Playback
)
227 return BackendPtr
{new Sdl2Backend
{device
}};