1 // Copyright (C) 1999-2005 Open Source Telecom Corporation.
2 // Copyright (C) 2006-2008 David Sugar, Tycho Softworks.
3 //
4 // This file is part of GNU ccAudio2.
5 //
6 // GNU ccAudio2 is free software: you can redistribute it and/or modify
7 // it under the terms of the GNU Lesser General Public License as published
8 // by the Free Software Foundation, either version 3 of the License, or
9 // (at your option) any later version.
10 //
11 // GNU ccAudio2 is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU Lesser General Public License for more details.
15 //
16 // You should have received a copy of the GNU Lesser General Public License
17 // along with GNU ccAudio2. If not, see <http://www.gnu.org/licenses/>.
18
25 #ifndef CCXX_AUDIO_H_
26 #define CCXX_AUDIO_H_
27
28 #ifndef CCXX_PACKING
29 #if defined(__GNUC__)
30 #define CCXX_PACKED
31 #elif !defined(__hpux) && !defined(_AIX)
33 #endif
34 #endif
35
36 #ifndef W32
37 #if defined(_WIN32) && defined(_MSC_VER)
38 #pragma warning(disable: 4996)
39 #define W32
40 #endif
41 #if defined(__BORLANDC__) && defined(__Windows)
42 #define W32
43 #endif
44 #endif
45
46 #if !defined(__EXPORT) && defined(W32)
47 #define __EXPORT __declspec(dllimport)
48 #endif
49
50 #ifndef __EXPORT
52 #endif
53
54 #ifdef W32
55 #include <windows.h>
56 #ifndef ssize_t
57 #define ssize_t int
58 #endif
59 #else
60 #include <cstddef>
61 #include <cstdlib>
62 #include <sys/types.h>
63 #include <netinet/in.h>
64 #endif
65
66 #include <ctime>
67
69
70 #define AUDIO_SIGNED_LINEAR_RAW 1
71 #define AUDIO_LINEAR_CONVERSION 1
72 #define AUDIO_CODEC_MODULES 1
73 #define AUDIO_LINEAR_FRAMING 1
74 #define AUDIO_NATIVE_METHODS 1
75 #define AUDIO_RATE_RESAMPLER 1
76
79
89 {
90 public:
91 #ifdef W32
98 #else
105 #endif
106
108
114
121
129
138
142
144
155 };
156
158
170
173 };
174
176
200
201 // Please keep the PCM types at the end of the list -
202 // see the "is this PCM or not?" code in
203 // AudioFile::close for why.
212
213 // speex codecs
216
220
221 speexNarrow = speexVoice,
222 speexWide = speexAudio,
223 g723_4bit = g721ADPCM
224 };
226
236 };
238
246 };
248
273 };
275
276
277 #ifdef CCXX_PACKED
278 #pragma pack(1)
279 #endif
280
282 #if __BYTE_ORDER == __LITTLE_ENDIAN
283 unsigned char mp_sync1 : 8;
284 unsigned char mp_crc : 1;
285 unsigned char mp_layer : 2;
286 unsigned char mp_ver : 2;
287 unsigned char mp_sync2 : 3;
288
289 unsigned char mp_priv : 1;
290 unsigned char mp_pad : 1;
291 unsigned char mp_srate : 2;
292 unsigned char mp_brate : 4;
293
294 unsigned char mp_emp : 2;
295 unsigned char mp_original : 1;
296 unsigned char mp_copyright: 1;
297 unsigned char mp_extend : 2;
298 unsigned char mp_channels : 2;
299
300 #else
301 unsigned char mp_sync1 : 8;
302
303 unsigned char mp_sync2 : 3;
304 unsigned char mp_ver : 2;
305 unsigned char mp_layer : 2;
306 unsigned char mp_crc : 1;
307
308 unsigned char mp_brate : 4;
309 unsigned char mp_srate : 2;
310 unsigned char mp_pad : 1;
311 unsigned char mp_priv : 1;
312
313 unsigned char mp_channels : 2;
314 unsigned char mp_extend : 2;
315 unsigned char mp_copyright : 1;
316 unsigned char mp_original : 1;
317 unsigned char mp_emp : 2;
318 #endif
320
330
331 #ifdef CCXX_PACKED
332 #pragma pack()
333 #endif
334
339 {
340 public:
346 unsigned framesize, framecount, headersize,
padding;
349
351 void clear(void);
352 void set(void);
354 void setRate(
Rate rate);
355 };
356
363 static Level tolevel(
float dbm);
364
371 static float todbm(
Level power);
372
380 static bool hasDevice(unsigned device = 0);
381
393
399 static const char *getCodecPath(void);
400
408 static const char *getMIME(
Info &
info);
409
417 static const char *getName(
Encoding encoding);
418
426 static const char *getExtension(
Encoding encoding);
427
438 static Encoding getEncoding(
const char *name);
439
447
455
462 static bool isLinear(
Encoding encoding);
463
472 static bool isBuffered(
Encoding encoding);
473
480 static bool isMono(
Encoding encoding);
481
488 static bool isStereo(
Encoding encoding);
489
498
508
517
526
534 static bool isEndian(
Encoding encoding);
535
544
554 static bool swapEndian(
Encoding encoding,
void *buffer,
unsigned number);
555
565
576 static bool swapEndian(
Info &
info,
void *buffer,
unsigned number);
577
586 static Level getImpulse(
Encoding encoding,
void *buffer,
unsigned number);
587
596 static Level getImpulse(
Info &
info,
void *buffer,
unsigned number = 0);
597
607 static Level getPeak(
Encoding encoding,
void *buffer,
unsigned number);
608
618 static Level getPeak(
Info &
info,
void *buffer,
unsigned number = 0);
619
627 static void toTimestamp(
timeout_t duration,
char *address,
size_t size);
628
635 static timeout_t toTimeout(
const char *timestamp);
636
659 static int getFrame(
Encoding encoding,
int samples = 0);
660
673 static int getCount(
Encoding encoding);
674
683 static unsigned long toSamples(
Encoding encoding,
size_t bytes);
684
693 static unsigned long toSamples(
Info &
info,
size_t bytes);
694
703 static size_t toBytes(
Info &
info,
unsigned long number);
704
713 static size_t toBytes(
Encoding encoding,
unsigned long number);
714
723 static void fill(
unsigned char *address,
int number,
Encoding encoding);
724
731 static bool loadPlugin(const char *path);
732
740 static size_t maxFramesize(
Info &
info);
741 };
742
751 {
752 protected:
757
758 public:
761
762 size_t process(Linear from, Linear to, size_t count);
763 size_t estimate(size_t count);
764 };
765
775 {
776 protected:
780 double df1, df2, p1,
p2;
783
787 void silence(void);
788
792 void reset(void);
793
797 void cleanup(void);
798
805 void single(
unsigned freq,
Level level);
806
815 void dual(
unsigned f1,
unsigned f2,
Level l1,
Level l2);
816
817 public:
824 {return rate;};
825
832 {return samples;};
833
839 bool isSilent(void);
840
848 virtual Linear getFrame(void);
849
858 unsigned getFrames(Linear buffer, unsigned number);
859
866 virtual bool isComplete(void);
867
875
886 AudioTone(
unsigned f1,
unsigned f2, Level l1, Level l2,
887 timeout_t duration = 20, Rate sample = rate8khz);
888
897 AudioTone(
unsigned freq, Level level,
timeout_t duration = 20, Rate sample = rate8khz);
898
900 };
901
909 {
910 protected:
912
913 public:
918
925
930
937 {
return info.encoding;};
938
946
954 virtual ssize_t putBuffer(Encoded data, size_t size) = 0;
955
964 ssize_t putNative(Encoded data, size_t size);
965
973 virtual ssize_t getBuffer(Encoded data, size_t size) = 0;
974
982 {return getBuffer(data, 0);};
983
991 ssize_t getNative(Encoded data, size_t size);
992 };
993
1002 {
1003 public:
1006
1014 ssize_t getBuffer(
Encoded data,
size_t number);
1015
1023 ssize_t putBuffer(
Encoded data,
size_t number);
1024
1025 private:
1029
1030 void enter(void);
1031 void leave(void);
1032 };
1033
1043 {
1044 protected:
1047 unsigned long header;
// offset to start of audio
1048 unsigned long minimum;
// minimum sample size required
1049 unsigned long length;
// current size of file, including header
1050
1051 void initialize(void);
1052 void getWaveFormat(int size);
1054
1055 union {
1058 } file;
1059
1062
1063 virtual bool afCreate(const char *path, bool exclusive = false);
1064 virtual bool afOpen(const char *path, Mode m = modeWrite);
1065 virtual bool afPeek(unsigned char *data, unsigned size);
1066
1068
1081 virtual int afRead(unsigned char *data, unsigned size);
1082
1094 virtual int afWrite(unsigned char *data, unsigned size);
1095
1105 virtual bool afSeek(unsigned long pos);
1106
1110 virtual void afClose(void);
1111
1120 {return NULL;};
1121
1130 const char * getErrorStr(Error err);
1131
1132 Error setError(Error err);
1133
1141 {return header;};
1142
1151 unsigned short getShort(unsigned char *data);
1152
1161 void setShort(unsigned char *data, unsigned short value);
1162
1171 unsigned long getLong(unsigned char *data);
1172
1181 void setLong(unsigned char *data, unsigned long value);
1182
1183 public:
1190 AudioFile(
const char *name,
unsigned long offset = 0);
1191
1200
1205 {initialize();};
1206
1208
1219 void open(
const char *name, Mode mode = modeWrite,
timeout_t framing = 0);
1220
1231 void create(
const char *name,
Info *
info,
bool exclusive =
false,
timeout_t framing = 0);
1232
1239 time_t getAge(void);
1240
1247 {
return maxFramesize(
info);};
1248
1254 void close(void);
1255
1263 void clear(void);
1264
1276 ssize_t getBuffer(Encoded buffer, size_t len = 0);
1277
1286 unsigned getLinear(Linear buffer, unsigned request = 0);
1287
1299 ssize_t putBuffer(Encoded buffer, size_t len = 0);
1300
1309 unsigned putLinear(Linear buffer, unsigned request = 0);
1310
1325 Error getSamples(void *buffer, unsigned samples = 0);
1326
1340 Error putSamples(void *buffer, unsigned samples = 0);
1341
1349 Error skip(long number);
1350
1358 Error setPosition(unsigned long samples = ~0l);
1359
1367 Error position(const char *timestamp);
1368
1375 void getPosition(char *timestamp, size_t size);
1376
1384 Error setLimit(unsigned long maximum = 0l);
1385
1394
1403 Error setMinimum(unsigned long minimum);
1404
1414 unsigned long getAbsolutePosition(void);
1415
1427 unsigned long getPosition(void);
1428
1434 virtual bool isOpen(void);
1435
1444 {return true;};
1445
1452 {
return info.encoding;};
1453
1460 {
return info.format;};
1461
1469 {
return info.rate;};
1470
1477 {
return info.annotation;};
1478
1486
1488 {return (bool)!isOpen();};
1489
1495 bool isSigned(void);
1496 };
1497
1510 {
1511 protected:
1520
1521 unsigned bufAudio(
Linear samples,
unsigned count,
unsigned size);
1522
1523 public:
1528
1537
1547
1549
1557 ssize_t getBuffer(Encoded data, size_t count);
1558
1566 void open(
const char *name,
Mode mode = modeRead,
timeout_t framing = 0);
1567
1576 void create(
const char *name,
Info *
info,
bool exclusive =
false,
timeout_t framing = 0);
1577
1581 void close(void);
1582
1586 void flush(void);
1587
1594 bool isStreamable(void);
1595
1599 unsigned getCount(void); // frame count
1600
1610 unsigned getEncoded(
AudioCodec *codec, Encoded address,
unsigned frames = 1);
1611
1621 unsigned putEncoded(
AudioCodec *codec, Encoded address,
unsigned frames = 1);
1622
1630 unsigned getEncoded(Encoded address, unsigned frames = 1);
1631
1639 unsigned putEncoded(Encoded address, unsigned frames = 1);
1640
1648 ssize_t getPacket(Encoded data);
1649
1658 unsigned getMono(
Linear buffer,
unsigned frames = 1);
1659
1668 unsigned getStereo(
Linear buffer,
unsigned frames = 1);
1669
1678 unsigned putMono(
Linear buffer,
unsigned frames = 1);
1679
1688 unsigned putStereo(
Linear buffer,
unsigned frames = 1);
1689
1699 unsigned bufMono(
Linear buffer,
unsigned count);
1700
1710 unsigned bufStereo(
Linear buffer,
unsigned count);
1711
1718 {return codec;};
1719 };
1720
1734 {
1735 protected:
1741
1743
1752 {return this;};
1753
1761 {return this;};
1762
1763 public:
1770 AudioCodec(
const char *name, Encoding encoding);
1771
1773
1781
1791 static AudioCodec *getCodec(Encoding encoding,
const char *format = NULL,
bool loaded =
false);
1792
1802
1809 static bool load(const char *name);
1810
1818 static bool load(Encoding encoding);
1819
1828 virtual Level getImpulse(void *buffer, unsigned number = 0);
1829
1837 virtual Level getPeak(void *buffer, unsigned number = 0);
1838
1849 virtual bool isSilent(Level threashold, void *buffer, unsigned number = 0);
1850
1859 virtual unsigned encode(Linear buffer, void *dest, unsigned number = 0) = 0;
1860
1869 virtual unsigned encodeBuffered(Linear
Buffer, Encoded dest,
unsigned number);
1870
1879 virtual unsigned decode(Linear buffer, void *source, unsigned number = 0) = 0;
1880
1890 virtual unsigned decodeBuffered(Linear buffer, Encoded source, unsigned len);
1891
1897 virtual unsigned getEstimated(void);
1898
1904 virtual unsigned getRequired(void);
1905
1915 virtual unsigned getPacket(Encoded destination, Encoded data, unsigned size);
1916
1924 };
1925
1927 {
1928 protected:
1930
1931 public:
1933
1941 virtual unsigned putSamples(Linear buffer, unsigned count) = 0;
1942
1950 virtual unsigned getSamples(Linear buffer, unsigned count) = 0;
1951
1960 virtual ssize_t putBuffer(Encoded data, size_t count);
1961
1970 virtual ssize_t getBuffer(Encoded data, size_t count);
1971
1980 {return false;};
1981
1990 virtual bool setAudio(Rate rate = rate8khz,
bool stereo =
false,
timeout_t framing = 20) = 0;
1991
1999 {return;};
2000
2004 virtual void flush(void) = 0;
2005
2015 unsigned bufMono(Linear buffer, unsigned count);
2016
2026 unsigned bufStereo(Linear buffer, unsigned count);
2027
2035
2045 {return enabled;};
2046 };
2047
2057 {
2058 public:
2065
2072
2083
2091
2098 bool isComplete(void);
2099
2100
2108 static bool load(const char *pathname, const char *locale = NULL);
2109
2117 static tonekey_t *
find(
const char *tone,
const char *locale = NULL);
2118
2119 protected:
2126 };
2127
2138 {
2139 protected:
2145
2146 public:
2156
2158
2160 bool isComplete(void);
2161 };
2162
2173 {
2174 protected:
2180
2181 public:
2191
2193
2195 bool isComplete(void);
2196 };
2197
2198
2204 {
2205 public:
2208
2217 int putSamples(
Linear buffer,
int count);
2218
2225 int getResult(char *data, int size);
2226
2227 protected:
2231
2232 private:
2234 tone_detection_descriptor_t dtmf_detect_row[4];
2235 tone_detection_descriptor_t dtmf_detect_col[4];
2236 tone_detection_descriptor_t dtmf_detect_row_2nd[4];
2237 tone_detection_descriptor_t dtmf_detect_col_2nd[4];
2240 };
2241
2242 }
2243
2244 #endif
2245
Mode
File processing mode, whether to skip missing files, etc.
AudioFile()
Construct an audio file without attaching to the filesystem.
Generic audio class to hold master data types and various useful class encapsulated friend functions ...
AudioStream accesses AudioFile base class content as fixed frames of streaming linear samples...
char * getAnnotation(void)
Get annotation extracted from header of containing file.
virtual AudioCodec * getByInfo(Info &info)
get a codec by audio source info descriptor.
Error getError(void)
Get last error code.
Encoding
Audio encoding formats.
__EXPORT AppLog & error(AppLog &sl)
Manipulator for error level.
Rate
Audio encoding rate, samples per second.
unsigned getSampleRate(void)
Get audio encoding sample rate, in samples per second, for this audio file.
The codec class is a virtual used for transcoding audio samples between linear frames (or other known...
unsigned long getHeader(void)
Get number of bytes in the file header.
__EXPORT AppLog & info(AppLog &sl)
Manipulator for info level.
The AudioResample class is used to manage linear intropolation buffering for rate conversions...
size_t getSize(void)
Get maximum size of frame buffer for data use.
dtmf_detect_state_t * state
MFTones is used to generate a series of mf audio data from a "telephone" number passed as an ASCII st...
__EXPORT char * find(const char *cs, char *str, size_t len=0)
Audio source description.
static AudioCodec * first
The AudioBuffer class is for mixing one-to-one soft joins.
ssize_t getPacket(Encoded data)
Get's a packet of audio data.
tone_detection_descriptor_t fax_detect
size_t getSamples(void)
Get the frame size for the number of audio samples generated.
Encoding getEncoding(void)
Generic get encoding.
A class used to manipulate audio data.
The buffer class represents an IPC service that is built upon a buffer of fixed capacity that can be ...
virtual void sync(void)
Synchronize timing for audio device to next audio frame.
AudioBase base class for many other audio classes which stream data.
Info * getInfo(void)
Get audio device source descriptor in effect for the device.
Info getInfo(void)
Get an info description for this codec.
static const unsigned ndata
Encoding getEncoding(void)
Return audio encoding format for this audio file.
virtual AudioCodec * getByFormat(const char *format)
often used to create a "new" codec of a subtype based on encoding format, default returns the current...
Format
Audio container file format.
virtual char * getContinuation(void)
This function is used to splice multiple audio files together into a single stream of continues audio...
AudioCodec * getCodec(void)
Return the codec being used if there is one.
The AudioTone class is used to create a frame of audio encoded single or dualtones.
tone_detection_descriptor_t fax_detect_2nd
unsigned getSampleRate(void)
Generic sample rate.
An object that is used to sequence and extract telephony tones based on a telephony tone descriptor r...
DeviceMode
Audio device access mode.
virtual bool setEncoded(Info &info)
Use encoding source descriptor to select the audio encoding format the audio device should be using...
Format getFormat(void)
Return base file format of containing audio file.
goertzel_state_t fax_tone
goertzel_state_t fax_tone2nd
bool isEnabled(void)
Whether device is currently enabled.
DTMFDetect is used for detecting DTMF tones in a stream of audio.
virtual bool hasPositioning(void)
Return true if underlying derived class supports direct access to file positioning.
Rate getRate(void)
Get the sample encoding rate being used for the tone generator.
Error
Audio error conditions.
DTMFTones is used to generate a series of dtmf audio data from a "telephone" number passed as an ASCI...