I've shoehorned a patch from Michael D�nzer that he wrote

for quakeforge a long time ago that allows sound playback on
machines that can't mmap /dev/dsp, like PowerPC machines.

The original patch was archived at:
http://www.geocrawler.com/mail/msg.php3?msg_id=4207733&list=856

The patch went in mostly smoothly, once I found where iD
kept their sound code.  Linux only at the moment, I haven't
bothered to look at snd_*.c in any of the other OS
directories.

It works for me, in that it doesn't interfere with mmapping
on my i386 machine, but I have no PPC machines to test that
it actually does what it's supposed to, so YMMV.
This commit is contained in:
Jamie Wilkinson 2002-02-20 04:38:42 +00:00
parent 55cf61d130
commit c64a5e828a
2 changed files with 125 additions and 56 deletions

View file

@ -62,7 +62,7 @@ endif
DEBUG_CFLAGS=$(BASE_CFLAGS) -g DEBUG_CFLAGS=$(BASE_CFLAGS) -g
LDFLAGS=-lm -ldl LDFLAGS=-lm -ldl -lpthread
SVGALDFLAGS=-lvga SVGALDFLAGS=-lvga

View file

@ -21,6 +21,7 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#include <fcntl.h> #include <fcntl.h>
#include <stdlib.h> #include <stdlib.h>
#include <sys/types.h> #include <sys/types.h>
#include <pthread.h>
#include <sys/ioctl.h> #include <sys/ioctl.h>
#include <sys/mman.h> #include <sys/mman.h>
#include <sys/shm.h> #include <sys/shm.h>
@ -31,8 +32,16 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#include "../client/client.h" #include "../client/client.h"
#include "../client/snd_loc.h" #include "../client/snd_loc.h"
int audio_fd = -1; #define FRAGSIZEEXP 9
int snd_inited; #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;
cvar_t *sndbits; cvar_t *sndbits;
cvar_t *sndspeed; cvar_t *sndspeed;
@ -41,6 +50,17 @@ cvar_t *snddevice;
static int tryrates[] = { 11025, 22051, 44100, 48000, 8000 }; static int tryrates[] = { 11025, 22051, 44100, 48000, 8000 };
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);
}
qboolean SNDDMA_Init(void) qboolean SNDDMA_Init(void)
{ {
int rc; int rc;
@ -64,7 +84,7 @@ qboolean SNDDMA_Init(void)
snddevice = Cvar_Get("snddevice", "/dev/dsp", CVAR_ARCHIVE); snddevice = Cvar_Get("snddevice", "/dev/dsp", CVAR_ARCHIVE);
} }
// open /dev/dsp, confirm capability to mmap, and get size of dma buffer // open /dev/dsp, check capability to mmap, and get size of dma buffer
if (audio_fd == -1) if (audio_fd == -1)
{ {
@ -92,23 +112,37 @@ qboolean SNDDMA_Init(void)
return 0; return 0;
} }
if (ioctl(audio_fd, SNDCTL_DSP_GETCAPS, &caps)==-1) 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); perror(snddevice->string);
Com_Printf("SNDDMA_Init: Sound driver too old.\n"); Com_Printf("SNDDMA_Init: Sound driver too old.\n");
close(audio_fd); close(audio_fd);
audio_fd = -1; audio_fd = -1;
return 0; return 0;
} else
mmapped = 1;
} }
if (!(caps & DSP_CAP_TRIGGER) || !(caps & DSP_CAP_MMAP)) 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
Com_Printf("SNDDMA_Init: Sorry, but your soundcard doesn't support trigger or mmap. (%08x)\n", caps); Com_Printf("SNDDMA_Init: Sorry, but your soundcard doesn't support trigger or mmap. (%08x)\n", caps);
*/
close(audio_fd); close(audio_fd);
audio_fd = -1; audio_fd = -1;
return 0; return 0;
} }
}
}
/* NOMMAP - jaq
if (ioctl(audio_fd, SNDCTL_DSP_GETOSPACE, &info)==-1) if (ioctl(audio_fd, SNDCTL_DSP_GETOSPACE, &info)==-1)
{ {
perror("GETOSPACE"); perror("GETOSPACE");
@ -117,6 +151,7 @@ qboolean SNDDMA_Init(void)
audio_fd = -1; audio_fd = -1;
return 0; return 0;
} }
*/
// set sample bits & speed // set sample bits & speed
@ -124,7 +159,7 @@ qboolean SNDDMA_Init(void)
if (dma.samplebits != 16 && dma.samplebits != 8) if (dma.samplebits != 16 && dma.samplebits != 8)
{ {
ioctl(audio_fd, SNDCTL_DSP_GETFMTS, &fmt); ioctl(audio_fd, SNDCTL_DSP_GETFMTS, &fmt);
if (fmt & AFMT_S16_LE) dma.samplebits = 16; if (fmt & AFMT_S16_NE) dma.samplebits = 16;
else if (fmt & AFMT_U8) dma.samplebits = 8; else if (fmt & AFMT_U8) dma.samplebits = 8;
} }
@ -141,6 +176,7 @@ qboolean SNDDMA_Init(void)
if (dma.channels < 1 || dma.channels > 2) if (dma.channels < 1 || dma.channels > 2)
dma.channels = 2; dma.channels = 2;
if (mmapped) {
dma.samples = info.fragstotal * info.fragsize / (dma.samplebits/8); dma.samples = info.fragstotal * info.fragsize / (dma.samplebits/8);
dma.submission_chunk = 1; dma.submission_chunk = 1;
@ -149,14 +185,14 @@ qboolean SNDDMA_Init(void)
if (!dma.buffer) if (!dma.buffer)
dma.buffer = (unsigned char *) mmap(NULL, info.fragstotal dma.buffer = (unsigned char *) mmap(NULL, info.fragstotal
* info.fragsize, PROT_WRITE, MAP_FILE|MAP_SHARED, audio_fd, 0); * info.fragsize, PROT_WRITE, MAP_FILE|MAP_SHARED, audio_fd, 0);
if (!dma.buffer || dma.buffer == MAP_FAILED) if (!dma.buffer || dma.buffer == MAP_FAILED) {
{
perror(snddevice->string); perror(snddevice->string);
Com_Printf("SNDDMA_Init: Could not mmap %s.\n", snddevice->string); Com_Printf("SNDDMA_Init: Could not mmap %s.\n", snddevice->string);
close(audio_fd); close(audio_fd);
audio_fd = -1; audio_fd = -1;
return 0; return 0;
} }
}
tmp = 0; tmp = 0;
if (dma.channels == 2) if (dma.channels == 2)
@ -178,7 +214,7 @@ qboolean SNDDMA_Init(void)
if (dma.samplebits == 16) if (dma.samplebits == 16)
{ {
rc = AFMT_S16_LE; rc = AFMT_S16_NE;
rc = ioctl(audio_fd, SNDCTL_DSP_SETFMT, &rc); rc = ioctl(audio_fd, SNDCTL_DSP_SETFMT, &rc);
if (rc < 0) if (rc < 0)
{ {
@ -223,10 +259,26 @@ qboolean SNDDMA_Init(void)
// toggle the trigger & start her up // toggle the trigger & start her up
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;
}
snd_inited = 1;
pthread_create(&audio, 0L, thesound, 0L);
} else {
tmp = 0; tmp = 0;
rc = ioctl(audio_fd, SNDCTL_DSP_SETTRIGGER, &tmp); rc = ioctl(audio_fd, SNDCTL_DSP_SETTRIGGER, &tmp);
if (rc < 0) if (rc < 0) {
{
perror(snddevice->string); perror(snddevice->string);
Com_Printf("SNDDMA_Init: Could not toggle. (1)\n"); Com_Printf("SNDDMA_Init: Could not toggle. (1)\n");
close(audio_fd); close(audio_fd);
@ -235,8 +287,7 @@ qboolean SNDDMA_Init(void)
} }
tmp = PCM_ENABLE_OUTPUT; tmp = PCM_ENABLE_OUTPUT;
rc = ioctl(audio_fd, SNDCTL_DSP_SETTRIGGER, &tmp); rc = ioctl(audio_fd, SNDCTL_DSP_SETTRIGGER, &tmp);
if (rc < 0) if (rc < 0) {
{
perror(snddevice->string); perror(snddevice->string);
Com_Printf("SNDDMA_Init: Could not toggle. (2)\n"); Com_Printf("SNDDMA_Init: Could not toggle. (2)\n");
close(audio_fd); close(audio_fd);
@ -245,8 +296,8 @@ qboolean SNDDMA_Init(void)
} }
dma.samplepos = 0; dma.samplepos = 0;
snd_inited = 1; snd_inited = 1;
}
return 1; return 1;
} }
@ -256,6 +307,9 @@ int SNDDMA_GetDMAPos(void)
if (!snd_inited) return 0; if (!snd_inited) return 0;
if (!mmapped)
return (frags_sent * FRAGSIZE) / (dma.samplebits/8) + FRAGSIZE/16;
if (ioctl(audio_fd, SNDCTL_DSP_GETOPTR, &count)==-1) if (ioctl(audio_fd, SNDCTL_DSP_GETOPTR, &count)==-1)
{ {
perror(snddevice->string); perror(snddevice->string);
@ -272,8 +326,7 @@ int SNDDMA_GetDMAPos(void)
return dma.samplepos; return dma.samplepos;
} }
void SNDDMA_Shutdown(void) void SNDDMA_Shutdown(void) {
{
#if 0 #if 0
if (snd_inited) if (snd_inited)
{ {
@ -282,6 +335,22 @@ void SNDDMA_Shutdown(void)
snd_inited = 0; snd_inited = 0;
} }
#endif #endif
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;
}
} }
/* /*