MP3FileReader.h
Go to the documentation of this file.
1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
2 
3 /*
4  Sonic Visualiser
5  An audio file viewer and annotation editor.
6  Centre for Digital Music, Queen Mary, University of London.
7  This file copyright 2006 Chris Cannam.
8 
9  This program is free software; you can redistribute it and/or
10  modify it under the terms of the GNU General Public License as
11  published by the Free Software Foundation; either version 2 of the
12  License, or (at your option) any later version. See the file
13  COPYING included with this distribution for more information.
14 */
15 
16 #ifndef SV_MP3_FILE_READER_H
17 #define SV_MP3_FILE_READER_H
18 
19 #ifdef HAVE_MAD
20 
21 #include "CodedAudioFileReader.h"
22 
23 #include "base/Thread.h"
24 #include <mad.h>
25 
26 #include <set>
27 #include <atomic>
28 
29 class ProgressReporter;
30 
32 {
33  Q_OBJECT
34 
35 public:
41  enum class GaplessMode {
51  Gapless,
52 
66  Gappy
67  };
68 
70  DecodeMode decodeMode,
71  CacheMode cacheMode,
72  GaplessMode gaplessMode,
73  sv_samplerate_t targetRate = 0,
74  bool normalised = false,
75  ProgressReporter *reporter = 0);
76  virtual ~MP3FileReader();
77 
78  QString getError() const override { return m_error; }
79 
80  QString getLocation() const override { return m_source.getLocation(); }
81  QString getTitle() const override { return m_title; }
82  QString getMaker() const override { return m_maker; }
83  TagMap getTags() const override { return m_tags; }
84 
85  static void getSupportedExtensions(std::set<QString> &extensions);
86  static bool supportsExtension(QString ext);
87  static bool supportsContentType(QString type);
88  static bool supports(FileSource &source);
89 
90  int getDecodeCompletion() const override { return m_completion; }
91 
92  bool isUpdating() const override {
93  return m_decodeThread && m_decodeThread->isRunning();
94  }
95 
96 public slots:
97  void cancelled();
98 
99 protected:
101  QString m_path;
102  QString m_error;
103  QString m_title;
104  QString m_maker;
108  double m_bitrateNum;
112  bool m_done;
113 
114  unsigned char *m_fileBuffer;
116 
117  float **m_sampleBuffer;
119 
121  std::atomic<bool> m_cancelled;
122 
124 
125  struct DecoderData {
126  unsigned char const *start;
128  bool finished;
130  };
131 
132  bool decode(void *mm, sv_frame_t sz);
133  enum mad_flow filter(struct mad_stream const *, struct mad_frame *);
134  enum mad_flow accept(struct mad_header const *, struct mad_pcm *);
135 
136  static enum mad_flow input_callback(void *, struct mad_stream *);
137  static enum mad_flow output_callback(void *, struct mad_header const *,
138  struct mad_pcm *);
139  static enum mad_flow filter_callback(void *, struct mad_stream const *,
140  struct mad_frame *);
141  static enum mad_flow error_callback(void *, struct mad_stream *,
142  struct mad_frame *);
143 
144  class DecodeThread : public Thread
145  {
146  public:
147  DecodeThread(MP3FileReader *reader) : m_reader(reader) { }
148  void run() override;
149 
150  protected:
152  };
153 
155 
156  void loadTags(int fd);
157  QString loadTag(void *vtag, const char *name);
158 };
159 
160 #endif
161 
162 #endif
double sv_samplerate_t
Sample rate.
Definition: BaseTypes.h:51
QString getLocation() const override
Return the location of the audio data in the reader (as passed in to the FileSource constructor...
Definition: MP3FileReader.h:80
unsigned char const * start
QString loadTag(void *vtag, const char *name)
enum mad_flow filter(struct mad_stream const *, struct mad_frame *)
int64_t sv_frame_t
Frame index, the unit of our time axis.
Definition: BaseTypes.h:31
static void getSupportedExtensions(std::set< QString > &extensions)
QString getLocation() const
Return the location filename or URL as passed to the constructor.
Definition: FileSource.cpp:616
bool m_decodeErrorShown
GaplessMode
How the MP3FileReader should handle leading and trailing gaps.
Definition: MP3FileReader.h:41
QString getMaker() const override
Return the "maker" of the work in the audio file, if known.
Definition: MP3FileReader.h:82
float ** m_sampleBuffer
DecodeThread * m_decodeThread
unsigned char * m_fileBuffer
Do not trim any samples.
size_t m_sampleBufferSize
int getDecodeCompletion() const override
Return a percentage value indicating how far through decoding the audio file we are.
Definition: MP3FileReader.h:90
GaplessMode m_gaplessMode
bool isUpdating() const override
Return true if decoding is still in progress and the frame count may change.
Definition: MP3FileReader.h:92
enum mad_flow accept(struct mad_header const *, struct mad_pcm *)
FileSource m_source
static enum mad_flow filter_callback(void *, struct mad_stream const *, struct mad_frame *)
std::map< QString, QString > TagMap
Return any tag pairs picked up from the audio file.
MP3FileReader(FileSource source, DecodeMode decodeMode, CacheMode cacheMode, GaplessMode gaplessMode, sv_samplerate_t targetRate=0, bool normalised=false, ProgressReporter *reporter=0)
Trim unwanted samples from the start and end of the decoded audio.
bool decode(void *mm, sv_frame_t sz)
FileSource is a class used to refer to the contents of a file that may be either local or at a remote...
Definition: FileSource.h:59
void loadTags(int fd)
double m_bitrateNum
static enum mad_flow output_callback(void *, struct mad_header const *, struct mad_pcm *)
static bool supports(FileSource &source)
size_t m_fileBufferSize
sv_frame_t m_fileSize
Definition: Thread.h:24
QString getTitle() const override
Return the title of the work in the audio file, if known.
Definition: MP3FileReader.h:81
ProgressReporter * m_reporter
TagMap getTags() const override
Definition: MP3FileReader.h:83
static enum mad_flow error_callback(void *, struct mad_stream *, struct mad_frame *)
std::atomic< bool > m_cancelled
DecodeThread(MP3FileReader *reader)
static bool supportsExtension(QString ext)
QString getError() const override
If isOK() is false, return an error string.
Definition: MP3FileReader.h:78
virtual ~MP3FileReader()
static bool supportsContentType(QString type)
static enum mad_flow input_callback(void *, struct mad_stream *)