add new debug bit for backend callbacks
[ardour.git] / libs / ardour / audiofile_tagger.cc
1 /*
2  * Copyright (C) 2008-2010 Paul Davis <paul@linuxaudiosystems.com>
3  * Copyright (C) 2009 David Robillard <d@drobilla.net>
4  * Copyright (C) 2013-2019 Robin Gareus <robin@gareus.org>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (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 #include "ardour/audiofile_tagger.h"
22
23 #include "ardour/session_metadata.h"
24
25 #include "pbd/string_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         /* see also SessionMetadata::av_export_tag () for ffmpeg/liblame */
46
47         TagLib::FileRef file (filename.c_str());
48         if (file.isNull()) {
49                 std::cerr << "TagLib::FileRef is null for file" << filename  << std::endl;
50                 return true; // continue anyway?!
51         }
52
53         if (!file.tag()) {
54                 std::cerr << "TagLib::Tag is null for file" << filename  << std::endl;
55                 return true; // continue anyway?!
56         }
57
58         TagLib::Tag & tag (*file.tag());
59
60         tag_generic (tag, metadata);
61
62         /* FLAC */
63
64         TagLib::FLAC::File * flac_file;
65         if ((flac_file = dynamic_cast<TagLib::FLAC::File *> (file.file()))) {
66                 TagLib::Ogg::XiphComment * vorbis_tag;
67                 if ((vorbis_tag = dynamic_cast<TagLib::Ogg::XiphComment *> (flac_file->xiphComment (true)))) {
68                         tag_vorbis_comment (*vorbis_tag, metadata);
69                 } else {
70                         std::cerr << "Could not get Xiph comment for FLAC file!" << std::endl;
71                 }
72         }
73
74         /* Ogg */
75
76         TagLib::Ogg::File * ogg_file;
77         if ((ogg_file = dynamic_cast<TagLib::Ogg::File *> (file.file()))) {
78                 TagLib::Ogg::XiphComment * vorbis_tag;
79                 if ((vorbis_tag = dynamic_cast<TagLib::Ogg::XiphComment *> (ogg_file->tag()))) {
80                         tag_vorbis_comment (*vorbis_tag, metadata);
81                 } else {
82                         std::cerr << "Could not get Xiph comment for Ogg file!" << std::endl;
83                 }
84         }
85
86         file.save();
87         return true;
88 }
89
90 bool
91 AudiofileTagger::tag_generic (TagLib::Tag & tag, SessionMetadata const & metadata)
92 {
93         tag.setTitle (TL_STR(metadata.title()));
94         tag.setArtist (TL_STR(metadata.artist()));
95         tag.setAlbum (TL_STR(metadata.album()));
96         tag.setComment (TL_STR(metadata.comment()));
97         tag.setGenre (TL_STR(metadata.genre()));
98         tag.setYear (metadata.year());
99         tag.setTrack (metadata.track_number());
100
101         return true;
102 }
103
104 bool
105 AudiofileTagger::tag_vorbis_comment (TagLib::Ogg::XiphComment & tag, SessionMetadata const & metadata)
106 {
107         tag.addField ("COPYRIGHT", TL_STR(metadata.copyright()));
108         tag.addField ("ISRC", TL_STR(metadata.isrc()));
109         tag.addField ("GROUPING ", TL_STR(metadata.grouping()));
110         tag.addField ("SUBTITLE", TL_STR(metadata.subtitle()));
111         tag.addField ("ALBUMARTIST", TL_STR(metadata.album_artist()));
112         tag.addField ("LYRICIST", TL_STR(metadata.lyricist()));
113         tag.addField ("COMPOSER", TL_STR(metadata.composer()));
114         tag.addField ("CONDUCTOR", TL_STR(metadata.conductor()));
115         tag.addField ("REMIXER", TL_STR(metadata.remixer()));
116         tag.addField ("ARRANGER", TL_STR(metadata.arranger()));
117         tag.addField ("ENGINEER", TL_STR(metadata.engineer()));
118         tag.addField ("PRODUCER", TL_STR(metadata.producer()));
119         tag.addField ("DJMIXER", TL_STR(metadata.dj_mixer()));
120         tag.addField ("MIXER", TL_STR(metadata.mixer()));
121         tag.addField ("COMPILATION", TL_STR(metadata.compilation()));
122         tag.addField ("DISCSUBTITLE", TL_STR(metadata.disc_subtitle()));
123         tag.addField ("DISCNUMBER", to_string (metadata.disc_number()));
124
125         // No field for total discs or tracks
126
127         return true;
128 }
129
130
131 } // namespace ARDOUR
132