Remove unnecessary 0 checks before delete; see http://www.parashift.com/c++-faq-lite...
[ardour.git] / libs / ardour / audiofile_tagger.cc
1 /*
2     Copyright (C) 2008 Paul Davis
3     Author: Sakari Bergen
4
5     This program is free software; you can redistribute it and/or modify
6     it under the terms of the GNU General Public License as published by
7     the Free Software Foundation; either version 2 of the License, or
8     (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., 675 Mass Ave, Cambridge, MA 02139, USA.
18
19 */
20
21 #include <ardour/audiofile_tagger.h>
22
23 #include <ardour/session_metadata.h>
24
25 #include <pbd/convert.h>
26
27 #include <taglib/fileref.h>
28 #include <taglib/oggfile.h>
29 #include <taglib/flacfile.h>
30
31 /* Convert Glib::ustring to TagLib::String */
32 #define TL_STR(ustring) TagLib::String ((ustring).c_str(), TagLib::String::UTF8)
33
34 using namespace PBD;
35
36 namespace ARDOUR
37 {
38
39 bool
40 AudiofileTagger::tag_file (string const & filename, SessionMetadata const & metadata)
41 {
42         TagLib::FileRef file (filename.c_str());
43         TagLib::Tag & tag (*file.tag());
44         
45         tag_generic (tag, metadata);
46         
47         /* FLAC */
48         
49         TagLib::FLAC::File * flac_file;
50         if ((flac_file = dynamic_cast<TagLib::FLAC::File *> (file.file()))) {
51                 TagLib::Ogg::XiphComment * vorbis_tag;
52                 if ((vorbis_tag = dynamic_cast<TagLib::Ogg::XiphComment *> (flac_file->xiphComment (true)))) {
53                         tag_vorbis_comment (*vorbis_tag, metadata);
54                 } else {
55                         std::cerr << "Could not get Xiph comment for FLAC file!" << std::endl;
56                 }
57         }
58         
59         /* Ogg */
60         
61         TagLib::Ogg::File * ogg_file;
62         if ((ogg_file = dynamic_cast<TagLib::Ogg::File *> (file.file()))) {
63                 TagLib::Ogg::XiphComment * vorbis_tag;
64                 if ((vorbis_tag = dynamic_cast<TagLib::Ogg::XiphComment *> (ogg_file->tag()))) {
65                         tag_vorbis_comment (*vorbis_tag, metadata);
66                 } else {
67                         std::cerr << "Could not get Xiph comment for Ogg file!" << std::endl;
68                 }
69         }
70         
71         file.save();
72         return true;
73 }
74
75 bool
76 AudiofileTagger::tag_generic (TagLib::Tag & tag, SessionMetadata const & metadata)
77 {
78         tag.setTitle (TL_STR(metadata.title()));
79         tag.setArtist (TL_STR(metadata.artist()));
80         tag.setAlbum (TL_STR(metadata.album()));
81         tag.setComment (TL_STR(metadata.comment()));
82         tag.setGenre (TL_STR(metadata.genre()));
83         tag.setYear (metadata.year());
84         tag.setTrack (metadata.track_number());
85         
86         return true;
87 }
88
89 bool
90 AudiofileTagger::tag_vorbis_comment (TagLib::Ogg::XiphComment & tag, SessionMetadata const & metadata)
91 {
92         tag.addField ("COPYRIGHT", TL_STR(metadata.copyright()));
93         tag.addField ("ISRC", TL_STR(metadata.isrc()));
94         tag.addField ("GROUPING ", TL_STR(metadata.grouping()));
95         tag.addField ("SUBTITLE", TL_STR(metadata.subtitle()));
96         tag.addField ("ALBUMARTIST", TL_STR(metadata.album_artist()));
97         tag.addField ("LYRICIST", TL_STR(metadata.lyricist()));
98         tag.addField ("COMPOSER", TL_STR(metadata.composer()));
99         tag.addField ("CONDUCTOR", TL_STR(metadata.conductor()));
100         tag.addField ("REMIXER", TL_STR(metadata.remixer()));
101         tag.addField ("ARRANGER", TL_STR(metadata.arranger()));
102         tag.addField ("ENGINEER", TL_STR(metadata.engineer()));
103         tag.addField ("PRODUCER", TL_STR(metadata.producer()));
104         tag.addField ("DJMIXER", TL_STR(metadata.dj_mixer()));
105         tag.addField ("MIXER", TL_STR(metadata.mixer()));
106         tag.addField ("COMPILATION", TL_STR(metadata.compilation()));
107         tag.addField ("DISCSUBTITLE", TL_STR(metadata.disc_subtitle()));
108         tag.addField ("DISCNUMBER", to_string (metadata.disc_number(), std::dec));
109         
110         // No field for total discs or tracks
111
112         return true;
113 }
114
115
116 } // namespace ARDOUR
117