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
26 byte *S_Alloc (int size);
33 void ResampleSfx (sfx_t *sfx, int inrate, byte *data, char *name)
36 int srcsample, srclength;
39 int samplefrac, fracstep;
42 sc = Cache_Check (&sfx->cache);
46 stepscale = (float)inrate / shm->speed; // this is usually 0.5, 1, or 2
48 srclength = sc->length << sc->stereo;
50 outcount = sc->length / stepscale;
51 sc->length = outcount;
52 if (sc->loopstart != -1)
53 sc->loopstart = sc->loopstart / stepscale;
55 sc->speed = shm->speed;
56 // if (loadas8bit.value)
59 // sc->width = inwidth;
62 // resample / decimate to the current source rate
64 if (stepscale == 1/* && inwidth == 1*/ && sc->width == 1)
68 // LordHavoc: I do not serve the readability gods...
69 int *indata, *outdata;
71 count1 = outcount << sc->stereo;
73 indata = (void *)data;
74 outdata = (void *)sc->data;
76 *outdata++ = *indata++ ^ 0x80808080;
78 ((short*)outdata)[0] = ((short*)indata)[0] ^ 0x8080;
80 ((char*)outdata)[2] = ((char*)indata)[2] ^ 0x80;
82 if (sc->stereo) // LordHavoc: stereo sound support
84 for (i=0 ; i<outcount ; i++)
85 ((signed char *)sc->data)[i] = ((unsigned char *)data)[i] - 128;
87 else if (stepscale == 1/* && inwidth == 2*/ && sc->width == 2) // LordHavoc: quick case for 16bit
89 if (sc->stereo) // LordHavoc: stereo sound support
91 for (i=0 ; i<outcount ;i++)
92 ((short *)sc->data)[i] = LittleShort (((short *)data)[i]);
97 Con_DPrintf("ResampleSfx: resampling sound %s\n", sfx->name);
99 fracstep = stepscale*256;
100 if ((fracstep & 255) == 0) // skipping points on perfect multiple
106 short *out = (void *)sc->data, *in = (void *)data;
107 if (sc->stereo) // LordHavoc: stereo sound support
110 for (i=0 ; i<outcount ; i++)
112 *out++ = LittleShort (in[srcsample ]);
113 *out++ = LittleShort (in[srcsample+1]);
114 srcsample += fracstep;
119 for (i=0 ; i<outcount ; i++)
121 *out++ = LittleShort (in[srcsample ]);
122 srcsample += fracstep;
128 signed char *out = (void *)sc->data;
129 unsigned char *in = (void *)data;
130 if (sc->stereo) // LordHavoc: stereo sound support
133 for (i=0 ; i<outcount ; i++)
135 *out++ = in[srcsample ] - 128;
136 *out++ = in[srcsample+1] - 128;
137 srcsample += fracstep;
142 for (i=0 ; i<outcount ; i++)
144 *out++ = in[srcsample ] - 128;
145 srcsample += fracstep;
156 short *out = (void *)sc->data, *in = (void *)data;
157 if (sc->stereo) // LordHavoc: stereo sound support
159 for (i=0 ; i<outcount ; i++)
161 srcsample = (samplefrac >> 8) << 1;
163 if (srcsample+2 >= srclength)
167 sample = (((b - a) * (samplefrac & 255)) >> 8) + a;
168 *out++ = (short) sample;
170 if (srcsample+2 >= srclength)
174 sample = (((b - a) * (samplefrac & 255)) >> 8) + a;
175 *out++ = (short) sample;
176 samplefrac += fracstep;
181 for (i=0 ; i<outcount ; i++)
183 srcsample = samplefrac >> 8;
185 if (srcsample+1 >= srclength)
189 sample = (((b - a) * (samplefrac & 255)) >> 8) + a;
190 *out++ = (short) sample;
191 samplefrac += fracstep;
197 signed char *out = (void *)sc->data;
198 unsigned char *in = (void *)data;
199 if (sc->stereo) // LordHavoc: stereo sound support
201 for (i=0 ; i<outcount ; i++)
203 srcsample = (samplefrac >> 8) << 1;
204 a = (int) in[srcsample ] - 128;
205 if (srcsample+2 >= srclength)
208 b = (int) in[srcsample+2] - 128;
209 sample = (((b - a) * (samplefrac & 255)) >> 8) + a;
210 *out++ = (signed char) sample;
211 a = (int) in[srcsample+1] - 128;
212 if (srcsample+2 >= srclength)
215 b = (int) in[srcsample+3] - 128;
216 sample = (((b - a) * (samplefrac & 255)) >> 8) + a;
217 *out++ = (signed char) sample;
218 samplefrac += fracstep;
223 for (i=0 ; i<outcount ; i++)
225 srcsample = samplefrac >> 8;
226 a = (int) in[srcsample ] - 128;
227 if (srcsample+1 >= srclength)
230 b = (int) in[srcsample+1] - 128;
231 sample = (((b - a) * (samplefrac & 255)) >> 8) + a;
232 *out++ = (signed char) sample;
233 samplefrac += fracstep;
240 // LordHavoc: use this for testing if it ever becomes useful again
242 COM_WriteFile (va("sound/%s.pcm", name), sc->data, (sc->length << sc->stereo) * sc->width);
246 //=============================================================================
253 sfxcache_t *S_LoadSound (sfx_t *s)
255 char namebuffer[256];
262 // see if still in memory
263 sc = Cache_Check (&s->cache);
267 //Con_Printf ("S_LoadSound: %x\n", (int)stackbuf);
269 strcpy(namebuffer, "sound/");
270 strcat(namebuffer, s->name);
272 // Con_Printf ("loading %s\n",namebuffer);
274 data = COM_LoadMallocFile(namebuffer, false);
278 Con_Printf ("Couldn't load %s\n", namebuffer);
282 info = GetWavinfo (s->name, data, com_filesize);
283 // LordHavoc: stereo sounds are now allowed (intended for music)
284 if (info.channels < 1 || info.channels > 2)
286 Con_Printf ("%s has an unsupported number of channels (%i)\n",s->name, info.channels);
291 if (info.channels != 1)
293 Con_Printf ("%s is a stereo sample\n",s->name);
298 stepscale = (float)info.rate / shm->speed;
299 len = info.samples / stepscale;
301 len = len * info.width * info.channels;
303 sc = Cache_Alloc ( &s->cache, len + sizeof(sfxcache_t), s->name);
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, byte *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;