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
24 qbyte *S_Alloc (int size);
31 void ResampleSfx (sfxcache_t *sc, qbyte *data, char *name)
33 int i, outcount, srcsample, srclength, samplefrac, fracstep;
35 // this is usually 0.5 (128), 1 (256), or 2 (512)
36 fracstep = ((double) sc->speed / (double) shm->speed) * 256.0;
38 srclength = sc->length << sc->stereo;
40 outcount = (double) sc->length * (double) shm->speed / (double) sc->speed;
41 sc->length = outcount;
42 if (sc->loopstart != -1)
43 sc->loopstart = (double) sc->loopstart * (double) shm->speed / (double) sc->speed;
45 sc->speed = shm->speed;
47 // resample / decimate to the current source rate
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]);
61 Con_DPrintf("ResampleSfx: resampling sound %s\n", name);
63 if ((fracstep & 255) == 0) // skipping points on perfect multiple
69 short *out = (void *)sc->data, *in = (void *)data;
70 if (sc->stereo) // LordHavoc: stereo sound support
73 for (i=0 ; i<outcount ; i++)
75 *out++ = LittleShort (in[srcsample ]);
76 *out++ = LittleShort (in[srcsample+1]);
77 srcsample += fracstep;
82 for (i=0 ; i<outcount ; i++)
84 *out++ = LittleShort (in[srcsample ]);
85 srcsample += fracstep;
91 signed char *out = (void *)sc->data;
92 unsigned char *in = (void *)data;
93 if (sc->stereo) // LordHavoc: stereo sound support
96 for (i=0 ; i<outcount ; i++)
98 *out++ = in[srcsample ] - 128;
99 *out++ = in[srcsample+1] - 128;
100 srcsample += fracstep;
105 for (i=0 ; i<outcount ; i++)
107 *out++ = in[srcsample ] - 128;
108 srcsample += fracstep;
119 short *out = (void *)sc->data, *in = (void *)data;
120 if (sc->stereo) // LordHavoc: stereo sound support
122 for (i=0 ; i<outcount ; i++)
124 srcsample = (samplefrac >> 8) << 1;
126 if (srcsample+2 >= srclength)
130 sample = (((b - a) * (samplefrac & 255)) >> 8) + a;
131 *out++ = (short) sample;
133 if (srcsample+2 >= srclength)
137 sample = (((b - a) * (samplefrac & 255)) >> 8) + a;
138 *out++ = (short) sample;
139 samplefrac += fracstep;
144 for (i=0 ; i<outcount ; i++)
146 srcsample = samplefrac >> 8;
148 if (srcsample+1 >= srclength)
152 sample = (((b - a) * (samplefrac & 255)) >> 8) + a;
153 *out++ = (short) sample;
154 samplefrac += fracstep;
160 signed char *out = (void *)sc->data;
161 unsigned char *in = (void *)data;
162 if (sc->stereo) // LordHavoc: stereo sound support
164 for (i=0 ; i<outcount ; i++)
166 srcsample = (samplefrac >> 8) << 1;
167 a = (int) in[srcsample ] - 128;
168 if (srcsample+2 >= srclength)
171 b = (int) in[srcsample+2] - 128;
172 sample = (((b - a) * (samplefrac & 255)) >> 8) + a;
173 *out++ = (signed char) sample;
174 a = (int) in[srcsample+1] - 128;
175 if (srcsample+2 >= srclength)
178 b = (int) in[srcsample+3] - 128;
179 sample = (((b - a) * (samplefrac & 255)) >> 8) + a;
180 *out++ = (signed char) sample;
181 samplefrac += fracstep;
186 for (i=0 ; i<outcount ; i++)
188 srcsample = samplefrac >> 8;
189 a = (int) in[srcsample ] - 128;
190 if (srcsample+1 >= srclength)
193 b = (int) in[srcsample+1] - 128;
194 sample = (((b - a) * (samplefrac & 255)) >> 8) + a;
195 *out++ = (signed char) sample;
196 samplefrac += fracstep;
203 // LordHavoc: use this for testing if it ever becomes useful again
205 COM_WriteFile (va("sound/%s.pcm", name), sc->data, (sc->length << sc->stereo) * sc->width);
209 //=============================================================================
216 sfxcache_t *S_LoadSound (sfx_t *s)
218 char namebuffer[256];
224 // see if still in memory
228 //Con_Printf ("S_LoadSound: %x\n", (int)stackbuf);
230 strcpy(namebuffer, "sound/");
231 strcat(namebuffer, s->name);
233 // Con_Printf ("loading %s\n",namebuffer);
235 data = COM_LoadFile(namebuffer, false);
239 Con_Printf ("Couldn't load %s\n", namebuffer);
243 info = GetWavinfo (s->name, data, com_filesize);
244 // LordHavoc: stereo sounds are now allowed (intended for music)
245 if (info.channels < 1 || info.channels > 2)
247 Con_Printf ("%s has an unsupported number of channels (%i)\n",s->name, info.channels);
252 if (info.channels != 1)
254 Con_Printf ("%s is a stereo sample\n",s->name);
259 // calculate resampled length
260 len = (int) ((double) info.samples * (double) shm->speed / (double) info.rate);
261 len = len * info.width * info.channels;
263 // FIXME: add S_UnloadSounds or something?
264 Mem_FreePool(&s->mempool);
265 s->mempool = Mem_AllocPool(s->name);
266 sc = s->sfxcache = Mem_Alloc(s->mempool, len + sizeof(sfxcache_t));
269 Mem_FreePool(&s->mempool);
274 sc->length = info.samples;
275 sc->loopstart = info.loopstart;
276 sc->speed = info.rate;
277 sc->width = info.width;
278 sc->stereo = info.channels == 2;
280 ResampleSfx (sc, data + info.dataofs, s->name);
289 ===============================================================================
293 ===============================================================================
304 short GetLittleShort(void)
308 val = val + (*(data_p+1)<<8);
313 int GetLittleLong(void)
317 val = val + (*(data_p+1)<<8);
318 val = val + (*(data_p+2)<<16);
319 val = val + (*(data_p+3)<<24);
324 void FindNextChunk(char *name)
330 if (data_p >= iff_end)
331 { // didn't find the chunk
337 iff_chunk_len = GetLittleLong();
338 if (iff_chunk_len < 0)
343 // if (iff_chunk_len > 1024*1024)
344 // Sys_Error ("FindNextChunk: %i length is past the 1 meg sanity limit", iff_chunk_len);
346 last_chunk = data_p + 8 + ( (iff_chunk_len + 1) & ~1 );
347 if (!strncmp(data_p, name, 4))
352 void FindChunk(char *name)
354 last_chunk = iff_data;
355 FindNextChunk (name);
359 void DumpChunks(void)
367 memcpy (str, data_p, 4);
369 iff_chunk_len = GetLittleLong();
370 Con_Printf ("0x%x : %s (%d)\n", (int)(data_p - 4), str, iff_chunk_len);
371 data_p += (iff_chunk_len + 1) & ~1;
372 } while (data_p < iff_end);
380 wavinfo_t GetWavinfo (char *name, qbyte *wav, int wavlength)
387 memset (&info, 0, sizeof(info));
393 iff_end = wav + wavlength;
397 if (!(data_p && !strncmp(data_p+8, "WAVE", 4)))
399 Con_Printf("Missing RIFF/WAVE chunks\n");
404 iff_data = data_p + 12;
410 Con_Printf("Missing fmt chunk\n");
414 format = GetLittleShort();
417 Con_Printf("Microsoft PCM format only\n");
421 info.channels = GetLittleShort();
422 info.rate = GetLittleLong();
424 info.width = GetLittleShort() / 8;
431 info.loopstart = GetLittleLong();
432 // Con_Printf("loopstart=%d\n", sfx->loopstart);
434 // if the next chunk is a LIST chunk, look for a cue length marker
435 FindNextChunk ("LIST");
438 if (!strncmp (data_p + 28, "mark", 4))
439 { // this is not a proper parse, but it works with cooledit...
441 i = GetLittleLong (); // samples in loop
442 info.samples = info.loopstart + i;
443 // Con_Printf("looped length: %i\n", i);
454 Con_Printf("Missing data chunk\n");
459 samples = GetLittleLong () / info.width / info.channels;
463 if (samples < info.samples)
464 Host_Error ("Sound %s has a bad loop length", name);
467 info.samples = samples;
469 info.dataofs = data_p - wav;