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