2005-06-15 10:02:50 +00:00
|
|
|
/*
|
|
|
|
flac.c
|
|
|
|
|
2005-06-15 10:06:55 +00:00
|
|
|
FLAC support
|
2005-06-15 10:02:50 +00:00
|
|
|
|
2005-06-15 10:06:55 +00:00
|
|
|
Copyright (C) 2005 Bill Currie <bill@taniwha.org>
|
2005-06-15 10:02:50 +00:00
|
|
|
|
|
|
|
Author: Bill Currie <bill@taniwha.org>
|
2005-06-15 10:06:55 +00:00
|
|
|
Date: 2005/6/15
|
2005-06-15 10:02:50 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
2007-05-30 22:06:47 +00:00
|
|
|
#include <FLAC/export.h>
|
|
|
|
|
2013-01-22 05:09:41 +00:00
|
|
|
#include "qfalloca.h"
|
|
|
|
|
2007-05-30 22:06:47 +00:00
|
|
|
/* FLAC 1.1.3 has FLAC_API_VERSION_CURRENT == 8 */
|
|
|
|
#if !defined(FLAC_API_VERSION_CURRENT) || FLAC_API_VERSION_CURRENT < 8
|
|
|
|
#define LEGACY_FLAC
|
|
|
|
#else
|
|
|
|
#undef LEGACY_FLAC
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef LEGACY_FLAC
|
2005-06-15 10:02:50 +00:00
|
|
|
#include <FLAC/seekable_stream_decoder.h>
|
2007-05-30 22:06:47 +00:00
|
|
|
#else
|
|
|
|
#include <FLAC/stream_decoder.h>
|
|
|
|
#endif
|
2005-06-15 11:56:03 +00:00
|
|
|
#include <FLAC/metadata.h>
|
2005-06-15 10:02:50 +00:00
|
|
|
|
|
|
|
#include "QF/cvar.h"
|
2005-06-16 00:06:37 +00:00
|
|
|
#include "QF/qendian.h"
|
2005-06-15 10:02:50 +00:00
|
|
|
#include "QF/quakefs.h"
|
|
|
|
#include "QF/sound.h"
|
|
|
|
#include "QF/sys.h"
|
|
|
|
|
2011-07-23 06:56:22 +00:00
|
|
|
#include "snd_internal.h"
|
2005-06-15 10:02:50 +00:00
|
|
|
|
2007-05-30 22:06:47 +00:00
|
|
|
#ifdef LEGACY_FLAC
|
2010-08-11 23:40:35 +00:00
|
|
|
#define FLAC__StreamDecoder FLAC__SeekableStreamDecoder
|
|
|
|
#define FLAC__StreamDecoderReadStatus FLAC__SeekableStreamDecoderReadStatus
|
|
|
|
#define FLAC__StreamDecoderSeekStatus FLAC__SeekableStreamDecoderSeekStatus
|
|
|
|
#define FLAC__StreamDecoderTellStatus FLAC__SeekableStreamDecoderTellStatus
|
|
|
|
#define FLAC__StreamDecoderLengthStatus FLAC__SeekableStreamDecoderLengthStatus
|
|
|
|
|
|
|
|
#define FLAC__stream_decoder_new FLAC__seekable_stream_decoder_new
|
|
|
|
#define FLAC__stream_decoder_finish FLAC__seekable_stream_decoder_finish
|
|
|
|
#define FLAC__stream_decoder_delete FLAC__seekable_stream_decoder_delete
|
|
|
|
#define FLAC__stream_decoder_process_single \
|
|
|
|
FLAC__seekable_stream_decoder_process_single
|
|
|
|
#define FLAC__stream_decoder_seek_absolute \
|
|
|
|
FLAC__seekable_stream_decoder_seek_absolute
|
|
|
|
|
|
|
|
#define FLAC__STREAM_DECODER_READ_STATUS_CONTINUE \
|
|
|
|
FLAC__SEEKABLE_STREAM_DECODER_READ_STATUS_OK
|
|
|
|
#define FLAC__STREAM_DECODER_SEEK_STATUS_OK \
|
|
|
|
FLAC__SEEKABLE_STREAM_DECODER_SEEK_STATUS_OK
|
|
|
|
#define FLAC__STREAM_DECODER_TELL_STATUS_OK \
|
|
|
|
FLAC__SEEKABLE_STREAM_DECODER_TELL_STATUS_OK
|
|
|
|
#define FLAC__STREAM_DECODER_LENGTH_STATUS_OK \
|
|
|
|
FLAC__SEEKABLE_STREAM_DECODER_LENGTH_STATUS_OK
|
2007-05-30 22:06:47 +00:00
|
|
|
#endif
|
2010-08-11 23:40:35 +00:00
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
FLAC__StreamDecoder *decoder;
|
2005-06-15 10:02:50 +00:00
|
|
|
QFile *file;
|
|
|
|
FLAC__StreamMetadata_StreamInfo info;
|
|
|
|
FLAC__StreamMetadata *vorbis_info;
|
2010-08-11 23:44:34 +00:00
|
|
|
float *buffer;
|
|
|
|
int size; // in frames
|
|
|
|
int pos; // in frames
|
2005-06-15 10:02:50 +00:00
|
|
|
} flacfile_t;
|
|
|
|
|
2005-06-15 11:56:03 +00:00
|
|
|
static void
|
2010-08-11 23:41:04 +00:00
|
|
|
flac_error_func (const FLAC__StreamDecoder *decoder,
|
|
|
|
FLAC__StreamDecoderErrorStatus status, void *client_data)
|
2005-06-15 11:56:03 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2007-05-30 22:06:47 +00:00
|
|
|
static FLAC__StreamDecoderReadStatus
|
2010-08-11 23:41:04 +00:00
|
|
|
flac_read_func (const FLAC__StreamDecoder *decoder, FLAC__byte buffer[],
|
|
|
|
size_t *bytes, void *client_data)
|
2005-06-15 10:02:50 +00:00
|
|
|
{
|
|
|
|
flacfile_t *ff = (flacfile_t *) client_data;
|
|
|
|
*bytes = Qread (ff->file, buffer, *bytes);
|
2007-05-30 22:06:47 +00:00
|
|
|
return FLAC__STREAM_DECODER_READ_STATUS_CONTINUE;
|
2005-06-15 10:02:50 +00:00
|
|
|
}
|
|
|
|
|
2007-05-30 22:06:47 +00:00
|
|
|
static FLAC__StreamDecoderSeekStatus
|
2010-08-11 23:41:04 +00:00
|
|
|
flac_seek_func (const FLAC__StreamDecoder *decoder,
|
|
|
|
FLAC__uint64 absolute_byte_offset, void *client_data)
|
2005-06-15 10:02:50 +00:00
|
|
|
{
|
|
|
|
flacfile_t *ff = (flacfile_t *) client_data;
|
|
|
|
Qseek (ff->file, absolute_byte_offset, SEEK_SET);
|
2010-08-11 23:40:35 +00:00
|
|
|
return FLAC__STREAM_DECODER_SEEK_STATUS_OK;
|
2005-06-15 10:02:50 +00:00
|
|
|
}
|
|
|
|
|
2007-05-30 22:06:47 +00:00
|
|
|
static FLAC__StreamDecoderTellStatus
|
2010-08-11 23:41:04 +00:00
|
|
|
flac_tell_func (const FLAC__StreamDecoder *decoder,
|
|
|
|
FLAC__uint64 *absolute_byte_offset, void *client_data)
|
2005-06-15 10:02:50 +00:00
|
|
|
{
|
|
|
|
flacfile_t *ff = (flacfile_t *) client_data;
|
|
|
|
*absolute_byte_offset = Qtell (ff->file);
|
2007-05-30 22:06:47 +00:00
|
|
|
return FLAC__STREAM_DECODER_TELL_STATUS_OK;
|
2005-06-15 10:02:50 +00:00
|
|
|
}
|
|
|
|
|
2007-05-30 22:06:47 +00:00
|
|
|
static FLAC__StreamDecoderLengthStatus
|
2010-08-11 23:41:04 +00:00
|
|
|
flac_length_func (const FLAC__StreamDecoder *decoder,
|
|
|
|
FLAC__uint64 *stream_length, void *client_data)
|
2005-06-15 10:02:50 +00:00
|
|
|
{
|
|
|
|
flacfile_t *ff = (flacfile_t *) client_data;
|
|
|
|
*stream_length = Qfilesize (ff->file);
|
2007-05-30 22:06:47 +00:00
|
|
|
return FLAC__STREAM_DECODER_LENGTH_STATUS_OK;
|
2005-06-15 10:02:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static FLAC__bool
|
2010-08-11 23:41:04 +00:00
|
|
|
flac_eof_func (const FLAC__StreamDecoder *decoder, void *client_data)
|
2005-06-15 10:02:50 +00:00
|
|
|
{
|
|
|
|
flacfile_t *ff = (flacfile_t *) client_data;
|
|
|
|
return Qeof (ff->file);
|
|
|
|
}
|
|
|
|
|
|
|
|
static FLAC__StreamDecoderWriteStatus
|
2010-08-11 23:41:04 +00:00
|
|
|
flac_write_func (const FLAC__StreamDecoder *decoder,
|
|
|
|
const FLAC__Frame *frame, const FLAC__int32 * const buffer[],
|
|
|
|
void *client_data)
|
2005-06-15 10:02:50 +00:00
|
|
|
{
|
|
|
|
flacfile_t *ff = (flacfile_t *) client_data;
|
2010-08-11 23:44:34 +00:00
|
|
|
float *out;
|
|
|
|
float scale = 2.0 / (1 << ff->info.bits_per_sample);
|
2010-08-11 23:45:09 +00:00
|
|
|
int step = ff->info.channels;
|
2010-08-11 23:45:32 +00:00
|
|
|
unsigned j;
|
|
|
|
int i;
|
2005-06-15 11:56:03 +00:00
|
|
|
|
|
|
|
if (!ff->buffer)
|
2010-08-11 23:45:09 +00:00
|
|
|
ff->buffer = calloc (ff->info.max_blocksize * ff->info.channels,
|
|
|
|
sizeof (float));
|
|
|
|
ff->size = frame->header.blocksize;
|
|
|
|
ff->pos = 0;
|
2010-08-11 23:44:34 +00:00
|
|
|
for (j = 0; j < ff->info.channels; j++) {
|
|
|
|
const FLAC__int32 *in = buffer[j];
|
|
|
|
|
|
|
|
out = ff->buffer + j;
|
2010-08-11 23:45:09 +00:00
|
|
|
for (i = 0; i < ff->size; i++, out += step)
|
2010-08-11 23:44:34 +00:00
|
|
|
*out = *in++ * scale;
|
2005-06-15 10:02:50 +00:00
|
|
|
}
|
|
|
|
return FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2010-08-11 23:41:04 +00:00
|
|
|
flac_meta_func (const FLAC__StreamDecoder *decoder,
|
|
|
|
const FLAC__StreamMetadata *metadata, void *client_data)
|
2005-06-15 10:02:50 +00:00
|
|
|
{
|
|
|
|
flacfile_t *ff = (flacfile_t *) client_data;
|
|
|
|
if (metadata->type == FLAC__METADATA_TYPE_STREAMINFO)
|
|
|
|
ff->info = metadata->data.stream_info;
|
2005-06-15 11:56:03 +00:00
|
|
|
if (metadata->type == FLAC__METADATA_TYPE_VORBIS_COMMENT)
|
|
|
|
ff->vorbis_info = FLAC__metadata_object_clone (metadata);
|
2005-06-15 10:02:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static flacfile_t *
|
2010-08-11 23:41:04 +00:00
|
|
|
flac_open (QFile *file)
|
2005-06-15 10:02:50 +00:00
|
|
|
{
|
|
|
|
flacfile_t *ff = calloc (1, sizeof (flacfile_t));
|
2007-05-30 22:06:47 +00:00
|
|
|
ff->decoder = FLAC__stream_decoder_new ();
|
2005-06-15 10:02:50 +00:00
|
|
|
ff->file = file;
|
|
|
|
|
2007-05-30 22:06:47 +00:00
|
|
|
#ifdef LEGACY_FLAC
|
2010-08-11 23:41:04 +00:00
|
|
|
FLAC__seekable_stream_decoder_set_error_callback (ff->decoder,
|
|
|
|
flac_error_func);
|
|
|
|
FLAC__seekable_stream_decoder_set_read_callback (ff->decoder,
|
|
|
|
flac_read_func);
|
|
|
|
FLAC__seekable_stream_decoder_set_seek_callback (ff->decoder,
|
|
|
|
flac_seek_func);
|
|
|
|
FLAC__seekable_stream_decoder_set_tell_callback (ff->decoder,
|
|
|
|
flac_tell_func);
|
2005-06-15 10:02:50 +00:00
|
|
|
FLAC__seekable_stream_decoder_set_length_callback (ff->decoder,
|
2010-08-11 23:41:04 +00:00
|
|
|
flac_length_func);
|
|
|
|
FLAC__seekable_stream_decoder_set_eof_callback (ff->decoder,
|
|
|
|
flac_eof_func);
|
|
|
|
FLAC__seekable_stream_decoder_set_write_callback (ff->decoder,
|
|
|
|
flac_write_func);
|
2005-06-15 10:02:50 +00:00
|
|
|
FLAC__seekable_stream_decoder_set_metadata_callback (ff->decoder,
|
2010-08-11 23:41:04 +00:00
|
|
|
flac_meta_func);
|
2005-06-15 10:02:50 +00:00
|
|
|
FLAC__seekable_stream_decoder_set_client_data (ff->decoder, ff);
|
2005-06-15 11:56:03 +00:00
|
|
|
FLAC__seekable_stream_decoder_set_metadata_respond (ff->decoder,
|
|
|
|
FLAC__METADATA_TYPE_VORBIS_COMMENT);
|
2005-06-15 10:02:50 +00:00
|
|
|
|
2005-06-16 00:35:55 +00:00
|
|
|
FLAC__seekable_stream_decoder_init (ff->decoder);
|
2005-06-15 10:02:50 +00:00
|
|
|
FLAC__seekable_stream_decoder_process_until_end_of_metadata (ff->decoder);
|
2007-05-30 22:06:47 +00:00
|
|
|
#else
|
2010-08-11 23:41:04 +00:00
|
|
|
FLAC__stream_decoder_set_metadata_respond (ff->decoder,
|
|
|
|
FLAC__METADATA_TYPE_VORBIS_COMMENT);
|
|
|
|
FLAC__stream_decoder_init_stream(ff->decoder,
|
|
|
|
flac_read_func, flac_seek_func,
|
|
|
|
flac_tell_func, flac_length_func,
|
|
|
|
flac_eof_func, flac_write_func,
|
|
|
|
flac_meta_func, flac_error_func,
|
|
|
|
ff);
|
2007-05-30 22:06:47 +00:00
|
|
|
FLAC__stream_decoder_process_until_end_of_metadata (ff->decoder);
|
|
|
|
#endif
|
2005-06-15 10:02:50 +00:00
|
|
|
return ff;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2010-08-11 23:41:04 +00:00
|
|
|
flac_close (flacfile_t *ff)
|
2005-06-15 10:02:50 +00:00
|
|
|
{
|
2007-05-30 22:06:47 +00:00
|
|
|
FLAC__stream_decoder_finish (ff->decoder);
|
|
|
|
FLAC__stream_decoder_delete (ff->decoder);
|
2005-06-15 10:02:50 +00:00
|
|
|
|
2005-06-15 11:56:03 +00:00
|
|
|
if (ff->vorbis_info)
|
|
|
|
FLAC__metadata_object_delete (ff->vorbis_info);
|
|
|
|
|
|
|
|
if (ff->buffer)
|
|
|
|
free (ff->buffer);
|
|
|
|
|
2005-06-15 10:02:50 +00:00
|
|
|
Qclose (ff->file);
|
|
|
|
|
|
|
|
free (ff);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2010-08-11 23:44:34 +00:00
|
|
|
flac_read (flacfile_t *ff, float *buf, int len)
|
2005-06-15 10:02:50 +00:00
|
|
|
{
|
|
|
|
int count = 0;
|
|
|
|
|
|
|
|
while (len) {
|
|
|
|
int res = 0;
|
|
|
|
if (ff->size == ff->pos)
|
2007-05-30 22:06:47 +00:00
|
|
|
FLAC__stream_decoder_process_single (ff->decoder);
|
2005-06-16 00:06:37 +00:00
|
|
|
res = ff->size - ff->pos;
|
2005-06-15 10:02:50 +00:00
|
|
|
if (res > len)
|
|
|
|
res = len;
|
|
|
|
if (res > 0) {
|
2010-08-11 23:44:34 +00:00
|
|
|
memcpy (buf, ff->buffer + ff->pos * ff->info.channels,
|
|
|
|
res * ff->info.channels * sizeof (float));
|
2005-06-15 10:02:50 +00:00
|
|
|
count += res;
|
|
|
|
len -= res;
|
2010-08-11 23:45:32 +00:00
|
|
|
buf += res * ff->info.channels;
|
2005-06-16 00:06:37 +00:00
|
|
|
ff->pos += res;
|
2005-06-15 10:02:50 +00:00
|
|
|
} else if (res < 0) {
|
|
|
|
Sys_Printf ("flac error %d\n", res);
|
|
|
|
return -1;
|
|
|
|
} else {
|
|
|
|
Sys_Printf ("unexpected eof\n");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
|
|
|
static sfxbuffer_t *
|
|
|
|
flac_load (flacfile_t *ff, sfxblock_t *block, cache_allocator_t allocator)
|
|
|
|
{
|
2010-08-11 23:44:34 +00:00
|
|
|
float *data;
|
2005-06-15 10:02:50 +00:00
|
|
|
sfxbuffer_t *sc = 0;
|
|
|
|
sfx_t *sfx = block->sfx;
|
|
|
|
wavinfo_t *info = &block->wavinfo;
|
|
|
|
|
|
|
|
data = malloc (info->datalen);
|
|
|
|
if (!data)
|
|
|
|
goto bail;
|
2010-08-11 23:44:34 +00:00
|
|
|
sc = SND_GetCache (info->frames, info->rate, info->channels,
|
2005-06-15 10:02:50 +00:00
|
|
|
block, allocator);
|
|
|
|
if (!sc)
|
|
|
|
goto bail;
|
|
|
|
sc->sfx = sfx;
|
2010-08-11 23:44:34 +00:00
|
|
|
if (flac_read (ff, data, info->frames) < 0)
|
2005-06-15 10:02:50 +00:00
|
|
|
goto bail;
|
2007-03-26 11:44:52 +00:00
|
|
|
SND_SetPaint (sc);
|
2010-08-11 23:45:09 +00:00
|
|
|
SND_SetupResampler (sc, 0);
|
2010-08-11 23:44:34 +00:00
|
|
|
SND_Resample (sc, data, info->frames);
|
2005-06-15 10:02:50 +00:00
|
|
|
sc->head = sc->length;
|
|
|
|
bail:
|
|
|
|
if (data)
|
|
|
|
free (data);
|
2010-08-11 23:41:04 +00:00
|
|
|
flac_close (ff);
|
2005-06-15 10:02:50 +00:00
|
|
|
return sc;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
flac_callback_load (void *object, cache_allocator_t allocator)
|
|
|
|
{
|
|
|
|
QFile *file;
|
|
|
|
flacfile_t *ff;
|
|
|
|
|
|
|
|
sfxblock_t *block = (sfxblock_t *) object;
|
2012-05-21 23:23:22 +00:00
|
|
|
|
2005-06-15 10:02:50 +00:00
|
|
|
QFS_FOpenFile (block->file, &file);
|
|
|
|
if (!file)
|
|
|
|
return; //FIXME Sys_Error?
|
|
|
|
|
2010-08-11 23:41:04 +00:00
|
|
|
if (!(ff = flac_open (file))) {
|
2005-06-15 10:02:50 +00:00
|
|
|
Sys_Printf ("Input does not appear to be an Ogg bitstream.\n");
|
|
|
|
Qclose (file);
|
|
|
|
return; //FIXME Sys_Error?
|
|
|
|
}
|
|
|
|
flac_load (ff, block, allocator);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
flac_cache (sfx_t *sfx, char *realname, flacfile_t *ff, wavinfo_t info)
|
|
|
|
{
|
2010-08-11 23:41:04 +00:00
|
|
|
flac_close (ff);
|
2007-03-18 01:44:46 +00:00
|
|
|
SND_SFX_Cache (sfx, realname, info, flac_callback_load);
|
2005-06-15 10:02:50 +00:00
|
|
|
}
|
|
|
|
|
2010-08-11 23:47:03 +00:00
|
|
|
static long
|
|
|
|
flac_stream_read (void *file, float **buf)
|
2005-06-15 10:02:50 +00:00
|
|
|
{
|
2010-08-11 23:47:03 +00:00
|
|
|
sfxstream_t *stream = (sfxstream_t *) file;
|
|
|
|
flacfile_t *ff = (flacfile_t *) stream->file;
|
2010-08-13 01:48:20 +00:00
|
|
|
int res = FLAC__stream_decoder_process_single (ff->decoder);
|
|
|
|
|
|
|
|
if (!res) {
|
|
|
|
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
|
|
|
*buf = ff->buffer;
|
|
|
|
return ff->size;
|
2005-06-15 10:02:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2010-08-11 23:47:03 +00:00
|
|
|
flac_stream_seek (sfxstream_t *stream, int pos)
|
2005-06-15 10:02:50 +00:00
|
|
|
{
|
2010-08-11 23:47:03 +00:00
|
|
|
flacfile_t *ff = stream->file;
|
2005-06-16 00:06:37 +00:00
|
|
|
|
|
|
|
ff->pos = ff->size = 0;
|
2007-05-30 22:06:47 +00:00
|
|
|
return FLAC__stream_decoder_seek_absolute (ff->decoder, pos);
|
2005-06-15 10:02:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
flac_stream_close (sfx_t *sfx)
|
|
|
|
{
|
2007-05-07 05:20:24 +00:00
|
|
|
sfxstream_t *stream = sfx->data.stream;
|
2005-06-15 10:02:50 +00:00
|
|
|
|
2010-08-11 23:41:04 +00:00
|
|
|
flac_close (stream->file);
|
2010-08-11 23:47:03 +00:00
|
|
|
SND_SFX_StreamClose (sfx);
|
2005-06-15 10:02:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static sfx_t *
|
2007-03-18 01:44:46 +00:00
|
|
|
flac_stream_open (sfx_t *sfx)
|
2005-06-15 10:02:50 +00:00
|
|
|
{
|
2007-05-07 05:20:24 +00:00
|
|
|
sfxstream_t *stream = sfx->data.stream;
|
2005-06-15 10:02:50 +00:00
|
|
|
QFile *file;
|
2007-03-18 01:44:46 +00:00
|
|
|
void *f;
|
2005-06-15 10:02:50 +00:00
|
|
|
|
|
|
|
QFS_FOpenFile (stream->file, &file);
|
|
|
|
if (!file)
|
|
|
|
return 0;
|
|
|
|
|
2010-08-11 23:41:04 +00:00
|
|
|
f = flac_open (file);
|
2007-03-18 01:44:46 +00:00
|
|
|
if (!f) {
|
2005-06-15 10:02:50 +00:00
|
|
|
Sys_Printf ("Input does not appear to be a flac bitstream.\n");
|
|
|
|
Qclose (file);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2007-03-18 01:44:46 +00:00
|
|
|
return SND_SFX_StreamOpen (sfx, f, flac_stream_read, flac_stream_seek,
|
|
|
|
flac_stream_close);
|
2005-06-15 10:02:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
flac_stream (sfx_t *sfx, char *realname, flacfile_t *ff, wavinfo_t info)
|
|
|
|
{
|
2010-08-11 23:41:04 +00:00
|
|
|
flac_close (ff);
|
2007-03-18 01:44:46 +00:00
|
|
|
SND_SFX_Stream (sfx, realname, info, flac_stream_open);
|
2005-06-15 10:02:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static wavinfo_t
|
2010-08-11 23:41:04 +00:00
|
|
|
flac_get_info (flacfile_t *ff)
|
2005-06-15 10:02:50 +00:00
|
|
|
{
|
|
|
|
int sample_start = -1, sample_count = 0;
|
|
|
|
int samples;
|
|
|
|
wavinfo_t info;
|
2005-06-15 11:56:03 +00:00
|
|
|
FLAC__StreamMetadata_VorbisComment *vc = 0;
|
2005-06-15 10:02:50 +00:00
|
|
|
FLAC__StreamMetadata_VorbisComment_Entry *ve;
|
|
|
|
FLAC__uint32 i;
|
|
|
|
|
|
|
|
samples = ff->info.total_samples;
|
2005-06-15 11:56:03 +00:00
|
|
|
if (ff->vorbis_info) {
|
|
|
|
vc = &ff->vorbis_info->data.vorbis_comment;
|
|
|
|
|
|
|
|
for (i = 0, ve = vc->comments; i < vc->num_comments; ve++, i++) {
|
2010-11-23 05:09:30 +00:00
|
|
|
Sys_MaskPrintf (SYS_DEV, "%.*s\n", ve->length, ve->entry);
|
2005-06-15 22:20:40 +00:00
|
|
|
if (strncmp ("CUEPOINT=", (char *) ve->entry, 9) == 0) {
|
2005-06-15 11:56:03 +00:00
|
|
|
char *str = alloca (ve->length + 1);
|
2005-06-15 22:20:40 +00:00
|
|
|
strncpy (str, (char *) ve->entry, ve->length);
|
2005-06-15 11:56:03 +00:00
|
|
|
str[ve->length] = 0;
|
|
|
|
sscanf (str + 9, "%d %d", &sample_start, &sample_count);
|
|
|
|
}
|
2005-06-15 10:02:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (sample_start != -1)
|
|
|
|
samples = sample_start + sample_count;
|
|
|
|
|
|
|
|
info.rate = ff->info.sample_rate;
|
|
|
|
info.width = ff->info.bits_per_sample / 8;
|
|
|
|
info.channels = ff->info.channels;
|
|
|
|
info.loopstart = sample_start;
|
2010-08-11 23:44:34 +00:00
|
|
|
info.frames = samples;
|
2005-06-15 10:02:50 +00:00
|
|
|
info.dataofs = 0;
|
2010-08-11 23:44:34 +00:00
|
|
|
info.datalen = samples * info.channels * sizeof (float);
|
2005-06-15 10:02:50 +00:00
|
|
|
|
2010-11-27 07:53:17 +00:00
|
|
|
Sys_MaskPrintf (SYS_DEV, "\nBitstream is %d channel, %dHz\n",
|
2005-06-15 10:02:50 +00:00
|
|
|
info.channels, info.rate);
|
2010-11-27 07:53:17 +00:00
|
|
|
Sys_MaskPrintf (SYS_DEV, "\nDecoded length: %d samples (%d bytes)\n",
|
2010-08-11 23:44:34 +00:00
|
|
|
info.frames, info.width);
|
2010-11-27 07:53:17 +00:00
|
|
|
if (vc) {
|
|
|
|
Sys_MaskPrintf (SYS_DEV, "Encoded by: %.*s\n\n",
|
2005-06-15 11:56:03 +00:00
|
|
|
vc->vendor_string.length, vc->vendor_string.entry);
|
2005-06-15 10:02:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return info;
|
|
|
|
}
|
|
|
|
|
2010-08-08 03:02:55 +00:00
|
|
|
int
|
2005-06-15 10:02:50 +00:00
|
|
|
SND_LoadFLAC (QFile *file, sfx_t *sfx, char *realname)
|
|
|
|
{
|
|
|
|
flacfile_t *ff;
|
|
|
|
wavinfo_t info;
|
|
|
|
|
2010-08-11 23:41:04 +00:00
|
|
|
if (!(ff = flac_open (file))) {
|
|
|
|
Sys_Printf ("Input does not appear to be a FLAC bitstream.\n");
|
2010-08-08 03:02:55 +00:00
|
|
|
return -1;
|
2005-06-15 10:02:50 +00:00
|
|
|
}
|
2010-08-11 23:41:04 +00:00
|
|
|
info = flac_get_info (ff);
|
2010-08-12 02:28:27 +00:00
|
|
|
if (info.channels < 1 || info.channels > 8) {
|
2005-06-15 10:02:50 +00:00
|
|
|
Sys_Printf ("unsupported number of channels");
|
2010-08-08 03:02:55 +00:00
|
|
|
return -1;
|
2005-06-15 10:02:50 +00:00
|
|
|
}
|
2010-08-11 23:44:34 +00:00
|
|
|
if (info.frames / info.rate < 3) {
|
2010-11-23 05:09:30 +00:00
|
|
|
Sys_MaskPrintf (SYS_DEV, "cache %s\n", realname);
|
2005-06-15 10:02:50 +00:00
|
|
|
flac_cache (sfx, realname, ff, info);
|
|
|
|
} else {
|
2010-11-23 05:09:30 +00:00
|
|
|
Sys_MaskPrintf (SYS_DEV, "stream %s\n", realname);
|
2005-06-15 10:02:50 +00:00
|
|
|
flac_stream (sfx, realname, ff, info);
|
|
|
|
}
|
2010-08-08 03:02:55 +00:00
|
|
|
return 0;
|
2005-06-15 10:02:50 +00:00
|
|
|
}
|