read wave files block align value, rather then assuming 2 or 4.
authorCampbell Barton <ideasman42@gmail.com>
Sun, 12 Oct 2008 07:58:05 +0000 (07:58 +0000)
committerCampbell Barton <ideasman42@gmail.com>
Sun, 12 Oct 2008 07:58:05 +0000 (07:58 +0000)
intern/SoundSystem/intern/SND_Utils.cpp

index 58023dd5c500ae51a0b593114ea5cd97030566a7..1593dd6c7948b84e9b1396dca5742708996c5f0e 100644 (file)
@@ -287,21 +287,27 @@ unsigned int SND_GetBitRate(void* sample)
 /* gets the length of the actual sample data (without the header) */
 unsigned int SND_GetNumberOfSamples(void* sample, int sample_length)
 {
-       unsigned int chunklength, length = 0, offset = 16;
-       
+       unsigned int chunklength, length = 0, offset;
+       unsigned short block_align;
        if (CheckSample(sample))
        {
-               memcpy(&chunklength, ((char*)sample) + offset, 4);
+               memcpy(&chunklength, ((char*)sample) + 16, 4);
+               memcpy(&block_align, ((char*)sample) + 32, 2); /* always 2 or 4 it seems */
+               
                /* This was endian unsafe. See top of the file for the define. */
-               if (SND_fEndian == SND_endianBig) SWITCH_INT(chunklength);
-
-               offset = offset + chunklength + 4;
+               if (SND_fEndian == SND_endianBig)
+               {
+                       SWITCH_INT(chunklength);
+                       SWITCH_SHORT(block_align);
+               }
+                               
+               offset = 16 + chunklength + 4;
 
                /* This seems very unsafe, what if data is never found (f.i. corrupt file)... */
                // lets find "data"
                while (memcmp(((char*)sample) + offset, "data", 4))
                {
-                       offset += 2;
+                       offset += block_align;
                        
                        if (offset+4 > sample_length) /* save us from crashing */
                                return 0;
@@ -322,18 +328,24 @@ unsigned int SND_GetNumberOfSamples(void* sample, int sample_length)
 unsigned int SND_GetHeaderSize(void* sample, int sample_length)
 {
        unsigned int chunklength, headersize = 0, offset = 16;
-       
+       unsigned short block_align;
        if (CheckSample(sample))
        {
                memcpy(&chunklength, ((char*)sample) + offset, 4);
+               memcpy(&block_align, ((char*)sample) + 32, 2); /* always 2 or 4 it seems */
+               
                /* This was endian unsafe. See top of the file for the define. */
-               if (SND_fEndian == SND_endianBig) SWITCH_INT(chunklength);
+               if (SND_fEndian == SND_endianBig)
+               {
+                       SWITCH_INT(chunklength);
+                       SWITCH_SHORT(block_align);
+               }
                offset = offset + chunklength + 4;
 
                // lets find "data"
                while (memcmp(((char*)sample) + offset, "data", 4))
                {
-                       offset += 2;
+                       offset += block_align;
                        
                        if (offset+4 > sample_length) /* save us from crashing */
                                return 0;
@@ -341,12 +353,10 @@ unsigned int SND_GetHeaderSize(void* sample, int sample_length)
                headersize = offset + 8;
        }
 
-
        return headersize;
 }
 
 
-
 unsigned int SND_GetExtraChunk(void* sample)
 {
        unsigned int extrachunk = 0, chunklength, offset = 16;