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@271
|
31
|
Chris@148
|
32 #include <QApplication>
|
Chris@186
|
33 #include <QFileInfo>
|
Chris@148
|
34 #include <QProgressDialog>
|
Chris@148
|
35
|
Chris@263
|
36 MP3FileReader::MP3FileReader(QString path, DecodeMode decodeMode, CacheMode mode) :
|
Chris@148
|
37 CodedAudioFileReader(mode),
|
Chris@264
|
38 m_path(path),
|
Chris@264
|
39 m_decodeThread(0)
|
Chris@148
|
40 {
|
Chris@148
|
41 m_frameCount = 0;
|
Chris@148
|
42 m_channelCount = 0;
|
Chris@148
|
43 m_sampleRate = 0;
|
Chris@148
|
44 m_fileSize = 0;
|
Chris@148
|
45 m_bitrateNum = 0;
|
Chris@148
|
46 m_bitrateDenom = 0;
|
Chris@148
|
47 m_frameCount = 0;
|
Chris@148
|
48 m_cancelled = false;
|
Chris@265
|
49 m_completion = 0;
|
Chris@263
|
50 m_done = false;
|
Chris@263
|
51 m_progress = 0;
|
Chris@148
|
52
|
Chris@148
|
53 struct stat stat;
|
Chris@148
|
54 if (::stat(path.toLocal8Bit().data(), &stat) == -1 || stat.st_size == 0) {
|
Chris@148
|
55 m_error = QString("File %1 does not exist.").arg(path);
|
Chris@148
|
56 return;
|
Chris@148
|
57 }
|
Chris@148
|
58
|
Chris@148
|
59 m_fileSize = stat.st_size;
|
Chris@148
|
60
|
Chris@229
|
61 int fd = -1;
|
Chris@231
|
62 if ((fd = ::open(path.toLocal8Bit().data(), O_RDONLY
|
Chris@231
|
63 #ifdef _WIN32
|
Chris@231
|
64 | O_BINARY
|
Chris@231
|
65 #endif
|
Chris@231
|
66 , 0)) < 0) {
|
Chris@148
|
67 m_error = QString("Failed to open file %1 for reading.").arg(path);
|
Chris@148
|
68 return;
|
Chris@148
|
69 }
|
Chris@148
|
70
|
Chris@263
|
71 m_filebuffer = 0;
|
Chris@148
|
72
|
Chris@148
|
73 try {
|
Chris@263
|
74 m_filebuffer = new unsigned char[m_fileSize];
|
Chris@148
|
75 } catch (...) {
|
Chris@148
|
76 m_error = QString("Out of memory");
|
Chris@148
|
77 ::close(fd);
|
Chris@148
|
78 return;
|
Chris@148
|
79 }
|
Chris@148
|
80
|
Chris@229
|
81 ssize_t sz = 0;
|
Chris@229
|
82 size_t offset = 0;
|
Chris@229
|
83 while (offset < m_fileSize) {
|
Chris@263
|
84 sz = ::read(fd, m_filebuffer + offset, m_fileSize - offset);
|
Chris@229
|
85 if (sz < 0) {
|
Chris@231
|
86 m_error = QString("Read error for file %1 (after %2 bytes)")
|
Chris@231
|
87 .arg(path).arg(offset);
|
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@186
|
107 (QObject::tr("Decoding %1...").arg(QFileInfo(path).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@263
|
112 m_error = QString("Failed to decode file %1.").arg(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@271
|
150 id3_file *file = id3_file_open(m_path.toLocal8Bit().data(),
|
Chris@271
|
151 ID3_FILE_MODE_READONLY);
|
Chris@271
|
152 if (!file) return;
|
Chris@271
|
153
|
Chris@271
|
154 id3_tag *tag = id3_file_tag(file);
|
Chris@271
|
155
|
Chris@271
|
156 if (tag) {
|
Chris@271
|
157 id3_frame *frame = id3_tag_findframe(tag, "TIT2", 0); // work title
|
Chris@271
|
158
|
Chris@271
|
159 if (frame && frame->nfields >= 2) {
|
Chris@271
|
160 unsigned int nstrings = id3_field_getnstrings(&frame->fields[1]);
|
Chris@271
|
161
|
Chris@271
|
162 if (nstrings > 0) {
|
Chris@271
|
163 id3_ucs4_t const *ustr = id3_field_getstrings(&frame->fields[1], 0);
|
Chris@271
|
164
|
Chris@271
|
165 if (ustr) {
|
Chris@271
|
166 id3_utf8_t *u8str = id3_ucs4_utf8duplicate(ustr);
|
Chris@271
|
167 if (u8str) {
|
Chris@271
|
168 m_title = QString::fromUtf8((const char *)u8str);
|
Chris@271
|
169 free(u8str);
|
Chris@271
|
170 }
|
Chris@271
|
171 }
|
Chris@271
|
172 }
|
Chris@271
|
173 }
|
Chris@271
|
174 }
|
Chris@271
|
175
|
Chris@271
|
176 id3_file_close(file);
|
Chris@271
|
177 #endif
|
Chris@271
|
178 }
|
Chris@271
|
179
|
Chris@271
|
180 void
|
Chris@263
|
181 MP3FileReader::DecodeThread::run()
|
Chris@263
|
182 {
|
Chris@263
|
183 if (!m_reader->decode(m_reader->m_filebuffer, m_reader->m_fileSize)) {
|
Chris@263
|
184 m_reader->m_error = QString("Failed to decode file %1.").arg(m_reader->m_path);
|
Chris@263
|
185 }
|
Chris@263
|
186
|
Chris@263
|
187 delete[] m_reader->m_filebuffer;
|
Chris@263
|
188 m_reader->m_filebuffer = 0;
|
Chris@263
|
189
|
Chris@263
|
190 if (m_reader->isDecodeCacheInitialised()) m_reader->finishDecodeCache();
|
Chris@263
|
191
|
Chris@263
|
192 m_reader->m_done = true;
|
Chris@265
|
193 m_reader->m_completion = 100;
|
Chris@263
|
194 }
|
Chris@263
|
195
|
Chris@148
|
196 bool
|
Chris@148
|
197 MP3FileReader::decode(void *mm, size_t sz)
|
Chris@148
|
198 {
|
Chris@148
|
199 DecoderData data;
|
Chris@148
|
200 struct mad_decoder decoder;
|
Chris@148
|
201
|
Chris@148
|
202 data.start = (unsigned char const *)mm;
|
Chris@148
|
203 data.length = (unsigned long)sz;
|
Chris@148
|
204 data.reader = this;
|
Chris@148
|
205
|
Chris@148
|
206 mad_decoder_init(&decoder, &data, input, 0, 0, output, error, 0);
|
Chris@148
|
207 mad_decoder_run(&decoder, MAD_DECODER_MODE_SYNC);
|
Chris@148
|
208 mad_decoder_finish(&decoder);
|
Chris@148
|
209
|
Chris@263
|
210 m_done = true;
|
Chris@148
|
211 return true;
|
Chris@148
|
212 }
|
Chris@148
|
213
|
Chris@148
|
214 enum mad_flow
|
Chris@148
|
215 MP3FileReader::input(void *dp, struct mad_stream *stream)
|
Chris@148
|
216 {
|
Chris@148
|
217 DecoderData *data = (DecoderData *)dp;
|
Chris@148
|
218
|
Chris@148
|
219 if (!data->length) return MAD_FLOW_STOP;
|
Chris@148
|
220 mad_stream_buffer(stream, data->start, data->length);
|
Chris@148
|
221 data->length = 0;
|
Chris@148
|
222
|
Chris@148
|
223 return MAD_FLOW_CONTINUE;
|
Chris@148
|
224 }
|
Chris@148
|
225
|
Chris@148
|
226 enum mad_flow
|
Chris@148
|
227 MP3FileReader::output(void *dp,
|
Chris@148
|
228 struct mad_header const *header,
|
Chris@148
|
229 struct mad_pcm *pcm)
|
Chris@148
|
230 {
|
Chris@148
|
231 DecoderData *data = (DecoderData *)dp;
|
Chris@148
|
232 return data->reader->accept(header, pcm);
|
Chris@148
|
233 }
|
Chris@148
|
234
|
Chris@148
|
235 enum mad_flow
|
Chris@148
|
236 MP3FileReader::accept(struct mad_header const *header,
|
Chris@148
|
237 struct mad_pcm *pcm)
|
Chris@148
|
238 {
|
Chris@148
|
239 int channels = pcm->channels;
|
Chris@148
|
240 int frames = pcm->length;
|
Chris@148
|
241
|
Chris@148
|
242 if (header) {
|
Chris@148
|
243 m_bitrateNum += header->bitrate;
|
Chris@148
|
244 m_bitrateDenom ++;
|
Chris@148
|
245 }
|
Chris@148
|
246
|
Chris@148
|
247 if (frames < 1) return MAD_FLOW_CONTINUE;
|
Chris@148
|
248
|
Chris@148
|
249 if (m_channelCount == 0) {
|
Chris@148
|
250 m_channelCount = channels;
|
Chris@148
|
251 m_sampleRate = pcm->samplerate;
|
Chris@148
|
252 }
|
Chris@148
|
253
|
Chris@148
|
254 if (m_bitrateDenom > 0) {
|
Chris@148
|
255 double bitrate = m_bitrateNum / m_bitrateDenom;
|
Chris@148
|
256 double duration = double(m_fileSize * 8) / bitrate;
|
Chris@148
|
257 double elapsed = double(m_frameCount) / m_sampleRate;
|
Chris@148
|
258 double percent = ((elapsed * 100.0) / duration);
|
Chris@265
|
259 int progress = int(percent);
|
Chris@265
|
260 if (progress < 1) progress = 1;
|
Chris@265
|
261 if (progress > 99) progress = 99;
|
Chris@265
|
262 m_completion = progress;
|
Chris@263
|
263 if (m_progress) {
|
Chris@263
|
264 if (progress > m_progress->value()) {
|
Chris@263
|
265 m_progress->setValue(progress);
|
Chris@263
|
266 m_progress->show();
|
Chris@263
|
267 m_progress->raise();
|
Chris@263
|
268 qApp->processEvents();
|
Chris@263
|
269 if (m_progress->wasCanceled()) {
|
Chris@263
|
270 m_cancelled = true;
|
Chris@263
|
271 }
|
Chris@148
|
272 }
|
Chris@148
|
273 }
|
Chris@148
|
274 }
|
Chris@148
|
275
|
Chris@148
|
276 if (m_cancelled) return MAD_FLOW_STOP;
|
Chris@148
|
277
|
Chris@148
|
278 m_frameCount += frames;
|
Chris@148
|
279
|
Chris@148
|
280 if (!isDecodeCacheInitialised()) {
|
Chris@148
|
281 initialiseDecodeCache();
|
Chris@148
|
282 }
|
Chris@148
|
283
|
Chris@148
|
284 for (int i = 0; i < frames; ++i) {
|
Chris@148
|
285
|
Chris@148
|
286 for (int ch = 0; ch < channels; ++ch) {
|
Chris@148
|
287 mad_fixed_t sample = 0;
|
Chris@148
|
288 if (ch < int(sizeof(pcm->samples) / sizeof(pcm->samples[0]))) {
|
Chris@148
|
289 sample = pcm->samples[ch][i];
|
Chris@148
|
290 }
|
Chris@148
|
291 float fsample = float(sample) / float(MAD_F_ONE);
|
Chris@148
|
292 addSampleToDecodeCache(fsample);
|
Chris@148
|
293 }
|
Chris@148
|
294
|
Chris@148
|
295 if (! (i & 0xffff)) {
|
Chris@148
|
296 // periodically munlock to ensure we don't exhaust real memory
|
Chris@148
|
297 // if running with memory locked down
|
Chris@148
|
298 MUNLOCK_SAMPLEBLOCK(m_data);
|
Chris@148
|
299 }
|
Chris@148
|
300 }
|
Chris@148
|
301
|
Chris@148
|
302 if (frames > 0) {
|
Chris@148
|
303 MUNLOCK_SAMPLEBLOCK(m_data);
|
Chris@148
|
304 }
|
Chris@148
|
305
|
Chris@148
|
306 return MAD_FLOW_CONTINUE;
|
Chris@148
|
307 }
|
Chris@148
|
308
|
Chris@148
|
309 enum mad_flow
|
Chris@148
|
310 MP3FileReader::error(void *dp,
|
Chris@148
|
311 struct mad_stream *stream,
|
Chris@148
|
312 struct mad_frame *)
|
Chris@148
|
313 {
|
Chris@148
|
314 DecoderData *data = (DecoderData *)dp;
|
Chris@148
|
315
|
Chris@148
|
316 fprintf(stderr, "decoding error 0x%04x (%s) at byte offset %u\n",
|
Chris@148
|
317 stream->error, mad_stream_errorstr(stream),
|
Chris@148
|
318 stream->this_frame - data->start);
|
Chris@148
|
319
|
Chris@148
|
320 return MAD_FLOW_CONTINUE;
|
Chris@148
|
321 }
|
Chris@148
|
322
|
Chris@157
|
323 void
|
Chris@157
|
324 MP3FileReader::getSupportedExtensions(std::set<QString> &extensions)
|
Chris@157
|
325 {
|
Chris@157
|
326 extensions.insert("mp3");
|
Chris@157
|
327 }
|
Chris@157
|
328
|
Chris@148
|
329 #endif
|