~mixxxdevelopers/mixxx/engine-control-refactor

« back to all changes in this revision

Viewing changes to mixxx/src/audiotagger.cpp

  • Committer: RJ Ryan
  • Date: 2013-06-04 00:41:29 UTC
  • mfrom: (2890.22.101 mixxx)
  • Revision ID: rryan@mixxx.org-20130604004129-8jjxkicsb3givu4a
MergingĀ fromĀ lp:mixxx.

Show diffs side-by-side

added added

removed removed

Lines of Context:
21
21
#include <taglib/wavfile.h>
22
22
#include <taglib/textidentificationframe.h>
23
23
 
24
 
AudioTagger::AudioTagger (QString file)
25
 
{
 
24
AudioTagger::AudioTagger (QString file) {
26
25
    m_artist = "";
27
26
    m_title = "";
28
27
    m_genre = "";
37
36
    m_file = file;
38
37
}
39
38
 
40
 
AudioTagger::~AudioTagger ( )
41
 
{
42
 
 
 
39
AudioTagger::~AudioTagger ( ) {
43
40
}
44
41
 
45
 
 
46
 
void AudioTagger::setArtist (QString artist )
47
 
{
 
42
void AudioTagger::setArtist (QString artist ) {
48
43
    m_artist = artist;
49
44
}
50
45
 
51
 
 
52
 
 
53
 
void AudioTagger::setTitle (QString title )
54
 
{
 
46
void AudioTagger::setTitle (QString title ) {
55
47
    m_title = title;
56
48
}
57
49
 
58
 
 
59
 
 
60
 
void AudioTagger::setAlbum (QString album )
61
 
{
 
50
void AudioTagger::setAlbum (QString album ) {
62
51
    m_album = album;
63
52
}
64
53
 
65
 
 
66
 
 
67
 
void AudioTagger::setGenre (QString genre )
68
 
{
 
54
void AudioTagger::setGenre (QString genre ) {
69
55
    m_genre = genre;
70
56
}
71
57
 
72
 
 
73
 
void AudioTagger::setComposer (QString composer )
74
 
{
 
58
void AudioTagger::setComposer (QString composer ) {
75
59
    m_composer = composer;
76
60
}
77
61
 
78
 
 
79
 
void AudioTagger::setYear (QString year )
80
 
{
 
62
void AudioTagger::setYear (QString year ) {
81
63
    m_year = year;
82
64
}
83
65
 
84
 
 
85
 
 
86
 
void AudioTagger::setComment (QString comment )
87
 
{
 
66
void AudioTagger::setComment (QString comment ) {
88
67
    m_comment = comment;
89
68
}
90
69
 
91
 
void AudioTagger::setKey (QString key )
92
 
{
 
70
void AudioTagger::setKey (QString key ) {
93
71
m_key = key;
94
72
}
95
73
 
96
 
void AudioTagger::setBpm (QString bpm )
97
 
{
 
74
void AudioTagger::setBpm (QString bpm ) {
98
75
    m_bpm = bpm;
99
76
}
100
77
 
101
 
void AudioTagger::setTracknumber (QString tracknumber )
102
 
{
 
78
void AudioTagger::setTracknumber (QString tracknumber ) {
103
79
    m_tracknumber = tracknumber;
104
80
}
105
 
bool AudioTagger::save ()
106
 
{
 
81
 
 
82
bool AudioTagger::save () {
107
83
    TagLib::File* file = NULL;
108
84
 
109
 
    if(m_file.endsWith(".mp3", Qt::CaseInsensitive)){
 
85
    if (m_file.endsWith(".mp3", Qt::CaseInsensitive)) {
110
86
        file =  new TagLib::MPEG::File(m_file.toUtf8().constData());
111
87
        //process special ID3 fields, APEv2 fiels, etc
112
88
 
116
92
        addAPETag( ((TagLib::MPEG::File*) file)->APETag(false)  );
117
93
 
118
94
    }
119
 
    if(m_file.endsWith(".m4a", Qt::CaseInsensitive)){
 
95
    if (m_file.endsWith(".m4a", Qt::CaseInsensitive)) {
120
96
        file =  new TagLib::MP4::File(m_file.toUtf8().constData());
121
97
        //process special ID3 fields, APEv2 fiels, etc
122
98
        processMP4Tag(((TagLib::MP4::File*) file)->tag());
123
99
 
124
100
    }
125
 
    if(m_file.endsWith(".ogg", Qt::CaseInsensitive)){
 
101
    if (m_file.endsWith(".ogg", Qt::CaseInsensitive)) {
126
102
        file =  new TagLib::Ogg::Vorbis::File(m_file.toUtf8().constData());
127
103
        //process special ID3 fields, APEv2 fiels, etc
128
104
        addXiphComment( ((TagLib::Ogg::Vorbis::File*) file)->tag()   );
129
105
 
130
106
    }
131
 
    if(m_file.endsWith(".wav", Qt::CaseInsensitive)){
 
107
    if (m_file.endsWith(".wav", Qt::CaseInsensitive)) {
132
108
        file =  new TagLib::RIFF::WAV::File(m_file.toUtf8().constData());
133
109
        //If the flac has no ID3v2 tag, we create a new one and add the TBPM and TKEY frame
134
110
        addID3v2Tag( ((TagLib::RIFF::WAV::File*) file)->tag()  );
135
111
 
136
112
    }
137
 
    if(m_file.endsWith(".flac", Qt::CaseInsensitive)){
 
113
    if (m_file.endsWith(".flac", Qt::CaseInsensitive)) {
138
114
        file =  new TagLib::FLAC::File(m_file.toUtf8().constData());
139
115
 
140
116
        //If the flac has no ID3v2 tag, we create a new one and add the TBPM and TKEY frame
143
119
        addXiphComment( ((TagLib::FLAC::File*) file)->xiphComment (true)   );
144
120
 
145
121
    }
146
 
    if(m_file.endsWith(".aif", Qt::CaseInsensitive) || m_file.endsWith(".aiff", Qt::CaseInsensitive)){
 
122
    if (m_file.endsWith(".aif", Qt::CaseInsensitive) || m_file.endsWith(".aiff", Qt::CaseInsensitive)) {
147
123
        file =  new TagLib::RIFF::AIFF::File(m_file.toUtf8().constData());
148
124
        //If the flac has no ID3v2 tag, we create a new one and add the TBPM and TKEY frame
149
125
        addID3v2Tag( ((TagLib::RIFF::AIFF::File*) file)->tag()  );
151
127
    }
152
128
 
153
129
    //process standard tags
154
 
    if(file){
 
130
    if (file) {
155
131
        TagLib::Tag *tag = file->tag();
156
 
        if (tag)
157
 
        {
 
132
        if (tag) {
158
133
            tag->setArtist(m_artist.toStdString());
159
134
            tag->setTitle(m_title.toStdString());
160
135
            tag->setAlbum(m_album.toStdString());
161
136
            tag->setGenre(m_genre.toStdString());
162
137
            tag->setComment(m_comment.toStdString());
163
138
            uint year =  m_year.toUInt();
164
 
            if(year >  0)
 
139
            if (year >  0)
165
140
                tag->setYear(year);
166
141
            uint tracknumber = m_tracknumber.toUInt();
167
 
            if(tracknumber > 0)
 
142
            if (tracknumber > 0)
168
143
                tag->setTrack(tracknumber);
169
144
 
170
145
        }
174
149
            qDebug() << "Successfully updated metadata of track " << m_file;
175
150
        } else {
176
151
             qDebug() << "Could not update metadata of track " << m_file;
177
 
         }
 
152
        }
178
153
        //delete file and return
179
154
        delete file;
180
155
        return success;
181
 
    }
182
 
    else{
 
156
    } else {
183
157
        return false;
184
158
    }
185
 
 
186
 
 
187
159
}
188
 
void AudioTagger::addID3v2Tag(TagLib::ID3v2::Tag* id3v2)
189
 
{
190
 
    if(!id3v2) return;
 
160
 
 
161
void AudioTagger::addID3v2Tag(TagLib::ID3v2::Tag* id3v2) {
 
162
    if (!id3v2)
 
163
        return;
191
164
 
192
165
    TagLib::ID3v2::FrameList bpmFrame = id3v2->frameListMap()["TBPM"];
193
 
    if (!bpmFrame.isEmpty())
194
 
    {
 
166
    if (!bpmFrame.isEmpty()) {
195
167
        bpmFrame.front()->setText(m_bpm.toStdString());
196
 
 
197
 
    }
198
 
    else
199
 
    {
200
 
        /*
201
 
         * add new frame TextIdentificationFrame which is responsible for TKEY and TBPM
202
 
         * see http://developer.kde.org/~wheeler/taglib/api/classTagLib_1_1ID3v2_1_1TextIdentificationFrame.html
203
 
         */
 
168
    } else {
 
169
         // add new frame TextIdentificationFrame which is responsible for TKEY and TBPM
 
170
         // see http://developer.kde.org/~wheeler/taglib/api/classTagLib_1_1ID3v2_1_1TextIdentificationFrame.html
204
171
 
205
172
        TagLib::ID3v2::TextIdentificationFrame* newFrame = new TagLib::ID3v2::TextIdentificationFrame("TBPM", TagLib::String::Latin1);
206
173
 
210
177
    }
211
178
 
212
179
    TagLib::ID3v2::FrameList keyFrame = id3v2->frameListMap()["TKEY"];
213
 
    if (!keyFrame.isEmpty())
214
 
    {
 
180
    if (!keyFrame.isEmpty()) {
215
181
        keyFrame.front()->setText(m_key.toStdString());
216
 
 
217
 
    }
218
 
    else
219
 
    {
 
182
    } else {
220
183
        //add new frame
221
184
        TagLib::ID3v2::TextIdentificationFrame* newFrame = new TagLib::ID3v2::TextIdentificationFrame("TKEY", TagLib::String::Latin1);
222
185
 
226
189
    }
227
190
 
228
191
    TagLib::ID3v2::FrameList composerFrame = id3v2->frameListMap()["TCOM"];
229
 
    if (!composerFrame.isEmpty())
230
 
    {
 
192
    if (!composerFrame.isEmpty()) {
231
193
        composerFrame.front()->setText(m_composer.toStdString());
232
 
    }
233
 
    else
234
 
    {
 
194
    } else {
235
195
        //add new frame
236
196
        TagLib::ID3v2::TextIdentificationFrame* newFrame =
237
197
                new TagLib::ID3v2::TextIdentificationFrame(
240
200
        id3v2->addFrame(newFrame);
241
201
    }
242
202
}
243
 
void AudioTagger::addAPETag(TagLib::APE::Tag* ape)
244
 
{
245
 
    if(!ape) return;
246
 
    /*
247
 
     * Adds to the item specified by key the data value.
248
 
     * If replace is true, then all of the other values on the same key will be removed first.
249
 
     */
 
203
 
 
204
void AudioTagger::addAPETag(TagLib::APE::Tag* ape) {
 
205
    if (!ape)
 
206
        return;
 
207
    // Adds to the item specified by key the data value.
 
208
    // If replace is true, then all of the other values on the same key will be removed first.
250
209
    ape->addValue("BPM",m_bpm.toStdString(), true);
251
210
    ape->addValue("BPM",m_bpm.toStdString(), true);
252
211
    ape->addValue("Composer",m_composer.toStdString(), true);
253
212
 
254
213
}
255
 
void AudioTagger::addXiphComment(TagLib::Ogg::XiphComment* xiph)
256
 
{
257
 
    if(!xiph) return;
 
214
void AudioTagger::addXiphComment(TagLib::Ogg::XiphComment* xiph) {
 
215
    if (!xiph)
 
216
        return;
258
217
 
259
218
    // Some tools use "BPM" so check for that.
260
219
 
261
 
    /* Taglib does not support the update of Vorbis comments.
262
 
     * thus, we have to reomve the old comment and add the new one
263
 
     */
 
220
    // Taglib does not support the update of Vorbis comments.
 
221
    // thus, we have to reomve the old comment and add the new one
264
222
    xiph->removeField("BPM");
265
223
    xiph->addField("BPM", m_bpm.toStdString());
266
224
 
275
233
    xiph->removeField("COMPOSER");
276
234
    xiph->addField("COMPOSER", m_key.toStdString());
277
235
}
278
 
void AudioTagger::processMP4Tag(TagLib::MP4::Tag* mp4)
279
 
{
 
236
void AudioTagger::processMP4Tag(TagLib::MP4::Tag* mp4) {
280
237
    //TODO
281
 
 
282
238
}
283
239
 
284
240