summaryrefslogtreecommitdiff
path: root/src/i_sdlsound.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/i_sdlsound.c')
-rw-r--r--src/i_sdlsound.c72
1 files changed, 68 insertions, 4 deletions
diff --git a/src/i_sdlsound.c b/src/i_sdlsound.c
index 0b3f8aa3..7deb683d 100644
--- a/src/i_sdlsound.c
+++ b/src/i_sdlsound.c
@@ -25,7 +25,6 @@
//
//-----------------------------------------------------------------------------
-
#include "config.h"
#include <stdio.h>
@@ -42,6 +41,7 @@
#include "deh_str.h"
#include "i_sound.h"
#include "i_system.h"
+#include "i_swap.h"
#include "m_argv.h"
#include "w_wad.h"
#include "z_zone.h"
@@ -49,6 +49,7 @@
#include "doomtype.h"
#define LOW_PASS_FILTER
+//#define DEBUG_DUMP_WAVS
#define MAX_SOUND_SLICE_TIME 70 /* ms */
#define NUM_CHANNELS 16
@@ -288,6 +289,56 @@ static boolean ConvertibleRatio(int freq1, int freq2)
}
}
+#ifdef DEBUG_DUMP_WAVS
+
+// Debug code to dump resampled sound effects to WAV files for analysis.
+
+static void WriteWAV(char *filename, byte *data,
+ uint32_t length, int samplerate)
+{
+ FILE *wav;
+ unsigned int i;
+ unsigned short s;
+
+ wav = fopen(filename, "wb");
+
+ // Header
+
+ fwrite("RIFF", 1, 4, wav);
+ i = LONG(36 + samplerate);
+ fwrite(&i, 4, 1, wav);
+ fwrite("WAVE", 1, 4, wav);
+
+ // Subchunk 1
+
+ fwrite("fmt ", 1, 4, wav);
+ i = LONG(16);
+ fwrite(&i, 4, 1, wav); // Length
+ s = SHORT(1);
+ fwrite(&s, 2, 1, wav); // Format (PCM)
+ s = SHORT(2);
+ fwrite(&s, 2, 1, wav); // Channels (2=stereo)
+ i = LONG(samplerate);
+ fwrite(&i, 4, 1, wav); // Sample rate
+ i = LONG(samplerate * 2 * 2);
+ fwrite(&i, 4, 1, wav); // Byte rate (samplerate * stereo * 16 bit)
+ s = SHORT(2 * 2);
+ fwrite(&s, 2, 1, wav); // Block align (stereo * 16 bit)
+ s = SHORT(16);
+ fwrite(&s, 2, 1, wav); // Bits per sample (16 bit)
+
+ // Data subchunk
+
+ fwrite("data", 1, 4, wav);
+ i = LONG(length);
+ fwrite(&i, 4, 1, wav); // Data length
+ fwrite(data, 1, length, wav); // Data
+
+ fclose(wav);
+}
+
+#endif
+
// Generic sound expansion function for any sample rate.
// Returns number of clipped samples (always 0).
@@ -313,7 +364,7 @@ static void ExpandSoundData_SDL(sfxinfo_t *sfxinfo,
chunk = AllocateChunk(sfxinfo, expanded_length);
// If we can, use the standard / optimized SDL conversion routines.
-
+
if (samplerate <= mixer_freq
&& ConvertibleRatio(samplerate, mixer_freq)
&& SDL_BuildAudioCVT(&convertor,
@@ -379,9 +430,12 @@ static void ExpandSoundData_SDL(sfxinfo_t *sfxinfo,
rc = 1.0f / (3.14f * samplerate);
alpha = dt / (rc + dt);
- for (i=1; i<expanded_length; ++i)
+ // Both channels are processed in parallel, hence [i-2]:
+
+ for (i=2; i<expanded_length * 2; ++i)
{
- expanded[i] = (Sint16) (alpha * expanded[i] + (1 - alpha) * expanded[i-1]);
+ expanded[i] = (Sint16) (alpha * expanded[i]
+ + (1 - alpha) * expanded[i-2]);
}
}
#endif /* #ifdef LOW_PASS_FILTER */
@@ -432,6 +486,16 @@ static boolean CacheSFX(sfxinfo_t *sfxinfo)
ExpandSoundData(sfxinfo, data + 8, samplerate, length);
+#ifdef DEBUG_DUMP_WAVS
+ {
+ char filename[16];
+
+ sprintf(filename, "%s.wav", DEH_String(S_sfx[sound].name));
+ WriteWAV(filename, sound_chunks[sound].abuf,
+ sound_chunks[sound].alen, mixer_freq);
+ }
+#endif
+
// don't need the original lump any more
W_ReleaseLumpNum(lumpnum);