add shorthand option --remove-replay-gain to metaflac
[platform/upstream/flac.git] / src / share / grabbag / replaygain.c
1 /* grabbag - Convenience lib for various routines common to several tools
2  * Copyright (C) 2002,2003,2004,2005  Josh Coalson
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.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
17  */
18
19 #include "share/grabbag.h"
20 #include "share/replaygain_analysis.h"
21 #include "FLAC/assert.h"
22 #include "FLAC/file_decoder.h"
23 #include "FLAC/metadata.h"
24 #include <locale.h>
25 #include <math.h>
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #if defined _MSC_VER || defined __MINGW32__
30 #include <io.h> /* for chmod() */
31 #endif
32 #include <sys/stat.h> /* for stat(), maybe chmod() */
33
34 #ifdef local_min
35 #undef local_min
36 #endif
37 #define local_min(a,b) ((a)<(b)?(a):(b))
38
39 #ifdef local_max
40 #undef local_max
41 #endif
42 #define local_max(a,b) ((a)>(b)?(a):(b))
43
44 static const char *peak_format_ = "%s=%1.8f";
45 static const char *gain_format_ = "%s=%+2.2f dB";
46
47 static double album_peak_, title_peak_;
48
49 const unsigned GRABBAG__REPLAYGAIN_MAX_TAG_SPACE_REQUIRED = 148;
50 /*
51         FLAC__STREAM_METADATA_VORBIS_COMMENT_ENTRY_LENGTH_LEN/8 + 21 + 1 + 10 +
52         FLAC__STREAM_METADATA_VORBIS_COMMENT_ENTRY_LENGTH_LEN/8 + 21 + 1 + 12 +
53         FLAC__STREAM_METADATA_VORBIS_COMMENT_ENTRY_LENGTH_LEN/8 + 21 + 1 + 10 +
54         FLAC__STREAM_METADATA_VORBIS_COMMENT_ENTRY_LENGTH_LEN/8 + 21 + 1 + 12
55 */
56
57 const FLAC__byte * const GRABBAG__REPLAYGAIN_TAG_TITLE_GAIN = (const FLAC__byte * const)"REPLAYGAIN_TRACK_GAIN";
58 const FLAC__byte * const GRABBAG__REPLAYGAIN_TAG_TITLE_PEAK = (const FLAC__byte * const)"REPLAYGAIN_TRACK_PEAK";
59 const FLAC__byte * const GRABBAG__REPLAYGAIN_TAG_ALBUM_GAIN = (const FLAC__byte * const)"REPLAYGAIN_ALBUM_GAIN";
60 const FLAC__byte * const GRABBAG__REPLAYGAIN_TAG_ALBUM_PEAK = (const FLAC__byte * const)"REPLAYGAIN_ALBUM_PEAK";
61
62
63 static FLAC__bool get_file_stats_(const char *filename, struct stat *stats)
64 {
65         FLAC__ASSERT(0 != filename);
66         FLAC__ASSERT(0 != stats);
67         return (0 == stat(filename, stats));
68 }
69
70 static void set_file_stats_(const char *filename, struct stat *stats)
71 {
72         FLAC__ASSERT(0 != filename);
73         FLAC__ASSERT(0 != stats);
74
75         (void)chmod(filename, stats->st_mode);
76 }
77
78 static FLAC__bool append_tag_(FLAC__StreamMetadata *block, const char *format, const FLAC__byte *name, float value)
79 {
80         char buffer[256];
81         char *saved_locale;
82         FLAC__StreamMetadata_VorbisComment_Entry entry;
83
84         FLAC__ASSERT(0 != block);
85         FLAC__ASSERT(block->type == FLAC__METADATA_TYPE_VORBIS_COMMENT);
86         FLAC__ASSERT(0 != name);
87         FLAC__ASSERT(0 != value);
88
89         buffer[sizeof(buffer)-1] = '\0';
90         /*
91          * We need to save the old locale and switch to "C" because the locale
92          * influences the formatting of %f and we want it a certain way.
93          */
94         saved_locale = setlocale(LC_ALL, 0);
95         setlocale(LC_ALL, "C");
96 #if defined _MSC_VER || defined __MINGW32__
97         _snprintf(buffer, sizeof(buffer)-1, format, name, value);
98 #else
99         snprintf(buffer, sizeof(buffer)-1, format, name, value);
100 #endif
101         setlocale(LC_ALL, saved_locale);
102
103         entry.entry = (FLAC__byte *)buffer;
104         entry.length = strlen(buffer);
105
106         return FLAC__metadata_object_vorbiscomment_append_comment(block, entry, /*copy=*/true);
107 }
108
109 FLAC__bool grabbag__replaygain_is_valid_sample_frequency(unsigned sample_frequency)
110 {
111         static const unsigned valid_sample_rates[] = {
112                 8000,
113                 11025,
114                 12000,
115                 16000,
116                 22050,
117                 24000,
118                 32000,
119                 44100,
120                 48000
121         };
122         static const unsigned n_valid_sample_rates = sizeof(valid_sample_rates) / sizeof(valid_sample_rates[0]);
123
124         unsigned i;
125
126         for(i = 0; i < n_valid_sample_rates; i++)
127                 if(sample_frequency == valid_sample_rates[i])
128                         return true;
129         return false;
130 }
131
132 FLAC__bool grabbag__replaygain_init(unsigned sample_frequency)
133 {
134         title_peak_ = album_peak_ = 0.0;
135         return InitGainAnalysis((long)sample_frequency) == INIT_GAIN_ANALYSIS_OK;
136 }
137
138 FLAC__bool grabbag__replaygain_analyze(const FLAC__int32 * const input[], FLAC__bool is_stereo, unsigned bps, unsigned samples)
139 {
140         /* using a small buffer improves data locality; we'd like it to fit easily in the dcache */
141         static Float_t lbuffer[2048], rbuffer[2048];
142         static const unsigned nbuffer = sizeof(lbuffer) / sizeof(lbuffer[0]);
143         FLAC__int32 block_peak = 0, s;
144         unsigned i, j;
145
146         FLAC__ASSERT(bps >= 4 && bps <= FLAC__REFERENCE_CODEC_MAX_BITS_PER_SAMPLE);
147         FLAC__ASSERT(FLAC__MIN_BITS_PER_SAMPLE == 4);
148         /*
149          * We use abs() on a FLAC__int32 which is undefined for the most negative value.
150          * If the reference codec ever handles 32bps we will have to write a special
151          * case here.
152          */
153         FLAC__ASSERT(FLAC__REFERENCE_CODEC_MAX_BITS_PER_SAMPLE < 32);
154
155         if(bps == 16) {
156                 if(is_stereo) {
157                         j = 0;
158                         while(samples > 0) {
159                                 const unsigned n = local_min(samples, nbuffer);
160                                 for(i = 0; i < n; i++, j++) {
161                                         s = input[0][j];
162                                         lbuffer[i] = (Float_t)s;
163                                         s = abs(s);
164                                         block_peak = local_max(block_peak, s);
165
166                                         s = input[1][j];
167                                         rbuffer[i] = (Float_t)s;
168                                         s = abs(s);
169                                         block_peak = local_max(block_peak, s);
170                                 }
171                                 samples -= n;
172                                 if(AnalyzeSamples(lbuffer, rbuffer, n, 2) != GAIN_ANALYSIS_OK)
173                                         return false;
174                         }
175                 }
176                 else {
177                         j = 0;
178                         while(samples > 0) {
179                                 const unsigned n = local_min(samples, nbuffer);
180                                 for(i = 0; i < n; i++, j++) {
181                                         s = input[0][j];
182                                         lbuffer[i] = (Float_t)s;
183                                         s = abs(s);
184                                         block_peak = local_max(block_peak, s);
185                                 }
186                                 samples -= n;
187                                 if(AnalyzeSamples(lbuffer, 0, n, 1) != GAIN_ANALYSIS_OK)
188                                         return false;
189                         }
190                 }
191         }
192         else { /* bps must be < 32 according to above assertion */
193                 const double scale = (
194                         (bps > 16)?
195                                 (double)1. / (double)(1u << (bps - 16)) :
196                                 (double)(1u << (16 - bps))
197                 );
198
199                 if(is_stereo) {
200                         j = 0;
201                         while(samples > 0) {
202                                 const unsigned n = local_min(samples, nbuffer);
203                                 for(i = 0; i < n; i++, j++) {
204                                         s = input[0][j];
205                                         lbuffer[i] = (Float_t)(scale * (double)s);
206                                         s = abs(s);
207                                         block_peak = local_max(block_peak, s);
208
209                                         s = input[1][j];
210                                         rbuffer[i] = (Float_t)(scale * (double)s);
211                                         s = abs(s);
212                                         block_peak = local_max(block_peak, s);
213                                 }
214                                 samples -= n;
215                                 if(AnalyzeSamples(lbuffer, rbuffer, n, 2) != GAIN_ANALYSIS_OK)
216                                         return false;
217                         }
218                 }
219                 else {
220                         j = 0;
221                         while(samples > 0) {
222                                 const unsigned n = local_min(samples, nbuffer);
223                                 for(i = 0; i < n; i++, j++) {
224                                         s = input[0][j];
225                                         lbuffer[i] = (Float_t)(scale * (double)s);
226                                         s = abs(s);
227                                         block_peak = local_max(block_peak, s);
228                                 }
229                                 samples -= n;
230                                 if(AnalyzeSamples(lbuffer, 0, n, 1) != GAIN_ANALYSIS_OK)
231                                         return false;
232                         }
233                 }
234         }
235
236         {
237                 const double peak_scale = (double)(1u << (bps - 1));
238                 double peak = (double)block_peak / peak_scale;
239                 if(peak > title_peak_)
240                         title_peak_ = peak;
241                 if(peak > album_peak_)
242                         album_peak_ = peak;
243         }
244
245         return true;
246 }
247
248 void grabbag__replaygain_get_album(float *gain, float *peak)
249 {
250         *gain = (float)GetAlbumGain();
251         *peak = (float)album_peak_;
252         album_peak_ = 0.0;
253 }
254
255 void grabbag__replaygain_get_title(float *gain, float *peak)
256 {
257         *gain = (float)GetTitleGain();
258         *peak = (float)title_peak_;
259         title_peak_ = 0.0;
260 }
261
262
263 typedef struct {
264         unsigned channels;
265         unsigned bits_per_sample;
266         unsigned sample_rate;
267         FLAC__bool error;
268 } DecoderInstance;
269
270 static FLAC__StreamDecoderWriteStatus write_callback_(const FLAC__FileDecoder *decoder, const FLAC__Frame *frame, const FLAC__int32 * const buffer[], void *client_data)
271 {
272         DecoderInstance *instance = (DecoderInstance*)client_data;
273         const unsigned bits_per_sample = frame->header.bits_per_sample;
274         const unsigned channels = frame->header.channels;
275         const unsigned sample_rate = frame->header.sample_rate;
276         const unsigned samples = frame->header.blocksize;
277
278         (void)decoder;
279
280         if(
281                 !instance->error &&
282                 (channels == 2 || channels == 1) &&
283                 bits_per_sample == instance->bits_per_sample &&
284                 channels == instance->channels &&
285                 sample_rate == instance->sample_rate
286         ) {
287                 instance->error = !grabbag__replaygain_analyze(buffer, channels==2, bits_per_sample, samples);
288         }
289         else {
290                 instance->error = true;
291         }
292
293         if(!instance->error)
294                 return FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE;
295         else
296                 return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
297 }
298
299 static void metadata_callback_(const FLAC__FileDecoder *decoder, const FLAC__StreamMetadata *metadata, void *client_data)
300 {
301         DecoderInstance *instance = (DecoderInstance*)client_data;
302
303         (void)decoder;
304
305         if(metadata->type == FLAC__METADATA_TYPE_STREAMINFO) {
306                 instance->bits_per_sample = metadata->data.stream_info.bits_per_sample;
307                 instance->channels = metadata->data.stream_info.channels;
308                 instance->sample_rate = metadata->data.stream_info.sample_rate;
309
310                 if(instance->channels != 1 && instance->channels != 2) {
311                         instance->error = true;
312                         return;
313                 }
314
315                 if(!grabbag__replaygain_is_valid_sample_frequency(instance->sample_rate)) {
316                         instance->error = true;
317                         return;
318                 }
319         }
320 }
321
322 static void error_callback_(const FLAC__FileDecoder *decoder, FLAC__StreamDecoderErrorStatus status, void *client_data)
323 {
324         DecoderInstance *instance = (DecoderInstance*)client_data;
325
326         (void)decoder, (void)status;
327
328         instance->error = true;
329 }
330
331 const char *grabbag__replaygain_analyze_file(const char *filename, float *title_gain, float *title_peak)
332 {
333         DecoderInstance instance;
334         FLAC__FileDecoder *decoder = FLAC__file_decoder_new();
335
336         if(0 == decoder)
337                 return "memory allocation error";
338
339         instance.error = false;
340
341         /* It does these three by default but lets be explicit: */
342         FLAC__file_decoder_set_md5_checking(decoder, false);
343         FLAC__file_decoder_set_metadata_ignore_all(decoder);
344         FLAC__file_decoder_set_metadata_respond(decoder, FLAC__METADATA_TYPE_STREAMINFO);
345
346         FLAC__file_decoder_set_filename(decoder, filename);
347         FLAC__file_decoder_set_write_callback(decoder, write_callback_);
348         FLAC__file_decoder_set_metadata_callback(decoder, metadata_callback_);
349         FLAC__file_decoder_set_error_callback(decoder, error_callback_);
350         FLAC__file_decoder_set_client_data(decoder, &instance);
351
352         if(FLAC__file_decoder_init(decoder) != FLAC__FILE_DECODER_OK) {
353                 FLAC__file_decoder_delete(decoder);
354                 return "initializing decoder";
355         }
356
357         if(!FLAC__file_decoder_process_until_end_of_file(decoder) || instance.error) {
358                 FLAC__file_decoder_delete(decoder);
359                 return "decoding file";
360         }
361
362         FLAC__file_decoder_delete(decoder);
363
364         grabbag__replaygain_get_title(title_gain, title_peak);
365
366         return 0;
367 }
368
369 const char *grabbag__replaygain_store_to_vorbiscomment(FLAC__StreamMetadata *block, float album_gain, float album_peak, float title_gain, float title_peak)
370 {
371         const char *error;
372
373         if(0 != (error = grabbag__replaygain_store_to_vorbiscomment_title(block, title_gain, title_peak)))
374                 return error;
375
376         if(0 != (error = grabbag__replaygain_store_to_vorbiscomment_album(block, album_gain, album_peak)))
377                 return error;
378
379         return 0;
380 }
381
382 const char *grabbag__replaygain_store_to_vorbiscomment_album(FLAC__StreamMetadata *block, float album_gain, float album_peak)
383 {
384         FLAC__ASSERT(0 != block);
385         FLAC__ASSERT(block->type == FLAC__METADATA_TYPE_VORBIS_COMMENT);
386
387         if(
388                 FLAC__metadata_object_vorbiscomment_remove_entries_matching(block, (const char *)GRABBAG__REPLAYGAIN_TAG_ALBUM_GAIN) < 0 ||
389                 FLAC__metadata_object_vorbiscomment_remove_entries_matching(block, (const char *)GRABBAG__REPLAYGAIN_TAG_ALBUM_PEAK) < 0
390         )
391                 return "memory allocation error";
392
393         if(
394                 !append_tag_(block, peak_format_, GRABBAG__REPLAYGAIN_TAG_ALBUM_PEAK, album_peak) ||
395                 !append_tag_(block, gain_format_, GRABBAG__REPLAYGAIN_TAG_ALBUM_GAIN, album_gain)
396         )
397                 return "memory allocation error";
398
399         return 0;
400 }
401
402 const char *grabbag__replaygain_store_to_vorbiscomment_title(FLAC__StreamMetadata *block, float title_gain, float title_peak)
403 {
404         FLAC__ASSERT(0 != block);
405         FLAC__ASSERT(block->type == FLAC__METADATA_TYPE_VORBIS_COMMENT);
406
407         if(
408                 FLAC__metadata_object_vorbiscomment_remove_entries_matching(block, (const char *)GRABBAG__REPLAYGAIN_TAG_TITLE_GAIN) < 0 ||
409                 FLAC__metadata_object_vorbiscomment_remove_entries_matching(block, (const char *)GRABBAG__REPLAYGAIN_TAG_TITLE_PEAK) < 0
410         )
411                 return "memory allocation error";
412
413         if(
414                 !append_tag_(block, peak_format_, GRABBAG__REPLAYGAIN_TAG_TITLE_PEAK, title_peak) ||
415                 !append_tag_(block, gain_format_, GRABBAG__REPLAYGAIN_TAG_TITLE_GAIN, title_gain)
416         )
417                 return "memory allocation error";
418
419         return 0;
420 }
421
422 static const char *store_to_file_pre_(const char *filename, FLAC__Metadata_Chain **chain, FLAC__StreamMetadata **block)
423 {
424         FLAC__Metadata_Iterator *iterator;
425         const char *error;
426         FLAC__bool found_vc_block = false;
427
428         if(0 == (*chain = FLAC__metadata_chain_new()))
429                 return "memory allocation error";
430
431         if(!FLAC__metadata_chain_read(*chain, filename)) {
432                 error = FLAC__Metadata_ChainStatusString[FLAC__metadata_chain_status(*chain)];
433                 FLAC__metadata_chain_delete(*chain);
434                 return error;
435         }
436
437         if(0 == (iterator = FLAC__metadata_iterator_new())) {
438                 FLAC__metadata_chain_delete(*chain);
439                 return "memory allocation error";
440         }
441
442         FLAC__metadata_iterator_init(iterator, *chain);
443
444         do {
445                 *block = FLAC__metadata_iterator_get_block(iterator);
446                 if((*block)->type == FLAC__METADATA_TYPE_VORBIS_COMMENT)
447                         found_vc_block = true;
448         } while(!found_vc_block && FLAC__metadata_iterator_next(iterator));
449
450         if(!found_vc_block) {
451                 /* create a new block */
452                 *block = FLAC__metadata_object_new(FLAC__METADATA_TYPE_VORBIS_COMMENT);
453                 if(0 == *block) {
454                         FLAC__metadata_chain_delete(*chain);
455                         FLAC__metadata_iterator_delete(iterator);
456                         return "memory allocation error";
457                 }
458                 while(FLAC__metadata_iterator_next(iterator))
459                         ;
460                 if(!FLAC__metadata_iterator_insert_block_after(iterator, *block)) {
461                         error = FLAC__Metadata_ChainStatusString[FLAC__metadata_chain_status(*chain)];
462                         FLAC__metadata_chain_delete(*chain);
463                         FLAC__metadata_iterator_delete(iterator);
464                         return error;
465                 }
466                 /* iterator is left pointing to new block */
467                 FLAC__ASSERT(FLAC__metadata_iterator_get_block(iterator) == *block);
468         }
469
470         FLAC__metadata_iterator_delete(iterator);
471
472         FLAC__ASSERT(0 != *block);
473         FLAC__ASSERT((*block)->type == FLAC__METADATA_TYPE_VORBIS_COMMENT);
474
475         return 0;
476 }
477
478 static const char *store_to_file_post_(const char *filename, FLAC__Metadata_Chain *chain, FLAC__bool preserve_modtime)
479 {
480         struct stat stats;
481         const FLAC__bool have_stats = get_file_stats_(filename, &stats);
482
483         (void)grabbag__file_change_stats(filename, /*read_only=*/false);
484
485         FLAC__metadata_chain_sort_padding(chain);
486         if(!FLAC__metadata_chain_write(chain, /*use_padding=*/true, preserve_modtime)) {
487                 FLAC__metadata_chain_delete(chain);
488                 return FLAC__Metadata_ChainStatusString[FLAC__metadata_chain_status(chain)];
489         }
490
491         FLAC__metadata_chain_delete(chain);
492
493         if(have_stats)
494                 set_file_stats_(filename, &stats);
495
496         return 0;
497 }
498
499 const char *grabbag__replaygain_store_to_file(const char *filename, float album_gain, float album_peak, float title_gain, float title_peak, FLAC__bool preserve_modtime)
500 {
501         FLAC__Metadata_Chain *chain;
502         FLAC__StreamMetadata *block;
503         const char *error;
504
505         if(0 != (error = store_to_file_pre_(filename, &chain, &block)))
506                 return error;
507
508         if(0 != (error = grabbag__replaygain_store_to_vorbiscomment(block, album_gain, album_peak, title_gain, title_peak))) {
509                 FLAC__metadata_chain_delete(chain);
510                 return error;
511         }
512
513         if(0 != (error = store_to_file_post_(filename, chain, preserve_modtime)))
514                 return error;
515
516         return 0;
517 }
518
519 const char *grabbag__replaygain_store_to_file_album(const char *filename, float album_gain, float album_peak, FLAC__bool preserve_modtime)
520 {
521         FLAC__Metadata_Chain *chain;
522         FLAC__StreamMetadata *block;
523         const char *error;
524
525         if(0 != (error = store_to_file_pre_(filename, &chain, &block)))
526                 return error;
527
528         if(0 != (error = grabbag__replaygain_store_to_vorbiscomment_album(block, album_gain, album_peak))) {
529                 FLAC__metadata_chain_delete(chain);
530                 return error;
531         }
532
533         if(0 != (error = store_to_file_post_(filename, chain, preserve_modtime)))
534                 return error;
535
536         return 0;
537 }
538
539 const char *grabbag__replaygain_store_to_file_title(const char *filename, float title_gain, float title_peak, FLAC__bool preserve_modtime)
540 {
541         FLAC__Metadata_Chain *chain;
542         FLAC__StreamMetadata *block;
543         const char *error;
544
545         if(0 != (error = store_to_file_pre_(filename, &chain, &block)))
546                 return error;
547
548         if(0 != (error = grabbag__replaygain_store_to_vorbiscomment_title(block, title_gain, title_peak))) {
549                 FLAC__metadata_chain_delete(chain);
550                 return error;
551         }
552
553         if(0 != (error = store_to_file_post_(filename, chain, preserve_modtime)))
554                 return error;
555
556         return 0;
557 }
558
559 static FLAC__bool parse_double_(const FLAC__StreamMetadata_VorbisComment_Entry *entry, double *val)
560 {
561         char s[32], *end;
562         const char *p, *q;
563         double v;
564
565         FLAC__ASSERT(0 != entry);
566         FLAC__ASSERT(0 != val);
567
568         p = (const char *)entry->entry;
569         q = strchr(p, '=');
570         if(0 == q)
571                 return false;
572         q++;
573         memset(s, 0, sizeof(s)-1);
574         strncpy(s, q, local_min(sizeof(s)-1, entry->length - (q-p)));
575
576         v = strtod(s, &end);
577         if(end == s)
578                 return false;
579
580         *val = v;
581         return true;
582 }
583
584 FLAC__bool grabbag__replaygain_load_from_vorbiscomment(const FLAC__StreamMetadata *block, FLAC__bool album_mode, double *gain, double *peak)
585 {
586         int gain_offset, peak_offset;
587
588         FLAC__ASSERT(0 != block);
589         FLAC__ASSERT(block->type == FLAC__METADATA_TYPE_VORBIS_COMMENT);
590
591         if(0 > (gain_offset = FLAC__metadata_object_vorbiscomment_find_entry_from(block, /*offset=*/0, (const char *)(album_mode? GRABBAG__REPLAYGAIN_TAG_ALBUM_GAIN : GRABBAG__REPLAYGAIN_TAG_TITLE_GAIN))))
592                 return false;
593         if(0 > (peak_offset = FLAC__metadata_object_vorbiscomment_find_entry_from(block, /*offset=*/0, (const char *)(album_mode? GRABBAG__REPLAYGAIN_TAG_ALBUM_PEAK : GRABBAG__REPLAYGAIN_TAG_TITLE_PEAK))))
594                 return false;
595
596         if(!parse_double_(block->data.vorbis_comment.comments + gain_offset, gain))
597                 return false;
598         if(!parse_double_(block->data.vorbis_comment.comments + peak_offset, peak))
599                 return false;
600
601         return true;
602 }
603
604 double grabbag__replaygain_compute_scale_factor(double peak, double gain, double preamp, FLAC__bool prevent_clipping)
605 {
606         double scale;
607         FLAC__ASSERT(peak >= 0.0);
608         gain += preamp;
609         scale = (float) pow(10.0, gain * 0.05);
610         if(prevent_clipping && peak > 0.0) {
611                 const double max_scale = (float)(1.0 / peak);
612                 if(scale > max_scale)
613                         scale = max_scale;
614         }
615         return scale;
616 }