-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)
-{
- return ch->sfx->fetcher_data;
-}
-
-
-snd_fetcher_t wav_fetcher = { WAV_FetchSound, NULL };
-
-
-/*
-==============
-S_LoadWavFile
-==============
-*/
-qboolean S_LoadWavFile (const char *filename, sfx_t *s)
-{
- qbyte *data;
- wavinfo_t info;
- int len;
- sfxbuffer_t* sb;
-
- Mem_FreePool (&s->mempool);
- s->mempool = Mem_AllocPool(s->name);
-
- // Load the file
- data = FS_LoadFile(filename, s->mempool, false);
- 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);
- 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)
- {
- Con_Printf("%s has an unsupported number of channels (%i)\n",s->name, info.channels);
- Mem_FreePool (&s->mempool);
- 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 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;
- short* ptr;
-
- len = info.samples * info.channels;
- ptr = (short*)(data + info.dataofs);
- for (i = 0; i < len; i++)
- ptr[i] = LittleShort (ptr[i]);
- }
-#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;
-
- Mem_Free (data);
- return true;
-}
-
-