Chris@148
|
1
|
Chris@148
|
2 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
|
Chris@148
|
3
|
Chris@148
|
4 /*
|
Chris@148
|
5 Sonic Visualiser
|
Chris@148
|
6 An audio file viewer and annotation editor.
|
Chris@148
|
7 Centre for Digital Music, Queen Mary, University of London.
|
Chris@148
|
8 This file copyright 2006 Chris Cannam.
|
Chris@148
|
9
|
Chris@148
|
10 This program is free software; you can redistribute it and/or
|
Chris@148
|
11 modify it under the terms of the GNU General Public License as
|
Chris@148
|
12 published by the Free Software Foundation; either version 2 of the
|
Chris@148
|
13 License, or (at your option) any later version. See the file
|
Chris@148
|
14 COPYING included with this distribution for more information.
|
Chris@148
|
15 */
|
Chris@148
|
16
|
Chris@148
|
17 #ifdef HAVE_MAD
|
Chris@148
|
18
|
Chris@148
|
19 #include "MP3FileReader.h"
|
Chris@150
|
20 #include "system/System.h"
|
Chris@148
|
21
|
Chris@148
|
22 #include <sys/types.h>
|
Chris@148
|
23 #include <sys/stat.h>
|
Chris@148
|
24 #include <fcntl.h>
|
Chris@148
|
25
|
Chris@148
|
26 #include <iostream>
|
Chris@148
|
27
|
Chris@271
|
28 #ifdef HAVE_ID3TAG
|
Chris@271
|
29 #include <id3tag.h>
|
Chris@271
|
30 #endif
|
Chris@273
|
31 #define DEBUG_ID3TAG 1
|
Chris@271
|
32
|
Chris@148
|
33 #include <QApplication>
|
Chris@186
|
34 #include <QFileInfo>
|
Chris@148
|
35 #include <QProgressDialog>
|
Chris@148
|
36
|
Chris@285
|
37 MP3FileReader::MP3FileReader(std::string path, DecodeMode decodeMode, CacheMode mode) :
|
Chris@148
|
38 CodedAudioFileReader(mode),
|
Chris@264
|
39 m_path(path),
|
Chris@264
|
40 m_decodeThread(0)
|
Chris@148
|
41 {
|
Chris@148
|
42 m_frameCount = 0;
|
Chris@148
|
43 m_channelCount = 0;
|
Chris@148
|
44 m_sampleRate = 0;
|
Chris@148
|
45 m_fileSize = 0;
|
Chris@148
|
46 m_bitrateNum = 0;
|
Chris@148
|
47 m_bitrateDenom = 0;
|
Chris@148
|
48 m_frameCount = 0;
|
Chris@148
|
49 m_cancelled = false;
|
Chris@265
|
50 m_completion = 0;
|
Chris@263
|
51 m_done = false;
|
Chris@263
|
52 m_progress = 0;
|
Chris@148
|
53
|
Chris@148
|
54 struct stat stat;
|
Chris@285
|
55 if (::stat(path.c_str(), &stat) == -1 || stat.st_size == 0) {
|
Chris@285
|
56 setError("File does not exist", path);
|
Chris@148
|
57 return;
|
Chris@148
|
58 }
|
Chris@148
|
59
|
Chris@148
|
60 m_fileSize = stat.st_size;
|
Chris@148
|
61
|
Chris@229
|
62 int fd = -1;
|
Chris@285
|
63 if ((fd = ::open(path.c_str(), O_RDONLY
|
Chris@231
|
64 #ifdef _WIN32
|
Chris@231
|
65 | O_BINARY
|
Chris@231
|
66 #endif
|
Chris@231
|
67 , 0)) < 0) {
|
Chris@285
|
68 setError("Failed to open file for reading", path);
|
Chris@148
|
69 return;
|
Chris@148
|
70 }
|
Chris@148
|
71
|
Chris@263
|
72 m_filebuffer = 0;
|
Chris@148
|
73
|
Chris@148
|
74 try {
|
Chris@263
|
75 m_filebuffer = new unsigned char[m_fileSize];
|
Chris@148
|
76 } catch (...) {
|
Chris@285
|
77 setError("Out of memory");
|
Chris@148
|
78 ::close(fd);
|
Chris@148
|
79 return;
|
Chris@148
|
80 }
|
Chris@148
|
81
|
Chris@229
|
82 ssize_t sz = 0;
|
Chris@229
|
83 size_t offset = 0;
|
Chris@229
|
84 while (offset < m_fileSize) {
|
Chris@263
|
85 sz = ::read(fd, m_filebuffer + offset, m_fileSize - offset);
|
Chris@229
|
86 if (sz < 0) {
|
Chris@285
|
87 setError("Read error", path);
|
Chris@263
|
88 delete[] m_filebuffer;
|
Chris@229
|
89 ::close(fd);
|
Chris@229
|
90 return;
|
Chris@230
|
91 } else if (sz == 0) {
|
Chris@231
|
92 std::cerr << QString("MP3FileReader::MP3FileReader: Warning: reached EOF after only %1 of %2 bytes")
|
Chris@231
|
93 .arg(offset).arg(m_fileSize).toStdString() << std::endl;
|
Chris@231
|
94 m_fileSize = offset;
|
Chris@230
|
95 break;
|
Chris@229
|
96 }
|
Chris@229
|
97 offset += sz;
|
Chris@148
|
98 }
|
Chris@148
|
99
|
Chris@148
|
100 ::close(fd);
|
Chris@148
|
101
|
Chris@271
|
102 loadTags();
|
Chris@271
|
103
|
Chris@263
|
104 if (decodeMode == DecodeAtOnce) {
|
Chris@263
|
105
|
Chris@148
|
106 m_progress = new QProgressDialog
|
Chris@285
|
107 (QObject::tr("Decoding %1...").arg(QFileInfo(path.c_str()).fileName()),
|
Chris@148
|
108 QObject::tr("Stop"), 0, 100);
|
Chris@148
|
109 m_progress->hide();
|
Chris@148
|
110
|
Chris@263
|
111 if (!decode(m_filebuffer, m_fileSize)) {
|
Chris@285
|
112 setError("Failed to decode file", path);
|
Chris@263
|
113 }
|
Chris@263
|
114
|
Chris@263
|
115 delete[] m_filebuffer;
|
Chris@263
|
116 m_filebuffer = 0;
|
Chris@148
|
117
|
Chris@263
|
118 if (isDecodeCacheInitialised()) finishDecodeCache();
|
Chris@263
|
119
|
Chris@148
|
120 delete m_progress;
|
Chris@148
|
121 m_progress = 0;
|
Chris@263
|
122
|
Chris@263
|
123 } else {
|
Chris@263
|
124
|
Chris@263
|
125 m_decodeThread = new DecodeThread(this);
|
Chris@263
|
126 m_decodeThread->start();
|
Chris@263
|
127
|
Chris@263
|
128 while (m_channelCount == 0 && !m_done) {
|
Chris@263
|
129 usleep(10);
|
Chris@263
|
130 }
|
Chris@148
|
131 }
|
Chris@148
|
132 }
|
Chris@148
|
133
|
Chris@148
|
134 MP3FileReader::~MP3FileReader()
|
Chris@148
|
135 {
|
Chris@263
|
136 if (m_decodeThread) {
|
Chris@265
|
137 m_cancelled = true;
|
Chris@263
|
138 m_decodeThread->wait();
|
Chris@263
|
139 delete m_decodeThread;
|
Chris@263
|
140 }
|
Chris@148
|
141 }
|
Chris@148
|
142
|
Chris@263
|
143 void
|
Chris@271
|
144 MP3FileReader::loadTags()
|
Chris@271
|
145 {
|
Chris@271
|
146 m_title = "";
|
Chris@271
|
147
|
Chris@271
|
148 #ifdef HAVE_ID3TAG
|
Chris@271
|
149
|
Chris@285
|
150 id3_file *file = id3_file_open(m_path.c_str(),
|
Chris@271
|
151 ID3_FILE_MODE_READONLY);
|
Chris@271
|
152 if (!file) return;
|
Chris@271
|
153
|
Chris@273
|
154 // We can do this a lot more elegantly, but we'll leave that for
|
Chris@273
|
155 // when we implement support for more than just the one tag!
|
Chris@273
|
156
|
Chris@271
|
157 id3_tag *tag = id3_file_tag(file);
|
Chris@273
|
158 if (!tag) {
|
Chris@273
|
159 #ifdef DEBUG_ID3TAG
|
Chris@273
|
160 std::cerr << "MP3FileReader::loadTags: No ID3 tag found" << std::endl;
|
Chris@273
|
161 #endif
|
Chris@273
|
162 id3_file_close(file);
|
Chris@273
|
163 return;
|
Chris@271
|
164 }
|
Chris@271
|
165
|
Chris@273
|
166 id3_frame *frame = id3_tag_findframe(tag, "TIT2", 0); // work title
|
Chris@273
|
167 if (!frame) {
|
Chris@273
|
168 #ifdef DEBUG_ID3TAG
|
Chris@273
|
169 std::cerr << "MP3FileReader::loadTags: No work title in ID3 tag" << std::endl;
|
Chris@273
|
170 #endif
|
Chris@273
|
171 id3_file_close(file);
|
Chris@273
|
172 return;
|
Chris@273
|
173 }
|
Chris@273
|
174
|
Chris@273
|
175 if (frame->nfields < 2) {
|
Chris@273
|
176 std::cerr << "MP3FileReader::loadTags: WARNING: Not enough fields (" << frame->nfields << ") for work title in ID3 tag" << std::endl;
|
Chris@273
|
177 id3_file_close(file);
|
Chris@273
|
178 return;
|
Chris@273
|
179 }
|
Chris@273
|
180
|
Chris@273
|
181 unsigned int nstrings = id3_field_getnstrings(&frame->fields[1]);
|
Chris@273
|
182 if (nstrings == 0) {
|
Chris@273
|
183 #ifdef DEBUG_ID3TAG
|
Chris@273
|
184 std::cerr << "MP3FileReader::loadTags: No data for work title in ID3 tag" << std::endl;
|
Chris@273
|
185 #endif
|
Chris@273
|
186 id3_file_close(file);
|
Chris@273
|
187 return;
|
Chris@273
|
188 }
|
Chris@273
|
189
|
Chris@273
|
190 id3_ucs4_t const *ustr = id3_field_getstrings(&frame->fields[1], 0);
|
Chris@273
|
191 if (!ustr) {
|
Chris@273
|
192 #ifdef DEBUG_ID3TAG
|
Chris@273
|
193 std::cerr << "MP3FileReader::loadTags: Invalid or absent data for work title in ID3 tag" << std::endl;
|
Chris@273
|
194 #endif
|
Chris@273
|
195 id3_file_close(file);
|
Chris@273
|
196 return;
|
Chris@273
|
197 }
|
Chris@273
|
198
|
Chris@273
|
199 id3_utf8_t *u8str = id3_ucs4_utf8duplicate(ustr);
|
Chris@273
|
200 if (!u8str) {
|
Chris@273
|
201 std::cerr << "MP3FileReader::loadTags: ERROR: Internal error: Failed to convert UCS4 to UTF8 in ID3 title" << std::endl;
|
Chris@273
|
202 id3_file_close(file);
|
Chris@273
|
203 return;
|
Chris@273
|
204 }
|
Chris@273
|
205
|
Chris@285
|
206 m_title = (const char *)u8str;
|
Chris@273
|
207 free(u8str);
|
Chris@271
|
208 id3_file_close(file);
|
Chris@273
|
209
|
Chris@273
|
210 #else
|
Chris@273
|
211 #ifdef DEBUG_ID3TAG
|
Chris@273
|
212 std::cerr << "MP3FileReader::loadTags: ID3 tag support not compiled in"
|
Chris@273
|
213 << std::endl;
|
Chris@271
|
214 #endif
|
Chris@273
|
215 #endif
|
Chris@273
|
216
|
Chris@271
|
217 }
|
Chris@271
|
218
|
Chris@271
|
219 void
|
Chris@263
|
220 MP3FileReader::DecodeThread::run()
|
Chris@263
|
221 {
|
Chris@263
|
222 if (!m_reader->decode(m_reader->m_filebuffer, m_reader->m_fileSize)) {
|
Chris@285
|
223 m_reader->setError("Failed to decode file", m_reader->m_path);
|
Chris@263
|
224 }
|
Chris@263
|
225
|
Chris@263
|
226 delete[] m_reader->m_filebuffer;
|
Chris@263
|
227 m_reader->m_filebuffer = 0;
|
Chris@263
|
228
|
Chris@263
|
229 if (m_reader->isDecodeCacheInitialised()) m_reader->finishDecodeCache();
|
Chris@263
|
230
|
Chris@263
|
231 m_reader->m_done = true;
|
Chris@265
|
232 m_reader->m_completion = 100;
|
Chris@263
|
233 }
|
Chris@263
|
234
|
Chris@148
|
235 bool
|
Chris@148
|
236 MP3FileReader::decode(void *mm, size_t sz)
|
Chris@148
|
237 {
|
Chris@148
|
238 DecoderData data;
|
Chris@148
|
239 struct mad_decoder decoder;
|
Chris@148
|
240
|
Chris@148
|
241 data.start = (unsigned char const *)mm;
|
Chris@148
|
242 data.length = (unsigned long)sz;
|
Chris@148
|
243 data.reader = this;
|
Chris@148
|
244
|
Chris@148
|
245 mad_decoder_init(&decoder, &data, input, 0, 0, output, error, 0);
|
Chris@148
|
246 mad_decoder_run(&decoder, MAD_DECODER_MODE_SYNC);
|
Chris@148
|
247 mad_decoder_finish(&decoder);
|
Chris@148
|
248
|
Chris@263
|
249 m_done = true;
|
Chris@148
|
250 return true;
|
Chris@148
|
251 }
|
Chris@148
|
252
|
Chris@148
|
253 enum mad_flow
|
Chris@148
|
254 MP3FileReader::input(void *dp, struct mad_stream *stream)
|
Chris@148
|
255 {
|
Chris@148
|
256 DecoderData *data = (DecoderData *)dp;
|
Chris@148
|
257
|
Chris@148
|
258 if (!data->length) return MAD_FLOW_STOP;
|
Chris@148
|
259 mad_stream_buffer(stream, data->start, data->length);
|
Chris@148
|
260 data->length = 0;
|
Chris@148
|
261
|
Chris@148
|
262 return MAD_FLOW_CONTINUE;
|
Chris@148
|
263 }
|
Chris@148
|
264
|
Chris@148
|
265 enum mad_flow
|
Chris@148
|
266 MP3FileReader::output(void *dp,
|
Chris@148
|
267 struct mad_header const *header,
|
Chris@148
|
268 struct mad_pcm *pcm)
|
Chris@148
|
269 {
|
Chris@148
|
270 DecoderData *data = (DecoderData *)dp;
|
Chris@148
|
271 return data->reader->accept(header, pcm);
|
Chris@148
|
272 }
|
Chris@148
|
273
|
Chris@148
|
274 enum mad_flow
|
Chris@148
|
275 MP3FileReader::accept(struct mad_header const *header,
|
Chris@148
|
276 struct mad_pcm *pcm)
|
Chris@148
|
277 {
|
Chris@148
|
278 int channels = pcm->channels;
|
Chris@148
|
279 int frames = pcm->length;
|
Chris@148
|
280
|
Chris@148
|
281 if (header) {
|
Chris@148
|
282 m_bitrateNum += header->bitrate;
|
Chris@148
|
283 m_bitrateDenom ++;
|
Chris@148
|
284 }
|
Chris@148
|
285
|
Chris@148
|
286 if (frames < 1) return MAD_FLOW_CONTINUE;
|
Chris@148
|
287
|
Chris@148
|
288 if (m_channelCount == 0) {
|
Chris@148
|
289 m_channelCount = channels;
|
Chris@148
|
290 m_sampleRate = pcm->samplerate;
|
Chris@148
|
291 }
|
Chris@148
|
292
|
Chris@148
|
293 if (m_bitrateDenom > 0) {
|
Chris@148
|
294 double bitrate = m_bitrateNum / m_bitrateDenom;
|
Chris@148
|
295 double duration = double(m_fileSize * 8) / bitrate;
|
Chris@148
|
296 double elapsed = double(m_frameCount) / m_sampleRate;
|
Chris@148
|
297 double percent = ((elapsed * 100.0) / duration);
|
Chris@265
|
298 int progress = int(percent);
|
Chris@265
|
299 if (progress < 1) progress = 1;
|
Chris@265
|
300 if (progress > 99) progress = 99;
|
Chris@265
|
301 m_completion = progress;
|
Chris@263
|
302 if (m_progress) {
|
Chris@263
|
303 if (progress > m_progress->value()) {
|
Chris@263
|
304 m_progress->setValue(progress);
|
Chris@263
|
305 m_progress->show();
|
Chris@263
|
306 m_progress->raise();
|
Chris@263
|
307 qApp->processEvents();
|
Chris@263
|
308 if (m_progress->wasCanceled()) {
|
Chris@263
|
309 m_cancelled = true;
|
Chris@263
|
310 }
|
Chris@148
|
311 }
|
Chris@148
|
312 }
|
Chris@148
|
313 }
|
Chris@148
|
314
|
Chris@148
|
315 if (m_cancelled) return MAD_FLOW_STOP;
|
Chris@148
|
316
|
Chris@148
|
317 m_frameCount += frames;
|
Chris@148
|
318
|
Chris@148
|
319 if (!isDecodeCacheInitialised()) {
|
Chris@148
|
320 initialiseDecodeCache();
|
Chris@148
|
321 }
|
Chris@148
|
322
|
Chris@148
|
323 for (int i = 0; i < frames; ++i) {
|
Chris@148
|
324
|
Chris@148
|
325 for (int ch = 0; ch < channels; ++ch) {
|
Chris@148
|
326 mad_fixed_t sample = 0;
|
Chris@148
|
327 if (ch < int(sizeof(pcm->samples) / sizeof(pcm->samples[0]))) {
|
Chris@148
|
328 sample = pcm->samples[ch][i];
|
Chris@148
|
329 }
|
Chris@148
|
330 float fsample = float(sample) / float(MAD_F_ONE);
|
Chris@148
|
331 addSampleToDecodeCache(fsample);
|
Chris@148
|
332 }
|
Chris@148
|
333
|
Chris@148
|
334 if (! (i & 0xffff)) {
|
Chris@148
|
335 // periodically munlock to ensure we don't exhaust real memory
|
Chris@148
|
336 // if running with memory locked down
|
Chris@148
|
337 MUNLOCK_SAMPLEBLOCK(m_data);
|
Chris@148
|
338 }
|
Chris@148
|
339 }
|
Chris@148
|
340
|
Chris@148
|
341 if (frames > 0) {
|
Chris@148
|
342 MUNLOCK_SAMPLEBLOCK(m_data);
|
Chris@148
|
343 }
|
Chris@148
|
344
|
Chris@148
|
345 return MAD_FLOW_CONTINUE;
|
Chris@148
|
346 }
|
Chris@148
|
347
|
Chris@148
|
348 enum mad_flow
|
Chris@148
|
349 MP3FileReader::error(void *dp,
|
Chris@148
|
350 struct mad_stream *stream,
|
Chris@148
|
351 struct mad_frame *)
|
Chris@148
|
352 {
|
Chris@148
|
353 DecoderData *data = (DecoderData *)dp;
|
Chris@148
|
354
|
Chris@148
|
355 fprintf(stderr, "decoding error 0x%04x (%s) at byte offset %u\n",
|
Chris@148
|
356 stream->error, mad_stream_errorstr(stream),
|
Chris@148
|
357 stream->this_frame - data->start);
|
Chris@148
|
358
|
Chris@148
|
359 return MAD_FLOW_CONTINUE;
|
Chris@148
|
360 }
|
Chris@148
|
361
|
Chris@157
|
362 void
|
Chris@285
|
363 MP3FileReader::getSupportedExtensions(std::set<std::string> &extensions)
|
Chris@157
|
364 {
|
Chris@157
|
365 extensions.insert("mp3");
|
Chris@157
|
366 }
|
Chris@157
|
367
|
Chris@148
|
368 #endif
|