X-Git-Url: http://de.git.xonotic.org/?p=xonotic%2Fdarkplaces.git;a=blobdiff_plain;f=snd_wav.c;h=7ce6b2744d3b164ecfd0e0a7a038a1d17f5bd32c;hp=e11c24563f5684ea674d2b19768a66b33f374dd8;hb=e0ed43d2d6fdae2375244257789852ecdf38893a;hpb=a1e05a11f1c94c609e55a1a25d72e5ed2eaf5ecb diff --git a/snd_wav.c b/snd_wav.c index e11c2456..7ce6b274 100644 --- a/snd_wav.c +++ b/snd_wav.c @@ -23,10 +23,11 @@ #include "quakedef.h" +#include "snd_main.h" #include "snd_wav.h" -typedef struct +typedef struct wavinfo_s { int rate; int width; @@ -37,10 +38,10 @@ typedef struct } wavinfo_t; -static qbyte *data_p; -static qbyte *iff_end; -static qbyte *last_chunk; -static qbyte *iff_data; +static unsigned char *data_p; +static unsigned char *iff_end; +static unsigned char *last_chunk; +static unsigned char *iff_data; static int iff_chunk_len; @@ -85,7 +86,7 @@ static void FindNextChunk(char *name) } data_p -= 8; last_chunk = data_p + 8 + ( (iff_chunk_len + 1) & ~1 ); - if (!strncmp(data_p, name, 4)) + if (!strncmp((const char *)data_p, name, 4)) return; } } @@ -121,7 +122,7 @@ static void DumpChunks(void) GetWavinfo ============ */ -static wavinfo_t GetWavinfo (char *name, qbyte *wav, int wavlength) +static wavinfo_t GetWavinfo (char *name, unsigned char *wav, int wavlength) { wavinfo_t info; int i; @@ -138,7 +139,7 @@ static wavinfo_t GetWavinfo (char *name, qbyte *wav, int wavlength) // find "RIFF" chunk FindChunk("RIFF"); - if (!(data_p && !strncmp(data_p+8, "WAVE", 4))) + if (!(data_p && !strncmp((const char *)data_p+8, "WAVE", 4))) { Con_Print("Missing RIFF/WAVE chunks\n"); return info; @@ -178,7 +179,7 @@ static wavinfo_t GetWavinfo (char *name, qbyte *wav, int wavlength) FindNextChunk ("LIST"); if (data_p) { - if (!strncmp (data_p + 28, "mark", 4)) + if (!strncmp ((const char *)data_p + 28, "mark", 4)) { // this is not a proper parse, but it works with cooledit... data_p += 24; i = GetLittleLong (); // samples in loop @@ -203,7 +204,10 @@ static wavinfo_t GetWavinfo (char *name, qbyte *wav, int wavlength) if (info.samples) { if (samples < info.samples) - Host_Error ("Sound %s has a bad loop length", name); + { + Con_Printf ("Sound %s has a bad loop length\n", name); + info.samples = samples; + } } else info.samples = samples; @@ -219,13 +223,36 @@ static wavinfo_t GetWavinfo (char *name, qbyte *wav, int wavlength) WAV_FetchSound ==================== */ -static const sfxbuffer_t* WAV_FetchSound (channel_t* ch, unsigned int start, unsigned int nbsamples) +static const snd_buffer_t* WAV_FetchSound (void *sfxfetcher, void **chfetcherpointer, unsigned int *start, unsigned int nbsampleframes) { - return ch->sfx->fetcher_data; + *start = 0; + return (snd_buffer_t *)sfxfetcher; } +/* +==================== +WAV_FreeSfx +==================== +*/ +static void WAV_FreeSfx (void *sfxfetcherdata) +{ + snd_buffer_t* sb = (snd_buffer_t *)sfxfetcherdata; + // Free the sound buffer + Mem_Free(sb); +} -snd_fetcher_t wav_fetcher = { WAV_FetchSound, NULL }; +/* +==================== +WAV_GetFormat +==================== +*/ +static const snd_format_t* WAV_GetFormat (sfx_t* sfx) +{ + snd_buffer_t* sb = (snd_buffer_t *)sfx->fetcher_data; + return &sb->format; +} + +const snd_fetcher_t wav_fetcher = { WAV_FetchSound, NULL, WAV_FreeSfx, WAV_GetFormat }; /* @@ -233,70 +260,48 @@ snd_fetcher_t wav_fetcher = { WAV_FetchSound, NULL }; S_LoadWavFile ============== */ -qboolean S_LoadWavFile (const char *filename, sfx_t *s) +qboolean S_LoadWavFile (const char *filename, sfx_t *sfx) { - qbyte *data; + fs_offset_t filesize; + unsigned char *data; wavinfo_t info; - int len; - sfxbuffer_t* sb; + snd_format_t wav_format; + snd_buffer_t* sb; - Mem_FreePool (&s->mempool); - s->mempool = Mem_AllocPool(s->name); + // Already loaded? + if (sfx->fetcher != NULL) + return true; // Load the file - data = FS_LoadFile(filename, s->mempool, false); + data = FS_LoadFile(filename, snd_mempool, false, &filesize); if (!data) - { - Mem_FreePool (&s->mempool); return false; - } // Don't try to load it if it's not a WAV file if (memcmp (data, "RIFF", 4) || memcmp (data + 8, "WAVE", 4)) { - Mem_FreePool (&s->mempool); + Mem_Free(data); return false; } Con_DPrintf ("Loading WAV file \"%s\"\n", filename); - info = GetWavinfo (s->name, data, fs_filesize); - // Stereo sounds are allowed (intended for music) - if (info.channels < 1 || info.channels > 2) + info = GetWavinfo (sfx->name, data, (int)filesize); + if (info.channels < 1 || info.channels > 2) // Stereo sounds are allowed (intended for music) { - Con_Printf("%s has an unsupported number of channels (%i)\n",s->name, info.channels); - Mem_FreePool (&s->mempool); + Con_Printf("%s has an unsupported number of channels (%i)\n",sfx->name, info.channels); + Mem_Free(data); return false; } - - // calculate resampled length - len = (int) ((double) info.samples * (double) shm->format.speed / (double) info.rate); - len = len * info.width * info.channels; - - sb = Mem_Alloc (s->mempool, len + sizeof (*sb) - sizeof (sb->data)); - if (sb == NULL) - { - Con_Printf("failed to allocate memory for sound \"%s\"\n", s->name); - Mem_FreePool(&s->mempool); - return false; - } - - s->fetcher = &wav_fetcher; - s->fetcher_data = sb; - s->format.speed = info.rate; - s->format.width = info.width; - s->format.channels = info.channels; - if (info.loopstart < 0) - s->loopstart = -1; - else - s->loopstart = (double)info.loopstart * (double)shm->format.speed / (double)s->format.speed; + //if (info.channels == 2) + // Log_Printf("stereosounds.log", "%s\n", sfx->name); #if BYTE_ORDER != LITTLE_ENDIAN // We must convert the WAV data from little endian // to the machine endianess before resampling it if (info.width == 2) { - int i; + unsigned int len, i; short* ptr; len = info.samples * info.channels; @@ -306,10 +311,27 @@ qboolean S_LoadWavFile (const char *filename, sfx_t *s) } #endif - sb->length = ResampleSfx (data + info.dataofs, info.samples, &s->format, sb->data, s->name); - s->format.speed = shm->format.speed; - s->total_length = sb->length; - sb->offset = 0; + wav_format.speed = info.rate; + wav_format.width = info.width; + wav_format.channels = info.channels; + sb = Snd_CreateSndBuffer (data + info.dataofs, info.samples, &wav_format, snd_renderbuffer->format.speed); + if (sb == NULL) + { + Mem_Free(data); + return false; + } + sfx->fetcher = &wav_fetcher; + sfx->fetcher_data = sb; + + sfx->total_length = sb->nbframes; + sfx->memsize += sb->maxframes * sb->format.channels * sb->format.width + sizeof (*sb) - sizeof (sb->samples); + + if (info.loopstart < 0) + sfx->loopstart = sfx->total_length; + else + sfx->loopstart = (double)info.loopstart * (double)snd_renderbuffer->format.speed / (double)sb->format.speed; + sfx->loopstart = min(sfx->loopstart, sfx->total_length); + sfx->flags &= ~SFXFLAG_STREAMED; Mem_Free (data); return true;