foxygit / doom Log in
commit 770fb3d7c7c23bc9b101ebbf314b142a440f6637
Author:     Simon Howard <fraggle@gmail.com>
AuthorDate: Fri Aug 3 22:17:15 2007 +0000
Commit:     Simon Howard <fraggle@gmail.com>
CommitDate: Fri Aug 3 22:17:15 2007 +0000

    Fix 'pop' at the end of sound effects caused by an audio conversion bug.

    Subversion-branch: /trunk/chocolate-doom
    Subversion-revision: 943
---
 src/i_sdlsound.c | 12 +++++++++---
 1 file changed, 9 insertions(+), 3 deletions(-)

diff --git a/src/i_sdlsound.c b/src/i_sdlsound.c
index 8e10a932..70f45470 100644
--- a/src/i_sdlsound.c
+++ b/src/i_sdlsound.c
@@ -199,12 +199,18 @@ static boolean CacheSFX(int sound)
     // If the header specifies that the length of the sound is greater than
     // the length of the lump itself, this is an invalid sound lump

-    if (length - 8 > lumplen)
+    if (length > lumplen - 8)
     {
         return false;
     }

-    expanded_length = (uint32_t) ((((uint64_t) length) * 4 * mixer_freq) / samplerate);
+    // Sample rate conversion
+
+    expanded_length = (uint32_t) ((((uint64_t) length) * mixer_freq) / samplerate);
+
+    // Double up twice: 8 -> 16 bit and mono -> stereo
+
+    expanded_length *= 4;

     sound_chunks[sound].allocated = 1;
     sound_chunks[sound].alen = expanded_length;
@@ -214,7 +220,7 @@ static boolean CacheSFX(int sound)

     ExpandSoundData(data + 8,
                     samplerate,
-                    length - 8,
+                    length,
                     &sound_chunks[sound]);

     // don't need the original lump any more