PTLib  Version 2.18.8
 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 
31 #ifndef PTLIB_PWAVFILE_H
32 #define PTLIB_PWAVFILE_H
33 
34 //#ifdef P_USE_PRAGMA
35 //#pragma interface
36 //#endif
37 
38 #include <ptlib.h>
39 
40 #ifdef P_WAVFILE
41 
42 #include <ptlib/pfactory.h>
43 
44 class PWAVFile;
45 
46 namespace PWAV {
47 
48 #pragma pack(1)
49 
50  struct ChunkHeader
51  {
52  char tag[4];
53  PInt32l len;
54  };
55 
56  struct RIFFChunkHeader
57  {
58  ChunkHeader hdr;
59  char tag[4];
60  };
61 
62  struct FMTChunk
63  {
64  ChunkHeader hdr;
65  PUInt16l format;
66  PUInt16l numChannels;
67  PUInt32l sampleRate;
68  PUInt32l bytesPerSec;
69  PUInt16l bytesPerSample;
70  PUInt16l bitsPerSample;
71  };
72 
73  struct G7231ExtendedInfo
74  {
75  PInt16l data1; // 1
76  PInt16l data2; // 480
77  };
78 
79  struct G7231FACTChunk
80  {
81  PWAV::ChunkHeader hdr;
82  PInt32l data1; // 0 Should be number of samples.
83  };
84 
85 #pragma pack()
86 
87 }; // namespace PWAV
88 
89 
92 class PWAVFileFormat
93 {
94 public:
95  virtual ~PWAVFileFormat() { }
96 
99  virtual unsigned GetFormat() const = 0;
100 
103  virtual PString GetFormatString() const = 0;
104 
107  virtual PString GetDescription() const = 0;
108 
110  virtual bool CanSetChannels(unsigned channels) const = 0;
111 
114  virtual void CreateHeader(PWAV::FMTChunk & header, PBYTEArray & extendedHeader) = 0;
115 
118  virtual void UpdateHeader(PWAV::FMTChunk & /*header*/, PBYTEArray & /*extendedHeader*/)
119  { }
120 
123  virtual void ProcessHeader(const PWAV::FMTChunk & /*header*/, const PBYTEArray & /*extendedHeader*/)
124  { }
125 
128  virtual PBoolean WriteExtraChunks(PWAVFile & /*file*/)
129  { return true; }
130 
133  virtual PBoolean ReadExtraChunks(PWAVFile & /*file*/)
134  { return true; }
135 
138  virtual void OnStart()
139  { }
140 
143  virtual void OnStop()
144  { }
145 
148  virtual PBoolean Read(PWAVFile & file, void * buf, PINDEX & len);
149 
152  virtual PBoolean Write(PWAVFile & file, const void * buf, PINDEX & len);
153 };
154 
155 typedef PFactory<PWAVFileFormat, PCaselessString> PWAVFileFormatByFormatFactory;
156 typedef PFactory<PWAVFileFormat, unsigned> PWAVFileFormatByIDFactory;
157 
158 #define PCREATE_WAVFILE_FORMAT_FACTORY(cls, id, name) \
159  PFACTORY_CREATE(PWAVFileFormatByIDFactory, cls, id); \
160  typedef cls cls##_ByFormat; \
161  PFACTORY_CREATE(PWAVFileFormatByFormatFactory, cls##_ByFormat, name)
162 
163 PFACTORY_LOAD(PWAVFileFormatPCM);
164 
165 
168 class PWAVFileConverter
169 {
170 public:
171  virtual ~PWAVFileConverter() { }
172  virtual unsigned GetFormat (const PWAVFile & file) const = 0;
173  virtual off_t GetPosition (const PWAVFile & file) const = 0;
174  virtual PBoolean SetPosition (PWAVFile & file, off_t pos, PFile::FilePositionOrigin origin) = 0;
175  virtual unsigned GetSampleSize(const PWAVFile & file) const = 0;
176  virtual off_t GetDataLength (PWAVFile & file) = 0;
177  virtual PBoolean Read (PWAVFile & file, void * buf, PINDEX len) = 0;
178  virtual PBoolean Write (PWAVFile & file, const void * buf, PINDEX len) = 0;
179 
180 protected:
181 };
182 
183 typedef PFactory<PWAVFileConverter, unsigned> PWAVFileConverterFactory;
184 
187 class PWAVFile : public PFile
188 {
189  PCLASSINFO(PWAVFile, PFile);
190 
191 public:
196  enum WaveType {
197  fmt_PCM = 1,
198  fmt_MSADPCM = 2,
199  fmt_ALaw = 6,
200  fmt_uLaw = 7,
201  fmt_VOXADPCM = 0x10,
202  fmt_IMAADPCM = 0x11,
203  fmt_GSM = 0x31,
204  fmt_G728 = 0x41,
205  fmt_G723 = 0x42,
206  fmt_MSG7231 = 0x42,
207  fmt_G726 = 0x64,
208  fmt_G722 = 0x65,
209  fmt_G729 = 0x83,
210  fmt_VivoG7231 = 0x111,
211 
212  // For backward compatibility
213  PCM_WavFile = fmt_PCM,
214  G7231_WavFile = fmt_VivoG7231,
215  };
216 
222  PWAVFile(
223  unsigned createFormat = fmt_PCM
224  );
225 
234  PWAVFile(
235  OpenMode mode,
236  OpenOptions opts = ModeDefault,
237  unsigned createFormat = fmt_PCM
238  );
239 
249  PWAVFile(
250  const PFilePath & name,
251  OpenMode mode = ReadWrite,
252  OpenOptions opts = ModeDefault,
253  unsigned createFormat = fmt_PCM
254  );
255 
256  PWAVFile(
257  const PString & createFormat,
258  const PFilePath & name,
259  OpenMode mode = ReadWrite,
260  OpenOptions opts = ModeDefault
261  );
262 
265  ~PWAVFile();
267 
277  virtual PBoolean Read(
278  void * buf,
279  PINDEX len
280  );
281 
289  virtual PBoolean Write(
290  const void * buf,
291  PINDEX len
292  );
293 
299  virtual PBoolean Close();
300 
306  virtual off_t GetLength() const;
307 
314  virtual PBoolean SetLength(
315  off_t len // New length of file.
316  );
317 
332  virtual PBoolean SetPosition(
333  off_t pos,
334  FilePositionOrigin origin = Start
335  );
336 
344  virtual off_t GetPosition() const;
346 
353  virtual PBoolean SetFormat(unsigned fmt);
354  virtual PBoolean SetFormat(const PString & format);
355 
358  virtual unsigned GetFormat() const;
359  virtual PString GetFormatString() const;
360 
364  virtual unsigned GetChannels() const;
365 
372  virtual bool SetChannels(unsigned v);
373 
376  virtual unsigned GetSampleRate() const;
377 
382  virtual bool SetSampleRate(unsigned v);
383 
386  virtual unsigned GetSampleSize() const;
387 
389  unsigned GetRawSampleSize() const;
390 
395  virtual bool SetSampleSize(unsigned v);
396 
399  virtual unsigned GetBytesPerSecond() const;
400 
405  virtual void SetBytesPerSecond(unsigned v);
406 
410  bool SetAutoconvert(bool convert = true);
412 
413  // Internal stuff
414  bool RawRead(void * buf, PINDEX len);
415  bool RawWrite(const void * buf, PINDEX len);
416 
417  off_t RawGetPosition() const;
418  bool RawSetPosition(off_t pos, FilePositionOrigin origin);
419  off_t RawGetDataLength() { return m_dataLength; }
420 
421  // Restored for backward compatibility reasons
422  static PWAVFile * format(const PString & format);
423  static PWAVFile * format(const PString & format, OpenMode mode, OpenOptions opts = ModeDefault);
424  __inline bool IsValid() const { return IsOpen(); }
425 
426 
427 protected:
428  virtual bool InternalOpen(OpenMode mode, OpenOptions opts, PFileInfo::Permissions permissions);
429  void Construct(OpenMode mode);
430  bool SelectFormat(PWAVFileFormat * handler);
431 
432  bool ProcessHeader();
433  bool GenerateHeader();
434  bool UpdateHeader();
435 
436  unsigned m_createFormat;
437  PWAV::FMTChunk m_wavFmtChunk;
438  PBYTEArray m_wavHeaderData;
439  PBYTEArray m_extendedHeader;
440  off_t m_headerLength;
441  off_t m_dataLength;
442 
443  PWAVFileFormat * m_formatHandler;
444  PWAVFileConverter * m_autoConverter;
445 
446  enum {
447  e_Reading,
448  e_PreWrite,
449  e_Writing
450  } m_status;
451 
452  // Rate/channel conversion of WAV file
453  unsigned m_readSampleRate;
454  unsigned m_readChannels;
455  PShortArray m_readBuffer;
456  PINDEX m_readBufCount;
457  PINDEX m_readBufPos;
458 };
459 
460 #endif // P_WAVFILE
461 
462 #endif // PTLIB_PWAVFILE_H
463 
464 // End Of File ///////////////////////////////////////////////////////////////
virtual PBoolean Close()
Close the file channel.
virtual PBoolean Read(void *buf, PINDEX len)
Low level read from the file channel.
This class represents a disk file.
Definition: file.h:57
#define PCLASSINFO(cls, par)
Declare all the standard PTLib class information.
Definition: object.h:2164
virtual off_t GetLength() const
Get the current size of the file.
This class describes a full description for a file on the particular platform.
Definition: filepath.h:61
virtual PBoolean SetPosition(off_t pos, FilePositionOrigin origin=Start)
Set the current active position in the file for the next read or write operation. ...
PThreadLocalStorage< Status > m_status[NumErrorGroups+1]
Definition: channel.h:749
virtual bool InternalOpen(OpenMode mode, OpenOptions opts, PFileInfo::Permissions permissions)
Array of unsigned characters.
Definition: array.h:605
virtual PBoolean Write(const void *buf, PINDEX len)
Low level write to the file channel.
#define PFACTORY_LOAD(ConcreteType)
Definition: pfactory.h:557
FilePositionOrigin
Options for the origin in setting the file position.
Definition: file.h:500
virtual off_t GetPosition() const
Get the current active position in the file for the next read or write operation. ...
bool PBoolean
Definition: object.h:174
The character string class.
Definition: pstring.h:108
Class for a factory to create concrete class instances without parameters during construction.
Definition: pfactory.h:396
virtual PBoolean IsOpen() const
Determine if the channel is currently open.
This template class maps the PAbstractArray to a specific element type.
Definition: array.h:504
virtual PBoolean SetLength(off_t len)
Set the size of the file, padding with 0 bytes if it would require expanding the file, or truncating it if being made shorter.