read the samples out of the buffer properly. fixes the horrible sounds :)

streamed flacs now work. cached not tested yet (haven't got any flacs that
small:)
This commit is contained in:
Bill Currie 2005-06-16 00:06:37 +00:00
parent a2f0cd94e8
commit d083fb4150

View file

@ -48,6 +48,7 @@ static __attribute__ ((unused)) const char rcsid[] =
#include <FLAC/metadata.h> #include <FLAC/metadata.h>
#include "QF/cvar.h" #include "QF/cvar.h"
#include "QF/qendian.h"
#include "QF/quakefs.h" #include "QF/quakefs.h"
#include "QF/sound.h" #include "QF/sound.h"
#include "QF/sys.h" #include "QF/sys.h"
@ -135,20 +136,20 @@ write_func (const FLAC__SeekableStreamDecoder *decoder,
char *ro = (char *) ff->buffer + 1; char *ro = (char *) ff->buffer + 1;
for (i = 0; i < frame->header.blocksize; i++, lo++, ro++) { for (i = 0; i < frame->header.blocksize; i++, lo++, ro++) {
*lo++ = *li++; *lo++ = LittleShort (*li++);
*ro++ = *ri++; *ro++ = LittleShort (*ri++);
} }
} else { } else {
short *lo = (short *) ff->buffer + 0; short *lo = (short *) ff->buffer + 0;
short *ro = (short *) ff->buffer + 1; short *ro = (short *) ff->buffer + 1;
for (i = 0; i < frame->header.blocksize; i++, lo++, ro++) { for (i = 0; i < frame->header.blocksize; i++, lo++, ro++) {
*lo++ = *li++; *lo++ = LittleShort (*li++);
*ro++ = *ri++; *ro++ = LittleShort (*ri++);
} }
} }
} }
ff->size = frame->header.blocksize; ff->size = frame->header.blocksize * bps * ff->info.channels;
ff->pos = 0; ff->pos = 0;
return FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE; return FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE;
} }
@ -211,13 +212,12 @@ static int
flac_read (flacfile_t *ff, byte *buf, int len) flac_read (flacfile_t *ff, byte *buf, int len)
{ {
int count = 0; int count = 0;
int bps = ff->info.channels * ff->info.bits_per_sample / 8;
while (len) { while (len) {
int res = 0; int res = 0;
if (ff->size == ff->pos) if (ff->size == ff->pos)
FLAC__seekable_stream_decoder_process_single (ff->decoder); FLAC__seekable_stream_decoder_process_single (ff->decoder);
res = (ff->size - ff->pos) * bps; res = ff->size - ff->pos;
if (res > len) if (res > len)
res = len; res = len;
if (res > 0) { if (res > 0) {
@ -225,7 +225,7 @@ flac_read (flacfile_t *ff, byte *buf, int len)
count += res; count += res;
len -= res; len -= res;
buf += res; buf += res;
ff->pos += res / bps; ff->pos += res;
} else if (res < 0) { } else if (res < 0) {
Sys_Printf ("flac error %d\n", res); Sys_Printf ("flac error %d\n", res);
return -1; return -1;
@ -325,6 +325,8 @@ static int
flac_stream_seek (void *file, int pos, wavinfo_t *info) flac_stream_seek (void *file, int pos, wavinfo_t *info)
{ {
flacfile_t *ff = file; flacfile_t *ff = file;
ff->pos = ff->size = 0;
return FLAC__seekable_stream_decoder_seek_absolute (ff->decoder, pos); return FLAC__seekable_stream_decoder_seek_absolute (ff->decoder, pos);
} }