svcore  1.9
MP3FileReader.h
Go to the documentation of this file.
00001 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*-  vi:set ts=8 sts=4 sw=4: */
00002 
00003 /*
00004     Sonic Visualiser
00005     An audio file viewer and annotation editor.
00006     Centre for Digital Music, Queen Mary, University of London.
00007     This file copyright 2006 Chris Cannam.
00008     
00009     This program is free software; you can redistribute it and/or
00010     modify it under the terms of the GNU General Public License as
00011     published by the Free Software Foundation; either version 2 of the
00012     License, or (at your option) any later version.  See the file
00013     COPYING included with this distribution for more information.
00014 */
00015 
00016 #ifndef _MP3_FILE_READER_H_
00017 #define _MP3_FILE_READER_H_
00018 
00019 #ifdef HAVE_MAD
00020 
00021 #include "CodedAudioFileReader.h"
00022 
00023 #include "base/Thread.h"
00024 #include <mad.h>
00025 
00026 #include <set>
00027 
00028 class ProgressReporter;
00029 
00030 class MP3FileReader : public CodedAudioFileReader
00031 {
00032     Q_OBJECT
00033 
00034 public:
00035     enum DecodeMode {
00036         DecodeAtOnce, // decode the file on construction, with progress
00037         DecodeThreaded // decode in a background thread after construction
00038     };
00039 
00040     MP3FileReader(FileSource source,
00041                   DecodeMode decodeMode,
00042                   CacheMode cacheMode,
00043                   int targetRate = 0,
00044                   bool normalised = false,
00045                   ProgressReporter *reporter = 0);
00046     virtual ~MP3FileReader();
00047 
00048     virtual QString getError() const { return m_error; }
00049 
00050     virtual QString getLocation() const { return m_source.getLocation(); }
00051     virtual QString getTitle() const { return m_title; }
00052     virtual QString getMaker() const { return m_maker; }
00053     virtual TagMap getTags() const { return m_tags; }
00054     
00055     static void getSupportedExtensions(std::set<QString> &extensions);
00056     static bool supportsExtension(QString ext);
00057     static bool supportsContentType(QString type);
00058     static bool supports(FileSource &source);
00059 
00060     virtual int getDecodeCompletion() const { return m_completion; }
00061 
00062     virtual bool isUpdating() const {
00063         return m_decodeThread && m_decodeThread->isRunning();
00064     }
00065 
00066 public slots:
00067     void cancelled();
00068 
00069 protected:
00070     FileSource m_source;
00071     QString m_path;
00072     QString m_error;
00073     QString m_title;
00074     QString m_maker;
00075     TagMap m_tags;
00076     int m_fileSize;
00077     double m_bitrateNum;
00078     int m_bitrateDenom;
00079     int m_completion;
00080     bool m_done;
00081 
00082     unsigned char *m_filebuffer;
00083     float **m_samplebuffer;
00084     int m_samplebuffersize;
00085 
00086     ProgressReporter *m_reporter;
00087     bool m_cancelled;
00088 
00089     struct DecoderData
00090     {
00091         unsigned char const *start;
00092         unsigned long length;
00093         MP3FileReader *reader;
00094     };
00095 
00096     bool decode(void *mm, int sz);
00097     enum mad_flow accept(struct mad_header const *, struct mad_pcm *);
00098 
00099     static enum mad_flow input(void *, struct mad_stream *);
00100     static enum mad_flow output(void *, struct mad_header const *, struct mad_pcm *);
00101     static enum mad_flow error(void *, struct mad_stream *, struct mad_frame *);
00102 
00103     class DecodeThread : public Thread
00104     {
00105     public:
00106         DecodeThread(MP3FileReader *reader) : m_reader(reader) { }
00107         virtual void run();
00108 
00109     protected:
00110         MP3FileReader *m_reader;
00111     };
00112 
00113     DecodeThread *m_decodeThread;
00114 
00115     void loadTags();
00116     QString loadTag(void *vtag, const char *name);
00117 };
00118 
00119 #endif
00120 
00121 #endif