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