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)
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 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)
65 // LordHavoc: I do not serve the readability gods...
66 int *indata, *outdata;
68 count1 = outcount << sc->stereo;
70 indata = (void *)data;
71 outdata = (void *)sc->data;
73 *outdata++ = *indata++ ^ 0x80808080;
75 ((short*)outdata)[0] = ((short*)indata)[0] ^ 0x8080;
77 ((char*)outdata)[2] = ((char*)indata)[2] ^ 0x80;
79 if (sc->stereo) // LordHavoc: stereo sound support
81 for (i=0 ; i<(outcount<<1) ; i++)
82 ((signed char *)sc->data)[i] = (int)( (unsigned char)(data[i]) - 128);
86 for (i=0 ; i<outcount ; i++)
87 ((signed char *)sc->data)[i] = (int)( (unsigned char)(data[i]) - 128);
91 else if (stepscale == 1/* && inwidth == 2*/ && sc->width == 2) // LordHavoc: quick case for 16bit
93 if (sc->stereo) // LordHavoc: stereo sound support
94 for (i=0 ; i<outcount*2 ;i++)
95 ((short *)sc->data)[i] = LittleShort (((short *)data)[i]);
97 for (i=0 ; i<outcount ;i++)
98 ((short *)sc->data)[i] = LittleShort (((short *)data)[i]);
103 Con_DPrintf("ResampleSfx: resampling sound %s\n", sfx->name);
105 fracstep = stepscale*256;
106 if ((fracstep & 255) == 0) // skipping points on perfect multiple
112 short *out = (void *)sc->data, *in = (void *)data;
113 if (sc->stereo) // LordHavoc: stereo sound support
116 for (i=0 ; i<outcount ;)
118 *out++ = (short) LittleShort (in[srcsample ]);
119 *out++ = (short) LittleShort (in[srcsample+1]);
120 srcsample += fracstep;
125 for (i=0 ; i<outcount ; i++)
127 *out++ = (short) LittleShort (in[srcsample ]);
128 srcsample += fracstep;
134 signed char *out = (void *)sc->data, *in = (void *)data;
135 if (sc->stereo) // LordHavoc: stereo sound support
138 for (i=0 ; i<outcount ;)
140 *out++ = (signed char) in[srcsample ] - 128;
141 *out++ = (signed char) in[srcsample+1] - 128;
142 srcsample += fracstep;
147 for (i=0 ; i<outcount ; i++)
149 *out++ = (signed char) in[srcsample ] - 128;
150 srcsample += fracstep;
160 short *out = (void *)sc->data, *in = (void *)data;
161 if (sc->stereo) // LordHavoc: stereo sound support
163 for (i=0 ; i<outcount ;)
165 srcsample = (samplefrac >> 7) & ~1;
166 samplefrac += fracstep;
167 sample = (LittleShort (in[srcsample ]) * (256 - (samplefrac & 255)) + LittleShort (in[srcsample+2]) * (samplefrac & 255)) >> 8;
168 *out++ = (short) sample;
169 sample = (LittleShort (in[srcsample+1]) * (256 - (samplefrac & 255)) + LittleShort (in[srcsample+3]) * (samplefrac & 255)) >> 8;
170 *out++ = (short) sample;
175 for (i=0 ; i<outcount ; i++)
177 srcsample = samplefrac >> 8;
178 samplefrac += fracstep;
179 sample = (LittleShort (in[srcsample ]) * (256 - (samplefrac & 255)) + LittleShort (in[srcsample+1]) * (samplefrac & 255)) >> 8;
180 *out++ = (short) sample;
186 signed char *out = (void *)sc->data, *in = (void *)data;
187 if (sc->stereo) // LordHavoc: stereo sound support
189 for (i=0 ; i<outcount ;)
191 srcsample = (samplefrac >> 7) & ~1;
192 samplefrac += fracstep;
193 sample = ((((unsigned char) in[srcsample ] - 128) * (256 - (samplefrac & 255))) + (((unsigned char) in[srcsample+2] - 128) * (samplefrac & 255))) >> 8;
194 *out++ = (signed char) sample;
195 sample = ((((unsigned char) in[srcsample+1] - 128) * (256 - (samplefrac & 255))) + (((unsigned char) in[srcsample+3] - 128) * (samplefrac & 255))) >> 8;
196 *out++ = (signed char) sample;
201 for (i=0 ; i<outcount ; i++)
203 srcsample = samplefrac >> 8;
204 samplefrac += fracstep;
205 sample = ((((unsigned char) in[srcsample ] - 128) * (256 - (samplefrac & 255))) + (((unsigned char) in[srcsample+1] - 128) * (samplefrac & 255))) >> 8;
206 *out++ = (signed char) sample;
214 //=============================================================================
221 sfxcache_t *S_LoadSound (sfx_t *s)
223 char namebuffer[256];
229 byte stackbuf[1*1024]; // avoid dirtying the cache heap
231 // see if still in memory
232 sc = Cache_Check (&s->cache);
236 //Con_Printf ("S_LoadSound: %x\n", (int)stackbuf);
238 strcpy(namebuffer, "sound/");
239 strcat(namebuffer, s->name);
241 // Con_Printf ("loading %s\n",namebuffer);
243 data = COM_LoadStackFile(namebuffer, stackbuf, sizeof(stackbuf), false);
247 Con_Printf ("Couldn't load %s\n", namebuffer);
251 info = GetWavinfo (s->name, data, com_filesize);
252 // LordHavoc: stereo sounds are now allowed (intended for music)
253 if (info.channels < 1 || info.channels > 2)
255 Con_Printf ("%s has an unsupported number of channels (%i)\n",s->name, info.channels);
259 if (info.channels != 1)
261 Con_Printf ("%s is a stereo sample\n",s->name);
266 stepscale = (float)info.rate / shm->speed;
267 len = info.samples / stepscale;
269 len = len * info.width * info.channels;
271 sc = Cache_Alloc ( &s->cache, len + sizeof(sfxcache_t), s->name);
275 sc->length = info.samples;
276 sc->loopstart = info.loopstart;
277 sc->speed = info.rate;
278 sc->width = info.width;
279 sc->stereo = info.channels == 2;
281 ResampleSfx (s, sc->speed, data + info.dataofs);
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, byte *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;
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;