Skip to content

Commit

Permalink
- added loop tag reading for Ogg and Flac sound effects. Due to lack …
Browse files Browse the repository at this point in the history
…of test material this is currently untested.

- removed unaligned memory access in FindLoopTags.
  • Loading branch information
coelckers committed Apr 21, 2017
1 parent 1852079 commit b3f3500
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 4 deletions.
8 changes: 4 additions & 4 deletions src/sound/musicformats/music_libsndfile.cpp
Expand Up @@ -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;
Expand Down
23 changes: 23 additions & 0 deletions src/sound/oalsound.cpp
Expand Up @@ -1195,6 +1195,8 @@ std::pair<SoundHandle,bool> 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<SoundHandle,bool> OpenALSoundRenderer::LoadSound(uint8_t *sfxdata, int length, bool monoize)
{
SoundHandle retval = { NULL };
Expand All @@ -1203,6 +1205,14 @@ std::pair<SoundHandle,bool> 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<SoundDecoder> decoder(CreateDecoder(&reader));
if(!decoder) return std::make_pair(retval, true);
Expand Down Expand Up @@ -1269,6 +1279,19 @@ std::pair<SoundHandle,bool> 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));
}
Expand Down

0 comments on commit b3f3500

Please sign in to comment.