2001-12-28 19:56:33 +00:00
|
|
|
/*
|
|
|
|
Copyright (C) 1997-2001 Id Software, Inc.
|
|
|
|
|
|
|
|
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 the Free Software
|
|
|
|
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
|
|
|
|
|
|
|
*/
|
2001-12-22 04:27:19 +00:00
|
|
|
#include <unistd.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <sys/types.h>
|
2002-02-20 04:38:42 +00:00
|
|
|
#include <pthread.h>
|
2001-12-22 04:27:19 +00:00
|
|
|
#include <sys/ioctl.h>
|
|
|
|
#include <sys/mman.h>
|
|
|
|
#include <sys/shm.h>
|
|
|
|
#include <sys/wait.h>
|
|
|
|
#include <linux/soundcard.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
|
|
|
|
#include "../client/client.h"
|
|
|
|
#include "../client/snd_loc.h"
|
|
|
|
|
2002-02-20 04:38:42 +00:00
|
|
|
#define FRAGSIZEEXP 9
|
|
|
|
#define FRAGSIZE (1<<FRAGSIZEEXP)
|
|
|
|
|
|
|
|
#define AUDIOBUFFERSIZE 4096
|
|
|
|
#define AUDIOBUFFERS 64
|
|
|
|
|
|
|
|
static int audio_fd = -1;
|
|
|
|
static volatile int snd_inited;
|
|
|
|
static volatile int frags_sent;
|
|
|
|
static int mmapped = 0;
|
2001-12-22 04:27:19 +00:00
|
|
|
|
|
|
|
cvar_t *sndbits;
|
|
|
|
cvar_t *sndspeed;
|
|
|
|
cvar_t *sndchannels;
|
|
|
|
cvar_t *snddevice;
|
|
|
|
|
2002-01-12 02:14:09 +00:00
|
|
|
static int tryrates[] = { 11025, 22051, 44100, 48000, 8000 };
|
2001-12-22 04:27:19 +00:00
|
|
|
|
2002-02-20 04:38:42 +00:00
|
|
|
static pthread_t audio;
|
|
|
|
|
|
|
|
void * thesound(void * arg) {
|
|
|
|
while (snd_inited) {
|
|
|
|
write(audio_fd, dma.buffer + frags_sent * FRAGSIZE, FRAGSIZE);
|
|
|
|
frags_sent++;
|
|
|
|
frags_sent &= (dma.samples * (dma.samplebits/8) / FRAGSIZE) - 1;
|
|
|
|
}
|
|
|
|
pthread_exit(0L);
|
|
|
|
}
|
|
|
|
|
2001-12-22 04:27:19 +00:00
|
|
|
qboolean SNDDMA_Init(void)
|
|
|
|
{
|
|
|
|
int rc;
|
|
|
|
int fmt;
|
|
|
|
int tmp;
|
|
|
|
int i;
|
|
|
|
struct audio_buf_info info;
|
|
|
|
int caps;
|
|
|
|
extern uid_t saved_euid;
|
|
|
|
|
|
|
|
if (snd_inited)
|
2001-12-29 02:40:13 +00:00
|
|
|
return 1;
|
2001-12-22 04:27:19 +00:00
|
|
|
|
2002-01-12 02:14:09 +00:00
|
|
|
snd_inited = 0;
|
|
|
|
|
|
|
|
if (!snddevice)
|
|
|
|
{
|
2001-12-22 04:27:19 +00:00
|
|
|
sndbits = Cvar_Get("sndbits", "16", CVAR_ARCHIVE);
|
|
|
|
sndspeed = Cvar_Get("sndspeed", "0", CVAR_ARCHIVE);
|
|
|
|
sndchannels = Cvar_Get("sndchannels", "2", CVAR_ARCHIVE);
|
|
|
|
snddevice = Cvar_Get("snddevice", "/dev/dsp", CVAR_ARCHIVE);
|
|
|
|
}
|
|
|
|
|
2002-02-20 04:38:42 +00:00
|
|
|
// open /dev/dsp, check capability to mmap, and get size of dma buffer
|
2001-12-22 04:27:19 +00:00
|
|
|
|
2002-01-12 02:14:09 +00:00
|
|
|
if (audio_fd == -1)
|
|
|
|
{
|
2001-12-22 04:27:19 +00:00
|
|
|
seteuid(saved_euid);
|
|
|
|
|
|
|
|
audio_fd = open(snddevice->string, O_RDWR);
|
|
|
|
|
2002-01-12 02:14:09 +00:00
|
|
|
if (audio_fd == -1)
|
2001-12-22 04:27:19 +00:00
|
|
|
{
|
|
|
|
perror(snddevice->string);
|
2002-01-12 02:14:09 +00:00
|
|
|
seteuid(getuid());
|
|
|
|
Com_Printf("SNDDMA_Init: Could not open %s.\n", snddevice->string);
|
2001-12-22 04:27:19 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2002-01-12 02:14:09 +00:00
|
|
|
seteuid(getuid());
|
2001-12-22 04:27:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
rc = ioctl(audio_fd, SNDCTL_DSP_RESET, 0);
|
2002-01-12 02:14:09 +00:00
|
|
|
if (rc == -1)
|
2001-12-22 04:27:19 +00:00
|
|
|
{
|
|
|
|
perror(snddevice->string);
|
2002-01-12 02:14:09 +00:00
|
|
|
Com_Printf("SNDDMA_Init: Could not reset %s.\n", snddevice->string);
|
2001-12-22 04:27:19 +00:00
|
|
|
close(audio_fd);
|
2002-01-12 02:14:09 +00:00
|
|
|
audio_fd = -1;
|
2001-12-22 04:27:19 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2002-02-20 04:38:42 +00:00
|
|
|
if (ioctl(audio_fd, SNDCTL_DSP_GETCAPS, &caps) == -1
|
|
|
|
|| !(caps & DSP_CAP_TRIGGER) || !(caps & DSP_CAP_MMAP)) {
|
|
|
|
Com_Printf("SNDDMA_Init: Sound device does not support mmap");
|
|
|
|
} else {
|
|
|
|
if (ioctl(audio_fd, SNDCTL_DSP_GETOSPACE, &info) == -1) {
|
|
|
|
perror(snddevice->string);
|
|
|
|
Com_Printf("SNDDMA_Init: Sound driver too old.\n");
|
|
|
|
close(audio_fd);
|
|
|
|
audio_fd = -1;
|
|
|
|
return 0;
|
|
|
|
} else
|
|
|
|
mmapped = 1;
|
2001-12-22 04:27:19 +00:00
|
|
|
}
|
|
|
|
|
2002-02-20 04:38:42 +00:00
|
|
|
if (!(caps & DSP_CAP_TRIGGER) || !(caps & DSP_CAP_MMAP)) {
|
|
|
|
if (!mmapped) {
|
|
|
|
int frags = 2 << 16 | FRAGSIZEEXP;
|
|
|
|
|
|
|
|
if (ioctl(audio_fd, SNDCTL_DSP_SETFRAGMENT, &frags) == -1) {
|
|
|
|
perror(snddevice->string);
|
|
|
|
Com_Printf("SNDDMA_Init: Could not set sound fragments");
|
|
|
|
/* NOMMAP - jaq
|
2002-01-12 02:14:09 +00:00
|
|
|
Com_Printf("SNDDMA_Init: Sorry, but your soundcard doesn't support trigger or mmap. (%08x)\n", caps);
|
2002-02-20 04:38:42 +00:00
|
|
|
*/
|
|
|
|
close(audio_fd);
|
|
|
|
audio_fd = -1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
2001-12-22 04:27:19 +00:00
|
|
|
}
|
2002-02-20 04:38:42 +00:00
|
|
|
/* NOMMAP - jaq
|
2001-12-22 04:27:19 +00:00
|
|
|
if (ioctl(audio_fd, SNDCTL_DSP_GETOSPACE, &info)==-1)
|
|
|
|
{
|
|
|
|
perror("GETOSPACE");
|
2002-01-12 02:14:09 +00:00
|
|
|
Com_Printf("SNDDMA_Init: GETOSPACE ioctl failed.\n");
|
2001-12-22 04:27:19 +00:00
|
|
|
close(audio_fd);
|
2002-01-12 02:14:09 +00:00
|
|
|
audio_fd = -1;
|
2001-12-22 04:27:19 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2002-02-20 04:38:42 +00:00
|
|
|
*/
|
|
|
|
|
2001-12-22 04:27:19 +00:00
|
|
|
// set sample bits & speed
|
|
|
|
|
|
|
|
dma.samplebits = (int)sndbits->value;
|
|
|
|
if (dma.samplebits != 16 && dma.samplebits != 8)
|
|
|
|
{
|
|
|
|
ioctl(audio_fd, SNDCTL_DSP_GETFMTS, &fmt);
|
2002-02-20 04:38:42 +00:00
|
|
|
if (fmt & AFMT_S16_NE) dma.samplebits = 16;
|
2001-12-22 04:27:19 +00:00
|
|
|
else if (fmt & AFMT_U8) dma.samplebits = 8;
|
|
|
|
}
|
|
|
|
|
|
|
|
dma.speed = (int)sndspeed->value;
|
2002-01-12 02:14:09 +00:00
|
|
|
if (!dma.speed)
|
|
|
|
{
|
|
|
|
for (i=0 ; i<sizeof(tryrates)/4 ; i++)
|
|
|
|
if (!ioctl(audio_fd, SNDCTL_DSP_SPEED, &tryrates[i]))
|
|
|
|
break;
|
|
|
|
dma.speed = tryrates[i];
|
|
|
|
}
|
2001-12-22 04:27:19 +00:00
|
|
|
|
|
|
|
dma.channels = (int)sndchannels->value;
|
|
|
|
if (dma.channels < 1 || dma.channels > 2)
|
|
|
|
dma.channels = 2;
|
2002-01-12 02:14:09 +00:00
|
|
|
|
2002-02-20 04:38:42 +00:00
|
|
|
if (mmapped) {
|
|
|
|
dma.samples = info.fragstotal * info.fragsize / (dma.samplebits/8);
|
|
|
|
dma.submission_chunk = 1;
|
2001-12-22 04:27:19 +00:00
|
|
|
|
2002-02-20 04:38:42 +00:00
|
|
|
// memory map the dma buffer
|
2001-12-22 04:27:19 +00:00
|
|
|
|
2002-02-20 04:38:42 +00:00
|
|
|
if (!dma.buffer)
|
|
|
|
dma.buffer = (unsigned char *) mmap(NULL, info.fragstotal
|
2002-01-12 02:14:09 +00:00
|
|
|
* info.fragsize, PROT_WRITE, MAP_FILE|MAP_SHARED, audio_fd, 0);
|
2002-02-20 04:38:42 +00:00
|
|
|
if (!dma.buffer || dma.buffer == MAP_FAILED) {
|
|
|
|
perror(snddevice->string);
|
|
|
|
Com_Printf("SNDDMA_Init: Could not mmap %s.\n", snddevice->string);
|
|
|
|
close(audio_fd);
|
|
|
|
audio_fd = -1;
|
|
|
|
return 0;
|
|
|
|
}
|
2001-12-22 04:27:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
tmp = 0;
|
|
|
|
if (dma.channels == 2)
|
|
|
|
tmp = 1;
|
|
|
|
rc = ioctl(audio_fd, SNDCTL_DSP_STEREO, &tmp);
|
|
|
|
if (rc < 0)
|
|
|
|
{
|
|
|
|
perror(snddevice->string);
|
2002-01-12 02:14:09 +00:00
|
|
|
Com_Printf("SNDDMA_Init: Could not set %s to stereo=%d.", snddevice->string, dma.channels);
|
2001-12-22 04:27:19 +00:00
|
|
|
close(audio_fd);
|
2002-01-12 02:14:09 +00:00
|
|
|
audio_fd = -1;
|
2001-12-22 04:27:19 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2002-01-12 02:14:09 +00:00
|
|
|
|
2001-12-22 04:27:19 +00:00
|
|
|
if (tmp)
|
|
|
|
dma.channels = 2;
|
|
|
|
else
|
|
|
|
dma.channels = 1;
|
|
|
|
|
|
|
|
if (dma.samplebits == 16)
|
|
|
|
{
|
2002-02-20 04:38:42 +00:00
|
|
|
rc = AFMT_S16_NE;
|
2001-12-22 04:27:19 +00:00
|
|
|
rc = ioctl(audio_fd, SNDCTL_DSP_SETFMT, &rc);
|
|
|
|
if (rc < 0)
|
|
|
|
{
|
|
|
|
perror(snddevice->string);
|
2002-01-12 02:14:09 +00:00
|
|
|
Com_Printf("SNDDMA_Init: Could not support 16-bit data. Try 8-bit.\n");
|
2001-12-22 04:27:19 +00:00
|
|
|
close(audio_fd);
|
2002-01-12 02:14:09 +00:00
|
|
|
audio_fd = -1;
|
2001-12-22 04:27:19 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (dma.samplebits == 8)
|
|
|
|
{
|
|
|
|
rc = AFMT_U8;
|
|
|
|
rc = ioctl(audio_fd, SNDCTL_DSP_SETFMT, &rc);
|
|
|
|
if (rc < 0)
|
|
|
|
{
|
|
|
|
perror(snddevice->string);
|
2002-01-12 02:14:09 +00:00
|
|
|
Com_Printf("SNDDMA_Init: Could not support 8-bit data.\n");
|
2001-12-22 04:27:19 +00:00
|
|
|
close(audio_fd);
|
2002-01-12 02:14:09 +00:00
|
|
|
audio_fd = -1;
|
2001-12-22 04:27:19 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
perror(snddevice->string);
|
2002-01-12 02:14:09 +00:00
|
|
|
Com_Printf("SNDDMA_Init: %d-bit sound not supported.", dma.samplebits);
|
2001-12-22 04:27:19 +00:00
|
|
|
close(audio_fd);
|
2002-01-12 02:14:09 +00:00
|
|
|
audio_fd = -1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
rc = ioctl(audio_fd, SNDCTL_DSP_SPEED, &dma.speed);
|
|
|
|
if (rc < 0)
|
|
|
|
{
|
|
|
|
perror(snddevice->string);
|
|
|
|
Com_Printf("SNDDMA_Init: Could not set %s speed to %d.", snddevice->string, dma.speed);
|
|
|
|
close(audio_fd);
|
|
|
|
audio_fd = -1;
|
2001-12-22 04:27:19 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// toggle the trigger & start her up
|
|
|
|
|
2002-02-20 04:38:42 +00:00
|
|
|
if (!mmapped) {
|
|
|
|
dma.submission_chunk = AUDIOBUFFERSIZE;
|
|
|
|
dma.samples = AUDIOBUFFERS * dma.submission_chunk * dma.channels;
|
|
|
|
dma.samplepos = 0;
|
|
|
|
|
|
|
|
if ((dma.buffer = malloc(dma.samples * (dma.samplebits/8))) == 0) {
|
|
|
|
perror(snddevice->string);
|
|
|
|
Com_Printf("Could not allocate shm->buffer");
|
|
|
|
close(audio_fd);
|
|
|
|
audio_fd = -1;
|
|
|
|
return 0;
|
|
|
|
}
|
2001-12-22 04:27:19 +00:00
|
|
|
|
2002-02-20 04:38:42 +00:00
|
|
|
snd_inited = 1;
|
|
|
|
|
|
|
|
pthread_create(&audio, 0L, thesound, 0L);
|
|
|
|
} else {
|
|
|
|
tmp = 0;
|
|
|
|
rc = ioctl(audio_fd, SNDCTL_DSP_SETTRIGGER, &tmp);
|
|
|
|
if (rc < 0) {
|
|
|
|
perror(snddevice->string);
|
|
|
|
Com_Printf("SNDDMA_Init: Could not toggle. (1)\n");
|
|
|
|
close(audio_fd);
|
|
|
|
audio_fd = -1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
tmp = PCM_ENABLE_OUTPUT;
|
|
|
|
rc = ioctl(audio_fd, SNDCTL_DSP_SETTRIGGER, &tmp);
|
|
|
|
if (rc < 0) {
|
|
|
|
perror(snddevice->string);
|
|
|
|
Com_Printf("SNDDMA_Init: Could not toggle. (2)\n");
|
|
|
|
close(audio_fd);
|
|
|
|
audio_fd = -1;
|
|
|
|
return 0;
|
|
|
|
}
|
2001-12-22 04:27:19 +00:00
|
|
|
|
2002-02-20 04:38:42 +00:00
|
|
|
dma.samplepos = 0;
|
|
|
|
snd_inited = 1;
|
|
|
|
}
|
2001-12-22 04:27:19 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int SNDDMA_GetDMAPos(void)
|
|
|
|
{
|
|
|
|
struct count_info count;
|
|
|
|
|
|
|
|
if (!snd_inited) return 0;
|
|
|
|
|
2002-02-20 04:38:42 +00:00
|
|
|
if (!mmapped)
|
|
|
|
return (frags_sent * FRAGSIZE) / (dma.samplebits/8) + FRAGSIZE/16;
|
|
|
|
|
2001-12-22 04:27:19 +00:00
|
|
|
if (ioctl(audio_fd, SNDCTL_DSP_GETOPTR, &count)==-1)
|
|
|
|
{
|
|
|
|
perror(snddevice->string);
|
2002-01-12 02:14:09 +00:00
|
|
|
Com_Printf("SNDDMA_GetDMAPos: GETOPTR failed.\n");
|
2001-12-22 04:27:19 +00:00
|
|
|
close(audio_fd);
|
2002-01-12 02:14:09 +00:00
|
|
|
audio_fd = -1;
|
2001-12-22 04:27:19 +00:00
|
|
|
snd_inited = 0;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
// dma.samplepos = (count.bytes / (dma.samplebits / 8)) & (dma.samples-1);
|
|
|
|
// fprintf(stderr, "%d \r", count.ptr);
|
|
|
|
dma.samplepos = count.ptr / (dma.samplebits / 8);
|
|
|
|
|
|
|
|
return dma.samplepos;
|
|
|
|
}
|
|
|
|
|
2002-02-20 04:38:42 +00:00
|
|
|
void SNDDMA_Shutdown(void) {
|
2001-12-22 04:27:19 +00:00
|
|
|
#if 0
|
|
|
|
if (snd_inited)
|
|
|
|
{
|
|
|
|
close(audio_fd);
|
2002-01-12 02:14:09 +00:00
|
|
|
audio_fd = -1;
|
2001-12-22 04:27:19 +00:00
|
|
|
snd_inited = 0;
|
|
|
|
}
|
|
|
|
#endif
|
2002-02-20 04:38:42 +00:00
|
|
|
if (snd_inited) {
|
|
|
|
if (!mmapped) {
|
|
|
|
snd_inited = 0L;
|
|
|
|
pthread_join(audio, 0L);
|
|
|
|
|
|
|
|
if (dma.buffer) {
|
|
|
|
free(dma.buffer);
|
|
|
|
dma.buffer = 0L;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* FIXME: use munmap() ? */
|
|
|
|
close(audio_fd);
|
|
|
|
audio_fd = -1;
|
|
|
|
snd_inited = 0;
|
|
|
|
}
|
2001-12-22 04:27:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
==============
|
|
|
|
SNDDMA_Submit
|
|
|
|
|
|
|
|
Send sound to device if buffer isn't really the dma buffer
|
|
|
|
===============
|
|
|
|
*/
|
|
|
|
void SNDDMA_Submit(void)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void SNDDMA_BeginPainting (void)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|