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