minor comments
[flac.git] / src / plugin_common / replaygain.c
1 /* plugin_common - Routines common to several plugins
2  * Copyright (C) 2002,2003,2004,2005,2006  Josh Coalson
3  * Copyright (C) 2003  Philip J├Ągenstedt
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License
7  * as published by the Free Software Foundation; either version 2
8  * of the License, or (at your option) any later version.
9  *
10  * This program 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
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
18  */
19
20 #if HAVE_CONFIG_H
21 #  include <config.h>
22 #endif
23
24 #include "replaygain.h"
25 #include "FLAC/ordinals.h"
26 #include "FLAC/metadata.h"
27 #include "share/grabbag.h"
28
29 void FLAC_plugin__replaygain_get_from_file(const char *filename,
30                                            double *reference, FLAC__bool *reference_set,
31                                            double *track_gain, FLAC__bool *track_gain_set,
32                                            double *album_gain, FLAC__bool *album_gain_set,
33                                            double *track_peak, FLAC__bool *track_peak_set,
34                                            double *album_peak, FLAC__bool *album_peak_set)
35 {
36         FLAC__Metadata_SimpleIterator *iterator = FLAC__metadata_simple_iterator_new();
37
38         *track_gain_set = *album_gain_set = *track_peak_set = *album_peak_set = false;
39
40         if(0 != iterator) {
41                 if(FLAC__metadata_simple_iterator_init(iterator, filename, /*read_only=*/true, /*preserve_file_stats=*/true)) {
42                         FLAC__bool got_vorbis_comments = false;
43                         do {
44                                 if(FLAC__metadata_simple_iterator_get_block_type(iterator) == FLAC__METADATA_TYPE_VORBIS_COMMENT) {
45                                         FLAC__StreamMetadata *block = FLAC__metadata_simple_iterator_get_block(iterator);
46                                         if(0 != block) {
47                                                 if(grabbag__replaygain_load_from_vorbiscomment(block, /*album_mode=*/false, /*strict=*/true, reference, track_gain, track_peak)) {
48                                                         *reference_set = *track_gain_set = *track_peak_set = true;
49                                                 }
50                                                 if(grabbag__replaygain_load_from_vorbiscomment(block, /*album_mode=*/true, /*strict=*/true, reference, album_gain, album_peak)) {
51                                                         *reference_set = *album_gain_set = *album_peak_set = true;
52                                                 }
53                                                 FLAC__metadata_object_delete(block);
54                                                 got_vorbis_comments = true;
55                                         }
56                                 }
57                         } while (!got_vorbis_comments && FLAC__metadata_simple_iterator_next(iterator));
58                 }
59                 FLAC__metadata_simple_iterator_delete(iterator);
60         }
61         return;
62 }