fix shm properly :)

This commit is contained in:
Bill Currie 2004-01-08 03:46:11 +00:00
parent 4597b44334
commit ccfa9081b0
13 changed files with 232 additions and 250 deletions

View file

@ -34,7 +34,7 @@
/* /*
All sound plugins must export these functions All sound plugins must export these functions
*/ */
typedef qboolean (QFPLUGIN *P_S_O_Init) (void); typedef volatile struct dma_s *(QFPLUGIN *P_S_O_Init) (void);
typedef void (QFPLUGIN *P_S_O_Shutdown) (void); typedef void (QFPLUGIN *P_S_O_Shutdown) (void);
typedef int (QFPLUGIN *P_S_O_GetDMAPos) (void); typedef int (QFPLUGIN *P_S_O_GetDMAPos) (void);
typedef void (QFPLUGIN *P_S_O_Submit) (void); typedef void (QFPLUGIN *P_S_O_Submit) (void);

View file

@ -59,8 +59,7 @@ struct sfx_s
void (*release) (sfx_t *sfx); void (*release) (sfx_t *sfx);
}; };
typedef struct typedef struct dma_s {
{
qboolean gamealive; qboolean gamealive;
qboolean soundalive; qboolean soundalive;
qboolean splitbuffer; qboolean splitbuffer;
@ -119,8 +118,6 @@ extern vec3_t listener_origin;
extern vec3_t listener_forward; extern vec3_t listener_forward;
extern vec3_t listener_right; extern vec3_t listener_right;
extern vec3_t listener_up; extern vec3_t listener_up;
extern volatile dma_t *shm;
extern volatile dma_t sn;
extern vec_t sound_nominal_clip_dist; extern vec_t sound_nominal_clip_dist;
extern struct cvar_s *snd_loadas8bit; extern struct cvar_s *snd_loadas8bit;

View file

@ -132,6 +132,7 @@ void SND_PaintChannelFrom16 (channel_t *ch, sfxbuffer_t *sc, int count);
void SND_PaintChannelStereo8 (channel_t *ch, sfxbuffer_t *sc, int count); void SND_PaintChannelStereo8 (channel_t *ch, sfxbuffer_t *sc, int count);
void SND_PaintChannelStereo16 (channel_t *ch, sfxbuffer_t *sc, int count); void SND_PaintChannelStereo16 (channel_t *ch, sfxbuffer_t *sc, int count);
extern volatile dma_t *shm;
extern channel_t channels[MAX_CHANNELS]; extern channel_t channels[MAX_CHANNELS];
// 0 to MAX_DYNAMIC_CHANNELS-1 = normal entity sounds // 0 to MAX_DYNAMIC_CHANNELS-1 = normal entity sounds
// MAX_DYNAMIC_CHANNELS to MAX_DYNAMIC_CHANNELS + NUM_AMBIENTS -1 = water, etc // MAX_DYNAMIC_CHANNELS to MAX_DYNAMIC_CHANNELS + NUM_AMBIENTS -1 = water, etc

View file

@ -152,15 +152,13 @@ SND_SoundInfo_f (void)
static void static void
SND_Startup (void) SND_Startup (void)
{ {
int rc;
if (!snd_initialized) if (!snd_initialized)
return; return;
if (!fakedma) { if (!fakedma) {
rc = snd_output_funcs->pS_O_Init (); shm = snd_output_funcs->pS_O_Init ();
if (!rc) { if (!shm) {
Sys_Printf ("S_Startup: S_O_Init failed.\n"); Sys_Printf ("S_Startup: S_O_Init failed.\n");
sound_started = 0; sound_started = 0;
return; return;

View file

@ -107,7 +107,7 @@ SNDDMA_Init_Cvars (void)
static int SNDDMA_GetDMAPos (void); static int SNDDMA_GetDMAPos (void);
static qboolean static volatile dma_t *
SNDDMA_Init (void) SNDDMA_Init (void)
{ {
int err; int err;
@ -259,29 +259,28 @@ SNDDMA_Init (void)
goto error; goto error;
} }
shm = &sn; memset ((dma_t *) &sn, 0, sizeof (sn));
memset ((dma_t *) shm, 0, sizeof (*shm)); sn.splitbuffer = 0;
shm->splitbuffer = 0; sn.channels = stereo + 1;
shm->channels = stereo + 1;
qfsnd_pcm_hw_params_get_period_size (hw, (snd_pcm_uframes_t *) qfsnd_pcm_hw_params_get_period_size (hw, (snd_pcm_uframes_t *)
&shm->submission_chunk, 0); &sn.submission_chunk, 0);
// don't mix less than this // don't mix less than this
shm->samplepos = 0; // in mono samples sn.samplepos = 0; // in mono samples
shm->samplebits = bps; sn.samplebits = bps;
qfsnd_pcm_hw_params_get_buffer_size (hw, &buffer_size); // FIXME: check error return value qfsnd_pcm_hw_params_get_buffer_size (hw, &buffer_size); // FIXME: check error return value
shm->samples = buffer_size * shm->channels; // mono samples in buffer sn.samples = buffer_size * sn.channels; // mono samples in buffer
shm->speed = rate; sn.speed = rate;
SNDDMA_GetDMAPos (); //XXX sets shm->buffer SNDDMA_GetDMAPos (); //XXX sets sn.buffer
Sys_Printf ("%5d stereo\n", shm->channels - 1); Sys_Printf ("%5d stereo\n", sn.channels - 1);
Sys_Printf ("%5d samples\n", shm->samples); Sys_Printf ("%5d samples\n", sn.samples);
Sys_Printf ("%5d samplepos\n", shm->samplepos); Sys_Printf ("%5d samplepos\n", sn.samplepos);
Sys_Printf ("%5d samplebits\n", shm->samplebits); Sys_Printf ("%5d samplebits\n", sn.samplebits);
Sys_Printf ("%5d submission_chunk\n", shm->submission_chunk); Sys_Printf ("%5d submission_chunk\n", sn.submission_chunk);
Sys_Printf ("%5d speed\n", shm->speed); Sys_Printf ("%5d speed\n", sn.speed);
Sys_Printf ("0x%x dma buffer\n", (int) shm->buffer); Sys_Printf ("0x%x dma buffer\n", (int) sn.buffer);
snd_inited = 1; snd_inited = 1;
return 1; return &sn;
error: error:
qfsnd_pcm_close (pcm); qfsnd_pcm_close (pcm);
return 0; return 0;
@ -292,18 +291,18 @@ SNDDMA_GetDMAPos (void)
{ {
const snd_pcm_channel_area_t *areas; const snd_pcm_channel_area_t *areas;
snd_pcm_uframes_t offset; snd_pcm_uframes_t offset;
snd_pcm_uframes_t nframes = shm->samples/shm->channels; snd_pcm_uframes_t nframes = sn.samples/sn.channels;
if (!snd_inited) if (!snd_inited)
return 0; return 0;
qfsnd_pcm_avail_update (pcm); qfsnd_pcm_avail_update (pcm);
qfsnd_pcm_mmap_begin (pcm, &areas, &offset, &nframes); qfsnd_pcm_mmap_begin (pcm, &areas, &offset, &nframes);
offset *= shm->channels; offset *= sn.channels;
nframes *= shm->channels; nframes *= sn.channels;
shm->samplepos = offset; sn.samplepos = offset;
shm->buffer = areas->addr; //XXX FIXME there's an area per channel sn.buffer = areas->addr; //XXX FIXME there's an area per channel
return shm->samplepos; return sn.samplepos;
} }
static void static void
@ -333,7 +332,7 @@ SNDDMA_Submit (void)
if (snd_blocked) if (snd_blocked)
return; return;
nframes = count / shm->channels; nframes = count / sn.channels;
qfsnd_pcm_avail_update (pcm); qfsnd_pcm_avail_update (pcm);
qfsnd_pcm_mmap_begin (pcm, &areas, &offset, &nframes); qfsnd_pcm_mmap_begin (pcm, &areas, &offset, &nframes);

View file

@ -56,7 +56,7 @@ static __attribute__ ((unused)) const char rcsid[] =
static int snd_inited; static int snd_inited;
static QFile *snd_file; static QFile *snd_file;
static int snd_blocked = 0; static int snd_blocked = 0;
//FIXME static volatile dma_t sn; static volatile dma_t sn;
static plugin_t plugin_info; static plugin_t plugin_info;
static plugin_data_t plugin_info_data; static plugin_data_t plugin_info_data;
@ -68,45 +68,44 @@ static snd_output_funcs_t plugin_info_snd_output_funcs;
/* FIXME /* FIXME
static qboolean static volatile dma_t *
SNDDMA_Init (void) SNDDMA_Init (void)
{ {
shm = &sn; memset ((dma_t *) sn, 0, sizeof (sn));
memset ((dma_t *) shm, 0, sizeof (*shm)); sn.splitbuffer = 0;
shm->splitbuffer = 0; sn.channels = 2;
shm->channels = 2; sn.submission_chunk = 1; // don't mix less than this #
shm->submission_chunk = 1; // don't mix less than this # sn.samplepos = 0; // in mono samples
shm->samplepos = 0; // in mono samples sn.samplebits = 16;
shm->samplebits = 16; sn.samples = 16384; // mono samples in buffer
shm->samples = 16384; // mono samples in buffer sn.speed = 44100;
shm->speed = 44100; sn.buffer = malloc (sn.samples * sn.channels * sn.samplebits / 8);
shm->buffer = malloc (shm->samples * shm->channels * shm->samplebits / 8); if (!sn.buffer) {
if (!shm->buffer) {
Sys_Printf ("SNDDMA_Init: memory allocation failure\n"); Sys_Printf ("SNDDMA_Init: memory allocation failure\n");
return 0; return 0;
} }
Sys_Printf ("%5d stereo\n", shm->channels - 1); Sys_Printf ("%5d stereo\n", sn.channels - 1);
Sys_Printf ("%5d samples\n", shm->samples); Sys_Printf ("%5d samples\n", sn.samples);
Sys_Printf ("%5d samplepos\n", shm->samplepos); Sys_Printf ("%5d samplepos\n", sn.samplepos);
Sys_Printf ("%5d samplebits\n", shm->samplebits); Sys_Printf ("%5d samplebits\n", sn.samplebits);
Sys_Printf ("%5d submission_chunk\n", shm->submission_chunk); Sys_Printf ("%5d submission_chunk\n", sn.submission_chunk);
Sys_Printf ("%5d speed\n", shm->speed); Sys_Printf ("%5d speed\n", sn.speed);
Sys_Printf ("0x%x dma buffer\n", (int) shm->buffer); Sys_Printf ("0x%x dma buffer\n", (int) sn.buffer);
if (!(snd_file = Qopen ("qf.raw", "wb"))) if (!(snd_file = Qopen ("qf.raw", "wb")))
return 0; return 0;
snd_inited = 1; snd_inited = 1;
return 1; return &sn;
} }
*/ */
static int static int
SNDDMA_GetDMAPos (void) SNDDMA_GetDMAPos (void)
{ {
shm->samplepos = 0; sn.samplepos = 0;
return shm->samplepos; return sn.samplepos;
} }
static void static void
@ -115,7 +114,7 @@ SNDDMA_Shutdown (void)
if (snd_inited) { if (snd_inited) {
Qclose (snd_file); Qclose (snd_file);
snd_file = 0; snd_file = 0;
free (shm->buffer); free (sn.buffer);
snd_inited = 0; snd_inited = 0;
} }
} }
@ -130,12 +129,12 @@ SNDDMA_Submit (void)
{ {
int count = ((*plugin_info_snd_output_data.paintedtime - int count = ((*plugin_info_snd_output_data.paintedtime -
*plugin_info_snd_output_data.soundtime) * *plugin_info_snd_output_data.soundtime) *
shm->samplebits / 8); sn.samplebits / 8);
if (snd_blocked) if (snd_blocked)
return; return;
Qwrite (snd_file, shm->buffer, count); Qwrite (snd_file, sn.buffer, count);
} }
static void static void

View file

@ -156,17 +156,15 @@ SNDDMA_InitDirect (void)
memset ((void *) &sn, 0, sizeof (sn)); memset ((void *) &sn, 0, sizeof (sn));
shm = &sn; sn.channels = 2;
sn.samplebits = 16;
shm->channels = 2; sn.speed = 11025;
shm->samplebits = 16;
shm->speed = 11025;
memset (&format, 0, sizeof (format)); memset (&format, 0, sizeof (format));
format.wFormatTag = WAVE_FORMAT_PCM; format.wFormatTag = WAVE_FORMAT_PCM;
format.nChannels = shm->channels; format.nChannels = sn.channels;
format.wBitsPerSample = shm->samplebits; format.wBitsPerSample = sn.samplebits;
format.nSamplesPerSec = shm->speed; format.nSamplesPerSec = sn.speed;
format.nBlockAlign = format.nChannels * format.wBitsPerSample / 8; format.nBlockAlign = format.nChannels * format.wBitsPerSample / 8;
format.cbSize = 0; format.cbSize = 0;
format.nAvgBytesPerSec = format.nSamplesPerSec * format.nBlockAlign; format.nAvgBytesPerSec = format.nSamplesPerSec * format.nBlockAlign;
@ -256,9 +254,9 @@ SNDDMA_InitDirect (void)
return SIS_FAILURE; return SIS_FAILURE;
} }
shm->channels = format.nChannels; sn.channels = format.nChannels;
shm->samplebits = format.wBitsPerSample; sn.samplebits = format.wBitsPerSample;
shm->speed = format.nSamplesPerSec; sn.speed = format.nSamplesPerSec;
if (DS_OK != IDirectSound_GetCaps (pDSBuf, &dsbcaps)) { if (DS_OK != IDirectSound_GetCaps (pDSBuf, &dsbcaps)) {
Sys_Printf ("DS:GetCaps failed\n"); Sys_Printf ("DS:GetCaps failed\n");
@ -321,13 +319,13 @@ SNDDMA_InitDirect (void)
&dwWrite); &dwWrite);
IDirectSoundBuffer_Play (pDSBuf, 0, 0, DSBPLAY_LOOPING); IDirectSoundBuffer_Play (pDSBuf, 0, 0, DSBPLAY_LOOPING);
shm->soundalive = true; sn.soundalive = true;
shm->splitbuffer = false; sn.splitbuffer = false;
shm->samples = gSndBufSize / (shm->samplebits / 8); sn.samples = gSndBufSize / (sn.samplebits / 8);
shm->samplepos = 0; sn.samplepos = 0;
shm->submission_chunk = 1; sn.submission_chunk = 1;
shm->buffer = lpData; sn.buffer = lpData;
sample16 = (shm->samplebits / 8) - 1; sample16 = (sn.samplebits / 8) - 1;
dsound_init = true; dsound_init = true;
@ -341,7 +339,7 @@ SNDDMA_InitDirect (void)
Try to find a sound device to mix for. Try to find a sound device to mix for.
Returns false if nothing is found. Returns false if nothing is found.
*/ */
static qboolean static volatile dma_t *
SNDDMA_Init (void) SNDDMA_Init (void)
{ {
sndinitstat stat; sndinitstat stat;
@ -362,7 +360,7 @@ SNDDMA_Init (void)
} }
} }
return 1; return &sn;
} }
/* /*
@ -385,7 +383,7 @@ SNDDMA_GetDMAPos (void)
Sys_Printf ("DSOUND_LockBuffer fails!\n"); Sys_Printf ("DSOUND_LockBuffer fails!\n");
return -1; return -1;
} }
shm->buffer = (unsigned char *) pbuf; sn.buffer = (unsigned char *) pbuf;
mmtime.wType = TIME_SAMPLES; mmtime.wType = TIME_SAMPLES;
IDirectSoundBuffer_GetCurrentPosition (pDSBuf, &mmtime.u.sample, IDirectSoundBuffer_GetCurrentPosition (pDSBuf, &mmtime.u.sample,
&dwWrite); &dwWrite);
@ -393,7 +391,7 @@ SNDDMA_GetDMAPos (void)
s >>= sample16; s >>= sample16;
s &= (shm->samples - 1); s &= (sn.samples - 1);
return s; return s;
} }
@ -472,7 +470,7 @@ DSOUND_ClearBuffer (int clear)
// FIXME: this should be called with 2nd pbuf2 = NULL, dwsize =0 // FIXME: this should be called with 2nd pbuf2 = NULL, dwsize =0
pData = DSOUND_LockBuffer (true); pData = DSOUND_LockBuffer (true);
memset (pData, clear, shm->samples * shm->samplebits / 8); memset (pData, clear, sn.samples * sn.samplebits / 8);
DSOUND_LockBuffer (false); DSOUND_LockBuffer (false);
} }

View file

@ -131,17 +131,15 @@ SNDDMA_InitWav ( void )
snd_sent = 0; snd_sent = 0;
snd_completed = 0; snd_completed = 0;
shm = &sn; sn.channels = 2;
sn.samplebits = 16;
shm->channels = 2; sn.speed = 11025;
shm->samplebits = 16;
shm->speed = 11025;
memset(format, 0, sizeof(*format)); memset(format, 0, sizeof(*format));
format->wf.wFormatTag = WAVE_FORMAT_PCM; format->wf.wFormatTag = WAVE_FORMAT_PCM;
format->wf.nChannels = shm->channels; format->wf.nChannels = sn.channels;
format->wBitsPerSample = shm->samplebits; format->wBitsPerSample = sn.samplebits;
format->wf.nSamplesPerSec = shm->speed; format->wf.nSamplesPerSec = sn.speed;
format->wf.nBlockAlign = format->wf.nChannels format->wf.nBlockAlign = format->wf.nChannels
*format->wBitsPerSample / 8; *format->wBitsPerSample / 8;
format->wf.nAvgBytesPerSec = format->wf.nSamplesPerSec format->wf.nAvgBytesPerSec = format->wf.nSamplesPerSec
@ -200,13 +198,13 @@ SNDDMA_InitWav ( void )
lpWaveHdr[i].lpData = lpData + i*WAV_BUFFER_SIZE; lpWaveHdr[i].lpData = lpData + i*WAV_BUFFER_SIZE;
} }
shm->soundalive = true; sn.soundalive = true;
shm->splitbuffer = false; sn.splitbuffer = false;
shm->samples = gSndBufSize/(shm->samplebits/8); sn.samples = gSndBufSize/(sn.samplebits/8);
shm->samplepos = 0; sn.samplepos = 0;
shm->submission_chunk = 1; sn.submission_chunk = 1;
shm->buffer = (unsigned char *) lpData; sn.buffer = (unsigned char *) lpData;
sample16 = (shm->samplebits/8) - 1; sample16 = (sn.samplebits/8) - 1;
wav_init = true; wav_init = true;
@ -219,7 +217,7 @@ SNDDMA_InitWav ( void )
Try to find a sound device to mix for. Try to find a sound device to mix for.
Returns false if nothing is found. Returns false if nothing is found.
*/ */
static qboolean static volatile dma_t *
SNDDMA_Init ( void ) SNDDMA_Init ( void )
{ {
wav_init = 0; wav_init = 0;
@ -245,7 +243,7 @@ SNDDMA_Init ( void )
return 0; return 0;
} }
return 1; return &sn;
} }
/* /*
@ -266,7 +264,7 @@ SNDDMA_GetDMAPos ( void )
s >>= sample16; s >>= sample16;
s &= (shm->samples-1); s &= (sn.samples-1);
return s; return s;
} }

View file

@ -115,7 +115,7 @@ SNDDMA_Init_Cvars (void)
"mmaped io"); "mmaped io");
} }
static int static volatile dma_t *
try_open (int rw) try_open (int rw)
{ {
int caps, fmt, rc, tmp, i; int caps, fmt, rc, tmp, i;
@ -176,67 +176,66 @@ try_open (int rw)
return 0; return 0;
} }
shm = &sn; sn.splitbuffer = 0;
shm->splitbuffer = 0;
// set sample bits & speed // set sample bits & speed
shm->samplebits = snd_bits->int_val; sn.samplebits = snd_bits->int_val;
if (shm->samplebits != 16 && shm->samplebits != 8) { if (sn.samplebits != 16 && sn.samplebits != 8) {
ioctl (audio_fd, SNDCTL_DSP_GETFMTS, &fmt); ioctl (audio_fd, SNDCTL_DSP_GETFMTS, &fmt);
if (fmt & AFMT_S16_LE) { // little-endian 16-bit signed if (fmt & AFMT_S16_LE) { // little-endian 16-bit signed
shm->samplebits = 16; sn.samplebits = 16;
} else { } else {
if (fmt & AFMT_U8) { // unsigned 8-bit ulaw if (fmt & AFMT_U8) { // unsigned 8-bit ulaw
shm->samplebits = 8; sn.samplebits = 8;
} }
} }
} }
if (snd_rate->int_val) { if (snd_rate->int_val) {
shm->speed = snd_rate->int_val; sn.speed = snd_rate->int_val;
} else { } else {
for (i = 0; i < ((int) sizeof (tryrates) / 4); i++) for (i = 0; i < ((int) sizeof (tryrates) / 4); i++)
if (!ioctl (audio_fd, SNDCTL_DSP_SPEED, &tryrates[i])) if (!ioctl (audio_fd, SNDCTL_DSP_SPEED, &tryrates[i]))
break; break;
shm->speed = tryrates[i]; sn.speed = tryrates[i];
} }
if (!snd_stereo->int_val) { if (!snd_stereo->int_val) {
shm->channels = 1; sn.channels = 1;
} else { } else {
shm->channels = 2; sn.channels = 2;
} }
shm->samples = info.fragstotal * info.fragsize / (shm->samplebits / 8); sn.samples = info.fragstotal * info.fragsize / (sn.samplebits / 8);
shm->submission_chunk = 1; sn.submission_chunk = 1;
tmp = 0; tmp = 0;
if (shm->channels == 2) if (sn.channels == 2)
tmp = 1; tmp = 1;
rc = ioctl (audio_fd, SNDCTL_DSP_STEREO, &tmp); rc = ioctl (audio_fd, SNDCTL_DSP_STEREO, &tmp);
if (rc < 0) { if (rc < 0) {
Sys_Printf ("Could not set %s to stereo=%d: %s\n", snd_dev, Sys_Printf ("Could not set %s to stereo=%d: %s\n", snd_dev,
shm->channels, strerror (errno)); sn.channels, strerror (errno));
close (audio_fd); close (audio_fd);
return 0; return 0;
} }
if (tmp) if (tmp)
shm->channels = 2; sn.channels = 2;
else else
shm->channels = 1; sn.channels = 1;
rc = ioctl (audio_fd, SNDCTL_DSP_SPEED, &shm->speed); rc = ioctl (audio_fd, SNDCTL_DSP_SPEED, &sn.speed);
if (rc < 0) { if (rc < 0) {
Sys_Printf ("Could not set %s speed to %d: %s\n", snd_dev, shm->speed, Sys_Printf ("Could not set %s speed to %d: %s\n", snd_dev, sn.speed,
strerror (errno)); strerror (errno));
close (audio_fd); close (audio_fd);
return 0; return 0;
} }
if (shm->samplebits == 16) { if (sn.samplebits == 16) {
rc = AFMT_S16_LE; rc = AFMT_S16_LE;
rc = ioctl (audio_fd, SNDCTL_DSP_SETFMT, &rc); rc = ioctl (audio_fd, SNDCTL_DSP_SETFMT, &rc);
if (rc < 0) { if (rc < 0) {
@ -245,7 +244,7 @@ try_open (int rw)
close (audio_fd); close (audio_fd);
return 0; return 0;
} }
} else if (shm->samplebits == 8) { } else if (sn.samplebits == 8) {
rc = AFMT_U8; rc = AFMT_U8;
rc = ioctl (audio_fd, SNDCTL_DSP_SETFMT, &rc); rc = ioctl (audio_fd, SNDCTL_DSP_SETFMT, &rc);
if (rc < 0) { if (rc < 0) {
@ -255,7 +254,7 @@ try_open (int rw)
return 0; return 0;
} }
} else { } else {
Sys_Printf ("%d-bit sound not supported. %s", shm->samplebits, Sys_Printf ("%d-bit sound not supported. %s", sn.samplebits,
strerror (errno)); strerror (errno));
close (audio_fd); close (audio_fd);
return 0; return 0;
@ -266,15 +265,15 @@ try_open (int rw)
unsigned long len = info.fragstotal * info.fragsize; unsigned long len = info.fragstotal * info.fragsize;
len = (len + sz - 1) & ~(sz - 1); len = (len + sz - 1) & ~(sz - 1);
shm->buffer = (byte *) mmap (NULL, len, mmmode, mmflags, audio_fd, 0); sn.buffer = (byte *) mmap (NULL, len, mmmode, mmflags, audio_fd, 0);
if (shm->buffer == MAP_FAILED) { if (sn.buffer == MAP_FAILED) {
Sys_Printf ("Could not mmap %s: %s\n", snd_dev, strerror (errno)); Sys_Printf ("Could not mmap %s: %s\n", snd_dev, strerror (errno));
close (audio_fd); close (audio_fd);
return 0; return 0;
} }
} else { } else {
shm->buffer = malloc (shm->samples * (shm->samplebits / 8) * 2); sn.buffer = malloc (sn.samples * (sn.samplebits / 8) * 2);
if (!shm->buffer) { if (!sn.buffer) {
Sys_Printf ("SNDDMA_Init: memory allocation failure\n"); Sys_Printf ("SNDDMA_Init: memory allocation failure\n");
close (audio_fd); close (audio_fd);
return 0; return 0;
@ -287,7 +286,7 @@ try_open (int rw)
if (rc < 0) { if (rc < 0) {
Sys_Printf ("Could not toggle.: %s\n", strerror (errno)); Sys_Printf ("Could not toggle.: %s\n", strerror (errno));
if (mmaped_io) if (mmaped_io)
munmap (shm->buffer, shm->samples * shm->samplebits / 8); munmap (sn.buffer, sn.samples * sn.samplebits / 8);
close (audio_fd); close (audio_fd);
return 0; return 0;
} }
@ -296,22 +295,23 @@ try_open (int rw)
if (rc < 0) { if (rc < 0) {
Sys_Printf ("Could not toggle.: %s\n", strerror (errno)); Sys_Printf ("Could not toggle.: %s\n", strerror (errno));
if (mmaped_io) if (mmaped_io)
munmap (shm->buffer, shm->samples * shm->samplebits / 8); munmap (sn.buffer, sn.samples * sn.samplebits / 8);
close (audio_fd); close (audio_fd);
return 0; return 0;
} }
shm->samplepos = 0; sn.samplepos = 0;
snd_inited = 1; snd_inited = 1;
return 1; return &sn;
} }
static qboolean static volatile dma_t *
SNDDMA_Init (void) SNDDMA_Init (void)
{ {
if (try_open (0)) volatile dma_t *shm;
return 1; if ((shm = try_open (0)))
return shm;
return try_open (1); return try_open (1);
} }
@ -326,16 +326,16 @@ SNDDMA_GetDMAPos (void)
if (ioctl (audio_fd, SNDCTL_DSP_GETOPTR, &count) == -1) { if (ioctl (audio_fd, SNDCTL_DSP_GETOPTR, &count) == -1) {
Sys_Printf ("Uh, %s dead: %s\n", snd_dev, strerror (errno)); Sys_Printf ("Uh, %s dead: %s\n", snd_dev, strerror (errno));
if (mmaped_io) if (mmaped_io)
munmap (shm->buffer, shm->samples * shm->samplebits / 8); munmap (sn.buffer, sn.samples * sn.samplebits / 8);
close (audio_fd); close (audio_fd);
snd_inited = 0; snd_inited = 0;
return 0; return 0;
} }
// shm->samplepos = (count.bytes / (shm->samplebits / 8)) & (shm->samples-1); // sn.samplepos = (count.bytes / (sn.samplebits / 8)) & (sn.samples-1);
// fprintf(stderr, "%d \r", count.ptr); // fprintf(stderr, "%d \r", count.ptr);
shm->samplepos = count.ptr / (shm->samplebits / 8); sn.samplepos = count.ptr / (sn.samplebits / 8);
return shm->samplepos; return sn.samplepos;
} }
@ -344,13 +344,13 @@ SNDDMA_Shutdown (void)
{ {
if (snd_inited) { if (snd_inited) {
if (mmaped_io) if (mmaped_io)
munmap (shm->buffer, shm->samples * shm->samplebits / 8); munmap (sn.buffer, sn.samples * sn.samplebits / 8);
close (audio_fd); close (audio_fd);
snd_inited = 0; snd_inited = 0;
} }
} }
#define BITSIZE (shm->samplebits / 8) #define BITSIZE (sn.samplebits / 8)
#define BYTES (samples / BITSIZE) #define BYTES (samples / BITSIZE)
/* /*
@ -367,13 +367,13 @@ SNDDMA_Submit (void)
samples = *plugin_info_snd_output_data.paintedtime samples = *plugin_info_snd_output_data.paintedtime
- *plugin_info_snd_output_data.soundtime; - *plugin_info_snd_output_data.soundtime;
if (shm->samplepos + BYTES <= shm->samples) if (sn.samplepos + BYTES <= sn.samples)
write (audio_fd, shm->buffer + BYTES, samples); write (audio_fd, sn.buffer + BYTES, samples);
else { else {
write (audio_fd, shm->buffer + BYTES, shm->samples - write (audio_fd, sn.buffer + BYTES, sn.samples -
shm->samplepos); sn.samplepos);
write (audio_fd, shm->buffer, BYTES - (shm->samples - write (audio_fd, sn.buffer, BYTES - (sn.samples -
shm->samplepos)); sn.samplepos));
} }
*plugin_info_snd_output_data.soundtime += samples; *plugin_info_snd_output_data.soundtime += samples;
} }

View file

@ -49,7 +49,7 @@ static __attribute__ ((unused)) const char rcsid[] =
#include "QF/sound.h" #include "QF/sound.h"
#include "QF/sys.h" #include "QF/sys.h"
static dma_t the_shm; static dma_t sn;
static int snd_inited; static int snd_inited;
static int snd_blocked = 0; static int snd_blocked = 0;
@ -70,26 +70,24 @@ paint_audio (void *unused, Uint8 * stream, int len)
{ {
int sampleposbytes, samplesbytes, streamsamples; int sampleposbytes, samplesbytes, streamsamples;
if (shm) { streamsamples = len / (sn.samplebits / 8);
streamsamples = len / (shm->samplebits / 8); sampleposbytes = sn.samplepos * (sn.samplebits / 8);
sampleposbytes = shm->samplepos * (shm->samplebits / 8); samplesbytes = sn.samples * (sn.samplebits / 8);
samplesbytes = shm->samples * (shm->samplebits / 8);
shm->samplepos += streamsamples; sn.samplepos += streamsamples;
while (shm->samplepos >= shm->samples) while (sn.samplepos >= sn.samples)
shm->samplepos -= shm->samples; sn.samplepos -= sn.samples;
// SND_PaintChannels (*plugin_info_snd_output_data.soundtime + streamsamples); // SND_PaintChannels (*plugin_info_snd_output_data.soundtime + streamsamples);
if (shm->samplepos + streamsamples <= shm->samples) if (sn.samplepos + streamsamples <= sn.samples)
memcpy (stream, shm->buffer + sampleposbytes, len); memcpy (stream, sn.buffer + sampleposbytes, len);
else { else {
memcpy (stream, shm->buffer + sampleposbytes, samplesbytes - memcpy (stream, sn.buffer + sampleposbytes, samplesbytes -
sampleposbytes); sampleposbytes);
memcpy (stream + samplesbytes - sampleposbytes, shm->buffer, len - memcpy (stream + samplesbytes - sampleposbytes, sn.buffer, len -
(samplesbytes - sampleposbytes)); (samplesbytes - sampleposbytes));
}
*plugin_info_snd_output_data.soundtime += streamsamples;
} }
*plugin_info_snd_output_data.soundtime += streamsamples;
} }
static void static void
@ -97,7 +95,7 @@ SNDDMA_Init_Cvars (void)
{ {
} }
static qboolean static volatile dma_t *
SNDDMA_Init (void) SNDDMA_Init (void)
{ {
SDL_AudioSpec desired, obtained; SDL_AudioSpec desired, obtained;
@ -164,28 +162,27 @@ SNDDMA_Init (void)
SDL_PauseAudio (0); SDL_PauseAudio (0);
/* Fill the audio DMA information block */ /* Fill the audio DMA information block */
shm = &the_shm; sn.splitbuffer = 0;
shm->splitbuffer = 0; sn.samplebits = (obtained.format & 0xFF);
shm->samplebits = (obtained.format & 0xFF); sn.speed = obtained.freq;
shm->speed = obtained.freq; sn.channels = obtained.channels;
shm->channels = obtained.channels; sn.samples = obtained.samples * 16;
shm->samples = obtained.samples * 16; sn.samplepos = 0;
shm->samplepos = 0; sn.submission_chunk = 1;
shm->submission_chunk = 1; sn.buffer = calloc(sn.samples * (sn.samplebits / 8), 1);
shm->buffer = calloc(shm->samples * (shm->samplebits / 8), 1); if (!sn.buffer)
if (!shm->buffer)
{ {
Sys_Error ("Failed to allocate buffer for sound!"); Sys_Error ("Failed to allocate buffer for sound!");
} }
snd_inited = 1; snd_inited = 1;
return 1; return &sn;
} }
static int static int
SNDDMA_GetDMAPos (void) SNDDMA_GetDMAPos (void)
{ {
return shm->samplepos; return sn.samplepos;
} }
static void static void
@ -196,7 +193,6 @@ SNDDMA_Shutdown (void)
SDL_UnlockAudio (); SDL_UnlockAudio ();
SDL_CloseAudio (); SDL_CloseAudio ();
snd_inited = 0; snd_inited = 0;
shm = NULL;
} }
} }

View file

@ -64,7 +64,7 @@ static snd_output_data_t plugin_info_snd_output_data;
static snd_output_funcs_t plugin_info_snd_output_funcs; static snd_output_funcs_t plugin_info_snd_output_funcs;
static qboolean static volatile dma_t *
SNDDMA_Init (void) SNDDMA_Init (void)
{ {
char *s; char *s;
@ -79,8 +79,7 @@ SNDDMA_Init (void)
return 0; return 0;
} }
shm = &sn; sn.splitbuffer = 0;
shm->splitbuffer = 0;
/* get & probe settings */ /* get & probe settings */
/* sample format */ /* sample format */
@ -94,11 +93,11 @@ SNDDMA_Init (void)
/* sample bits */ /* sample bits */
s = getenv ("QUAKE_SOUND_SAMPLEBITS"); s = getenv ("QUAKE_SOUND_SAMPLEBITS");
if (s) if (s)
shm->samplebits = atoi (s); sn.samplebits = atoi (s);
else if ((i = COM_CheckParm ("-sndbits")) != 0) else if ((i = COM_CheckParm ("-sndbits")) != 0)
shm->samplebits = atoi (com_argv[i + 1]); sn.samplebits = atoi (com_argv[i + 1]);
if (shm->samplebits != 16 && shm->samplebits != 8) { if (sn.samplebits != 16 && sn.samplebits != 8) {
alpv.param = AL_WORDSIZE; alpv.param = AL_WORDSIZE;
if (alGetParams (AL_DEFAULT_OUTPUT, &alpv, 1) < 0) { if (alGetParams (AL_DEFAULT_OUTPUT, &alpv, 1) < 0) {
@ -108,10 +107,10 @@ SNDDMA_Init (void)
} }
if (alpv.value.i >= 16) { if (alpv.value.i >= 16) {
shm->samplebits = 16; sn.samplebits = 16;
} else { } else {
if (alpv.value.i >= 8) if (alpv.value.i >= 8)
shm->samplebits = 8; sn.samplebits = 8;
else { else {
Sys_Printf ("Sound disabled since interface " Sys_Printf ("Sound disabled since interface "
"doesn't even support 8 bit."); "doesn't even support 8 bit.");
@ -124,9 +123,9 @@ SNDDMA_Init (void)
/* sample rate */ /* sample rate */
s = getenv ("QUAKE_SOUND_SPEED"); s = getenv ("QUAKE_SOUND_SPEED");
if (s) if (s)
shm->speed = atoi (s); sn.speed = atoi (s);
else if ((i = COM_CheckParm ("-sndspeed")) != 0) else if ((i = COM_CheckParm ("-sndspeed")) != 0)
shm->speed = atoi (com_argv[i + 1]); sn.speed = atoi (com_argv[i + 1]);
else { else {
alpv.param = AL_RATE; alpv.param = AL_RATE;
@ -144,33 +143,33 @@ SNDDMA_Init (void)
return 0; return 0;
} }
shm->speed = tryrates[i]; sn.speed = tryrates[i];
} }
/* channels */ /* channels */
s = getenv ("QUAKE_SOUND_CHANNELS"); s = getenv ("QUAKE_SOUND_CHANNELS");
if (s) if (s)
shm->channels = atoi (s); sn.channels = atoi (s);
else if ((i = COM_CheckParm ("-sndmono")) != 0) else if ((i = COM_CheckParm ("-sndmono")) != 0)
shm->channels = 1; sn.channels = 1;
else if ((i = COM_CheckParm ("-sndstereo")) != 0) else if ((i = COM_CheckParm ("-sndstereo")) != 0)
shm->channels = 2; sn.channels = 2;
else else
shm->channels = 2; sn.channels = 2;
/* set 'em */ /* set 'em */
/* channels */ /* channels */
while (shm->channels > 0) { while (sn.channels > 0) {
if (alSetChannels (alc, shm->channels) < 0) { if (alSetChannels (alc, sn.channels) < 0) {
Sys_Printf ("Unable to set number of channels to %d, " Sys_Printf ("Unable to set number of channels to %d, "
"trying half\n", shm->channels); "trying half\n", sn.channels);
shm->channels /= 2; sn.channels /= 2;
} else } else
break; break;
} }
if (shm->channels <= 0) { if (sn.channels <= 0) {
Sys_Printf ("Sound disabled since interface doesn't even support 1 " Sys_Printf ("Sound disabled since interface doesn't even support 1 "
"channel\n"); "channel\n");
alFreeConfig (alc); alFreeConfig (alc);
@ -179,11 +178,11 @@ SNDDMA_Init (void)
/* sample rate */ /* sample rate */
alpv.param = AL_RATE; alpv.param = AL_RATE;
alpv.value.ll = alDoubleToFixed (shm->speed); alpv.value.ll = alDoubleToFixed (sn.speed);
if (alSetParams (AL_DEFAULT_OUTPUT, &alpv, 1) < 0) { if (alSetParams (AL_DEFAULT_OUTPUT, &alpv, 1) < 0) {
Sys_Printf ("Could not set samplerate of default output to %d: %s\n", Sys_Printf ("Could not set samplerate of default output to %d: %s\n",
shm->speed, alGetErrorString (oserror ())); sn.speed, alGetErrorString (oserror ()));
alFreeConfig (alc); alFreeConfig (alc);
return 0; return 0;
} }
@ -192,7 +191,7 @@ SNDDMA_Init (void)
frequency of 11025 use *huge* buffers since at least my indigo2 frequency of 11025 use *huge* buffers since at least my indigo2
has enough to do to get sound on the way anyway has enough to do to get sound on the way anyway
*/ */
bufsize = 32768 * (int) ((double) shm->speed / 11025.0); bufsize = 32768 * (int) ((double) sn.speed / 11025.0);
dma_buffer = malloc (bufsize); dma_buffer = malloc (bufsize);
@ -214,7 +213,7 @@ SNDDMA_Init (void)
} }
/* sample bits */ /* sample bits */
switch (shm->samplebits) { switch (sn.samplebits) {
case 24: case 24:
i = AL_SAMPLE_24; i = AL_SAMPLE_24;
break; break;
@ -230,7 +229,7 @@ SNDDMA_Init (void)
if (alSetWidth (alc, i) < 0) { if (alSetWidth (alc, i) < 0) {
Sys_Printf ("Could not set wordsize of default output to %d: %s\n", Sys_Printf ("Could not set wordsize of default output to %d: %s\n",
shm->samplebits, alGetErrorString (oserror ())); sn.samplebits, alGetErrorString (oserror ()));
free (write_buffer); free (write_buffer);
free (dma_buffer); free (dma_buffer);
alFreeConfig (alc); alFreeConfig (alc);
@ -248,16 +247,16 @@ SNDDMA_Init (void)
return 0; return 0;
} }
shm->soundalive = true; sn.soundalive = true;
shm->samples = bufsize / (shm->samplebits / 8); sn.samples = bufsize / (sn.samplebits / 8);
shm->samplepos = 0; sn.samplepos = 0;
shm->submission_chunk = 1; sn.submission_chunk = 1;
shm->buffer = dma_buffer; sn.buffer = dma_buffer;
framecount = 0; framecount = 0;
snd_inited = 1; snd_inited = 1;
return 1; return &sn;
} }
static void static void
@ -268,11 +267,11 @@ SNDDMA_Init_Cvars (void)
static int static int
SNDDMA_GetDMAPos (void) SNDDMA_GetDMAPos (void)
{ {
/* Sys_Printf("framecount: %d %d\n", (framecount * shm->channels) % /* Sys_Printf("framecount: %d %d\n", (framecount * sn.channels) %
shm->samples, alGetFilled(alp)); */ sn.samples, alGetFilled(alp)); */
shm->samplepos = ((framecount - alGetFilled (alp)) sn.samplepos = ((framecount - alGetFilled (alp))
* shm->channels) % shm->samples; * sn.channels) % sn.samples;
return shm->samplepos; return sn.samplepos;
} }
static void static void
@ -305,7 +304,7 @@ SNDDMA_Submit (void)
if (*plugin_info_snd_output_data.paintedtime < wbufp) if (*plugin_info_snd_output_data.paintedtime < wbufp)
wbufp = 0; // reset wbufp = 0; // reset
bsize = shm->channels * (shm->samplebits / 8); bsize = sn.channels * (sn.samplebits / 8);
bytes = (*plugin_info_snd_output_data.paintedtime - wbufp) * bsize; bytes = (*plugin_info_snd_output_data.paintedtime - wbufp) * bsize;
if (!bytes) if (!bytes)

View file

@ -75,7 +75,7 @@ static snd_output_data_t plugin_info_sound_data;
static snd_output_funcs_t plugin_info_sound_funcs; static snd_output_funcs_t plugin_info_sound_funcs;
static qboolean static volatile dma_t *
SNDDMA_Init (void) SNDDMA_Init (void)
{ {
if (snd_inited) { if (snd_inited) {
@ -83,8 +83,7 @@ SNDDMA_Init (void)
return 0; return 0;
} }
shm = &sn; sn.splitbuffer = 0;
shm->splitbuffer = 0;
audio_fd = open ("/dev/audio", O_WRONLY | O_NDELAY); audio_fd = open ("/dev/audio", O_WRONLY | O_NDELAY);
@ -112,7 +111,7 @@ SNDDMA_Init (void)
AUDIO_INITINFO (&info); AUDIO_INITINFO (&info);
shm->speed = 11025; sn.speed = 11025;
// try 16 bit stereo // try 16 bit stereo
info.play.encoding = AUDIO_ENCODING_LINEAR; info.play.encoding = AUDIO_ENCODING_LINEAR;
@ -131,23 +130,23 @@ SNDDMA_Init (void)
return 0; return 0;
} }
Sys_Printf ("16 bit mono sound initialized\n"); Sys_Printf ("16 bit mono sound initialized\n");
shm->samplebits = 16; sn.samplebits = 16;
shm->channels = 1; sn.channels = 1;
} else { // 16 bit stereo } else { // 16 bit stereo
Sys_Printf ("16 bit stereo sound initialized\n"); Sys_Printf ("16 bit stereo sound initialized\n");
shm->samplebits = 16; sn.samplebits = 16;
shm->channels = 2; sn.channels = 2;
} }
shm->soundalive = true; sn.soundalive = true;
shm->samples = sizeof (dma_buffer) / (shm->samplebits / 8); sn.samples = sizeof (dma_buffer) / (sn.samplebits / 8);
shm->samplepos = 0; sn.samplepos = 0;
shm->submission_chunk = 1; sn.submission_chunk = 1;
shm->buffer = (unsigned char *) dma_buffer; sn.buffer = (unsigned char *) dma_buffer;
snd_inited = 1; snd_inited = 1;
return 1; return &sn;
} }
static int static int
@ -164,7 +163,7 @@ SNDDMA_GetDMAPos (void)
return (0); return (0);
} }
return ((info.play.samples * shm->channels) % shm->samples); return ((info.play.samples * sn.channels) % sn.samples);
} }
static int static int
@ -212,7 +211,7 @@ SNDDMA_Submit (void)
if (*plugin_info_sound_data.paintedtime < wbufp) if (*plugin_info_sound_data.paintedtime < wbufp)
wbufp = 0; // reset wbufp = 0; // reset
bsize = shm->channels * (shm->samplebits / 8); bsize = sn.channels * (sn.samplebits / 8);
bytes = (*plugin_info_sound_data.paintedtime - wbufp) * bsize; bytes = (*plugin_info_sound_data.paintedtime - wbufp) * bsize;
if (!bytes) if (!bytes)

View file

@ -152,17 +152,15 @@ SNDDMA_InitWav (void)
snd_sent = 0; snd_sent = 0;
snd_completed = 0; snd_completed = 0;
shm = &sn; sn.channels = 2;
sn.samplebits = 16;
shm->channels = 2; sn.speed = 11025;
shm->samplebits = 16;
shm->speed = 11025;
memset (&format, 0, sizeof (format)); memset (&format, 0, sizeof (format));
format.wFormatTag = WAVE_FORMAT_PCM; format.wFormatTag = WAVE_FORMAT_PCM;
format.nChannels = shm->channels; format.nChannels = sn.channels;
format.wBitsPerSample = shm->samplebits; format.wBitsPerSample = sn.samplebits;
format.nSamplesPerSec = shm->speed; format.nSamplesPerSec = sn.speed;
format.nBlockAlign = format.nChannels * format.wBitsPerSample / 8; format.nBlockAlign = format.nChannels * format.wBitsPerSample / 8;
format.cbSize = 0; format.cbSize = 0;
format.nAvgBytesPerSec = format.nSamplesPerSec * format.nBlockAlign; format.nAvgBytesPerSec = format.nSamplesPerSec * format.nBlockAlign;
@ -236,13 +234,13 @@ SNDDMA_InitWav (void)
} }
} }
shm->soundalive = true; sn.soundalive = true;
shm->splitbuffer = false; sn.splitbuffer = false;
shm->samples = gSndBufSize / (shm->samplebits / 8); sn.samples = gSndBufSize / (sn.samplebits / 8);
shm->samplepos = 0; sn.samplepos = 0;
shm->submission_chunk = 1; sn.submission_chunk = 1;
shm->buffer = (unsigned char *) lpData; sn.buffer = (unsigned char *) lpData;
sample16 = (shm->samplebits / 8) - 1; sample16 = (sn.samplebits / 8) - 1;
return true; return true;
} }
@ -253,7 +251,7 @@ SNDDMA_InitWav (void)
Try to find a sound device to mix for. Try to find a sound device to mix for.
Returns false if nothing is found. Returns false if nothing is found.
*/ */
static qboolean static volatile dma_t *
SNDDMA_Init (void) SNDDMA_Init (void)
{ {
if (snd_firsttime) { if (snd_firsttime) {
@ -265,7 +263,7 @@ SNDDMA_Init (void)
} }
snd_firsttime = false; snd_firsttime = false;
return 1; return &sn;
} }
/* /*
@ -284,7 +282,7 @@ SNDDMA_GetDMAPos (void)
s >>= sample16; s >>= sample16;
s &= (shm->samples - 1); s &= (sn.samples - 1);
return s; return s;
} }