summaryrefslogtreecommitdiffstats
path: root/src/audiotagger.cpp
blob: 4ea0627a279feb3b163a658558142e27604c2929 (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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
#include "audiotagger.h"

#include <QtDebug>
#include <taglib/tag.h>
#include <taglib/id3v2frame.h>

#include <taglib/id3v2frame.h>
#include <taglib/id3v2header.h>
#include <taglib/id3v1tag.h>
#include <taglib/tmap.h>
#include <taglib/tstringlist.h>

#include <taglib/vorbisfile.h>
#include <taglib/wavpackfile.h>
#include <taglib/mpegfile.h>
#include <taglib/mp4file.h>
#include <taglib/oggfile.h>
#include <taglib/flacfile.h>
#include <taglib/aifffile.h>
#include <taglib/rifffile.h>
#include <taglib/wavfile.h>
#include <taglib/textidentificationframe.h>

#ifdef _WIN32
static_assert(sizeof(wchar_t) == sizeof(QChar), "wchar_t is not the same size than QChar");
#define TAGLIB_FILENAME_FROM_QSTRING(fileName) (const wchar_t*)fileName.utf16()
// Note: we cannot use QString::toStdWString since QT 4 is compiled with
// '/Zc:wchar_t-' flag and QT 5 not
#else
#define TAGLIB_FILENAME_FROM_QSTRING(fileName) (fileName).toLocal8Bit().constData()
#endif // _WIN32

AudioTagger::AudioTagger(const QString& file, SecurityTokenPointer pToken)
        : m_file(file),
          m_pSecurityToken(pToken.isNull() ? Sandbox::openSecurityToken(
                  m_file, true) : pToken) {
}

AudioTagger::~AudioTagger() {
}

void AudioTagger::setArtist(QString artist) {
    m_artist = artist;
}

void AudioTagger::setTitle(QString title) {
    m_title = title;
}

void AudioTagger::setAlbum(QString album) {
    m_album = album;
}

void AudioTagger::setAlbumArtist (QString albumArtist) {
    m_albumArtist = albumArtist;
}

void AudioTagger::setGenre (QString genre) {
    m_genre = genre;
}

void AudioTagger::setComposer(QString composer) {
    m_composer = composer;
}

void AudioTagger::setGrouping (QString grouping) {
    m_grouping = grouping;
}

void AudioTagger::setYear (QString year) {
    m_year = year;
}

void AudioTagger::setComment(QString comment) {
    m_comment = comment;
}

void AudioTagger::setKey(QString key) {
    m_key = key;
}

void AudioTagger::setBpm(QString bpm) {
    m_bpm = bpm;
}

void AudioTagger::setTracknumber(QString tracknumber) {
    m_tracknumber = tracknumber;
}

bool AudioTagger::save() {
    TagLib::File* file = NULL;

    const QString& filePath = m_file.canonicalFilePath();

    if (filePath.endsWith(".mp3", Qt::CaseInsensitive)) {
        file = new TagLib::MPEG::File(TAGLIB_FILENAME_FROM_QSTRING(filePath));
        // process special ID3 fields, APEv2 fiels, etc

        // If the mp3 has no ID3v2 tag, we create a new one and add the TBPM and TKEY frame
        addID3v2Tag(((TagLib::MPEG::File*) file)->ID3v2Tag(true));
        // If the mp3 has an APE tag, we update
        addAPETag(((TagLib::MPEG::File*) file)->APETag(false));
    }

    if (filePath.endsWith(".m4a", Qt::CaseInsensitive)) {
        file = new TagLib::MP4::File(TAGLIB_FILENAME_FROM_QSTRING(filePath));
        // process special ID3 fields, APEv2 fiels, etc
        processMP4Tag(((TagLib::MP4::File*) file)->tag());

    }
    if (filePath.endsWith(".ogg", Qt::CaseInsensitive)) {
        file = new TagLib::Ogg::Vorbis::File(TAGLIB_FILENAME_FROM_QSTRING(filePath));
        // process special ID3 fields, APEv2 fiels, etc
        addXiphComment(((TagLib::Ogg::Vorbis::File*)file)->tag());

    }
    if (filePath.endsWith(".wav", Qt::CaseInsensitive)) {
        file = new TagLib::RIFF::WAV::File(TAGLIB_FILENAME_FROM_QSTRING(filePath));
        //If the flac has no ID3v2 tag, we create a new one and add the TBPM and TKEY frame
        addID3v2Tag(((TagLib::RIFF::WAV::File*)file)->tag());

    }
    if (filePath.endsWith(".flac", Qt::CaseInsensitive)) {
        file = new TagLib::FLAC::File(TAGLIB_FILENAME_FROM_QSTRING(filePath));

        //If the flac has no ID3v2 tag, we create a new one and add the TBPM and TKEY frame
        addID3v2Tag(((TagLib::FLAC::File*)file)->ID3v2Tag(true));
        //If the flac has no APE tag, we create a new one and add the TBPM and TKEY frame
        addXiphComment(((TagLib::FLAC::File*) file)->xiphComment(true));

    }
    if (filePath.endsWith(".aif", Qt::CaseInsensitive) ||
            filePath.endsWith(".aiff", Qt::CaseInsensitive)) {
        file = new TagLib::RIFF::AIFF::File(TAGLIB_FILENAME_FROM_QSTRING(filePath));
        //If the flac has no ID3v2 tag, we create a new one and add the TBPM and TKEY frame
        addID3v2Tag(((TagLib::RIFF::AIFF::File*)file)->tag());

    }

    //process standard tags
    if (file) {
        TagLib::Tag *tag = file->tag();
        if (tag) {
            tag->setArtist(m_artist.toStdString());
            tag->setTitle(m_title.toStdString());
            tag->setAlbum(m_album.toStdString());
            tag->setGenre(m_genre.toStdString());
            tag->setComment(m_comment.toStdString());
            uint year =  m_year.toUInt();
            if (year >  0)
                tag->setYear(year);
            uint tracknumber = m_tracknumber.toUInt();
            if (tracknumber > 0)
                tag->setTrack(tracknumber);

        }
        //write audio tags to file
        int success = file->save();
        if (success) {
            qDebug() << "Successfully updated metadata of track " << filePath;
        } else {
            qDebug() << "Could not update metadata of track " << filePath;
        }
        //delete file and return
        delete file;
        return success;
    } else {
        return false;
    }
}

void AudioTagger::addID3v2Tag(TagLib::ID3v2::Tag* id3v2) {
    if (!id3v2)
        return;

    TagLib::ID3v2::FrameList albumArtistFrame = id3v2->frameListMap()["TPE2"];
    if (!albumArtistFrame.isEmpty()) {
        albumArtistFrame.front()->setText(m_albumArtist.toStdString());
    } else {
        //add new frame
        TagLib::ID3v2::TextIdentificationFrame* newFrame =
                new TagLib::ID3v2::TextIdentificationFrame(
                    "TPE2", TagLib::String::Latin1);
        newFrame->setText(m_albumArtist.toStdString());
        id3v2->addFrame(newFrame);
    }

    TagLib::ID3v2::FrameList bpmFrame = id3v2->frameListMap()["TBPM"];
    if (!bpmFrame.isEmpty()) {
        bpmFrame.front()->setText(m_bpm.toStdString());
    } else {
         // add new frame TextIdentificationFrame which is responsible for TKEY and TBPM
         // see http://developer.kde.org/~wheeler/taglib/api/classTagLib_1_1ID3v2_1_1TextIdentificationFrame.html

        TagLib::ID3v2::TextIdentificationFrame* newFrame = new TagLib::ID3v2::TextIdentificationFrame("TBPM", TagLib::String::Latin1);

        newFrame->setText(m_bpm.toStdString());
        id3v2->addFrame(newFrame);

    }

    TagLib::ID3v2::FrameList keyFrame = id3v2->frameListMap()["TKEY"];
    if (!keyFrame.isEmpty()) {
        keyFrame.front()->setText(m_key.toStdString());
    } else {
        //add new frame
        TagLib::ID3v2::TextIdentificationFrame* newFrame = new TagLib::ID3v2::TextIdentificationFrame("TKEY", TagLib::String::Latin1);

        newFrame->setText(m_key.toStdString());
        id3v2->addFrame(newFrame);

    }

    TagLib::ID3v2::FrameList composerFrame = id3v2->frameListMap()["TCOM"];
    if (!composerFrame.isEmpty()) {
        composerFrame.front()->setText(m_composer.toStdString());
    } else {
        //add new frame
        TagLib::ID3v2::TextIdentificationFrame* newFrame =
                new TagLib::ID3v2::TextIdentificationFrame(
                    "TCOM", TagLib::String::Latin1);
        newFrame->setText(m_composer.toStdString());
        id3v2->addFrame(newFrame);
    }

    TagLib::ID3v2::FrameList groupingFrame = id3v2->frameListMap()["TIT1"];
    if (!groupingFrame.isEmpty()) {
        groupingFrame.front()->setText(m_grouping.toStdString());
    } else {
        //add new frame
        TagLib::ID3v2::TextIdentificationFrame* newFrame =
                new TagLib::ID3v2::TextIdentificationFrame(
                    "TIT1", TagLib::String::Latin1);
        newFrame->setText(m_grouping.toStdString());
        id3v2->addFrame(newFrame);
    }
}

void AudioTagger::addAPETag(TagLib::APE::Tag* ape) {
    if (!ape)
        return;
    // Adds to the item specified by key the data value.
    // If replace is true, then all of the other values on the same key will be removed first.
    ape->addValue("BPM",m_bpm.toStdString(), true);
    ape->addValue("Album Artist",m_albumArtist.toStdString(), true);
    ape->addValue("Composer",m_composer.toStdString(), true);
    ape->addValue("Grouping",m_grouping.toStdString(), true);

}

void AudioTagger::addXiphComment(TagLib::Ogg::XiphComment* xiph) {
    if