2 Copyright (C) 1996-1997 Id Software, Inc.
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of the GNU General Public License
6 as published by the Free Software Foundation; either version 2
7 of the License, or (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13 See the GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20 // snd_mem.c: sound caching
32 void ResampleSfx (sfxcache_t *sc, qbyte *data, char *name)
34 int i, outcount, srcsample, srclength, samplefrac, fracstep;
36 // this is usually 0.5 (128), 1 (256), or 2 (512)
37 fracstep = ((double) sc->speed / (double) shm->speed) * 256.0;
39 srclength = sc->length << sc->stereo;
41 outcount = (double) sc->length * (double) shm->speed / (double) sc->speed;
42 Con_DPrintf("ResampleSfx: resampling sound %s from %dhz to %dhz (%d samples to %d samples)\n", name, sc->speed, shm->speed, sc->length, outcount);
43 sc->length = outcount;
44 if (sc->loopstart != -1)
45 sc->loopstart = (double) sc->loopstart * (double) shm->speed / (double) sc->speed;
47 sc->speed = shm->speed;
49 // resample / decimate to the current source rate
53 // fast case for direct transfer
54 if (sc->width == 1) // 8bit
55 for (i = 0;i < srclength;i++)
56 ((signed char *)sc->data)[i] = ((unsigned char *)data)[i] - 128;
57 else //if (sc->width == 2) // 16bit
58 for (i = 0;i < srclength;i++)
59 ((short *)sc->data)[i] = ((short *)data)[i];
65 if ((fracstep & 255) == 0) // skipping points on perfect multiple
71 short *out = (void *)sc->data, *in = (void *)data;
72 if (sc->stereo) // LordHavoc: stereo sound support
75 for (i=0 ; i<outcount ; i++)
77 *out++ = in[srcsample ];
78 *out++ = in[srcsample+1];
79 srcsample += fracstep;
84 for (i=0 ; i<outcount ; i++)
86 *out++ = in[srcsample];
87 srcsample += fracstep;
93 signed char *out = (void *)sc->data;
94 unsigned char *in = (void *)data;
95 if (sc->stereo) // LordHavoc: stereo sound support
98 for (i=0 ; i<outcount ; i++)
100 *out++ = in[srcsample ] - 128;
101 *out++ = in[srcsample+1] - 128;
102 srcsample += fracstep;
107 for (i=0 ; i<outcount ; i++)
109 *out++ = in[srcsample ] - 128;
110 srcsample += fracstep;
121 short *out = (void *)sc->data, *in = (void *)data;
122 if (sc->stereo) // LordHavoc: stereo sound support
124 for (i=0 ; i<outcount ; i++)
126 srcsample = (samplefrac >> 8) << 1;
128 if (srcsample+2 >= srclength)
132 sample = (((b - a) * (samplefrac & 255)) >> 8) + a;
133 *out++ = (short) sample;
135 if (srcsample+2 >= srclength)
139 sample = (((b - a) * (samplefrac & 255)) >> 8) + a;
140 *out++ = (short) sample;
141 samplefrac += fracstep;
146 for (i=0 ; i<outcount ; i++)
148 srcsample = samplefrac >> 8;
150 if (srcsample+1 >= srclength)
154 sample = (((b - a) * (samplefrac & 255)) >> 8) + a;
155 *out++ = (short) sample;
156 samplefrac += fracstep;
162 signed char *out = (void *)sc->data;
163 unsigned char *in = (void *)data;
164 if (sc->stereo) // LordHavoc: stereo sound support
166 for (i=0 ; i<outcount ; i++)
168 srcsample = (samplefrac >> 8) << 1;
169 a = (int) in[srcsample ] - 128;
170 if (srcsample+2 >= srclength)
173 b = (int) in[srcsample+2] - 128;
174 sample = (((b - a) * (samplefrac & 255)) >> 8) + a;
175 *out++ = (signed char) sample;
176 a = (int) in[srcsample+1] - 128;
177 if (srcsample+2 >= srclength)
180 b = (int) in[srcsample+3] - 128;
181 sample = (((b - a) * (samplefrac & 255)) >> 8) + a;
182 *out++ = (signed char) sample;
183 samplefrac += fracstep;
188 for (i=0 ; i<outcount ; i++)
190 srcsample = samplefrac >> 8;
191 a = (int) in[srcsample ] - 128;
192 if (srcsample+1 >= srclength)
195 b = (int) in[srcsample+1] - 128;
196 sample = (((b - a) * (samplefrac & 255)) >> 8) + a;
197 *out++ = (signed char) sample;
198 samplefrac += fracstep;
205 // LordHavoc: use this for testing if it ever becomes useful again
206 //COM_WriteFile (va("sound/%s.pcm", name), sc->data, (sc->length << sc->stereo) * sc->width);
209 //=============================================================================
216 sfxcache_t *S_LoadWavFile (const char *filename, sfx_t *s)
224 data = FS_LoadFile(filename, false);
228 // Don't try to load it if it's not a WAV file
229 if (memcmp (data, "RIFF", 4) || memcmp (data + 8, "WAVE", 4))
232 info = GetWavinfo (s->name, data, fs_filesize);
233 // Stereo sounds are allowed (intended for music)
234 if (info.channels < 1 || info.channels > 2)
236 Con_Printf("%s has an unsupported number of channels (%i)\n",s->name, info.channels);
241 // calculate resampled length
242 len = (int) ((double) info.samples * (double) shm->speed / (double) info.rate);
243 len = len * info.width * info.channels;
245 Mem_FreePool(&s->mempool);
246 s->mempool = Mem_AllocPool(s->name);
247 sc = s->sfxcache = Mem_Alloc(s->mempool, len + sizeof(sfxcache_t));
250 Con_Printf("failed to allocate memory for sound \"%s\"\n", s->name);
251 Mem_FreePool(&s->mempool);
256 sc->length = info.samples;
257 sc->loopstart = info.loopstart;
258 sc->speed = info.rate;
259 sc->width = info.width;
260 sc->stereo = info.channels == 2;
262 #if BYTE_ORDER != LITTLE_ENDIAN
263 // We must convert the WAV data from little endian
264 // to the machine endianess before resampling it
270 len = info.samples * info.channels;
271 ptr = (short*)(data + info.dataofs);
272 for (i = 0; i < len; i++)
273 ptr[i] = LittleShort (ptr[i]);
277 ResampleSfx(sc, data + info.dataofs, s->name);
289 sfxcache_t *S_LoadSound (sfx_t *s, int complain)
291 char namebuffer[MAX_QPATH];
294 qboolean modified_name = false;
296 // see if still in memory
297 if (!shm || !shm->speed)
299 if (s->sfxcache && (s->sfxcache->speed == shm->speed))
302 len = snprintf (namebuffer, sizeof (namebuffer), "sound/%s", s->name);
303 if (len >= sizeof (namebuffer))
306 // Try to load it as a WAV file
307 sc = S_LoadWavFile (namebuffer, s);
311 // Else, try to load it as an Ogg Vorbis file
312 if (!strcasecmp (namebuffer + len - 4, ".wav"))
314 strcpy (namebuffer + len - 3, "ogg");
315 modified_name = true;
317 sc = OGG_LoadVorbisFile (namebuffer, s);
321 // Can't load the sound!
323 s->flags |= SFXFLAG_SILENTLYMISSING;
325 s->flags &= ~SFXFLAG_SILENTLYMISSING;
329 strcpy (namebuffer + len - 3, "wav");
330 Con_Printf("Couldn't load %s\n", namebuffer);
335 void S_UnloadSound(sfx_t *s)
340 Mem_FreePool(&s->mempool);
346 ===============================================================================
350 ===============================================================================
354 static qbyte *data_p;
355 static qbyte *iff_end;
356 static qbyte *last_chunk;
357 static qbyte *iff_data;
358 static int iff_chunk_len;
361 short GetLittleShort(void)
365 val = BuffLittleShort (data_p);
371 int GetLittleLong(void)
375 val = BuffLittleLong (data_p);
381 void FindNextChunk(char *name)
387 if (data_p >= iff_end)
388 { // didn't find the chunk
394 iff_chunk_len = GetLittleLong();
395 if (iff_chunk_len < 0)
401 last_chunk = data_p + 8 + ( (iff_chunk_len + 1) & ~1 );
402 if (!strncmp(data_p, name, 4))
407 void FindChunk(char *name)
409 last_chunk = iff_data;
410 FindNextChunk (name);
414 void DumpChunks(void)
422 memcpy (str, data_p, 4);
424 iff_chunk_len = GetLittleLong();
425 Con_Printf("0x%x : %s (%d)\n", (int)(data_p - 4), str, iff_chunk_len);
426 data_p += (iff_chunk_len + 1) & ~1;
427 } while (data_p < iff_end);
435 wavinfo_t GetWavinfo (char *name, qbyte *wav, int wavlength)
442 memset (&info, 0, sizeof(info));
448 iff_end = wav + wavlength;
452 if (!(data_p && !strncmp(data_p+8, "WAVE", 4)))
454 Con_Print("Missing RIFF/WAVE chunks\n");
459 iff_data = data_p + 12;
465 Con_Print("Missing fmt chunk\n");
469 format = GetLittleShort();
472 Con_Print("Microsoft PCM format only\n");
476 info.channels = GetLittleShort();
477 info.rate = GetLittleLong();
479 info.width = GetLittleShort() / 8;
486 info.loopstart = GetLittleLong();
488 // if the next chunk is a LIST chunk, look for a cue length marker
489 FindNextChunk ("LIST");
492 if (!strncmp (data_p + 28, "mark", 4))
493 { // this is not a proper parse, but it works with cooledit...
495 i = GetLittleLong (); // samples in loop
496 info.samples = info.loopstart + i;
507 Con_Print("Missing data chunk\n");
512 samples = GetLittleLong () / info.width / info.channels;
516 if (samples < info.samples)
517 Host_Error ("Sound %s has a bad loop length", name);
520 info.samples = samples;
522 info.dataofs = data_p - wav;