From da44c783fb8b2d03b615bad7c4e78e591f3dcdc1 Mon Sep 17 00:00:00 2001 From: Bill Currie Date: Tue, 15 Apr 2003 02:34:17 +0000 Subject: [PATCH] get the new sound system mostly working. just getting a bogus read count in the stream reader at the moment, otherwise it's working wonderfully :) --- include/alsa_funcs_list.h | 1 + libs/audio/renderer/snd_dma.c | 6 ++++++ libs/audio/renderer/snd_mem.c | 19 +++++++++++++++++++ libs/audio/renderer/snd_mix.c | 19 ++++++++++--------- libs/audio/renderer/vorbis.c | 7 +++++-- libs/audio/renderer/wav.c | 11 +++++++---- libs/audio/snd.c | 4 ++-- libs/audio/targets/snd_alsa_0_9.c | 1 + 8 files changed, 51 insertions(+), 17 deletions(-) diff --git a/include/alsa_funcs_list.h b/include/alsa_funcs_list.h index 6d778f4fa..745906981 100644 --- a/include/alsa_funcs_list.h +++ b/include/alsa_funcs_list.h @@ -46,6 +46,7 @@ QF_ALSA_NEED (snd_pcm_uframes_t, snd_pcm_hw_params_set_period_size_near, (snd_pc QF_ALSA_NEED (unsigned int, snd_pcm_hw_params_set_rate_near, (snd_pcm_t *pcm, snd_pcm_hw_params_t *params, unsigned int val, int *dir)) QF_ALSA_NEED (size_t, snd_pcm_hw_params_sizeof, (void)) QF_ALSA_NEED (int, snd_pcm_mmap_begin, (snd_pcm_t *pcm, const snd_pcm_channel_area_t **areas, snd_pcm_uframes_t *offset, snd_pcm_uframes_t *frames)) +QF_ALSA_NEED (int, snd_pcm_avail_update, (snd_pcm_t *pcm)) QF_ALSA_NEED (snd_pcm_sframes_t, snd_pcm_mmap_commit, (snd_pcm_t *pcm, snd_pcm_uframes_t offset, snd_pcm_uframes_t frames)) QF_ALSA_NEED (int, snd_pcm_open, (snd_pcm_t **pcm, const char *name, snd_pcm_stream_t stream, int mode)) QF_ALSA_NEED (int, snd_pcm_pause, (snd_pcm_t *pcm, int enable)) diff --git a/libs/audio/renderer/snd_dma.c b/libs/audio/renderer/snd_dma.c index 719ab49bf..912232bc3 100644 --- a/libs/audio/renderer/snd_dma.c +++ b/libs/audio/renderer/snd_dma.c @@ -529,11 +529,14 @@ SND_UpdateAmbientSounds (void) } } +extern double realtime; + static void SND_GetSoundtime (void) { int fullsamples, samplepos; static int buffers, oldsamplepos; + static double oldrealtime; fullsamples = shm->samples / shm->channels; @@ -557,6 +560,9 @@ SND_GetSoundtime (void) sound_delta = soundtime; soundtime = buffers * fullsamples + samplepos / shm->channels; sound_delta = soundtime - sound_delta; + + sound_delta = (int)((realtime - oldrealtime) * shm->speed); + oldrealtime = realtime; } static void diff --git a/libs/audio/renderer/snd_mem.c b/libs/audio/renderer/snd_mem.c index 84330ad56..ad8462d22 100644 --- a/libs/audio/renderer/snd_mem.c +++ b/libs/audio/renderer/snd_mem.c @@ -116,6 +116,7 @@ read_samples (sfxbuffer_t *buffer, int count) if (buffer->head + count > buffer->length) { count -= buffer->length - buffer->head; read_samples (buffer, buffer->length - buffer->head); + read_samples (buffer, count); } else { byte *data; float stepscale; @@ -124,6 +125,9 @@ read_samples (sfxbuffer_t *buffer, int count) sfxstream_t *stream = (sfxstream_t *) sfx->data; wavinfo_t *info = &stream->wavinfo; + if (count <= 0) + Sys_Error ("read_samples: count <= 0"); + stepscale = (float) info->rate / shm->speed; // usually 0.5, 1, or 2 samples = count * stepscale; @@ -162,6 +166,8 @@ SND_StreamAdvance (sfxbuffer_t *buffer, int count) headpos = sfx->length; else headpos -= sfx->length - sfx->loopstart; + if (headpos > sfx->length) + Sys_Error ("huh"); } if (samples < count) { @@ -261,6 +267,7 @@ SND_Load (sfx_t *sfx) SND_LoadWav (file, sfx, realname); return; } + Qclose (file); free (realname); } @@ -327,6 +334,9 @@ SND_ResampleMono (sfxbuffer_t *sc, byte *data, int length) sc->paint = SND_PaintChannelFrom16; } + if (!length) + return; + // resample / decimate to the current source rate if (stepscale == 1) { if (inwidth == 1 && outwidth == 1) { @@ -443,6 +453,9 @@ SND_ResampleStereo (sfxbuffer_t *sc, byte *data, int length) sc->paint = SND_PaintChannelStereo16; } + if (!length) + return; + // resample / decimate to the current source rate if (stepscale == 1) { if (inwidth == 1 && outwidth == 1) { @@ -533,4 +546,10 @@ SND_ResampleStereo (sfxbuffer_t *sc, byte *data, int length) } } } + { + byte *x = sc->data + sc->length * outwidth * 2; + if (memcmp (x, "\xde\xad\xbe\xef", 4)) + Sys_Error ("SND_ResampleMono screwed the pooch %02x%02x%02x%02x", + x[0], x[1], x[2], x[3]); + } } diff --git a/libs/audio/renderer/snd_mix.c b/libs/audio/renderer/snd_mix.c index 12e21795b..9ca4afec4 100644 --- a/libs/audio/renderer/snd_mix.c +++ b/libs/audio/renderer/snd_mix.c @@ -207,6 +207,10 @@ SND_PaintChannels (int endtime) ltime += count; } + + if (sc->advance) + sc->advance (sc, count); + // if at end of loop, restart if (ltime >= ch->end) { if (ch->sfx->loopstart >= 0) { @@ -220,9 +224,6 @@ SND_PaintChannels (int endtime) } } - if (sc->advance) - sc->advance (sc, sound_delta); - if (ch->sfx) ch->sfx->release (ch->sfx); } @@ -407,8 +408,8 @@ snd_paint_stereo_16 (int offs, channel_t *ch, void *bytes, int count) samp = (short *) bytes; pair = paintbuffer + offs; while (count-- > 0) { - pair->left += *samp++ * leftvol; - pair->right += *samp++ * rightvol; + pair->left += (*samp++ * leftvol) >> 8; + pair->right += (*samp++ * rightvol) >> 8; pair++; } } @@ -425,7 +426,7 @@ SND_PaintChannelFrom8 (channel_t *ch, sfxbuffer_t *sc, int count) if (pos + count > sc->length) { int sub = sc->length - pos; snd_paint_mono_8 (0, ch, samps, sub); - snd_paint_mono_8 (sub, ch, samps, count - sub); + snd_paint_mono_8 (sub, ch, sc->data, count - sub); } else { snd_paint_mono_8 (0, ch, samps, count); } @@ -444,7 +445,7 @@ SND_PaintChannelFrom16 (channel_t *ch, sfxbuffer_t *sc, int count) if (pos + count > sc->length) { int sub = sc->length - pos; snd_paint_mono_16 (0, ch, samps, sub); - snd_paint_mono_16 (sub, ch, samps, count - sub); + snd_paint_mono_16 (sub, ch, sc->data, count - sub); } else { snd_paint_mono_16 (0, ch, samps, count); } @@ -463,7 +464,7 @@ SND_PaintChannelStereo8 (channel_t *ch, sfxbuffer_t *sc, int count) if (pos + count > sc->length) { int sub = sc->length - pos; snd_paint_stereo_8 (0, ch, samps, sub); - snd_paint_stereo_8 (sub, ch, samps, count - sub); + snd_paint_stereo_8 (sub, ch, sc->data, count - sub); } else { snd_paint_stereo_8 (0, ch, samps, count); } @@ -482,7 +483,7 @@ SND_PaintChannelStereo16 (channel_t *ch, sfxbuffer_t *sc, int count) if (pos + count > sc->length) { int sub = sc->length - pos; snd_paint_stereo_16 (0, ch, samps, sub); - snd_paint_stereo_16 (sub, ch, samps, count - sub); + snd_paint_stereo_16 (sub, ch, sc->data, count - sub); } else { snd_paint_stereo_16 (0, ch, samps, count); } diff --git a/libs/audio/renderer/vorbis.c b/libs/audio/renderer/vorbis.c index f626231b2..aa527be9f 100644 --- a/libs/audio/renderer/vorbis.c +++ b/libs/audio/renderer/vorbis.c @@ -275,6 +275,9 @@ vorbis_stream (sfx_t *sfx, char *realname, OggVorbis_File *vf, wavinfo_t info) stream->buffer.advance = SND_StreamAdvance; stream->buffer.sfx = sfx; + stream->resample (&stream->buffer, 0, 0); // get sfx setup properly + stream->seek (stream->file, 0, &stream->wavinfo); + stream->buffer.advance (&stream->buffer, 0); } @@ -296,10 +299,10 @@ SND_LoadOgg (QFile *file, sfx_t *sfx, char *realname) return; } if (info.samples / info.rate < 3) { - printf ("cache %s\n", realname); + Sys_DPrintf ("cache %s\n", realname); vorbis_cache (sfx, realname, &vf, info); } else { - printf ("stream %s\n", realname); + Sys_DPrintf ("stream %s\n", realname); vorbis_stream (sfx, realname, &vf, info); } } diff --git a/libs/audio/renderer/wav.c b/libs/audio/renderer/wav.c index 00b0c4308..a6084db34 100644 --- a/libs/audio/renderer/wav.c +++ b/libs/audio/renderer/wav.c @@ -145,6 +145,9 @@ wav_stream (sfx_t *sfx, char *realname, void *file, wavinfo_t info) stream->buffer.advance = SND_StreamAdvance; stream->buffer.sfx = sfx; + stream->resample (&stream->buffer, 0, 0); // get sfx setup properly + stream->seek (stream->file, 0, &stream->wavinfo); + stream->buffer.advance (&stream->buffer, 0); } @@ -236,10 +239,10 @@ get_info (QFile *file) if (dltxt) info.samples = info.loopstart + dltxt->len; else - info.samples = data->ck.len / info.width; + info.samples = data->ck.len / (info.width * info.channels); } else { info.loopstart = -1; - info.samples = data->ck.len / info.width; + info.samples = data->ck.len / (info.width * info.channels); } info.dataofs = *(int *)data->data; info.datalen = data->ck.len; @@ -261,10 +264,10 @@ SND_LoadWav (QFile *file, sfx_t *sfx, char *realname) } if (info.samples / info.rate < 3) { - printf ("cache %s\n", realname); + Sys_DPrintf ("cache %s\n", realname); wav_cache (sfx, realname, file, info); } else { - printf ("stream %s\n", realname); + Sys_DPrintf ("stream %s\n", realname); wav_stream (sfx, realname, file, info); } } diff --git a/libs/audio/snd.c b/libs/audio/snd.c index e4f543507..3763c601c 100644 --- a/libs/audio/snd.c +++ b/libs/audio/snd.c @@ -238,8 +238,8 @@ S_EndPrecaching (void) void S_ExtraUpdate (void) { - if (snd_render_funcs) - snd_render_funcs->pS_ExtraUpdate (); + //if (snd_render_funcs) + // snd_render_funcs->pS_ExtraUpdate (); } void diff --git a/libs/audio/targets/snd_alsa_0_9.c b/libs/audio/targets/snd_alsa_0_9.c index 0a159e75b..20176df28 100644 --- a/libs/audio/targets/snd_alsa_0_9.c +++ b/libs/audio/targets/snd_alsa_0_9.c @@ -291,6 +291,7 @@ SNDDMA_GetDMAPos (void) if (!snd_inited) return 0; + qfsnd_pcm_avail_update (pcm); qfsnd_pcm_mmap_begin (pcm, &areas, &offset, &nframes); offset *= shm->channels; nframes *= shm->channels;