rlm@0: /** rlm@0: * OpenAL cross platform audio library rlm@0: * Copyright (C) 1999-2007 by authors. rlm@0: * This library is free software; you can redistribute it and/or rlm@0: * modify it under the terms of the GNU Library General Public rlm@0: * License as published by the Free Software Foundation; either rlm@0: * version 2 of the License, or (at your option) any later version. rlm@0: * rlm@0: * This library is distributed in the hope that it will be useful, rlm@0: * but WITHOUT ANY WARRANTY; without even the implied warranty of rlm@0: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU rlm@0: * Library General Public License for more details. rlm@0: * rlm@0: * You should have received a copy of the GNU Library General Public rlm@0: * License along with this library; if not, write to the rlm@0: * Free Software Foundation, Inc., 59 Temple Place - Suite 330, rlm@0: * Boston, MA 02111-1307, USA. rlm@0: * Or go to http://www.gnu.org/copyleft/lgpl.html rlm@0: */ rlm@0: rlm@0: #include "config.h" rlm@0: rlm@0: #include rlm@0: #include rlm@0: #include rlm@0: #include "alMain.h" rlm@0: #include "AL/al.h" rlm@0: #include "AL/alc.h" rlm@0: rlm@0: #include rlm@0: rlm@0: rlm@0: static const ALCchar sndio_device[] = "SndIO Default"; rlm@0: rlm@0: rlm@0: static void *sndio_handle; rlm@0: #ifdef HAVE_DYNLOAD rlm@0: #define MAKE_FUNC(x) static typeof(x) * p##x rlm@0: MAKE_FUNC(sio_initpar); rlm@0: MAKE_FUNC(sio_open); rlm@0: MAKE_FUNC(sio_close); rlm@0: MAKE_FUNC(sio_setpar); rlm@0: MAKE_FUNC(sio_getpar); rlm@0: MAKE_FUNC(sio_getcap); rlm@0: MAKE_FUNC(sio_onmove); rlm@0: MAKE_FUNC(sio_write); rlm@0: MAKE_FUNC(sio_read); rlm@0: MAKE_FUNC(sio_start); rlm@0: MAKE_FUNC(sio_stop); rlm@0: MAKE_FUNC(sio_nfds); rlm@0: MAKE_FUNC(sio_pollfd); rlm@0: MAKE_FUNC(sio_revents); rlm@0: MAKE_FUNC(sio_eof); rlm@0: MAKE_FUNC(sio_setvol); rlm@0: MAKE_FUNC(sio_onvol); rlm@0: rlm@0: #define sio_initpar psio_initpar rlm@0: #define sio_open psio_open rlm@0: #define sio_close psio_close rlm@0: #define sio_setpar psio_setpar rlm@0: #define sio_getpar psio_getpar rlm@0: #define sio_getcap psio_getcap rlm@0: #define sio_onmove psio_onmove rlm@0: #define sio_write psio_write rlm@0: #define sio_read psio_read rlm@0: #define sio_start psio_start rlm@0: #define sio_stop psio_stop rlm@0: #define sio_nfds psio_nfds rlm@0: #define sio_pollfd psio_pollfd rlm@0: #define sio_revents psio_revents rlm@0: #define sio_eof psio_eof rlm@0: #define sio_setvol psio_setvol rlm@0: #define sio_onvol psio_onvol rlm@0: #endif rlm@0: rlm@0: rlm@0: static ALCboolean sndio_load(void) rlm@0: { rlm@0: if(!sndio_handle) rlm@0: { rlm@0: #ifdef HAVE_DYNLOAD rlm@0: sndio_handle = LoadLib("libsndio.so"); rlm@0: if(!sndio_handle) rlm@0: return ALC_FALSE; rlm@0: rlm@0: #define LOAD_FUNC(f) do { \ rlm@0: p##f = GetSymbol(sndio_handle, #f); \ rlm@0: if(p##f == NULL) { \ rlm@0: CloseLib(sndio_handle); \ rlm@0: sndio_handle = NULL; \ rlm@0: return ALC_FALSE; \ rlm@0: } \ rlm@0: } while(0) rlm@0: LOAD_FUNC(sio_initpar); rlm@0: LOAD_FUNC(sio_open); rlm@0: LOAD_FUNC(sio_close); rlm@0: LOAD_FUNC(sio_setpar); rlm@0: LOAD_FUNC(sio_getpar); rlm@0: LOAD_FUNC(sio_getcap); rlm@0: LOAD_FUNC(sio_onmove); rlm@0: LOAD_FUNC(sio_write); rlm@0: LOAD_FUNC(sio_read); rlm@0: LOAD_FUNC(sio_start); rlm@0: LOAD_FUNC(sio_stop); rlm@0: LOAD_FUNC(sio_nfds); rlm@0: LOAD_FUNC(sio_pollfd); rlm@0: LOAD_FUNC(sio_revents); rlm@0: LOAD_FUNC(sio_eof); rlm@0: LOAD_FUNC(sio_setvol); rlm@0: LOAD_FUNC(sio_onvol); rlm@0: #undef LOAD_FUNC rlm@0: #else rlm@0: sndio_handle = (void*)0xDEADBEEF; rlm@0: #endif rlm@0: } rlm@0: return ALC_TRUE; rlm@0: } rlm@0: rlm@0: rlm@0: typedef struct { rlm@0: struct sio_hdl *sndHandle; rlm@0: rlm@0: ALvoid *mix_data; rlm@0: ALsizei data_size; rlm@0: rlm@0: volatile int killNow; rlm@0: ALvoid *thread; rlm@0: } sndio_data; rlm@0: rlm@0: rlm@0: static ALuint sndio_proc(ALvoid *ptr) rlm@0: { rlm@0: ALCdevice *device = ptr; rlm@0: sndio_data *data = device->ExtraData; rlm@0: ALsizei frameSize; rlm@0: size_t wrote; rlm@0: rlm@0: SetRTPriority(); rlm@0: rlm@0: frameSize = FrameSizeFromDevFmt(device->FmtChans, device->FmtType); rlm@0: rlm@0: while(!data->killNow && device->Connected) rlm@0: { rlm@0: ALsizei len = data->data_size; rlm@0: ALubyte *WritePtr = data->mix_data; rlm@0: rlm@0: aluMixData(device, WritePtr, len/frameSize); rlm@0: while(len > 0 && !data->killNow) rlm@0: { rlm@0: wrote = sio_write(data->sndHandle, WritePtr, len); rlm@0: if(wrote == 0) rlm@0: { rlm@0: ERR("sio_write failed\n"); rlm@0: aluHandleDisconnect(device); rlm@0: break; rlm@0: } rlm@0: rlm@0: len -= wrote; rlm@0: WritePtr += wrote; rlm@0: } rlm@0: } rlm@0: rlm@0: return 0; rlm@0: } rlm@0: rlm@0: rlm@0: rlm@0: static ALCboolean sndio_open_playback(ALCdevice *device, const ALCchar *deviceName) rlm@0: { rlm@0: sndio_data *data; rlm@0: rlm@0: if(!deviceName) rlm@0: deviceName = sndio_device; rlm@0: else if(strcmp(deviceName, sndio_device) != 0) rlm@0: return ALC_FALSE; rlm@0: rlm@0: data = calloc(1, sizeof(*data)); rlm@0: data->killNow = 0; rlm@0: rlm@0: data->sndHandle = sio_open(NULL, SIO_PLAY, 0); rlm@0: if(data->sndHandle == NULL) rlm@0: { rlm@0: free(data); rlm@0: ERR("Could not open device\n"); rlm@0: return ALC_FALSE; rlm@0: } rlm@0: rlm@0: device->szDeviceName = strdup(deviceName); rlm@0: device->ExtraData = data; rlm@0: rlm@0: return ALC_TRUE; rlm@0: } rlm@0: rlm@0: static void sndio_close_playback(ALCdevice *device) rlm@0: { rlm@0: sndio_data *data = device->ExtraData; rlm@0: rlm@0: sio_close(data->sndHandle); rlm@0: free(data); rlm@0: device->ExtraData = NULL; rlm@0: } rlm@0: rlm@0: static ALCboolean sndio_reset_playback(ALCdevice *device) rlm@0: { rlm@0: sndio_data *data = device->ExtraData; rlm@0: struct sio_par par; rlm@0: rlm@0: sio_initpar(&par); rlm@0: rlm@0: par.rate = device->Frequency; rlm@0: rlm@0: par.pchan = ((device->FmtChans != DevFmtMono) ? 2 : 1); rlm@0: rlm@0: switch(device->FmtType) rlm@0: { rlm@0: case DevFmtByte: rlm@0: par.bits = 8; rlm@0: par.sig = 1; rlm@0: break; rlm@0: case DevFmtUByte: rlm@0: par.bits = 8; rlm@0: par.sig = 0; rlm@0: break; rlm@0: case DevFmtFloat: rlm@0: device->FmtType = DevFmtShort; rlm@0: /* fall-through */ rlm@0: case DevFmtShort: rlm@0: par.bits = 16; rlm@0: par.sig = 1; rlm@0: break; rlm@0: case DevFmtUShort: rlm@0: par.bits = 16; rlm@0: par.sig = 0; rlm@0: break; rlm@0: } rlm@0: par.le = SIO_LE_NATIVE; rlm@0: rlm@0: par.round = device->UpdateSize; rlm@0: par.appbufsz = device->UpdateSize * (device->NumUpdates-1); rlm@0: if(!par.appbufsz) par.appbufsz = device->UpdateSize; rlm@0: rlm@0: rlm@0: if(!sio_setpar(data->sndHandle, &par) || !sio_getpar(data->sndHandle, &par)) rlm@0: { rlm@0: ERR("Failed to set device parameters\n"); rlm@0: return ALC_FALSE; rlm@0: } rlm@0: rlm@0: if(par.rate != device->Frequency) rlm@0: { rlm@0: if((device->Flags&DEVICE_FREQUENCY_REQUEST)) rlm@0: ERR("Failed to set frequency %uhz, got %uhz instead\n", device->Frequency, par.rate); rlm@0: device->Flags &= ~DEVICE_FREQUENCY_REQUEST; rlm@0: device->Frequency = par.rate; rlm@0: } rlm@0: rlm@0: if(par.pchan != ChannelsFromDevFmt(device->FmtChans)) rlm@0: { rlm@0: if(par.pchan != 1 && par.pchan != 2) rlm@0: { rlm@0: ERR("Unhandled channel count: %u\n", par.pchan); rlm@0: return ALC_FALSE; rlm@0: } rlm@0: if((device->Flags&DEVICE_CHANNELS_REQUEST)) rlm@0: ERR("Failed to set %s, got %u channels instead\n", DevFmtChannelsString(device->FmtChans), par.pchan); rlm@0: device->Flags &= ~DEVICE_CHANNELS_REQUEST; rlm@0: device->FmtChans = ((par.pchan==1) ? DevFmtMono : DevFmtStereo); rlm@0: } rlm@0: rlm@0: if(par.bits != par.bps*8) rlm@0: { rlm@0: ERR("Padded samples not supported (%u of %u bits)\n", par.bits, par.bps*8); rlm@0: return ALC_FALSE; rlm@0: } rlm@0: rlm@0: if(par.bits == 8 && par.sig == 1) rlm@0: device->FmtType = DevFmtByte; rlm@0: else if(par.bits == 8 && par.sig == 0) rlm@0: device->FmtType = DevFmtUByte; rlm@0: else if(par.bits == 16 && par.sig == 1) rlm@0: device->FmtType = DevFmtShort; rlm@0: else if(par.bits == 16 && par.sig == 0) rlm@0: device->FmtType = DevFmtUShort; rlm@0: else rlm@0: { rlm@0: ERR("Unhandled sample format: %s %u-bit\n", (par.sig?"signed":"unsigned"), par.bits); rlm@0: return ALC_FALSE; rlm@0: } rlm@0: rlm@0: rlm@0: device->UpdateSize = par.round; rlm@0: device->NumUpdates = (par.bufsz/par.round) + 1; rlm@0: rlm@0: SetDefaultChannelOrder(device); rlm@0: rlm@0: rlm@0: if(!sio_start(data->sndHandle)) rlm@0: { rlm@0: ERR("Error starting playback\n"); rlm@0: return ALC_FALSE; rlm@0: } rlm@0: rlm@0: data->data_size = device->UpdateSize * par.bps * par.pchan; rlm@0: data->mix_data = calloc(1, data->data_size); rlm@0: rlm@0: data->thread = StartThread(sndio_proc, device); rlm@0: if(data->thread == NULL) rlm@0: { rlm@0: sio_stop(data->sndHandle); rlm@0: free(data->mix_data); rlm@0: data->mix_data = NULL; rlm@0: return ALC_FALSE; rlm@0: } rlm@0: rlm@0: return ALC_TRUE; rlm@0: } rlm@0: rlm@0: static void sndio_stop_playback(ALCdevice *device) rlm@0: { rlm@0: sndio_data *data = device->ExtraData; rlm@0: rlm@0: if(!data->thread) rlm@0: return; rlm@0: rlm@0: data->killNow = 1; rlm@0: StopThread(data->thread); rlm@0: data->thread = NULL; rlm@0: rlm@0: data->killNow = 0; rlm@0: if(!sio_stop(data->sndHandle)) rlm@0: ERR("Error stopping device\n"); rlm@0: rlm@0: free(data->mix_data); rlm@0: data->mix_data = NULL; rlm@0: } rlm@0: rlm@0: rlm@0: static const BackendFuncs sndio_funcs = { rlm@0: sndio_open_playback, rlm@0: sndio_close_playback, rlm@0: sndio_reset_playback, rlm@0: sndio_stop_playback, rlm@0: NULL, rlm@0: NULL, rlm@0: NULL, rlm@0: NULL, rlm@0: NULL, rlm@0: NULL rlm@0: }; rlm@0: rlm@0: ALCboolean alc_sndio_init(BackendFuncs *func_list) rlm@0: { rlm@0: if(!sndio_load()) rlm@0: return ALC_FALSE; rlm@0: *func_list = sndio_funcs; rlm@0: return ALC_TRUE; rlm@0: } rlm@0: rlm@0: void alc_sndio_deinit(void) rlm@0: { rlm@0: #ifdef HAVE_DYNLOAD rlm@0: if(sndio_handle) rlm@0: CloseLib(sndio_handle); rlm@0: sndio_handle = NULL; rlm@0: #endif rlm@0: } rlm@0: rlm@0: void alc_sndio_probe(enum DevProbe type) rlm@0: { rlm@0: switch(type) rlm@0: { rlm@0: case DEVICE_PROBE: rlm@0: AppendDeviceList(sndio_device); rlm@0: break; rlm@0: case ALL_DEVICE_PROBE: rlm@0: AppendAllDeviceList(sndio_device); rlm@0: break; rlm@0: case CAPTURE_DEVICE_PROBE: rlm@0: break; rlm@0: } rlm@0: }