From c47ebac6c11158191428c1733172d8a5f48d718e Mon Sep 17 00:00:00 2001 From: hendricks266 Date: Fri, 24 Apr 2015 00:09:42 +0000 Subject: [PATCH] AudioLib: When handling a LOOP_END tag in FLAC or Ogg, truncate any blocks that read past it so that the specified value is respected precisely, instead of rounding up to the nearest block size. DONT_BUILD. git-svn-id: https://svn.eduke32.com/eduke32@5163 1a8010ca-5511-0410-912e-c29ae57300e0 --- polymer/eduke32/source/jaudiolib/src/flac.c | 20 +++++++++++++------ polymer/eduke32/source/jaudiolib/src/vorbis.c | 4 +++- 2 files changed, 17 insertions(+), 7 deletions(-) diff --git a/polymer/eduke32/source/jaudiolib/src/flac.c b/polymer/eduke32/source/jaudiolib/src/flac.c index 1e52dd912..c7648b504 100644 --- a/polymer/eduke32/source/jaudiolib/src/flac.c +++ b/polymer/eduke32/source/jaudiolib/src/flac.c @@ -208,7 +208,7 @@ FLAC__StreamDecoderWriteStatus write_flac_stream(const FLAC__StreamDecoder *deco { flac_data * fd = (flac_data *) client_data; VoiceNode *voice = fd->owner; - const FLAC__uint64 samples = frame->header.blocksize; + FLAC__uint64 samples = frame->header.blocksize; UNREFERENCED_PARAMETER(decoder); @@ -216,6 +216,19 @@ FLAC__StreamDecoderWriteStatus write_flac_stream(const FLAC__StreamDecoder *deco voice->bits = frame->header.bits_per_sample; voice->SamplingRate = frame->header.sample_rate; + if (frame->header.number_type == FLAC__FRAME_NUMBER_TYPE_FRAME_NUMBER) + fd->sample_pos = frame->header.number.frame_number; + else if (frame->header.number_type == FLAC__FRAME_NUMBER_TYPE_SAMPLE_NUMBER) + fd->sample_pos = frame->header.number.sample_number; + + if ((FLAC__uint64)(uintptr_t)voice->LoopEnd > 0 && fd->sample_pos + samples >= (FLAC__uint64)(uintptr_t)voice->LoopEnd) + { + samples = (FLAC__uint64)(uintptr_t)voice->LoopEnd - fd->sample_pos; + if (!FLAC__stream_decoder_seek_absolute(fd->stream, (FLAC__uint64)(uintptr_t)voice->LoopStart)) + MV_Printf("MV_GetNextFLACBlock FLAC__stream_decoder_seek_absolute: LOOP_START %ul, LOOP_END %ul\n", + (FLAC__uint64)(uintptr_t)voice->LoopStart, (FLAC__uint64)(uintptr_t)voice->LoopEnd); + } + voice->length = ((samples * (voice->bits/8))/2)<bits; voice->position = 0; voice->BlockLength = 0; @@ -224,11 +237,6 @@ FLAC__StreamDecoderWriteStatus write_flac_stream(const FLAC__StreamDecoder *deco voice->FixedPointBufferSize = ( voice->RateScale * MV_MIXBUFFERSIZE ) - voice->RateScale; MV_SetVoiceMixMode( voice ); - if (frame->header.number_type == FLAC__FRAME_NUMBER_TYPE_FRAME_NUMBER) - fd->sample_pos = frame->header.number.frame_number; - else if (frame->header.number_type == FLAC__FRAME_NUMBER_TYPE_SAMPLE_NUMBER) - fd->sample_pos = frame->header.number.sample_number; - { const size_t size = samples * voice->channels * (voice->bits/8); if (size > fd->blocksize) diff --git a/polymer/eduke32/source/jaudiolib/src/vorbis.c b/polymer/eduke32/source/jaudiolib/src/vorbis.c index ab4f8151e..d530c96c7 100644 --- a/polymer/eduke32/source/jaudiolib/src/vorbis.c +++ b/polymer/eduke32/source/jaudiolib/src/vorbis.c @@ -242,9 +242,11 @@ static playbackstatus MV_GetNextVorbisBlock #endif //fprintf(stderr, "ov_read = %d\n", bytes); if (bytes > 0) { + ogg_int64_t currentPosition; bytesread += bytes; if ((ogg_int64_t)(intptr_t)voice->LoopEnd > 0 - && ov_pcm_tell(&vd->vf) >= (ogg_int64_t)(intptr_t)voice->LoopEnd) { + && (currentPosition = ov_pcm_tell(&vd->vf)) >= (ogg_int64_t)(intptr_t)voice->LoopEnd) { + bytesread -= (currentPosition - (ogg_int64_t)(intptr_t)voice->LoopEnd) * voice->channels * 2; // (voice->bits>>3) err = ov_pcm_seek(&vd->vf,(ogg_int64_t)(intptr_t)voice->LoopStart); if (err != 0) { MV_Printf("MV_GetNextVorbisBlock ov_pcm_seek: LOOP_START %l, LOOP_END %l, err %d\n",