1
/* grabbag - Convenience lib for various routines common to several tools
2
* Copyright (C) 2002,2003 Josh Coalson
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.
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.
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.
19
#include "share/grabbag.h"
20
#include "share/gain_analysis.h"
21
#include "FLAC/assert.h"
22
#include "FLAC/file_decoder.h"
23
#include "FLAC/metadata.h"
29
#if defined _MSC_VER || defined __MINGW32__
30
#include <io.h> /* for chmod() */
32
#include <sys/stat.h> /* for stat(), maybe chmod() */
37
#define local_min(a,b) ((a)<(b)?(a):(b))
42
#define local_max(a,b) ((a)>(b)?(a):(b))
44
static const FLAC__byte *tag_title_gain_ = "REPLAYGAIN_TRACK_GAIN";
45
static const FLAC__byte *tag_title_peak_ = "REPLAYGAIN_TRACK_PEAK";
46
static const FLAC__byte *tag_album_gain_ = "REPLAYGAIN_ALBUM_GAIN";
47
static const FLAC__byte *tag_album_peak_ = "REPLAYGAIN_ALBUM_PEAK";
48
static const char *peak_format_ = "%s=%1.8f";
49
static const char *gain_format_ = "%s=%+2.2f dB";
51
static double album_peak_, title_peak_;
53
const unsigned GRABBAG__REPLAYGAIN_MAX_TAG_SPACE_REQUIRED = 148;
55
FLAC__STREAM_METADATA_VORBIS_COMMENT_ENTRY_LENGTH_LEN/8 + 21 + 1 + 10 +
56
FLAC__STREAM_METADATA_VORBIS_COMMENT_ENTRY_LENGTH_LEN/8 + 21 + 1 + 12 +
57
FLAC__STREAM_METADATA_VORBIS_COMMENT_ENTRY_LENGTH_LEN/8 + 21 + 1 + 10 +
58
FLAC__STREAM_METADATA_VORBIS_COMMENT_ENTRY_LENGTH_LEN/8 + 21 + 1 + 12
62
static FLAC__bool get_file_stats_(const char *filename, struct stat *stats)
64
FLAC__ASSERT(0 != filename);
65
FLAC__ASSERT(0 != stats);
66
return (0 == stat(filename, stats));
69
static void set_file_stats_(const char *filename, struct stat *stats)
71
FLAC__ASSERT(0 != filename);
72
FLAC__ASSERT(0 != stats);
74
(void)chmod(filename, stats->st_mode);
77
static FLAC__bool append_tag_(FLAC__StreamMetadata *block, const char *format, const FLAC__byte *name, float value)
81
FLAC__StreamMetadata_VorbisComment_Entry entry;
83
FLAC__ASSERT(0 != block);
84
FLAC__ASSERT(block->type == FLAC__METADATA_TYPE_VORBIS_COMMENT);
85
FLAC__ASSERT(0 != name);
86
FLAC__ASSERT(0 != value);
88
buffer[sizeof(buffer)-1] = '\0';
90
* We need to save the old locale and switch to "C" because the locale
91
* influences the formatting of %f and we want it a certain way.
93
saved_locale = setlocale(LC_ALL, 0);
94
setlocale(LC_ALL, "C");
95
#if defined _MSC_VER || defined __MINGW32__
96
_snprintf(buffer, sizeof(buffer)-1, format, name, value);
98
snprintf(buffer, sizeof(buffer)-1, format, name, value);
100
setlocale(LC_ALL, saved_locale);
102
entry.entry = buffer;
103
entry.length = strlen(buffer);
105
return FLAC__metadata_object_vorbiscomment_insert_comment(block, block->data.vorbis_comment.num_comments, entry, /*copy=*/true);
108
FLAC__bool grabbag__replaygain_is_valid_sample_frequency(unsigned sample_frequency)
110
static const unsigned valid_sample_rates[] = {
121
static const unsigned n_valid_sample_rates = sizeof(valid_sample_rates) / sizeof(valid_sample_rates[0]);
125
for(i = 0; i < n_valid_sample_rates; i++)
126
if(sample_frequency == valid_sample_rates[i])
131
FLAC__bool grabbag__replaygain_init(unsigned sample_frequency)
133
title_peak_ = album_peak_ = 0.0;
134
return InitGainAnalysis((long)sample_frequency) == INIT_GAIN_ANALYSIS_OK;
137
FLAC__bool grabbag__replaygain_analyze(const FLAC__int32 * const input[], FLAC__bool is_stereo, unsigned bps, unsigned samples)
139
/* using a small buffer improves data locality; we'd like it to fit easily in the dcache */
140
static Float_t lbuffer[2048], rbuffer[2048];
141
static const unsigned nbuffer = sizeof(lbuffer) / sizeof(lbuffer[0]);
142
FLAC__int32 block_peak = 0, s;
145
FLAC__ASSERT(bps >= 4 && bps <= FLAC__REFERENCE_CODEC_MAX_BITS_PER_SAMPLE);
146
FLAC__ASSERT(FLAC__MIN_BITS_PER_SAMPLE == 4);
148
* We use abs() on a FLAC__int32 which is undefined for the most negative value.
149
* If the reference codec ever handles 32bps we will have to write a special
152
FLAC__ASSERT(FLAC__REFERENCE_CODEC_MAX_BITS_PER_SAMPLE < 32);
158
const unsigned n = local_min(samples, nbuffer);
159
for(i = 0; i < n; i++, j++) {
161
lbuffer[i] = (Float_t)s;
163
block_peak = local_max(block_peak, s);
166
rbuffer[i] = (Float_t)s;
168
block_peak = local_max(block_peak, s);
171
if(AnalyzeSamples(lbuffer, rbuffer, n, 2) != GAIN_ANALYSIS_OK)
178
const unsigned n = local_min(samples, nbuffer);
179
for(i = 0; i < n; i++, j++) {
181
lbuffer[i] = (Float_t)s;
183
block_peak = local_max(block_peak, s);
186
if(AnalyzeSamples(lbuffer, 0, n, 1) != GAIN_ANALYSIS_OK)
191
else { /* bps must be < 32 according to above assertion */
192
const double scale = (
194
(double)1. / (double)(1u << (bps - 16)) :
195
(double)(1u << (16 - bps))
201
const unsigned n = local_min(samples, nbuffer);
202
for(i = 0; i < n; i++, j++) {
204
lbuffer[i] = (Float_t)(scale * (double)s);
206
block_peak = local_max(block_peak, s);
209
rbuffer[i] = (Float_t)(scale * (double)s);
211
block_peak = local_max(block_peak, s);
214
if(AnalyzeSamples(lbuffer, rbuffer, n, 2) != GAIN_ANALYSIS_OK)
221
const unsigned n = local_min(samples, nbuffer);
222
for(i = 0; i < n; i++, j++) {
224
lbuffer[i] = (Float_t)(scale * (double)s);
226
block_peak = local_max(block_peak, s);
229
if(AnalyzeSamples(lbuffer, 0, n, 1) != GAIN_ANALYSIS_OK)
236
const double peak_scale = (double)(1u << (bps - 1));
237
double peak = (double)block_peak / peak_scale;
238
if(peak > title_peak_)
240
if(peak > album_peak_)
247
void grabbag__replaygain_get_album(float *gain, float *peak)
249
*gain = (float)GetAlbumGain();
250
*peak = (float)album_peak_;
254
void grabbag__replaygain_get_title(float *gain, float *peak)
256
*gain = (float)GetTitleGain();
257
*peak = (float)title_peak_;
264
unsigned bits_per_sample;
265
unsigned sample_rate;
269
static FLAC__StreamDecoderWriteStatus write_callback_(const FLAC__FileDecoder *decoder, const FLAC__Frame *frame, const FLAC__int32 * const buffer[], void *client_data)
271
DecoderInstance *instance = (DecoderInstance*)client_data;
272
const unsigned bits_per_sample = frame->header.bits_per_sample;
273
const unsigned channels = frame->header.channels;
274
const unsigned sample_rate = frame->header.sample_rate;
275
const unsigned samples = frame->header.blocksize;
281
(channels == 2 || channels == 1) &&
282
bits_per_sample == instance->bits_per_sample &&
283
channels == instance->channels &&
284
sample_rate == instance->sample_rate
286
instance->error = !grabbag__replaygain_analyze(buffer, channels==2, bits_per_sample, samples);
289
instance->error = true;
293
return FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE;
295
return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
298
static void metadata_callback_(const FLAC__FileDecoder *decoder, const FLAC__StreamMetadata *metadata, void *client_data)
300
DecoderInstance *instance = (DecoderInstance*)client_data;
304
if(metadata->type == FLAC__METADATA_TYPE_STREAMINFO) {
305
instance->bits_per_sample = metadata->data.stream_info.bits_per_sample;
306
instance->channels = metadata->data.stream_info.channels;
307
instance->sample_rate = metadata->data.stream_info.sample_rate;
309
if(instance->channels != 1 && instance->channels != 2) {
310
instance->error = true;
314
if(!grabbag__replaygain_is_valid_sample_frequency(instance->sample_rate)) {
315
instance->error = true;
321
static void error_callback_(const FLAC__FileDecoder *decoder, FLAC__StreamDecoderErrorStatus status, void *client_data)
323
DecoderInstance *instance = (DecoderInstance*)client_data;
325
(void)decoder, (void)status;
327
instance->error = true;
330
const char *grabbag__replaygain_analyze_file(const char *filename, float *title_gain, float *title_peak)
332
DecoderInstance instance;
333
FLAC__FileDecoder *decoder = FLAC__file_decoder_new();
336
return "memory allocation error";
338
instance.error = false;
340
/* It does these three by default but lets be explicit: */
341
FLAC__file_decoder_set_md5_checking(decoder, false);
342
FLAC__file_decoder_set_metadata_ignore_all(decoder);
343
FLAC__file_decoder_set_metadata_respond(decoder, FLAC__METADATA_TYPE_STREAMINFO);
345
FLAC__file_decoder_set_filename(decoder, filename);
346
FLAC__file_decoder_set_write_callback(decoder, write_callback_);
347
FLAC__file_decoder_set_metadata_callback(decoder, metadata_callback_);
348
FLAC__file_decoder_set_error_callback(decoder, error_callback_);
349
FLAC__file_decoder_set_client_data(decoder, &instance);
351
if(FLAC__file_decoder_init(decoder) != FLAC__FILE_DECODER_OK) {
352
FLAC__file_decoder_delete(decoder);
353
return "initializing decoder";
356
if(!FLAC__file_decoder_process_until_end_of_file(decoder) || instance.error) {
357
FLAC__file_decoder_delete(decoder);
358
return "decoding file";
361
FLAC__file_decoder_delete(decoder);
363
grabbag__replaygain_get_title(title_gain, title_peak);
368
const char *grabbag__replaygain_store_to_vorbiscomment(FLAC__StreamMetadata *block, float album_gain, float album_peak, float title_gain, float title_peak)
372
if(0 != (error = grabbag__replaygain_store_to_vorbiscomment_title(block, title_gain, title_peak)))
375
if(0 != (error = grabbag__replaygain_store_to_vorbiscomment_album(block, album_gain, album_peak)))
381
const char *grabbag__replaygain_store_to_vorbiscomment_album(FLAC__StreamMetadata *block, float album_gain, float album_peak)
383
FLAC__ASSERT(0 != block);
384
FLAC__ASSERT(block->type == FLAC__METADATA_TYPE_VORBIS_COMMENT);
387
FLAC__metadata_object_vorbiscomment_remove_entries_matching(block, tag_album_gain_) < 0 ||
388
FLAC__metadata_object_vorbiscomment_remove_entries_matching(block, tag_album_peak_) < 0
390
return "memory allocation error";
393
!append_tag_(block, peak_format_, tag_album_peak_, album_peak) ||
394
!append_tag_(block, gain_format_, tag_album_gain_, album_gain)
396
return "memory allocation error";
401
const char *grabbag__replaygain_store_to_vorbiscomment_title(FLAC__StreamMetadata *block, float title_gain, float title_peak)
403
FLAC__ASSERT(0 != block);
404
FLAC__ASSERT(block->type == FLAC__METADATA_TYPE_VORBIS_COMMENT);
407
FLAC__metadata_object_vorbiscomment_remove_entries_matching(block, tag_title_gain_) < 0 ||
408
FLAC__metadata_object_vorbiscomment_remove_entries_matching(block, tag_title_peak_) < 0
410
return "memory allocation error";
413
!append_tag_(block, peak_format_, tag_title_peak_, title_peak) ||
414
!append_tag_(block, gain_format_, tag_title_gain_, title_gain)
416
return "memory allocation error";
421
static const char *store_to_file_pre_(const char *filename, FLAC__Metadata_Chain **chain, FLAC__StreamMetadata **block)
423
FLAC__Metadata_Iterator *iterator;
425
FLAC__bool found_vc_block = false;
427
if(0 == (*chain = FLAC__metadata_chain_new()))
428
return "memory allocation error";
430
if(!FLAC__metadata_chain_read(*chain, filename)) {
431
error = FLAC__Metadata_ChainStatusString[FLAC__metadata_chain_status(*chain)];
432
FLAC__metadata_chain_delete(*chain);
436
if(0 == (iterator = FLAC__metadata_iterator_new())) {
437
FLAC__metadata_chain_delete(*chain);
438
return "memory allocation error";
441
FLAC__metadata_iterator_init(iterator, *chain);
444
*block = FLAC__metadata_iterator_get_block(iterator);
445
if((*block)->type == FLAC__METADATA_TYPE_VORBIS_COMMENT)
446
found_vc_block = true;
447
} while(!found_vc_block && FLAC__metadata_iterator_next(iterator));
449
if(!found_vc_block) {
450
/* create a new block */
451
*block = FLAC__metadata_object_new(FLAC__METADATA_TYPE_VORBIS_COMMENT);
453
FLAC__metadata_chain_delete(*chain);
454
FLAC__metadata_iterator_delete(iterator);
455
return "memory allocation error";
457
while(FLAC__metadata_iterator_next(iterator))
459
if(!FLAC__metadata_iterator_insert_block_after(iterator, *block)) {
460
error = FLAC__Metadata_ChainStatusString[FLAC__metadata_chain_status(*chain)];
461
FLAC__metadata_chain_delete(*chain);
462
FLAC__metadata_iterator_delete(iterator);
465
/* iterator is left pointing to new block */
466
FLAC__ASSERT(FLAC__metadata_iterator_get_block(iterator) == *block);
469
FLAC__metadata_iterator_delete(iterator);
471
FLAC__ASSERT(0 != *block);
472
FLAC__ASSERT((*block)->type == FLAC__METADATA_TYPE_VORBIS_COMMENT);
477
static const char *store_to_file_post_(const char *filename, FLAC__Metadata_Chain *chain, FLAC__bool preserve_modtime)
480
const FLAC__bool have_stats = get_file_stats_(filename, &stats);
482
(void)grabbag__file_change_stats(filename, /*read_only=*/false);
484
FLAC__metadata_chain_sort_padding(chain);
485
if(!FLAC__metadata_chain_write(chain, /*use_padding=*/true, preserve_modtime)) {
486
FLAC__metadata_chain_delete(chain);
487
return FLAC__Metadata_ChainStatusString[FLAC__metadata_chain_status(chain)];
490
FLAC__metadata_chain_delete(chain);
493
set_file_stats_(filename, &stats);
498
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
FLAC__Metadata_Chain *chain;
501
FLAC__StreamMetadata *block;
504
if(0 != (error = store_to_file_pre_(filename, &chain, &block)))
507
if(0 != (error = grabbag__replaygain_store_to_vorbiscomment(block, album_gain, album_peak, title_gain, title_peak))) {
508
FLAC__metadata_chain_delete(chain);
512
if(0 != (error = store_to_file_post_(filename, chain, preserve_modtime)))
518
const char *grabbag__replaygain_store_to_file_album(const char *filename, float album_gain, float album_peak, FLAC__bool preserve_modtime)
520
FLAC__Metadata_Chain *chain;
521
FLAC__StreamMetadata *block;
524
if(0 != (error = store_to_file_pre_(filename, &chain, &block)))
527
if(0 != (error = grabbag__replaygain_store_to_vorbiscomment_album(block, album_gain, album_peak))) {
528
FLAC__metadata_chain_delete(chain);
532
if(0 != (error = store_to_file_post_(filename, chain, preserve_modtime)))
538
const char *grabbag__replaygain_store_to_file_title(const char *filename, float title_gain, float title_peak, FLAC__bool preserve_modtime)
540
FLAC__Metadata_Chain *chain;
541
FLAC__StreamMetadata *block;
544
if(0 != (error = store_to_file_pre_(filename, &chain, &block)))
547
if(0 != (error = grabbag__replaygain_store_to_vorbiscomment_title(block, title_gain, title_peak))) {
548
FLAC__metadata_chain_delete(chain);
552
if(0 != (error = store_to_file_post_(filename, chain, preserve_modtime)))
558
static FLAC__bool parse_double_(const FLAC__StreamMetadata_VorbisComment_Entry *entry, double *val)
564
FLAC__ASSERT(0 != entry);
565
FLAC__ASSERT(0 != val);
567
p = (const char *)entry->entry;
572
memset(s, 0, sizeof(s)-1);
573
strncpy(s, q, local_min(sizeof(s)-1, entry->length - (q-p)));
583
FLAC__bool grabbag__replaygain_load_from_vorbiscomment(const FLAC__StreamMetadata *block, FLAC__bool album_mode, double *gain, double *peak)
585
int gain_offset, peak_offset;
587
FLAC__ASSERT(0 != block);
588
FLAC__ASSERT(block->type == FLAC__METADATA_TYPE_VORBIS_COMMENT);
590
if(0 > (gain_offset = FLAC__metadata_object_vorbiscomment_find_entry_from(block, /*offset=*/0, album_mode? tag_album_gain_ : tag_title_gain_)))
592
if(0 > (peak_offset = FLAC__metadata_object_vorbiscomment_find_entry_from(block, /*offset=*/0, album_mode? tag_album_peak_ : tag_title_peak_)))
595
if(!parse_double_(block->data.vorbis_comment.comments + gain_offset, gain))
597
if(!parse_double_(block->data.vorbis_comment.comments + peak_offset, peak))
603
double grabbag__replaygain_compute_scale_factor(double peak, double gain, double preamp, FLAC__bool prevent_clipping)
606
FLAC__ASSERT(peak >= 0.0);
608
scale = (float) pow(10.0, gain * 0.05);
609
if(prevent_clipping && peak > 0.0) {
610
const double max_scale = (float)(1.0 / peak);
611
if(scale > max_scale)