svcore  1.9
CodedAudioFileReader.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 _CODED_AUDIO_FILE_READER_H_
00017 #define _CODED_AUDIO_FILE_READER_H_
00018 
00019 #include "AudioFileReader.h"
00020 
00021 #include <sndfile.h>
00022 #include <QMutex>
00023 #include <QReadWriteLock>
00024 
00025 class WavFileReader;
00026 class Serialiser;
00027 class Resampler;
00028 
00029 class CodedAudioFileReader : public AudioFileReader
00030 {
00031     Q_OBJECT
00032 
00033 public:
00034     virtual ~CodedAudioFileReader();
00035 
00036     enum CacheMode {
00037         CacheInTemporaryFile,
00038         CacheInMemory
00039     };
00040 
00041     virtual void getInterleavedFrames(int start, int count,
00042                                       SampleBlock &frames) const;
00043 
00044     virtual int getNativeRate() const { return m_fileRate; }
00045 
00047     virtual bool isQuicklySeekable() const { return true; }
00048 
00049 signals:
00050     void progress(int);
00051 
00052 protected:
00053     CodedAudioFileReader(CacheMode cacheMode, 
00054                          int targetRate,
00055                          bool normalised);
00056 
00057     void initialiseDecodeCache(); // samplerate, channels must have been set
00058 
00059     // may throw InsufficientDiscSpace:
00060     void addSamplesToDecodeCache(float **samples, int nframes);
00061     void addSamplesToDecodeCache(float *samplesInterleaved, int nframes);
00062     void addSamplesToDecodeCache(const SampleBlock &interleaved);
00063 
00064     // may throw InsufficientDiscSpace:
00065     void finishDecodeCache();
00066 
00067     bool isDecodeCacheInitialised() const { return m_initialised; }
00068 
00069     void startSerialised(QString id);
00070     void endSerialised();
00071 
00072 private:
00073     void pushBuffer(float *interleaved, int sz, bool final);
00074     void pushBufferResampling(float *interleaved, int sz, float ratio, bool final);
00075     void pushBufferNonResampling(float *interleaved, int sz);
00076 
00077 protected:
00078     QMutex m_cacheMutex;
00079     CacheMode m_cacheMode;
00080     SampleBlock m_data;
00081     mutable QReadWriteLock m_dataLock;
00082     bool m_initialised;
00083     Serialiser *m_serialiser;
00084     int m_fileRate;
00085 
00086     QString m_cacheFileName;
00087     SNDFILE *m_cacheFileWritePtr;
00088     WavFileReader *m_cacheFileReader;
00089     float *m_cacheWriteBuffer;
00090     int m_cacheWriteBufferIndex;
00091     int m_cacheWriteBufferSize; // frames
00092 
00093     Resampler *m_resampler;
00094     float *m_resampleBuffer;
00095     int m_fileFrameCount;
00096 
00097     bool m_normalised;
00098     float m_max;
00099     float m_gain;
00100 };
00101 
00102 #endif