2018-03-07 20:53:56 +01:00
|
|
|
/**
|
|
|
|
* 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"
|
|
|
|
|
2018-03-07 20:53:56 +01:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <SDL2/SDL.h>
|
|
|
|
|
2018-11-12 22:57:39 -08:00
|
|
|
#include <string>
|
|
|
|
|
2018-03-07 20:53:56 +01:00
|
|
|
#include "alMain.h"
|
|
|
|
#include "alu.h"
|
|
|
|
#include "threads.h"
|
|
|
|
#include "compat.h"
|
|
|
|
|
|
|
|
|
2018-03-09 11:41:28 -08:00
|
|
|
#ifdef _WIN32
|
|
|
|
#define DEVNAME_PREFIX "OpenAL Soft on "
|
|
|
|
#else
|
|
|
|
#define DEVNAME_PREFIX ""
|
|
|
|
#endif
|
|
|
|
|
2018-11-12 22:57:39 -08:00
|
|
|
struct ALCsdl2Backend final : public ALCbackend {
|
2018-03-07 20:53:56 +01:00
|
|
|
SDL_AudioDeviceID deviceID;
|
2018-03-08 22:23:36 -08:00
|
|
|
ALsizei frameSize;
|
2018-03-08 18:22:03 -08:00
|
|
|
|
2018-03-08 20:39:15 +01:00
|
|
|
ALuint Frequency;
|
|
|
|
enum DevFmtChannels FmtChans;
|
|
|
|
enum DevFmtType FmtType;
|
2018-03-08 18:22:03 -08:00
|
|
|
ALuint UpdateSize;
|
2018-11-12 22:57:39 -08:00
|
|
|
};
|
2018-03-07 20:53:56 +01:00
|
|
|
|
|
|
|
static void ALCsdl2Backend_Construct(ALCsdl2Backend *self, ALCdevice *device);
|
2018-03-08 18:12:14 -08:00
|
|
|
static void ALCsdl2Backend_Destruct(ALCsdl2Backend *self);
|
2018-03-07 20:53:56 +01:00
|
|
|
static ALCenum ALCsdl2Backend_open(ALCsdl2Backend *self, const ALCchar *name);
|
|
|
|
static ALCboolean ALCsdl2Backend_reset(ALCsdl2Backend *self);
|
|
|
|
static ALCboolean ALCsdl2Backend_start(ALCsdl2Backend *self);
|
|
|
|
static void ALCsdl2Backend_stop(ALCsdl2Backend *self);
|
|
|
|
static DECLARE_FORWARD2(ALCsdl2Backend, ALCbackend, ALCenum, captureSamples, void*, ALCuint)
|
|
|
|
static DECLARE_FORWARD(ALCsdl2Backend, ALCbackend, ALCuint, availableSamples)
|
|
|
|
static DECLARE_FORWARD(ALCsdl2Backend, ALCbackend, ClockLatency, getClockLatency)
|
2018-03-08 18:16:10 -08:00
|
|
|
static void ALCsdl2Backend_lock(ALCsdl2Backend *self);
|
|
|
|
static void ALCsdl2Backend_unlock(ALCsdl2Backend *self);
|
2018-03-07 20:53:56 +01:00
|
|
|
DECLARE_DEFAULT_ALLOCATORS(ALCsdl2Backend)
|
|
|
|
|
|
|
|
DEFINE_ALCBACKEND_VTABLE(ALCsdl2Backend);
|
|
|
|
|
2018-03-09 11:41:28 -08:00
|
|
|
static const ALCchar defaultDeviceName[] = DEVNAME_PREFIX "Default Device";
|
2018-03-08 10:07:22 +01:00
|
|
|
|
2018-03-07 20:53:56 +01:00
|
|
|
static void ALCsdl2Backend_Construct(ALCsdl2Backend *self, ALCdevice *device)
|
|
|
|
{
|
2018-11-12 22:57:39 -08:00
|
|
|
new (self) ALCsdl2Backend{};
|
2018-03-07 20:53:56 +01:00
|
|
|
ALCbackend_Construct(STATIC_CAST(ALCbackend, self), device);
|
|
|
|
SET_VTABLE2(ALCsdl2Backend, ALCbackend, self);
|
|
|
|
|
2018-03-08 18:12:14 -08:00
|
|
|
self->deviceID = 0;
|
2018-11-18 08:01:50 -08:00
|
|
|
self->frameSize = FrameSizeFromDevFmt(device->FmtChans, device->FmtType, device->mAmbiOrder);
|
2018-03-08 20:39:15 +01:00
|
|
|
self->Frequency = device->Frequency;
|
|
|
|
self->FmtChans = device->FmtChans;
|
|
|
|
self->FmtType = device->FmtType;
|
2018-03-08 18:59:22 -08:00
|
|
|
self->UpdateSize = device->UpdateSize;
|
2018-03-07 20:53:56 +01:00
|
|
|
}
|
|
|
|
|
2018-03-08 18:12:14 -08:00
|
|
|
static void ALCsdl2Backend_Destruct(ALCsdl2Backend *self)
|
|
|
|
{
|
|
|
|
if(self->deviceID)
|
|
|
|
SDL_CloseAudioDevice(self->deviceID);
|
|
|
|
self->deviceID = 0;
|
|
|
|
|
|
|
|
ALCbackend_Destruct(STATIC_CAST(ALCbackend, self));
|
2018-11-12 22:57:39 -08:00
|
|
|
self->~ALCsdl2Backend();
|
2018-03-08 18:12:14 -08:00
|
|
|
}
|
|
|
|
|
2018-03-07 20:53:56 +01:00
|
|
|
|
2018-03-08 22:23:36 -08:00
|
|
|
static void ALCsdl2Backend_audioCallback(void *ptr, Uint8 *stream, int len)
|
2018-03-07 20:53:56 +01:00
|
|
|
{
|
2018-11-12 22:57:39 -08:00
|
|
|
ALCsdl2Backend *self = static_cast<ALCsdl2Backend*>(ptr);
|
2018-03-07 20:53:56 +01:00
|
|
|
ALCdevice *device = STATIC_CAST(ALCbackend, self)->mDevice;
|
2018-03-08 18:16:10 -08:00
|
|
|
|
2018-03-08 22:23:36 -08:00
|
|
|
assert((len % self->frameSize) == 0);
|
|
|
|
aluMixData(device, stream, len / self->frameSize);
|
2018-03-07 20:53:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static ALCenum ALCsdl2Backend_open(ALCsdl2Backend *self, const ALCchar *name)
|
|
|
|
{
|
|
|
|
ALCdevice *device = STATIC_CAST(ALCbackend, self)->mDevice;
|
2018-03-08 10:17:02 +01:00
|
|
|
SDL_AudioSpec want, have;
|
2018-03-09 10:53:03 -08:00
|
|
|
|
2018-03-07 20:53:56 +01:00
|
|
|
SDL_zero(want);
|
2018-03-09 10:53:03 -08:00
|
|
|
SDL_zero(have);
|
|
|
|
|
2018-03-07 20:53:56 +01:00
|
|
|
want.freq = device->Frequency;
|
2018-03-09 10:53:03 -08:00
|
|
|
switch(device->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;
|
|
|
|
}
|
2018-03-08 20:39:15 +01:00
|
|
|
want.channels = (device->FmtChans == DevFmtMono) ? 1 : 2;
|
2018-03-07 20:53:56 +01:00
|
|
|
want.samples = device->UpdateSize;
|
2018-03-08 09:50:16 +01:00
|
|
|
want.callback = ALCsdl2Backend_audioCallback;
|
|
|
|
want.userdata = self;
|
2018-03-07 20:53:56 +01:00
|
|
|
|
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)
|
2018-11-12 22:57:39 -08:00
|
|
|
self->deviceID = SDL_OpenAudioDevice(nullptr, SDL_FALSE, &want, &have,
|
2018-03-09 11:41:28 -08:00
|
|
|
SDL_AUDIO_ALLOW_ANY_CHANGE);
|
|
|
|
else
|
|
|
|
{
|
|
|
|
const size_t prefix_len = strlen(DEVNAME_PREFIX);
|
|
|
|
if(strncmp(name, DEVNAME_PREFIX, prefix_len) == 0)
|
|
|
|
self->deviceID = SDL_OpenAudioDevice(name+prefix_len, SDL_FALSE, &want, &have,
|
|
|
|
SDL_AUDIO_ALLOW_ANY_CHANGE);
|
|
|
|
else
|
|
|
|
self->deviceID = SDL_OpenAudioDevice(name, SDL_FALSE, &want, &have,
|
|
|
|
SDL_AUDIO_ALLOW_ANY_CHANGE);
|
|
|
|
}
|
2018-03-08 20:39:15 +01:00
|
|
|
if(self->deviceID == 0)
|
|
|
|
return ALC_INVALID_VALUE;
|
2018-03-08 18:22:03 -08:00
|
|
|
|
|
|
|
device->Frequency = have.freq;
|
2018-03-08 20:26:31 +01:00
|
|
|
if(have.channels == 1)
|
|
|
|
device->FmtChans = DevFmtMono;
|
|
|
|
else if(have.channels == 2)
|
|
|
|
device->FmtChans = DevFmtStereo;
|
|
|
|
else
|
|
|
|
{
|
2018-03-08 22:09:30 -08:00
|
|
|
ERR("Got unhandled SDL channel count: %d\n", (int)have.channels);
|
2018-03-08 20:26:31 +01:00
|
|
|
return ALC_INVALID_VALUE;
|
|
|
|
}
|
|
|
|
switch(have.format)
|
|
|
|
{
|
|
|
|
case AUDIO_U8: device->FmtType = DevFmtUByte; break;
|
|
|
|
case AUDIO_S8: device->FmtType = DevFmtByte; break;
|
|
|
|
case AUDIO_U16SYS: device->FmtType = DevFmtUShort; break;
|
|
|
|
case AUDIO_S16SYS: device->FmtType = DevFmtShort; break;
|
|
|
|
case AUDIO_S32SYS: device->FmtType = DevFmtInt; break;
|
|
|
|
case AUDIO_F32SYS: device->FmtType = DevFmtFloat; break;
|
|
|
|
default:
|
2018-03-08 22:09:30 -08:00
|
|
|
ERR("Got unsupported SDL format: 0x%04x\n", have.format);
|
2018-03-08 20:26:31 +01:00
|
|
|
return ALC_INVALID_VALUE;
|
|
|
|
}
|
2018-03-08 18:22:03 -08:00
|
|
|
device->UpdateSize = have.samples;
|
|
|
|
device->NumUpdates = 2; /* SDL always (tries to) use two periods. */
|
|
|
|
|
2018-11-18 08:01:50 -08:00
|
|
|
self->frameSize = FrameSizeFromDevFmt(device->FmtChans, device->FmtType, device->mAmbiOrder);
|
2018-03-08 20:39:15 +01:00
|
|
|
self->Frequency = device->Frequency;
|
|
|
|
self->FmtChans = device->FmtChans;
|
|
|
|
self->FmtType = device->FmtType;
|
2018-03-08 18:22:03 -08:00
|
|
|
self->UpdateSize = device->UpdateSize;
|
|
|
|
|
2018-11-18 18:45:45 -08:00
|
|
|
device->DeviceName = name ? name : defaultDeviceName;
|
2018-03-07 20:53:56 +01:00
|
|
|
return ALC_NO_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
static ALCboolean ALCsdl2Backend_reset(ALCsdl2Backend *self)
|
|
|
|
{
|
2018-03-08 20:39:15 +01:00
|
|
|
ALCdevice *device = STATIC_CAST(ALCbackend, self)->mDevice;
|
|
|
|
device->Frequency = self->Frequency;
|
|
|
|
device->FmtChans = self->FmtChans;
|
|
|
|
device->FmtType = self->FmtType;
|
2018-03-08 18:22:03 -08:00
|
|
|
device->UpdateSize = self->UpdateSize;
|
|
|
|
device->NumUpdates = 2;
|
2018-03-08 20:39:15 +01:00
|
|
|
SetDefaultWFXChannelOrder(device);
|
2018-03-07 20:53:56 +01:00
|
|
|
return ALC_TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static ALCboolean ALCsdl2Backend_start(ALCsdl2Backend *self)
|
|
|
|
{
|
|
|
|
SDL_PauseAudioDevice(self->deviceID, 0);
|
|
|
|
return ALC_TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void ALCsdl2Backend_stop(ALCsdl2Backend *self)
|
|
|
|
{
|
2018-03-08 09:50:16 +01:00
|
|
|
SDL_PauseAudioDevice(self->deviceID, 1);
|
2018-03-07 20:53:56 +01:00
|
|
|
}
|
|
|
|
|
2018-03-08 18:16:10 -08:00
|
|
|
static void ALCsdl2Backend_lock(ALCsdl2Backend *self)
|
|
|
|
{
|
|
|
|
SDL_LockAudioDevice(self->deviceID);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void ALCsdl2Backend_unlock(ALCsdl2Backend *self)
|
|
|
|
{
|
|
|
|
SDL_UnlockAudioDevice(self->deviceID);
|
|
|
|
}
|
|
|
|
|
2018-03-07 20:53:56 +01:00
|
|
|
|
2018-11-15 22:15:10 -08:00
|
|
|
BackendFactory &SDL2BackendFactory::getFactory()
|
2018-03-07 20:53:56 +01:00
|
|
|
{
|
2018-11-15 22:15:10 -08:00
|
|
|
static SDL2BackendFactory factory{};
|
|
|
|
return factory;
|
2018-03-07 20:53:56 +01:00
|
|
|
}
|
|
|
|
|
2018-11-15 22:15:10 -08:00
|
|
|
bool SDL2BackendFactory::init()
|
2018-03-07 20:53:56 +01:00
|
|
|
{
|
2018-11-15 22:15:10 -08:00
|
|
|
return (SDL_InitSubSystem(SDL_INIT_AUDIO) == 0);
|
2018-03-08 18:59:22 -08:00
|
|
|
}
|
|
|
|
|
2018-11-15 22:15:10 -08:00
|
|
|
void SDL2BackendFactory::deinit()
|
2018-03-08 18:59:22 -08:00
|
|
|
{
|
|
|
|
SDL_QuitSubSystem(SDL_INIT_AUDIO);
|
2018-03-07 20:53:56 +01:00
|
|
|
}
|
|
|
|
|
2018-11-15 22:15:10 -08:00
|
|
|
bool SDL2BackendFactory::querySupport(ALCbackend_Type type)
|
2018-03-07 20:53:56 +01:00
|
|
|
{
|
2018-11-15 22:15:10 -08:00
|
|
|
return (type == ALCbackend_Playback);
|
2018-03-07 20:53:56 +01:00
|
|
|
}
|
|
|
|
|
2018-11-15 22:15:10 -08:00
|
|
|
void SDL2BackendFactory::probe(enum DevProbe type, std::string *outnames)
|
2018-03-07 20:53:56 +01:00
|
|
|
{
|
|
|
|
if(type != ALL_DEVICE_PROBE)
|
|
|
|
return;
|
2018-03-08 18:07:49 -08:00
|
|
|
|
2018-11-12 22:57:39 -08:00
|
|
|
int num_devices{SDL_GetNumAudioDevices(SDL_FALSE)};
|
2018-03-08 18:07:49 -08:00
|
|
|
|
2018-11-15 04:24:33 -08:00
|
|
|
/* 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())
|
2018-11-15 04:24:33 -08:00
|
|
|
outnames->append(name.c_str(), name.length()+1);
|
2018-03-09 11:41:28 -08:00
|
|
|
}
|
2018-03-07 20:53:56 +01:00
|
|
|
}
|
|
|
|
|
2018-11-15 22:15:10 -08:00
|
|
|
ALCbackend *SDL2BackendFactory::createBackend(ALCdevice *device, ALCbackend_Type type)
|
2018-03-07 20:53:56 +01:00
|
|
|
{
|
|
|
|
if(type == ALCbackend_Playback)
|
|
|
|
{
|
|
|
|
ALCsdl2Backend *backend;
|
|
|
|
NEW_OBJ(backend, ALCsdl2Backend)(device);
|
2018-11-12 22:57:39 -08:00
|
|
|
if(!backend) return nullptr;
|
2018-03-07 20:53:56 +01:00
|
|
|
return STATIC_CAST(ALCbackend, backend);
|
|
|
|
}
|
|
|
|
|
2018-11-12 22:57:39 -08:00
|
|
|
return nullptr;
|
2018-03-07 20:53:56 +01:00
|
|
|
}
|