pwavfile.h
Go to the documentation of this file.
/*
* pwavfile.h
*
* WAV file I/O channel class.
*
* Portable Tools Library
*
* Copyright (c) 2001 Equivalence Pty. Ltd.
*
* The contents of this file are subject to the Mozilla Public License
* Version 1.0 (the "License"); you may not use this file except in
* compliance with the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS"
* basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
* the License for the specific language governing rights and limitations
* under the License.
*
* The Original Code is Portable Windows Library.
*
* The Initial Developer of the Original Code is
* Roger Hardiman <roger@freebsd.org>
* and Shawn Pai-Hsiang Hsiao <shawn@eecs.harvard.edu>
*
* All Rights Reserved.
*
* Contributor(s): ______________________________________.
*
* $Revision: 27139 $
* $Author: rjongbloed $
* $Date: 2012-03-06 21:51:15 -0600 (Tue, 06 Mar 2012) $
*/
#ifndef PTLIB_PWAVFILE_H
#define PTLIB_PWAVFILE_H
//#ifdef P_USE_PRAGMA
//#pragma interface
//#endif
#include <ptlib/pfactory.h>
class PWAVFile;
namespace PWAV {
#ifdef __GNUC__
#define P_PACKED __attribute__ ((packed));
#else
#define P_PACKED
#pragma pack(1)
#endif
struct ChunkHeader
{
char tag[4];
PInt32l len P_PACKED;
};
struct RIFFChunkHeader
{
ChunkHeader hdr;
char tag[4];
};
struct FMTChunk
{
ChunkHeader hdr;
PUInt16l format P_PACKED;
PUInt16l numChannels P_PACKED;
PUInt32l sampleRate P_PACKED;
PUInt32l bytesPerSec P_PACKED;
PUInt16l bytesPerSample P_PACKED;
PUInt16l bitsPerSample P_PACKED;
};
}; // namespace PWAV
#ifdef __GNUC__
#undef P_PACKED
#else
#pragma pack()
#endif
class PWAVFileFormat
{
public:
virtual ~PWAVFileFormat() { }
virtual unsigned GetFormat() const = 0;
virtual PString GetFormatString() const = 0;
virtual PString GetDescription() const = 0;
virtual bool CanSetChannels(unsigned channels) const = 0;
virtual void CreateHeader(PWAV::FMTChunk & header, PBYTEArray & extendedHeader) = 0;
virtual void UpdateHeader(PWAV::FMTChunk & /*header*/, PBYTEArray & /*extendedHeader*/)
{ }
virtual PBoolean WriteExtraChunks(PWAVFile & /*file*/)
{ return true; }
virtual PBoolean ReadExtraChunks(PWAVFile & /*file*/)
{ return true; }
virtual void OnStart()
{ }
virtual void OnStop()
{ }
virtual PBoolean Read(PWAVFile & file, void * buf, PINDEX & len);
virtual PBoolean Write(PWAVFile & file, const void * buf, PINDEX & len);
};
typedef PFactory<PWAVFileFormat, PCaselessString> PWAVFileFormatByFormatFactory;
typedef PFactory<PWAVFileFormat, unsigned> PWAVFileFormatByIDFactory;
PFACTORY_LOAD(PWAVFileFormatPCM);
class PWAVFileConverter
{
public:
virtual ~PWAVFileConverter() { }
virtual unsigned GetFormat (const PWAVFile & file) const = 0;
virtual off_t GetPosition (const PWAVFile & file) const = 0;
virtual PBoolean SetPosition (PWAVFile & file, off_t pos, PFile::FilePositionOrigin origin) = 0;
virtual unsigned GetSampleSize(const PWAVFile & file) const = 0;
virtual off_t GetDataLength (PWAVFile & file) = 0;
virtual PBoolean Read (PWAVFile & file, void * buf, PINDEX len) = 0;
virtual PBoolean Write (PWAVFile & file, const void * buf, PINDEX len) = 0;
};
typedef PFactory<PWAVFileConverter, unsigned> PWAVFileConverterFactory;
class PWAVFile : public PFile
{
PCLASSINFO(PWAVFile, PFile);
public:
enum WaveType {
fmt_PCM = 1,
fmt_MSADPCM = 2,
fmt_ALaw = 6,
fmt_uLaw = 7,
fmt_VOXADPCM = 0x10,
fmt_IMAADPCM = 0x11,
fmt_GSM = 0x31,
fmt_G728 = 0x41,
fmt_G723 = 0x42,
fmt_MSG7231 = 0x42,
fmt_G726 = 0x64,
fmt_G722 = 0x65,
fmt_G729 = 0x83,
fmt_VivoG7231 = 0x111,
// For backward compatibility
PCM_WavFile = fmt_PCM,
G7231_WavFile = fmt_VivoG7231,
// allow opening files without knowing the format
fmt_NotKnown = 0x10000
};
PWAVFile(
unsigned format = fmt_PCM
);
PWAVFile(
OpenMode mode,
int opts = ModeDefault,
unsigned format = fmt_PCM
);
PWAVFile(
const PFilePath & name,
OpenMode mode = ReadWrite,
int opts = ModeDefault,
unsigned format = fmt_PCM
);
PWAVFile(
const PString & format,
const PFilePath & name,
OpenMode mode = PFile::ReadWrite,
int opts = PFile::ModeDefault
);
~PWAVFile();
virtual PBoolean Read(
void * buf,
PINDEX len
);
virtual PBoolean Write(
const void * buf,
PINDEX len
);
virtual PBoolean Open(
OpenMode mode = ReadWrite,
int opts = ModeDefault
);
virtual PBoolean Open(
const PFilePath & name,
OpenMode mode = ReadWrite,
int opts = ModeDefault
);
virtual PBoolean Close();
virtual PBoolean SetPosition(
off_t pos,
FilePositionOrigin origin = Start
);
virtual off_t GetPosition() const;
virtual PBoolean SetFormat(unsigned fmt);
virtual PBoolean SetFormat(const PString & format);
virtual unsigned GetFormat() const;
virtual PString GetFormatAsString() const;
virtual unsigned GetChannels() const;
virtual void SetChannels(unsigned v);
virtual unsigned GetSampleRate() const;
virtual void SetSampleRate(unsigned v);
virtual unsigned GetSampleSize() const;
virtual void SetSampleSize(unsigned v);
virtual unsigned GetBytesPerSecond() const;
virtual void SetBytesPerSecond(unsigned v);
off_t GetHeaderLength() const;
virtual off_t GetDataLength();
PBoolean IsValid() const { return isValidWAV; }
PString GetFormatString() const
{ if (formatHandler == NULL) return PString("N/A"); else return formatHandler->GetFormatString(); }
void SetAutoconvert();
PBoolean RawRead(void * buf, PINDEX len);
PBoolean RawWrite(const void * buf, PINDEX len);
PBoolean FileRead(void * buf, PINDEX len);
PBoolean FileWrite(const void * buf, PINDEX len);
off_t RawGetPosition() const;
PBoolean RawSetPosition(off_t pos, FilePositionOrigin origin);
off_t RawGetDataLength();
void SetLastReadCount(PINDEX v) { lastReadCount = v; }
void SetLastWriteCount(PINDEX v) { lastWriteCount = v; }
// Restored for backward compatibility reasons
static PWAVFile * format(const PString & format);
static PWAVFile * format(const PString & format, PFile::OpenMode mode, int opts = PFile::ModeDefault);
protected:
void Construct();
bool SelectFormat(unsigned fmt);
bool SelectFormat(const PString & format);
PBoolean ProcessHeader();
PBoolean GenerateHeader();
PBoolean UpdateHeader();
PBYTEArray wavHeaderData;
PWAV::FMTChunk wavFmtChunk;
PBYTEArray extendedHeader;
bool isValidWAV;
unsigned int origFmt;
PWAVFileFormat * formatHandler;
PBoolean autoConvert;
PWAVFileConverter * autoConverter;
off_t lenHeader;
off_t lenData;
bool header_needs_updating;
friend class PWAVFileConverter;
};
#endif // PTLIB_PWAVFILE_H
// End Of File ///////////////////////////////////////////////////////////////
PChannel::lastWriteCount
PINDEX lastWriteCount
Number of byte last written by the Write() function.
Definition: channel.h:723
PWAVFile::RawGetPosition
off_t RawGetPosition() const
PWAVFile::GetFormatString
PString GetFormatString() const
Return a string that describes the WAV format.
Definition: pwavfile.h:414
PWAVFile::fmt_IMAADPCM
IMA-ADPCM, 8kHz mono.
Definition: pwavfile.h:188
PFile
This class represents a disk file.
Definition: file.h:60
PWAVFile::GetFormatAsString
virtual PString GetFormatAsString() const
PWAVFile::wavHeaderData
PBYTEArray wavHeaderData
Definition: pwavfile.h:450
PWAV::RIFFChunkHeader::tag
char tag[4]
Definition: pwavfile.h:65
PWAVFile::Open
virtual PBoolean Open(OpenMode mode=ReadWrite, int opts=ModeDefault)
Open the current file in the specified mode and with the specified options.
PWAVFile::format
static PWAVFile * format(const PString &format)
PWAVFile::SetLastReadCount
void SetLastReadCount(PINDEX v)
Definition: pwavfile.h:433
PWAVFile::fmt_NotKnown
Definition: pwavfile.h:203
PWAVFileConverter::~PWAVFileConverter
virtual ~PWAVFileConverter()
Definition: pwavfile.h:158
PWAVFile::SelectFormat
bool SelectFormat(unsigned fmt)
PWAVFile::G7231_WavFile
Definition: pwavfile.h:200
PWAVFile::SetSampleSize
virtual void SetSampleSize(unsigned v)
PWAVFileConverter::Read
virtual PBoolean Read(PWAVFile &file, void *buf, PINDEX len)=0
pfactory.h
PWAVFileConverter::SetPosition
virtual PBoolean SetPosition(PWAVFile &file, off_t pos, PFile::FilePositionOrigin origin)=0
PWAVFile::Read
virtual PBoolean Read(void *buf, PINDEX len)
Call PFile::Read() to read in audio data and perform necessary processing such as byte-order swaping...
PWAVFile::RawSetPosition
PBoolean RawSetPosition(off_t pos, FilePositionOrigin origin)
PWAVFile
A class representing a WAV audio file.
Definition: pwavfile.h:172
PWAVFile::fmt_MSADPCM
MS-ADPCM, 8kHz, mono.
Definition: pwavfile.h:184
PWAVFile::GetFormat
virtual unsigned GetFormat() const
Find out the format of the WAV file.
PWAVFile::autoConverter
PWAVFileConverter * autoConverter
Definition: pwavfile.h:460
PWAVFile::SetBytesPerSecond
virtual void SetBytesPerSecond(unsigned v)
PWAVFile::fmt_G723
RFC2361.
Definition: pwavfile.h:191
PWAV::RIFFChunkHeader::hdr
ChunkHeader hdr
Definition: pwavfile.h:64
PFilePath
This class describes a full description for a file on the particular platform.
Definition: filepath.h:65
PFile::ReadWrite
File can be both read and written.
Definition: file.h:80
PWAVFile::wavFmtChunk
PWAV::FMTChunk wavFmtChunk
Definition: pwavfile.h:451
PWAVFile::GetSampleRate
virtual unsigned GetSampleRate() const
Find out the sample rate of the WAV file in Hz.
PWAVFile::RawRead
PBoolean RawRead(void *buf, PINDEX len)
PWAVFile::extendedHeader
PBYTEArray extendedHeader
Definition: pwavfile.h:452
PWAVFile::formatHandler
PWAVFileFormat * formatHandler
Definition: pwavfile.h:457
PWAVFileConverter::GetSampleSize
virtual unsigned GetSampleSize(const PWAVFile &file) const =0
PWAVFileFormat::GetFormatString
virtual PString GetFormatString() const =0
Return a string that can be used as a media format.
PWAVFile::fmt_VivoG7231
VivoActive G.723.1.
Definition: pwavfile.h:196
PWAVFile::GetHeaderLength
off_t GetHeaderLength() const
Find out the size of WAV header presented in the file.
PWAVFileConverter::GetPosition
virtual off_t GetPosition(const PWAVFile &file) const =0
PWAVFile::lenHeader
off_t lenHeader
Definition: pwavfile.h:462
PWAV::FMTChunk::P_PACKED
PUInt16l bitsPerSample P_PACKED
Bits Per Sample, eg 16.
Definition: pwavfile.h:76
PWAV::FMTChunk::P_PACKED
PUInt16l bytesPerSample P_PACKED
Bytes Per Sample, eg 2.
Definition: pwavfile.h:75
PWAV::RIFFChunkHeader
Definition: pwavfile.h:62
PWAVFileFormat::Read
virtual PBoolean Read(PWAVFile &file, void *buf, PINDEX &len)
Write data to the file.
PWAV::FMTChunk::P_PACKED
PUInt32l sampleRate P_PACKED
Sample Rate in Hz.
Definition: pwavfile.h:73
PWAVFile::FileRead
PBoolean FileRead(void *buf, PINDEX len)
PWAVFileFormatByFormatFactory
PFactory< PWAVFileFormat, PCaselessString > PWAVFileFormatByFormatFactory
Definition: pwavfile.h:147
PWAVFile::SetSampleRate
virtual void SetSampleRate(unsigned v)
PWAVFileFormat::ReadExtraChunks
virtual PBoolean ReadExtraChunks(PWAVFile &)
Read any extra headers after the FORMAT chunk.
Definition: pwavfile.h:125
PWAVFile::fmt_ALaw
A-Law 8kHz.
Definition: pwavfile.h:185
PWAVFile::Write
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...
PWAVFile::FileWrite
PBoolean FileWrite(const void *buf, PINDEX len)
PWAVFileFormat::OnStop
virtual void OnStop()
Called after the reading/writing stops.
Definition: pwavfile.h:135
PChannel::lastReadCount
PINDEX lastReadCount
Number of byte last read by the Read() function.
Definition: channel.h:721
PBoolean
BOOL PBoolean
Definition: object.h:102
PWAVFile::fmt_G729
RFC2361.
Definition: pwavfile.h:195
PWAVFileConverter::GetFormat
virtual unsigned GetFormat(const PWAVFile &file) const =0
PBYTEArray
Array of unsigned characters.
Definition: array.h:670
PWAVFileFormat::UpdateHeader
virtual void UpdateHeader(PWAV::FMTChunk &, PBYTEArray &)
Populate the header with the correct values after initial parameters are set.
Definition: pwavfile.h:115
PFACTORY_LOAD
PFACTORY_LOAD(PWAVFileFormatPCM)
PWAVFile::isValidWAV
bool isValidWAV
Definition: pwavfile.h:454
PWAVFileConverter::GetDataLength
virtual off_t GetDataLength(PWAVFile &file)=0
PWAVFileFormat::GetFormat
virtual unsigned GetFormat() const =0
Return a PWAVFile format code.
PWAVFileFormat::CanSetChannels
virtual bool CanSetChannels(unsigned channels) const =0
Check that this format can be set to the number of channels.
PWAVFile::lenData
off_t lenData
Definition: pwavfile.h:463
PWAV::FMTChunk::hdr
ChunkHeader hdr
chunk header (already packed)
Definition: pwavfile.h:70
PWAVFile::IsValid
PBoolean IsValid() const
Determine if the WAV file is a valid wave file.
Definition: pwavfile.h:410
PWAVFile::PCM_WavFile
Definition: pwavfile.h:199
PWAVFile::header_needs_updating
bool header_needs_updating
Definition: pwavfile.h:465
PWAVFileFormatByIDFactory
PFactory< PWAVFileFormat, unsigned > PWAVFileFormatByIDFactory
Definition: pwavfile.h:148
PWAVFile::RawWrite
PBoolean RawWrite(const void *buf, PINDEX len)
PFile::FilePositionOrigin
FilePositionOrigin
Options for the origin in setting the file position.
Definition: file.h:457
PWAVFile::SetFormat
virtual PBoolean SetFormat(unsigned fmt)
Find out the format of the WAV file.
PWAVFile::Close
virtual PBoolean Close()
Close the file channel.
PWAVFile::PWAVFile
PWAVFile(unsigned format=fmt_PCM)
Create a WAV file object but do not open it.
PWAVFileFormat::CreateHeader
virtual void CreateHeader(PWAV::FMTChunk &header, PBYTEArray &extendedHeader)=0
Populate the header with the correct values.
PWAVFile::fmt_G728
RFC2361.
Definition: pwavfile.h:190
PWAV::FMTChunk::P_PACKED
PUInt32l bytesPerSec P_PACKED
Average bytes Per Second.
Definition: pwavfile.h:74
PWAV::FMTChunk::P_PACKED
PUInt16l numChannels P_PACKED
Channels 0x01 = mono, 0x02 = stereo.
Definition: pwavfile.h:72
PWAVFile::SetLastWriteCount
void SetLastWriteCount(PINDEX v)
Definition: pwavfile.h:434
PWAVFileFormat::WriteExtraChunks
virtual PBoolean WriteExtraChunks(PWAVFile &)
Write any extra headers after the FORMAT chunk.
Definition: pwavfile.h:120
PString
The character string class.
Definition: pstring.h:108
PWAVFile::fmt_VOXADPCM
OKI ADPCM.
Definition: pwavfile.h:187
PWAVFile::fmt_uLaw
u-Law 8kHz
Definition: pwavfile.h:186
PFile::ModeDefault
File options depend on the OpenMode parameter.
Definition: file.h:95
PWAVFile::ProcessHeader
PBoolean ProcessHeader()
PWAVFile::Construct
void Construct()
PWAV
Definition: pwavfile.h:47
PWAVFile::fmt_G722
RFC2361.
Definition: pwavfile.h:194
PWAVFileFormat::OnStart
virtual void OnStart()
Called before the reading/writing starts.
Definition: pwavfile.h:130
PWAV::ChunkHeader::tag
char tag[4]
Definition: pwavfile.h:58
PWAVFile::fmt_MSG7231
Microsoft G.723.1.
Definition: pwavfile.h:192
PWAVFileFormat::GetDescription
virtual PString GetDescription() const =0
Return a string that can be used as a text description.
PWAVFile::SetChannels
virtual void SetChannels(unsigned v)
PWAVFile::UpdateHeader
PBoolean UpdateHeader()
PFactory
Template class for generic factories of an abstract class.
Definition: pfactory.h:144
PWAVFile::autoConvert
PBoolean autoConvert
Definition: pwavfile.h:459
PWAVFile::GetDataLength
virtual off_t GetDataLength()
Find out how many bytes of audio data there are.
PWAVFile::GetPosition
virtual off_t GetPosition() const
Get the current active position in the file for the next read or write operation. ...
PWAVFileFormat
Abstract factory class for handling WAV files formats.
Definition: pwavfile.h:89
PWAVFile::GenerateHeader
PBoolean GenerateHeader()
PWAVFileConverterFactory
PFactory< PWAVFileConverter, unsigned > PWAVFileConverterFactory
Definition: pwavfile.h:168
PWAVFile::origFmt
unsigned int origFmt
Definition: pwavfile.h:456
PWAVFile::GetSampleSize
virtual unsigned GetSampleSize() const
Find out how may bits there are per sample, eg 8 or 16.
PWAVFile::fmt_GSM
GSM.
Definition: pwavfile.h:189
PWAVFileConverter
Abstract factory class for autoconversion of WAV files to/from PCM-16.
Definition: pwavfile.h:155
PWAV::FMTChunk::P_PACKED
PUInt16l format P_PACKED
Format.
Definition: pwavfile.h:71
PWAVFile::GetChannels
virtual unsigned GetChannels() const
Find out the number of channels the WAV file has.
PFile::Start
Set position relative to start of file.
Definition: file.h:459
PWAVFile::fmt_G726
RFC2361.
Definition: pwavfile.h:193
PWAV::ChunkHeader
Definition: pwavfile.h:56
PWAVFile::SetAutoconvert
void SetAutoconvert()
Enable autoconversion between PCM-16 and the native format.
PWAVFile::~PWAVFile
~PWAVFile()
Close the file before destruction.
PWAV::ChunkHeader::P_PACKED
PInt32l len P_PACKED
Definition: pwavfile.h:59
PWAVFileConverter::Write
virtual PBoolean Write(PWAVFile &file, const void *buf, PINDEX len)=0
PWAVFile::WaveType
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
PWAV::FMTChunk
Definition: pwavfile.h:68
PWAVFile::fmt_PCM
PCM, 8kHz, 16 bit, mono.
Definition: pwavfile.h:183
PWAVFileFormat::Write
virtual PBoolean Write(PWAVFile &file, const void *buf, PINDEX &len)
Read data from the file.
PWAVFileFormat::~PWAVFileFormat
virtual ~PWAVFileFormat()
Definition: pwavfile.h:92
PWAVFile::SetPosition
virtual PBoolean SetPosition(off_t pos, FilePositionOrigin origin=Start)
Set the current active position in the file for the next read or write operation. ...
PFile::OpenMode
OpenMode
When a file is opened, it may restrict the access available to operations on the object instance...
Definition: file.h:77
PWAVFile::GetBytesPerSecond
virtual unsigned GetBytesPerSecond() const
Find out how may bytes there are per second.
PWAVFile::RawGetDataLength
off_t RawGetDataLength()