2001-04-10 09:40:09 +00:00
|
|
|
/*
|
|
|
|
snd_mem.c
|
|
|
|
|
|
|
|
sound caching
|
|
|
|
|
|
|
|
Copyright (C) 1996-1997 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:
|
|
|
|
|
|
|
|
Free Software Foundation, Inc.
|
|
|
|
59 Temple Place - Suite 330
|
|
|
|
Boston, MA 02111-1307, USA
|
|
|
|
|
|
|
|
*/
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
# include "config.h"
|
|
|
|
#endif
|
2003-01-15 15:31:36 +00:00
|
|
|
|
|
|
|
static __attribute__ ((unused)) const char rcsid[] =
|
|
|
|
"$Id$";
|
|
|
|
|
2001-04-10 09:40:09 +00:00
|
|
|
#ifdef HAVE_STRING_H
|
|
|
|
# include <string.h>
|
|
|
|
#endif
|
|
|
|
#ifdef HAVE_STRINGS_H
|
|
|
|
# include <strings.h>
|
|
|
|
#endif
|
|
|
|
|
2001-05-31 03:41:35 +00:00
|
|
|
#include "QF/cvar.h"
|
2003-04-09 05:55:41 +00:00
|
|
|
#include "QF/dstring.h"
|
2001-04-10 21:45:42 +00:00
|
|
|
#include "QF/sound.h"
|
2001-04-10 09:40:09 +00:00
|
|
|
#include "QF/sys.h"
|
2002-08-27 07:16:28 +00:00
|
|
|
#include "QF/qendian.h"
|
|
|
|
#include "QF/quakefs.h"
|
2001-04-10 09:40:09 +00:00
|
|
|
|
2003-04-11 01:17:48 +00:00
|
|
|
#include "compat.h"
|
2003-01-31 20:51:04 +00:00
|
|
|
#include "snd_render.h"
|
|
|
|
|
2003-04-17 17:43:21 +00:00
|
|
|
#define SAMPLE_GAP 4
|
|
|
|
|
2003-04-11 18:59:14 +00:00
|
|
|
typedef struct {
|
|
|
|
byte left;
|
|
|
|
byte right;
|
|
|
|
} stereo8_t;
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
short left;
|
|
|
|
short right;
|
|
|
|
} stereo16_t;
|
|
|
|
|
2003-04-11 01:17:48 +00:00
|
|
|
static sfxbuffer_t *
|
|
|
|
snd_fail (sfx_t *sfx)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
snd_noop (sfx_t *sfx)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2003-04-20 07:19:51 +00:00
|
|
|
static sfx_t *
|
|
|
|
snd_open (sfx_t *sfx)
|
|
|
|
{
|
|
|
|
return sfx;
|
|
|
|
}
|
|
|
|
|
2003-04-11 18:59:14 +00:00
|
|
|
sfxbuffer_t *
|
|
|
|
SND_CacheTouch (sfx_t *sfx)
|
|
|
|
{
|
|
|
|
return Cache_Check (&((sfxblock_t *) sfx->data)->cache);
|
|
|
|
}
|
|
|
|
|
2003-04-11 01:17:48 +00:00
|
|
|
sfxbuffer_t *
|
|
|
|
SND_CacheRetain (sfx_t *sfx)
|
|
|
|
{
|
2003-04-11 02:21:25 +00:00
|
|
|
return Cache_TryGet (&((sfxblock_t *) sfx->data)->cache);
|
2003-04-11 01:17:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
SND_CacheRelease (sfx_t *sfx)
|
|
|
|
{
|
|
|
|
Cache_Release (&((sfxblock_t *) sfx->data)->cache);
|
|
|
|
}
|
|
|
|
|
|
|
|
sfxbuffer_t *
|
|
|
|
SND_StreamRetain (sfx_t *sfx)
|
|
|
|
{
|
|
|
|
return &((sfxstream_t *) sfx->data)->buffer;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
SND_StreamRelease (sfx_t *sfx)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2003-04-11 21:14:38 +00:00
|
|
|
wavinfo_t *
|
|
|
|
SND_CacheWavinfo (sfx_t *sfx)
|
|
|
|
{
|
|
|
|
return &((sfxblock_t *) sfx->data)->wavinfo;
|
|
|
|
}
|
|
|
|
|
|
|
|
wavinfo_t *
|
|
|
|
SND_StreamWavinfo (sfx_t *sfx)
|
|
|
|
{
|
|
|
|
return &((sfxstream_t *) sfx->data)->wavinfo;
|
|
|
|
}
|
|
|
|
|
2003-04-14 06:11:53 +00:00
|
|
|
static void
|
2003-04-17 17:43:21 +00:00
|
|
|
read_samples (sfxbuffer_t *buffer, int count, void *prev)
|
2003-04-14 06:11:53 +00:00
|
|
|
{
|
2003-04-17 17:43:21 +00:00
|
|
|
|
2003-04-14 06:11:53 +00:00
|
|
|
if (buffer->head + count > buffer->length) {
|
2003-04-17 17:43:21 +00:00
|
|
|
int s = (buffer->length - 1);
|
|
|
|
|
2003-04-14 06:11:53 +00:00
|
|
|
count -= buffer->length - buffer->head;
|
2003-04-17 17:43:21 +00:00
|
|
|
read_samples (buffer, buffer->length - buffer->head, prev);
|
|
|
|
prev = buffer->data + s * buffer->bps;
|
|
|
|
read_samples (buffer, count, prev);
|
2003-04-14 06:11:53 +00:00
|
|
|
} else {
|
|
|
|
byte *data;
|
|
|
|
float stepscale;
|
|
|
|
int samples, size;
|
|
|
|
sfx_t *sfx = buffer->sfx;
|
|
|
|
sfxstream_t *stream = (sfxstream_t *) sfx->data;
|
|
|
|
wavinfo_t *info = &stream->wavinfo;
|
|
|
|
|
|
|
|
stepscale = (float) info->rate / shm->speed; // usually 0.5, 1, or 2
|
|
|
|
|
|
|
|
samples = count * stepscale;
|
|
|
|
size = samples * info->width * info->channels;
|
|
|
|
data = alloca (size);
|
|
|
|
|
2003-04-15 21:11:41 +00:00
|
|
|
if (stream->read (stream->file, data, size, info) < size)
|
|
|
|
Sys_Printf ("%s\n", sfx->name);
|
2003-04-17 17:43:21 +00:00
|
|
|
stream->resample (buffer, data, samples, prev);
|
2003-04-14 06:11:53 +00:00
|
|
|
buffer->head += count;
|
|
|
|
if (buffer->head >= buffer->length)
|
|
|
|
buffer->head -= buffer->length;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-04-26 04:30:07 +00:00
|
|
|
static void
|
|
|
|
fill_buffer (sfx_t *sfx, sfxstream_t *stream, sfxbuffer_t *buffer,
|
|
|
|
wavinfo_t *info, unsigned int headpos)
|
|
|
|
{
|
|
|
|
void *prev;
|
|
|
|
unsigned int samples;
|
|
|
|
unsigned int loop_samples = 0;
|
|
|
|
|
|
|
|
// find out how many samples can be read into the buffer
|
|
|
|
samples = buffer->tail - buffer->head - SAMPLE_GAP;
|
|
|
|
if (buffer->tail <= buffer->head)
|
|
|
|
samples += buffer->length;
|
|
|
|
|
|
|
|
if (headpos + samples > sfx->length) {
|
|
|
|
if (sfx->loopstart == (unsigned int)-1) {
|
|
|
|
samples = sfx->length - headpos;
|
|
|
|
} else {
|
|
|
|
loop_samples = headpos + samples - sfx->length;
|
|
|
|
samples -= loop_samples;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (samples) {
|
|
|
|
if (buffer->head != buffer->tail) {
|
|
|
|
int s = buffer->head - 1;
|
|
|
|
if (!buffer->head)
|
|
|
|
s += buffer->length;
|
|
|
|
prev = buffer->data + s * buffer->bps;
|
|
|
|
} else {
|
|
|
|
prev = 0;
|
|
|
|
}
|
|
|
|
read_samples (buffer, samples, prev);
|
|
|
|
}
|
|
|
|
if (loop_samples) {
|
|
|
|
if (buffer->head != buffer->tail) {
|
|
|
|
int s = buffer->head - 1;
|
|
|
|
if (!buffer->head)
|
|
|
|
s += buffer->length;
|
|
|
|
prev = buffer->data + s * buffer->bps;
|
|
|
|
} else {
|
|
|
|
prev = 0;
|
|
|
|
}
|
|
|
|
stream->seek (stream->file, info->loopstart, info);
|
|
|
|
read_samples (buffer, loop_samples, prev);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
SND_StreamSetPos (sfxbuffer_t *buffer, unsigned int pos)
|
|
|
|
{
|
|
|
|
float stepscale;
|
|
|
|
sfx_t *sfx = buffer->sfx;
|
|
|
|
sfxstream_t *stream = (sfxstream_t *) sfx->data;
|
|
|
|
wavinfo_t *info = &stream->wavinfo;
|
|
|
|
|
|
|
|
stepscale = (float) info->rate / shm->speed; // usually 0.5, 1, or 2
|
|
|
|
|
|
|
|
buffer->head = buffer->tail = 0;
|
|
|
|
buffer->pos = pos;
|
|
|
|
stream->pos = pos;
|
|
|
|
stream->seek (stream->file, buffer->pos * stepscale, info);
|
|
|
|
fill_buffer (sfx, stream, buffer, info, pos);
|
|
|
|
}
|
|
|
|
|
2003-04-14 06:11:53 +00:00
|
|
|
void
|
2003-04-17 00:01:48 +00:00
|
|
|
SND_StreamAdvance (sfxbuffer_t *buffer, unsigned int count)
|
2003-04-14 06:11:53 +00:00
|
|
|
{
|
|
|
|
float stepscale;
|
2003-04-17 00:01:48 +00:00
|
|
|
unsigned int headpos, samples;
|
2003-04-14 06:11:53 +00:00
|
|
|
sfx_t *sfx = buffer->sfx;
|
|
|
|
sfxstream_t *stream = (sfxstream_t *) sfx->data;
|
|
|
|
wavinfo_t *info = &stream->wavinfo;
|
|
|
|
|
2003-04-20 03:42:19 +00:00
|
|
|
stream->pos += count;
|
|
|
|
count = (stream->pos - buffer->pos) & ~255;
|
|
|
|
if (!count)
|
|
|
|
return;
|
|
|
|
|
2003-04-14 06:11:53 +00:00
|
|
|
stepscale = (float) info->rate / shm->speed; // usually 0.5, 1, or 2
|
|
|
|
|
|
|
|
// find out how many samples the buffer currently holds
|
2003-04-17 02:40:17 +00:00
|
|
|
samples = buffer->head - buffer->tail;
|
|
|
|
if (buffer->head < buffer->tail)
|
|
|
|
samples += buffer->length;
|
2003-04-14 06:11:53 +00:00
|
|
|
|
|
|
|
// find out where head points to in the stream
|
|
|
|
headpos = buffer->pos + samples;
|
|
|
|
if (headpos >= sfx->length) {
|
2003-04-17 00:01:48 +00:00
|
|
|
if (sfx->loopstart == (unsigned int)-1)
|
2003-04-14 06:11:53 +00:00
|
|
|
headpos = sfx->length;
|
|
|
|
else
|
|
|
|
headpos -= sfx->length - sfx->loopstart;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (samples < count) {
|
|
|
|
buffer->head = buffer->tail = 0;
|
|
|
|
buffer->pos += count;
|
|
|
|
if (buffer->pos > sfx->length) {
|
2003-04-17 00:01:48 +00:00
|
|
|
if (sfx->loopstart == (unsigned int)-1) {
|
2003-04-14 06:11:53 +00:00
|
|
|
// reset the buffer and fill it incase it's needed again
|
2003-04-17 03:58:16 +00:00
|
|
|
buffer->pos = 0;
|
2003-04-14 06:11:53 +00:00
|
|
|
} else {
|
|
|
|
buffer->pos -= sfx->loopstart;
|
|
|
|
buffer->pos %= sfx->length - sfx->loopstart;
|
|
|
|
buffer->pos += sfx->loopstart;
|
|
|
|
}
|
2003-04-20 03:42:19 +00:00
|
|
|
stream->pos = buffer->pos;
|
2003-04-14 06:11:53 +00:00
|
|
|
}
|
2003-04-17 03:58:16 +00:00
|
|
|
headpos = buffer->pos;
|
2003-04-14 06:11:53 +00:00
|
|
|
stream->seek (stream->file, buffer->pos * stepscale, info);
|
|
|
|
} else {
|
|
|
|
buffer->pos += count;
|
|
|
|
if (buffer->pos >= sfx->length) {
|
2003-04-17 00:01:48 +00:00
|
|
|
if (sfx->loopstart == (unsigned int)-1) {
|
2003-04-14 06:11:53 +00:00
|
|
|
// reset the buffer and fill it incase it's needed again
|
2003-04-17 03:53:54 +00:00
|
|
|
headpos = buffer->pos = 0;
|
2003-04-14 06:11:53 +00:00
|
|
|
buffer->head = buffer->tail = 0;
|
|
|
|
count = 0;
|
2003-04-17 03:53:54 +00:00
|
|
|
stream->seek (stream->file, buffer->pos * stepscale, info);
|
2003-04-14 06:11:53 +00:00
|
|
|
} else {
|
|
|
|
buffer->pos -= sfx->length - sfx->loopstart;
|
|
|
|
}
|
2003-04-20 03:42:19 +00:00
|
|
|
stream->pos = buffer->pos;
|
2003-04-14 06:11:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
buffer->tail += count;
|
|
|
|
if (buffer->tail >= buffer->length)
|
|
|
|
buffer->tail -= buffer->length;
|
|
|
|
}
|
2003-04-26 04:30:07 +00:00
|
|
|
fill_buffer (sfx, stream, buffer, info, headpos);
|
2003-04-14 06:11:53 +00:00
|
|
|
}
|
|
|
|
|
2003-04-11 01:17:48 +00:00
|
|
|
void
|
|
|
|
SND_Load (sfx_t *sfx)
|
|
|
|
{
|
|
|
|
dstring_t *name = dstring_new ();
|
|
|
|
dstring_t *foundname = dstring_new ();
|
|
|
|
char *realname;
|
|
|
|
char buf[4];
|
|
|
|
QFile *file;
|
|
|
|
|
|
|
|
sfx->touch = sfx->retain = snd_fail;
|
|
|
|
sfx->release = snd_noop;
|
2003-04-20 07:19:51 +00:00
|
|
|
sfx->close = snd_noop;
|
|
|
|
sfx->open = snd_open;
|
2003-04-11 01:17:48 +00:00
|
|
|
|
|
|
|
dsprintf (name, "sound/%s", sfx->name);
|
|
|
|
_QFS_FOpenFile (name->str, &file, foundname, 1);
|
|
|
|
if (!file) {
|
|
|
|
Sys_Printf ("Couldn't load %s\n", name->str);
|
|
|
|
dstring_delete (name);
|
|
|
|
dstring_delete (foundname);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!strequal (foundname->str, name->str)) {
|
|
|
|
realname = foundname->str;
|
|
|
|
dstring_delete (name);
|
|
|
|
free (foundname);
|
|
|
|
} else {
|
|
|
|
realname = name->str;
|
|
|
|
free (name);
|
|
|
|
dstring_delete (foundname);
|
|
|
|
}
|
|
|
|
Qread (file, buf, 4);
|
|
|
|
Qseek (file, 0, SEEK_SET);
|
2003-04-11 14:38:31 +00:00
|
|
|
#ifdef HAVE_VORBIS
|
2003-04-11 01:17:48 +00:00
|
|
|
if (strnequal ("OggS", buf, 4)) {
|
2003-04-11 02:21:25 +00:00
|
|
|
Sys_DPrintf ("SND_Load: ogg file\n");
|
2003-04-11 01:17:48 +00:00
|
|
|
SND_LoadOgg (file, sfx, realname);
|
|
|
|
return;
|
|
|
|
}
|
2003-04-11 14:38:31 +00:00
|
|
|
#endif
|
2003-04-11 01:17:48 +00:00
|
|
|
if (strnequal ("RIFF", buf, 4)) {
|
2003-04-11 02:21:25 +00:00
|
|
|
Sys_DPrintf ("SND_Load: wav file\n");
|
2003-04-11 01:17:48 +00:00
|
|
|
SND_LoadWav (file, sfx, realname);
|
|
|
|
return;
|
|
|
|
}
|
2003-04-15 02:34:17 +00:00
|
|
|
Qclose (file);
|
2003-04-11 01:17:48 +00:00
|
|
|
free (realname);
|
|
|
|
}
|
2001-04-10 09:40:09 +00:00
|
|
|
|
2003-04-11 01:17:48 +00:00
|
|
|
sfxbuffer_t *
|
2002-06-15 05:43:56 +00:00
|
|
|
SND_GetCache (long samples, int rate, int inwidth, int channels,
|
2003-04-11 01:17:48 +00:00
|
|
|
sfxblock_t *block, cache_allocator_t allocator)
|
2002-06-15 05:43:56 +00:00
|
|
|
{
|
2003-04-14 01:40:40 +00:00
|
|
|
int len, size;
|
2002-06-15 05:43:56 +00:00
|
|
|
int width;
|
2002-06-17 00:26:13 +00:00
|
|
|
float stepscale;
|
2003-04-11 01:17:48 +00:00
|
|
|
sfxbuffer_t *sc;
|
|
|
|
sfx_t *sfx = block->sfx;
|
2002-06-15 05:43:56 +00:00
|
|
|
|
|
|
|
width = snd_loadas8bit->int_val ? 1 : 2;
|
2002-06-17 00:26:13 +00:00
|
|
|
stepscale = (float) rate / shm->speed; // usually 0.5, 1, or 2
|
2003-04-14 01:40:40 +00:00
|
|
|
len = size = samples / stepscale;
|
2003-04-11 23:52:18 +00:00
|
|
|
//printf ("%ld %d\n", samples, size);
|
2002-06-15 05:43:56 +00:00
|
|
|
size *= width * channels;
|
2003-04-11 01:17:48 +00:00
|
|
|
sc = allocator (&block->cache, sizeof (sfxbuffer_t) + size, sfx->name);
|
2002-06-15 05:43:56 +00:00
|
|
|
if (!sc)
|
|
|
|
return 0;
|
2003-04-14 06:11:53 +00:00
|
|
|
memset (sc, 0, sizeof (sfxbuffer_t) + size);
|
2003-04-14 01:40:40 +00:00
|
|
|
sc->length = len;
|
2003-04-11 21:14:38 +00:00
|
|
|
memcpy (sc->data + size, "\xde\xad\xbe\xef", 4);
|
2002-06-15 05:43:56 +00:00
|
|
|
return sc;
|
|
|
|
}
|
2001-08-22 20:26:25 +00:00
|
|
|
|
2001-04-10 09:40:09 +00:00
|
|
|
void
|
2003-04-17 17:43:21 +00:00
|
|
|
SND_ResampleMono (sfxbuffer_t *sc, byte *data, int length, void *prev)
|
2001-04-10 09:40:09 +00:00
|
|
|
{
|
2003-04-17 17:43:21 +00:00
|
|
|
byte *ib, *ob, *pb;
|
2001-09-09 21:41:35 +00:00
|
|
|
int fracstep, outcount, sample, samplefrac, srcsample, i;
|
|
|
|
float stepscale;
|
2003-04-17 17:43:21 +00:00
|
|
|
short *is, *os, *ps;
|
2003-04-11 21:14:38 +00:00
|
|
|
wavinfo_t *info = sc->sfx->wavinfo (sc->sfx);
|
|
|
|
int inwidth = info->width;
|
|
|
|
int inrate = info->rate;
|
2003-04-11 18:59:14 +00:00
|
|
|
int outwidth;
|
2003-04-17 17:43:21 +00:00
|
|
|
short zero_s[1];
|
|
|
|
byte zero_b[1];
|
2001-04-10 09:40:09 +00:00
|
|
|
|
|
|
|
is = (short *) data;
|
|
|
|
os = (short *) sc->data;
|
|
|
|
ib = data;
|
|
|
|
ob = sc->data;
|
|
|
|
|
2003-04-17 17:43:21 +00:00
|
|
|
ps = zero_s;
|
|
|
|
pb = zero_b;
|
|
|
|
|
|
|
|
if (!prev) {
|
|
|
|
zero_s[0] = 0;
|
|
|
|
zero_b[0] = 128;
|
|
|
|
}
|
|
|
|
|
2003-04-13 06:46:24 +00:00
|
|
|
os += sc->head;
|
|
|
|
ob += sc->head;
|
|
|
|
|
2001-08-22 20:26:25 +00:00
|
|
|
stepscale = (float) inrate / shm->speed; // usually 0.5, 1, or 2
|
2001-04-10 09:40:09 +00:00
|
|
|
|
2003-04-11 18:59:14 +00:00
|
|
|
outcount = length / stepscale;
|
2003-04-11 23:52:18 +00:00
|
|
|
//printf ("%d %d\n", length, outcount);
|
2003-04-11 21:14:38 +00:00
|
|
|
|
2003-04-13 06:46:24 +00:00
|
|
|
sc->sfx->length = info->samples / stepscale;
|
2003-04-17 00:01:48 +00:00
|
|
|
if (info->loopstart != (unsigned int)-1)
|
2003-04-11 21:14:38 +00:00
|
|
|
sc->sfx->loopstart = info->loopstart / stepscale;
|
2003-04-12 00:03:56 +00:00
|
|
|
else
|
2003-04-17 00:01:48 +00:00
|
|
|
sc->sfx->loopstart = (unsigned int)-1;
|
2001-04-10 09:40:09 +00:00
|
|
|
|
2003-04-11 18:59:14 +00:00
|
|
|
if (snd_loadas8bit->int_val) {
|
2003-04-17 17:43:21 +00:00
|
|
|
sc->bps = outwidth = 1;
|
2003-04-11 18:59:14 +00:00
|
|
|
sc->paint = SND_PaintChannelFrom8;
|
2003-04-17 17:43:21 +00:00
|
|
|
if (prev) {
|
|
|
|
zero_s[0] = ((char *)prev)[0];
|
|
|
|
zero_b[0] = ((char *)prev)[0] + 128;
|
|
|
|
}
|
2003-04-11 18:59:14 +00:00
|
|
|
} else {
|
2003-04-17 17:43:21 +00:00
|
|
|
sc->bps = outwidth = 2;
|
2003-04-11 18:59:14 +00:00
|
|
|
sc->paint = SND_PaintChannelFrom16;
|
2003-04-17 17:43:21 +00:00
|
|
|
if (prev) {
|
|
|
|
zero_s[0] = ((short *)prev)[0];
|
|
|
|
zero_b[0] = (((short *)prev)[0] >> 8) + 128;
|
|
|
|
}
|
2003-04-11 18:59:14 +00:00
|
|
|
}
|
2001-04-10 09:40:09 +00:00
|
|
|
|
2003-04-15 02:34:17 +00:00
|
|
|
if (!length)
|
|
|
|
return;
|
|
|
|
|
2001-04-10 09:40:09 +00:00
|
|
|
// resample / decimate to the current source rate
|
|
|
|
if (stepscale == 1) {
|
2003-04-11 18:59:14 +00:00
|
|
|
if (inwidth == 1 && outwidth == 1) {
|
2001-04-10 09:40:09 +00:00
|
|
|
for (i = 0; i < outcount; i++) {
|
|
|
|
*ob++ = *ib++ - 128;
|
|
|
|
}
|
2003-04-11 18:59:14 +00:00
|
|
|
} else if (inwidth == 1 && outwidth == 2) {
|
2001-04-10 09:40:09 +00:00
|
|
|
for (i = 0; i < outcount; i++) {
|
|
|
|
*os++ = (*ib++ - 128) << 8;
|
|
|
|
}
|
2003-04-11 18:59:14 +00:00
|
|
|
} else if (inwidth == 2 && outwidth == 1) {
|
2001-04-10 09:40:09 +00:00
|
|
|
for (i = 0; i < outcount; i++) {
|
|
|
|
*ob++ = LittleShort (*is++) >> 8;
|
|
|
|
}
|
2003-04-11 18:59:14 +00:00
|
|
|
} else if (inwidth == 2 && outwidth == 2) {
|
2001-04-10 09:40:09 +00:00
|
|
|
for (i = 0; i < outcount; i++) {
|
|
|
|
*os++ = LittleShort (*is++);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// general case
|
|
|
|
if (snd_interp->int_val && stepscale < 1) {
|
2001-09-09 21:41:35 +00:00
|
|
|
int j;
|
|
|
|
int points = 1 / stepscale;
|
2001-04-10 09:40:09 +00:00
|
|
|
|
2003-04-11 18:59:14 +00:00
|
|
|
for (i = 0; i < length; i++) {
|
2001-04-10 09:40:09 +00:00
|
|
|
int s1, s2;
|
|
|
|
|
|
|
|
if (inwidth == 2) {
|
2003-04-17 17:43:21 +00:00
|
|
|
s1 = LittleShort (*ps);
|
|
|
|
s2 = LittleShort (*is);
|
|
|
|
ps = is++;
|
2001-04-10 09:40:09 +00:00
|
|
|
} else {
|
2003-04-17 17:43:21 +00:00
|
|
|
s1 = (*pb - 128) << 8;
|
|
|
|
s2 = (*ib - 128) << 8;
|
|
|
|
pb = ib++;
|
2001-04-10 09:40:09 +00:00
|
|
|
}
|
|
|
|
for (j = 0; j < points; j++) {
|
|
|
|
sample = s1 + (s2 - s1) * ((float) j) / points;
|
2003-04-11 18:59:14 +00:00
|
|
|
if (outwidth == 2) {
|
2001-04-10 09:40:09 +00:00
|
|
|
os[j] = sample;
|
|
|
|
} else {
|
|
|
|
ob[j] = sample >> 8;
|
|
|
|
}
|
|
|
|
}
|
2003-04-11 18:59:14 +00:00
|
|
|
if (outwidth == 2) {
|
2001-04-10 09:40:09 +00:00
|
|
|
os += points;
|
|
|
|
} else {
|
|
|
|
ob += points;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
samplefrac = 0;
|
|
|
|
fracstep = stepscale * 256;
|
|
|
|
for (i = 0; i < outcount; i++) {
|
|
|
|
srcsample = samplefrac >> 8;
|
|
|
|
samplefrac += fracstep;
|
|
|
|
if (inwidth == 2)
|
2003-04-11 18:59:14 +00:00
|
|
|
sample = LittleShort (is[srcsample]);
|
2001-04-10 09:40:09 +00:00
|
|
|
else
|
2003-04-11 18:59:14 +00:00
|
|
|
sample = (ib[srcsample] - 128) << 8;
|
|
|
|
if (outwidth == 2)
|
|
|
|
os[i] = sample;
|
2001-04-10 09:40:09 +00:00
|
|
|
else
|
2003-04-11 18:59:14 +00:00
|
|
|
ob[i] = sample >> 8;
|
2001-04-10 09:40:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2003-04-11 21:14:38 +00:00
|
|
|
{
|
2003-04-13 06:46:24 +00:00
|
|
|
byte *x = sc->data + sc->length * outwidth;
|
2003-04-11 21:14:38 +00:00
|
|
|
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]);
|
|
|
|
}
|
2003-04-11 18:59:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2003-04-17 17:43:21 +00:00
|
|
|
SND_ResampleStereo (sfxbuffer_t *sc, byte *data, int length, void *prev)
|
2003-04-11 18:59:14 +00:00
|
|
|
{
|
|
|
|
int fracstep, outcount, sl, sr, samplefrac, srcsample, i;
|
|
|
|
float stepscale;
|
2003-04-17 17:43:21 +00:00
|
|
|
stereo8_t *ib, *ob, *pb;
|
|
|
|
stereo16_t *is, *os, *ps;
|
2003-04-11 21:14:38 +00:00
|
|
|
wavinfo_t *info = sc->sfx->wavinfo (sc->sfx);
|
|
|
|
int inwidth = info->width;
|
|
|
|
int inrate = info->rate;
|
2003-04-11 18:59:14 +00:00
|
|
|
int outwidth;
|
2003-04-17 17:43:21 +00:00
|
|
|
stereo16_t zero_s;
|
|
|
|
stereo8_t zero_b;
|
2003-04-11 18:59:14 +00:00
|
|
|
|
|
|
|
is = (stereo16_t *) data;
|
|
|
|
os = (stereo16_t *) sc->data;
|
|
|
|
ib = (stereo8_t *) data;
|
|
|
|
ob = (stereo8_t *) sc->data;
|
|
|
|
|
2003-04-17 17:43:21 +00:00
|
|
|
ps = &zero_s;
|
|
|
|
pb = &zero_b;
|
|
|
|
|
|
|
|
if (!prev) {
|
|
|
|
zero_s.left = zero_s.right = 0;
|
|
|
|
zero_b.left = zero_b.right = 128;
|
|
|
|
}
|
|
|
|
|
2003-04-13 06:46:24 +00:00
|
|
|
os += sc->head;
|
|
|
|
ob += sc->head;
|
|
|
|
|
2003-04-11 18:59:14 +00:00
|
|
|
stepscale = (float) inrate / shm->speed; // usually 0.5, 1, or 2
|
|
|
|
|
|
|
|
outcount = length / stepscale;
|
|
|
|
|
2003-04-13 06:46:24 +00:00
|
|
|
sc->sfx->length = info->samples / stepscale;
|
2003-04-17 00:01:48 +00:00
|
|
|
if (info->loopstart != (unsigned int)-1)
|
2003-04-11 21:14:38 +00:00
|
|
|
sc->sfx->loopstart = info->loopstart / stepscale;
|
2003-04-12 00:03:56 +00:00
|
|
|
else
|
2003-04-17 00:01:48 +00:00
|
|
|
sc->sfx->loopstart = (unsigned int)-1;
|
2003-04-11 21:14:38 +00:00
|
|
|
|
2003-04-11 18:59:14 +00:00
|
|
|
if (snd_loadas8bit->int_val) {
|
|
|
|
outwidth = 1;
|
|
|
|
sc->paint = SND_PaintChannelStereo8;
|
2003-04-17 17:43:21 +00:00
|
|
|
sc->bps = 2;
|
|
|
|
if (prev) {
|
|
|
|
zero_s.left = ((char *)prev)[0];
|
|
|
|
zero_s.right = ((char *)prev)[1];
|
|
|
|
zero_b.left = ((char *)prev)[0] + 128;
|
|
|
|
zero_b.right = ((char *)prev)[1] + 128;
|
|
|
|
}
|
2003-04-11 18:59:14 +00:00
|
|
|
} else {
|
|
|
|
outwidth = 2;
|
|
|
|
sc->paint = SND_PaintChannelStereo16;
|
2003-04-17 17:43:21 +00:00
|
|
|
sc->bps = 4;
|
|
|
|
if (prev) {
|
|
|
|
zero_s.left = ((short *)prev)[0];
|
|
|
|
zero_s.right = ((short *)prev)[1];
|
|
|
|
zero_b.left = (((short *)prev)[0] >> 8) + 128;
|
|
|
|
zero_b.right = (((short *)prev)[1] >> 8) + 128;
|
|
|
|
}
|
2003-04-11 18:59:14 +00:00
|
|
|
}
|
|
|
|
|
2003-04-15 02:34:17 +00:00
|
|
|
if (!length)
|
|
|
|
return;
|
|
|
|
|
2003-04-11 18:59:14 +00:00
|
|
|
// resample / decimate to the current source rate
|
|
|
|
if (stepscale == 1) {
|
|
|
|
if (inwidth == 1 && outwidth == 1) {
|
|
|
|
for (i = 0; i < outcount; i++, ob++, ib++) {
|
|
|
|
ob->left = ib->left - 128;
|
|
|
|
ob->right = ib->right - 128;
|
|
|
|
}
|
|
|
|
} else if (inwidth == 1 && outwidth == 2) {
|
|
|
|
for (i = 0; i < outcount; i++, os++, ib++) {
|
|
|
|
os->left = (ib->left - 128) << 8;
|
|
|
|
os->right = (ib->right - 128) << 8;
|
|
|
|
}
|
|
|
|
} else if (inwidth == 2 && outwidth == 1) {
|
|
|
|
for (i = 0; i < outcount; i++, ob++, ib++) {
|
|
|
|
ob->left = LittleShort (is->left) >> 8;
|
|
|
|
ob->right = LittleShort (is->right) >> 8;
|
|
|
|
}
|
|
|
|
} else if (inwidth == 2 && outwidth == 2) {
|
|
|
|
for (i = 0; i < outcount; i++, os++, is++) {
|
|
|
|
os->left = LittleShort (is->left);
|
|
|
|
os->right = LittleShort (is->right);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// general case
|
|
|
|
if (snd_interp->int_val && stepscale < 1) {
|
|
|
|
int j;
|
|
|
|
int points = 1 / stepscale;
|
|
|
|
|
|
|
|
for (i = 0; i < length; i++) {
|
|
|
|
int sl1, sl2;
|
|
|
|
int sr1, sr2;
|
2001-04-10 09:40:09 +00:00
|
|
|
|
2003-04-11 18:59:14 +00:00
|
|
|
if (inwidth == 2) {
|
2003-04-17 17:43:21 +00:00
|
|
|
sl1 = LittleShort (ps->left);
|
|
|
|
sr1 = LittleShort (ps->right);
|
|
|
|
sl2 = LittleShort (is->left);
|
|
|
|
sr2 = LittleShort (is->right);
|
|
|
|
ps = is++;
|
2003-04-11 18:59:14 +00:00
|
|
|
} else {
|
2003-04-17 17:43:21 +00:00
|
|
|
sl1 = (pb->left - 128) << 8;
|
|
|
|
sr1 = (pb->right - 128) << 8;
|
|
|
|
sl2 = (ib->left - 128) << 8;
|
|
|
|
sr2 = (ib->right - 128) << 8;
|
|
|
|
pb = ib++;
|
2003-04-11 18:59:14 +00:00
|
|
|
}
|
|
|
|
for (j = 0; j < points; j++) {
|
|
|
|
sl = sl1 + (sl2 - sl1) * ((float) j) / points;
|
|
|
|
sr = sr1 + (sr2 - sr1) * ((float) j) / points;
|
|
|
|
if (outwidth == 2) {
|
|
|
|
os[j].left = sl;
|
|
|
|
os[j].right = sr;
|
|
|
|
} else {
|
|
|
|
ob[j].left = sl >> 8;
|
|
|
|
ob[j].right = sr >> 8;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (outwidth == 2) {
|
|
|
|
os += points;
|
|
|
|
} else {
|
|
|
|
ob += points;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
samplefrac = 0;
|
|
|
|
fracstep = stepscale * 256;
|
|
|
|
for (i = 0; i < outcount; i++) {
|
|
|
|
srcsample = samplefrac >> 8;
|
|
|
|
samplefrac += fracstep;
|
|
|
|
if (inwidth == 2) {
|
|
|
|
sl = LittleShort (is[srcsample].left);
|
|
|
|
sr = LittleShort (is[srcsample].right);
|
|
|
|
} else {
|
|
|
|
sl = (ib[srcsample].left - 128) << 8;
|
|
|
|
sr = (ib[srcsample].right - 128) << 8;
|
|
|
|
}
|
|
|
|
if (outwidth == 2) {
|
|
|
|
os[i].left = sl;
|
|
|
|
os[i].right = sr;
|
|
|
|
} else {
|
|
|
|
ob[i].left = sl >> 8;
|
|
|
|
ob[i].right = sr >> 8;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2003-04-15 02:34:17 +00:00
|
|
|
{
|
|
|
|
byte *x = sc->data + sc->length * outwidth * 2;
|
|
|
|
if (memcmp (x, "\xde\xad\xbe\xef", 4))
|
2003-04-15 04:03:02 +00:00
|
|
|
Sys_Error ("SND_ResampleStereo screwed the pooch %02x%02x%02x%02x",
|
2003-04-15 02:34:17 +00:00
|
|
|
x[0], x[1], x[2], x[3]);
|
|
|
|
}
|
2001-04-10 09:40:09 +00:00
|
|
|
}
|