2002-06-14 20:36:28 +00:00
|
|
|
/*
|
|
|
|
vorbis.c
|
|
|
|
|
2003-04-14 06:11:53 +00:00
|
|
|
Ogg Vorbis support
|
2002-06-14 20:36:28 +00:00
|
|
|
|
|
|
|
Copyright (C) 2001 Bill Currie <bill@taniwha.org>
|
|
|
|
|
|
|
|
Author: Bill Currie <bill@taniwha.org>
|
|
|
|
Date: 2002/6/14
|
|
|
|
|
|
|
|
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$";
|
|
|
|
|
2003-04-11 14:38:31 +00:00
|
|
|
#ifdef HAVE_VORBIS
|
|
|
|
|
2002-06-15 05:43:56 +00:00
|
|
|
#ifdef HAVE_STRING_H
|
|
|
|
# include "string.h"
|
|
|
|
#endif
|
|
|
|
#ifdef HAVE_STRINGS_H
|
|
|
|
# include "strings.h"
|
|
|
|
#endif
|
2002-06-14 20:36:28 +00:00
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <vorbis/vorbisfile.h>
|
|
|
|
|
|
|
|
#include "QF/cvar.h"
|
2003-04-11 01:17:48 +00:00
|
|
|
#include "QF/quakefs.h"
|
2002-06-14 20:36:28 +00:00
|
|
|
#include "QF/sound.h"
|
|
|
|
#include "QF/sys.h"
|
|
|
|
|
2003-01-31 20:51:04 +00:00
|
|
|
#include "snd_render.h"
|
2002-06-14 20:36:28 +00:00
|
|
|
|
|
|
|
static size_t
|
|
|
|
read_func (void *ptr, size_t size, size_t nmemb, void *datasource)
|
|
|
|
{
|
|
|
|
return Qread (datasource, ptr, size * nmemb);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
seek_func (void *datasource, ogg_int64_t offset, int whence)
|
|
|
|
{
|
|
|
|
return Qseek (datasource, offset, whence);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
close_func (void *datasource)
|
|
|
|
{
|
|
|
|
Qclose (datasource);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static long
|
|
|
|
tell_func (void *datasource)
|
|
|
|
{
|
|
|
|
return Qtell (datasource);
|
|
|
|
}
|
|
|
|
|
|
|
|
static ov_callbacks callbacks = {
|
|
|
|
read_func,
|
|
|
|
seek_func,
|
|
|
|
close_func,
|
|
|
|
tell_func,
|
|
|
|
};
|
|
|
|
|
2003-04-11 21:14:38 +00:00
|
|
|
static wavinfo_t
|
|
|
|
get_info (OggVorbis_File *vf)
|
2002-06-14 20:36:28 +00:00
|
|
|
{
|
|
|
|
vorbis_info *vi;
|
2002-07-29 22:25:32 +00:00
|
|
|
int sample_start = -1, sample_count = 0;
|
2003-04-11 21:14:38 +00:00
|
|
|
int samples;
|
2002-07-29 22:25:32 +00:00
|
|
|
char **ptr;
|
2003-04-13 06:46:24 +00:00
|
|
|
wavinfo_t info;
|
2002-06-14 20:36:28 +00:00
|
|
|
|
2003-04-11 01:17:48 +00:00
|
|
|
vi = ov_info (vf, -1);
|
2003-04-11 21:14:38 +00:00
|
|
|
samples = ov_pcm_total (vf, -1);
|
2002-07-29 22:25:32 +00:00
|
|
|
|
2003-04-11 01:17:48 +00:00
|
|
|
for (ptr = ov_comment (vf, -1)->user_comments; *ptr; ptr++) {
|
2002-07-29 22:25:32 +00:00
|
|
|
Sys_DPrintf ("%s\n", *ptr);
|
|
|
|
if (strncmp ("CUEPOINT=", *ptr, 9) == 0) {
|
|
|
|
sscanf (*ptr + 9, "%d %d", &sample_start, &sample_count);
|
|
|
|
}
|
|
|
|
}
|
2003-04-11 01:17:48 +00:00
|
|
|
|
2003-04-11 21:14:38 +00:00
|
|
|
if (sample_start != -1)
|
|
|
|
samples = sample_start + sample_count;
|
|
|
|
|
|
|
|
info.rate = vi->rate;
|
|
|
|
info.width = 2;
|
|
|
|
info.channels = vi->channels;
|
|
|
|
info.loopstart = sample_start;
|
|
|
|
info.samples = samples;
|
|
|
|
info.dataofs = 0;
|
|
|
|
info.datalen = samples * 2;
|
|
|
|
|
2002-06-14 20:36:28 +00:00
|
|
|
if (developer->int_val) {
|
2003-04-11 01:17:48 +00:00
|
|
|
Sys_Printf ("\nBitstream is %d channel, %dHz\n",
|
2003-04-11 21:14:38 +00:00
|
|
|
info.channels, info.rate);
|
2003-04-11 01:17:48 +00:00
|
|
|
Sys_Printf ("\nDecoded length: %d samples (%d bytes)\n",
|
2003-04-11 21:14:38 +00:00
|
|
|
info.samples, info.samples * info.channels * 2);
|
2003-04-11 01:17:48 +00:00
|
|
|
Sys_Printf ("Encoded by: %s\n\n", ov_comment (vf, -1)->vendor);
|
2002-06-14 20:36:28 +00:00
|
|
|
}
|
2003-04-11 01:17:48 +00:00
|
|
|
|
2003-04-11 21:14:38 +00:00
|
|
|
return info;
|
2003-04-11 01:17:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2003-04-14 06:11:53 +00:00
|
|
|
vorbis_read (OggVorbis_File *vf, byte *buf, int len)
|
2003-04-11 01:17:48 +00:00
|
|
|
{
|
|
|
|
int count = 0;
|
|
|
|
int current_section;
|
|
|
|
|
|
|
|
while (len) {
|
|
|
|
int res = ov_read (vf, buf, len, 0, 2, 1, ¤t_section);
|
2002-06-15 05:43:56 +00:00
|
|
|
if (res > 0) {
|
2003-04-11 01:17:48 +00:00
|
|
|
count += res;
|
|
|
|
len -= res;
|
|
|
|
buf += res;
|
2002-06-15 05:43:56 +00:00
|
|
|
} else if (res < 0) {
|
|
|
|
Sys_Printf ("vorbis error %d\n", res);
|
2003-04-11 01:17:48 +00:00
|
|
|
return -1;
|
2002-06-15 05:43:56 +00:00
|
|
|
} else {
|
|
|
|
Sys_Printf ("unexpected eof\n");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2003-04-11 01:17:48 +00:00
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
|
|
|
static sfxbuffer_t *
|
2003-04-14 06:11:53 +00:00
|
|
|
vorbis_load (OggVorbis_File *vf, sfxblock_t *block, cache_allocator_t allocator)
|
2003-04-11 01:17:48 +00:00
|
|
|
{
|
|
|
|
byte *data;
|
|
|
|
sfxbuffer_t *sc = 0;
|
|
|
|
sfx_t *sfx = block->sfx;
|
2003-04-17 17:43:21 +00:00
|
|
|
void (*resample)(sfxbuffer_t *, byte *, int, void *);
|
2003-04-13 06:46:24 +00:00
|
|
|
wavinfo_t *info = &block->wavinfo;
|
2003-04-11 01:17:48 +00:00
|
|
|
|
2003-04-13 06:46:24 +00:00
|
|
|
switch (info->channels) {
|
2003-04-11 02:58:24 +00:00
|
|
|
case 1:
|
2003-04-11 18:59:14 +00:00
|
|
|
resample = SND_ResampleMono;
|
2003-04-11 02:58:24 +00:00
|
|
|
break;
|
|
|
|
case 2:
|
2003-04-11 18:59:14 +00:00
|
|
|
resample = SND_ResampleStereo;
|
2003-04-11 02:58:24 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
Sys_Printf ("%s: unsupported channel count: %d\n",
|
2003-04-13 06:46:24 +00:00
|
|
|
sfx->name, info->channels);
|
2003-04-11 02:58:24 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2003-04-13 06:46:24 +00:00
|
|
|
data = malloc (info->datalen);
|
2003-04-11 01:17:48 +00:00
|
|
|
if (!data)
|
|
|
|
goto bail;
|
2003-04-13 06:46:24 +00:00
|
|
|
sc = SND_GetCache (info->samples, info->rate, info->width, info->channels,
|
2003-04-11 21:14:38 +00:00
|
|
|
block, allocator);
|
2003-04-11 01:17:48 +00:00
|
|
|
if (!sc)
|
|
|
|
goto bail;
|
2003-04-11 21:14:38 +00:00
|
|
|
sc->sfx = sfx;
|
2003-04-14 06:11:53 +00:00
|
|
|
if (vorbis_read (vf, data, info->datalen) < 0)
|
2003-04-11 01:17:48 +00:00
|
|
|
goto bail;
|
2003-04-17 17:43:21 +00:00
|
|
|
resample (sc, data, info->samples, 0);
|
2003-04-14 01:40:40 +00:00
|
|
|
sc->head = sc->length;
|
2002-06-14 20:36:28 +00:00
|
|
|
bail:
|
|
|
|
if (data)
|
|
|
|
free (data);
|
2003-04-11 01:17:48 +00:00
|
|
|
ov_clear (vf);
|
2002-06-14 20:36:28 +00:00
|
|
|
return sc;
|
|
|
|
}
|
|
|
|
|
2003-04-11 01:17:48 +00:00
|
|
|
static void
|
2003-04-14 06:11:53 +00:00
|
|
|
vorbis_callback_load (void *object, cache_allocator_t allocator)
|
2003-04-11 01:17:48 +00:00
|
|
|
{
|
|
|
|
QFile *file;
|
|
|
|
OggVorbis_File vf;
|
|
|
|
|
|
|
|
sfxblock_t *block = (sfxblock_t *) object;
|
|
|
|
|
|
|
|
QFS_FOpenFile (block->file, &file);
|
|
|
|
if (!file)
|
|
|
|
return; //FIXME Sys_Error?
|
|
|
|
|
|
|
|
if (ov_open_callbacks (file, &vf, 0, 0, callbacks) < 0) {
|
|
|
|
Sys_Printf ("Input does not appear to be an Ogg bitstream.\n");
|
|
|
|
Qclose (file);
|
|
|
|
return; //FIXME Sys_Error?
|
|
|
|
}
|
2003-04-14 06:11:53 +00:00
|
|
|
vorbis_load (&vf, block, allocator);
|
2003-04-11 01:17:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2003-04-14 06:11:53 +00:00
|
|
|
vorbis_cache (sfx_t *sfx, char *realname, OggVorbis_File *vf, wavinfo_t info)
|
2003-04-13 06:46:24 +00:00
|
|
|
{
|
|
|
|
sfxblock_t *block = calloc (1, sizeof (sfxblock_t));
|
|
|
|
ov_clear (vf);
|
|
|
|
sfx->data = block;
|
|
|
|
sfx->wavinfo = SND_CacheWavinfo;
|
|
|
|
sfx->touch = SND_CacheTouch;
|
|
|
|
sfx->retain = SND_CacheRetain;
|
|
|
|
sfx->release = SND_CacheRelease;
|
|
|
|
|
|
|
|
block->sfx = sfx;
|
|
|
|
block->file = realname;
|
|
|
|
block->wavinfo = info;
|
|
|
|
|
2003-04-14 06:11:53 +00:00
|
|
|
Cache_Add (&block->cache, block, vorbis_callback_load);
|
2003-04-13 06:46:24 +00:00
|
|
|
}
|
|
|
|
|
2003-04-14 06:11:53 +00:00
|
|
|
static int
|
|
|
|
vorbis_stream_read (void *file, byte *buf, int count, wavinfo_t *info)
|
2003-04-13 06:46:24 +00:00
|
|
|
{
|
2003-04-14 06:11:53 +00:00
|
|
|
return vorbis_read (file, buf, count);
|
2003-04-13 06:46:24 +00:00
|
|
|
}
|
|
|
|
|
2003-04-14 06:11:53 +00:00
|
|
|
static int
|
|
|
|
vorbis_stream_seek (void *file, int pos, wavinfo_t *info)
|
2003-04-13 06:46:24 +00:00
|
|
|
{
|
2003-04-14 06:11:53 +00:00
|
|
|
return ov_pcm_seek (file, pos);
|
2003-04-13 06:46:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2003-04-20 07:19:51 +00:00
|
|
|
vorbis_stream_close (sfx_t *sfx)
|
2003-04-11 01:17:48 +00:00
|
|
|
{
|
2003-04-20 07:19:51 +00:00
|
|
|
sfxstream_t *stream = (sfxstream_t *)sfx->data;
|
|
|
|
|
|
|
|
ov_clear (stream->file);
|
|
|
|
free (stream);
|
|
|
|
free (sfx);
|
|
|
|
}
|
|
|
|
|
|
|
|
static sfx_t *
|
|
|
|
vorbis_stream_open (sfx_t *_sfx)
|
|
|
|
{
|
|
|
|
sfx_t *sfx;
|
|
|
|
sfxstream_t *stream = (sfxstream_t *) _sfx->data;
|
|
|
|
wavinfo_t *info = &stream->wavinfo;
|
2003-04-14 06:11:53 +00:00
|
|
|
int samples;
|
|
|
|
int size;
|
2003-04-20 07:19:51 +00:00
|
|
|
QFile *file;
|
|
|
|
|
|
|
|
QFS_FOpenFile (stream->file, &file);
|
|
|
|
if (!file)
|
|
|
|
return 0;
|
2003-04-21 01:44:55 +00:00
|
|
|
|
2003-04-20 07:19:51 +00:00
|
|
|
sfx = calloc (1, sizeof (sfx_t));
|
2003-04-17 17:43:21 +00:00
|
|
|
samples = shm->speed * 0.3;
|
2003-04-20 03:42:19 +00:00
|
|
|
size = samples = (samples + 255) & ~255;
|
2003-04-13 06:46:24 +00:00
|
|
|
if (!snd_loadas8bit->int_val)
|
|
|
|
size *= 2;
|
2003-04-20 07:19:51 +00:00
|
|
|
if (info->channels == 2)
|
2003-04-13 06:46:24 +00:00
|
|
|
size *= 2;
|
|
|
|
stream = calloc (1, sizeof (sfxstream_t) + size);
|
|
|
|
memcpy (stream->buffer.data + size, "\xde\xad\xbe\xef", 4);
|
|
|
|
|
2003-04-21 18:09:53 +00:00
|
|
|
sfx->name = _sfx->name;
|
2003-04-13 06:46:24 +00:00
|
|
|
sfx->data = stream;
|
|
|
|
sfx->wavinfo = SND_CacheWavinfo;
|
|
|
|
sfx->touch = sfx->retain = SND_StreamRetain;
|
|
|
|
sfx->release = SND_StreamRelease;
|
2003-04-20 07:19:51 +00:00
|
|
|
sfx->close = vorbis_stream_close;
|
2003-04-13 06:46:24 +00:00
|
|
|
|
|
|
|
stream->sfx = sfx;
|
2003-04-14 16:28:20 +00:00
|
|
|
stream->file = malloc (sizeof (OggVorbis_File));
|
2003-04-20 07:19:51 +00:00
|
|
|
if (ov_open_callbacks (file, stream->file, 0, 0, callbacks) < 0) {
|
|
|
|
Sys_Printf ("Input does not appear to be an Ogg bitstream.\n");
|
|
|
|
Qclose (file);
|
|
|
|
free (stream);
|
|
|
|
free (sfx);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
stream->resample = info->channels == 2 ? SND_ResampleStereo
|
2003-04-13 06:46:24 +00:00
|
|
|
: SND_ResampleMono;
|
2003-04-14 06:11:53 +00:00
|
|
|
stream->read = vorbis_stream_read;
|
|
|
|
stream->seek = vorbis_stream_seek;
|
2003-04-20 07:19:51 +00:00
|
|
|
stream->wavinfo = *info;
|
2003-04-13 06:46:24 +00:00
|
|
|
|
|
|
|
stream->buffer.length = samples;
|
2003-04-14 06:11:53 +00:00
|
|
|
stream->buffer.advance = SND_StreamAdvance;
|
2003-04-26 04:30:07 +00:00
|
|
|
stream->buffer.setpos = SND_StreamSetPos;
|
2003-04-14 06:11:53 +00:00
|
|
|
stream->buffer.sfx = sfx;
|
|
|
|
|
2003-04-17 17:43:21 +00:00
|
|
|
stream->resample (&stream->buffer, 0, 0, 0); // get sfx setup properly
|
2003-04-15 02:34:17 +00:00
|
|
|
stream->seek (stream->file, 0, &stream->wavinfo);
|
|
|
|
|
2003-04-14 06:11:53 +00:00
|
|
|
stream->buffer.advance (&stream->buffer, 0);
|
2003-04-20 07:19:51 +00:00
|
|
|
|
|
|
|
return sfx;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
vorbis_stream (sfx_t *sfx, char *realname, OggVorbis_File *vf, wavinfo_t info)
|
|
|
|
{
|
|
|
|
sfxstream_t *stream = calloc (1, sizeof (sfxstream_t));
|
|
|
|
|
|
|
|
ov_clear (vf);
|
|
|
|
sfx->open = vorbis_stream_open;
|
|
|
|
sfx->wavinfo = SND_CacheWavinfo;
|
|
|
|
sfx->touch = sfx->retain = SND_StreamRetain;
|
|
|
|
sfx->release = SND_StreamRelease;
|
|
|
|
sfx->data = stream;
|
|
|
|
|
|
|
|
stream->file = realname;
|
|
|
|
stream->wavinfo = info;
|
2003-04-11 01:17:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
SND_LoadOgg (QFile *file, sfx_t *sfx, char *realname)
|
|
|
|
{
|
|
|
|
OggVorbis_File vf;
|
2003-04-13 06:46:24 +00:00
|
|
|
wavinfo_t info;
|
2003-04-11 01:17:48 +00:00
|
|
|
|
|
|
|
if (ov_open_callbacks (file, &vf, 0, 0, callbacks) < 0) {
|
|
|
|
Sys_Printf ("Input does not appear to be an Ogg bitstream.\n");
|
|
|
|
Qclose (file);
|
|
|
|
free (realname);
|
|
|
|
return;
|
|
|
|
}
|
2003-04-13 06:46:24 +00:00
|
|
|
info = get_info (&vf);
|
|
|
|
if (info.channels < 1 || info.channels > 2) {
|
|
|
|
Sys_Printf ("unsupported number of channels");
|
|
|
|
return;
|
|
|
|
}
|
2003-04-14 15:47:08 +00:00
|
|
|
if (info.samples / info.rate < 3) {
|
2003-04-15 02:34:17 +00:00
|
|
|
Sys_DPrintf ("cache %s\n", realname);
|
2003-04-14 06:11:53 +00:00
|
|
|
vorbis_cache (sfx, realname, &vf, info);
|
2003-04-11 01:17:48 +00:00
|
|
|
} else {
|
2003-04-15 02:34:17 +00:00
|
|
|
Sys_DPrintf ("stream %s\n", realname);
|
2003-04-14 06:11:53 +00:00
|
|
|
vorbis_stream (sfx, realname, &vf, info);
|
2003-04-11 01:17:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-06-14 20:36:28 +00:00
|
|
|
#endif//HAVE_VORBIS
|