summaryrefslogtreecommitdiff
path: root/libs/ardour/audiofile_tagger.cc
blob: 0131292680b394e5747cddaf31239165924bbbc6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
/*
    Copyright (C) 2008 Paul Davis
    Author: Sakari Bergen

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

*/

#include "ardour/audiofile_tagger.h"

#include "ardour/session_metadata.h"

#include "pbd/string_convert.h"

#include <taglib/fileref.h>
#include <taglib/flacfile.h>
#include <taglib/oggfile.h>
#include <taglib/tag.h>
#include <taglib/taglib.h>
#include <taglib/xiphcomment.h>

/* Convert string to TagLib::String */
#define TL_STR(string) TagLib::String ((string).c_str(), TagLib::String::UTF8)

using namespace PBD;

namespace ARDOUR
{

bool
AudiofileTagger::tag_file (std::string const & filename, SessionMetadata const & metadata)
{
	TagLib::FileRef file (filename.c_str());
	if (file.isNull()) {
		std::cerr << "TagLib::FileRef is null for file" << filename  << std::endl;
		return true; // continue anyway?!
	}

	if (!file.tag()) {
		std::cerr << "TagLib::Tag is null for file" << filename  << std::endl;
		return true; // continue anyway?!
	}

	TagLib::Tag & tag (*file.tag());

	tag_generic (tag, metadata);

	/* FLAC */

	TagLib::FLAC::File * flac_file;
	if ((flac_file = dynamic_cast<TagLib::FLAC::File *> (file.file()))) {
		TagLib::Ogg::XiphComment * vorbis_tag;
		if ((vorbis_tag = dynamic_cast<TagLib::Ogg::XiphComment *> (flac_file->xiphComment (true)))) {
			tag_vorbis_comment (*vorbis_tag, metadata);
		} else {
			std::cerr << "Could not get Xiph comment for FLAC file!" << std::endl;
		}
	}

	/* Ogg */

	TagLib::Ogg::File * ogg_file;
	if ((ogg_file = dynamic_cast<TagLib::Ogg::File *> (file.file()))) {
		TagLib::Ogg::XiphComment * vorbis_tag;
		if ((vorbis_tag = dynamic_cast<TagLib::Ogg::XiphComment *> (ogg_file->tag()))) {
			tag_vorbis_comment (*vorbis_tag, metadata);
		} else {
			std::cerr << "Could not get Xiph comment for Ogg file!" << std::endl;
		}
	}

	file.save();
	return true;
}

bool
AudiofileTagger::tag_generic (TagLib::Tag & tag, SessionMetadata const & metadata)
{
	tag.setTitle (TL_STR(metadata.title()));
	tag.setArtist (TL_STR(metadata.artist()));
	tag.setAlbum (TL_STR(metadata.album()));
	tag.setComment (TL_STR(metadata.comment()));
	tag.setGenre (TL_STR(metadata.genre()));
	tag.setYear (metadata.year());
	tag.setTrack (metadata.track_number());

	return true;
}

bool
AudiofileTagger::tag_vorbis_comment (TagLib::Ogg::XiphComment & tag, SessionMetadata const & metadata)
{
	tag.addField ("COPYRIGHT", TL_STR(metadata.copyright()));
	tag.addField ("ISRC", TL_STR(metadata.isrc()));
	tag.addField ("GROUPING ", TL_STR(metadata.grouping()));
	tag.addField ("SUBTITLE", TL_STR(metadata.subtitle()));
	tag.addField ("ALBUMARTIST", TL_STR(metadata.album_artist()));
	tag.addField ("LYRICIST", TL_STR(metadata.lyricist()));
	tag.addField ("COMPOSER", TL_STR(metadata.composer()));
	tag.addField ("CONDUCTOR", TL_STR(metadata.conductor()));
	tag.addField ("REMIXER", TL_STR(metadata.remixer()));
	tag.addField ("ARRANGER", TL_STR(metadata.arranger()));
	tag.addField ("ENGINEER", TL_STR(metadata.engineer()));
	tag.addField ("PRODUCER", TL_STR(metadata.producer()));
	tag.addField ("DJMIXER", TL_STR(metadata.dj_mixer()));
	tag.addField ("MIXER", TL_STR(metadata.mixer()));
	tag.addField ("COMPILATION", TL_STR(metadata.compilation()));
	tag.addField ("DISCSUBTITLE", TL_STR(metadata.disc_subtitle()));
	tag.addField ("DISCNUMBER", to_string (metadata.disc_number()));

	// No field for total discs or tracks

	return true;
}


} // namespace ARDOUR