Main Page   Namespace List   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   Related Pages  

wav_reader.h

00001 //----------------------------------------------------------------------------
00008 //----------------------------------------------------------------------------
00009 #ifndef WAVREADER_H_INCLUDED
00010 #define WAVREADER_H_INCLUDED
00011 //----------------------------------------------------------------------------
00012 #include <string>
00013 #include <fstream>
00014 #include "datatypes.h"
00015 //----------------------------------------------------------------------------
00016 namespace ac3 {
00017 //----------------------------------------------------------------------------
00018 #define WAVREADER_UNKNOWN_ERROR -1            
00019 #define WAVREADER_NO_ERROR 0
00020 #define WAVREADER_FILE_OPEN_ERROR 1
00021 #define WAVREADER_FILE_NOT_OPEN 2
00022 #define WAVREADER_READ_ERROR 3
00023 #define WAVREADER_INVALID_FORMAT 4
00024 #define WAVREADER_UNSUPPORTED_FORMAT 5
00025 #define WAVREADER_CHUNK_NOT_FOUND 6
00026 #define WAVREADER_SAMPLE_DOES_NOT_EXIST 7
00027 #define WAVREADER_CHANNEL_DOES_NOT_EXIST 8
00028 
00029 #define WAVREADER_ERROR_NUM 9
00030 
00038 class CWAVReaderException
00039 {
00040 public:
00042   CWAVReaderException() : m_nReason(WAVREADER_UNKNOWN_ERROR) {}
00044   CWAVReaderException(int nReason) : m_nReason(nReason) {}
00046   CWAVReaderException(int nReason, const std::string& sHint)
00047     : m_nReason(nReason), m_sHint(sHint) {}
00048   
00049 public:
00051   int GetReason() const { return m_nReason; }
00053   std::string GetHint() const { return m_sHint; }
00054   std::string GetString() const;
00055   
00056 private:
00057   int m_nReason;
00058   std::string m_sHint;
00059 
00060   static std::string m_sUnknownError;
00061   static std::string m_sError[WAVREADER_ERROR_NUM];
00062 };
00063 //----------------------------------------------------------------------------
00078 class CWAVReader
00079 {
00080 public:
00081   CWAVReader();
00082   CWAVReader(const char* pszFilename);
00083   CWAVReader(const std::string& sFilename);
00084 private:
00085   CWAVReader(const CWAVReader& r) {}
00086 public:
00087   ~CWAVReader();
00088   
00089 public:
00090   void OpenFile(const std::string& sFilename);
00091   void OpenFile(const char* pszFilename);
00092   void CloseFile();
00093 
00094   void GetSamplesAs8Bit(UINT32 nCount, UINT16 nChannel,
00095                         UINT8* pnBuffer, UINT32 nStart = (UINT32)-1);  
00096   void GetSamplesAs16Bit(UINT32 nCount, UINT16 nChannel,
00097                          INT16* pnBuffer, UINT32 nStart = (UINT32)-1);
00098   void GetSamplesAs24Bit(UINT32 nCount, UINT16 nChannel,
00099                          INT32* pnBuffer, UINT32 nStart = (UINT32)-1);
00100   void GetSamplesAs32Bit(UINT32 nCount, UINT16 nChannel,
00101                          INT32* pnBuffer, UINT32 nStart = (UINT32)-1);
00102 
00103   void SetChannelPos(UINT16 nChannel, UINT32 nNewPos);
00104   UINT32 GetChannelPos(UINT16 nChannel);
00105   UINT32 SamplesLeft(UINT16 nChannel);
00106 
00107   UINT32 GetSampleRate() const;
00108   UINT16 GetChannelCount() const;
00109   UINT16 GetBitsPerSample() const;
00110   UINT32 GetSampleCount() const;
00111   
00112   UINT32 TimeToSample(double dSec) const;
00113   double SampleToTime(UINT32 nSample) const;
00114 
00115 private:
00116   void ParseHeader();
00117   UINT32 SeekToSubchunk(const std::string& sSubchunk);
00118   void SeekToSample(UINT32 nSample);
00119   bool SampleExists(UINT32 nSample) const;
00120   
00121   void Read8BitSamples(UINT32 nStart, UINT32 nCount, UINT16 nChannel,
00122                        UINT8* pnBuffer);
00123   void Read16BitSamples(UINT32 nStart, UINT32 nCount, UINT16 nChannel,
00124                         INT16* pnBuffer);
00125   void Read24BitSamples(UINT32 nStart, UINT32 nCount, UINT16 nChannel,
00126                         INT32* pnBuffer);
00127   void Read32BitSamples(UINT32 nStart, UINT32 nCount, UINT16 nChannel,
00128                         INT32* pnBuffer);
00129 
00130   std::string ReadFOURCC();
00131   INT8 ReadINT8();
00132   UINT8 ReadUINT8();
00133   INT16 ReadINT16();  
00134   UINT16 ReadUINT16();
00135   INT32 ReadINT24();
00136   UINT32 ReadUINT24();
00137   INT32 ReadINT32();
00138   UINT32 ReadUINT32();
00139   
00140   CWAVReader& operator=(const CWAVReader& r) { return *this; }
00141   
00142 private:
00143   bool m_bFileOpen;
00144   std::ifstream m_file;
00145   UINT32 m_nRiffChunkDataSize;
00146   
00147   UINT16 m_nChannels;
00148   UINT32 m_nSampleRate;
00149   UINT16 m_nBitsPerSample;
00150 
00151   std::streampos m_dataPos;
00152   UINT32 m_nDataSize;
00153 
00154   std::vector<UINT32> m_channelPos;
00155 };
00156 //----------------------------------------------------------------------------
00157 } // namespace ac3
00158 //----------------------------------------------------------------------------
00159 #endif // WAVREADER_H_INCLUDED

Generated on Sat Sep 25 14:06:42 2004 for ac3 by doxygen1.2.18