2005-06-07 19:10:44 +00:00
|
|
|
/*
|
|
|
|
snd_alsa.c
|
|
|
|
|
|
|
|
Support for the ALSA 1.0.1 sound driver
|
|
|
|
|
|
|
|
Copyright (C) 1999,2000 contributors of the QuakeForge project
|
|
|
|
Please see the file "AUTHORS" for a list of contributors
|
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or
|
|
|
|
modify it under the terms of the GNU General Public License
|
|
|
|
as published by the Free Software Foundation; either version 2
|
|
|
|
of the License, or (at your option) any later version.
|
|
|
|
|
|
|
|
This program 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 General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program; if not, write to:
|
|
|
|
|
|
|
|
Free Software Foundation, Inc.
|
|
|
|
59 Temple Place - Suite 330
|
|
|
|
Boston, MA 02111-1307, USA
|
|
|
|
|
|
|
|
*/
|
|
|
|
//actually stolen from darkplaces.
|
|
|
|
//I guess noone can be arsed to write it themselves. :/
|
2007-07-23 11:11:47 +00:00
|
|
|
//
|
|
|
|
//This file is otherwise known as 'will the linux jokers please stop fucking over the open sound system please'
|
2018-08-23 07:04:55 +00:00
|
|
|
#ifndef NO_ALSA
|
2005-06-07 19:10:44 +00:00
|
|
|
#include <alsa/asoundlib.h>
|
|
|
|
|
|
|
|
#include "quakedef.h"
|
2005-07-03 15:16:20 +00:00
|
|
|
#include <dlfcn.h>
|
|
|
|
|
|
|
|
static void *alsasharedobject;
|
|
|
|
|
|
|
|
int (*psnd_pcm_open) (snd_pcm_t **pcm, const char *name, snd_pcm_stream_t stream, int mode);
|
|
|
|
int (*psnd_pcm_close) (snd_pcm_t *pcm);
|
2018-03-06 16:46:57 +00:00
|
|
|
int (*psnd_config_update_free_global)(void);
|
2005-07-03 15:16:20 +00:00
|
|
|
const char *(*psnd_strerror) (int errnum);
|
|
|
|
int (*psnd_pcm_hw_params_any) (snd_pcm_t *pcm, snd_pcm_hw_params_t *params);
|
|
|
|
int (*psnd_pcm_hw_params_set_access) (snd_pcm_t *pcm, snd_pcm_hw_params_t *params, snd_pcm_access_t _access);
|
|
|
|
int (*psnd_pcm_hw_params_set_format) (snd_pcm_t *pcm, snd_pcm_hw_params_t *params, snd_pcm_format_t val);
|
|
|
|
int (*psnd_pcm_hw_params_set_channels) (snd_pcm_t *pcm, snd_pcm_hw_params_t *params, unsigned int val);
|
|
|
|
int (*psnd_pcm_hw_params_set_rate_near) (snd_pcm_t *pcm, snd_pcm_hw_params_t *params, unsigned int *val, int *dir);
|
|
|
|
int (*psnd_pcm_hw_params_set_period_size_near) (snd_pcm_t *pcm, snd_pcm_hw_params_t *params, snd_pcm_uframes_t *val, int *dir);
|
|
|
|
int (*psnd_pcm_hw_params) (snd_pcm_t *pcm, snd_pcm_hw_params_t *params);
|
|
|
|
int (*psnd_pcm_sw_params_current) (snd_pcm_t *pcm, snd_pcm_sw_params_t *params);
|
|
|
|
int (*psnd_pcm_sw_params_set_start_threshold) (snd_pcm_t *pcm, snd_pcm_sw_params_t *params, snd_pcm_uframes_t val);
|
|
|
|
int (*psnd_pcm_sw_params_set_stop_threshold) (snd_pcm_t *pcm, snd_pcm_sw_params_t *params, snd_pcm_uframes_t val);
|
|
|
|
int (*psnd_pcm_sw_params) (snd_pcm_t *pcm, snd_pcm_sw_params_t *params);
|
|
|
|
int (*psnd_pcm_hw_params_get_buffer_size) (const snd_pcm_hw_params_t *params, snd_pcm_uframes_t *val);
|
2006-05-10 05:33:09 +00:00
|
|
|
int (*psnd_pcm_hw_params_set_buffer_size_near) (snd_pcm_t *pcm, snd_pcm_hw_params_t *params, snd_pcm_uframes_t *val);
|
2013-03-12 22:48:31 +00:00
|
|
|
int (*psnd_pcm_set_params) (snd_pcm_t *pcm, snd_pcm_format_t format, snd_pcm_access_t access, unsigned int channels, unsigned int rate, int soft_resample, unsigned int latency);
|
2005-07-03 15:16:20 +00:00
|
|
|
snd_pcm_sframes_t (*psnd_pcm_avail_update) (snd_pcm_t *pcm);
|
|
|
|
snd_pcm_state_t (*psnd_pcm_state) (snd_pcm_t *pcm);
|
|
|
|
int (*psnd_pcm_start) (snd_pcm_t *pcm);
|
2013-03-12 22:48:31 +00:00
|
|
|
int (*psnd_pcm_recover) (snd_pcm_t *pcm, int err, int silent);
|
2005-07-03 15:16:20 +00:00
|
|
|
|
|
|
|
size_t (*psnd_pcm_hw_params_sizeof) (void);
|
|
|
|
size_t (*psnd_pcm_sw_params_sizeof) (void);
|
|
|
|
|
2011-01-29 19:53:38 +00:00
|
|
|
int (*psnd_pcm_mmap_begin) (snd_pcm_t *pcm, const snd_pcm_channel_area_t **areas, snd_pcm_uframes_t *offset, snd_pcm_uframes_t *frames);
|
|
|
|
snd_pcm_sframes_t (*psnd_pcm_mmap_commit) (snd_pcm_t *pcm, snd_pcm_uframes_t offset, snd_pcm_uframes_t frames);
|
2005-06-07 19:10:44 +00:00
|
|
|
|
2011-01-29 19:53:38 +00:00
|
|
|
snd_pcm_sframes_t (*psnd_pcm_writei) (snd_pcm_t *pcm, const void *buffer, snd_pcm_uframes_t size);
|
|
|
|
int (*psnd_pcm_prepare) (snd_pcm_t *pcm);
|
2005-06-07 19:10:44 +00:00
|
|
|
|
2015-05-14 03:06:58 +00:00
|
|
|
int (*psnd_device_name_hint) (int card, const char *iface, void ***hints);
|
|
|
|
char * (*psnd_device_name_get_hint) (const void *hint, const char *id);
|
|
|
|
int (*psnd_device_name_free_hint) (void **hints);
|
|
|
|
|
2011-01-29 19:53:38 +00:00
|
|
|
|
|
|
|
static unsigned int ALSA_MMap_GetDMAPos (soundcardinfo_t *sc)
|
2005-06-14 04:52:10 +00:00
|
|
|
{
|
|
|
|
const snd_pcm_channel_area_t *areas;
|
|
|
|
snd_pcm_uframes_t offset;
|
|
|
|
snd_pcm_uframes_t nframes = sc->sn.samples / sc->sn.numchannels;
|
|
|
|
|
2005-07-03 15:16:20 +00:00
|
|
|
psnd_pcm_avail_update (sc->handle);
|
|
|
|
psnd_pcm_mmap_begin (sc->handle, &areas, &offset, &nframes);
|
2005-06-14 04:52:10 +00:00
|
|
|
offset *= sc->sn.numchannels;
|
|
|
|
nframes *= sc->sn.numchannels;
|
|
|
|
sc->sn.samplepos = offset;
|
|
|
|
sc->sn.buffer = areas->addr;
|
|
|
|
return sc->sn.samplepos;
|
|
|
|
}
|
|
|
|
|
2011-01-29 19:53:38 +00:00
|
|
|
static void ALSA_MMap_Submit (soundcardinfo_t *sc, int start, int end)
|
2005-06-14 04:52:10 +00:00
|
|
|
{
|
|
|
|
int state;
|
2011-01-29 19:53:38 +00:00
|
|
|
int count = end - start;
|
2005-06-14 04:52:10 +00:00
|
|
|
const snd_pcm_channel_area_t *areas;
|
|
|
|
snd_pcm_uframes_t nframes;
|
|
|
|
snd_pcm_uframes_t offset;
|
|
|
|
|
|
|
|
nframes = count / sc->sn.numchannels;
|
|
|
|
|
2005-07-03 15:16:20 +00:00
|
|
|
psnd_pcm_avail_update (sc->handle);
|
|
|
|
psnd_pcm_mmap_begin (sc->handle, &areas, &offset, &nframes);
|
2005-06-14 04:52:10 +00:00
|
|
|
|
2005-07-03 15:16:20 +00:00
|
|
|
state = psnd_pcm_state (sc->handle);
|
2005-06-14 04:52:10 +00:00
|
|
|
|
|
|
|
switch (state) {
|
|
|
|
case SND_PCM_STATE_PREPARED:
|
2005-07-03 15:16:20 +00:00
|
|
|
psnd_pcm_mmap_commit (sc->handle, offset, nframes);
|
|
|
|
psnd_pcm_start (sc->handle);
|
2005-06-14 04:52:10 +00:00
|
|
|
break;
|
|
|
|
case SND_PCM_STATE_RUNNING:
|
2005-07-03 15:16:20 +00:00
|
|
|
psnd_pcm_mmap_commit (sc->handle, offset, nframes);
|
2005-06-14 04:52:10 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2011-01-29 19:53:38 +00:00
|
|
|
static unsigned int ALSA_RW_GetDMAPos (soundcardinfo_t *sc)
|
|
|
|
{
|
|
|
|
int frames;
|
|
|
|
frames = psnd_pcm_avail_update(sc->handle);
|
2013-03-12 22:48:31 +00:00
|
|
|
if (frames < 0)
|
|
|
|
{
|
|
|
|
psnd_pcm_start (sc->handle);
|
|
|
|
psnd_pcm_recover(sc->handle, frames, true);
|
|
|
|
frames = psnd_pcm_avail_update(sc->handle);
|
|
|
|
}
|
2011-01-29 19:53:38 +00:00
|
|
|
if (frames >= 0)
|
|
|
|
{
|
|
|
|
sc->sn.samplepos = (sc->snd_sent + frames) * sc->sn.numchannels;
|
|
|
|
}
|
|
|
|
return sc->sn.samplepos;
|
|
|
|
}
|
|
|
|
static void ALSA_RW_Submit (soundcardinfo_t *sc, int start, int end)
|
|
|
|
{
|
2013-03-12 22:48:31 +00:00
|
|
|
// int state;
|
2012-11-27 03:23:19 +00:00
|
|
|
unsigned int frames, offset, ringsize;
|
|
|
|
unsigned chunk;
|
|
|
|
int result;
|
2017-09-20 11:27:13 +00:00
|
|
|
int stride = sc->sn.numchannels * sc->sn.samplebytes;
|
2011-01-29 19:53:38 +00:00
|
|
|
|
2013-03-12 22:48:31 +00:00
|
|
|
while(1)
|
|
|
|
{
|
|
|
|
/*we can't change the data that was already written*/
|
|
|
|
frames = end - sc->snd_sent;
|
|
|
|
if (frames <= 0)
|
|
|
|
return;
|
2011-06-02 05:16:44 +00:00
|
|
|
|
2013-03-12 22:48:31 +00:00
|
|
|
// state = psnd_pcm_state (sc->handle);
|
2011-01-29 19:53:38 +00:00
|
|
|
|
2013-03-12 22:48:31 +00:00
|
|
|
ringsize = sc->sn.samples / sc->sn.numchannels;
|
2011-01-29 19:53:38 +00:00
|
|
|
|
2013-03-12 22:48:31 +00:00
|
|
|
chunk = frames;
|
|
|
|
offset = sc->snd_sent % ringsize;
|
2011-01-29 19:53:38 +00:00
|
|
|
|
2013-03-12 22:48:31 +00:00
|
|
|
if (offset + chunk >= ringsize)
|
|
|
|
chunk = ringsize - offset;
|
|
|
|
result = psnd_pcm_writei(sc->handle, sc->sn.buffer + offset*stride, chunk);
|
|
|
|
if (result < chunk)
|
|
|
|
{
|
|
|
|
if (result < 0)
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
sc->snd_sent += chunk;
|
2012-11-27 03:23:19 +00:00
|
|
|
|
2013-03-12 22:48:31 +00:00
|
|
|
chunk = frames - chunk;
|
|
|
|
if (chunk)
|
|
|
|
{
|
|
|
|
result = psnd_pcm_writei(sc->handle, sc->sn.buffer, chunk);
|
|
|
|
if (result > 0)
|
|
|
|
sc->snd_sent += result;
|
|
|
|
}
|
2011-01-29 19:53:38 +00:00
|
|
|
|
2013-03-12 22:48:31 +00:00
|
|
|
// if (state == SND_PCM_STATE_PREPARED)
|
|
|
|
// psnd_pcm_start (sc->handle);
|
|
|
|
};
|
2011-01-29 19:53:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void ALSA_Shutdown (soundcardinfo_t *sc)
|
|
|
|
{
|
|
|
|
psnd_pcm_close (sc->handle);
|
2018-03-06 16:46:57 +00:00
|
|
|
psnd_config_update_free_global(); //and try to reduce leaks
|
2011-01-30 01:32:30 +00:00
|
|
|
|
2011-06-02 05:16:44 +00:00
|
|
|
if (sc->Submit == ALSA_RW_Submit)
|
|
|
|
free(sc->sn.buffer);
|
2015-05-14 03:06:58 +00:00
|
|
|
|
|
|
|
Con_DPrintf("Alsa closed\n");
|
2011-01-29 19:53:38 +00:00
|
|
|
}
|
2005-06-14 04:52:10 +00:00
|
|
|
|
2012-04-09 19:12:12 +00:00
|
|
|
static void *ALSA_LockBuffer(soundcardinfo_t *sc, unsigned int *sampidx)
|
2005-06-14 04:52:10 +00:00
|
|
|
{
|
|
|
|
return sc->sn.buffer;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void ALSA_UnlockBuffer(soundcardinfo_t *sc, void *buffer)
|
|
|
|
{
|
|
|
|
}
|
2005-06-07 19:10:44 +00:00
|
|
|
|
2005-07-03 15:16:20 +00:00
|
|
|
static qboolean Alsa_InitAlsa(void)
|
|
|
|
{
|
|
|
|
static qboolean tried;
|
|
|
|
static qboolean alsaworks;
|
|
|
|
if (tried)
|
|
|
|
return alsaworks;
|
|
|
|
tried = true;
|
|
|
|
|
2018-08-21 18:57:51 +00:00
|
|
|
//pulseaudio's wrapper library fucks with alsa in bad ways, making it unusable on some systems.
|
|
|
|
if (COM_CheckParm("-noalsa"))
|
|
|
|
return false;
|
|
|
|
|
2008-04-29 22:11:17 +00:00
|
|
|
// Try alternative names of libasound, sometimes it is not linked correctly.
|
|
|
|
alsasharedobject = dlopen("libasound.so.2", RTLD_LAZY|RTLD_LOCAL);
|
2005-07-03 15:16:20 +00:00
|
|
|
if (!alsasharedobject)
|
2005-11-26 03:02:55 +00:00
|
|
|
{
|
2008-04-29 22:14:16 +00:00
|
|
|
alsasharedobject = dlopen("libasound.so", RTLD_LAZY|RTLD_LOCAL);
|
|
|
|
if (!alsasharedobject)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2005-11-26 03:02:55 +00:00
|
|
|
}
|
2005-07-03 15:16:20 +00:00
|
|
|
|
|
|
|
|
2015-05-14 03:06:58 +00:00
|
|
|
psnd_pcm_open = dlsym(alsasharedobject, "snd_pcm_open");
|
|
|
|
psnd_pcm_close = dlsym(alsasharedobject, "snd_pcm_close");
|
2018-03-06 16:46:57 +00:00
|
|
|
psnd_config_update_free_global = dlsym(alsasharedobject, "snd_config_update_free_global");
|
2015-05-14 03:06:58 +00:00
|
|
|
psnd_strerror = dlsym(alsasharedobject, "snd_strerror");
|
|
|
|
psnd_pcm_hw_params_any = dlsym(alsasharedobject, "snd_pcm_hw_params_any");
|
|
|
|
psnd_pcm_hw_params_set_access = dlsym(alsasharedobject, "snd_pcm_hw_params_set_access");
|
|
|
|
psnd_pcm_hw_params_set_format = dlsym(alsasharedobject, "snd_pcm_hw_params_set_format");
|
|
|
|
psnd_pcm_hw_params_set_channels = dlsym(alsasharedobject, "snd_pcm_hw_params_set_channels");
|
|
|
|
psnd_pcm_hw_params_set_rate_near = dlsym(alsasharedobject, "snd_pcm_hw_params_set_rate_near");
|
2005-07-03 15:16:20 +00:00
|
|
|
psnd_pcm_hw_params_set_period_size_near = dlsym(alsasharedobject, "snd_pcm_hw_params_set_period_size_near");
|
2015-05-14 03:06:58 +00:00
|
|
|
psnd_pcm_hw_params = dlsym(alsasharedobject, "snd_pcm_hw_params");
|
|
|
|
psnd_pcm_sw_params_current = dlsym(alsasharedobject, "snd_pcm_sw_params_current");
|
2005-07-03 15:16:20 +00:00
|
|
|
psnd_pcm_sw_params_set_start_threshold = dlsym(alsasharedobject, "snd_pcm_sw_params_set_start_threshold");
|
|
|
|
psnd_pcm_sw_params_set_stop_threshold = dlsym(alsasharedobject, "snd_pcm_sw_params_set_stop_threshold");
|
2015-05-14 03:06:58 +00:00
|
|
|
psnd_pcm_sw_params = dlsym(alsasharedobject, "snd_pcm_sw_params");
|
|
|
|
psnd_pcm_hw_params_get_buffer_size = dlsym(alsasharedobject, "snd_pcm_hw_params_get_buffer_size");
|
|
|
|
psnd_pcm_avail_update = dlsym(alsasharedobject, "snd_pcm_avail_update");
|
|
|
|
psnd_pcm_state = dlsym(alsasharedobject, "snd_pcm_state");
|
|
|
|
psnd_pcm_start = dlsym(alsasharedobject, "snd_pcm_start");
|
|
|
|
psnd_pcm_recover = dlsym(alsasharedobject, "snd_pcm_recover");
|
|
|
|
psnd_pcm_set_params = dlsym(alsasharedobject, "snd_pcm_set_params");
|
|
|
|
psnd_pcm_hw_params_sizeof = dlsym(alsasharedobject, "snd_pcm_hw_params_sizeof");
|
|
|
|
psnd_pcm_sw_params_sizeof = dlsym(alsasharedobject, "snd_pcm_sw_params_sizeof");
|
2006-05-10 05:33:09 +00:00
|
|
|
psnd_pcm_hw_params_set_buffer_size_near = dlsym(alsasharedobject, "snd_pcm_hw_params_set_buffer_size_near");
|
2005-07-03 15:16:20 +00:00
|
|
|
|
2015-05-14 03:06:58 +00:00
|
|
|
psnd_pcm_mmap_begin = dlsym(alsasharedobject, "snd_pcm_mmap_begin");
|
|
|
|
psnd_pcm_mmap_commit = dlsym(alsasharedobject, "snd_pcm_mmap_commit");
|
2011-01-29 19:53:38 +00:00
|
|
|
|
2015-05-14 03:06:58 +00:00
|
|
|
psnd_pcm_writei = dlsym(alsasharedobject, "snd_pcm_writei");
|
|
|
|
psnd_pcm_prepare = dlsym(alsasharedobject, "snd_pcm_prepare");
|
|
|
|
|
|
|
|
psnd_device_name_hint = dlsym(alsasharedobject, "snd_device_name_hint");
|
|
|
|
psnd_device_name_get_hint = dlsym(alsasharedobject, "snd_device_name_get_hint");
|
|
|
|
psnd_device_name_free_hint = dlsym(alsasharedobject, "snd_device_name_free_hint");
|
2011-01-29 19:53:38 +00:00
|
|
|
|
2005-07-03 15:16:20 +00:00
|
|
|
alsaworks = psnd_pcm_open
|
|
|
|
&& psnd_pcm_close
|
2018-03-06 16:46:57 +00:00
|
|
|
&& psnd_config_update_free_global
|
2005-07-03 15:16:20 +00:00
|
|
|
&& psnd_strerror
|
|
|
|
&& psnd_pcm_hw_params_any
|
|
|
|
&& psnd_pcm_hw_params_set_access
|
|
|
|
&& psnd_pcm_hw_params_set_format
|
|
|
|
&& psnd_pcm_hw_params_set_channels
|
|
|
|
&& psnd_pcm_hw_params_set_rate_near
|
|
|
|
&& psnd_pcm_hw_params_set_period_size_near
|
|
|
|
&& psnd_pcm_hw_params
|
|
|
|
&& psnd_pcm_sw_params_current
|
|
|
|
&& psnd_pcm_sw_params_set_start_threshold
|
|
|
|
&& psnd_pcm_sw_params_set_stop_threshold
|
|
|
|
&& psnd_pcm_sw_params
|
|
|
|
&& psnd_pcm_hw_params_get_buffer_size
|
|
|
|
&& psnd_pcm_avail_update
|
|
|
|
&& psnd_pcm_state
|
|
|
|
&& psnd_pcm_start
|
|
|
|
&& psnd_pcm_hw_params_sizeof
|
2006-05-10 05:33:09 +00:00
|
|
|
&& psnd_pcm_sw_params_sizeof
|
2011-01-29 19:53:38 +00:00
|
|
|
&& psnd_pcm_hw_params_set_buffer_size_near
|
|
|
|
&& psnd_pcm_mmap_begin
|
|
|
|
&& psnd_pcm_mmap_commit
|
|
|
|
&& psnd_pcm_writei && psnd_pcm_prepare
|
2015-05-14 03:06:58 +00:00
|
|
|
&& psnd_device_name_hint && psnd_device_name_get_hint && psnd_device_name_free_hint
|
2011-01-29 19:53:38 +00:00
|
|
|
;
|
2005-07-03 15:16:20 +00:00
|
|
|
|
|
|
|
return alsaworks;
|
|
|
|
}
|
|
|
|
|
2015-05-14 03:06:58 +00:00
|
|
|
static qboolean QDECL ALSA_InitCard (soundcardinfo_t *sc, const char *pcmname)
|
2005-06-07 19:10:44 +00:00
|
|
|
{
|
2005-06-14 04:52:10 +00:00
|
|
|
snd_pcm_t *pcm;
|
|
|
|
snd_pcm_uframes_t buffer_size;
|
|
|
|
|
2006-05-09 22:31:23 +00:00
|
|
|
int err;
|
2005-06-07 19:10:44 +00:00
|
|
|
snd_pcm_hw_params_t *hw;
|
|
|
|
snd_pcm_sw_params_t *sw;
|
2013-05-11 05:03:07 +00:00
|
|
|
#if 0
|
|
|
|
int bps, stereo;
|
|
|
|
unsigned int rate;
|
2005-06-07 19:10:44 +00:00
|
|
|
snd_pcm_uframes_t frag_size;
|
2013-05-11 05:03:07 +00:00
|
|
|
#endif
|
2011-01-29 19:53:38 +00:00
|
|
|
qboolean mmap = false;
|
2005-06-07 19:10:44 +00:00
|
|
|
|
2005-07-03 15:16:20 +00:00
|
|
|
if (!Alsa_InitAlsa())
|
2005-11-26 03:02:55 +00:00
|
|
|
{
|
2007-10-05 18:08:47 +00:00
|
|
|
Con_Printf(CON_ERROR "Alsa does not appear to be installed or compatible\n");
|
2015-05-14 03:06:58 +00:00
|
|
|
return false;
|
2005-11-26 03:02:55 +00:00
|
|
|
}
|
2005-07-03 15:16:20 +00:00
|
|
|
|
|
|
|
hw = alloca(psnd_pcm_hw_params_sizeof());
|
|
|
|
sw = alloca(psnd_pcm_sw_params_sizeof());
|
2007-07-23 11:11:47 +00:00
|
|
|
memset(sw, 0, psnd_pcm_sw_params_sizeof());
|
|
|
|
memset(hw, 0, psnd_pcm_hw_params_sizeof());
|
2005-06-07 19:10:44 +00:00
|
|
|
|
2007-07-23 11:11:47 +00:00
|
|
|
//WARNING: 'default' as the default sucks arse. it adds about a second's worth of lag.
|
2015-05-14 03:06:58 +00:00
|
|
|
if (!pcmname)
|
|
|
|
pcmname = "default";
|
2005-06-14 04:52:10 +00:00
|
|
|
|
|
|
|
sc->inactive_sound = true; //linux sound devices always play sound, even when we're not the active app...
|
2005-06-07 19:10:44 +00:00
|
|
|
|
2009-05-20 17:28:15 +00:00
|
|
|
Con_Printf("Initing ALSA sound device \"%s\"\n", pcmname);
|
2005-11-26 03:02:55 +00:00
|
|
|
|
2005-07-03 15:16:20 +00:00
|
|
|
err = psnd_pcm_open (&pcm, pcmname, SND_PCM_STREAM_PLAYBACK,
|
2005-06-07 19:10:44 +00:00
|
|
|
SND_PCM_NONBLOCK);
|
2006-05-09 21:38:27 +00:00
|
|
|
if (0 > err)
|
|
|
|
{
|
2015-05-14 03:06:58 +00:00
|
|
|
Con_Printf (CON_ERROR "ALSA Error: open error (%s): %s\n", pcmname, psnd_strerror (err));
|
2005-06-07 19:10:44 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
Con_Printf ("ALSA: Using PCM %s.\n", pcmname);
|
|
|
|
|
2013-03-12 22:48:31 +00:00
|
|
|
#if 1
|
2017-09-20 11:27:13 +00:00
|
|
|
if (!sc->sn.sampleformat)
|
|
|
|
sc->sn.sampleformat = (sc->sn.samplebytes==1)?QSF_U8:QSF_S16;
|
|
|
|
switch(sc->sn.sampleformat)
|
|
|
|
{
|
|
|
|
case QSF_U8: err = SND_PCM_FORMAT_U8; break;
|
|
|
|
case QSF_S8: err = SND_PCM_FORMAT_S8; break;
|
|
|
|
case QSF_S16: err = SND_PCM_FORMAT_S16; break;
|
|
|
|
case QSF_F32: err = SND_PCM_FORMAT_FLOAT; break;
|
|
|
|
default:
|
|
|
|
Con_Printf (CON_ERROR "ALSA: unsupported sample format %i\n", sc->sn.sampleformat);
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
|
|
|
err = psnd_pcm_set_params(pcm, err, (mmap?SND_PCM_ACCESS_MMAP_INTERLEAVED:SND_PCM_ACCESS_RW_INTERLEAVED), sc->sn.numchannels, sc->sn.speed, true, 0.04*1000000);
|
2013-03-12 22:48:31 +00:00
|
|
|
if (0 > err)
|
|
|
|
{
|
2015-05-14 03:06:58 +00:00
|
|
|
Con_Printf (CON_ERROR "ALSA: error setting params. %s\n", psnd_strerror (err));
|
2013-03-12 22:48:31 +00:00
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
|
|
|
// sc->sn.numchannels = stereo;
|
|
|
|
// sc->sn.samplepos = 0;
|
2017-09-20 11:27:13 +00:00
|
|
|
// sc->sn.samplebytes = bps/8;
|
2013-03-12 22:48:31 +00:00
|
|
|
|
|
|
|
sc->samplequeue = buffer_size = 2048;
|
|
|
|
#else
|
2005-07-03 15:16:20 +00:00
|
|
|
err = psnd_pcm_hw_params_any (pcm, hw);
|
2013-03-12 22:48:31 +00:00
|
|
|
if (0 > err)
|
|
|
|
{
|
2015-05-14 03:06:58 +00:00
|
|
|
Con_Printf (CON_ERROR "ALSA: error setting hw_params_any. %s\n", psnd_strerror (err));
|
2005-06-07 19:10:44 +00:00
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
2011-01-29 19:53:38 +00:00
|
|
|
err = psnd_pcm_hw_params_set_access (pcm, hw, mmap?SND_PCM_ACCESS_MMAP_INTERLEAVED:SND_PCM_ACCESS_RW_INTERLEAVED);
|
2011-06-02 05:16:44 +00:00
|
|
|
if (0 > err)
|
2006-05-09 21:38:27 +00:00
|
|
|
{
|
2015-05-14 03:06:58 +00:00
|
|
|
Con_Printf (CON_ERROR "ALSA: Failure to set interleaved PCM access. %s\n", psnd_strerror (err));
|
2005-06-07 19:10:44 +00:00
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
2006-05-09 21:38:27 +00:00
|
|
|
// get sample bit size
|
2017-09-20 11:27:13 +00:00
|
|
|
bps = sc->sn.samplebytes*8;
|
2006-05-09 21:38:27 +00:00
|
|
|
{
|
|
|
|
snd_pcm_format_t spft;
|
|
|
|
if (bps == 16)
|
|
|
|
spft = SND_PCM_FORMAT_S16;
|
|
|
|
else
|
|
|
|
spft = SND_PCM_FORMAT_U8;
|
|
|
|
|
|
|
|
err = psnd_pcm_hw_params_set_format (pcm, hw, spft);
|
|
|
|
while (err < 0)
|
|
|
|
{
|
|
|
|
if (spft == SND_PCM_FORMAT_S16)
|
|
|
|
{
|
2005-06-07 19:10:44 +00:00
|
|
|
bps = 8;
|
2006-05-09 21:38:27 +00:00
|
|
|
spft = SND_PCM_FORMAT_U8;
|
2005-06-07 19:10:44 +00:00
|
|
|
}
|
2006-05-09 21:38:27 +00:00
|
|
|
else
|
|
|
|
{
|
2007-09-23 15:28:06 +00:00
|
|
|
Con_Printf (CON_ERROR "ALSA: no usable formats. %s\n", psnd_strerror (err));
|
2005-06-07 19:10:44 +00:00
|
|
|
goto error;
|
|
|
|
}
|
2006-05-09 21:38:27 +00:00
|
|
|
err = psnd_pcm_hw_params_set_format (pcm, hw, spft);
|
|
|
|
}
|
2005-06-07 19:10:44 +00:00
|
|
|
}
|
|
|
|
|
2006-05-09 21:38:27 +00:00
|
|
|
// get speaker channels
|
2006-05-10 05:18:08 +00:00
|
|
|
stereo = sc->sn.numchannels;
|
2006-05-09 21:38:27 +00:00
|
|
|
err = psnd_pcm_hw_params_set_channels (pcm, hw, stereo);
|
2011-06-02 05:16:44 +00:00
|
|
|
while (err < 0)
|
2006-05-09 21:38:27 +00:00
|
|
|
{
|
|
|
|
if (stereo > 2)
|
|
|
|
stereo = 2;
|
|
|
|
else if (stereo > 1)
|
|
|
|
stereo = 1;
|
|
|
|
else
|
|
|
|
{
|
2007-09-23 15:28:06 +00:00
|
|
|
Con_Printf (CON_ERROR "ALSA: no usable number of channels. %s\n", psnd_strerror (err));
|
2005-06-07 19:10:44 +00:00
|
|
|
goto error;
|
2006-05-09 21:38:27 +00:00
|
|
|
}
|
|
|
|
err = psnd_pcm_hw_params_set_channels (pcm, hw, stereo);
|
2005-06-07 19:10:44 +00:00
|
|
|
}
|
|
|
|
|
2006-05-09 21:38:27 +00:00
|
|
|
// get rate
|
|
|
|
rate = sc->sn.speed;
|
|
|
|
err = psnd_pcm_hw_params_set_rate_near (pcm, hw, &rate, 0);
|
|
|
|
while (err < 0)
|
|
|
|
{
|
|
|
|
if (rate > 48000)
|
|
|
|
rate = 48000;
|
|
|
|
else if (rate > 44100)
|
2005-06-07 19:10:44 +00:00
|
|
|
rate = 44100;
|
2006-05-09 21:38:27 +00:00
|
|
|
else if (rate > 22150)
|
|
|
|
rate = 22150;
|
|
|
|
else if (rate > 11025)
|
|
|
|
rate = 11025;
|
|
|
|
else if (rate > 800)
|
|
|
|
rate = 800;
|
|
|
|
else
|
|
|
|
{
|
2007-09-23 15:28:06 +00:00
|
|
|
Con_Printf (CON_ERROR "ALSA: no usable rates. %s\n", psnd_strerror (err));
|
2005-06-07 19:10:44 +00:00
|
|
|
goto error;
|
2006-05-09 21:38:27 +00:00
|
|
|
}
|
|
|
|
err = psnd_pcm_hw_params_set_rate_near (pcm, hw, &rate, 0);
|
2005-06-07 19:10:44 +00:00
|
|
|
}
|
|
|
|
|
2006-05-09 21:38:27 +00:00
|
|
|
if (rate > 11025)
|
|
|
|
frag_size = 8 * bps * rate / 11025;
|
|
|
|
else
|
|
|
|
frag_size = 8 * bps;
|
|
|
|
|
2005-07-03 15:16:20 +00:00
|
|
|
err = psnd_pcm_hw_params_set_period_size_near (pcm, hw, &frag_size, 0);
|
2013-03-12 22:48:31 +00:00
|
|
|
if (0 > err)
|
|
|
|
{
|
2015-05-14 03:06:58 +00:00
|
|
|
Con_Printf (CON_ERROR "ALSA: unable to set period size near %i. %s\n", (int) frag_size, psnd_strerror (err));
|
2005-06-07 19:10:44 +00:00
|
|
|
goto error;
|
|
|
|
}
|
2005-07-03 15:16:20 +00:00
|
|
|
err = psnd_pcm_hw_params (pcm, hw);
|
2005-06-07 19:10:44 +00:00
|
|
|
if (0 > err) {
|
2007-09-23 15:28:06 +00:00
|
|
|
Con_Printf (CON_ERROR "ALSA: unable to install hw params: %s\n",
|
2005-07-03 15:16:20 +00:00
|
|
|
psnd_strerror (err));
|
2005-06-07 19:10:44 +00:00
|
|
|
goto error;
|
|
|
|
}
|
2005-07-03 15:16:20 +00:00
|
|
|
err = psnd_pcm_sw_params_current (pcm, sw);
|
2005-06-07 19:10:44 +00:00
|
|
|
if (0 > err) {
|
2015-05-14 03:06:58 +00:00
|
|
|
Con_Printf (CON_ERROR "ALSA: unable to determine current sw params. %s\n", psnd_strerror (err));
|
2005-06-07 19:10:44 +00:00
|
|
|
goto error;
|
|
|
|
}
|
2005-07-03 15:16:20 +00:00
|
|
|
err = psnd_pcm_sw_params_set_start_threshold (pcm, sw, ~0U);
|
2005-06-07 19:10:44 +00:00
|
|
|
if (0 > err) {
|
2015-05-14 03:06:58 +00:00
|
|
|
Con_Printf (CON_ERROR "ALSA: unable to set playback threshold. %s\n", psnd_strerror (err));
|
2005-06-07 19:10:44 +00:00
|
|
|
goto error;
|
|
|
|
}
|
2005-07-03 15:16:20 +00:00
|
|
|
err = psnd_pcm_sw_params_set_stop_threshold (pcm, sw, ~0U);
|
2005-06-07 19:10:44 +00:00
|
|
|
if (0 > err) {
|
2015-05-14 03:06:58 +00:00
|
|
|
Con_Printf (CON_ERROR "ALSA: unable to set playback stop threshold. %s\n", psnd_strerror (err));
|
2005-06-07 19:10:44 +00:00
|
|
|
goto error;
|
|
|
|
}
|
2005-07-03 15:16:20 +00:00
|
|
|
err = psnd_pcm_sw_params (pcm, sw);
|
2005-06-07 19:10:44 +00:00
|
|
|
if (0 > err) {
|
2015-05-14 03:06:58 +00:00
|
|
|
Con_Printf (CON_ERROR "ALSA: unable to install sw params. %s\n", psnd_strerror (err));
|
2005-06-07 19:10:44 +00:00
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
2006-05-09 21:38:27 +00:00
|
|
|
sc->sn.numchannels = stereo;
|
2005-06-07 19:10:44 +00:00
|
|
|
sc->sn.samplepos = 0;
|
2017-09-20 11:27:13 +00:00
|
|
|
sc->sn.samplebytes = bps/8;
|
2005-06-07 19:10:44 +00:00
|
|
|
|
2006-05-10 05:18:08 +00:00
|
|
|
buffer_size = sc->sn.samples / stereo;
|
|
|
|
if (buffer_size)
|
|
|
|
{
|
|
|
|
err = psnd_pcm_hw_params_set_buffer_size_near(pcm, hw, &buffer_size);
|
|
|
|
if (err < 0)
|
|
|
|
{
|
2007-09-23 15:28:06 +00:00
|
|
|
Con_Printf (CON_ERROR "ALSA: unable to set buffer size. %s\n", psnd_strerror (err));
|
2006-05-10 05:18:08 +00:00
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-07-03 15:16:20 +00:00
|
|
|
err = psnd_pcm_hw_params_get_buffer_size (hw, &buffer_size);
|
2005-06-07 19:10:44 +00:00
|
|
|
if (0 > err) {
|
2015-05-14 03:06:58 +00:00
|
|
|
Con_Printf (CON_ERROR "ALSA: unable to get buffer size. %s\n", psnd_strerror (err));
|
2005-06-07 19:10:44 +00:00
|
|
|
goto error;
|
|
|
|
}
|
2013-03-12 22:48:31 +00:00
|
|
|
sc->sn.speed = rate;
|
|
|
|
#endif
|
2005-06-07 19:10:44 +00:00
|
|
|
|
|
|
|
sc->sn.samples = buffer_size * sc->sn.numchannels; // mono samples in buffer
|
2005-06-14 04:52:10 +00:00
|
|
|
sc->handle = pcm;
|
2009-05-20 17:28:15 +00:00
|
|
|
|
2011-01-29 19:53:38 +00:00
|
|
|
sc->Lock = ALSA_LockBuffer;
|
|
|
|
sc->Unlock = ALSA_UnlockBuffer;
|
|
|
|
sc->Shutdown = ALSA_Shutdown;
|
|
|
|
if (mmap)
|
2009-05-20 17:28:15 +00:00
|
|
|
{
|
2011-01-29 19:53:38 +00:00
|
|
|
sc->GetDMAPos = ALSA_MMap_GetDMAPos;
|
|
|
|
sc->Submit = ALSA_MMap_Submit;
|
|
|
|
sc->GetDMAPos(sc); // sets shm->buffer
|
2011-06-02 05:16:44 +00:00
|
|
|
|
2011-01-29 19:53:38 +00:00
|
|
|
//alsa doesn't seem to like high mixahead values
|
|
|
|
//(maybe it tells us above somehow...)
|
|
|
|
//so force it lower
|
|
|
|
//quake's default of 0.2 was for 10fps rendering anyway
|
|
|
|
//so force it down to 0.1 which is the default for halflife at least, and should give better latency
|
2009-05-20 17:28:15 +00:00
|
|
|
{
|
2011-01-29 19:53:38 +00:00
|
|
|
extern cvar_t _snd_mixahead;
|
|
|
|
if (_snd_mixahead.value >= 0.2)
|
|
|
|
{
|
|
|
|
Con_Printf("Alsa Hack: _snd_mixahead forced lower\n");
|
|
|
|
_snd_mixahead.value = 0.1;
|
|
|
|
}
|
2009-05-20 17:28:15 +00:00
|
|
|
}
|
|
|
|
}
|
2011-01-29 19:53:38 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
sc->GetDMAPos = ALSA_RW_GetDMAPos;
|
|
|
|
sc->Submit = ALSA_RW_Submit;
|
|
|
|
|
|
|
|
sc->samplequeue = sc->sn.samples;
|
2017-09-20 11:27:13 +00:00
|
|
|
sc->sn.buffer = malloc(sc->sn.samples * sc->sn.samplebytes);
|
2011-01-29 19:53:38 +00:00
|
|
|
|
|
|
|
err = psnd_pcm_prepare(pcm);
|
|
|
|
if (0 > err)
|
|
|
|
{
|
2015-05-14 03:06:58 +00:00
|
|
|
Con_Printf (CON_ERROR "ALSA: unable to prepare for use. %s\n", psnd_strerror (err));
|
2011-01-29 19:53:38 +00:00
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-06-07 19:10:44 +00:00
|
|
|
return true;
|
|
|
|
|
|
|
|
error:
|
2005-07-03 15:16:20 +00:00
|
|
|
psnd_pcm_close (pcm);
|
2005-06-07 19:10:44 +00:00
|
|
|
return false;
|
|
|
|
}
|
2015-05-14 03:06:58 +00:00
|
|
|
#define SDRVNAME "ALSA"
|
|
|
|
static qboolean QDECL ALSA_Enumerate(void (QDECL *cb) (const char *drivername, const char *devicecode, const char *readablename))
|
|
|
|
{
|
|
|
|
size_t i;
|
|
|
|
void **hints;
|
2005-06-07 19:10:44 +00:00
|
|
|
|
2015-05-14 03:06:58 +00:00
|
|
|
if (Alsa_InitAlsa())
|
|
|
|
{
|
|
|
|
if (!psnd_device_name_hint(-1, "pcm", &hints))
|
|
|
|
{
|
|
|
|
for (i = 0; hints[i]; i++)
|
|
|
|
{
|
|
|
|
char *n = psnd_device_name_get_hint(hints[i], "NAME");
|
|
|
|
if (n)
|
|
|
|
{
|
|
|
|
char *t = psnd_device_name_get_hint(hints[i], "IOID");
|
|
|
|
if (!t || strcasecmp(t, "Input"))
|
|
|
|
{
|
|
|
|
char *d = psnd_device_name_get_hint(hints[i], "DESC");
|
|
|
|
if (d)
|
|
|
|
cb(SDRVNAME, n, va("ALSA (%s)", d));
|
|
|
|
else
|
|
|
|
cb(SDRVNAME, n, n);
|
|
|
|
free(d);
|
|
|
|
}
|
|
|
|
free(t);
|
|
|
|
free(n); //dangerous to free things across boundaries.
|
|
|
|
}
|
|
|
|
}
|
|
|
|
psnd_device_name_free_hint(hints);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
sounddriver_t ALSA_Output =
|
|
|
|
{
|
|
|
|
SDRVNAME,
|
|
|
|
ALSA_InitCard,
|
|
|
|
ALSA_Enumerate
|
|
|
|
};
|
2018-08-23 07:04:55 +00:00
|
|
|
#endif
|
|
|
|
|