/src/flac/src/share/grabbag/replaygain.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* grabbag - Convenience lib for various routines common to several tools |
2 | | * Copyright (C) 2002-2009 Josh Coalson |
3 | | * Copyright (C) 2011-2023 Xiph.Org Foundation |
4 | | * |
5 | | * This library is free software; you can redistribute it and/or |
6 | | * modify it under the terms of the GNU Lesser General Public |
7 | | * License as published by the Free Software Foundation; either |
8 | | * version 2.1 of the License, or (at your option) any later version. |
9 | | * |
10 | | * This library is distributed in the hope that it will be useful, |
11 | | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
12 | | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
13 | | * Lesser General Public License for more details. |
14 | | * |
15 | | * You should have received a copy of the GNU Lesser General Public |
16 | | * License along with this library; if not, write to the Free Software |
17 | | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
18 | | */ |
19 | | |
20 | | #ifdef HAVE_CONFIG_H |
21 | | # include <config.h> |
22 | | #endif |
23 | | |
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 | | #include "FLAC/assert.h" |
35 | | #include "FLAC/metadata.h" |
36 | | #include "FLAC/stream_decoder.h" |
37 | | #include "share/grabbag.h" |
38 | | #include "share/replaygain_analysis.h" |
39 | | #include "share/safe_str.h" |
40 | | |
41 | | #ifdef local_min |
42 | | #undef local_min |
43 | | #endif |
44 | 136k | #define local_min(a,b) ((a)<(b)?(a):(b)) |
45 | | |
46 | | #ifdef local_max |
47 | | #undef local_max |
48 | | #endif |
49 | 296M | #define local_max(a,b) ((a)>(b)?(a):(b)) |
50 | | |
51 | | #ifdef abs32 |
52 | | #undef abs32 |
53 | | #endif |
54 | 249M | #define abs32(a) (((a)==INT32_MIN)?INT32_MAX:abs(a)) |
55 | | |
56 | | static const char *reference_format_ = "%s=%2.1f dB"; |
57 | | static const char *gain_format_ = "%s=%+2.2f dB"; |
58 | | static const char *peak_format_ = "%s=%1.8f"; |
59 | | |
60 | | static double album_peak_, title_peak_; |
61 | | |
62 | | const uint32_t GRABBAG__REPLAYGAIN_MAX_TAG_SPACE_REQUIRED = 190; |
63 | | /* |
64 | | FLAC__STREAM_METADATA_VORBIS_COMMENT_ENTRY_LENGTH_LEN/8 + 29 + 1 + 8 + |
65 | | FLAC__STREAM_METADATA_VORBIS_COMMENT_ENTRY_LENGTH_LEN/8 + 21 + 1 + 10 + |
66 | | FLAC__STREAM_METADATA_VORBIS_COMMENT_ENTRY_LENGTH_LEN/8 + 21 + 1 + 12 + |
67 | | FLAC__STREAM_METADATA_VORBIS_COMMENT_ENTRY_LENGTH_LEN/8 + 21 + 1 + 10 + |
68 | | FLAC__STREAM_METADATA_VORBIS_COMMENT_ENTRY_LENGTH_LEN/8 + 21 + 1 + 12 |
69 | | */ |
70 | | |
71 | | const FLAC__byte * const GRABBAG__REPLAYGAIN_TAG_REFERENCE_LOUDNESS = (const FLAC__byte * const)"REPLAYGAIN_REFERENCE_LOUDNESS"; |
72 | | const FLAC__byte * const GRABBAG__REPLAYGAIN_TAG_TITLE_GAIN = (const FLAC__byte * const)"REPLAYGAIN_TRACK_GAIN"; |
73 | | const FLAC__byte * const GRABBAG__REPLAYGAIN_TAG_TITLE_PEAK = (const FLAC__byte * const)"REPLAYGAIN_TRACK_PEAK"; |
74 | | const FLAC__byte * const GRABBAG__REPLAYGAIN_TAG_ALBUM_GAIN = (const FLAC__byte * const)"REPLAYGAIN_ALBUM_GAIN"; |
75 | | const FLAC__byte * const GRABBAG__REPLAYGAIN_TAG_ALBUM_PEAK = (const FLAC__byte * const)"REPLAYGAIN_ALBUM_PEAK"; |
76 | | |
77 | | |
78 | | static FLAC__bool get_file_stats_(const char *filename, struct flac_stat_s *stats) |
79 | 22 | { |
80 | 22 | FLAC__ASSERT(0 != filename); |
81 | 22 | FLAC__ASSERT(0 != stats); |
82 | 22 | return (0 == flac_stat(filename, stats)); |
83 | 22 | } |
84 | | |
85 | | static void set_file_stats_(const char *filename, struct flac_stat_s *stats) |
86 | 22 | { |
87 | 22 | FLAC__ASSERT(0 != filename); |
88 | 22 | FLAC__ASSERT(0 != stats); |
89 | | |
90 | 22 | (void)flac_chmod(filename, stats->st_mode); |
91 | 22 | } |
92 | | |
93 | | static FLAC__bool append_tag_(FLAC__StreamMetadata *block, const char *format, const FLAC__byte *name, float value) |
94 | 110 | { |
95 | 110 | char buffer[256]; |
96 | 110 | char *saved_locale; |
97 | 110 | FLAC__StreamMetadata_VorbisComment_Entry entry; |
98 | | |
99 | 110 | FLAC__ASSERT(0 != block); |
100 | 110 | FLAC__ASSERT(block->type == FLAC__METADATA_TYPE_VORBIS_COMMENT); |
101 | 110 | FLAC__ASSERT(0 != format); |
102 | 110 | FLAC__ASSERT(0 != name); |
103 | | |
104 | 110 | buffer[sizeof(buffer)-1] = '\0'; |
105 | | /* |
106 | | * We need to save the old locale and switch to "C" because the locale |
107 | | * influences the formatting of %f and we want it a certain way. |
108 | | */ |
109 | 110 | saved_locale = strdup(setlocale(LC_ALL, 0)); |
110 | 110 | if (0 == saved_locale) |
111 | 0 | return false; |
112 | 110 | setlocale(LC_ALL, "C"); |
113 | 110 | flac_snprintf(buffer, sizeof(buffer), format, name, value); |
114 | 110 | setlocale(LC_ALL, saved_locale); |
115 | 110 | free(saved_locale); |
116 | | |
117 | 110 | entry.entry = (FLAC__byte *)buffer; |
118 | 110 | entry.length = strlen(buffer); |
119 | | |
120 | 110 | return FLAC__metadata_object_vorbiscomment_append_comment(block, entry, /*copy=*/true); |
121 | 110 | } |
122 | | |
123 | | FLAC__bool grabbag__replaygain_is_valid_sample_frequency(uint32_t sample_frequency) |
124 | 36.5k | { |
125 | 36.5k | return ValidGainFrequency( sample_frequency ); |
126 | 36.5k | } |
127 | | |
128 | | FLAC__bool grabbag__replaygain_init(uint32_t sample_frequency) |
129 | 12.2k | { |
130 | 12.2k | title_peak_ = album_peak_ = 0.0; |
131 | 12.2k | return InitGainAnalysis((long)sample_frequency) == INIT_GAIN_ANALYSIS_OK; |
132 | 12.2k | } |
133 | | |
134 | | FLAC__bool grabbag__replaygain_analyze(const FLAC__int32 * const input[], FLAC__bool is_stereo, uint32_t bps, uint32_t samples) |
135 | 37.5k | { |
136 | | /* using a small buffer improves data locality; we'd like it to fit easily in the dcache */ |
137 | 37.5k | static flac_float_t lbuffer[2048], rbuffer[2048]; |
138 | 37.5k | static const uint32_t nbuffer = sizeof(lbuffer) / sizeof(lbuffer[0]); |
139 | 37.5k | FLAC__int32 block_peak = 0, s; |
140 | 37.5k | uint32_t i, j; |
141 | | |
142 | 37.5k | FLAC__ASSERT(bps >= FLAC__MIN_BITS_PER_SAMPLE && bps <= FLAC__MAX_BITS_PER_SAMPLE); |
143 | 37.5k | FLAC__ASSERT(FLAC__MIN_BITS_PER_SAMPLE == 4 && FLAC__MAX_BITS_PER_SAMPLE == 32); |
144 | | |
145 | 37.5k | if(bps == 16) { |
146 | 4.20k | if(is_stereo) { |
147 | 777 | j = 0; |
148 | 8.22k | while(samples > 0) { |
149 | 7.44k | const uint32_t n = local_min(samples, nbuffer); |
150 | 14.2M | for(i = 0; i < n; i++, j++) { |
151 | 14.1M | s = input[0][j]; |
152 | 14.1M | lbuffer[i] = (flac_float_t)s; |
153 | 14.1M | s = abs(s); |
154 | 14.1M | block_peak = local_max(block_peak, s); |
155 | | |
156 | 14.1M | s = input[1][j]; |
157 | 14.1M | rbuffer[i] = (flac_float_t)s; |
158 | 14.1M | s = abs(s); |
159 | 14.1M | block_peak = local_max(block_peak, s); |
160 | 14.1M | } |
161 | 7.44k | samples -= n; |
162 | 7.44k | if(AnalyzeSamples(lbuffer, rbuffer, n, 2) != GAIN_ANALYSIS_OK) |
163 | 0 | return false; |
164 | 7.44k | } |
165 | 777 | } |
166 | 3.42k | else { |
167 | 3.42k | j = 0; |
168 | 14.9k | while(samples > 0) { |
169 | 11.5k | const uint32_t n = local_min(samples, nbuffer); |
170 | 18.7M | for(i = 0; i < n; i++, j++) { |
171 | 18.6M | s = input[0][j]; |
172 | 18.6M | lbuffer[i] = (flac_float_t)s; |
173 | 18.6M | s = abs(s); |
174 | 18.6M | block_peak = local_max(block_peak, s); |
175 | 18.6M | } |
176 | 11.5k | samples -= n; |
177 | 11.5k | if(AnalyzeSamples(lbuffer, 0, n, 1) != GAIN_ANALYSIS_OK) |
178 | 0 | return false; |
179 | 11.5k | } |
180 | 3.42k | } |
181 | 4.20k | } |
182 | 33.3k | else { |
183 | 33.3k | const double scale = ( |
184 | 33.3k | (bps > 16)? |
185 | 7.66k | (double)1. / (double)(1u << (bps - 16)) : |
186 | 33.3k | (double)(1u << (16 - bps)) |
187 | 33.3k | ); |
188 | | |
189 | 33.3k | if(is_stereo) { |
190 | 26.8k | j = 0; |
191 | 83.0k | while(samples > 0) { |
192 | 56.2k | const uint32_t n = local_min(samples, nbuffer); |
193 | 66.5M | for(i = 0; i < n; i++, j++) { |
194 | 66.4M | s = input[0][j]; |
195 | 66.4M | lbuffer[i] = (flac_float_t)(scale * (double)s); |
196 | 66.4M | s = abs32(s); |
197 | 66.4M | block_peak = local_max(block_peak, s); |
198 | | |
199 | 66.4M | s = input[1][j]; |
200 | 66.4M | rbuffer[i] = (flac_float_t)(scale * (double)s); |
201 | 66.4M | s = abs32(s); |
202 | 66.4M | block_peak = local_max(block_peak, s); |
203 | 66.4M | } |
204 | 56.2k | samples -= n; |
205 | 56.2k | if(AnalyzeSamples(lbuffer, rbuffer, n, 2) != GAIN_ANALYSIS_OK) |
206 | 0 | return false; |
207 | 56.2k | } |
208 | 26.8k | } |
209 | 6.55k | else { |
210 | 6.55k | j = 0; |
211 | 68.1k | while(samples > 0) { |
212 | 61.6k | const uint32_t n = local_min(samples, nbuffer); |
213 | 116M | for(i = 0; i < n; i++, j++) { |
214 | 116M | s = input[0][j]; |
215 | 116M | lbuffer[i] = (flac_float_t)(scale * (double)s); |
216 | 116M | s = abs32(s); |
217 | 116M | block_peak = local_max(block_peak, s); |
218 | 116M | } |
219 | 61.6k | samples -= n; |
220 | 61.6k | if(AnalyzeSamples(lbuffer, 0, n, 1) != GAIN_ANALYSIS_OK) |
221 | 0 | return false; |
222 | 61.6k | } |
223 | 6.55k | } |
224 | 33.3k | } |
225 | | |
226 | 37.5k | { |
227 | 37.5k | const double peak_scale = (double)(1u << (bps - 1)); |
228 | 37.5k | double peak = (double)block_peak / peak_scale; |
229 | 37.5k | if(peak > title_peak_) |
230 | 2.23k | title_peak_ = peak; |
231 | 37.5k | if(peak > album_peak_) |
232 | 1.65k | album_peak_ = peak; |
233 | 37.5k | } |
234 | | |
235 | 37.5k | return true; |
236 | 37.5k | } |
237 | | |
238 | | void grabbag__replaygain_get_album(float *gain, float *peak) |
239 | 921 | { |
240 | 921 | *gain = (float)GetAlbumGain(); |
241 | 921 | *peak = (float)album_peak_; |
242 | 921 | album_peak_ = 0.0; |
243 | 921 | } |
244 | | |
245 | | void grabbag__replaygain_get_title(float *gain, float *peak) |
246 | 2.16k | { |
247 | 2.16k | *gain = (float)GetTitleGain(); |
248 | 2.16k | *peak = (float)title_peak_; |
249 | 2.16k | title_peak_ = 0.0; |
250 | 2.16k | } |
251 | | |
252 | | |
253 | | typedef struct { |
254 | | uint32_t channels; |
255 | | uint32_t bits_per_sample; |
256 | | uint32_t sample_rate; |
257 | | FLAC__bool error; |
258 | | } DecoderInstance; |
259 | | |
260 | | static FLAC__StreamDecoderWriteStatus write_callback_(const FLAC__StreamDecoder *decoder, const FLAC__Frame *frame, const FLAC__int32 * const buffer[], void *client_data) |
261 | 29.5k | { |
262 | 29.5k | DecoderInstance *instance = (DecoderInstance*)client_data; |
263 | 29.5k | const uint32_t bits_per_sample = frame->header.bits_per_sample; |
264 | 29.5k | const uint32_t channels = frame->header.channels; |
265 | 29.5k | const uint32_t sample_rate = frame->header.sample_rate; |
266 | 29.5k | const uint32_t samples = frame->header.blocksize; |
267 | | |
268 | 29.5k | (void)decoder; |
269 | | |
270 | 29.5k | if( |
271 | 29.5k | !instance->error && |
272 | 29.5k | (channels == 2 || channels == 1) && |
273 | 29.5k | bits_per_sample == instance->bits_per_sample && |
274 | 29.5k | channels == instance->channels && |
275 | 29.5k | sample_rate == instance->sample_rate |
276 | 29.5k | ) { |
277 | 28.9k | instance->error = !grabbag__replaygain_analyze(buffer, channels==2, bits_per_sample, samples); |
278 | 28.9k | } |
279 | 583 | else { |
280 | 583 | instance->error = true; |
281 | 583 | } |
282 | | |
283 | 29.5k | if(!instance->error) |
284 | 28.9k | return FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE; |
285 | 583 | else |
286 | 583 | return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT; |
287 | 29.5k | } |
288 | | |
289 | | static void metadata_callback_(const FLAC__StreamDecoder *decoder, const FLAC__StreamMetadata *metadata, void *client_data) |
290 | 12.4k | { |
291 | 12.4k | DecoderInstance *instance = (DecoderInstance*)client_data; |
292 | | |
293 | 12.4k | (void)decoder; |
294 | | |
295 | 12.4k | if(metadata->type == FLAC__METADATA_TYPE_STREAMINFO) { |
296 | 12.4k | instance->bits_per_sample = metadata->data.stream_info.bits_per_sample; |
297 | 12.4k | instance->channels = metadata->data.stream_info.channels; |
298 | 12.4k | instance->sample_rate = metadata->data.stream_info.sample_rate; |
299 | | |
300 | 12.4k | if(instance->channels != 1 && instance->channels != 2) { |
301 | 54 | instance->error = true; |
302 | 54 | return; |
303 | 54 | } |
304 | | |
305 | 12.3k | if(!grabbag__replaygain_is_valid_sample_frequency(instance->sample_rate)) { |
306 | 91 | instance->error = true; |
307 | 91 | return; |
308 | 91 | } |
309 | 12.3k | } |
310 | 12.4k | } |
311 | | |
312 | | static void error_callback_(const FLAC__StreamDecoder *decoder, FLAC__StreamDecoderErrorStatus status, void *client_data) |
313 | 154k | { |
314 | 154k | DecoderInstance *instance = (DecoderInstance*)client_data; |
315 | | |
316 | 154k | (void)decoder, (void)status; |
317 | | |
318 | 154k | instance->error = true; |
319 | 154k | } |
320 | | |
321 | | const char *grabbag__replaygain_analyze_file(const char *filename, float *title_gain, float *title_peak) |
322 | 11.9k | { |
323 | 11.9k | DecoderInstance instance; |
324 | 11.9k | FLAC__StreamDecoder *decoder = FLAC__stream_decoder_new(); |
325 | | |
326 | 11.9k | if(0 == decoder) |
327 | 0 | return "memory allocation error"; |
328 | | |
329 | 11.9k | instance.error = false; |
330 | | |
331 | | /* It does these three by default but lets be explicit: */ |
332 | 11.9k | FLAC__stream_decoder_set_md5_checking(decoder, false); |
333 | 11.9k | FLAC__stream_decoder_set_metadata_ignore_all(decoder); |
334 | 11.9k | FLAC__stream_decoder_set_metadata_respond(decoder, FLAC__METADATA_TYPE_STREAMINFO); |
335 | | |
336 | 11.9k | if(FLAC__stream_decoder_init_file(decoder, filename, write_callback_, metadata_callback_, error_callback_, &instance) != FLAC__STREAM_DECODER_INIT_STATUS_OK) { |
337 | 0 | FLAC__stream_decoder_delete(decoder); |
338 | 0 | return "initializing decoder"; |
339 | 0 | } |
340 | | |
341 | 11.9k | if(!FLAC__stream_decoder_process_until_end_of_stream(decoder) || instance.error) { |
342 | 10.2k | FLAC__stream_decoder_delete(decoder); |
343 | 10.2k | return "decoding file"; |
344 | 10.2k | } |
345 | | |
346 | 1.79k | FLAC__stream_decoder_delete(decoder); |
347 | | |
348 | 1.79k | grabbag__replaygain_get_title(title_gain, title_peak); |
349 | | |
350 | 1.79k | return 0; |
351 | 11.9k | } |
352 | | |
353 | | const char *grabbag__replaygain_store_to_vorbiscomment(FLAC__StreamMetadata *block, float album_gain, float album_peak, float title_gain, float title_peak) |
354 | 22 | { |
355 | 22 | const char *error; |
356 | | |
357 | 22 | if(0 != (error = grabbag__replaygain_store_to_vorbiscomment_reference(block))) |
358 | 0 | return error; |
359 | | |
360 | 22 | if(0 != (error = grabbag__replaygain_store_to_vorbiscomment_title(block, title_gain, title_peak))) |
361 | 0 | return error; |
362 | | |
363 | 22 | if(0 != (error = grabbag__replaygain_store_to_vorbiscomment_album(block, album_gain, album_peak))) |
364 | 0 | return error; |
365 | | |
366 | 22 | return 0; |
367 | 22 | } |
368 | | |
369 | | const char *grabbag__replaygain_store_to_vorbiscomment_reference(FLAC__StreamMetadata *block) |
370 | 22 | { |
371 | 22 | FLAC__ASSERT(0 != block); |
372 | 22 | FLAC__ASSERT(block->type == FLAC__METADATA_TYPE_VORBIS_COMMENT); |
373 | | |
374 | 22 | if(FLAC__metadata_object_vorbiscomment_remove_entries_matching(block, (const char *)GRABBAG__REPLAYGAIN_TAG_REFERENCE_LOUDNESS) < 0) |
375 | 0 | return "memory allocation error"; |
376 | | |
377 | 22 | if(!append_tag_(block, reference_format_, GRABBAG__REPLAYGAIN_TAG_REFERENCE_LOUDNESS, ReplayGainReferenceLoudness)) |
378 | 0 | return "memory allocation error"; |
379 | | |
380 | 22 | return 0; |
381 | 22 | } |
382 | | |
383 | | const char *grabbag__replaygain_store_to_vorbiscomment_album(FLAC__StreamMetadata *block, float album_gain, float album_peak) |
384 | 22 | { |
385 | 22 | FLAC__ASSERT(0 != block); |
386 | 22 | FLAC__ASSERT(block->type == FLAC__METADATA_TYPE_VORBIS_COMMENT); |
387 | | |
388 | 22 | if( |
389 | 22 | FLAC__metadata_object_vorbiscomment_remove_entries_matching(block, (const char *)GRABBAG__REPLAYGAIN_TAG_ALBUM_GAIN) < 0 || |
390 | 22 | FLAC__metadata_object_vorbiscomment_remove_entries_matching(block, (const char *)GRABBAG__REPLAYGAIN_TAG_ALBUM_PEAK) < 0 |
391 | 22 | ) |
392 | 0 | return "memory allocation error"; |
393 | | |
394 | 22 | if( |
395 | 22 | !append_tag_(block, gain_format_, GRABBAG__REPLAYGAIN_TAG_ALBUM_GAIN, album_gain) || |
396 | 22 | !append_tag_(block, peak_format_, GRABBAG__REPLAYGAIN_TAG_ALBUM_PEAK, album_peak) |
397 | 22 | ) |
398 | 0 | return "memory allocation error"; |
399 | | |
400 | 22 | return 0; |
401 | 22 | } |
402 | | |
403 | | const char *grabbag__replaygain_store_to_vorbiscomment_title(FLAC__StreamMetadata *block, float title_gain, float title_peak) |
404 | 22 | { |
405 | 22 | FLAC__ASSERT(0 != block); |
406 | 22 | FLAC__ASSERT(block->type == FLAC__METADATA_TYPE_VORBIS_COMMENT); |
407 | | |
408 | 22 | if( |
409 | 22 | FLAC__metadata_object_vorbiscomment_remove_entries_matching(block, (const char *)GRABBAG__REPLAYGAIN_TAG_TITLE_GAIN) < 0 || |
410 | 22 | FLAC__metadata_object_vorbiscomment_remove_entries_matching(block, (const char *)GRABBAG__REPLAYGAIN_TAG_TITLE_PEAK) < 0 |
411 | 22 | ) |
412 | 0 | return "memory allocation error"; |
413 | | |
414 | 22 | if( |
415 | 22 | !append_tag_(block, gain_format_, GRABBAG__REPLAYGAIN_TAG_TITLE_GAIN, title_gain) || |
416 | 22 | !append_tag_(block, peak_format_, GRABBAG__REPLAYGAIN_TAG_TITLE_PEAK, title_peak) |
417 | 22 | ) |
418 | 0 | return "memory allocation error"; |
419 | | |
420 | 22 | return 0; |
421 | 22 | } |
422 | | |
423 | | static const char *store_to_file_pre_(const char *filename, FLAC__Metadata_Chain **chain, FLAC__StreamMetadata **block) |
424 | 392 | { |
425 | 392 | FLAC__Metadata_Iterator *iterator; |
426 | 392 | const char *error; |
427 | 392 | FLAC__bool found_vc_block = false; |
428 | | |
429 | 392 | if(0 == (*chain = FLAC__metadata_chain_new())) |
430 | 0 | return "memory allocation error"; |
431 | | |
432 | 392 | if(!FLAC__metadata_chain_read(*chain, filename)) { |
433 | 370 | error = FLAC__Metadata_ChainStatusString[FLAC__metadata_chain_status(*chain)]; |
434 | 370 | FLAC__metadata_chain_delete(*chain); |
435 | 370 | return error; |
436 | 370 | } |
437 | | |
438 | 22 | if(0 == (iterator = FLAC__metadata_iterator_new())) { |
439 | 0 | FLAC__metadata_chain_delete(*chain); |
440 | 0 | return "memory allocation error"; |
441 | 0 | } |
442 | | |
443 | 22 | FLAC__metadata_iterator_init(iterator, *chain); |
444 | | |
445 | 42 | do { |
446 | 42 | *block = FLAC__metadata_iterator_get_block(iterator); |
447 | 42 | if((*block)->type == FLAC__METADATA_TYPE_VORBIS_COMMENT) |
448 | 15 | found_vc_block = true; |
449 | 42 | } while(!found_vc_block && FLAC__metadata_iterator_next(iterator)); |
450 | | |
451 | 22 | if(!found_vc_block) { |
452 | | /* create a new block */ |
453 | 7 | *block = FLAC__metadata_object_new(FLAC__METADATA_TYPE_VORBIS_COMMENT); |
454 | 7 | if(0 == *block) { |
455 | 0 | FLAC__metadata_chain_delete(*chain); |
456 | 0 | FLAC__metadata_iterator_delete(iterator); |
457 | 0 | return "memory allocation error"; |
458 | 0 | } |
459 | 7 | while(FLAC__metadata_iterator_next(iterator)) |
460 | 0 | ; |
461 | 7 | if(!FLAC__metadata_iterator_insert_block_after(iterator, *block)) { |
462 | 0 | error = FLAC__Metadata_ChainStatusString[FLAC__metadata_chain_status(*chain)]; |
463 | 0 | FLAC__metadata_chain_delete(*chain); |
464 | 0 | FLAC__metadata_iterator_delete(iterator); |
465 | 0 | return error; |
466 | 0 | } |
467 | | /* iterator is left pointing to new block */ |
468 | 7 | FLAC__ASSERT(FLAC__metadata_iterator_get_block(iterator) == *block); |
469 | 7 | } |
470 | | |
471 | 22 | FLAC__metadata_iterator_delete(iterator); |
472 | | |
473 | 22 | FLAC__ASSERT(0 != *block); |
474 | 22 | FLAC__ASSERT((*block)->type == FLAC__METADATA_TYPE_VORBIS_COMMENT); |
475 | | |
476 | 22 | return 0; |
477 | 22 | } |
478 | | |
479 | | static const char *store_to_file_post_(const char *filename, FLAC__Metadata_Chain *chain, FLAC__bool preserve_modtime) |
480 | 22 | { |
481 | 22 | struct flac_stat_s stats; |
482 | 22 | const FLAC__bool have_stats = get_file_stats_(filename, &stats); |
483 | | |
484 | 22 | (void)grabbag__file_change_stats(filename, /*read_only=*/false); |
485 | | |
486 | 22 | FLAC__metadata_chain_sort_padding(chain); |
487 | 22 | if(!FLAC__metadata_chain_write(chain, /*use_padding=*/true, preserve_modtime)) { |
488 | 0 | const char *error; |
489 | 0 | error = FLAC__Metadata_ChainStatusString[FLAC__metadata_chain_status(chain)]; |
490 | 0 | FLAC__metadata_chain_delete(chain); |
491 | 0 | return error; |
492 | 0 | } |
493 | | |
494 | 22 | FLAC__metadata_chain_delete(chain); |
495 | | |
496 | 22 | if(have_stats) |
497 | 22 | set_file_stats_(filename, &stats); |
498 | | |
499 | 22 | return 0; |
500 | 22 | } |
501 | | |
502 | | 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) |
503 | 22 | { |
504 | 22 | FLAC__Metadata_Chain *chain; |
505 | 22 | FLAC__StreamMetadata *block = NULL; |
506 | 22 | const char *error; |
507 | | |
508 | 22 | if(0 != (error = store_to_file_pre_(filename, &chain, &block))) |
509 | 0 | return error; |
510 | | |
511 | 22 | if(0 != (error = grabbag__replaygain_store_to_vorbiscomment(block, album_gain, album_peak, title_gain, title_peak))) { |
512 | 0 | FLAC__metadata_chain_delete(chain); |
513 | 0 | return error; |
514 | 0 | } |
515 | | |
516 | 22 | if(0 != (error = store_to_file_post_(filename, chain, preserve_modtime))) |
517 | 0 | return error; |
518 | | |
519 | 22 | return 0; |
520 | 22 | } |
521 | | |
522 | | const char *grabbag__replaygain_store_to_file_reference(const char *filename, FLAC__bool preserve_modtime) |
523 | 370 | { |
524 | 370 | FLAC__Metadata_Chain *chain; |
525 | 370 | FLAC__StreamMetadata *block = NULL; |
526 | 370 | const char *error; |
527 | | |
528 | 370 | if(0 != (error = store_to_file_pre_(filename, &chain, &block))) |
529 | 370 | return error; |
530 | | |
531 | 0 | if(0 != (error = grabbag__replaygain_store_to_vorbiscomment_reference(block))) { |
532 | 0 | FLAC__metadata_chain_delete(chain); |
533 | 0 | return error; |
534 | 0 | } |
535 | | |
536 | 0 | if(0 != (error = store_to_file_post_(filename, chain, preserve_modtime))) |
537 | 0 | return error; |
538 | | |
539 | 0 | return 0; |
540 | 0 | } |
541 | | |
542 | | const char *grabbag__replaygain_store_to_file_album(const char *filename, float album_gain, float album_peak, FLAC__bool preserve_modtime) |
543 | 0 | { |
544 | 0 | FLAC__Metadata_Chain *chain; |
545 | 0 | FLAC__StreamMetadata *block = NULL; |
546 | 0 | const char *error; |
547 | |
|
548 | 0 | if(0 != (error = store_to_file_pre_(filename, &chain, &block))) |
549 | 0 | return error; |
550 | | |
551 | 0 | if(0 != (error = grabbag__replaygain_store_to_vorbiscomment_album(block, album_gain, album_peak))) { |
552 | 0 | FLAC__metadata_chain_delete(chain); |
553 | 0 | return error; |
554 | 0 | } |
555 | | |
556 | 0 | if(0 != (error = store_to_file_post_(filename, chain, preserve_modtime))) |
557 | 0 | return error; |
558 | | |
559 | 0 | return 0; |
560 | 0 | } |
561 | | |
562 | | const char *grabbag__replaygain_store_to_file_title(const char *filename, float title_gain, float title_peak, FLAC__bool preserve_modtime) |
563 | 0 | { |
564 | 0 | FLAC__Metadata_Chain *chain; |
565 | 0 | FLAC__StreamMetadata *block = NULL; |
566 | 0 | const char *error; |
567 | |
|
568 | 0 | if(0 != (error = store_to_file_pre_(filename, &chain, &block))) |
569 | 0 | return error; |
570 | | |
571 | 0 | if(0 != (error = grabbag__replaygain_store_to_vorbiscomment_title(block, title_gain, title_peak))) { |
572 | 0 | FLAC__metadata_chain_delete(chain); |
573 | 0 | return error; |
574 | 0 | } |
575 | | |
576 | 0 | if(0 != (error = store_to_file_post_(filename, chain, preserve_modtime))) |
577 | 0 | return error; |
578 | | |
579 | 0 | return 0; |
580 | 0 | } |
581 | | |
582 | | static FLAC__bool parse_double_(const FLAC__StreamMetadata_VorbisComment_Entry *entry, double *val) |
583 | 0 | { |
584 | 0 | char s[32], *end; |
585 | 0 | const char *p, *q; |
586 | 0 | double v; |
587 | |
|
588 | 0 | FLAC__ASSERT(0 != entry); |
589 | 0 | FLAC__ASSERT(0 != val); |
590 | |
|
591 | 0 | p = (const char *)entry->entry; |
592 | 0 | q = strchr(p, '='); |
593 | 0 | if(0 == q) |
594 | 0 | return false; |
595 | 0 | q++; |
596 | 0 | safe_strncpy(s, q, local_min(sizeof(s), (size_t) (entry->length - (q-p)))); |
597 | |
|
598 | 0 | v = strtod(s, &end); |
599 | 0 | if(end == s) |
600 | 0 | return false; |
601 | | |
602 | 0 | *val = v; |
603 | 0 | return true; |
604 | 0 | } |
605 | | |
606 | | FLAC__bool grabbag__replaygain_load_from_vorbiscomment(const FLAC__StreamMetadata *block, FLAC__bool album_mode, FLAC__bool strict, double *reference, double *gain, double *peak) |
607 | 780 | { |
608 | 780 | int reference_offset, gain_offset, peak_offset; |
609 | 780 | char *saved_locale; |
610 | 780 | FLAC__bool res = true; |
611 | | |
612 | 780 | FLAC__ASSERT(0 != block); |
613 | 780 | FLAC__ASSERT(0 != reference); |
614 | 780 | FLAC__ASSERT(0 != gain); |
615 | 780 | FLAC__ASSERT(0 != peak); |
616 | 780 | FLAC__ASSERT(block->type == FLAC__METADATA_TYPE_VORBIS_COMMENT); |
617 | | |
618 | | /* Default to current level until overridden by a detected tag; this |
619 | | * will always be true until we change replaygain_analysis.c |
620 | | */ |
621 | 780 | *reference = ReplayGainReferenceLoudness; |
622 | | |
623 | | /* |
624 | | * We need to save the old locale and switch to "C" because the locale |
625 | | * influences the behaviour of strtod and we want it a certain way. |
626 | | */ |
627 | 780 | saved_locale = strdup(setlocale(LC_ALL, 0)); |
628 | 780 | if (0 == saved_locale) |
629 | 0 | return false; |
630 | 780 | setlocale(LC_ALL, "C"); |
631 | | |
632 | 780 | if(0 <= (reference_offset = FLAC__metadata_object_vorbiscomment_find_entry_from(block, /*offset=*/0, (const char *)GRABBAG__REPLAYGAIN_TAG_REFERENCE_LOUDNESS))) |
633 | 0 | (void)parse_double_(block->data.vorbis_comment.comments + reference_offset, reference); |
634 | | |
635 | 780 | 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)))) |
636 | 780 | res = false; |
637 | 780 | 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)))) |
638 | 780 | res = false; |
639 | | |
640 | 780 | if(res && !parse_double_(block->data.vorbis_comment.comments + gain_offset, gain)) |
641 | 0 | res = false; |
642 | 780 | if(res && !parse_double_(block->data.vorbis_comment.comments + peak_offset, peak)) |
643 | 0 | res = false; |
644 | 780 | if(res && *peak < 0.0) |
645 | 0 | res = false; |
646 | | |
647 | 780 | setlocale(LC_ALL, saved_locale); |
648 | 780 | free(saved_locale); |
649 | | |
650 | | /* something failed; retry with strict */ |
651 | 780 | if (!res && !strict) |
652 | 390 | res = grabbag__replaygain_load_from_vorbiscomment(block, !album_mode, /*strict=*/true, reference, gain, peak); |
653 | | |
654 | 780 | return res; |
655 | 780 | } |
656 | | |
657 | | double grabbag__replaygain_compute_scale_factor(double peak, double gain, double preamp, FLAC__bool prevent_clipping) |
658 | 0 | { |
659 | 0 | double scale; |
660 | 0 | FLAC__ASSERT(peak >= 0.0); |
661 | 0 | gain += preamp; |
662 | 0 | scale = (float) pow(10.0, gain * 0.05); |
663 | 0 | if(prevent_clipping && peak > 0.0) { |
664 | 0 | const double max_scale = (float)(1.0 / peak); |
665 | 0 | if(scale > max_scale) |
666 | 0 | scale = max_scale; |
667 | 0 | } |
668 | 0 | return scale; |
669 | 0 | } |