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 // fast case for direct transfer
52 if (sc->width == 1) // 8bit
53 for (i = 0;i < srclength;i++)
54 ((signed char *)sc->data)[i] = ((unsigned char *)data)[i] - 128;
55 else //if (sc->width == 2) // 16bit
56 for (i = 0;i < srclength;i++)
57 ((short *)sc->data)[i] = LittleShort (((short *)data)[i]);
62 Con_DPrintf("ResampleSfx: resampling sound %s\n", name);
64 if ((fracstep & 255) == 0) // skipping points on perfect multiple
70 short *out = (void *)sc->data, *in = (void *)data;
71 if (sc->stereo) // LordHavoc: stereo sound support
74 for (i=0 ; i<outcount ; i++)
76 *out++ = LittleShort (in[srcsample ]);
77 *out++ = LittleShort (in[srcsample+1]);
78 srcsample += fracstep;
83 for (i=0 ; i<outcount ; i++)
85 *out++ = LittleShort (in[srcsample ]);
86 srcsample += fracstep;
92 signed char *out = (void *)sc->data;
93 unsigned char *in = (void *)data;
94 if (sc->stereo) // LordHavoc: stereo sound support
97 for (i=0 ; i<outcount ; i++)
99 *out++ = in[srcsample ] - 128;
100 *out++ = in[srcsample+1] - 128;
101 srcsample += fracstep;
106 for (i=0 ; i<outcount ; i++)
108 *out++ = in[srcsample ] - 128;
109 srcsample += fracstep;
120 short *out = (void *)sc->data, *in = (void *)data;
121 if (sc->stereo) // LordHavoc: stereo sound support
123 for (i=0 ; i<outcount ; i++)
125 srcsample = (samplefrac >> 8) << 1;
127 if (srcsample+2 >= srclength)
131 sample = (((b - a) * (samplefrac & 255)) >> 8) + a;
132 *out++ = (short) sample;
134 if (srcsample+2 >= srclength)
138 sample = (((b - a) * (samplefrac & 255)) >> 8) + a;
139 *out++ = (short) sample;
140 samplefrac += fracstep;
145 for (i=0 ; i<outcount ; i++)
147 srcsample = samplefrac >> 8;
149 if (srcsample+1 >= srclength)
153 sample = (((b - a) * (samplefrac & 255)) >> 8) + a;
154 *out++ = (short) sample;
155 samplefrac += fracstep;
161 signed char *out = (void *)sc->data;
162 unsigned char *in = (void *)data;
163 if (sc->stereo) // LordHavoc: stereo sound support
165 for (i=0 ; i<outcount ; i++)
167 srcsample = (samplefrac >> 8) << 1;
168 a = (int) in[srcsample ] - 128;
169 if (srcsample+2 >= srclength)
172 b = (int) in[srcsample+2] - 128;
173 sample = (((b - a) * (samplefrac & 255)) >> 8) + a;
174 *out++ = (signed char) sample;
175 a = (int) in[srcsample+1] - 128;
176 if (srcsample+2 >= srclength)
179 b = (int) in[srcsample+3] - 128;
180 sample = (((b - a) * (samplefrac & 255)) >> 8) + a;
181 *out++ = (signed char) sample;
182 samplefrac += fracstep;
187 for (i=0 ; i<outcount ; i++)
189 srcsample = samplefrac >> 8;
190 a = (int) in[srcsample ] - 128;
191 if (srcsample+1 >= srclength)
194 b = (int) in[srcsample+1] - 128;
195 sample = (((b - a) * (samplefrac & 255)) >> 8) + a;
196 *out++ = (signed char) sample;
197 samplefrac += fracstep;
204 // 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);
208 //=============================================================================
215 sfxcache_t *S_LoadSound (sfx_t *s, int complain)
217 char namebuffer[256];
223 // see if still in memory
228 strcpy(namebuffer, "sound/");
229 strcat(namebuffer, s->name);
231 data = COM_LoadFile(namebuffer, false);
236 Con_Printf ("Couldn't load %s\n", namebuffer);
240 info = GetWavinfo (s->name, data, com_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 Mem_FreePool(&s->mempool);
264 sc->length = info.samples;
265 sc->loopstart = info.loopstart;
266 sc->speed = info.rate;
267 sc->width = info.width;
268 sc->stereo = info.channels == 2;
270 ResampleSfx (sc, data + info.dataofs, s->name);
279 ===============================================================================
283 ===============================================================================
294 short GetLittleShort(void)
298 val = val + (*(data_p+1)<<8);
303 int GetLittleLong(void)
307 val = val + (*(data_p+1)<<8);
308 val = val + (*(data_p+2)<<16);
309 val = val + (*(data_p+3)<<24);
314 void FindNextChunk(char *name)
320 if (data_p >= iff_end)
321 { // didn't find the chunk
327 iff_chunk_len = GetLittleLong();
328 if (iff_chunk_len < 0)
334 last_chunk = data_p + 8 + ( (iff_chunk_len + 1) & ~1 );
335 if (!strncmp(data_p, name, 4))
340 void FindChunk(char *name)
342 last_chunk = iff_data;
343 FindNextChunk (name);
347 void DumpChunks(void)
355 memcpy (str, data_p, 4);
357 iff_chunk_len = GetLittleLong();
358 Con_Printf ("0x%x : %s (%d)\n", (int)(data_p - 4), str, iff_chunk_len);
359 data_p += (iff_chunk_len + 1) & ~1;
360 } while (data_p < iff_end);
368 wavinfo_t GetWavinfo (char *name, qbyte *wav, int wavlength)
375 memset (&info, 0, sizeof(info));
381 iff_end = wav + wavlength;
385 if (!(data_p && !strncmp(data_p+8, "WAVE", 4)))
387 Con_Printf("Missing RIFF/WAVE chunks\n");
392 iff_data = data_p + 12;
398 Con_Printf("Missing fmt chunk\n");
402 format = GetLittleShort();
405 Con_Printf("Microsoft PCM format only\n");
409 info.channels = GetLittleShort();
410 info.rate = GetLittleLong();
412 info.width = GetLittleShort() / 8;
419 info.loopstart = GetLittleLong();
421 // if the next chunk is a LIST chunk, look for a cue length marker
422 FindNextChunk ("LIST");
425 if (!strncmp (data_p + 28, "mark", 4))
426 { // this is not a proper parse, but it works with cooledit...
428 i = GetLittleLong (); // samples in loop
429 info.samples = info.loopstart + i;
440 Con_Printf("Missing data chunk\n");
445 samples = GetLittleLong () / info.width / info.channels;
449 if (samples < info.samples)
450 Host_Error ("Sound %s has a bad loop length", name);
453 info.samples = samples;
455 info.dataofs = data_p - wav;