don't crash if taglib cannot open file
[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/flacfile.h>
29 #include <taglib/oggfile.h>
30 #include <taglib/tag.h>
31 #include <taglib/taglib.h>
32 #include <taglib/xiphcomment.h>
33
34 /* Convert string to TagLib::String */
35 #define TL_STR(string) TagLib::String ((string).c_str(), TagLib::String::UTF8)
36
37 using namespace PBD;
38
39 namespace ARDOUR
40 {
41
42 bool
43 AudiofileTagger::tag_file (std::string const & filename, SessionMetadata const & metadata)
44 {
45         TagLib::FileRef file (filename.c_str());
46         if (file.isNull()) {
47                 std::cerr << "TagLib::FileRef is null for file" << filename  << std::endl;
48                 return true; // continue anyway?!
49         }
50
51         if (!file.tag()) {
52                 std::cerr << "TagLib::Tag is null for file" << filename  << std::endl;
53                 return true; // continue anyway?!
54         }
55
56         TagLib::Tag & tag (*file.tag());
57
58         tag_generic (tag, metadata);
59
60         /* FLAC */
61
62         TagLib::FLAC::File * flac_file;
63         if ((flac_file = dynamic_cast<TagLib::FLAC::File *> (file.file()))) {
64                 TagLib::Ogg::XiphComment * vorbis_tag;
65                 if ((vorbis_tag = dynamic_cast<TagLib::Ogg::XiphComment *> (flac_file->xiphComment (true)))) {
66                         tag_vorbis_comment (*vorbis_tag, metadata);
67                 } else {
68                         std::cerr << "Could not get Xiph comment for FLAC file!" << std::endl;
69                 }
70         }
71
72         /* Ogg */
73
74         TagLib::Ogg::File * ogg_file;
75         if ((ogg_file = dynamic_cast<TagLib::Ogg::File *> (file.file()))) {
76                 TagLib::Ogg::XiphComment * vorbis_tag;
77                 if ((vorbis_tag = dynamic_cast<TagLib::Ogg::XiphComment *> (ogg_file->tag()))) {
78                         tag_vorbis_comment (*vorbis_tag, metadata);
79                 } else {
80                         std::cerr << "Could not get Xiph comment for Ogg file!" << std::endl;
81                 }
82         }
83
84         file.save();
85         return true;
86 }
87
88 bool
89 AudiofileTagger::tag_generic (TagLib::Tag & tag, SessionMetadata const & metadata)
90 {
91         tag.setTitle (TL_STR(metadata.title()));
92         tag.setArtist (TL_STR(metadata.artist()));
93         tag.setAlbum (TL_STR(metadata.album()));
94         tag.setComment (TL_STR(metadata.comment()));
95         tag.setGenre (TL_STR(metadata.genre()));
96         tag.setYear (metadata.year());
97         tag.setTrack (metadata.track_number());
98
99         return true;
100 }
101
102 bool
103 AudiofileTagger::tag_vorbis_comment (TagLib::Ogg::XiphComment & tag, SessionMetadata const & metadata)
104 {
105         tag.addField ("COPYRIGHT", TL_STR(metadata.copyright()));
106         tag.addField ("ISRC", TL_STR(metadata.isrc()));
107         tag.addField ("GROUPING ", TL_STR(metadata.grouping()));
108         tag.addField ("SUBTITLE", TL_STR(metadata.subtitle()));
109         tag.addField ("ALBUMARTIST", TL_STR(metadata.album_artist()));
110         tag.addField ("LYRICIST", TL_STR(metadata.lyricist()));
111         tag.addField ("COMPOSER", TL_STR(metadata.composer()));
112         tag.addField ("CONDUCTOR", TL_STR(metadata.conductor()));
113         tag.addField ("REMIXER", TL_STR(metadata.remixer()));
114         tag.addField ("ARRANGER", TL_STR(metadata.arranger()));
115         tag.addField ("ENGINEER", TL_STR(metadata.engineer()));
116         tag.addField ("PRODUCER", TL_STR(metadata.producer()));
117         tag.addField ("DJMIXER", TL_STR(metadata.dj_mixer()));
118         tag.addField ("MIXER", TL_STR(metadata.mixer()));
119         tag.addField ("COMPILATION", TL_STR(metadata.compilation()));
120         tag.addField ("DISCSUBTITLE", TL_STR(metadata.disc_subtitle()));
121         tag.addField ("DISCNUMBER", to_string (metadata.disc_number(), std::dec));
122
123         // No field for total discs or tracks
124
125         return true;
126 }
127
128
129 } // namespace ARDOUR
130