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 = FS_LoadFile(namebuffer, false);
235 s->silentlymissing = !complain;
237 Con_Printf ("Couldn't load %s\n", namebuffer);
241 info = GetWavinfo (s->name, data, fs_filesize);
242 // LordHavoc: stereo sounds are now allowed (intended for music)
243 if (info.channels < 1 || info.channels > 2)
245 Con_Printf ("%s has an unsupported number of channels (%i)\n",s->name, info.channels);
250 // calculate resampled length
251 len = (int) ((double) info.samples * (double) shm->speed / (double) info.rate);
252 len = len * info.width * info.channels;
254 // FIXME: add S_UnloadSounds or something?
255 Mem_FreePool(&s->mempool);
256 s->mempool = Mem_AllocPool(s->name);
257 sc = s->sfxcache = Mem_Alloc(s->mempool, len + sizeof(sfxcache_t));
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);
280 ===============================================================================
284 ===============================================================================
295 short GetLittleShort(void)
299 val = val + (*(data_p+1)<<8);
304 int GetLittleLong(void)
308 val = val + (*(data_p+1)<<8);
309 val = val + (*(data_p+2)<<16);
310 val = val + (*(data_p+3)<<24);
315 void FindNextChunk(char *name)
321 if (data_p >= iff_end)
322 { // didn't find the chunk
328 iff_chunk_len = GetLittleLong();
329 if (iff_chunk_len < 0)
335 last_chunk = data_p + 8 + ( (iff_chunk_len + 1) & ~1 );
336 if (!strncmp(data_p, name, 4))
341 void FindChunk(char *name)
343 last_chunk = iff_data;
344 FindNextChunk (name);
348 void DumpChunks(void)
356 memcpy (str, data_p, 4);
358 iff_chunk_len = GetLittleLong();
359 Con_Printf ("0x%x : %s (%d)\n", (int)(data_p - 4), str, iff_chunk_len);
360 data_p += (iff_chunk_len + 1) & ~1;
361 } while (data_p < iff_end);
369 wavinfo_t GetWavinfo (char *name, qbyte *wav, int wavlength)
376 memset (&info, 0, sizeof(info));
382 iff_end = wav + wavlength;
386 if (!(data_p && !strncmp(data_p+8, "WAVE", 4)))
388 Con_Printf("Missing RIFF/WAVE chunks\n");
393 iff_data = data_p + 12;
399 Con_Printf("Missing fmt chunk\n");
403 format = GetLittleShort();
406 Con_Printf("Microsoft PCM format only\n");
410 info.channels = GetLittleShort();
411 info.rate = GetLittleLong();
413 info.width = GetLittleShort() / 8;
420 info.loopstart = GetLittleLong();
422 // if the next chunk is a LIST chunk, look for a cue length marker
423 FindNextChunk ("LIST");
426 if (!strncmp (data_p + 28, "mark", 4))
427 { // this is not a proper parse, but it works with cooledit...
429 i = GetLittleLong (); // samples in loop
430 info.samples = info.loopstart + i;
441 Con_Printf("Missing data chunk\n");
446 samples = GetLittleLong () / info.width / info.channels;
450 if (samples < info.samples)
451 Host_Error ("Sound %s has a bad loop length", name);
454 info.samples = samples;
456 info.dataofs = data_p - wav;