]> icculus.org git repositories - divverent/darkplaces.git/blob - snd_mem.c
independent control of gl_mesh_maxtriangles and gl_mesh_transtriangles (that is to...
[divverent/darkplaces.git] / snd_mem.c
1 /*
2 Copyright (C) 1996-1997 Id Software, Inc.
3
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.
8
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.
12
13 See the GNU General Public License for more details.
14
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.
18
19 */
20 // snd_mem.c: sound caching
21
22 #include "quakedef.h"
23
24 qbyte *S_Alloc (int size);
25
26 /*
27 ================
28 ResampleSfx
29 ================
30 */
31 void ResampleSfx (sfxcache_t *sc, qbyte *data, char *name)
32 {
33         int i, outcount, srcsample, srclength, samplefrac, fracstep;
34
35         // this is usually 0.5 (128), 1 (256), or 2 (512)
36         fracstep = ((double) sc->speed / (double) shm->speed) * 256.0;
37
38         srclength = sc->length << sc->stereo;
39
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;
44
45         sc->speed = shm->speed;
46
47 // resample / decimate to the current source rate
48
49         if (fracstep == 256)
50         {
51                 if (sc->width == 1) // 8bit
52                         for (i = 0;i < srclength;i++)
53                                 ((signed char *)sc->data)[i] = ((unsigned char *)data)[i] - 128;
54                 else //if (sc->width == 2) // 16bit
55                         for (i = 0;i < srclength;i++)
56                                 ((short *)sc->data)[i] = LittleShort (((short *)data)[i]);
57         }
58         else
59         {
60 // general case
61                 Con_DPrintf("ResampleSfx: resampling sound %s\n", name);
62                 samplefrac = 0;
63                 if ((fracstep & 255) == 0) // skipping points on perfect multiple
64                 {
65                         srcsample = 0;
66                         fracstep >>= 8;
67                         if (sc->width == 2)
68                         {
69                                 short *out = (void *)sc->data, *in = (void *)data;
70                                 if (sc->stereo) // LordHavoc: stereo sound support
71                                 {
72                                         fracstep <<= 1;
73                                         for (i=0 ; i<outcount ; i++)
74                                         {
75                                                 *out++ = LittleShort (in[srcsample  ]);
76                                                 *out++ = LittleShort (in[srcsample+1]);
77                                                 srcsample += fracstep;
78                                         }
79                                 }
80                                 else
81                                 {
82                                         for (i=0 ; i<outcount ; i++)
83                                         {
84                                                 *out++ = LittleShort (in[srcsample  ]);
85                                                 srcsample += fracstep;
86                                         }
87                                 }
88                         }
89                         else
90                         {
91                                 signed char *out = (void *)sc->data;
92                                 unsigned char *in = (void *)data;
93                                 if (sc->stereo) // LordHavoc: stereo sound support
94                                 {
95                                         fracstep <<= 1;
96                                         for (i=0 ; i<outcount ; i++)
97                                         {
98                                                 *out++ = in[srcsample  ] - 128;
99                                                 *out++ = in[srcsample+1] - 128;
100                                                 srcsample += fracstep;
101                                         }
102                                 }
103                                 else
104                                 {
105                                         for (i=0 ; i<outcount ; i++)
106                                         {
107                                                 *out++ = in[srcsample  ] - 128;
108                                                 srcsample += fracstep;
109                                         }
110                                 }
111                         }
112                 }
113                 else
114                 {
115                         int sample;
116                         int a, b;
117                         if (sc->width == 2)
118                         {
119                                 short *out = (void *)sc->data, *in = (void *)data;
120                                 if (sc->stereo) // LordHavoc: stereo sound support
121                                 {
122                                         for (i=0 ; i<outcount ; i++)
123                                         {
124                                                 srcsample = (samplefrac >> 8) << 1;
125                                                 a = in[srcsample  ];
126                                                 if (srcsample+2 >= srclength)
127                                                         b = 0;
128                                                 else
129                                                         b = in[srcsample+2];
130                                                 sample = (((b - a) * (samplefrac & 255)) >> 8) + a;
131                                                 *out++ = (short) sample;
132                                                 a = in[srcsample+1];
133                                                 if (srcsample+2 >= srclength)
134                                                         b = 0;
135                                                 else
136                                                         b = in[srcsample+3];
137                                                 sample = (((b - a) * (samplefrac & 255)) >> 8) + a;
138                                                 *out++ = (short) sample;
139                                                 samplefrac += fracstep;
140                                         }
141                                 }
142                                 else
143                                 {
144                                         for (i=0 ; i<outcount ; i++)
145                                         {
146                                                 srcsample = samplefrac >> 8;
147                                                 a = in[srcsample  ];
148                                                 if (srcsample+1 >= srclength)
149                                                         b = 0;
150                                                 else
151                                                         b = in[srcsample+1];
152                                                 sample = (((b - a) * (samplefrac & 255)) >> 8) + a;
153                                                 *out++ = (short) sample;
154                                                 samplefrac += fracstep;
155                                         }
156                                 }
157                         }
158                         else
159                         {
160                                 signed char *out = (void *)sc->data;
161                                 unsigned char *in = (void *)data;
162                                 if (sc->stereo) // LordHavoc: stereo sound support
163                                 {
164                                         for (i=0 ; i<outcount ; i++)
165                                         {
166                                                 srcsample = (samplefrac >> 8) << 1;
167                                                 a = (int) in[srcsample  ] - 128;
168                                                 if (srcsample+2 >= srclength)
169                                                         b = 0;
170                                                 else
171                                                         b = (int) in[srcsample+2] - 128;
172                                                 sample = (((b - a) * (samplefrac & 255)) >> 8) + a;
173                                                 *out++ = (signed char) sample;
174                                                 a = (int) in[srcsample+1] - 128;
175                                                 if (srcsample+2 >= srclength)
176                                                         b = 0;
177                                                 else
178                                                         b = (int) in[srcsample+3] - 128;
179                                                 sample = (((b - a) * (samplefrac & 255)) >> 8) + a;
180                                                 *out++ = (signed char) sample;
181                                                 samplefrac += fracstep;
182                                         }
183                                 }
184                                 else
185                                 {
186                                         for (i=0 ; i<outcount ; i++)
187                                         {
188                                                 srcsample = samplefrac >> 8;
189                                                 a = (int) in[srcsample  ] - 128;
190                                                 if (srcsample+1 >= srclength)
191                                                         b = 0;
192                                                 else
193                                                         b = (int) in[srcsample+1] - 128;
194                                                 sample = (((b - a) * (samplefrac & 255)) >> 8) + a;
195                                                 *out++ = (signed char) sample;
196                                                 samplefrac += fracstep;
197                                         }
198                                 }
199                         }
200                 }
201         }
202
203         // LordHavoc: use this for testing if it ever becomes useful again
204 #if 0
205         COM_WriteFile (va("sound/%s.pcm", name), sc->data, (sc->length << sc->stereo) * sc->width);
206 #endif
207 }
208
209 //=============================================================================
210
211 /*
212 ==============
213 S_LoadSound
214 ==============
215 */
216 sfxcache_t *S_LoadSound (sfx_t *s)
217 {
218     char        namebuffer[256];
219         qbyte   *data;
220         wavinfo_t       info;
221         int             len;
222         sfxcache_t      *sc;
223
224 // see if still in memory
225         if (s->sfxcache)
226                 return s->sfxcache;
227
228 //Con_Printf ("S_LoadSound: %x\n", (int)stackbuf);
229 // load it in
230         strcpy(namebuffer, "sound/");
231         strcat(namebuffer, s->name);
232
233 //      Con_Printf ("loading %s\n",namebuffer);
234
235         data = COM_LoadFile(namebuffer, false);
236
237         if (!data)
238         {
239                 Con_Printf ("Couldn't load %s\n", namebuffer);
240                 return NULL;
241         }
242
243         info = GetWavinfo (s->name, data, com_filesize);
244         // LordHavoc: stereo sounds are now allowed (intended for music)
245         if (info.channels < 1 || info.channels > 2)
246         {
247                 Con_Printf ("%s has an unsupported number of channels (%i)\n",s->name, info.channels);
248                 Mem_Free(data);
249                 return NULL;
250         }
251         /*
252         if (info.channels != 1)
253         {
254                 Con_Printf ("%s is a stereo sample\n",s->name);
255                 return NULL;
256         }
257         */
258
259         // calculate resampled length
260         len = (int) ((double) info.samples * (double) shm->speed / (double) info.rate);
261         len = len * info.width * info.channels;
262
263         // FIXME: add S_UnloadSounds or something?
264         Mem_FreePool(&s->mempool);
265         s->mempool = Mem_AllocPool(s->name);
266         sc = s->sfxcache = Mem_Alloc(s->mempool, len + sizeof(sfxcache_t));
267         if (!sc)
268         {
269                 Mem_FreePool(&s->mempool);
270                 Mem_Free(data);
271                 return NULL;
272         }
273
274         sc->length = info.samples;
275         sc->loopstart = info.loopstart;
276         sc->speed = info.rate;
277         sc->width = info.width;
278         sc->stereo = info.channels == 2;
279
280         ResampleSfx (sc, data + info.dataofs, s->name);
281
282         Mem_Free(data);
283         return sc;
284 }
285
286
287
288 /*
289 ===============================================================================
290
291 WAV loading
292
293 ===============================================================================
294 */
295
296
297 qbyte   *data_p;
298 qbyte   *iff_end;
299 qbyte   *last_chunk;
300 qbyte   *iff_data;
301 int     iff_chunk_len;
302
303
304 short GetLittleShort(void)
305 {
306         short val = 0;
307         val = *data_p;
308         val = val + (*(data_p+1)<<8);
309         data_p += 2;
310         return val;
311 }
312
313 int GetLittleLong(void)
314 {
315         int val = 0;
316         val = *data_p;
317         val = val + (*(data_p+1)<<8);
318         val = val + (*(data_p+2)<<16);
319         val = val + (*(data_p+3)<<24);
320         data_p += 4;
321         return val;
322 }
323
324 void FindNextChunk(char *name)
325 {
326         while (1)
327         {
328                 data_p=last_chunk;
329
330                 if (data_p >= iff_end)
331                 {       // didn't find the chunk
332                         data_p = NULL;
333                         return;
334                 }
335
336                 data_p += 4;
337                 iff_chunk_len = GetLittleLong();
338                 if (iff_chunk_len < 0)
339                 {
340                         data_p = NULL;
341                         return;
342                 }
343 //              if (iff_chunk_len > 1024*1024)
344 //                      Sys_Error ("FindNextChunk: %i length is past the 1 meg sanity limit", iff_chunk_len);
345                 data_p -= 8;
346                 last_chunk = data_p + 8 + ( (iff_chunk_len + 1) & ~1 );
347                 if (!strncmp(data_p, name, 4))
348                         return;
349         }
350 }
351
352 void FindChunk(char *name)
353 {
354         last_chunk = iff_data;
355         FindNextChunk (name);
356 }
357
358
359 void DumpChunks(void)
360 {
361         char    str[5];
362         
363         str[4] = 0;
364         data_p=iff_data;
365         do
366         {
367                 memcpy (str, data_p, 4);
368                 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);
373 }
374
375 /*
376 ============
377 GetWavinfo
378 ============
379 */
380 wavinfo_t GetWavinfo (char *name, qbyte *wav, int wavlength)
381 {
382         wavinfo_t       info;
383         int     i;
384         int     format;
385         int             samples;
386
387         memset (&info, 0, sizeof(info));
388
389         if (!wav)
390                 return info;
391
392         iff_data = wav;
393         iff_end = wav + wavlength;
394
395 // find "RIFF" chunk
396         FindChunk("RIFF");
397         if (!(data_p && !strncmp(data_p+8, "WAVE", 4)))
398         {
399                 Con_Printf("Missing RIFF/WAVE chunks\n");
400                 return info;
401         }
402
403 // get "fmt " chunk
404         iff_data = data_p + 12;
405 // DumpChunks ();
406
407         FindChunk("fmt ");
408         if (!data_p)
409         {
410                 Con_Printf("Missing fmt chunk\n");
411                 return info;
412         }
413         data_p += 8;
414         format = GetLittleShort();
415         if (format != 1)
416         {
417                 Con_Printf("Microsoft PCM format only\n");
418                 return info;
419         }
420
421         info.channels = GetLittleShort();
422         info.rate = GetLittleLong();
423         data_p += 4+2;
424         info.width = GetLittleShort() / 8;
425
426 // get cue chunk
427         FindChunk("cue ");
428         if (data_p)
429         {
430                 data_p += 32;
431                 info.loopstart = GetLittleLong();
432 //              Con_Printf("loopstart=%d\n", sfx->loopstart);
433
434         // if the next chunk is a LIST chunk, look for a cue length marker
435                 FindNextChunk ("LIST");
436                 if (data_p)
437                 {
438                         if (!strncmp (data_p + 28, "mark", 4))
439                         {       // this is not a proper parse, but it works with cooledit...
440                                 data_p += 24;
441                                 i = GetLittleLong ();   // samples in loop
442                                 info.samples = info.loopstart + i;
443 //                              Con_Printf("looped length: %i\n", i);
444                         }
445                 }
446         }
447         else
448                 info.loopstart = -1;
449
450 // find data chunk
451         FindChunk("data");
452         if (!data_p)
453         {
454                 Con_Printf("Missing data chunk\n");
455                 return info;
456         }
457
458         data_p += 4;
459         samples = GetLittleLong () / info.width / info.channels;
460
461         if (info.samples)
462         {
463                 if (samples < info.samples)
464                         Host_Error ("Sound %s has a bad loop length", name);
465         }
466         else
467                 info.samples = samples;
468
469         info.dataofs = data_p - wav;
470
471         return info;
472 }
473