PTLib  Version 2.10.4
 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: 26303 $
31  * $Author: rjongbloed $
32  * $Date: 2011-08-12 03:44:54 -0500 (Fri, 12 Aug 2011) $
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 
108  virtual void CreateHeader(PWAV::FMTChunk & header, PBYTEArray & extendedHeader) = 0;
109 
112  virtual void UpdateHeader(PWAV::FMTChunk & /*header*/, PBYTEArray & /*extendedHeader*/)
113  { }
114 
117  virtual PBoolean WriteExtraChunks(PWAVFile & /*file*/)
118  { return true; }
119 
122  virtual PBoolean ReadExtraChunks(PWAVFile & /*file*/)
123  { return true; }
124 
127  virtual void OnStart()
128  { }
129 
132  virtual void OnStop()
133  { }
134 
137  virtual PBoolean Read(PWAVFile & file, void * buf, PINDEX & len);
138 
141  virtual PBoolean Write(PWAVFile & file, const void * buf, PINDEX & len);
142 };
143 
146 
147 PFACTORY_LOAD(PWAVFileFormatPCM);
148 
149 
153 {
154 public:
155  virtual ~PWAVFileConverter() { }
156  virtual unsigned GetFormat (const PWAVFile & file) const = 0;
157  virtual off_t GetPosition (const PWAVFile & file) const = 0;
158  virtual PBoolean SetPosition (PWAVFile & file, off_t pos, PFile::FilePositionOrigin origin) = 0;
159  virtual unsigned GetSampleSize(const PWAVFile & file) const = 0;
160  virtual off_t GetDataLength (PWAVFile & file) = 0;
161  virtual PBoolean Read (PWAVFile & file, void * buf, PINDEX len) = 0;
162  virtual PBoolean Write (PWAVFile & file, const void * buf, PINDEX len) = 0;
163 };
164 
166 
169 class PWAVFile : public PFile
170 {
171  PCLASSINFO(PWAVFile, PFile);
172 
173 public:
179  enum WaveType {
180  fmt_PCM = 1,
182  fmt_ALaw = 6,
183  fmt_uLaw = 7,
184  fmt_VOXADPCM = 0x10,
185  fmt_IMAADPCM = 0x11,
186  fmt_GSM = 0x31,
187  fmt_G728 = 0x41,
188  fmt_G723 = 0x42,
189  fmt_MSG7231 = 0x42,
190  fmt_G726 = 0x64,
191  fmt_G722 = 0x65,
192  fmt_G729 = 0x83,
193  fmt_VivoG7231 = 0x111,
194 
195  // For backward compatibility
198 
199  // allow opening files without knowing the format
200  fmt_NotKnown = 0x10000
201  };
202 
212  PWAVFile(
213  unsigned format = fmt_PCM
214  );
215 
228  PWAVFile(
229  OpenMode mode,
230  int opts = ModeDefault,
231  unsigned format = fmt_PCM
232  );
233 
243  PWAVFile(
244  const PFilePath & name,
245  OpenMode mode = ReadWrite,
246  int opts = ModeDefault,
247  unsigned format = fmt_PCM
248  );
249 
250  PWAVFile(
251  const PString & format,
252  const PFilePath & name,
253  OpenMode mode = PFile::ReadWrite,
254  int opts = PFile::ModeDefault
255  );
256 
259  ~PWAVFile();
261 
271  virtual PBoolean Read(
272  void * buf,
273  PINDEX len
274  );
275 
283  virtual PBoolean Write(
284  const void * buf,
285  PINDEX len
286  );
287 
300  virtual PBoolean Open(
301  OpenMode mode = ReadWrite,
302  int opts = ModeDefault
303  );
304 
318  virtual PBoolean Open(
319  const PFilePath & name,
320  OpenMode mode = ReadWrite,
321  int opts = ModeDefault
322  );
323 
329  virtual PBoolean Close();
330 
345  virtual PBoolean SetPosition(
346  off_t pos,
347  FilePositionOrigin origin = Start
348  );
349 
357  virtual off_t GetPosition() const;
359 
364  virtual PBoolean SetFormat(unsigned fmt);
365  virtual PBoolean SetFormat(const PString & format);
366 
369  virtual unsigned GetFormat() const;
370  virtual PString GetFormatAsString() const;
371 
375  virtual unsigned GetChannels() const;
376  virtual void SetChannels(unsigned v);
377 
380  virtual unsigned GetSampleRate() const;
381  virtual void SetSampleRate(unsigned v);
382 
385  virtual unsigned GetSampleSize() const;
386  virtual void SetSampleSize(unsigned v);
387 
390  virtual unsigned GetBytesPerSecond() const;
391  virtual void SetBytesPerSecond(unsigned v);
392 
395  off_t GetHeaderLength() const;
396 
399  virtual off_t GetDataLength();
400 
407  PBoolean IsValid() const { return isValidWAV; }
408 
412  { if (formatHandler == NULL) return PString("N/A"); else return formatHandler->GetFormatString(); }
413 
416  void SetAutoconvert();
417 
419 
420  PBoolean RawRead(void * buf, PINDEX len);
421  PBoolean RawWrite(const void * buf, PINDEX len);
422 
423  PBoolean FileRead(void * buf, PINDEX len);
424  PBoolean FileWrite(const void * buf, PINDEX len);
425 
426  off_t RawGetPosition() const;
427  PBoolean RawSetPosition(off_t pos, FilePositionOrigin origin);
428  off_t RawGetDataLength();
429 
430  void SetLastReadCount(PINDEX v) { lastReadCount = v; }
431  void SetLastWriteCount(PINDEX v) { lastWriteCount = v; }
432 
433  // Restored for backward compatibility reasons
434  static PWAVFile * format(const PString & format);
435  static PWAVFile * format(const PString & format, PFile::OpenMode mode, int opts = PFile::ModeDefault);
436 
437 
438 protected:
439  void Construct();
440  bool SelectFormat(unsigned fmt);
441  bool SelectFormat(const PString & format);
442 
446 
450 
452 
453  unsigned int origFmt;
455 
458 
459  off_t lenHeader;
460  off_t lenData;
461 
463 
464 friend class PWAVFileConverter;
465 };
466 
467 #endif // PTLIB_PWAVFILE_H
468 
469 // End Of File ///////////////////////////////////////////////////////////////