232 lines
6.8 KiB
C++
Raw Normal View History

/**
* OpenAL cross platform audio library
* Copyright (C) 2018 by authors.
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library; if not, write to the
* Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
* Or go to http://www.gnu.org/copyleft/lgpl.html
*/
#include "config.h"
2018-11-15 22:15:10 -08:00
#include "backends/sdl2.h"
#include <stdlib.h>
#include <SDL2/SDL.h>
2018-11-12 22:57:39 -08:00
#include <string>
#include "alMain.h"
#include "alu.h"
#include "threads.h"
#include "compat.h"
namespace {
2018-03-09 11:41:28 -08:00
#ifdef _WIN32
#define DEVNAME_PREFIX "OpenAL Soft on "
#else
#define DEVNAME_PREFIX ""
#endif
constexpr ALCchar defaultDeviceName[] = DEVNAME_PREFIX "Default Device";
struct Sdl2Backend final : public BackendBase {
Sdl2Backend(ALCdevice *device) noexcept : BackendBase{device} { }
~Sdl2Backend() override;
static void audioCallbackC(void *ptr, Uint8 *stream, int len);
void audioCallback(Uint8 *stream, int len);
ALCenum open(const ALCchar *name) override;
ALCboolean reset() override;
ALCboolean start() override;
void stop() override;
void lock() override;
void unlock() override;
2018-12-27 17:48:02 -08:00
SDL_AudioDeviceID mDeviceID{0u};
ALsizei mFrameSize{0};
2018-03-08 18:22:03 -08:00
2018-12-27 17:48:02 -08:00
ALuint mFrequency{0u};
DevFmtChannels mFmtChans{};
DevFmtType mFmtType{};
ALuint mUpdateSize{0u};
static constexpr inline const char *CurrentPrefix() noexcept { return "ALCsdl2Playback::"; }
DEF_NEWDEL(Sdl2Backend)
};
2018-03-08 18:12:14 -08:00
Sdl2Backend::~Sdl2Backend()
{
if(mDeviceID)
SDL_CloseAudioDevice(mDeviceID);
mDeviceID = 0;
2018-03-08 18:12:14 -08:00
}
void Sdl2Backend::audioCallbackC(void *ptr, Uint8 *stream, int len)
{ static_cast<Sdl2Backend*>(ptr)->audioCallback(stream, len); }
void Sdl2Backend::audioCallback(Uint8 *stream, int len)
{
assert((len % mFrameSize) == 0);
aluMixData(mDevice, stream, len / mFrameSize);
}
ALCenum Sdl2Backend::open(const ALCchar *name)
{
SDL_AudioSpec want{}, have{};
want.freq = mDevice->Frequency;
switch(mDevice->FmtType)
{
case DevFmtUByte: want.format = AUDIO_U8; break;
case DevFmtByte: want.format = AUDIO_S8; break;
case DevFmtUShort: want.format = AUDIO_U16SYS; break;
case DevFmtShort: want.format = AUDIO_S16SYS; break;
case DevFmtUInt: /* fall-through */
case DevFmtInt: want.format = AUDIO_S32SYS; break;
case DevFmtFloat: want.format = AUDIO_F32; break;
}
want.channels = (mDevice->FmtChans == DevFmtMono) ? 1 : 2;
want.samples = mDevice->UpdateSize;
want.callback = &Sdl2Backend::audioCallbackC;
want.userdata = this;
2018-11-12 22:57:39 -08:00
/* Passing nullptr to SDL_OpenAudioDevice opens a default, which isn't
2018-03-09 11:41:28 -08:00
* necessarily the first in the list.
*/
if(!name || strcmp(name, defaultDeviceName) == 0)
mDeviceID = SDL_OpenAudioDevice(nullptr, SDL_FALSE, &want, &have,
SDL_AUDIO_ALLOW_ANY_CHANGE);
2018-03-09 11:41:28 -08:00
else
{
const size_t prefix_len = strlen(DEVNAME_PREFIX);
if(strncmp(name, DEVNAME_PREFIX, prefix_len) == 0)
mDeviceID = SDL_OpenAudioDevice(name+prefix_len, SDL_FALSE, &want, &have,
SDL_AUDIO_ALLOW_ANY_CHANGE);
2018-03-09 11:41:28 -08:00
else
mDeviceID = SDL_OpenAudioDevice(name, SDL_FALSE, &want, &have,
SDL_AUDIO_ALLOW_ANY_CHANGE);
2018-03-09 11:41:28 -08:00
}
if(mDeviceID == 0)
2018-03-08 20:39:15 +01:00
return ALC_INVALID_VALUE;
2018-03-08 18:22:03 -08:00
mDevice->Frequency = have.freq;
if(have.channels == 1)
mDevice->FmtChans = DevFmtMono;
else if(have.channels == 2)
mDevice->FmtChans = DevFmtStereo;
else
{
2018-03-08 22:09:30 -08:00
ERR("Got unhandled SDL channel count: %d\n", (int)have.channels);
return ALC_INVALID_VALUE;
}
switch(have.format)
{
case AUDIO_U8: mDevice->FmtType = DevFmtUByte; break;
case AUDIO_S8: mDevice->FmtType = DevFmtByte; break;
case AUDIO_U16SYS: mDevice->FmtType = DevFmtUShort; break;
case AUDIO_S16SYS: mDevice->FmtType = DevFmtShort; break;
case AUDIO_S32SYS: mDevice->FmtType = DevFmtInt; break;
case AUDIO_F32SYS: mDevice->FmtType = DevFmtFloat; break;
default:
2018-03-08 22:09:30 -08:00
ERR("Got unsupported SDL format: 0x%04x\n", have.format);
return ALC_INVALID_VALUE;
}
mDevice->UpdateSize = have.samples;
mDevice->NumUpdates = 2; /* SDL always (tries to) use two periods. */
2018-03-08 18:22:03 -08:00
mFrameSize = mDevice->frameSizeFromFmt();
mFrequency = mDevice->Frequency;
mFmtChans = mDevice->FmtChans;
mFmtType = mDevice->FmtType;
mUpdateSize = mDevice->UpdateSize;
2018-03-08 18:22:03 -08:00
mDevice->DeviceName = name ? name : defaultDeviceName;
return ALC_NO_ERROR;
}
ALCboolean Sdl2Backend::reset()
{
mDevice->Frequency = mFrequency;
mDevice->FmtChans = mFmtChans;
mDevice->FmtType = mFmtType;
mDevice->UpdateSize = mUpdateSize;
mDevice->NumUpdates = 2;
SetDefaultWFXChannelOrder(mDevice);
return ALC_TRUE;
}
ALCboolean Sdl2Backend::start()
{
SDL_PauseAudioDevice(mDeviceID, 0);
return ALC_TRUE;
}
void Sdl2Backend::stop()
{ SDL_PauseAudioDevice(mDeviceID, 1); }
void Sdl2Backend::lock()
{ SDL_LockAudioDevice(mDeviceID); }
void Sdl2Backend::unlock()
{ SDL_UnlockAudioDevice(mDeviceID); }
} // namespace
2018-11-15 22:15:10 -08:00
BackendFactory &SDL2BackendFactory::getFactory()
{
2018-11-15 22:15:10 -08:00
static SDL2BackendFactory factory{};
return factory;
}
2018-11-15 22:15:10 -08:00
bool SDL2BackendFactory::init()
{
2018-11-15 22:15:10 -08:00
return (SDL_InitSubSystem(SDL_INIT_AUDIO) == 0);
}
2018-11-15 22:15:10 -08:00
void SDL2BackendFactory::deinit()
{
SDL_QuitSubSystem(SDL_INIT_AUDIO);
}
2018-12-29 01:38:26 -08:00
bool SDL2BackendFactory::querySupport(BackendType type)
{ return type == BackendType::Playback; }
void SDL2BackendFactory::probe(DevProbe type, std::string *outnames)
{
if(type != ALL_DEVICE_PROBE)
return;
2018-11-12 22:57:39 -08:00
int num_devices{SDL_GetNumAudioDevices(SDL_FALSE)};
/* Includes null char. */
outnames->append(defaultDeviceName, sizeof(defaultDeviceName));
2018-11-12 22:57:39 -08:00
for(int i{0};i < num_devices;++i)
2018-03-09 11:41:28 -08:00
{
2018-11-12 22:57:39 -08:00
std::string name{DEVNAME_PREFIX};
name += SDL_GetAudioDeviceName(i, SDL_FALSE);
if(!name.empty())
outnames->append(name.c_str(), name.length()+1);
2018-03-09 11:41:28 -08:00
}
}
2018-12-29 02:16:16 -08:00
BackendPtr SDL2BackendFactory::createBackend(ALCdevice *device, BackendType type)
{
2018-12-29 01:38:26 -08:00
if(type == BackendType::Playback)
2018-12-29 02:16:16 -08:00
return BackendPtr{new Sdl2Backend{device}};
2018-11-12 22:57:39 -08:00
return nullptr;
}