comparison data/fileio/MP3FileReader.cpp @ 271:822bd7fd526c

* Add support for reading mp3 and Ogg file title tags
author Chris Cannam
date Mon, 02 Jul 2007 13:53:38 +0000
parents e08f486e8d8c
children f1f47660483d
comparison
equal deleted inserted replaced
270:840dd5e6400f 271:822bd7fd526c
22 #include <sys/types.h> 22 #include <sys/types.h>
23 #include <sys/stat.h> 23 #include <sys/stat.h>
24 #include <fcntl.h> 24 #include <fcntl.h>
25 25
26 #include <iostream> 26 #include <iostream>
27
28 #ifdef HAVE_ID3TAG
29 #include <id3tag.h>
30 #endif
27 31
28 #include <QApplication> 32 #include <QApplication>
29 #include <QFileInfo> 33 #include <QFileInfo>
30 #include <QProgressDialog> 34 #include <QProgressDialog>
31 35
93 offset += sz; 97 offset += sz;
94 } 98 }
95 99
96 ::close(fd); 100 ::close(fd);
97 101
102 loadTags();
103
98 if (decodeMode == DecodeAtOnce) { 104 if (decodeMode == DecodeAtOnce) {
99 105
100 m_progress = new QProgressDialog 106 m_progress = new QProgressDialog
101 (QObject::tr("Decoding %1...").arg(QFileInfo(path).fileName()), 107 (QObject::tr("Decoding %1...").arg(QFileInfo(path).fileName()),
102 QObject::tr("Stop"), 0, 100); 108 QObject::tr("Stop"), 0, 100);
130 if (m_decodeThread) { 136 if (m_decodeThread) {
131 m_cancelled = true; 137 m_cancelled = true;
132 m_decodeThread->wait(); 138 m_decodeThread->wait();
133 delete m_decodeThread; 139 delete m_decodeThread;
134 } 140 }
141 }
142
143 void
144 MP3FileReader::loadTags()
145 {
146 m_title = "";
147
148 #ifdef HAVE_ID3TAG
149
150 id3_file *file = id3_file_open(m_path.toLocal8Bit().data(),
151 ID3_FILE_MODE_READONLY);
152 if (!file) return;
153
154 id3_tag *tag = id3_file_tag(file);
155
156 if (tag) {
157 id3_frame *frame = id3_tag_findframe(tag, "TIT2", 0); // work title
158
159 if (frame && frame->nfields >= 2) {
160 unsigned int nstrings = id3_field_getnstrings(&frame->fields[1]);
161
162 if (nstrings > 0) {
163 id3_ucs4_t const *ustr = id3_field_getstrings(&frame->fields[1], 0);
164
165 if (ustr) {
166 id3_utf8_t *u8str = id3_ucs4_utf8duplicate(ustr);
167 if (u8str) {
168 m_title = QString::fromUtf8((const char *)u8str);
169 free(u8str);
170 }
171 }
172 }
173 }
174 }
175
176 id3_file_close(file);
177 #endif
135 } 178 }
136 179
137 void 180 void
138 MP3FileReader::DecodeThread::run() 181 MP3FileReader::DecodeThread::run()
139 { 182 {