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@148
|
28 #include <QApplication>
|
Chris@186
|
29 #include <QFileInfo>
|
Chris@148
|
30 #include <QProgressDialog>
|
Chris@148
|
31
|
Chris@263
|
32 MP3FileReader::MP3FileReader(QString path, DecodeMode decodeMode, CacheMode mode) :
|
Chris@148
|
33 CodedAudioFileReader(mode),
|
Chris@264
|
34 m_path(path),
|
Chris@264
|
35 m_decodeThread(0)
|
Chris@148
|
36 {
|
Chris@148
|
37 m_frameCount = 0;
|
Chris@148
|
38 m_channelCount = 0;
|
Chris@148
|
39 m_sampleRate = 0;
|
Chris@148
|
40 m_fileSize = 0;
|
Chris@148
|
41 m_bitrateNum = 0;
|
Chris@148
|
42 m_bitrateDenom = 0;
|
Chris@148
|
43 m_frameCount = 0;
|
Chris@148
|
44 m_cancelled = false;
|
Chris@263
|
45 m_done = false;
|
Chris@263
|
46 m_progress = 0;
|
Chris@148
|
47
|
Chris@148
|
48 struct stat stat;
|
Chris@148
|
49 if (::stat(path.toLocal8Bit().data(), &stat) == -1 || stat.st_size == 0) {
|
Chris@148
|
50 m_error = QString("File %1 does not exist.").arg(path);
|
Chris@148
|
51 return;
|
Chris@148
|
52 }
|
Chris@148
|
53
|
Chris@148
|
54 m_fileSize = stat.st_size;
|
Chris@148
|
55
|
Chris@229
|
56 int fd = -1;
|
Chris@231
|
57 if ((fd = ::open(path.toLocal8Bit().data(), O_RDONLY
|
Chris@231
|
58 #ifdef _WIN32
|
Chris@231
|
59 | O_BINARY
|
Chris@231
|
60 #endif
|
Chris@231
|
61 , 0)) < 0) {
|
Chris@148
|
62 m_error = QString("Failed to open file %1 for reading.").arg(path);
|
Chris@148
|
63 return;
|
Chris@148
|
64 }
|
Chris@148
|
65
|
Chris@263
|
66 m_filebuffer = 0;
|
Chris@148
|
67
|
Chris@148
|
68 try {
|
Chris@263
|
69 m_filebuffer = new unsigned char[m_fileSize];
|
Chris@148
|
70 } catch (...) {
|
Chris@148
|
71 m_error = QString("Out of memory");
|
Chris@148
|
72 ::close(fd);
|
Chris@148
|
73 return;
|
Chris@148
|
74 }
|
Chris@148
|
75
|
Chris@229
|
76 ssize_t sz = 0;
|
Chris@229
|
77 size_t offset = 0;
|
Chris@229
|
78 while (offset < m_fileSize) {
|
Chris@263
|
79 sz = ::read(fd, m_filebuffer + offset, m_fileSize - offset);
|
Chris@229
|
80 if (sz < 0) {
|
Chris@231
|
81 m_error = QString("Read error for file %1 (after %2 bytes)")
|
Chris@231
|
82 .arg(path).arg(offset);
|
Chris@263
|
83 delete[] m_filebuffer;
|
Chris@229
|
84 ::close(fd);
|
Chris@229
|
85 return;
|
Chris@230
|
86 } else if (sz == 0) {
|
Chris@231
|
87 std::cerr << QString("MP3FileReader::MP3FileReader: Warning: reached EOF after only %1 of %2 bytes")
|
Chris@231
|
88 .arg(offset).arg(m_fileSize).toStdString() << std::endl;
|
Chris@231
|
89 m_fileSize = offset;
|
Chris@230
|
90 break;
|
Chris@229
|
91 }
|
Chris@229
|
92 offset += sz;
|
Chris@148
|
93 }
|
Chris@148
|
94
|
Chris@148
|
95 ::close(fd);
|
Chris@148
|
96
|
Chris@263
|
97 if (decodeMode == DecodeAtOnce) {
|
Chris@263
|
98
|
Chris@148
|
99 m_progress = new QProgressDialog
|
Chris@186
|
100 (QObject::tr("Decoding %1...").arg(QFileInfo(path).fileName()),
|
Chris@148
|
101 QObject::tr("Stop"), 0, 100);
|
Chris@148
|
102 m_progress->hide();
|
Chris@148
|
103
|
Chris@263
|
104 if (!decode(m_filebuffer, m_fileSize)) {
|
Chris@263
|
105 m_error = QString("Failed to decode file %1.").arg(path);
|
Chris@263
|
106 }
|
Chris@263
|
107
|
Chris@263
|
108 delete[] m_filebuffer;
|
Chris@263
|
109 m_filebuffer = 0;
|
Chris@148
|
110
|
Chris@263
|
111 if (isDecodeCacheInitialised()) finishDecodeCache();
|
Chris@263
|
112
|
Chris@148
|
113 delete m_progress;
|
Chris@148
|
114 m_progress = 0;
|
Chris@263
|
115
|
Chris@263
|
116 } else {
|
Chris@263
|
117
|
Chris@263
|
118 m_decodeThread = new DecodeThread(this);
|
Chris@263
|
119 m_decodeThread->start();
|
Chris@263
|
120
|
Chris@263
|
121 while (m_channelCount == 0 && !m_done) {
|
Chris@263
|
122 usleep(10);
|
Chris@263
|
123 }
|
Chris@148
|
124 }
|
Chris@148
|
125 }
|
Chris@148
|
126
|
Chris@148
|
127 MP3FileReader::~MP3FileReader()
|
Chris@148
|
128 {
|
Chris@263
|
129 if (m_decodeThread) {
|
Chris@263
|
130 m_decodeThread->wait();
|
Chris@263
|
131 delete m_decodeThread;
|
Chris@263
|
132 }
|
Chris@148
|
133 }
|
Chris@148
|
134
|
Chris@263
|
135 void
|
Chris@263
|
136 MP3FileReader::DecodeThread::run()
|
Chris@263
|
137 {
|
Chris@263
|
138 if (!m_reader->decode(m_reader->m_filebuffer, m_reader->m_fileSize)) {
|
Chris@263
|
139 m_reader->m_error = QString("Failed to decode file %1.").arg(m_reader->m_path);
|
Chris@263
|
140 }
|
Chris@263
|
141
|
Chris@263
|
142 delete[] m_reader->m_filebuffer;
|
Chris@263
|
143 m_reader->m_filebuffer = 0;
|
Chris@263
|
144
|
Chris@263
|
145 if (m_reader->isDecodeCacheInitialised()) m_reader->finishDecodeCache();
|
Chris@263
|
146
|
Chris@263
|
147 m_reader->m_done = true;
|
Chris@263
|
148 }
|
Chris@263
|
149
|
Chris@148
|
150 bool
|
Chris@148
|
151 MP3FileReader::decode(void *mm, size_t sz)
|
Chris@148
|
152 {
|
Chris@148
|
153 DecoderData data;
|
Chris@148
|
154 struct mad_decoder decoder;
|
Chris@148
|
155
|
Chris@148
|
156 data.start = (unsigned char const *)mm;
|
Chris@148
|
157 data.length = (unsigned long)sz;
|
Chris@148
|
158 data.reader = this;
|
Chris@148
|
159
|
Chris@148
|
160 mad_decoder_init(&decoder, &data, input, 0, 0, output, error, 0);
|
Chris@148
|
161 mad_decoder_run(&decoder, MAD_DECODER_MODE_SYNC);
|
Chris@148
|
162 mad_decoder_finish(&decoder);
|
Chris@148
|
163
|
Chris@263
|
164 m_done = true;
|
Chris@148
|
165 return true;
|
Chris@148
|
166 }
|
Chris@148
|
167
|
Chris@148
|
168 enum mad_flow
|
Chris@148
|
169 MP3FileReader::input(void *dp, struct mad_stream *stream)
|
Chris@148
|
170 {
|
Chris@148
|
171 DecoderData *data = (DecoderData *)dp;
|
Chris@148
|
172
|
Chris@148
|
173 if (!data->length) return MAD_FLOW_STOP;
|
Chris@148
|
174 mad_stream_buffer(stream, data->start, data->length);
|
Chris@148
|
175 data->length = 0;
|
Chris@148
|
176
|
Chris@148
|
177 return MAD_FLOW_CONTINUE;
|
Chris@148
|
178 }
|
Chris@148
|
179
|
Chris@148
|
180 enum mad_flow
|
Chris@148
|
181 MP3FileReader::output(void *dp,
|
Chris@148
|
182 struct mad_header const *header,
|
Chris@148
|
183 struct mad_pcm *pcm)
|
Chris@148
|
184 {
|
Chris@148
|
185 DecoderData *data = (DecoderData *)dp;
|
Chris@148
|
186 return data->reader->accept(header, pcm);
|
Chris@148
|
187 }
|
Chris@148
|
188
|
Chris@148
|
189 enum mad_flow
|
Chris@148
|
190 MP3FileReader::accept(struct mad_header const *header,
|
Chris@148
|
191 struct mad_pcm *pcm)
|
Chris@148
|
192 {
|
Chris@148
|
193 int channels = pcm->channels;
|
Chris@148
|
194 int frames = pcm->length;
|
Chris@148
|
195
|
Chris@148
|
196 if (header) {
|
Chris@148
|
197 m_bitrateNum += header->bitrate;
|
Chris@148
|
198 m_bitrateDenom ++;
|
Chris@148
|
199 }
|
Chris@148
|
200
|
Chris@148
|
201 if (frames < 1) return MAD_FLOW_CONTINUE;
|
Chris@148
|
202
|
Chris@148
|
203 if (m_channelCount == 0) {
|
Chris@148
|
204 m_channelCount = channels;
|
Chris@148
|
205 m_sampleRate = pcm->samplerate;
|
Chris@148
|
206 }
|
Chris@148
|
207
|
Chris@148
|
208 if (m_bitrateDenom > 0) {
|
Chris@148
|
209 double bitrate = m_bitrateNum / m_bitrateDenom;
|
Chris@148
|
210 double duration = double(m_fileSize * 8) / bitrate;
|
Chris@148
|
211 double elapsed = double(m_frameCount) / m_sampleRate;
|
Chris@148
|
212 double percent = ((elapsed * 100.0) / duration);
|
Chris@263
|
213 if (m_progress) {
|
Chris@263
|
214 int progress = int(percent);
|
Chris@263
|
215 if (progress < 1) progress = 1;
|
Chris@263
|
216 if (progress > 99) progress = 99;
|
Chris@263
|
217 if (progress > m_progress->value()) {
|
Chris@263
|
218 m_progress->setValue(progress);
|
Chris@263
|
219 m_progress->show();
|
Chris@263
|
220 m_progress->raise();
|
Chris@263
|
221 qApp->processEvents();
|
Chris@263
|
222 if (m_progress->wasCanceled()) {
|
Chris@263
|
223 m_cancelled = true;
|
Chris@263
|
224 }
|
Chris@148
|
225 }
|
Chris@148
|
226 }
|
Chris@148
|
227 }
|
Chris@148
|
228
|
Chris@148
|
229 if (m_cancelled) return MAD_FLOW_STOP;
|
Chris@148
|
230
|
Chris@148
|
231 m_frameCount += frames;
|
Chris@148
|
232
|
Chris@148
|
233 if (!isDecodeCacheInitialised()) {
|
Chris@148
|
234 initialiseDecodeCache();
|
Chris@148
|
235 }
|
Chris@148
|
236
|
Chris@148
|
237 for (int i = 0; i < frames; ++i) {
|
Chris@148
|
238
|
Chris@148
|
239 for (int ch = 0; ch < channels; ++ch) {
|
Chris@148
|
240 mad_fixed_t sample = 0;
|
Chris@148
|
241 if (ch < int(sizeof(pcm->samples) / sizeof(pcm->samples[0]))) {
|
Chris@148
|
242 sample = pcm->samples[ch][i];
|
Chris@148
|
243 }
|
Chris@148
|
244 float fsample = float(sample) / float(MAD_F_ONE);
|
Chris@148
|
245 addSampleToDecodeCache(fsample);
|
Chris@148
|
246 }
|
Chris@148
|
247
|
Chris@148
|
248 if (! (i & 0xffff)) {
|
Chris@148
|
249 // periodically munlock to ensure we don't exhaust real memory
|
Chris@148
|
250 // if running with memory locked down
|
Chris@148
|
251 MUNLOCK_SAMPLEBLOCK(m_data);
|
Chris@148
|
252 }
|
Chris@148
|
253 }
|
Chris@148
|
254
|
Chris@148
|
255 if (frames > 0) {
|
Chris@148
|
256 MUNLOCK_SAMPLEBLOCK(m_data);
|
Chris@148
|
257 }
|
Chris@148
|
258
|
Chris@148
|
259 return MAD_FLOW_CONTINUE;
|
Chris@148
|
260 }
|
Chris@148
|
261
|
Chris@148
|
262 enum mad_flow
|
Chris@148
|
263 MP3FileReader::error(void *dp,
|
Chris@148
|
264 struct mad_stream *stream,
|
Chris@148
|
265 struct mad_frame *)
|
Chris@148
|
266 {
|
Chris@148
|
267 DecoderData *data = (DecoderData *)dp;
|
Chris@148
|
268
|
Chris@148
|
269 fprintf(stderr, "decoding error 0x%04x (%s) at byte offset %u\n",
|
Chris@148
|
270 stream->error, mad_stream_errorstr(stream),
|
Chris@148
|
271 stream->this_frame - data->start);
|
Chris@148
|
272
|
Chris@148
|
273 return MAD_FLOW_CONTINUE;
|
Chris@148
|
274 }
|
Chris@148
|
275
|
Chris@157
|
276 void
|
Chris@157
|
277 MP3FileReader::getSupportedExtensions(std::set<QString> &extensions)
|
Chris@157
|
278 {
|
Chris@157
|
279 extensions.insert("mp3");
|
Chris@157
|
280 }
|
Chris@157
|
281
|
Chris@148
|
282 #endif
|