no longer complains about missing sounds loaded by the engine (wind2, water1, etc...
[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 //      COM_WriteFile (va("sound/%s.pcm", name), sc->data, (sc->length << sc->stereo) * sc->width);
205 }
206
207 //=============================================================================
208
209 /*
210 ==============
211 S_LoadSound
212 ==============
213 */
214 sfxcache_t *S_LoadSound (sfx_t *s, int complain)
215 {
216     char        namebuffer[256];
217         qbyte   *data;
218         wavinfo_t       info;
219         int             len;
220         sfxcache_t      *sc;
221
222 // see if still in memory
223         if (s->sfxcache)
224                 return s->sfxcache;
225
226 // load it in
227         strcpy(namebuffer, "sound/");
228         strcat(namebuffer, s->name);
229
230         data = COM_LoadFile(namebuffer, false);
231
232         if (!data)
233         {
234                 if (complain)
235                         Con_Printf ("Couldn't load %s\n", namebuffer);
236                 return NULL;
237         }
238
239         info = GetWavinfo (s->name, data, com_filesize);
240         // LordHavoc: stereo sounds are now allowed (intended for music)
241         if (info.channels < 1 || info.channels > 2)
242         {
243                 Con_Printf ("%s has an unsupported number of channels (%i)\n",s->name, info.channels);
244                 Mem_Free(data);
245                 return NULL;
246         }
247
248         // calculate resampled length
249         len = (int) ((double) info.samples * (double) shm->speed / (double) info.rate);
250         len = len * info.width * info.channels;
251
252         // FIXME: add S_UnloadSounds or something?
253         Mem_FreePool(&s->mempool);
254         s->mempool = Mem_AllocPool(s->name);
255         sc = s->sfxcache = Mem_Alloc(s->mempool, len + sizeof(sfxcache_t));
256         if (!sc)
257         {
258                 Mem_FreePool(&s->mempool);
259                 Mem_Free(data);
260                 return NULL;
261         }
262
263         sc->length = info.samples;
264         sc->loopstart = info.loopstart;
265         sc->speed = info.rate;
266         sc->width = info.width;
267         sc->stereo = info.channels == 2;
268
269         ResampleSfx (sc, data + info.dataofs, s->name);
270
271         Mem_Free(data);
272         return sc;
273 }
274
275
276
277 /*
278 ===============================================================================
279
280 WAV loading
281
282 ===============================================================================
283 */
284
285
286 qbyte   *data_p;
287 qbyte   *iff_end;
288 qbyte   *last_chunk;
289 qbyte   *iff_data;
290 int     iff_chunk_len;
291
292
293 short GetLittleShort(void)
294 {
295         short val = 0;
296         val = *data_p;
297         val = val + (*(data_p+1)<<8);
298         data_p += 2;
299         return val;
300 }
301
302 int GetLittleLong(void)
303 {
304         int val = 0;
305         val = *data_p;
306         val = val + (*(data_p+1)<<8);
307         val = val + (*(data_p+2)<<16);
308         val = val + (*(data_p+3)<<24);
309         data_p += 4;
310         return val;
311 }
312
313 void FindNextChunk(char *name)
314 {
315         while (1)
316         {
317                 data_p=last_chunk;
318
319                 if (data_p >= iff_end)
320                 {       // didn't find the chunk
321                         data_p = NULL;
322                         return;
323                 }
324
325                 data_p += 4;
326                 iff_chunk_len = GetLittleLong();
327                 if (iff_chunk_len < 0)
328                 {
329                         data_p = NULL;
330                         return;
331                 }
332                 data_p -= 8;
333                 last_chunk = data_p + 8 + ( (iff_chunk_len + 1) & ~1 );
334                 if (!strncmp(data_p, name, 4))
335                         return;
336         }
337 }
338
339 void FindChunk(char *name)
340 {
341         last_chunk = iff_data;
342         FindNextChunk (name);
343 }
344
345
346 void DumpChunks(void)
347 {
348         char    str[5];
349         
350         str[4] = 0;
351         data_p=iff_data;
352         do
353         {
354                 memcpy (str, data_p, 4);
355                 data_p += 4;
356                 iff_chunk_len = GetLittleLong();
357                 Con_Printf ("0x%x : %s (%d)\n", (int)(data_p - 4), str, iff_chunk_len);
358                 data_p += (iff_chunk_len + 1) & ~1;
359         } while (data_p < iff_end);
360 }
361
362 /*
363 ============
364 GetWavinfo
365 ============
366 */
367 wavinfo_t GetWavinfo (char *name, qbyte *wav, int wavlength)
368 {
369         wavinfo_t       info;
370         int     i;
371         int     format;
372         int             samples;
373
374         memset (&info, 0, sizeof(info));
375
376         if (!wav)
377                 return info;
378
379         iff_data = wav;
380         iff_end = wav + wavlength;
381
382 // find "RIFF" chunk
383         FindChunk("RIFF");
384         if (!(data_p && !strncmp(data_p+8, "WAVE", 4)))
385         {
386                 Con_Printf("Missing RIFF/WAVE chunks\n");
387                 return info;
388         }
389
390 // get "fmt " chunk
391         iff_data = data_p + 12;
392 // DumpChunks ();
393
394         FindChunk("fmt ");
395         if (!data_p)
396         {
397                 Con_Printf("Missing fmt chunk\n");
398                 return info;
399         }
400         data_p += 8;
401         format = GetLittleShort();
402         if (format != 1)
403         {
404                 Con_Printf("Microsoft PCM format only\n");
405                 return info;
406         }
407
408         info.channels = GetLittleShort();
409         info.rate = GetLittleLong();
410         data_p += 4+2;
411         info.width = GetLittleShort() / 8;
412
413 // get cue chunk
414         FindChunk("cue ");
415         if (data_p)
416         {
417                 data_p += 32;
418                 info.loopstart = GetLittleLong();
419
420         // if the next chunk is a LIST chunk, look for a cue length marker
421                 FindNextChunk ("LIST");
422                 if (data_p)
423                 {
424                         if (!strncmp (data_p + 28, "mark", 4))
425                         {       // this is not a proper parse, but it works with cooledit...
426                                 data_p += 24;
427                                 i = GetLittleLong ();   // samples in loop
428                                 info.samples = info.loopstart + i;
429                         }
430                 }
431         }
432         else
433                 info.loopstart = -1;
434
435 // find data chunk
436         FindChunk("data");
437         if (!data_p)
438         {
439                 Con_Printf("Missing data chunk\n");
440                 return info;
441         }
442
443         data_p += 4;
444         samples = GetLittleLong () / info.width / info.channels;
445
446         if (info.samples)
447         {
448                 if (samples < info.samples)
449                         Host_Error ("Sound %s has a bad loop length", name);
450         }
451         else
452                 info.samples = samples;
453
454         info.dataofs = data_p - wav;
455
456         return info;
457 }
458