PTLib
Version 2.10.10
Main Page
Namespaces
Classes
Files
File List
File Members
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
62
struct
RIFFChunkHeader
63
{
64
ChunkHeader
hdr
;
65
char
tag
[4];
66
};
67
68
struct
FMTChunk
69
{
70
ChunkHeader
hdr
;
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
89
class
PWAVFileFormat
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
147
typedef
PFactory<PWAVFileFormat, PCaselessString>
PWAVFileFormatByFormatFactory
;
148
typedef
PFactory<PWAVFileFormat, unsigned>
PWAVFileFormatByIDFactory
;
149
150
PFACTORY_LOAD
(PWAVFileFormatPCM);
151
152
155
class
PWAVFileConverter
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
168
typedef
PFactory<PWAVFileConverter, unsigned>
PWAVFileConverterFactory
;
169
172
class
PWAVFile
:
public
PFile
173
{
174
PCLASSINFO(
PWAVFile
,
PFile
);
175
176
public
:
182
enum
WaveType
{
183
fmt_PCM
= 1,
184
fmt_MSADPCM
= 2,
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
199
PCM_WavFile
=
fmt_PCM
,
200
G7231_WavFile
=
fmt_VivoG7231
,
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
414
PString
GetFormatString
()
const
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
446
PBoolean
ProcessHeader
();
447
PBoolean
GenerateHeader
();
448
PBoolean
UpdateHeader
();
449
450
PBYTEArray
wavHeaderData
;
451
PWAV::FMTChunk
wavFmtChunk
;
452
PBYTEArray
extendedHeader
;
453
454
bool
isValidWAV
;
455
456
unsigned
int
origFmt
;
457
PWAVFileFormat
*
formatHandler
;
458
459
PBoolean
autoConvert
;
460
PWAVFileConverter
*
autoConverter
;
461
462
off_t
lenHeader
;
463
off_t
lenData
;
464
465
bool
header_needs_updating
;
466
467
friend
class
PWAVFileConverter
;
468
};
469
470
#endif // PTLIB_PWAVFILE_H
471
472
// End Of File ///////////////////////////////////////////////////////////////
include
ptclib
pwavfile.h
Generated on Mon Mar 4 2013 13:14:09 for PTLib by
1.8.3.1