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 (sfx_t *sfx, int inrate, qbyte *data, char *name)
34 int srcsample, srclength;
37 int samplefrac, fracstep;
44 stepscale = (float)inrate / shm->speed; // this is usually 0.5, 1, or 2
46 srclength = sc->length << sc->stereo;
48 outcount = sc->length / stepscale;
49 sc->length = outcount;
50 if (sc->loopstart != -1)
51 sc->loopstart = sc->loopstart / stepscale;
53 sc->speed = shm->speed;
54 // if (loadas8bit.value)
57 // sc->width = inwidth;
60 // resample / decimate to the current source rate
62 if (stepscale == 1/* && inwidth == 1*/ && sc->width == 1)
66 // LordHavoc: I do not serve the readability gods...
67 int *indata, *outdata;
69 count1 = outcount << sc->stereo;
71 indata = (void *)data;
72 outdata = (void *)sc->data;
74 *outdata++ = *indata++ ^ 0x80808080;
76 ((short*)outdata)[0] = ((short*)indata)[0] ^ 0x8080;
78 ((char*)outdata)[2] = ((char*)indata)[2] ^ 0x80;
80 if (sc->stereo) // LordHavoc: stereo sound support
82 for (i=0 ; i<outcount ; i++)
83 ((signed char *)sc->data)[i] = ((unsigned char *)data)[i] - 128;
85 else if (stepscale == 1/* && inwidth == 2*/ && sc->width == 2) // LordHavoc: quick case for 16bit
87 if (sc->stereo) // LordHavoc: stereo sound support
89 for (i=0 ; i<outcount ;i++)
90 ((short *)sc->data)[i] = LittleShort (((short *)data)[i]);
95 Con_DPrintf("ResampleSfx: resampling sound %s\n", sfx->name);
97 fracstep = stepscale*256;
98 if ((fracstep & 255) == 0) // skipping points on perfect multiple
104 short *out = (void *)sc->data, *in = (void *)data;
105 if (sc->stereo) // LordHavoc: stereo sound support
108 for (i=0 ; i<outcount ; i++)
110 *out++ = LittleShort (in[srcsample ]);
111 *out++ = LittleShort (in[srcsample+1]);
112 srcsample += fracstep;
117 for (i=0 ; i<outcount ; i++)
119 *out++ = LittleShort (in[srcsample ]);
120 srcsample += fracstep;
126 signed char *out = (void *)sc->data;
127 unsigned char *in = (void *)data;
128 if (sc->stereo) // LordHavoc: stereo sound support
131 for (i=0 ; i<outcount ; i++)
133 *out++ = in[srcsample ] - 128;
134 *out++ = in[srcsample+1] - 128;
135 srcsample += fracstep;
140 for (i=0 ; i<outcount ; i++)
142 *out++ = in[srcsample ] - 128;
143 srcsample += fracstep;
154 short *out = (void *)sc->data, *in = (void *)data;
155 if (sc->stereo) // LordHavoc: stereo sound support
157 for (i=0 ; i<outcount ; i++)
159 srcsample = (samplefrac >> 8) << 1;
161 if (srcsample+2 >= srclength)
165 sample = (((b - a) * (samplefrac & 255)) >> 8) + a;
166 *out++ = (short) sample;
168 if (srcsample+2 >= srclength)
172 sample = (((b - a) * (samplefrac & 255)) >> 8) + a;
173 *out++ = (short) sample;
174 samplefrac += fracstep;
179 for (i=0 ; i<outcount ; i++)
181 srcsample = samplefrac >> 8;
183 if (srcsample+1 >= srclength)
187 sample = (((b - a) * (samplefrac & 255)) >> 8) + a;
188 *out++ = (short) sample;
189 samplefrac += fracstep;
195 signed char *out = (void *)sc->data;
196 unsigned char *in = (void *)data;
197 if (sc->stereo) // LordHavoc: stereo sound support
199 for (i=0 ; i<outcount ; i++)
201 srcsample = (samplefrac >> 8) << 1;
202 a = (int) in[srcsample ] - 128;
203 if (srcsample+2 >= srclength)
206 b = (int) in[srcsample+2] - 128;
207 sample = (((b - a) * (samplefrac & 255)) >> 8) + a;
208 *out++ = (signed char) sample;
209 a = (int) in[srcsample+1] - 128;
210 if (srcsample+2 >= srclength)
213 b = (int) in[srcsample+3] - 128;
214 sample = (((b - a) * (samplefrac & 255)) >> 8) + a;
215 *out++ = (signed char) sample;
216 samplefrac += fracstep;
221 for (i=0 ; i<outcount ; i++)
223 srcsample = samplefrac >> 8;
224 a = (int) in[srcsample ] - 128;
225 if (srcsample+1 >= srclength)
228 b = (int) in[srcsample+1] - 128;
229 sample = (((b - a) * (samplefrac & 255)) >> 8) + a;
230 *out++ = (signed char) sample;
231 samplefrac += fracstep;
238 // LordHavoc: use this for testing if it ever becomes useful again
240 COM_WriteFile (va("sound/%s.pcm", name), sc->data, (sc->length << sc->stereo) * sc->width);
244 //=============================================================================
251 sfxcache_t *S_LoadSound (sfx_t *s)
253 char namebuffer[256];
260 // see if still in memory
264 //Con_Printf ("S_LoadSound: %x\n", (int)stackbuf);
266 strcpy(namebuffer, "sound/");
267 strcat(namebuffer, s->name);
269 // Con_Printf ("loading %s\n",namebuffer);
271 data = COM_LoadFile(namebuffer, false);
275 Con_Printf ("Couldn't load %s\n", namebuffer);
279 info = GetWavinfo (s->name, data, com_filesize);
280 // LordHavoc: stereo sounds are now allowed (intended for music)
281 if (info.channels < 1 || info.channels > 2)
283 Con_Printf ("%s has an unsupported number of channels (%i)\n",s->name, info.channels);
288 if (info.channels != 1)
290 Con_Printf ("%s is a stereo sample\n",s->name);
295 stepscale = (float)info.rate / shm->speed;
296 len = info.samples / stepscale;
298 len = len * info.width * info.channels;
300 // FIXME: add S_UnloadSounds or something?
301 Mem_FreePool(&s->mempool);
302 s->mempool = Mem_AllocPool(s->name);
303 sc = s->sfxcache = Mem_Alloc(s->mempool, len + sizeof(sfxcache_t));
310 sc->length = info.samples;
311 sc->loopstart = info.loopstart;
312 sc->speed = info.rate;
313 sc->width = info.width;
314 sc->stereo = info.channels == 2;
316 ResampleSfx (s, sc->speed, data + info.dataofs, s->name);
325 ===============================================================================
329 ===============================================================================
340 short GetLittleShort(void)
344 val = val + (*(data_p+1)<<8);
349 int GetLittleLong(void)
353 val = val + (*(data_p+1)<<8);
354 val = val + (*(data_p+2)<<16);
355 val = val + (*(data_p+3)<<24);
360 void FindNextChunk(char *name)
366 if (data_p >= iff_end)
367 { // didn't find the chunk
373 iff_chunk_len = GetLittleLong();
374 if (iff_chunk_len < 0)
379 // if (iff_chunk_len > 1024*1024)
380 // Sys_Error ("FindNextChunk: %i length is past the 1 meg sanity limit", iff_chunk_len);
382 last_chunk = data_p + 8 + ( (iff_chunk_len + 1) & ~1 );
383 if (!strncmp(data_p, name, 4))
388 void FindChunk(char *name)
390 last_chunk = iff_data;
391 FindNextChunk (name);
395 void DumpChunks(void)
403 memcpy (str, data_p, 4);
405 iff_chunk_len = GetLittleLong();
406 Con_Printf ("0x%x : %s (%d)\n", (int)(data_p - 4), str, iff_chunk_len);
407 data_p += (iff_chunk_len + 1) & ~1;
408 } while (data_p < iff_end);
416 wavinfo_t GetWavinfo (char *name, qbyte *wav, int wavlength)
423 memset (&info, 0, sizeof(info));
429 iff_end = wav + wavlength;
433 if (!(data_p && !strncmp(data_p+8, "WAVE", 4)))
435 Con_Printf("Missing RIFF/WAVE chunks\n");
440 iff_data = data_p + 12;
446 Con_Printf("Missing fmt chunk\n");
450 format = GetLittleShort();
453 Con_Printf("Microsoft PCM format only\n");
457 info.channels = GetLittleShort();
458 info.rate = GetLittleLong();
460 info.width = GetLittleShort() / 8;
467 info.loopstart = GetLittleLong();
468 // Con_Printf("loopstart=%d\n", sfx->loopstart);
470 // if the next chunk is a LIST chunk, look for a cue length marker
471 FindNextChunk ("LIST");
474 if (!strncmp (data_p + 28, "mark", 4))
475 { // this is not a proper parse, but it works with cooledit...
477 i = GetLittleLong (); // samples in loop
478 info.samples = info.loopstart + i;
479 // Con_Printf("looped length: %i\n", i);
490 Con_Printf("Missing data chunk\n");
495 samples = GetLittleLong () / info.width;
499 if (samples < info.samples)
500 Host_Error ("Sound %s has a bad loop length", name);
503 info.samples = samples;
505 info.dataofs = data_p - wav;