pwavfile.h

Go to the documentation of this file.
00001 /*
00002  * pwavfile.h
00003  *
00004  * WAV file I/O channel class.
00005  *
00006  * Portable Windows Library
00007  *
00008  * Copyright (c) 2001 Equivalence Pty. Ltd.
00009  *
00010  * The contents of this file are subject to the Mozilla Public License
00011  * Version 1.0 (the "License"); you may not use this file except in
00012  * compliance with the License. You may obtain a copy of the License at
00013  * http://www.mozilla.org/MPL/
00014  *
00015  * Software distributed under the License is distributed on an "AS IS"
00016  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
00017  * the License for the specific language governing rights and limitations
00018  * under the License.
00019  *
00020  * The Original Code is Portable Windows Library.
00021  *
00022  * The Initial Developer of the Original Code is
00023  * Roger Hardiman <roger@freebsd.org>
00024  * and Shawn Pai-Hsiang Hsiao <shawn@eecs.harvard.edu>
00025  *
00026  * All Rights Reserved.
00027  *
00028  * Contributor(s): ______________________________________.
00029  *
00030  * $Log: pwavfile.h,v $
00031  * Revision 1.27  2006/08/08 08:14:59  rjongbloed
00032  * Fixed GCC warnings on packed structures that do not need packing.
00033  *
00034  * Revision 1.26  2006/06/21 05:19:38  csoutheren
00035  * Fixed build with latest PWLib
00036  *
00037  * Revision 1.25  2006/04/10 23:57:25  csoutheren
00038  * Checked in changes to remove some warnings with gcc effc++ flag
00039  *
00040  * Revision 1.24  2006/04/06 00:39:37  csoutheren
00041  * Ensure autoconvert format is preserved across file close
00042  *
00043  * Revision 1.23  2005/11/30 12:47:37  csoutheren
00044  * Removed tabs, reformatted some code, and changed tags for Doxygen
00045  *
00046  * Revision 1.22  2005/10/30 23:25:51  csoutheren
00047  * Fixed formatting
00048  * Removed throw() declarations (PWLib does not do exceptions)
00049  * Removed duplicate destructor declarations and definitions
00050  *
00051  * Revision 1.21  2005/10/30 19:41:53  dominance
00052  * fixed most of the warnings occuring during compilation
00053  *
00054  * Revision 1.20  2005/06/09 00:33:12  csoutheren
00055  * Fixed crash problem caused by recent leak fix
00056  * Removed bogus error when reading all of file contents in a single read
00057  *
00058  * Revision 1.19  2005/05/12 05:25:04  csoutheren
00059  * PWAVFile format abstract factory must use a PCaseless as a key
00060  *
00061  * Revision 1.18  2005/03/19 02:52:53  csoutheren
00062  * Fix warnings from gcc 4.1-20050313 shapshot
00063  *
00064  * Revision 1.17  2005/01/04 07:44:02  csoutheren
00065  * More changes to implement the new configuration methodology, and also to
00066  * attack the global static problem
00067  *
00068  * Revision 1.16  2004/11/11 07:34:50  csoutheren
00069  * Added #include <ptlib.h>
00070  *
00071  * Revision 1.15  2004/11/08 04:07:40  csoutheren
00072  * Fixed crash opportunity under some conditions
00073  * Fixed incorrect WAV file type display
00074  *
00075  * Revision 1.14  2004/07/15 03:12:41  csoutheren
00076  * Migrated changes from crs_vxnml_devel branch into main trunk
00077  *
00078  * Revision 1.13.4.4  2004/07/13 08:13:04  csoutheren
00079  * Lots of implementation of factory-based PWAVFile
00080  *
00081  * Revision 1.13.4.3  2004/07/12 09:17:19  csoutheren
00082  * Fixed warnings and errors under Linux
00083  *
00084  * Revision 1.13.4.2  2004/07/12 08:30:16  csoutheren
00085  * More fixes for abstract factory implementation of PWAVFile
00086  *
00087  * Revision 1.13.4.1  2004/07/07 07:07:41  csoutheren
00088  * Changed PWAVFile to use abstract factories (extensively)
00089  * Removed redundant blocking/unblocking when using G.723.1
00090  * More support for call transfer
00091  *
00092  * Revision 1.13  2003/03/07 06:12:05  robertj
00093  * Added more WAV file "magic numbers".
00094  *
00095  * Revision 1.12  2002/09/16 01:08:59  robertj
00096  * Added #define so can select if #pragma interface/implementation is used on
00097  *   platform basis (eg MacOS) rather than compiler, thanks Robert Monaghan.
00098  *
00099  * Revision 1.11  2002/06/20 00:51:38  craigs
00100  * Added virtuals to allow overriding
00101  *
00102  * Revision 1.10  2002/05/21 01:56:53  robertj
00103  * Removed the enum which made yet another set of magic numbers for audio
00104  *   formats, now uses the WAV file format numbers.
00105  * Fixed failure to write header when destroying object without and explicit
00106  *   call to Close().
00107  * Fixed missing Open() function which does not have file name parameter.
00108  * Added ability to set the audio format after construction.
00109  *
00110  * Revision 1.9  2002/01/22 03:55:07  craigs
00111  * Added #define guards when file moved to PTCLib
00112  *
00113  * Revision 1.8  2002/01/13 21:00:41  rogerh
00114  * The type of new .WAV files must now be specified in the class constructor.
00115  * Take out Open() function from the last commit and create a new Open()
00116  * function which replaces the one in the PFile base class.
00117  *
00118  * Revision 1.7  2002/01/11 16:33:46  rogerh
00119  * Create a PWAVFile Open() function, which processes the WAV header
00120  *
00121  * Revision 1.6  2001/10/16 13:27:37  rogerh
00122  * Add support for writing G.723.1 WAV files.
00123  * MS Windows can play G.723.1 WAV Files in Media Player and Sound Recorder.
00124  * Sound Recorder can also convert them to normal PCM format WAV files.
00125  * Thanks go to M.Stoychev <M.Stoychev@cnsys.bg> for sample WAV files.
00126  *
00127  * Revision 1.5  2001/10/15 11:48:15  rogerh
00128  * Add GetFormat to return the format of a WAV file
00129  *
00130  * Revision 1.4  2001/07/23 01:20:20  rogerh
00131  * Add updates from Shawn - ensure isvalidWAV is false for zero length files.
00132  * GetDataLength uses actual file size to support file updates as well as appends.
00133  * Add updates from Roger - Update Header() just writes to specific fields which
00134  * preserves any 'extra' data in an existing header between FORMAT and DATA chunks.
00135  *
00136  * Revision 1.3  2001/07/20 07:06:27  rogerh
00137  * Fix a typo
00138  *
00139  * Revision 1.2  2001/07/20 03:30:59  robertj
00140  * Minor cosmetic changes to new PWAVFile class.
00141  *
00142  * Revision 1.1  2001/07/19 09:55:48  rogerh
00143  * Add PWAVFile, a class to read and write .wav files, written by
00144  * Roger Hardiman and <roger@freebsd.org> and
00145  * Shawn Pai-Hsiang Hsiao <shawn@eecs.harvard.edu>
00146  *
00147  *
00148  */
00149 
00150 #ifndef _PWAVFILE
00151 #define _PWAVFILE
00152 
00153 //#ifdef P_USE_PRAGMA
00154 //#pragma interface
00155 //#endif
00156 
00157 #include <ptlib.h>
00158 #include <ptlib/pfactory.h>
00159 
00160 class PWAVFile;
00161 
00162 namespace PWAV {
00163 
00164 #ifdef __GNUC__
00165 #define P_PACKED    __attribute__ ((packed));
00166 #else
00167 #define P_PACKED
00168 #pragma pack(1)
00169 #endif
00170 
00171 struct ChunkHeader
00172 {
00173   char    tag[4];
00174   PInt32l len    P_PACKED;
00175 };
00176 
00177 struct RIFFChunkHeader 
00178 {
00179   ChunkHeader hdr;
00180   char        tag[4];
00181 };
00182 
00183 struct FMTChunk
00184 {
00185   ChunkHeader hdr;                    
00186   PUInt16l format          P_PACKED;  
00187   PUInt16l numChannels     P_PACKED;  
00188   PUInt32l sampleRate      P_PACKED;  
00189   PUInt32l bytesPerSec     P_PACKED;  
00190   PUInt16l bytesPerSample  P_PACKED;  
00191   PUInt16l bitsPerSample   P_PACKED;  
00192 };
00193 
00194 }; // namespace PWAV
00195 
00196 #ifdef __GNUC__
00197 #undef P_PACKED
00198 #else
00199 #pragma pack()
00200 #endif
00201 
00205 class PWAVFileFormat
00206 {
00207   public:
00208     virtual ~PWAVFileFormat() { }
00209 
00213     virtual unsigned GetFormat() const = 0;
00214 
00218     virtual PString GetFormatString() const = 0;
00219 
00223     virtual PString GetDescription() const = 0;
00224 
00228     virtual void CreateHeader(PWAV::FMTChunk & header, PBYTEArray & extendedHeader) = 0;
00229 
00233     virtual BOOL WriteExtraChunks(PWAVFile & /*file*/)
00234     { return TRUE; }
00235 
00239     virtual BOOL ReadExtraChunks(PWAVFile & /*file*/)
00240     { return TRUE; }
00241 
00245     virtual void OnStart()
00246     { }
00247 
00251     virtual void OnStop()
00252     { }
00253 
00257     virtual BOOL Read(PWAVFile & file, void * buf, PINDEX & len);
00258 
00262     virtual BOOL Write(PWAVFile & file, const void * buf, PINDEX & len);
00263 };
00264 
00265 typedef PFactory<PWAVFileFormat, PCaselessString> PWAVFileFormatByFormatFactory;
00266 typedef PFactory<PWAVFileFormat, unsigned> PWAVFileFormatByIDFactory;
00267 
00271 class PWAVFileConverter 
00272 {
00273   public:
00274     virtual ~PWAVFileConverter() { }
00275     virtual unsigned GetFormat    (const PWAVFile & file) const = 0;
00276     virtual off_t GetPosition     (const PWAVFile & file) const = 0;
00277     virtual BOOL SetPosition      (PWAVFile & file, off_t pos, PFile::FilePositionOrigin origin) = 0;
00278     virtual unsigned GetSampleSize(const PWAVFile & file) const = 0;
00279     virtual off_t GetDataLength   (PWAVFile & file) = 0;
00280     virtual BOOL Read             (PWAVFile & file, void * buf, PINDEX len)  = 0;
00281     virtual BOOL Write            (PWAVFile & file, const void * buf, PINDEX len) = 0;
00282 };
00283 
00284 typedef PFactory<PWAVFileConverter, unsigned> PWAVFileConverterFactory;
00285 
00288 class PWAVFile : public PFile
00289 {
00290   PCLASSINFO(PWAVFile, PFile);
00291 
00292   public:
00298     enum {
00299       fmt_PCM         = 1,      
00300       fmt_ALaw        = 6,      
00301       fmt_uLaw        = 7,      
00302       fmt_GSM         = 0x31,   
00303       fmt_G728        = 0x41,   
00304       fmt_G723        = 0x42,   
00305       fmt_MSG7231     = 0x42,   
00306       fmt_G726        = 0x64,   
00307       fmt_G722        = 0x65,   
00308       fmt_G729        = 0x84,   
00309       fmt_VivoG7231   = 0x111,  
00310 
00311       // For backward compatibility
00312       PCM_WavFile     = fmt_PCM,
00313       G7231_WavFile   = fmt_VivoG7231,
00314 
00315       // allow opening files without knowing the format
00316       fmt_NotKnown    = 0x10000
00317     };
00318 
00328     PWAVFile(
00329       unsigned format = fmt_PCM 
00330     );
00331     static PWAVFile * format(
00332       const PString & format    
00333     );
00334 
00347     PWAVFile(
00348       OpenMode mode,          
00349       int opts = ModeDefault, 
00350       unsigned format = fmt_PCM 
00351     );
00352     static PWAVFile * format(
00353       const PString & format,  
00354       PFile::OpenMode mode,          
00355       int opts = PFile::ModeDefault 
00356     );
00357 
00367     PWAVFile(
00368       const PFilePath & name,     
00369       OpenMode mode = ReadWrite,  
00370       int opts = ModeDefault,     
00371       unsigned format = fmt_PCM 
00372     );
00373     PWAVFile(
00374       const PString & format,  
00375       const PFilePath & name,     
00376       OpenMode mode = PFile::ReadWrite,  
00377       int opts = PFile::ModeDefault     
00378     );
00379 
00382     ~PWAVFile();
00384 
00394     virtual BOOL Read(
00395       void * buf,   
00396       PINDEX len    
00397     );
00398 
00406     virtual BOOL Write(
00407       const void * buf,   
00408       PINDEX len    
00409     );
00410 
00422     virtual BOOL Open(
00423       OpenMode mode = ReadWrite,  
00424       int opts = ModeDefault      
00425     );
00426 
00440     virtual BOOL Open(
00441       const PFilePath & name,    
00442       OpenMode mode = ReadWrite, 
00443       int opts = ModeDefault     
00444     );
00445 
00451     virtual BOOL Close();
00452 
00467     virtual BOOL SetPosition(
00468       off_t pos,                         
00469       FilePositionOrigin origin = Start  
00470     );
00471 
00479     virtual off_t GetPosition() const;
00481 
00486     virtual BOOL SetFormat(unsigned fmt);
00487     virtual BOOL SetFormat(const PString & format);
00488 
00491     virtual unsigned GetFormat() const;
00492     virtual PString GetFormatAsString() const;
00493 
00497     virtual unsigned GetChannels() const;
00498     virtual void SetChannels(unsigned v);
00499 
00502     virtual unsigned GetSampleRate() const;
00503     virtual void SetSampleRate(unsigned v);
00504 
00507     virtual unsigned GetSampleSize() const;
00508     virtual void SetSampleSize(unsigned v);
00509 
00512     off_t GetHeaderLength() const;
00513 
00516     virtual off_t GetDataLength();
00517 
00524     BOOL IsValid() const { return isValidWAV; }
00525 
00529     PString GetFormatString() const
00530     { if (formatHandler == NULL) return PString("N/A"); else return formatHandler->GetFormatString(); }
00531 
00535     void SetAutoconvert();
00536 
00538  
00539     friend class PWAVFileConverter;
00540 
00541     BOOL RawRead(void * buf, PINDEX len);
00542     BOOL RawWrite(const void * buf, PINDEX len);
00543 
00544     BOOL FileRead(void * buf, PINDEX len);
00545     BOOL FileWrite(const void * buf, PINDEX len);
00546 
00547     off_t RawGetPosition() const;
00548     BOOL RawSetPosition(off_t pos, FilePositionOrigin origin);
00549     off_t RawGetDataLength();
00550 
00551     void SetLastReadCount(PINDEX v) { lastReadCount = v; } 
00552 
00553     PWAV::FMTChunk wavFmtChunk;
00554     PBYTEArray extendedHeader;
00555 
00556   protected:
00557     void Construct();
00558     void SelectFormat(unsigned fmt);
00559     void SelectFormat(const PString & format);
00560 
00561     PBYTEArray wavHeaderData;
00562 
00563     BOOL ProcessHeader();
00564     BOOL GenerateHeader();
00565     BOOL UpdateHeader();
00566 
00567     BOOL     isValidWAV;
00568 
00569     unsigned int origFmt;
00570     PWAVFileFormat * formatHandler;
00571 
00572     BOOL     autoConvert;
00573     PWAVFileConverter * autoConverter;
00574 
00575     off_t lenHeader;
00576     off_t lenData;
00577 
00578     BOOL     header_needs_updating;
00579 };
00580 
00581 #endif
00582 
00583 // End Of File ///////////////////////////////////////////////////////////////

Generated on Sun Sep 6 03:50:33 2009 for PWLib by  doxygen 1.6.1