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