Chris@148
|
1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
|
Chris@148
|
2
|
Chris@148
|
3 /*
|
Chris@148
|
4 Sonic Visualiser
|
Chris@148
|
5 An audio file viewer and annotation editor.
|
Chris@148
|
6 Centre for Digital Music, Queen Mary, University of London.
|
Chris@148
|
7 This file copyright 2006 Chris Cannam.
|
Chris@148
|
8
|
Chris@148
|
9 This program is free software; you can redistribute it and/or
|
Chris@148
|
10 modify it under the terms of the GNU General Public License as
|
Chris@148
|
11 published by the Free Software Foundation; either version 2 of the
|
Chris@148
|
12 License, or (at your option) any later version. See the file
|
Chris@148
|
13 COPYING included with this distribution for more information.
|
Chris@148
|
14 */
|
Chris@148
|
15
|
Chris@1581
|
16 #ifndef SV_MP3_FILE_READER_H
|
Chris@1581
|
17 #define SV_MP3_FILE_READER_H
|
Chris@148
|
18
|
Chris@148
|
19 #ifdef HAVE_MAD
|
Chris@148
|
20
|
Chris@148
|
21 #include "CodedAudioFileReader.h"
|
Chris@148
|
22
|
Chris@263
|
23 #include "base/Thread.h"
|
Chris@148
|
24 #include <mad.h>
|
Chris@148
|
25
|
Chris@157
|
26 #include <set>
|
Chris@1858
|
27 #include <atomic>
|
Chris@157
|
28
|
Chris@392
|
29 class ProgressReporter;
|
Chris@148
|
30
|
Chris@148
|
31 class MP3FileReader : public CodedAudioFileReader
|
Chris@148
|
32 {
|
Chris@392
|
33 Q_OBJECT
|
Chris@392
|
34
|
Chris@148
|
35 public:
|
Chris@1305
|
36 /**
|
Chris@1305
|
37 * How the MP3FileReader should handle leading and trailing gaps.
|
Chris@1305
|
38 * See http://lame.sourceforge.net/tech-FAQ.txt for a technical
|
Chris@1305
|
39 * explanation of the numbers here.
|
Chris@1305
|
40 */
|
Chris@1313
|
41 enum class GaplessMode {
|
Chris@1305
|
42 /**
|
Chris@1305
|
43 * Trim unwanted samples from the start and end of the decoded
|
Chris@1305
|
44 * audio. From the start, trim a number of samples equal to
|
Chris@1305
|
45 * the decoder delay (a fixed 529 samples) plus any encoder
|
Chris@1305
|
46 * delay that may be specified in Xing/LAME metadata. From the
|
Chris@1305
|
47 * end, trim any padding specified in Xing/LAME metadata, less
|
Chris@1305
|
48 * the fixed decoder delay. This usually results in "gapless"
|
Chris@1305
|
49 * audio, i.e. with no spurious zero padding at either end.
|
Chris@1305
|
50 */
|
Chris@1305
|
51 Gapless,
|
Chris@1305
|
52
|
Chris@1305
|
53 /**
|
Chris@1305
|
54 * Do not trim any samples. Also do not suppress any frames
|
Chris@1305
|
55 * from being passed to the mp3 decoder, even Xing/LAME
|
Chris@1305
|
56 * metadata frames. This will result in the audio being padded
|
Chris@1305
|
57 * with zeros at either end: at the start, typically
|
Chris@1305
|
58 * 529+576+1152 = 2257 samples for LAME-encoded mp3s; at the
|
Chris@1305
|
59 * end an unknown number depending on the fill ratio of the
|
Chris@1305
|
60 * final coded frame, but typically less than 1152-529 = 623.
|
Chris@1305
|
61 *
|
Chris@1305
|
62 * This mode produces the same output as produced by older
|
Chris@1305
|
63 * versions of this code before the gapless option was added,
|
Chris@1305
|
64 * and is present mostly for backward compatibility.
|
Chris@1305
|
65 */
|
Chris@1305
|
66 Gappy
|
Chris@1305
|
67 };
|
Chris@1305
|
68
|
Chris@317
|
69 MP3FileReader(FileSource source,
|
Chris@297
|
70 DecodeMode decodeMode,
|
Chris@297
|
71 CacheMode cacheMode,
|
Chris@1305
|
72 GaplessMode gaplessMode,
|
Chris@1040
|
73 sv_samplerate_t targetRate = 0,
|
Chris@920
|
74 bool normalised = false,
|
Chris@392
|
75 ProgressReporter *reporter = 0);
|
Chris@148
|
76 virtual ~MP3FileReader();
|
Chris@148
|
77
|
Chris@1580
|
78 QString getError() const override { return m_error; }
|
Chris@290
|
79
|
Chris@1580
|
80 QString getLocation() const override { return m_source.getLocation(); }
|
Chris@1580
|
81 QString getTitle() const override { return m_title; }
|
Chris@1580
|
82 QString getMaker() const override { return m_maker; }
|
Chris@1580
|
83 TagMap getTags() const override { return m_tags; }
|
Chris@271
|
84
|
Chris@290
|
85 static void getSupportedExtensions(std::set<QString> &extensions);
|
Chris@316
|
86 static bool supportsExtension(QString ext);
|
Chris@316
|
87 static bool supportsContentType(QString type);
|
Chris@317
|
88 static bool supports(FileSource &source);
|
Chris@316
|
89
|
Chris@1580
|
90 int getDecodeCompletion() const override { return m_completion; }
|
Chris@265
|
91
|
Chris@1580
|
92 bool isUpdating() const override {
|
Chris@263
|
93 return m_decodeThread && m_decodeThread->isRunning();
|
Chris@263
|
94 }
|
Chris@263
|
95
|
Chris@392
|
96 public slots:
|
Chris@392
|
97 void cancelled();
|
Chris@392
|
98
|
Chris@148
|
99 protected:
|
Chris@317
|
100 FileSource m_source;
|
Chris@290
|
101 QString m_path;
|
Chris@290
|
102 QString m_error;
|
Chris@290
|
103 QString m_title;
|
Chris@333
|
104 QString m_maker;
|
Chris@632
|
105 TagMap m_tags;
|
Chris@1305
|
106 GaplessMode m_gaplessMode;
|
Chris@1038
|
107 sv_frame_t m_fileSize;
|
Chris@148
|
108 double m_bitrateNum;
|
Chris@929
|
109 int m_bitrateDenom;
|
Chris@1288
|
110 int m_mp3FrameCount;
|
Chris@265
|
111 int m_completion;
|
Chris@263
|
112 bool m_done;
|
Chris@263
|
113
|
Chris@1290
|
114 unsigned char *m_fileBuffer;
|
Chris@1290
|
115 size_t m_fileBufferSize;
|
Chris@1290
|
116
|
Chris@1290
|
117 float **m_sampleBuffer;
|
Chris@1290
|
118 size_t m_sampleBufferSize;
|
Chris@148
|
119
|
Chris@392
|
120 ProgressReporter *m_reporter;
|
Chris@1858
|
121 std::atomic<bool> m_cancelled;
|
Chris@148
|
122
|
Chris@1284
|
123 bool m_decodeErrorShown;
|
Chris@1284
|
124
|
Chris@1290
|
125 struct DecoderData {
|
Chris@1429
|
126 unsigned char const *start;
|
Chris@1429
|
127 sv_frame_t length;
|
Chris@1310
|
128 bool finished;
|
Chris@1429
|
129 MP3FileReader *reader;
|
Chris@148
|
130 };
|
Chris@148
|
131
|
Chris@1038
|
132 bool decode(void *mm, sv_frame_t sz);
|
Chris@1288
|
133 enum mad_flow filter(struct mad_stream const *, struct mad_frame *);
|
Chris@148
|
134 enum mad_flow accept(struct mad_header const *, struct mad_pcm *);
|
Chris@148
|
135
|
Chris@1288
|
136 static enum mad_flow input_callback(void *, struct mad_stream *);
|
Chris@1288
|
137 static enum mad_flow output_callback(void *, struct mad_header const *,
|
Chris@1288
|
138 struct mad_pcm *);
|
Chris@1288
|
139 static enum mad_flow filter_callback(void *, struct mad_stream const *,
|
Chris@1288
|
140 struct mad_frame *);
|
Chris@1288
|
141 static enum mad_flow error_callback(void *, struct mad_stream *,
|
Chris@1288
|
142 struct mad_frame *);
|
Chris@263
|
143
|
Chris@263
|
144 class DecodeThread : public Thread
|
Chris@263
|
145 {
|
Chris@263
|
146 public:
|
Chris@263
|
147 DecodeThread(MP3FileReader *reader) : m_reader(reader) { }
|
Chris@1580
|
148 void run() override;
|
Chris@263
|
149
|
Chris@263
|
150 protected:
|
Chris@263
|
151 MP3FileReader *m_reader;
|
Chris@263
|
152 };
|
Chris@263
|
153
|
Chris@263
|
154 DecodeThread *m_decodeThread;
|
Chris@271
|
155
|
Chris@1348
|
156 void loadTags(int fd);
|
Chris@333
|
157 QString loadTag(void *vtag, const char *name);
|
Chris@148
|
158 };
|
Chris@148
|
159
|
Chris@148
|
160 #endif
|
Chris@148
|
161
|
Chris@148
|
162 #endif
|