2003-04-09 07:30:49 +00:00
|
|
|
/*
|
|
|
|
wav.c
|
|
|
|
|
|
|
|
wav file loading
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
#ifdef HAVE_STRING_H
|
|
|
|
# include <string.h>
|
|
|
|
#endif
|
|
|
|
#ifdef HAVE_STRINGS_H
|
|
|
|
# include <strings.h>
|
|
|
|
#endif
|
2003-04-11 04:39:51 +00:00
|
|
|
#include <stdlib.h>
|
2003-04-09 07:30:49 +00:00
|
|
|
|
2013-01-22 05:09:41 +00:00
|
|
|
#include "qfalloca.h"
|
|
|
|
|
2003-04-09 07:30:49 +00:00
|
|
|
#include "QF/cvar.h"
|
|
|
|
#include "QF/sound.h"
|
|
|
|
#include "QF/sys.h"
|
|
|
|
#include "QF/qendian.h"
|
|
|
|
#include "QF/quakefs.h"
|
2003-04-11 01:17:48 +00:00
|
|
|
#include "QF/riff.h"
|
2003-04-09 07:30:49 +00:00
|
|
|
|
2011-07-23 06:56:22 +00:00
|
|
|
#include "snd_internal.h"
|
2003-04-09 07:30:49 +00:00
|
|
|
|
2010-08-11 23:47:03 +00:00
|
|
|
#define FRAMES 1024
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
float *data;
|
|
|
|
QFile *file;
|
|
|
|
} wav_file_t;
|
|
|
|
|
2003-04-11 04:39:51 +00:00
|
|
|
static void
|
|
|
|
wav_callback_load (void *object, cache_allocator_t allocator)
|
|
|
|
{
|
|
|
|
sfxblock_t *block = (sfxblock_t *) object;
|
|
|
|
sfx_t *sfx = block->sfx;
|
2003-04-11 21:14:38 +00:00
|
|
|
const char *name = (const char *) block->file;
|
2003-04-11 04:39:51 +00:00
|
|
|
QFile *file;
|
2013-01-19 07:09:32 +00:00
|
|
|
int len, fdata_ofs;
|
2003-04-11 04:39:51 +00:00
|
|
|
byte *data;
|
2010-08-11 23:44:34 +00:00
|
|
|
float *fdata;
|
2003-04-11 04:39:51 +00:00
|
|
|
sfxbuffer_t *buffer;
|
2003-04-11 21:14:38 +00:00
|
|
|
wavinfo_t *info = &block->wavinfo;
|
2003-04-11 04:39:51 +00:00
|
|
|
|
2014-01-23 02:57:57 +00:00
|
|
|
file = QFS_FOpenFile (name);
|
2003-04-11 04:39:51 +00:00
|
|
|
if (!file)
|
|
|
|
return; //FIXME Sys_Error?
|
|
|
|
|
2003-04-11 21:14:38 +00:00
|
|
|
Qseek (file, info->dataofs, SEEK_SET);
|
2013-01-19 07:09:32 +00:00
|
|
|
fdata_ofs = (info->datalen + sizeof (float) - 1) & ~(sizeof (float) - 1);
|
|
|
|
len = fdata_ofs + info->frames * info->channels * sizeof (float);
|
2010-08-11 23:44:34 +00:00
|
|
|
data = malloc (len);
|
2013-01-19 07:09:32 +00:00
|
|
|
fdata = (float *) (data + fdata_ofs);
|
2003-04-11 21:14:38 +00:00
|
|
|
Qread (file, data, info->datalen);
|
2003-04-11 04:39:51 +00:00
|
|
|
Qclose (file);
|
|
|
|
|
2010-08-11 23:44:34 +00:00
|
|
|
SND_Convert (data, fdata, info->frames, info->channels, info->width);
|
|
|
|
|
|
|
|
buffer = SND_GetCache (info->frames, info->rate,
|
2003-04-11 21:14:38 +00:00
|
|
|
info->channels, block, allocator);
|
|
|
|
buffer->sfx = sfx;
|
2007-03-26 11:44:52 +00:00
|
|
|
SND_SetPaint (buffer);
|
2010-08-11 23:45:09 +00:00
|
|
|
SND_SetupResampler (buffer, 0);
|
2010-08-11 23:44:34 +00:00
|
|
|
SND_Resample (buffer, fdata, info->frames);
|
2003-04-14 01:40:40 +00:00
|
|
|
buffer->head = buffer->length;
|
2003-04-11 04:39:51 +00:00
|
|
|
free (data);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2003-04-14 06:11:53 +00:00
|
|
|
wav_cache (sfx_t *sfx, char *realname, void *file, wavinfo_t info)
|
2003-04-11 04:39:51 +00:00
|
|
|
{
|
2003-04-14 06:11:53 +00:00
|
|
|
Qclose (file);
|
2007-03-18 01:44:46 +00:00
|
|
|
SND_SFX_Cache (sfx, realname, info, wav_callback_load);
|
2003-04-14 06:11:53 +00:00
|
|
|
}
|
|
|
|
|
2010-08-11 23:47:03 +00:00
|
|
|
static long
|
|
|
|
wav_stream_read (void *file, float **buf)
|
2003-04-14 06:11:53 +00:00
|
|
|
{
|
2010-08-11 23:47:03 +00:00
|
|
|
sfxstream_t *stream = (sfxstream_t *) file;
|
|
|
|
wavinfo_t *info = &stream->wavinfo;
|
|
|
|
wav_file_t *wf = (wav_file_t *) stream->file;
|
2010-08-11 23:44:34 +00:00
|
|
|
int res;
|
2010-08-11 23:47:03 +00:00
|
|
|
int len = FRAMES * info->channels * info->width;
|
2010-08-11 23:44:34 +00:00
|
|
|
byte *data = alloca (len);
|
|
|
|
|
2010-08-11 23:47:03 +00:00
|
|
|
if (!wf->data)
|
|
|
|
wf->data = malloc (FRAMES * info->channels * sizeof (float));
|
|
|
|
|
|
|
|
res = Qread (wf->file, data, len);
|
2010-08-13 01:48:20 +00:00
|
|
|
if (res <= 0) {
|
|
|
|
stream->error = 1;
|
2010-08-13 01:48:48 +00:00
|
|
|
return 0;
|
2010-08-13 01:48:20 +00:00
|
|
|
}
|
2010-08-11 23:47:03 +00:00
|
|
|
res /= (info->channels * info->width);
|
|
|
|
SND_Convert (data, wf->data, res, info->channels, info->width);
|
|
|
|
*buf = wf->data;
|
2010-08-11 23:44:34 +00:00
|
|
|
return res;
|
2003-04-14 06:11:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2010-08-11 23:47:03 +00:00
|
|
|
wav_stream_seek (sfxstream_t *stream, int pos)
|
2003-04-14 06:11:53 +00:00
|
|
|
{
|
2010-08-11 23:47:03 +00:00
|
|
|
wavinfo_t *info = &stream->wavinfo;
|
|
|
|
wav_file_t *wf = (wav_file_t *) stream->file;
|
2003-04-14 06:11:53 +00:00
|
|
|
pos *= info->width * info->channels;
|
|
|
|
pos += info->dataofs;
|
2010-08-11 23:47:03 +00:00
|
|
|
return Qseek (wf->file, pos, SEEK_SET);
|
2003-04-14 06:11:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2003-04-20 07:19:51 +00:00
|
|
|
wav_stream_close (sfx_t *sfx)
|
2003-04-14 06:11:53 +00:00
|
|
|
{
|
2007-05-07 05:20:24 +00:00
|
|
|
sfxstream_t *stream = sfx->data.stream;
|
2010-08-11 23:47:03 +00:00
|
|
|
wav_file_t *wf = (wav_file_t *) stream->file;
|
2003-04-20 07:19:51 +00:00
|
|
|
|
2010-08-11 23:47:03 +00:00
|
|
|
Qclose (wf->file);
|
|
|
|
if (wf->data)
|
|
|
|
free (wf->data);
|
|
|
|
free (wf);
|
|
|
|
SND_SFX_StreamClose (sfx);
|
2003-04-20 07:19:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static sfx_t *
|
2007-03-18 01:44:46 +00:00
|
|
|
wav_stream_open (sfx_t *sfx)
|
2003-04-20 07:19:51 +00:00
|
|
|
{
|
2007-05-07 05:20:24 +00:00
|
|
|
sfxstream_t *stream = sfx->data.stream;
|
2003-04-20 07:19:51 +00:00
|
|
|
QFile *file;
|
2010-08-11 23:47:03 +00:00
|
|
|
wav_file_t *wf;
|
2003-04-14 06:11:53 +00:00
|
|
|
|
2014-01-23 02:57:57 +00:00
|
|
|
file = QFS_FOpenFile (stream->file);
|
2003-04-20 07:19:51 +00:00
|
|
|
if (!file)
|
|
|
|
return 0;
|
|
|
|
|
2010-08-11 23:47:03 +00:00
|
|
|
wf = calloc (sizeof (wav_file_t), 1);
|
|
|
|
wf->file = file;
|
|
|
|
return SND_SFX_StreamOpen (sfx, wf, wav_stream_read, wav_stream_seek,
|
2007-03-18 01:44:46 +00:00
|
|
|
wav_stream_close);
|
2003-04-20 07:19:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
wav_stream (sfx_t *sfx, char *realname, void *file, wavinfo_t info)
|
|
|
|
{
|
|
|
|
Qclose (file);
|
2007-03-18 01:44:46 +00:00
|
|
|
SND_SFX_Stream (sfx, realname, info, wav_stream_open);
|
2003-04-11 04:39:51 +00:00
|
|
|
}
|
2003-04-09 07:30:49 +00:00
|
|
|
|
2003-04-11 21:14:38 +00:00
|
|
|
static wavinfo_t
|
2010-08-11 23:41:04 +00:00
|
|
|
wav_get_info (QFile *file)
|
2003-04-09 07:30:49 +00:00
|
|
|
{
|
2003-04-11 01:17:48 +00:00
|
|
|
riff_t *riff;
|
|
|
|
riff_d_chunk_t **ck;
|
2003-04-09 07:30:49 +00:00
|
|
|
|
2003-04-11 01:17:48 +00:00
|
|
|
riff_format_t *fmt;
|
|
|
|
riff_d_format_t *dfmt = 0;
|
2003-04-09 07:30:49 +00:00
|
|
|
|
2003-04-11 01:17:48 +00:00
|
|
|
riff_data_t *data = 0;
|
2003-04-09 07:30:49 +00:00
|
|
|
|
2003-04-11 01:17:48 +00:00
|
|
|
riff_cue_t *cue;
|
|
|
|
riff_d_cue_t *dcue;
|
|
|
|
riff_d_cue_point_t *cp = 0;
|
2003-04-09 07:30:49 +00:00
|
|
|
|
2003-04-11 01:17:48 +00:00
|
|
|
riff_list_t *list;
|
|
|
|
riff_d_chunk_t **lck;
|
2003-04-09 07:30:49 +00:00
|
|
|
|
2003-04-11 01:17:48 +00:00
|
|
|
riff_ltxt_t *ltxt;
|
|
|
|
riff_d_ltxt_t *dltxt = 0;
|
2003-04-09 07:30:49 +00:00
|
|
|
|
2003-04-11 21:14:38 +00:00
|
|
|
wavinfo_t info;
|
2003-04-11 04:39:51 +00:00
|
|
|
|
2005-06-08 06:35:48 +00:00
|
|
|
memset (&info, 0, sizeof (info));
|
2003-04-09 07:30:49 +00:00
|
|
|
|
2003-04-11 01:17:48 +00:00
|
|
|
if (!(riff = riff_read (file))) {
|
|
|
|
Sys_Printf ("bad riff file\n");
|
2003-04-11 21:14:38 +00:00
|
|
|
return info;
|
2003-04-09 07:30:49 +00:00
|
|
|
}
|
|
|
|
|
2003-04-11 01:17:48 +00:00
|
|
|
for (ck = riff->chunks; *ck; ck++) {
|
|
|
|
RIFF_SWITCH ((*ck)->name) {
|
|
|
|
case RIFF_CASE ('f','m','t',' '):
|
|
|
|
fmt = (riff_format_t *) *ck;
|
|
|
|
dfmt = (riff_d_format_t *) fmt->fdata;
|
|
|
|
break;
|
|
|
|
case RIFF_CASE ('d','a','t','a'):
|
|
|
|
data = (riff_data_t *) *ck;
|
|
|
|
break;
|
|
|
|
case RIFF_CASE ('c','u','e',' '):
|
|
|
|
cue = (riff_cue_t *) *ck;
|
|
|
|
dcue = cue->cue;
|
|
|
|
if (dcue->count)
|
|
|
|
cp = &dcue->cue_points[dcue->count - 1];
|
|
|
|
break;
|
|
|
|
case RIFF_CASE ('L','I','S','T'):
|
|
|
|
list = (riff_list_t *) *ck;
|
|
|
|
RIFF_SWITCH (list->name) {
|
|
|
|
case RIFF_CASE ('a','d','t','l'):
|
|
|
|
for (lck = list->chunks; *lck; lck++) {
|
|
|
|
RIFF_SWITCH ((*lck)->name) {
|
|
|
|
case RIFF_CASE ('l','t','x','t'):
|
2003-04-11 21:14:38 +00:00
|
|
|
ltxt = (riff_ltxt_t *) *lck;
|
2003-04-11 01:17:48 +00:00
|
|
|
dltxt = <xt->ltxt;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
2003-04-09 07:30:49 +00:00
|
|
|
}
|
|
|
|
}
|
2003-04-11 01:17:48 +00:00
|
|
|
if (!dfmt) {
|
|
|
|
Sys_Printf ("missing format chunk\n");
|
|
|
|
goto bail;
|
2003-04-09 07:30:49 +00:00
|
|
|
}
|
2003-04-11 04:39:51 +00:00
|
|
|
if (dfmt->format_tag != 1) {
|
2010-11-21 05:18:15 +00:00
|
|
|
Sys_Printf ("not Microsoft PCM\n");
|
2003-04-11 04:39:51 +00:00
|
|
|
goto bail;
|
|
|
|
}
|
2010-08-12 02:28:27 +00:00
|
|
|
if (dfmt->channels < 1 || dfmt->channels > 8) {
|
2003-04-11 04:39:51 +00:00
|
|
|
Sys_Printf ("unsupported channel count\n");
|
|
|
|
goto bail;
|
|
|
|
}
|
2010-11-21 05:18:15 +00:00
|
|
|
if (!data) {
|
|
|
|
Sys_Printf ("missing data chunk\n");
|
|
|
|
goto bail;
|
|
|
|
}
|
2012-05-21 23:23:22 +00:00
|
|
|
|
2003-04-11 21:14:38 +00:00
|
|
|
info.rate = dfmt->samples_per_sec;
|
|
|
|
info.width = dfmt->bits_per_sample / 8;
|
|
|
|
info.channels = dfmt->channels;
|
2010-08-11 23:44:34 +00:00
|
|
|
info.frames = 0;
|
2003-04-11 21:14:38 +00:00
|
|
|
if (cp) {
|
|
|
|
info.loopstart = cp->sample_offset;
|
|
|
|
if (dltxt)
|
2010-08-11 23:44:34 +00:00
|
|
|
info.frames = info.loopstart + dltxt->len;
|
2003-04-11 21:14:38 +00:00
|
|
|
} else {
|
|
|
|
info.loopstart = -1;
|
|
|
|
}
|
2010-08-11 23:44:34 +00:00
|
|
|
if (!info.frames)
|
|
|
|
info.frames = data->ck.len / (info.width * info.channels);
|
2003-04-11 21:14:38 +00:00
|
|
|
info.dataofs = *(int *)data->data;
|
|
|
|
info.datalen = data->ck.len;
|
|
|
|
|
|
|
|
bail:
|
|
|
|
riff_free (riff);
|
|
|
|
return info;
|
|
|
|
}
|
|
|
|
|
2010-08-08 03:02:55 +00:00
|
|
|
int
|
2003-04-11 21:14:38 +00:00
|
|
|
SND_LoadWav (QFile *file, sfx_t *sfx, char *realname)
|
|
|
|
{
|
|
|
|
wavinfo_t info;
|
|
|
|
|
2010-08-11 23:41:04 +00:00
|
|
|
info = wav_get_info (file);
|
2003-04-14 06:11:53 +00:00
|
|
|
if (!info.rate) {
|
2010-08-08 03:02:55 +00:00
|
|
|
return -1;
|
2003-04-14 06:11:53 +00:00
|
|
|
}
|
2003-04-11 04:39:51 +00:00
|
|
|
|
2010-08-11 23:44:34 +00:00
|
|
|
if (info.frames / info.rate < 3) {
|
2021-03-29 10:58:00 +00:00
|
|
|
Sys_MaskPrintf (SYS_dev, "cache %s\n", realname);
|
2003-04-14 06:11:53 +00:00
|
|
|
wav_cache (sfx, realname, file, info);
|
2003-04-11 04:39:51 +00:00
|
|
|
} else {
|
2021-03-29 10:58:00 +00:00
|
|
|
Sys_MaskPrintf (SYS_dev, "stream %s\n", realname);
|
2003-04-14 06:11:53 +00:00
|
|
|
wav_stream (sfx, realname, file, info);
|
2003-04-11 04:39:51 +00:00
|
|
|
}
|
2010-08-08 03:02:55 +00:00
|
|
|
return 0;
|
2003-04-09 07:30:49 +00:00
|
|
|
}
|