From 5e9a9a52dbebabda79f221caf318dd8fba4b0781 Mon Sep 17 00:00:00 2001 From: Simon Howard Date: Sat, 7 Aug 2010 16:07:00 +0000 Subject: Fix sound resampling low pass filter. Subversion-branch: /trunk/chocolate-doom Subversion-revision: 1951 --- src/i_sdlsound.c | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) (limited to 'src/i_sdlsound.c') diff --git a/src/i_sdlsound.c b/src/i_sdlsound.c index d51345c7..ff191202 100644 --- a/src/i_sdlsound.c +++ b/src/i_sdlsound.c @@ -244,12 +244,15 @@ static void ExpandSoundData_SDL(byte *data, // (maximum frequency, by nyquist) dt = 1.0f / mixer_freq; - rc = 1.0f / (3.14f * samplerate); + rc = 1.0f / (2 * 3.14f * samplerate); alpha = dt / (rc + dt); - for (i=1; i @@ -41,6 +40,7 @@ #include "deh_main.h" #include "i_system.h" +#include "i_swap.h" #include "s_sound.h" #include "m_argv.h" #include "w_wad.h" @@ -49,6 +49,7 @@ #include "doomdef.h" #define LOW_PASS_FILTER +//#define DEBUG_DUMP_WAVS #define MAX_SOUND_SLICE_TIME 70 /* ms */ #define NUM_CHANNELS 16 @@ -159,12 +160,62 @@ 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. static void ExpandSoundData_SDL(byte *data, - int samplerate, - uint32_t length, - Mix_Chunk *destination) + int samplerate, + uint32_t length, + Mix_Chunk *destination) { SDL_AudioCVT convertor; uint32_t expanded_length; @@ -181,7 +232,7 @@ static void ExpandSoundData_SDL(byte *data, = Z_Malloc(expanded_length, PU_STATIC, &destination->abuf); // If we can, use the standard / optimized SDL conversion routines. - + if (samplerate <= mixer_freq && ConvertibleRatio(samplerate, mixer_freq) && SDL_BuildAudioCVT(&convertor, @@ -244,7 +295,7 @@ static void ExpandSoundData_SDL(byte *data, // (maximum frequency, by nyquist) dt = 1.0f / mixer_freq; - rc = 1.0f / (2 * 3.14f * samplerate); + rc = 1.0f / (3.14f * samplerate); alpha = dt / (rc + dt); // Both channels are processed in parallel, hence [i-2]: @@ -347,6 +398,16 @@ static boolean CacheSFX_SDL(int sound) length, &sound_chunks[sound]); +#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); -- cgit v1.2.3