From b3f3500ce5972f9a11598c91bf0e1d832db2596e Mon Sep 17 00:00:00 2001 From: Christoph Oelckers Date: Fri, 21 Apr 2017 12:29:50 +0200 Subject: [PATCH] - added loop tag reading for Ogg and Flac sound effects. Due to lack of test material this is currently untested. - removed unaligned memory access in FindLoopTags. --- src/sound/musicformats/music_libsndfile.cpp | 8 +++---- src/sound/oalsound.cpp | 23 +++++++++++++++++++++ 2 files changed, 27 insertions(+), 4 deletions(-) diff --git a/src/sound/musicformats/music_libsndfile.cpp b/src/sound/musicformats/music_libsndfile.cpp index a338d344f2..fc159314f0 100644 --- a/src/sound/musicformats/music_libsndfile.cpp +++ b/src/sound/musicformats/music_libsndfile.cpp @@ -120,15 +120,15 @@ void FindLoopTags(FileReader *fr, uint32_t *start, bool *startass, uint32_t *end continue; } c -= 3; - int len = LittleLong(*(int*)c); - if (len > 1000000 || len <= (eqp - c + 1)) + int len = c[0] + 256*c[1] + 65536*c[2]; + if (c[3] || len > 1000000 || len <= (eqp - c + 1)) { // length looks fishy so retry with the next '=' continue; } c -= 4; - count = LittleLong(*(int*)c); - if (count <= 0 || count > 1000) + count = c[0] + 256 * c[1]; + if (c[2] || c[3] || count <= 0 || count > 1000) { // very unlikely to have 1000 tags continue; diff --git a/src/sound/oalsound.cpp b/src/sound/oalsound.cpp index 0dfa479b98..4eb8d41e7f 100644 --- a/src/sound/oalsound.cpp +++ b/src/sound/oalsound.cpp @@ -1195,6 +1195,8 @@ std::pair OpenALSoundRenderer::LoadSoundRaw(uint8_t *sfxdata, return std::make_pair(retval, channels==1); } +void FindLoopTags(FileReader *fr, uint32_t *start, bool *startass, uint32_t *end, bool *endass); + std::pair OpenALSoundRenderer::LoadSound(uint8_t *sfxdata, int length, bool monoize) { SoundHandle retval = { NULL }; @@ -1203,6 +1205,14 @@ std::pair OpenALSoundRenderer::LoadSound(uint8_t *sfxdata, int ChannelConfig chans; SampleType type; int srate; + uint32_t loop_start = 0, loop_end = ~0u; + bool startass = false, endass = false; + + if (!memcmp(sfxdata, "OggS", 4) || !memcmp(sfxdata, "FLAC", 4)) + { + MemoryReader mr((char*)sfxdata, length); + FindLoopTags(&mr, &loop_start, &startass, &loop_end, &endass); + } std::unique_ptr decoder(CreateDecoder(&reader)); if(!decoder) return std::make_pair(retval, true); @@ -1269,6 +1279,19 @@ std::pair OpenALSoundRenderer::LoadSound(uint8_t *sfxdata, int return std::make_pair(retval, true); } + if (!startass) loop_start = Scale(loop_start, srate, 1000); + if (!endass) loop_end = Scale(loop_end, srate, 1000); + if (loop_start < 0) loop_start = 0; + + if ((loop_start > 0 || loop_end > 0) && loop_end > loop_start && AL.SOFT_loop_points) + { + ALint loops[2] = { loop_start, loop_end }; + DPrintf(DMSG_NOTIFY, "Setting loop points %d -> %d\n", loops[0], loops[1]); + alBufferiv(buffer, AL_LOOP_POINTS_SOFT, loops); + getALError(); + } + + retval.data = MAKE_PTRID(buffer); return std::make_pair(retval, (chans == ChannelConfig_Mono || monoize)); }