annotate Plugins/Input/flac/replaygain.c @ 145:8794a4781a15 trunk

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