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, int inwidth, byte *data)
39 int sample, 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;
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);
94 Con_DPrintf("ResampleSfx: resampling sound %s\n", sfx->name);
96 fracstep = stepscale*256;
97 if (sc->stereo) // LordHavoc: stereo sound support
99 for (i=0 ; i<outcount ; i+=2)
101 srcsample = samplefrac >> 8;
102 samplefrac += fracstep;
106 sample = LittleShort ( ((short *)data)[srcsample] );
108 sample = (int)( (unsigned char)(data[srcsample]) - 128) << 8;
110 ((short *)sc->data)[i] = sample;
112 ((signed char *)sc->data)[i] = sample >> 8;
116 sample = LittleShort ( ((short *)data)[srcsample] );
118 sample = (int)( (unsigned char)(data[srcsample]) - 128) << 8;
120 ((short *)sc->data)[i+1] = sample;
122 ((signed char *)sc->data)[i+1] = sample >> 8;
127 for (i=0 ; i<outcount ; i++)
129 srcsample = samplefrac >> 8;
130 samplefrac += fracstep;
132 sample = LittleShort ( ((short *)data)[srcsample] );
134 sample = (int)( (unsigned char)(data[srcsample]) - 128) << 8;
136 ((short *)sc->data)[i] = sample;
138 ((signed char *)sc->data)[i] = sample >> 8;
144 //=============================================================================
151 sfxcache_t *S_LoadSound (sfx_t *s)
153 char namebuffer[256];
159 byte stackbuf[1*1024]; // avoid dirtying the cache heap
161 // see if still in memory
162 sc = Cache_Check (&s->cache);
166 //Con_Printf ("S_LoadSound: %x\n", (int)stackbuf);
168 strcpy(namebuffer, "sound/");
169 strcat(namebuffer, s->name);
171 // Con_Printf ("loading %s\n",namebuffer);
173 data = COM_LoadStackFile(namebuffer, stackbuf, sizeof(stackbuf), false);
177 Con_Printf ("Couldn't load %s\n", namebuffer);
181 info = GetWavinfo (s->name, data, com_filesize);
182 // LordHavoc: stereo sounds are now allowed (intended for music)
183 if (info.channels < 1 || info.channels > 2)
185 Con_Printf ("%s has an unsupported number of channels (%i)\n",s->name, info.channels);
189 if (info.channels != 1)
191 Con_Printf ("%s is a stereo sample\n",s->name);
196 stepscale = (float)info.rate / shm->speed;
197 len = info.samples / stepscale;
199 len = len * info.width * info.channels;
201 sc = Cache_Alloc ( &s->cache, len + sizeof(sfxcache_t), s->name);
205 sc->length = info.samples;
206 sc->loopstart = info.loopstart;
207 sc->speed = info.rate;
208 sc->width = info.width;
209 sc->stereo = info.channels == 2;
211 ResampleSfx (s, sc->speed, sc->width, data + info.dataofs);
219 ===============================================================================
223 ===============================================================================
234 short GetLittleShort(void)
238 val = val + (*(data_p+1)<<8);
243 int GetLittleLong(void)
247 val = val + (*(data_p+1)<<8);
248 val = val + (*(data_p+2)<<16);
249 val = val + (*(data_p+3)<<24);
254 void FindNextChunk(char *name)
260 if (data_p >= iff_end)
261 { // didn't find the chunk
267 iff_chunk_len = GetLittleLong();
268 if (iff_chunk_len < 0)
273 // if (iff_chunk_len > 1024*1024)
274 // Sys_Error ("FindNextChunk: %i length is past the 1 meg sanity limit", iff_chunk_len);
276 last_chunk = data_p + 8 + ( (iff_chunk_len + 1) & ~1 );
277 if (!strncmp(data_p, name, 4))
282 void FindChunk(char *name)
284 last_chunk = iff_data;
285 FindNextChunk (name);
289 void DumpChunks(void)
297 memcpy (str, data_p, 4);
299 iff_chunk_len = GetLittleLong();
300 Con_Printf ("0x%x : %s (%d)\n", (int)(data_p - 4), str, iff_chunk_len);
301 data_p += (iff_chunk_len + 1) & ~1;
302 } while (data_p < iff_end);
310 wavinfo_t GetWavinfo (char *name, byte *wav, int wavlength)
317 memset (&info, 0, sizeof(info));
323 iff_end = wav + wavlength;
327 if (!(data_p && !strncmp(data_p+8, "WAVE", 4)))
329 Con_Printf("Missing RIFF/WAVE chunks\n");
334 iff_data = data_p + 12;
340 Con_Printf("Missing fmt chunk\n");
344 format = GetLittleShort();
347 Con_Printf("Microsoft PCM format only\n");
351 info.channels = GetLittleShort();
352 info.rate = GetLittleLong();
354 info.width = GetLittleShort() / 8;
361 info.loopstart = GetLittleLong();
362 // Con_Printf("loopstart=%d\n", sfx->loopstart);
364 // if the next chunk is a LIST chunk, look for a cue length marker
365 FindNextChunk ("LIST");
368 if (!strncmp (data_p + 28, "mark", 4))
369 { // this is not a proper parse, but it works with cooledit...
371 i = GetLittleLong (); // samples in loop
372 info.samples = info.loopstart + i;
373 // Con_Printf("looped length: %i\n", i);
384 Con_Printf("Missing data chunk\n");
389 samples = GetLittleLong () / info.width;
393 if (samples < info.samples)
394 Sys_Error ("Sound %s has a bad loop length", name);
397 info.samples = samples;
399 info.dataofs = data_p - wav;