PTLib  Version 2.10.10
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
pwavfile.h
Go to the documentation of this file.
1 /*
2  * pwavfile.h
3  *
4  * WAV file I/O channel class.
5  *
6  * Portable Tools Library
7  *
8  * Copyright (c) 2001 Equivalence Pty. Ltd.
9  *
10  * The contents of this file are subject to the Mozilla Public License
11  * Version 1.0 (the "License"); you may not use this file except in
12  * compliance with the License. You may obtain a copy of the License at
13  * http://www.mozilla.org/MPL/
14  *
15  * Software distributed under the License is distributed on an "AS IS"
16  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
17  * the License for the specific language governing rights and limitations
18  * under the License.
19  *
20  * The Original Code is Portable Windows Library.
21  *
22  * The Initial Developer of the Original Code is
23  * Roger Hardiman <roger@freebsd.org>
24  * and Shawn Pai-Hsiang Hsiao <shawn@eecs.harvard.edu>
25  *
26  * All Rights Reserved.
27  *
28  * Contributor(s): ______________________________________.
29  *
30  * $Revision: 27139 $
31  * $Author: rjongbloed $
32  * $Date: 2012-03-06 21:51:15 -0600 (Tue, 06 Mar 2012) $
33  */
34 
35 #ifndef PTLIB_PWAVFILE_H
36 #define PTLIB_PWAVFILE_H
37 
38 //#ifdef P_USE_PRAGMA
39 //#pragma interface
40 //#endif
41 
42 
43 #include <ptlib/pfactory.h>
44 
45 class PWAVFile;
46 
47 namespace PWAV {
48 
49 #ifdef __GNUC__
50 #define P_PACKED __attribute__ ((packed));
51 #else
52 #define P_PACKED
53 #pragma pack(1)
54 #endif
55 
56  struct ChunkHeader
57  {
58  char tag[4];
59  PInt32l len P_PACKED;
60  };
61 
63  {
65  char tag[4];
66  };
67 
68  struct FMTChunk
69  {
71  PUInt16l format P_PACKED;
72  PUInt16l numChannels P_PACKED;
73  PUInt32l sampleRate P_PACKED;
74  PUInt32l bytesPerSec P_PACKED;
75  PUInt16l bytesPerSample P_PACKED;
76  PUInt16l bitsPerSample P_PACKED;
77  };
78 
79 }; // namespace PWAV
80 
81 #ifdef __GNUC__
82 #undef P_PACKED
83 #else
84 #pragma pack()
85 #endif
86 
90 {
91 public:
92  virtual ~PWAVFileFormat() { }
93 
96  virtual unsigned GetFormat() const = 0;
97 
100  virtual PString GetFormatString() const = 0;
101 
104  virtual PString GetDescription() const = 0;
105 
107  virtual bool CanSetChannels(unsigned channels) const = 0;
108 
111  virtual void CreateHeader(PWAV::FMTChunk & header, PBYTEArray & extendedHeader) = 0;
112 
115  virtual void UpdateHeader(PWAV::FMTChunk & /*header*/, PBYTEArray & /*extendedHeader*/)
116  { }
117 
120  virtual PBoolean WriteExtraChunks(PWAVFile & /*file*/)
121  { return true; }
122 
125  virtual PBoolean ReadExtraChunks(PWAVFile & /*file*/)
126  { return true; }
127 
130  virtual void OnStart()
131  { }
132 
135  virtual void OnStop()
136  { }
137 
140  virtual PBoolean Read(PWAVFile & file, void * buf, PINDEX & len);
141 
144  virtual PBoolean Write(PWAVFile & file, const void * buf, PINDEX & len);
145 };
146 
149 
150 PFACTORY_LOAD(PWAVFileFormatPCM);
151 
152 
156 {
157 public:
158  virtual ~PWAVFileConverter() { }
159  virtual unsigned GetFormat (const PWAVFile & file) const = 0;
160  virtual off_t GetPosition (const PWAVFile & file) const = 0;
161  virtual PBoolean SetPosition (PWAVFile & file, off_t pos, PFile::FilePositionOrigin origin) = 0;
162  virtual unsigned GetSampleSize(const PWAVFile & file) const = 0;
163  virtual off_t GetDataLength (PWAVFile & file) = 0;
164  virtual PBoolean Read (PWAVFile & file, void * buf, PINDEX len) = 0;
165  virtual PBoolean Write (PWAVFile & file, const void * buf, PINDEX len) = 0;
166 };
167 
169 
172 class PWAVFile : public PFile
173 {
174  PCLASSINFO(PWAVFile, PFile);
175 
176 public:
182  enum WaveType {
183  fmt_PCM = 1,
185  fmt_ALaw = 6,
186  fmt_uLaw = 7,
187  fmt_VOXADPCM = 0x10,
188  fmt_IMAADPCM = 0x11,
189  fmt_GSM = 0x31,
190  fmt_G728 = 0x41,
191  fmt_G723 = 0x42,
192  fmt_MSG7231 = 0x42,
193  fmt_G726 = 0x64,
194  fmt_G722 = 0x65,
195  fmt_G729 = 0x83,
196  fmt_VivoG7231 = 0x111,
197 
198  // For backward compatibility
201 
202  // allow opening files without knowing the format
203  fmt_NotKnown = 0x10000
204  };
205 
215  PWAVFile(
216  unsigned format = fmt_PCM
217  );
218 
231  PWAVFile(
232  OpenMode mode,
233  int opts = ModeDefault,
234  unsigned format = fmt_PCM
235  );
236 
246  PWAVFile(
247  const PFilePath & name,
248  OpenMode mode = ReadWrite,
249  int opts = ModeDefault,
250  unsigned format = fmt_PCM
251  );
252 
253  PWAVFile(
254  const PString & format,
255  const PFilePath & name,
256  OpenMode mode = PFile::ReadWrite,
257  int opts = PFile::ModeDefault
258  );
259 
262  ~PWAVFile();
264 
274  virtual PBoolean Read(
275  void * buf,
276  PINDEX len
277  );
278 
286  virtual PBoolean Write(
287  const void * buf,
288  PINDEX len
289  );
290 
303  virtual PBoolean Open(
304  OpenMode mode = ReadWrite,
305  int opts = ModeDefault
306  );
307 
321  virtual PBoolean Open(
322  const PFilePath & name,
323  OpenMode mode = ReadWrite,
324  int opts = ModeDefault
325  );
326 
332  virtual PBoolean Close();
333 
348  virtual PBoolean SetPosition(
349  off_t pos,
350  FilePositionOrigin origin = Start
351  );
352 
360  virtual off_t GetPosition() const;
362 
367  virtual PBoolean SetFormat(unsigned fmt);
368  virtual PBoolean SetFormat(const PString & format);
369 
372  virtual unsigned GetFormat() const;
373  virtual PString GetFormatAsString() const;
374 
378  virtual unsigned GetChannels() const;
379  virtual void SetChannels(unsigned v);
380 
383  virtual unsigned GetSampleRate() const;
384  virtual void SetSampleRate(unsigned v);
385 
388  virtual unsigned GetSampleSize() const;
389  virtual void SetSampleSize(unsigned v);
390 
393  virtual unsigned GetBytesPerSecond() const;
394  virtual void SetBytesPerSecond(unsigned v);
395 
398  off_t GetHeaderLength() const;
399 
402  virtual off_t GetDataLength();
403 
410  PBoolean IsValid() const { return isValidWAV; }
411 
415  { if (formatHandler == NULL) return PString("N/A"); else return formatHandler->GetFormatString(); }
416 
419  void SetAutoconvert();
420 
422 
423  PBoolean RawRead(void * buf, PINDEX len);
424  PBoolean RawWrite(const void * buf, PINDEX len);
425 
426  PBoolean FileRead(void * buf, PINDEX len);
427  PBoolean FileWrite(const void * buf, PINDEX len);
428 
429  off_t RawGetPosition() const;
430  PBoolean RawSetPosition(off_t pos, FilePositionOrigin origin);
431  off_t RawGetDataLength();
432 
433  void SetLastReadCount(PINDEX v) { lastReadCount = v; }
434  void SetLastWriteCount(PINDEX v) { lastWriteCount = v; }
435 
436  // Restored for backward compatibility reasons
437  static PWAVFile * format(const PString & format);
438  static PWAVFile * format(const PString & format, PFile::OpenMode mode, int opts = PFile::ModeDefault);
439 
440 
441 protected:
442  void Construct();
443  bool SelectFormat(unsigned fmt);
444  bool SelectFormat(const PString & format);
445 
449 
453 
455 
456  unsigned int origFmt;
458 
461 
462  off_t lenHeader;
463  off_t lenData;
464 
466 
467 friend class PWAVFileConverter;
468 };
469 
470 #endif // PTLIB_PWAVFILE_H
471 
472 // End Of File ///////////////////////////////////////////////////////////////
PINDEX lastWriteCount
Number of byte last written by the Write() function.
Definition: channel.h:723
off_t RawGetPosition() const
PString GetFormatString() const
Return a string that describes the WAV format.
Definition: pwavfile.h:414
IMA-ADPCM, 8kHz mono.
Definition: pwavfile.h:188
This class represents a disk file.
Definition: file.h:60
virtual PString GetFormatAsString() const
PBYTEArray wavHeaderData
Definition: pwavfile.h:450
char tag[4]
Definition: pwavfile.h:65
virtual PBoolean Open(OpenMode mode=ReadWrite, int opts=ModeDefault)
Open the current file in the specified mode and with the specified options.
static PWAVFile * format(const PString &format)
void SetLastReadCount(PINDEX v)
Definition: pwavfile.h:433
Definition: pwavfile.h:203
virtual ~PWAVFileConverter()
Definition: pwavfile.h:158
bool SelectFormat(unsigned fmt)
Definition: pwavfile.h:200
virtual void SetSampleSize(unsigned v)
virtual PBoolean Read(PWAVFile &file, void *buf, PINDEX len)=0
virtual PBoolean SetPosition(PWAVFile &file, off_t pos, PFile::FilePositionOrigin origin)=0
virtual PBoolean Read(void *buf, PINDEX len)
Call PFile::Read() to read in audio data and perform necessary processing such as byte-order swaping...
PBoolean RawSetPosition(off_t pos, FilePositionOrigin origin)
A class representing a WAV audio file.
Definition: pwavfile.h:172
MS-ADPCM, 8kHz, mono.
Definition: pwavfile.h:184
virtual unsigned GetFormat() const
Find out the format of the WAV file.
PWAVFileConverter * autoConverter
Definition: pwavfile.h:460
virtual void SetBytesPerSecond(unsigned v)
RFC2361.
Definition: pwavfile.h:191
ChunkHeader hdr
Definition: pwavfile.h:64
This class describes a full description for a file on the particular platform.
Definition: filepath.h:65
File can be both read and written.
Definition: file.h:80
PWAV::FMTChunk wavFmtChunk
Definition: pwavfile.h:451
virtual unsigned GetSampleRate() const
Find out the sample rate of the WAV file in Hz.
PBoolean RawRead(void *buf, PINDEX len)
PBYTEArray extendedHeader
Definition: pwavfile.h:452
PWAVFileFormat * formatHandler
Definition: pwavfile.h:457
virtual unsigned GetSampleSize(const PWAVFile &file) const =0
virtual PString GetFormatString() const =0
Return a string that can be used as a media format.
VivoActive G.723.1.
Definition: pwavfile.h:196
off_t GetHeaderLength() const
Find out the size of WAV header presented in the file.
virtual off_t GetPosition(const PWAVFile &file) const =0
off_t lenHeader
Definition: pwavfile.h:462
PUInt16l bitsPerSample P_PACKED
Bits Per Sample, eg 16.
Definition: pwavfile.h:76
PUInt16l bytesPerSample P_PACKED
Bytes Per Sample, eg 2.
Definition: pwavfile.h:75
Definition: pwavfile.h:62
virtual PBoolean Read(PWAVFile &file, void *buf, PINDEX &len)
Write data to the file.
PUInt32l sampleRate P_PACKED
Sample Rate in Hz.
Definition: pwavfile.h:73
PBoolean FileRead(void *buf, PINDEX len)
PFactory< PWAVFileFormat, PCaselessString > PWAVFileFormatByFormatFactory
Definition: pwavfile.h:147
virtual void SetSampleRate(unsigned v)
virtual PBoolean ReadExtraChunks(PWAVFile &)
Read any extra headers after the FORMAT chunk.
Definition: pwavfile.h:125
A-Law 8kHz.
Definition: pwavfile.h:185
virtual PBoolean Write(const void *buf, PINDEX len)
Call PFile::Write() to write out audio data and perform necessary processing such as byte-order swapi...
PBoolean FileWrite(const void *buf, PINDEX len)
virtual void OnStop()
Called after the reading/writing stops.
Definition: pwavfile.h:135
PINDEX lastReadCount
Number of byte last read by the Read() function.
Definition: channel.h:721
BOOL PBoolean
Definition: object.h:102
RFC2361.
Definition: pwavfile.h:195
virtual unsigned GetFormat(const PWAVFile &file) const =0
Array of unsigned characters.
Definition: array.h:670
virtual void UpdateHeader(PWAV::FMTChunk &, PBYTEArray &)
Populate the header with the correct values after initial parameters are set.
Definition: pwavfile.h:115
bool isValidWAV
Definition: pwavfile.h:454
virtual off_t GetDataLength(PWAVFile &file)=0
virtual unsigned GetFormat() const =0
Return a PWAVFile format code.
virtual bool CanSetChannels(unsigned channels) const =0
Check that this format can be set to the number of channels.
off_t lenData
Definition: pwavfile.h:463
ChunkHeader hdr
chunk header (already packed)
Definition: pwavfile.h:70
PBoolean IsValid() const
Determine if the WAV file is a valid wave file.
Definition: pwavfile.h:410
Definition: pwavfile.h:199
bool header_needs_updating
Definition: pwavfile.h:465
#define PFACTORY_LOAD(ConcreteType)
Definition: pfactory.h:431
PFactory< PWAVFileFormat, unsigned > PWAVFileFormatByIDFactory
Definition: pwavfile.h:148
PBoolean RawWrite(const void *buf, PINDEX len)
FilePositionOrigin
Options for the origin in setting the file position.
Definition: file.h:457
virtual PBoolean SetFormat(unsigned fmt)
Find out the format of the WAV file.
virtual PBoolean Close()
Close the file channel.
PWAVFile(unsigned format=fmt_PCM)
Create a WAV file object but do not open it.
virtual void CreateHeader(PWAV::FMTChunk &header, PBYTEArray &extendedHeader)=0
Populate the header with the correct values.
RFC2361.
Definition: pwavfile.h:190
PUInt32l bytesPerSec P_PACKED
Average bytes Per Second.
Definition: pwavfile.h:74
PUInt16l numChannels P_PACKED
Channels 0x01 = mono, 0x02 = stereo.
Definition: pwavfile.h:72
void SetLastWriteCount(PINDEX v)
Definition: pwavfile.h:434
virtual PBoolean WriteExtraChunks(PWAVFile &)
Write any extra headers after the FORMAT chunk.
Definition: pwavfile.h:120
The character string class.
Definition: pstring.h:108
OKI ADPCM.
Definition: pwavfile.h:187
u-Law 8kHz
Definition: pwavfile.h:186
File options depend on the OpenMode parameter.
Definition: file.h:95
PBoolean ProcessHeader()
void Construct()
RFC2361.
Definition: pwavfile.h:194
virtual void OnStart()
Called before the reading/writing starts.
Definition: pwavfile.h:130
char tag[4]
Definition: pwavfile.h:58
Microsoft G.723.1.
Definition: pwavfile.h:192
virtual PString GetDescription() const =0
Return a string that can be used as a text description.
virtual void SetChannels(unsigned v)
PBoolean UpdateHeader()
Template class for generic factories of an abstract class.
Definition: pfactory.h:144
PBoolean autoConvert
Definition: pwavfile.h:459
virtual off_t GetDataLength()
Find out how many bytes of audio data there are.
virtual off_t GetPosition() const
Get the current active position in the file for the next read or write operation. ...
Abstract factory class for handling WAV files formats.
Definition: pwavfile.h:89
PBoolean GenerateHeader()
PFactory< PWAVFileConverter, unsigned > PWAVFileConverterFactory
Definition: pwavfile.h:168
unsigned int origFmt
Definition: pwavfile.h:456
virtual unsigned GetSampleSize() const
Find out how may bits there are per sample, eg 8 or 16.
GSM.
Definition: pwavfile.h:189
Abstract factory class for autoconversion of WAV files to/from PCM-16.
Definition: pwavfile.h:155
PUInt16l format P_PACKED
Format.
Definition: pwavfile.h:71
virtual unsigned GetChannels() const
Find out the number of channels the WAV file has.
Set position relative to start of file.
Definition: file.h:459
RFC2361.
Definition: pwavfile.h:193
Definition: pwavfile.h:56
void SetAutoconvert()
Enable autoconversion between PCM-16 and the native format.
~PWAVFile()
Close the file before destruction.
PInt32l len P_PACKED
Definition: pwavfile.h:59
virtual PBoolean Write(PWAVFile &file, const void *buf, PINDEX len)=0
WaveType
When a file is opened for writing, we can specify if this is a PCM wav file or a G.723.1 wav file.
Definition: pwavfile.h:182
Definition: pwavfile.h:68
PCM, 8kHz, 16 bit, mono.
Definition: pwavfile.h:183
virtual PBoolean Write(PWAVFile &file, const void *buf, PINDEX &len)
Read data from the file.
virtual ~PWAVFileFormat()
Definition: pwavfile.h:92
virtual PBoolean SetPosition(off_t pos, FilePositionOrigin origin=Start)
Set the current active position in the file for the next read or write operation. ...
OpenMode
When a file is opened, it may restrict the access available to operations on the object instance...
Definition: file.h:77
virtual unsigned GetBytesPerSecond() const
Find out how may bytes there are per second.
off_t RawGetDataLength()