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
29 void ResampleSfx (sfxcache_t *sc, qbyte *data, char *name)
31 int i, outcount, srcsample, srclength, samplefrac, fracstep;
33 // this is usually 0.5 (128), 1 (256), or 2 (512)
34 fracstep = ((double) sc->speed / (double) shm->speed) * 256.0;
36 srclength = sc->length << sc->stereo;
38 outcount = (double) sc->length * (double) shm->speed / (double) sc->speed;
39 Con_DPrintf("ResampleSfx: resampling sound %s from %dhz to %dhz (%d samples to %d samples)\n", name, sc->speed, shm->speed, sc->length, outcount);
40 sc->length = outcount;
41 if (sc->loopstart != -1)
42 sc->loopstart = (double) sc->loopstart * (double) shm->speed / (double) sc->speed;
44 sc->speed = shm->speed;
46 // resample / decimate to the current source rate
50 // fast case for direct transfer
51 if (sc->width == 1) // 8bit
52 for (i = 0;i < srclength;i++)
53 ((signed char *)sc->data)[i] = ((unsigned char *)data)[i] - 128;
54 else //if (sc->width == 2) // 16bit
55 for (i = 0;i < srclength;i++)
56 ((short *)sc->data)[i] = LittleShort (((short *)data)[i]);
62 if ((fracstep & 255) == 0) // skipping points on perfect multiple
68 short *out = (void *)sc->data, *in = (void *)data;
69 if (sc->stereo) // LordHavoc: stereo sound support
72 for (i=0 ; i<outcount ; i++)
74 *out++ = LittleShort (in[srcsample ]);
75 *out++ = LittleShort (in[srcsample+1]);
76 srcsample += fracstep;
81 for (i=0 ; i<outcount ; i++)
83 *out++ = LittleShort (in[srcsample ]);
84 srcsample += fracstep;
90 signed char *out = (void *)sc->data;
91 unsigned char *in = (void *)data;
92 if (sc->stereo) // LordHavoc: stereo sound support
95 for (i=0 ; i<outcount ; i++)
97 *out++ = in[srcsample ] - 128;
98 *out++ = in[srcsample+1] - 128;
99 srcsample += fracstep;
104 for (i=0 ; i<outcount ; i++)
106 *out++ = in[srcsample ] - 128;
107 srcsample += fracstep;
118 short *out = (void *)sc->data, *in = (void *)data;
119 if (sc->stereo) // LordHavoc: stereo sound support
121 for (i=0 ; i<outcount ; i++)
123 srcsample = (samplefrac >> 8) << 1;
125 if (srcsample+2 >= srclength)
129 sample = (((b - a) * (samplefrac & 255)) >> 8) + a;
130 *out++ = (short) sample;
132 if (srcsample+2 >= srclength)
136 sample = (((b - a) * (samplefrac & 255)) >> 8) + a;
137 *out++ = (short) sample;
138 samplefrac += fracstep;
143 for (i=0 ; i<outcount ; i++)
145 srcsample = samplefrac >> 8;
147 if (srcsample+1 >= srclength)
151 sample = (((b - a) * (samplefrac & 255)) >> 8) + a;
152 *out++ = (short) sample;
153 samplefrac += fracstep;
159 signed char *out = (void *)sc->data;
160 unsigned char *in = (void *)data;
161 if (sc->stereo) // LordHavoc: stereo sound support
163 for (i=0 ; i<outcount ; i++)
165 srcsample = (samplefrac >> 8) << 1;
166 a = (int) in[srcsample ] - 128;
167 if (srcsample+2 >= srclength)
170 b = (int) in[srcsample+2] - 128;
171 sample = (((b - a) * (samplefrac & 255)) >> 8) + a;
172 *out++ = (signed char) sample;
173 a = (int) in[srcsample+1] - 128;
174 if (srcsample+2 >= srclength)
177 b = (int) in[srcsample+3] - 128;
178 sample = (((b - a) * (samplefrac & 255)) >> 8) + a;
179 *out++ = (signed char) sample;
180 samplefrac += fracstep;
185 for (i=0 ; i<outcount ; i++)
187 srcsample = samplefrac >> 8;
188 a = (int) in[srcsample ] - 128;
189 if (srcsample+1 >= srclength)
192 b = (int) in[srcsample+1] - 128;
193 sample = (((b - a) * (samplefrac & 255)) >> 8) + a;
194 *out++ = (signed char) sample;
195 samplefrac += fracstep;
202 // LordHavoc: use this for testing if it ever becomes useful again
203 //COM_WriteFile (va("sound/%s.pcm", name), sc->data, (sc->length << sc->stereo) * sc->width);
206 //=============================================================================
213 sfxcache_t *S_LoadSound (sfx_t *s, int complain)
215 char namebuffer[MAX_QPATH];
221 // see if still in memory
222 if (!shm || !shm->speed)
224 if (s->sfxcache && s->sfxcache->speed == shm->speed)
228 snprintf(namebuffer, sizeof(namebuffer), "sound/%s", s->name);
230 data = FS_LoadFile(namebuffer, false);
234 s->silentlymissing = !complain;
236 Con_Printf("Couldn't load %s\n", namebuffer);
240 info = GetWavinfo (s->name, data, fs_filesize);
241 // LordHavoc: stereo sounds are now allowed (intended for music)
242 if (info.channels < 1 || info.channels > 2)
244 Con_Printf("%s has an unsupported number of channels (%i)\n",s->name, info.channels);
249 // calculate resampled length
250 len = (int) ((double) info.samples * (double) shm->speed / (double) info.rate);
251 len = len * info.width * info.channels;
253 // FIXME: add S_UnloadSounds or something?
254 Mem_FreePool(&s->mempool);
255 s->mempool = Mem_AllocPool(s->name);
256 sc = s->sfxcache = Mem_Alloc(s->mempool, len + sizeof(sfxcache_t));
259 Con_Printf("failed to allocate memory for sound \"%s\"\n", s->name);
260 Mem_FreePool(&s->mempool);
265 sc->length = info.samples;
266 sc->loopstart = info.loopstart;
267 sc->speed = info.rate;
268 sc->width = info.width;
269 sc->stereo = info.channels == 2;
271 ResampleSfx(sc, data + info.dataofs, s->name);
277 void S_UnloadSound(sfx_t *s)
282 Mem_FreePool(&s->mempool);
288 ===============================================================================
292 ===============================================================================
303 short GetLittleShort(void)
307 val = val + (*(data_p+1)<<8);
312 int GetLittleLong(void)
316 val = val + (*(data_p+1)<<8);
317 val = val + (*(data_p+2)<<16);
318 val = val + (*(data_p+3)<<24);
323 void FindNextChunk(char *name)
329 if (data_p >= iff_end)
330 { // didn't find the chunk
336 iff_chunk_len = GetLittleLong();
337 if (iff_chunk_len < 0)
343 last_chunk = data_p + 8 + ( (iff_chunk_len + 1) & ~1 );
344 if (!strncmp(data_p, name, 4))
349 void FindChunk(char *name)
351 last_chunk = iff_data;
352 FindNextChunk (name);
356 void DumpChunks(void)
364 memcpy (str, data_p, 4);
366 iff_chunk_len = GetLittleLong();
367 Con_Printf ("0x%x : %s (%d)\n", (int)(data_p - 4), str, iff_chunk_len);
368 data_p += (iff_chunk_len + 1) & ~1;
369 } while (data_p < iff_end);
377 wavinfo_t GetWavinfo (char *name, qbyte *wav, int wavlength)
384 memset (&info, 0, sizeof(info));
390 iff_end = wav + wavlength;
394 if (!(data_p && !strncmp(data_p+8, "WAVE", 4)))
396 Con_Printf("Missing RIFF/WAVE chunks\n");
401 iff_data = data_p + 12;
407 Con_Printf("Missing fmt chunk\n");
411 format = GetLittleShort();
414 Con_Printf("Microsoft PCM format only\n");
418 info.channels = GetLittleShort();
419 info.rate = GetLittleLong();
421 info.width = GetLittleShort() / 8;
428 info.loopstart = GetLittleLong();
430 // if the next chunk is a LIST chunk, look for a cue length marker
431 FindNextChunk ("LIST");
434 if (!strncmp (data_p + 28, "mark", 4))
435 { // this is not a proper parse, but it works with cooledit...
437 i = GetLittleLong (); // samples in loop
438 info.samples = info.loopstart + i;
449 Con_Printf("Missing data chunk\n");
454 samples = GetLittleLong () / info.width / info.channels;
458 if (samples < info.samples)
459 Host_Error ("Sound %s has a bad loop length", name);
462 info.samples = samples;
464 info.dataofs = data_p - wav;