src/test_streams/ : Pull out write_simple_wavex_header() for reuse.
[flac.git] / src / plugin_common / replaygain.c
1 /* plugin_common - Routines common to several plugins
2  * Copyright (C) 2002-2009  Josh Coalson
3  * Copyright (C) 2011-2014  Xiph.Org Foundation
4  * Copyright (C) 2003  Philip J├Ągenstedt
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License along
17  * with this program; if not, write to the Free Software Foundation, Inc.,
18  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19  */
20
21 #ifdef HAVE_CONFIG_H
22 #  include <config.h>
23 #endif
24
25 #include "replaygain.h"
26 #include "FLAC/ordinals.h"
27 #include "FLAC/metadata.h"
28 #include "share/grabbag.h"
29
30 FLAC__bool FLAC_plugin__replaygain_get_from_file(const char *filename,
31                                                  double *reference, FLAC__bool *reference_set,
32                                                  double *track_gain, FLAC__bool *track_gain_set,
33                                                  double *album_gain, FLAC__bool *album_gain_set,
34                                                  double *track_peak, FLAC__bool *track_peak_set,
35                                                  double *album_peak, FLAC__bool *album_peak_set)
36 {
37         FLAC__Metadata_SimpleIterator *iterator = FLAC__metadata_simple_iterator_new();
38         FLAC__bool ret = false;
39
40         *track_gain_set = *album_gain_set = *track_peak_set = *album_peak_set = false;
41
42         if(0 != iterator) {
43                 if(FLAC__metadata_simple_iterator_init(iterator, filename, /*read_only=*/true, /*preserve_file_stats=*/true)) {
44                         FLAC__bool got_vorbis_comments = false;
45                         ret = true;
46                         do {
47                                 if(FLAC__metadata_simple_iterator_get_block_type(iterator) == FLAC__METADATA_TYPE_VORBIS_COMMENT) {
48                                         FLAC__StreamMetadata *block = FLAC__metadata_simple_iterator_get_block(iterator);
49                                         if(0 != block) {
50                                                 if(grabbag__replaygain_load_from_vorbiscomment(block, /*album_mode=*/false, /*strict=*/true, reference, track_gain, track_peak)) {
51                                                         *reference_set = *track_gain_set = *track_peak_set = true;
52                                                 }
53                                                 if(grabbag__replaygain_load_from_vorbiscomment(block, /*album_mode=*/true, /*strict=*/true, reference, album_gain, album_peak)) {
54                                                         *reference_set = *album_gain_set = *album_peak_set = true;
55                                                 }
56                                                 FLAC__metadata_object_delete(block);
57                                                 got_vorbis_comments = true;
58                                         }
59                                 }
60                         } while (!got_vorbis_comments && FLAC__metadata_simple_iterator_next(iterator));
61                 }
62                 FLAC__metadata_simple_iterator_delete(iterator);
63         }
64         return ret;
65 }