00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098
00099
00100
00101
00102
00103
00104
00105
00106
00107
00108
00109
00110
00111
00112
00113
00114
00115
00116
00117
00118
00119
00120
00121
00122
00123
00124
00125
00126
00127
00128
00129
00130
00131
00132
00133
00134
00135
00136
00137
00138
00139
00140
00141
00142
00143
00144
00145
00146
00147
00148
00149
00150
00151
00152
00153
00154
00155
00156
00157
00158
00159
00160
00161
00162
00163
00164 #ifndef _PWAVFILE
00165 #define _PWAVFILE
00166
00167
00168
00169
00170
00171 #ifndef _PTLIB_H
00172 #include <ptlib.h>
00173 #endif
00174
00175 #include <ptlib/pfactory.h>
00176
00177 class PWAVFile;
00178
00179 namespace PWAV {
00180
00181 #ifdef __GNUC__
00182 #define P_PACKED __attribute__ ((packed));
00183 #else
00184 #define P_PACKED
00185 #pragma pack(1)
00186 #endif
00187
00188 struct ChunkHeader
00189 {
00190 char tag[4];
00191 PInt32l len P_PACKED;
00192 };
00193
00194 struct RIFFChunkHeader
00195 {
00196 ChunkHeader hdr;
00197 char tag[4];
00198 };
00199
00200 struct FMTChunk
00201 {
00202 ChunkHeader hdr;
00203 PUInt16l format P_PACKED;
00204 PUInt16l numChannels P_PACKED;
00205 PUInt32l sampleRate P_PACKED;
00206 PUInt32l bytesPerSec P_PACKED;
00207 PUInt16l bytesPerSample P_PACKED;
00208 PUInt16l bitsPerSample P_PACKED;
00209 };
00210
00211 };
00212
00213 #ifdef __GNUC__
00214 #undef P_PACKED
00215 #else
00216 #pragma pack()
00217 #endif
00218
00222 class PWAVFileFormat
00223 {
00224 public:
00225 virtual ~PWAVFileFormat() { }
00226
00230 virtual unsigned GetFormat() const = 0;
00231
00235 virtual PString GetFormatString() const = 0;
00236
00240 virtual PString GetDescription() const = 0;
00241
00245 virtual void CreateHeader(PWAV::FMTChunk & header, PBYTEArray & extendedHeader) = 0;
00246
00250 virtual void UpdateHeader(PWAV::FMTChunk & , PBYTEArray & )
00251 { }
00252
00256 virtual BOOL WriteExtraChunks(PWAVFile & )
00257 { return TRUE; }
00258
00262 virtual BOOL ReadExtraChunks(PWAVFile & )
00263 { return TRUE; }
00264
00268 virtual void OnStart()
00269 { }
00270
00274 virtual void OnStop()
00275 { }
00276
00280 virtual BOOL Read(PWAVFile & file, void * buf, PINDEX & len);
00281
00285 virtual BOOL Write(PWAVFile & file, const void * buf, PINDEX & len);
00286 };
00287
00288 typedef PFactory<PWAVFileFormat, PCaselessString> PWAVFileFormatByFormatFactory;
00289 typedef PFactory<PWAVFileFormat, unsigned> PWAVFileFormatByIDFactory;
00290
00294 class PWAVFileConverter
00295 {
00296 public:
00297 virtual ~PWAVFileConverter() { }
00298 virtual unsigned GetFormat (const PWAVFile & file) const = 0;
00299 virtual off_t GetPosition (const PWAVFile & file) const = 0;
00300 virtual BOOL SetPosition (PWAVFile & file, off_t pos, PFile::FilePositionOrigin origin) = 0;
00301 virtual unsigned GetSampleSize(const PWAVFile & file) const = 0;
00302 virtual off_t GetDataLength (PWAVFile & file) = 0;
00303 virtual BOOL Read (PWAVFile & file, void * buf, PINDEX len) = 0;
00304 virtual BOOL Write (PWAVFile & file, const void * buf, PINDEX len) = 0;
00305 };
00306
00307 typedef PFactory<PWAVFileConverter, unsigned> PWAVFileConverterFactory;
00308
00311 class PWAVFile : public PFile
00312 {
00313 PCLASSINFO(PWAVFile, PFile);
00314
00315 public:
00321 enum {
00322 fmt_PCM = 1,
00323 fmt_MSADPCM = 2,
00324 fmt_ALaw = 6,
00325 fmt_uLaw = 7,
00326 fmt_VOXADPCM = 0x10,
00327 fmt_IMAADPCM = 0x11,
00328 fmt_GSM = 0x31,
00329 fmt_G728 = 0x41,
00330 fmt_G723 = 0x42,
00331 fmt_MSG7231 = 0x42,
00332 fmt_G726 = 0x64,
00333 fmt_G722 = 0x65,
00334 fmt_G729 = 0x83,
00335 fmt_VivoG7231 = 0x111,
00336
00337
00338 PCM_WavFile = fmt_PCM,
00339 G7231_WavFile = fmt_VivoG7231,
00340
00341
00342 fmt_NotKnown = 0x10000
00343 };
00344
00354 PWAVFile(
00355 unsigned format = fmt_PCM
00356 );
00357 static PWAVFile * format(
00358 const PString & format
00359 );
00360
00373 PWAVFile(
00374 OpenMode mode,
00375 int opts = ModeDefault,
00376 unsigned format = fmt_PCM
00377 );
00378 static PWAVFile * format(
00379 const PString & format,
00380 PFile::OpenMode mode,
00381 int opts = PFile::ModeDefault
00382 );
00383
00393 PWAVFile(
00394 const PFilePath & name,
00395 OpenMode mode = ReadWrite,
00396 int opts = ModeDefault,
00397 unsigned format = fmt_PCM
00398 );
00399 PWAVFile(
00400 const PString & format,
00401 const PFilePath & name,
00402 OpenMode mode = PFile::ReadWrite,
00403 int opts = PFile::ModeDefault
00404 );
00405
00408 ~PWAVFile();
00410
00420 virtual BOOL Read(
00421 void * buf,
00422 PINDEX len
00423 );
00424
00432 virtual BOOL Write(
00433 const void * buf,
00434 PINDEX len
00435 );
00436
00448 virtual BOOL Open(
00449 OpenMode mode = ReadWrite,
00450 int opts = ModeDefault
00451 );
00452
00466 virtual BOOL Open(
00467 const PFilePath & name,
00468 OpenMode mode = ReadWrite,
00469 int opts = ModeDefault
00470 );
00471
00477 virtual BOOL Close();
00478
00493 virtual BOOL SetPosition(
00494 off_t pos,
00495 FilePositionOrigin origin = Start
00496 );
00497
00505 virtual off_t GetPosition() const;
00507
00512 virtual BOOL SetFormat(unsigned fmt);
00513 virtual BOOL SetFormat(const PString & format);
00514
00517 virtual unsigned GetFormat() const;
00518 virtual PString GetFormatAsString() const;
00519
00523 virtual unsigned GetChannels() const;
00524 virtual void SetChannels(unsigned v);
00525
00528 virtual unsigned GetSampleRate() const;
00529 virtual void SetSampleRate(unsigned v);
00530
00533 virtual unsigned GetSampleSize() const;
00534 virtual void SetSampleSize(unsigned v);
00535
00538 virtual unsigned GetBytesPerSecond() const;
00539 virtual void SetBytesPerSecond(unsigned v);
00540
00543 off_t GetHeaderLength() const;
00544
00547 virtual off_t GetDataLength();
00548
00555 BOOL IsValid() const { return isValidWAV; }
00556
00560 PString GetFormatString() const
00561 { if (formatHandler == NULL) return PString("N/A"); else return formatHandler->GetFormatString(); }
00562
00566 void SetAutoconvert();
00567
00569
00570 friend class PWAVFileConverter;
00571
00572 BOOL RawRead(void * buf, PINDEX len);
00573 BOOL RawWrite(const void * buf, PINDEX len);
00574
00575 BOOL FileRead(void * buf, PINDEX len);
00576 BOOL FileWrite(const void * buf, PINDEX len);
00577
00578 off_t RawGetPosition() const;
00579 BOOL RawSetPosition(off_t pos, FilePositionOrigin origin);
00580 off_t RawGetDataLength();
00581
00582 void SetLastReadCount(PINDEX v) { lastReadCount = v; }
00583
00584 PWAV::FMTChunk wavFmtChunk;
00585 PBYTEArray extendedHeader;
00586
00587 protected:
00588 void Construct();
00589 void SelectFormat(unsigned fmt);
00590 void SelectFormat(const PString & format);
00591
00592 PBYTEArray wavHeaderData;
00593
00594 BOOL ProcessHeader();
00595 BOOL GenerateHeader();
00596 BOOL UpdateHeader();
00597
00598 BOOL isValidWAV;
00599
00600 unsigned int origFmt;
00601 PWAVFileFormat * formatHandler;
00602
00603 BOOL autoConvert;
00604 PWAVFileConverter * autoConverter;
00605
00606 off_t lenHeader;
00607 off_t lenData;
00608
00609 BOOL header_needs_updating;
00610 };
00611
00612 #endif
00613
00614