]> git.xonotic.org Git - xonotic/darkplaces.git/blob - snd_ogg.c
Remove unneeded #includes in some headers. Make some headers standalone.
[xonotic/darkplaces.git] / snd_ogg.c
1 /*
2         Copyright (C) 2003-2005  Mathieu Olivier
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:
17
18                 Free Software Foundation, Inc.
19                 59 Temple Place - Suite 330
20                 Boston, MA  02111-1307, USA
21
22 */
23
24
25 #include "darkplaces.h"
26 #include "snd_main.h"
27 #include "snd_ogg.h"
28 #include "snd_wav.h"
29 #include "sound.h"
30
31 #ifdef LINK_TO_LIBVORBIS
32 #define OV_EXCLUDE_STATIC_CALLBACKS
33 #include <ogg/ogg.h>
34 #include <vorbis/vorbisfile.h>
35
36 #define qov_clear ov_clear
37 #define qov_info ov_info
38 #define qov_comment ov_comment
39 #define qov_open_callbacks ov_open_callbacks
40 #define qov_pcm_seek ov_pcm_seek
41 #define qov_pcm_total ov_pcm_total
42 #define qov_read ov_read
43 #define qvorbis_comment_query vorbis_comment_query
44
45 qbool OGG_OpenLibrary (void) {return true;}
46 void OGG_CloseLibrary (void) {}
47 #else
48
49 /*
50 =================================================================
51
52   Minimal set of definitions from the Ogg Vorbis lib
53   (C) COPYRIGHT 1994-2001 by the XIPHOPHORUS Company
54   http://www.xiph.org/
55
56   WARNING: for a matter of simplicity, several pointer types are
57   casted to "void*", and most enumerated values are not included
58
59 =================================================================
60 */
61
62 #ifdef _MSC_VER
63 typedef __int64 ogg_int64_t;
64 #else
65 typedef long long ogg_int64_t;
66 #endif
67
68 typedef struct
69 {
70         size_t  (*read_func)    (void *ptr, size_t size, size_t nmemb, void *datasource);
71         int             (*seek_func)    (void *datasource, ogg_int64_t offset, int whence);
72         int             (*close_func)   (void *datasource);
73         long    (*tell_func)    (void *datasource);
74 } ov_callbacks;
75
76 typedef struct
77 {
78         unsigned char   *data;
79         int                             storage;
80         int                             fill;
81         int                             returned;
82         int                             unsynced;
83         int                             headerbytes;
84         int                             bodybytes;
85 } ogg_sync_state;
86
87 typedef struct
88 {
89         int             version;
90         int             channels;
91         long    rate;
92         long    bitrate_upper;
93         long    bitrate_nominal;
94         long    bitrate_lower;
95         long    bitrate_window;
96         void    *codec_setup;
97 } vorbis_info;
98
99 typedef struct
100 {
101         unsigned char   *body_data;
102         long                    body_storage;
103         long                    body_fill;
104         long                    body_returned;
105         int                             *lacing_vals;
106         ogg_int64_t             *granule_vals;
107         long                    lacing_storage;
108         long                    lacing_fill;
109         long                    lacing_packet;
110         long                    lacing_returned;
111         unsigned char   header[282];
112         int                             header_fill;
113         int                             e_o_s;
114         int                             b_o_s;
115         long                    serialno;
116         long                    pageno;
117         ogg_int64_t             packetno;
118         ogg_int64_t             granulepos;
119 } ogg_stream_state;
120
121 typedef struct
122 {
123         int                     analysisp;
124         vorbis_info     *vi;
125         float           **pcm;
126         float           **pcmret;
127         int                     pcm_storage;
128         int                     pcm_current;
129         int                     pcm_returned;
130         int                     preextrapolate;
131         int                     eofflag;
132         long            lW;
133         long            W;
134         long            nW;
135         long            centerW;
136         ogg_int64_t     granulepos;
137         ogg_int64_t     sequence;
138         ogg_int64_t     glue_bits;
139         ogg_int64_t     time_bits;
140         ogg_int64_t     floor_bits;
141         ogg_int64_t     res_bits;
142         void            *backend_state;
143 } vorbis_dsp_state;
144
145 typedef struct
146 {
147         long                    endbyte;
148         int                             endbit;
149         unsigned char   *buffer;
150         unsigned char   *ptr;
151         long                    storage;
152 } oggpack_buffer;
153
154 typedef struct
155 {
156         float                           **pcm;
157         oggpack_buffer          opb;
158         long                            lW;
159         long                            W;
160         long                            nW;
161         int                                     pcmend;
162         int                                     mode;
163         int                                     eofflag;
164         ogg_int64_t                     granulepos;
165         ogg_int64_t                     sequence;
166         vorbis_dsp_state        *vd;
167         void                            *localstore;
168         long                            localtop;
169         long                            localalloc;
170         long                            totaluse;
171         void                            *reap;  // VOIDED POINTER
172         long                            glue_bits;
173         long                            time_bits;
174         long                            floor_bits;
175         long                            res_bits;
176         void                            *internal;
177 } vorbis_block;
178
179 typedef struct
180 {
181         char **user_comments;
182         int   *comment_lengths;
183         int    comments;
184         char  *vendor;
185 } vorbis_comment;
186
187 typedef struct
188 {
189         void                            *datasource;
190         int                                     seekable;
191         ogg_int64_t                     offset;
192         ogg_int64_t                     end;
193         ogg_sync_state          oy;
194         int                                     links;
195         ogg_int64_t                     *offsets;
196         ogg_int64_t                     *dataoffsets;
197         long                            *serialnos;
198         ogg_int64_t                     *pcmlengths;
199         vorbis_info                     *vi;
200         vorbis_comment          *vc;
201         ogg_int64_t                     pcm_offset;
202         int                                     ready_state;
203         long                            current_serialno;
204         int                                     current_link;
205         double                          bittrack;
206         double                          samptrack;
207         ogg_stream_state        os;
208         vorbis_dsp_state        vd;
209         vorbis_block            vb;
210         ov_callbacks            callbacks;
211 } OggVorbis_File;
212
213
214 /*
215 =================================================================
216
217   DarkPlaces definitions
218
219 =================================================================
220 */
221
222 // Functions exported from the vorbisfile library
223 static int (*qov_clear) (OggVorbis_File *vf);
224 static vorbis_info* (*qov_info) (OggVorbis_File *vf,int link);
225 static vorbis_comment* (*qov_comment) (OggVorbis_File *vf,int link);
226 static char * (*qvorbis_comment_query) (vorbis_comment *vc, const char *tag, int count);
227 static int (*qov_open_callbacks) (void *datasource, OggVorbis_File *vf,
228                                                                   char *initial, long ibytes,
229                                                                   ov_callbacks callbacks);
230 static int (*qov_pcm_seek) (OggVorbis_File *vf,ogg_int64_t pos);
231 static ogg_int64_t (*qov_pcm_total) (OggVorbis_File *vf,int i);
232 static long (*qov_read) (OggVorbis_File *vf,char *buffer,int length,
233                                                  int bigendianp,int word,int sgned,int *bitstream);
234
235 static dllfunction_t vorbisfilefuncs[] =
236 {
237         {"ov_clear",                            (void **) &qov_clear},
238         {"ov_info",                                     (void **) &qov_info},
239         {"ov_comment",                          (void **) &qov_comment},
240         {"ov_open_callbacks",           (void **) &qov_open_callbacks},
241         {"ov_pcm_seek",                         (void **) &qov_pcm_seek},
242         {"ov_pcm_total",                        (void **) &qov_pcm_total},
243         {"ov_read",                                     (void **) &qov_read},
244         {NULL, NULL}
245 };
246
247 static dllfunction_t vorbisfuncs[] =
248 {
249         {"vorbis_comment_query",        (void **) &qvorbis_comment_query},
250         {NULL, NULL}
251 };
252
253 // Handles for the Vorbis and Vorbisfile DLLs
254 static dllhandle_t vo_dll = NULL;
255 static dllhandle_t vf_dll = NULL;
256
257
258 /*
259 =================================================================
260
261   DLL load & unload
262
263 =================================================================
264 */
265
266 /*
267 ====================
268 OGG_OpenLibrary
269
270 Try to load the VorbisFile DLL
271 ====================
272 */
273 qbool OGG_OpenLibrary (void)
274 {
275         const char* dllnames_vo [] =
276         {
277 #if defined(WIN32)
278                 "libvorbis-0.dll",
279                 "libvorbis.dll",
280                 "vorbis.dll",
281 #elif defined(MACOSX)
282                 "libvorbis.dylib",
283 #else
284                 "libvorbis.so.0",
285                 "libvorbis.so",
286 #endif
287                 NULL
288         };
289         const char* dllnames_vf [] =
290         {
291 #if defined(WIN32)
292                 "libvorbisfile-3.dll",
293                 "libvorbisfile.dll",
294                 "vorbisfile.dll",
295 #elif defined(MACOSX)
296                 "libvorbisfile.dylib",
297 #else
298                 "libvorbisfile.so.3",
299                 "libvorbisfile.so",
300 #endif
301                 NULL
302         };
303
304         // Already loaded?
305         if (vf_dll)
306                 return true;
307
308 // COMMANDLINEOPTION: Sound: -novorbis disables ogg vorbis sound support
309         if (Sys_CheckParm("-novorbis"))
310                 return false;
311
312         // Load the DLLs
313         // We need to load both by hand because some OSes seem to not load
314         // the vorbis DLL automatically when loading the VorbisFile DLL
315         return Sys_LoadLibrary (dllnames_vo, &vo_dll, vorbisfuncs) && Sys_LoadLibrary (dllnames_vf, &vf_dll, vorbisfilefuncs);
316 }
317
318
319 /*
320 ====================
321 OGG_CloseLibrary
322
323 Unload the VorbisFile DLL
324 ====================
325 */
326 void OGG_CloseLibrary (void)
327 {
328         Sys_UnloadLibrary (&vf_dll);
329         Sys_UnloadLibrary (&vo_dll);
330 }
331
332 #endif
333
334 /*
335 =================================================================
336
337         Ogg Vorbis decoding
338
339 =================================================================
340 */
341
342 typedef struct
343 {
344         unsigned char *buffer;
345         ogg_int64_t ind, buffsize;
346 } ov_decode_t;
347
348 static size_t ovcb_read (void *ptr, size_t size, size_t nb, void *datasource)
349 {
350         ov_decode_t *ov_decode = (ov_decode_t*)datasource;
351         size_t remain, len;
352
353         remain = ov_decode->buffsize - ov_decode->ind;
354         len = size * nb;
355         if (remain < len)
356                 len = remain - remain % size;
357
358         memcpy (ptr, ov_decode->buffer + ov_decode->ind, len);
359         ov_decode->ind += len;
360
361         return len / size;
362 }
363
364 static int ovcb_seek (void *datasource, ogg_int64_t offset, int whence)
365 {
366         ov_decode_t *ov_decode = (ov_decode_t*)datasource;
367
368         switch (whence)
369         {
370                 case SEEK_SET:
371                         break;
372                 case SEEK_CUR:
373                         offset += ov_decode->ind;
374                         break;
375                 case SEEK_END:
376                         offset += ov_decode->buffsize;
377                         break;
378                 default:
379                         return -1;
380         }
381         if (offset < 0 || offset > ov_decode->buffsize)
382                 return -1;
383
384         ov_decode->ind = offset;
385         return 0;
386 }
387
388 static int ovcb_close (void *ov_decode)
389 {
390         return 0;
391 }
392
393 static long ovcb_tell (void *ov_decode)
394 {
395         return ((ov_decode_t*)ov_decode)->ind;
396 }
397
398 // Per-sfx data structure
399 typedef struct
400 {
401         unsigned char   *file;
402         size_t                  filesize;
403 } ogg_stream_persfx_t;
404
405 // Per-channel data structure
406 typedef struct
407 {
408         OggVorbis_File  vf;
409         ov_decode_t             ov_decode;
410         int                             bs;
411         int                             buffer_firstframe;
412         int                             buffer_numframes;
413         unsigned char   buffer[STREAM_BUFFERSIZE*4];
414 } ogg_stream_perchannel_t;
415
416
417 static const ov_callbacks callbacks = {ovcb_read, ovcb_seek, ovcb_close, ovcb_tell};
418
419 /*
420 ====================
421 OGG_GetSamplesFloat
422 ====================
423 */
424 static void OGG_GetSamplesFloat (channel_t *ch, sfx_t *sfx, int firstsampleframe, int numsampleframes, float *outsamplesfloat)
425 {
426         ogg_stream_perchannel_t *per_ch = (ogg_stream_perchannel_t *)ch->fetcher_data;
427         ogg_stream_persfx_t *per_sfx = (ogg_stream_persfx_t *)sfx->fetcher_data;
428         int f = sfx->format.width * sfx->format.channels; // bytes per frame in the buffer
429         short *buf;
430         int i, len;
431         int newlength, done, ret;
432
433         // if this channel does not yet have a channel fetcher, make one
434         if (per_ch == NULL)
435         {
436                 // allocate a struct to keep track of our file position and buffer
437                 per_ch = (ogg_stream_perchannel_t *)Mem_Alloc(snd_mempool, sizeof(*per_ch));
438                 // begin decoding the file
439                 per_ch->ov_decode.buffer = per_sfx->file;
440                 per_ch->ov_decode.ind = 0;
441                 per_ch->ov_decode.buffsize = per_sfx->filesize;
442                 if (qov_open_callbacks(&per_ch->ov_decode, &per_ch->vf, NULL, 0, callbacks) < 0)
443                 {
444                         // this never happens - this function succeeded earlier on the same data
445                         Mem_Free(per_ch);
446                         return;
447                 }
448                 per_ch->bs = 0;
449                 per_ch->buffer_firstframe = 0;
450                 per_ch->buffer_numframes = 0;
451                 // attach the struct to our channel
452                 ch->fetcher_data = (void *)per_ch;
453         }
454
455         // if the request is too large for our buffer, loop...
456         while (numsampleframes * f > (int)sizeof(per_ch->buffer))
457         {
458                 done = sizeof(per_ch->buffer) / f;
459                 OGG_GetSamplesFloat(ch, sfx, firstsampleframe, done, outsamplesfloat);
460                 firstsampleframe += done;
461                 numsampleframes -= done;
462                 outsamplesfloat += done * sfx->format.channels;
463         }
464
465         // seek if the request is before the current buffer (loop back)
466         // seek if the request starts beyond the current buffer by at least one frame (channel was zero volume for a while)
467         // do not seek if the request overlaps the buffer end at all (expected behavior)
468         if (per_ch->buffer_firstframe > firstsampleframe || per_ch->buffer_firstframe + per_ch->buffer_numframes < firstsampleframe)
469         {
470                 // we expect to decode forward from here so this will be our new buffer start
471                 per_ch->buffer_firstframe = firstsampleframe;
472                 per_ch->buffer_numframes = 0;
473                 ret = qov_pcm_seek(&per_ch->vf, (ogg_int64_t)firstsampleframe);
474                 if (ret != 0)
475                 {
476                         // LadyHavoc: we can't Con_Printf here, not thread safe...
477                         //Con_Printf("OGG_FetchSound: qov_pcm_seek(..., %d) returned %d\n", firstsampleframe, ret);
478                         return;
479                 }
480         }
481
482         // decompress the file as needed
483         if (firstsampleframe + numsampleframes > per_ch->buffer_firstframe + per_ch->buffer_numframes)
484         {
485                 // first slide the buffer back, discarding any data preceding the range we care about
486                 int offset = firstsampleframe - per_ch->buffer_firstframe;
487                 int keeplength = per_ch->buffer_numframes - offset;
488                 if (keeplength > 0)
489                         memmove(per_ch->buffer, per_ch->buffer + offset * sfx->format.width * sfx->format.channels, keeplength * sfx->format.width * sfx->format.channels);
490                 per_ch->buffer_firstframe = firstsampleframe;
491                 per_ch->buffer_numframes -= offset;
492                 // decompress as much as we can fit in the buffer
493                 newlength = sizeof(per_ch->buffer) - per_ch->buffer_numframes * f;
494                 done = 0;
495                 while (newlength > done && (ret = qov_read(&per_ch->vf, (char *)per_ch->buffer + per_ch->buffer_numframes * f + done, (int)(newlength - done), mem_bigendian, 2, 1, &per_ch->bs)) > 0)
496                         done += ret;
497                 // clear the missing space if any
498                 if (done < newlength)
499                         memset(per_ch->buffer + done, 0, newlength - done);
500                 // we now have more data in the buffer
501                 per_ch->buffer_numframes += done / f;
502         }
503
504         // convert the sample format for the caller
505         buf = (short *)((char *)per_ch->buffer + (firstsampleframe - per_ch->buffer_firstframe) * f);
506         len = numsampleframes * sfx->format.channels;
507         for (i = 0;i < len;i++)
508                 outsamplesfloat[i] = buf[i] * (1.0f / 32768.0f);
509 }
510
511
512 /*
513 ====================
514 OGG_StopChannel
515 ====================
516 */
517 static void OGG_StopChannel(channel_t *ch)
518 {
519         ogg_stream_perchannel_t *per_ch = (ogg_stream_perchannel_t *)ch->fetcher_data;
520         if (per_ch != NULL)
521         {
522                 // release the vorbis decompressor
523                 qov_clear(&per_ch->vf);
524                 Mem_Free(per_ch);
525         }
526 }
527
528
529 /*
530 ====================
531 OGG_FreeSfx
532 ====================
533 */
534 static void OGG_FreeSfx(sfx_t *sfx)
535 {
536         ogg_stream_persfx_t *per_sfx = (ogg_stream_persfx_t *)sfx->fetcher_data;
537         // free the complete file we were keeping around
538         Mem_Free(per_sfx->file);
539         // free the file information structure
540         Mem_Free(per_sfx);
541 }
542
543
544 static const snd_fetcher_t ogg_fetcher = {OGG_GetSamplesFloat, OGG_StopChannel, OGG_FreeSfx};
545
546 static void OGG_DecodeTags(vorbis_comment *vc, unsigned int *start, unsigned int *length, unsigned int numsamples, double *peak, double *gaindb)
547 {
548         const char *startcomment = NULL, *lengthcomment = NULL, *endcomment = NULL, *thiscomment = NULL;
549
550         *start = numsamples;
551         *length = numsamples;
552         *peak = 0.0;
553         *gaindb = 0.0;
554
555         if(!vc)
556                 return;
557
558         thiscomment = qvorbis_comment_query(vc, "REPLAYGAIN_TRACK_PEAK", 0);
559         if(thiscomment)
560                 *peak = atof(thiscomment);
561         thiscomment = qvorbis_comment_query(vc, "REPLAYGAIN_TRACK_GAIN", 0);
562         if(thiscomment)
563                 *gaindb = atof(thiscomment);
564         
565         startcomment = qvorbis_comment_query(vc, "LOOP_START", 0); // DarkPlaces, and some Japanese app
566         if(startcomment)
567         {
568                 endcomment = qvorbis_comment_query(vc, "LOOP_END", 0);
569                 if(!endcomment)
570                         lengthcomment = qvorbis_comment_query(vc, "LOOP_LENGTH", 0);
571         }
572         else
573         {
574                 startcomment = qvorbis_comment_query(vc, "LOOPSTART", 0); // RPG Maker VX
575                 if(startcomment)
576                 {
577                         lengthcomment = qvorbis_comment_query(vc, "LOOPLENGTH", 0);
578                         if(!lengthcomment)
579                                 endcomment = qvorbis_comment_query(vc, "LOOPEND", 0);
580                 }
581                 else
582                 {
583                         startcomment = qvorbis_comment_query(vc, "LOOPPOINT", 0); // Sonic Robo Blast 2
584                 }
585         }
586
587         if(startcomment)
588         {
589                 *start = (unsigned int) bound(0, atof(startcomment), numsamples);
590                 if(endcomment)
591                         *length = (unsigned int) bound(0, atof(endcomment), numsamples);
592                 else if(lengthcomment)
593                         *length = (unsigned int) bound(0, *start + atof(lengthcomment), numsamples);
594         }
595 }
596
597 /*
598 ====================
599 OGG_LoadVorbisFile
600
601 Load an Ogg Vorbis file into memory
602 ====================
603 */
604 qbool OGG_LoadVorbisFile(const char *filename, sfx_t *sfx)
605 {
606         unsigned char *data;
607         fs_offset_t filesize;
608         ov_decode_t ov_decode;
609         OggVorbis_File vf;
610         vorbis_info *vi;
611         vorbis_comment *vc;
612         double peak, gaindb;
613
614 #ifndef LINK_TO_LIBVORBIS
615         if (!vf_dll)
616                 return false;
617 #endif
618
619         // Return if already loaded
620         if (sfx->fetcher != NULL)
621                 return true;
622
623         // Load the file completely
624         data = FS_LoadFile(filename, snd_mempool, false, &filesize);
625         if (data == NULL)
626                 return false;
627
628         if (developer_loading.integer >= 2)
629                 Con_Printf("Loading Ogg Vorbis file \"%s\"\n", filename);
630
631         // Open it with the VorbisFile API
632         ov_decode.buffer = data;
633         ov_decode.ind = 0;
634         ov_decode.buffsize = filesize;
635         if (qov_open_callbacks(&ov_decode, &vf, NULL, 0, callbacks) < 0)
636         {
637                 Con_Printf("error while opening Ogg Vorbis file \"%s\"\n", filename);
638                 Mem_Free(data);
639                 return false;
640         }
641
642         // Get the stream information
643         vi = qov_info(&vf, -1);
644         if (vi->channels < 1 || vi->channels > 2)
645         {
646                 Con_Printf("%s has an unsupported number of channels (%i)\n",
647                                         sfx->name, vi->channels);
648                 qov_clear (&vf);
649                 Mem_Free(data);
650                 return false;
651         }
652
653         sfx->format.speed = vi->rate;
654         sfx->format.channels = vi->channels;
655         sfx->format.width = 2;  // We always work with 16 bits samples
656
657         sfx->total_length = qov_pcm_total(&vf, -1);
658
659         if (snd_streaming.integer && (snd_streaming.integer >= 2 || sfx->total_length > max(sizeof(ogg_stream_perchannel_t), snd_streaming_length.value * sfx->format.speed)))
660         {
661                 // large sounds use the OGG fetcher to decode the file on demand (but the entire file is held in memory)
662                 ogg_stream_persfx_t* per_sfx;
663                 if (developer_loading.integer >= 2)
664                         Con_Printf("Ogg sound file \"%s\" will be streamed\n", filename);
665                 per_sfx = (ogg_stream_persfx_t *)Mem_Alloc(snd_mempool, sizeof(*per_sfx));
666                 sfx->memsize += sizeof (*per_sfx);
667                 per_sfx->file = data;
668                 per_sfx->filesize = filesize;
669                 sfx->memsize += filesize;
670                 sfx->fetcher_data = per_sfx;
671                 sfx->fetcher = &ogg_fetcher;
672                 sfx->flags |= SFXFLAG_STREAMED;
673                 vc = qov_comment(&vf, -1);
674                 OGG_DecodeTags(vc, &sfx->loopstart, &sfx->total_length, sfx->total_length, &peak, &gaindb);
675                 qov_clear(&vf);
676         }
677         else
678         {
679                 // small sounds are entirely loaded and use the PCM fetcher
680                 char *buff;
681                 ogg_int64_t len;
682                 ogg_int64_t done;
683                 int bs;
684                 long ret;
685                 if (developer_loading.integer >= 2)
686                         Con_Printf ("Ogg sound file \"%s\" will be cached\n", filename);
687                 len = sfx->total_length * sfx->format.channels * sfx->format.width;
688                 sfx->flags &= ~SFXFLAG_STREAMED;
689                 sfx->memsize += len;
690                 sfx->fetcher = &wav_fetcher;
691                 sfx->fetcher_data = Mem_Alloc(snd_mempool, (size_t)len);
692                 buff = (char *)sfx->fetcher_data;
693                 done = 0;
694                 bs = 0;
695                 while ((ret = qov_read(&vf, &buff[done], (int)(len - done), mem_bigendian, 2, 1, &bs)) > 0)
696                         done += ret;
697                 vc = qov_comment(&vf, -1);
698                 OGG_DecodeTags(vc, &sfx->loopstart, &sfx->total_length, sfx->total_length, &peak, &gaindb);
699                 qov_clear(&vf);
700                 Mem_Free(data);
701         }
702
703         if(peak)
704         {
705                 sfx->volume_mult = min(1.0f / peak, exp(gaindb * 0.05f * log(10.0f)));
706                 sfx->volume_peak = peak;
707                 if (developer_loading.integer >= 2)
708                         Con_Printf ("Ogg sound file \"%s\" uses ReplayGain (gain %f, peak %f)\n", filename, sfx->volume_mult, sfx->volume_peak);
709         }
710         else if(gaindb != 0)
711         {
712                 sfx->volume_mult = min(1.0f / peak, exp(gaindb * 0.05f * log(10.0f)));
713                 sfx->volume_peak = 1.0; // if peak is not defined, we won't trust it
714                 if (developer_loading.integer >= 2)
715                         Con_Printf ("Ogg sound file \"%s\" uses ReplayGain (gain %f, peak not defined and assumed to be %f)\n", filename, sfx->volume_mult, sfx->volume_peak);
716         }
717
718         return true;
719 }