comparison data/fileio/MP3FileReader.cpp @ 148:1a42221a1522

* Reorganising code base. This revision will not compile.
author Chris Cannam
date Mon, 31 Jul 2006 11:49:58 +0000
parents
children 4b2ea82fd0ed
comparison
equal deleted inserted replaced
147:3a13b0d4934e 148:1a42221a1522
1
2 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
3
4 /*
5 Sonic Visualiser
6 An audio file viewer and annotation editor.
7 Centre for Digital Music, Queen Mary, University of London.
8 This file copyright 2006 Chris Cannam.
9
10 This program is free software; you can redistribute it and/or
11 modify it under the terms of the GNU General Public License as
12 published by the Free Software Foundation; either version 2 of the
13 License, or (at your option) any later version. See the file
14 COPYING included with this distribution for more information.
15 */
16
17 #ifdef HAVE_MAD
18
19 #include "MP3FileReader.h"
20 #include "base/System.h"
21
22 #include <sys/types.h>
23 #include <sys/stat.h>
24 #include <fcntl.h>
25
26 #include <iostream>
27
28 #include <QApplication>
29 #include <QProgressDialog>
30
31 MP3FileReader::MP3FileReader(QString path, bool showProgress, CacheMode mode) :
32 CodedAudioFileReader(mode),
33 m_path(path)
34 {
35 m_frameCount = 0;
36 m_channelCount = 0;
37 m_sampleRate = 0;
38 m_fileSize = 0;
39 m_bitrateNum = 0;
40 m_bitrateDenom = 0;
41 m_frameCount = 0;
42 m_cancelled = false;
43
44 struct stat stat;
45 if (::stat(path.toLocal8Bit().data(), &stat) == -1 || stat.st_size == 0) {
46 m_error = QString("File %1 does not exist.").arg(path);
47 return;
48 }
49
50 m_fileSize = stat.st_size;
51
52 int fd;
53 if ((fd = ::open(path.toLocal8Bit().data(), O_RDONLY, 0)) < 0) {
54 m_error = QString("Failed to open file %1 for reading.").arg(path);
55 return;
56 }
57
58 unsigned char *filebuffer = 0;
59
60 try {
61 filebuffer = new unsigned char[stat.st_size];
62 } catch (...) {
63 m_error = QString("Out of memory");
64 ::close(fd);
65 return;
66 }
67
68 if (::read(fd, filebuffer, stat.st_size) < stat.st_size) {
69 m_error = QString("Failed to read file %1.").arg(path);
70 delete[] filebuffer;
71 ::close(fd);
72 return;
73 }
74
75 ::close(fd);
76
77 if (showProgress) {
78 m_progress = new QProgressDialog
79 (QObject::tr("Decoding MP3 file..."),
80 QObject::tr("Stop"), 0, 100);
81 m_progress->hide();
82 }
83
84 if (!decode(filebuffer, stat.st_size)) {
85 m_error = QString("Failed to decode file %1.").arg(path);
86 delete[] filebuffer;
87 return;
88 }
89
90 if (isDecodeCacheInitialised()) finishDecodeCache();
91
92 if (showProgress) {
93 delete m_progress;
94 m_progress = 0;
95 }
96
97 delete[] filebuffer;
98 }
99
100 MP3FileReader::~MP3FileReader()
101 {
102 }
103
104 bool
105 MP3FileReader::decode(void *mm, size_t sz)
106 {
107 DecoderData data;
108 struct mad_decoder decoder;
109
110 data.start = (unsigned char const *)mm;
111 data.length = (unsigned long)sz;
112 data.reader = this;
113
114 mad_decoder_init(&decoder, &data, input, 0, 0, output, error, 0);
115 mad_decoder_run(&decoder, MAD_DECODER_MODE_SYNC);
116 mad_decoder_finish(&decoder);
117
118 return true;
119 }
120
121 enum mad_flow
122 MP3FileReader::input(void *dp, struct mad_stream *stream)
123 {
124 DecoderData *data = (DecoderData *)dp;
125
126 if (!data->length) return MAD_FLOW_STOP;
127 mad_stream_buffer(stream, data->start, data->length);
128 data->length = 0;
129
130 return MAD_FLOW_CONTINUE;
131 }
132
133 enum mad_flow
134 MP3FileReader::output(void *dp,
135 struct mad_header const *header,
136 struct mad_pcm *pcm)
137 {
138 DecoderData *data = (DecoderData *)dp;
139 return data->reader->accept(header, pcm);
140 }
141
142 enum mad_flow
143 MP3FileReader::accept(struct mad_header const *header,
144 struct mad_pcm *pcm)
145 {
146 int channels = pcm->channels;
147 int frames = pcm->length;
148
149 if (header) {
150 m_bitrateNum += header->bitrate;
151 m_bitrateDenom ++;
152 }
153
154 if (frames < 1) return MAD_FLOW_CONTINUE;
155
156 if (m_channelCount == 0) {
157 m_channelCount = channels;
158 m_sampleRate = pcm->samplerate;
159 }
160
161 if (m_bitrateDenom > 0) {
162 double bitrate = m_bitrateNum / m_bitrateDenom;
163 double duration = double(m_fileSize * 8) / bitrate;
164 double elapsed = double(m_frameCount) / m_sampleRate;
165 double percent = ((elapsed * 100.0) / duration);
166 int progress = int(percent);
167 if (progress < 1) progress = 1;
168 if (progress > 99) progress = 99;
169 if (progress > m_progress->value()) {
170 m_progress->setValue(progress);
171 m_progress->show();
172 m_progress->raise();
173 qApp->processEvents();
174 if (m_progress->wasCanceled()) {
175 m_cancelled = true;
176 }
177 }
178 }
179
180 if (m_cancelled) return MAD_FLOW_STOP;
181
182 m_frameCount += frames;
183
184 if (!isDecodeCacheInitialised()) {
185 initialiseDecodeCache();
186 }
187
188 for (int i = 0; i < frames; ++i) {
189
190 for (int ch = 0; ch < channels; ++ch) {
191 mad_fixed_t sample = 0;
192 if (ch < int(sizeof(pcm->samples) / sizeof(pcm->samples[0]))) {
193 sample = pcm->samples[ch][i];
194 }
195 float fsample = float(sample) / float(MAD_F_ONE);
196 addSampleToDecodeCache(fsample);
197 }
198
199 if (! (i & 0xffff)) {
200 // periodically munlock to ensure we don't exhaust real memory
201 // if running with memory locked down
202 MUNLOCK_SAMPLEBLOCK(m_data);
203 }
204 }
205
206 if (frames > 0) {
207 MUNLOCK_SAMPLEBLOCK(m_data);
208 }
209
210 return MAD_FLOW_CONTINUE;
211 }
212
213 enum mad_flow
214 MP3FileReader::error(void *dp,
215 struct mad_stream *stream,
216 struct mad_frame *)
217 {
218 DecoderData *data = (DecoderData *)dp;
219
220 fprintf(stderr, "decoding error 0x%04x (%s) at byte offset %u\n",
221 stream->error, mad_stream_errorstr(stream),
222 stream->this_frame - data->start);
223
224 return MAD_FLOW_CONTINUE;
225 }
226
227 #endif