1 // Copyright (C) 1995-1999 David Sugar, Tycho Softworks.
2 // Copyright (C) 1999-2005 Open Source Telecom Corp.
3 // Copyright (C) 2005-2014 David Sugar, Tycho Softworks.
4 // Copyright (C) 2015 Cherokees of Idaho.
5 //
6 // This file is part of GNU ccAudio2.
7 //
8 // GNU ccAudio2 is free software: you can redistribute it and/or modify
9 // it under the terms of the GNU Lesser General Public License as published
10 // by the Free Software Foundation, either version 3 of the License, or
11 // (at your option) any later version.
12 //
13 // GNU ccAudio2 is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 // GNU Lesser General Public License for more details.
17 //
18 // You should have received a copy of the GNU Lesser General Public License
19 // along with GNU ccAudio2.  If not, see <http://www.gnu.org/licenses/>.
20 
21 /**
22  * This library holds the GNU telephonic audio library, ccAudio.  This
23  * library offers support for audio transcoding, for accessing of audio
24  * files, tone generation and detection, and phrasebook voice libraries.
25  * @file ccaudio.h
26  */
27 
28 #ifndef _CCAUDIO2_H_
29 #define _CCAUDIO2_H_
30 
31 #ifndef _UCOMMON_UCOMMON_H_
32 #include <ucommon/ucommon.h>
33 #endif
34 
35 namespace ucommon {
36 
37 #define AUDIO_SIGNED_LINEAR_RAW 1
38 #define AUDIO_LINEAR_CONVERSION 1
39 #define AUDIO_CODEC_MODULES 1
40 #define AUDIO_LINEAR_FRAMING    1
41 #define AUDIO_NATIVE_METHODS    1
42 #define AUDIO_RATE_RESAMPLER    1
43 
44 class __EXPORT AudioCodec;
45 class __EXPORT AudioDevice;
46 
47 /**
48  * Generic audio class to hold master data types and various useful
49  * class encapsulated friend functions as per GNU Common C++ 2 coding
50  * standard.
51  *
52  * @author David Sugar <dyfet@ostel.com>
53  * @short Master audio class.
54  */
55 class __EXPORT Audio
56 {
57 private:
58     __DELETE_COPY(Audio);
59 
60 public:
61     typedef int16_t snd16_t;
62     typedef int32_t snd32_t;
63     typedef int16_t Level;
64     typedef int16_t Sample;
65     typedef int16_t *Linear;
66 
67 #if _MSC_VER > 1400        // windows broken dll linkage issue...
68     const static unsigned ndata = (-1);
69 #else
70     const static unsigned ndata;
71 #endif
72 
73     typedef struct {
74     float v2;
75         float v3;
76         float fac;
77     } goertzel_state_t;
78 
79     typedef struct {
80         int hit1;
81         int hit2;
82         int hit3;
83         int hit4;
84         int mhit;
85 
86         goertzel_state_t row_out[4];
87         goertzel_state_t col_out[4];
88         goertzel_state_t row_out2nd[4];
89         goertzel_state_t col_out2nd[4];
90         goertzel_state_t fax_tone;
91         goertzel_state_t fax_tone2nd;
92         float energy;
93 
94         int current_sample;
95         char digits[129];
96         int current_digits;
97         int detected_digits;
98         int lost_digits;
99         int digit_hits[16];
100         int fax_hits;
101     } dtmf_detect_state_t;
102 
103     typedef struct {
104         float fac;
105     } tone_detection_descriptor_t;
106 
107     typedef unsigned char *Encoded;
108 
109     /**
110      * Audio encoding rate, samples per second.
111      */
112     enum    Rate {
113         rateUnknown,
114         rate6khz = 6000,
115         rate8khz = 8000,
116         rate11khz = 11025,
117         rate16khz = 16000,
118         rate22khz = 22050,
119         rate32khz = 32000,
120         rate44khz = 44100
121     };
122 
123     typedef enum Rate Rate;
124 
125     /**
126      * File processing mode, whether to skip missing files, etc.
127      */
128     enum    Mode {
129         modeRead,
130         modeReadAny,
131         modeReadOne,
132         modeWrite,
133         modeCache,
134         modeInfo,
135         modeFeed,
136 
137         modeAppend, // app specific placeholders...
138         modeCreate
139     };
140 
141     typedef enum Mode Mode;
142 
143     /**
144      * Audio encoding formats.
145      */
146     enum    Encoding {
147         unknownEncoding = 0,
148         g721ADPCM,
149         g722Audio,
150         g722_7bit,
151         g722_6bit,
152         g723_2bit,
153         g723_3bit,
154         g723_5bit,
155         gsmVoice,
156         msgsmVoice,
157         mulawAudio,
158         alawAudio,
159         mp1Audio,
160         mp2Audio,
161         mp3Audio,
162         okiADPCM,
163         voxADPCM,
164         sx73Voice,
165         sx96Voice,
166 
167         // Please keep the PCM types at the end of the list -
168         // see the "is this PCM or not?" code in
169         // AudioFile::close for why.
170         cdaStereo,
171         cdaMono,
172         pcm8Stereo,
173         pcm8Mono,
174         pcm16Stereo,
175         pcm16Mono,
176         pcm32Stereo,
177         pcm32Mono,
178 
179         // speex codecs
180         speexVoice, // narrow band
181         speexAudio,
182 
183         g729Audio,
184         ilbcAudio,
185         speexUltra,
186 
187         speexNarrow = speexVoice,
188         speexWide = speexAudio,
189         g723_4bit = g721ADPCM
190     };
191     typedef enum Encoding Encoding;
192 
193     /**
194      * Audio container file format.
195      */
196     enum Format {
197         raw,
198         snd,
199         riff,
200         mpeg,
201         wave
202     };
203     typedef enum Format Format;
204 
205     /**
206      * Audio device access mode.
207      */
208     enum DeviceMode {
209         PLAY,
210         RECORD,
211         PLAYREC
212     };
213     typedef enum DeviceMode DeviceMode;
214 
215     /**
216      * Audio error conditions.
217      */
218     enum Error {
219         errSuccess = 0,
220         errReadLast,
221         errNotOpened,
222         errEndOfFile,
223         errStartOfFile,
224         errRateInvalid,
225         errEncodingInvalid,
226         errReadInterrupt,
227         errWriteInterrupt,
228         errReadFailure,
229         errWriteFailure,
230         errReadIncomplete,
231         errWriteIncomplete,
232         errRequestInvalid,
233         errTOCFailed,
234         errStatFailed,
235         errInvalidTrack,
236         errPlaybackFailed,
237         errNotPlaying,
238         errNoCodec
239     };
240     typedef enum Error Error;
241 
242     /**
243      * Audio source description.
244      */
245     class __EXPORT Info
246     {
247     public:
248         Format format;
249         Encoding encoding;
250         unsigned long rate;
251         unsigned long bitrate;
252         unsigned order;
253         unsigned framesize, framecount, headersize, padding;
254         timeout_t framing;
255         char *annotation;
256 
257         Info();
258         Info(const Info& copy);
259 
260         void clear(void);
261         void set(void);
262         void setFraming(timeout_t frame);
263         void setRate(Rate rate);
264     };
265 
Audio()266     inline Audio() {}
267 
268     /**
269      * Convert dbm power level to integer value (0-32768).
270      *
271      * @param dbm power level
272      * @return integer value.
273      */
274     static Level tolevel(float dbm);
275 
276     /**
277      * Convert integer power levels to dbm.
278      *
279      * @param power level.
280      * @return dbm power level.
281      */
282     static float todbm(Level power);
283 
284     /**
285      * Test for the presense of a specified (indexed) audio device.
286      * This is normally used to test for local soundcard access.
287      *
288      * @param device index or 0 for default audio device.
289      * @return true if device exists.
290      */
291     static bool is_available(unsigned device = 0);
292 
293     /**
294      * Get a audio device object that can be used to play or record
295      * audio.  This is normally a local soundcard, though an
296      * abstract base class is returned, so the underlying device may
297      * be different.
298      *
299      * @param device index or 0 for default audio device.
300      * @param mode of device; play, record, or full duplex.
301      * @return pointer to abstract audio device object interface class.
302      */
303     static AudioDevice *getDevice(unsigned device = 0, DeviceMode mode = PLAY);
304 
305     /**
306      * Get the mime descriptive type for a given Audio encoding
307      * description, usually retrieved from a newly opened audio file.
308      *
309      * @param info source description object
310      * @return text of mime type to use for this audio source.
311      */
312     static const char *getMIME(Info &info);
313 
314     /**
315      * Get the short ascii description used for the given audio
316      * encoding type.
317      *
318      * @param encoding format.
319      * @return ascii name of encoding format.
320      */
321     static const char *getName(Encoding encoding);
322 
323     /**
324      * Get the preferred file extension name to use for a given
325      * audio encoding type.
326      *
327      * @param encoding format.
328      * @return ascii file extension to use.
329      */
330     static const char *getExtension(Encoding encoding);
331 
332     /**
333      * Get the audio encoding format that is specified by a short
334      * ascii name.  This will either accept names like those returned
335      * from getName(), or .xxx file extensions, and return the
336      * audio encoding type associated with the name or extension.
337      *
338      * @param name of encoding or file extension.
339      * @return audio encoding format.
340      * @see #getName
341      */
342     static Encoding getEncoding(const char *name);
343 
344     /**
345      * Get the stereo encoding format associated with the given format.
346      *
347      * @param encoding format being tested for stereo.
348      * @return associated stereo audio encoding format.
349      */
350     static Encoding getStereo(Encoding encoding);
351 
352     /**
353      * Get the mono encoding format associated with the given format.
354      *
355      * @param encoding format.
356      * @return associated mono audio encoding format.
357      */
358     static Encoding getMono(Encoding encoding);
359 
360     /**
361      * Test if the audio encoding format is a linear one.
362      *
363      * @return true if encoding format is linear audio data.
364      * @param encoding format.
365      */
366     static bool is_linear(Encoding encoding);
367 
368     /**
369      * Test if the audio encoding format must be packetized (that
370      * is, has irregular sized frames) and must be processed
371      * only through buffered codecs.
372      *
373      * @return true if packetized audio.
374      * @param encoding format.
375      */
376     static bool is_buffered(Encoding encoding);
377 
378     /**
379      * Test if the audio encoding format is a mono format.
380      *
381      * @return true if encoding format is mono audio data.
382      * @param encoding format.
383      */
384     static bool is_mono(Encoding encoding);
385 
386     /**
387      * Test if the audio encoding format is a stereo format.
388      *
389      * @return true if encoding format is stereo audio data.
390      * @param encoding format.
391      */
392     static bool is_stereo(Encoding encoding);
393 
394     /**
395      * Return default sample rate associated with the specified
396      * audio encoding format.
397      *
398      * @return sample rate for audio data.
399      * @param encoding format.
400      */
401     static Rate getRate(Encoding encoding);
402 
403     /**
404      * Return optional rate setting effect.  Many codecs are
405      * fixed rate.
406      *
407      * @return result rate for audio date.
408      * @param encoding format.
409      * @param requested rate.
410      */
411     static Rate getRate(Encoding e, Rate request);
412 
413     /**
414      * Return frame timing for an audio encoding format.
415      *
416      * @return frame time to use in milliseconds.
417      * @param encoding of frame to get timing segment for.
418      * @param timeout of frame time segment to request.
419      */
420     static timeout_t getFraming(Encoding encoding, timeout_t timeout = 0);
421 
422     /**
423      * Return frame time for an audio source description.
424      *
425      * @return frame time to use in milliseconds.
426      * @param info descriptor of frame encoding to get timing segment for.
427      * @param timeout of frame time segment to request.
428      */
429     static timeout_t getFraming(Info &info, timeout_t timeout = 0);
430 
431     /**
432      * Test if the endian byte order of the encoding format is
433      * different from the machine's native byte order.
434      *
435      * @return true if endian format is different.
436      * @param encoding format.
437      */
438     static bool is_endian(Encoding encoding);
439 
440     /**
441      * Test if the endian byte order of the audio source description
442      * is different from the machine's native byte order.
443      *
444      * @return true if endian format is different.
445      * @param info source description object.
446      */
447     static bool is_endian(Info &info);
448 
449     /**
450      * Optionally swap endian of audio data if the encoding format
451      * endian byte order is different from the machine's native endian.
452      *
453      * @return true if endian format was different.
454      * @param encoding format of data.
455      * @param buffer of audio data.
456      * @param number of audio samples.
457      */
458     static bool swapEndian(Encoding encoding, void *buffer, unsigned number);
459 
460     /**
461      * Optionally swap endian of encoded audio data based on the
462      * audio encoding type, and relationship to native byte order.
463      *
464      * @param info source description of object.
465      * @param buffer of audio data.
466      * @param number of bytes of audio data.
467      */
468     static void swapEncoded(Info &info, Encoded data, size_t bytes);
469 
470        /**
471      * Optionally swap endian of audio data if the audio source
472      * description byte order is different from the machine's native
473      * endian byte order.
474      *
475      * @return true if endian format was different.
476      * @param info source description object of data.
477      * @param buffer of audio data.
478      * @param number of audio samples.
479      */
480     static bool swapEndian(Info &info, void *buffer, unsigned number);
481 
482     static const char *getPluginPath(void);
483 
484     /**
485      * Get the energey impulse level of a frame of audio data.
486      *
487      * @return impulse energy level of audio data.
488      * @param encoding format of data to examine.
489      * @param buffer of audio data to examine.
490      * @param number of audio samples to examine.
491      */
492     static Level impulse(Encoding encoding, void *buffer, unsigned number);
493 
494     /**
495      * Get the energey impulse level of a frame of audio data.
496      *
497      * @return impulse energy level of audio data.
498      * @param info encoding source description object.
499      * @param buffer of audio data to examine.
500      * @param number of audio samples to examine.
501      */
502     static Level impulse(Info &info, void *buffer, unsigned number = 0);
503 
504     /**
505      * Get the peak (highest energy) level found in a frame of audio
506      * data.
507      *
508      * @return peak energy level found in data.
509      * @param encoding format of data.
510      * @param buffer of audio data.
511      * @param number of samples to examine.
512      */
513     static Level peak(Encoding encoding, void *buffer, unsigned number);
514 
515     /**
516      * Get the peak (highest energy) level found in a frame of audio
517      * data.
518      *
519      * @return peak energy level found in data.
520      * @param info description object of audio data.
521      * @param buffer of audio data.
522      * @param number of samples to examine.
523      */
524     static Level peak(Info &info, void *buffer, unsigned number = 0);
525 
526     /**
527      * Provide ascii timestamp representation of a timeout value.
528      *
529      * @param duration timeout value
530      * @param address for ascii data.
531      * @param size of ascii data.
532      */
533     static void toTimestamp(timeout_t duration, char *address, size_t size);
534 
535     /**
536      * Convert ascii timestamp representation to a timeout number.
537      *
538      * @param timestamp ascii data.
539      * @return timeout_t duration from data.
540      */
541     static timeout_t toTimeout(const char *timestamp);
542 
543     /**
544      * Returns the number of bytes in a sample frame for the given
545      * encoding type, rounded up to the nearest integer.  A frame
546      * is defined as the minimum number of bytes necessary to
547      * create a point or points in the output waveform for all
548      * output channels.  For example, 16-bit mono PCM has a frame
549      * size of two (because those two bytes constitute a point in
550      * the output waveform).  GSM has it's own definition of a
551      * frame which involves decompressing a sequence of bytes to
552      * determine the final points on the output waveform.  The
553      * minimum number of bytes you can feed to the decompression
554      * engine is 32.5 (260 bits), so this function will return 33
555      * (because we round up) given an encoding type of GSM.  Other
556      * compressed encodings will return similar results.  Be
557      * prepared to deal with nonintuitive return values for
558      * rare encodings.
559      *
560      * @param encoding The encoding type to get the frame size for.
561      * @param samples Reserved.  Use zero.
562      *
563      * @return The number of bytes in a frame for the given encoding.
564      */
565     static int getFrame(Encoding encoding, int samples = 0);
566 
567     /**
568      * Returns the number of samples in all channels for a frame
569      * in the given encoding.  For example, pcm32Stereo has a
570      * frame size of 8 bytes: Note that different codecs have
571      * different definitions of a frame - for example, compressed
572      * encodings have a rather large frame size relative to the
573      * sample size due to the way bytes are fed to the
574      * decompression engine.
575      *
576      * @param encoding The encoding to calculate the frame sample count for.
577      * @return samples The number of samples in a frame of the given encoding.
578      */
579     static int getCount(Encoding encoding);
580 
581     /**
582      * Compute byte counts of audio data into number of samples
583      * based on the audio encoding format used.
584      *
585      * @return number of audio samples in specified data.
586      * @param encoding format.
587      * @param bytes of data.
588      */
589     static unsigned long toSamples(Encoding encoding, size_t bytes);
590 
591     /**
592      * Compute byte counts of audio data into number of samples
593      * based on the audio source description used.
594      *
595      * @return number of audio samples in specified data.
596      * @param info encoding source description.
597      * @param bytes of data.
598      */
599     static unsigned long toSamples(Info &info, size_t bytes);
600 
601     /**
602      * Compute the number of bytes a given number of samples in
603      * a given audio encoding will occupy.
604      *
605      * @return number of bytes samples will occupy.
606      * @param info encoding source description.
607      * @param number of samples.
608      */
609     static size_t toBytes(Info &info, unsigned long number);
610 
611     /**
612      * Compute the number of bytes a given number of samples in
613      * a given audio encoding will occupy.
614      *
615      * @return number of bytes samples will occupy.
616      * @param encoding format.
617      * @param number of samples.
618      */
619     static size_t toBytes(Encoding encoding, unsigned long number);
620 
621     /**
622      * Fill an audio buffer with "empty" (silent) audio data, based
623      * on the audio encoding format.
624      *
625      * @param address of data to fill.
626      * @param number of samples to fill.
627      * @param encoding format of data.
628      */
629     static void fill(unsigned char *address, int number, Encoding encoding);
630 
631     /**
632      * Maximum framesize for a given coding that may be needed to
633      * store a result.
634      *
635      * @param info source description object.
636      * @return maximum possible frame size to allocate for encoded data.
637      */
638     static size_t maxFramesize(Info &info);
639 
640     /**
641      * Initialize by loading any plugins.
642      */
643     static void init(void);
644 };
645 
646 /**
647  * The AudioResample class is used to manage linear intropolation
648  * buffering for rate conversions.
649  *
650  * @author David Sugar <dyfet@ostel.com>
651  * @short linear intropolation and rate conversion.
652  */
653 class __EXPORT AudioResample : public Audio
654 {
655 private:
656     __DELETE_DEFAULTS(AudioResample);
657 
658 protected:
659     unsigned mfact, dfact, max;
660     unsigned gpos, ppos;
661     Sample last;
662     Linear buffer;
663 
664 public:
665     AudioResample(Rate mul, Rate div);
666     ~AudioResample();
667 
668     size_t process(Linear from, Linear to, size_t count);
669     size_t estimate(size_t count);
670 };
671 
672 /**
673  * The AudioTone class is used to create a frame of audio encoded single or
674  * dualtones.  The frame will be iterated for each request, so a
675  * continual tone can be extracted by frame.
676  *
677  * @author David Sugar <dyfet@ostel.com>
678  * @short audio tone generator class.
679  */
680 class __EXPORT AudioTone : public Audio
681 {
682 private:
683     __DELETE_COPY(AudioTone);
684 
685 protected:
686     Rate rate;
687     unsigned samples;
688     Linear frame;
689     double df1, df2, p1, p2;
690     Level m1, m2;
691     bool silencer;
692 
693     /**
694      * Set the frame to silent.
695      */
696     void silence(void);
697 
698     /**
699      * Reset the tone generator completely.  Produces silence.,
700      */
701     void reset(void);
702 
703     /**
704      * Cleanup for virtual destructors to use.
705      */
706     void cleanup(void);
707 
708     /**
709      * Set frame to generate single tone...
710      *
711      * @param freq of tone.
712      * @param level of tone.
713      */
714     void single(unsigned freq, Level level);
715 
716     /**
717      * Set frame to generate dual tone...
718      *
719      * @param f1 frequency of tone 1
720      * @param f2 frequency of tone 2
721      * @param l1 level of tone 1
722      * @param l2 level of tone 2
723      */
724     void dual(unsigned f1, unsigned f2, Level l1, Level l2);
725 
726 public:
727     /**
728      * Get the sample encoding rate being used for the tone generator
729      *
730      * @return sample rate in samples per second.
731      */
getRate(void)732     inline Rate getRate(void) const
733         {return rate;}
734 
735     /**
736      * Get the frame size for the number of audio samples generated.
737      *
738      * @return number of samples processed in frame.
739      */
getSamples(void)740     inline size_t getSamples(void) const
741         {return samples;}
742 
743     /**
744      * Test if the tone generator is currently set to silence.
745      *
746      * @return true if generator set for silence.
747      */
748     bool is_silent(void);
749 
750     /**
751      * Iterate the tone frame, and extract linear samples in
752      * native frame.  If endian flag passed, then convert for
753      * standard endian representation (byte swap) if needed.
754      *
755      * @return pointer to samples.
756      */
757     virtual Linear getFrame(void);
758 
759     /**
760      * This is used to copy one or more pages of framed audio
761      * quickly to an external buffer.
762      *
763      * @return number of frames copied.
764      * @param buffer to copy into.
765      * @param number of frames requested.
766      */
767     unsigned getFrames(Linear buffer, unsigned number);
768 
769     /**
770      * See if at end of tone.  This is used for non-continues audio
771      * tones, or to detect "break" events.
772      *
773      * @return true if end of data.
774      */
775     virtual bool is_complete(void);
776 
777     /**
778      * Construct a silent tone generator of specific frame size.
779      *
780      * @param duration of frame in milliseconds.
781      * @param rate of samples.
782      */
783     AudioTone(timeout_t duration = 20, Rate rate = rate8khz);
784 
785     /**
786      * Construct a dual tone frame generator.
787      *
788      * @param f1 frequency of tone 1.
789      * @param f2 frequency of tone 2.
790      * @param l1 level of tone 1.
791      * @param l2 level of tone 2.
792      * @param duration of frame in milliseconds.
793      * @param sample rate being generated.
794      */
795     AudioTone(unsigned f1, unsigned f2, Level l1, Level l2,
796         timeout_t duration = 20, Rate sample = rate8khz);
797 
798     /**
799      * Construct a single tone frame generator.
800      *
801      * @param freq of tone.
802      * @param level of tone.
803      * @param duration of frame in milliseconds.
804      * @param sample rate being generated.
805      */
806     AudioTone(unsigned freq, Level level, timeout_t duration = 20, Rate sample = rate8khz);
807 
808     virtual ~AudioTone();
809 };
810 
811 /**
812  * AudioBase base class for many other audio classes which stream
813  * data.
814  *
815  * @short common audio stream base.
816  */
817 class __EXPORT AudioBase : public Audio
818 {
819 private:
820     __DELETE_COPY(AudioBase);
821 
822 protected:
823     Info info;
824 
825     /**
826      * Abstract interface to get raw data.
827      *
828      * @return data received in buffer.
829      * @param data to get.
830      * @param size of data to get.
831      */
832     virtual ssize_t getBuffer(Encoded data, size_t size) = 0;
833 
834 public:
835     /**
836      * Create audio base object with no info.
837      */
838     AudioBase();
839 
840     /**
841      * Create audio base object with audio source description.
842      *
843      * @param info source description.
844      */
845     AudioBase(Info *info);
846 
847     /**
848      * Destroy an audio base object.
849      */
850     virtual ~AudioBase();
851 
852     /**
853      * Generic get encoding.
854      *
855      * @return audio encoding of this object.
856      */
getEncoding(void)857     inline Encoding getEncoding(void) const
858         {return info.encoding;}
859 
860     /**
861      * Generic sample rate.
862      *
863      * @return audio sample rate of this object.
864      */
getSampleRate(void)865     inline unsigned getSampleRate(void) const
866         {return info.rate;}
867 
868     /**
869      * Abstract interface to put raw data.
870      *
871      * @param data to put.
872      * @param size of data to put.
873      * @return number of bytes actually put.
874      */
875     virtual ssize_t putBuffer(Encoded data, size_t size) = 0;
876 
877     /**
878      * Puts raw data and does native to refined endian swapping
879      * if needed based on encoding type and local machine endian.
880      *
881      * @param data to put.
882      * @param size of data to put.
883      * @return number of bytes actually put.
884      */
885     ssize_t putNative(Encoded data, size_t size);
886 
887     /**
888      * Get's a packet of audio data.
889      *
890      * @return count of data received.
891      * @param data to get.
892      */
getPacket(Encoded data)893     inline ssize_t getPacket(Encoded data)
894         {return getBuffer(data, 0);}
895 
896     /**
897      * Get raw data and assure is in native machine endian.
898      *
899      * @return data received in buffer.
900      * @param data to get.
901      * @param size of data to get.
902      */
903     ssize_t getNative(Encoded data, size_t size);
904 };
905 
906 /**
907  * The AudioBuffer class is for mixing one-to-one
908  * soft joins.
909  *
910  * @author Mark Lipscombe <markl@gasupnow.com>
911  * @short audio buffer mixer class
912  */
913 class __EXPORT AudioBuffer : public AudioBase
914 {
915 private:
916     __DELETE_DEFAULTS(AudioBuffer);
917 
918 public:
919     AudioBuffer(Info *info, size_t size = 4096);
920     virtual ~AudioBuffer();
921 
922     /**
923      * save audio data from buffer data.
924      *
925      * @return number of bytes actually saved.
926      * @param data save buffer.
927      * @param number of bytes to save.
928      */
929     ssize_t get(Encoded data, size_t number);
930 
931     /**
932      * Put data into the audio buffer.
933      *
934      * @return number of bytes actually put.
935      * @param data of data to load.
936      * @param number of bytes to load.
937      */
938     ssize_t put(Encoded data, size_t number);
939 
940 private:
941     char *buf;
942     size_t size, start, len;
943     mutex_t mutex;
944 };
945 
946 /**
947  * A class used to manipulate audio data.  This class provides file
948  * level access to audio data stored in different formats.  This class
949  * also provides the ability to write audio data into a disk file.
950  *
951  * @author David Sugar <dyfet@ostel.com>
952  * @short audio file access.
953  */
954 class __EXPORT AudioFile: public AudioBase
955 {
956 private:
957     __DELETE_COPY(AudioFile);
958 
959 protected:
960     char *pathname;
961     Error error;
962     unsigned long header;       // offset to start of audio
963     unsigned long minimum;      // minimum sample size required
964     unsigned long length;           // current size of file, including header
965 
966     void initialize(void);
967     void getWaveFormat(int size);
968 
969     union {
970         int fd;
971         void *handle;
972     } file;
973 
974     Mode mode;
975     unsigned long iolimit;
976 
977     virtual bool afCreate(const char *path, bool exclusive = false);
978     virtual bool afOpen(const char *path, Mode m = modeWrite);
979     virtual bool afPeek(unsigned char *data, unsigned size);
980 
981     AudioCodec *getCodec(void);
982 
983     /**
984      * Read a given number of bytes from the file, starting from
985      * the current file pointer.  May be overridden by derived
986      * classes.
987      *
988      * @param data A pointer to the buffer to copy the bytes to.
989      * @param size The number of bytes to read.
990      * @return The number of bytes read, or -1 if an error occurs.
991      * On UNIX platforms, use strerror(errno) to get the
992      * human-readable error string or
993      * FormatMessage(GetLastError()) on Windows platforms.
994      */
995     virtual int afRead(unsigned char *data, unsigned size);
996 
997     /**
998      * Write a number of bytes into the file at the current file
999      * pointer.  May be overridden by derived classes.
1000      *
1001      * @param data A pointer to the buffer with the bytes to write.
1002      * @param size The number of bytes to write from the buffer.
1003      * @return The number of bytes written, or -1 if an error
1004      * occurs.  On UNIX platforms, use strerror(errno) to get the
1005      * human-readable error string or
1006      * FormatMessage(GetLastError()) on Windows platforms.
1007      */
1008     virtual int afWrite(unsigned char *data, unsigned size);
1009 
1010     /**
1011      * Seek to the given position relative to the start of the
1012      * file and set the file pointer.  This does not use 64-bit
1013      * clean seek functions, so seeking to positions greater than
1014      * (2^32)-1 will result in undefined behavior.
1015      *
1016      * @param pos The position to seek to.
1017      * @return true if successful, false otherwise.
1018      */
1019     virtual bool afSeek(unsigned long pos);
1020 
1021     /**
1022      * Close the derived file handling system's file handle.
1023      */
1024     virtual void afClose(void);
1025 
1026     /**
1027      * This function is used to splice multiple audio files together
1028      * into a single stream of continues audio data.  The
1029      * continuation method returns the next audio file to open.
1030      *
1031      * @return next file to open or NULL when done.
1032      */
getContinuation(void)1033     virtual char *getContinuation(void)
1034         {return NULL;}
1035 
1036     /**
1037      * Return a human-readable error message given a numeric error
1038      * code of type Audio::Error.
1039      *
1040      * @param err The numeric error code to translate.
1041      * @return A pointer to a character string containing the
1042      * human-readable error message.
1043      */
1044     const char *getErrorStr(Error err) const;
1045 
1046     Error setError(Error err);
1047 
1048     /**
1049      * Get number of bytes in the file header.  Data packets will
1050      * begin after this header.
1051      *
1052      * @return number of bytes in file header.
1053      */
getHeader(void)1054     inline unsigned long getHeader(void) const
1055         {return header;}
1056 
1057     /**
1058      * Convert binary 2 byte data stored in the order specified
1059      * in the source description into a short variable.  This is
1060      * often used to manipulate header data.  The info type of the
1061      * object is used to determine the endian format.
1062      *
1063      * @return short value.
1064      * @param data binary 2 byte data pointer.
1065      */
1066     unsigned short getShort(unsigned char *data) const;
1067 
1068     /**
1069      * Save a short as two byte binary data stored in the endian
1070      * order specified in the source description.  This is often
1071      * used to manipulate header data.
1072      *
1073      * @param data binary 2 byte data pointer.
1074      * @param value to convert.
1075      */
1076     void setShort(unsigned char *data, unsigned short value);
1077 
1078     /**
1079      * Convert binary 4 byte data stored in the order specified
1080      * in the source description into a long variable.  This is
1081      * often used to manipulate header data.  The object's info
1082      * order is used to determine the endian.
1083      *
1084      * @return long value.
1085      * @param data binary 4 byte data pointer.
1086      */
1087     unsigned long getLong(unsigned char *data) const;
1088 
1089     /**
1090      * Save a long as four byte binary data stored in the endian
1091      * order specified in the source description.  This is often
1092      * used to manipulate header data.
1093      *
1094      * @param data binary 4 byte data pointer.
1095      * @param value to convert.
1096      */
1097     void setLong(unsigned char *data, unsigned long value);
1098 
1099 public:
1100     /**
1101      * Construct and open an existing audio file for read/write.
1102      *
1103      * @param name of file to open.
1104      * @param offset to start access.
1105      */
1106     AudioFile(const char *name, unsigned long offset = 0);
1107 
1108     /**
1109      * Create and open a new audio file for writing.
1110      *
1111      * @param name of file to create.
1112      * @param info source description for new file.
1113      * @param minimum file size to accept at close.
1114      */
1115     AudioFile(const char *name, Info *info, unsigned long minimum = 0);
1116 
1117     /**
1118      * Construct an audio file without attaching to the filesystem.
1119      */
AudioFile()1120     inline AudioFile()
1121         {initialize();}
1122 
1123     virtual ~AudioFile();
1124 
1125     /**
1126      * Open an audio file and associate it with this object.
1127      * Called implicitly by the two-argument version of the
1128      * constructor.
1129      *
1130      * @param name of the file to open.  Don't forget to
1131      * double your backslashes for DOS-style pathnames.
1132      * @param mode to open file under.
1133      * @param framing time in milliseconds.
1134      */
1135     void open(const char *name, Mode mode = modeWrite, timeout_t framing = 0);
1136 
1137     /**
1138      * Create a new audio file and associate it with this object.
1139      * Called implicitly by the three-argument version of the
1140      * constructor.
1141      *
1142      * @param name The name of the file to open.
1143      * @param info The type of the audio file to be created.
1144      * @param exclusive create option.
1145      * @param framing time in milliseconds.
1146      */
1147     void create(const char *name, Info *info, bool exclusive = false, timeout_t framing = 0);
1148 
1149     /**
1150      * Returns age since last prior access.  Used for cache
1151      * computations.
1152      *
1153      * @return age in seconds.
1154      */
1155     time_t age(void);
1156 
1157     /**
1158      * Get maximum size of frame buffer for data use.
1159      *
1160      * @return max frame size in bytes.
1161      */
getSize(void)1162     inline size_t getSize(void)
1163         {return maxFramesize(info);}
1164 
1165     /**
1166      * Close an object associated with an open file.  This
1167      * updates the header metadata with the file length if the
1168      * file length has changed.
1169      */
1170     void close(void);
1171 
1172     /**
1173      * Clear the AudioFile structure.  Called by
1174      * AudioFile::close().  Sets all fields to zero and deletes
1175      * the dynamically allocated memory pointed to by the pathname
1176      * and info.annotation members.  See AudioFile::initialize()
1177      * for the dynamic allocation code.
1178      */
1179     void clear(void);
1180 
1181     /**
1182      * Retrieve bytes from the file into a memory buffer.  This
1183      * increments the file pointer so subsequent calls read further
1184      * bytes.  If you want to read a number of samples rather than
1185      * bytes, use getSamples().
1186      *
1187      * @param buffer area to copy the samples to.
1188      * @param len The number of bytes (not samples) to copy or 0 for frame.
1189      * @return The number of bytes (not samples) read.  Returns -1
1190      * if no bytes are read and an error occurs.
1191      */
1192     ssize_t getBuffer(Encoded buffer, size_t len = 0);
1193 
1194     /**
1195      * Retrieve and convert content to linear encoded audio data
1196      * from it's original form.
1197      *
1198      * @param buffer to copy linear data into.
1199      * @param request number of linear samples to extract or 0 for frame.
1200      * @return number of samples retrieved, 0 if no codec or eof.
1201      */
1202     unsigned getLinear(Linear buffer, unsigned request = 0);
1203 
1204     /**
1205      * Insert bytes into the file from a memory buffer.  This
1206      * increments the file pointer so subsequent calls append
1207      * further samples.  If you want to write a number of samples
1208      * rather than bytes, use putSamples().
1209      *
1210      * @param buffer area to append the samples from.
1211      * @param len The number of bytes (not samples) to append.
1212      * @return The number of bytes (not samples) read.  Returns -1
1213      * if an error occurs and no bytes are written.
1214      */
1215     ssize_t putBuffer(Encoded buffer, size_t len = 0);
1216 
1217     /**
1218      * Convert and store content from linear encoded audio data
1219      * to the format of the audio file.
1220      *
1221      * @param buffer to copy linear data from.
1222      * @param request Number of linear samples to save or 0 for frame.
1223      * @return number of samples saved, 0 if no codec or eof.
1224      */
1225     unsigned putLinear(Linear buffer, unsigned request = 0);
1226 
1227     /**
1228      * Retrieve samples from the file into a memory buffer.  This
1229      * increments the file pointer so subsequent calls read
1230      * further samples.  If a limit has been set using setLimit(),
1231      * the number of samples read will be truncated to the limit
1232      * position.  If you want to read a certain number of bytes
1233      * rather than a certain number of samples, use getBuffer().
1234      *
1235      * @param buffer pointer to copy the samples to.
1236      * @param samples The number of samples to read or 0 for frame.
1237      * @return errSuccess if successful, !errSuccess if
1238      * error.  Use getErrorStr() to retrieve the human-readable
1239      * error string.
1240      */
1241     Error getSamples(void *buffer, unsigned samples = 0);
1242 
1243     /**
1244      * Insert samples into the file from a memory buffer.  This
1245      * increments the file pointer so subsequent calls append
1246      * further samples.  If you want to write a certain number of
1247      * bytes rather than a certain number of samples, use
1248      * putBuffer().
1249      *
1250      * @param buffer pointer to append the samples from.
1251      * @param samples The number of samples (not bytes) to append.
1252      * @return errSuccess if successful, !errSuccess if
1253      * error.  Use getErrorStr() to retrieve the human-readable
1254      * error string.
1255      */
1256     Error putSamples(void *buffer, unsigned samples = 0);
1257 
1258     /**
1259      * Change the file position by skipping a specified number
1260      * of audio samples of audio data.
1261      *
1262      * @return errSuccess or error condition on failure.
1263      * @param number of samples to skip.
1264      */
1265     Error skip(long number);
1266 
1267     /**
1268      * Seek a file position by sample count.  If no position
1269      * specified, then seeks to end of file.
1270      *
1271      * @return errSuccess or error condition on failure.
1272      * @param samples position to seek in file.
1273      */
1274     Error setPosition(unsigned long samples = ~0l);
1275 
1276     /**
1277      * Seek a file position by timestamp.  The actual position
1278      * will be rounded by framing.
1279      *
1280      * @return errSuccess if successful.
1281      * @param timestamp position to seek.
1282      */
1283     Error position(const char *timestamp);
1284 
1285     /**
1286      * Return the timestamp of the current absolute file position.
1287      *
1288      * @param timestamp to save ascii position into.
1289      * @param size of timestamp buffer.
1290      */
1291     void getPosition(char *timestamp, size_t size);
1292 
1293     /**
1294      * Set the maximum file position for reading and writing of
1295      * audio data by samples.  If 0, then no limit is set.
1296      *
1297      * @param maximum file i/o access size sample position.
1298      * @return errSuccess if successful.
1299      */
1300     Error setLimit(unsigned long maximum = 0l);
1301 
1302     /**
1303      * Copy the source description of the audio file into the
1304      * specified object.  Can set error state of object if fails.
1305      *
1306      * @param info pointer to object to copy source description into.
1307      * @return errSucess.
1308      */
1309     Error getInfo(Info *info);
1310 
1311     /**
1312      * Set minimum file size for a created file.  If the file
1313      * is closed with fewer samples than this, it will also be
1314      * deleted.
1315      *
1316      * @param minimum number of samples for new file.
1317      * @return errSuccess if successful.
1318      */
1319     Error setMinimum(unsigned long minimum);
1320 
1321     /**
1322      * Get the current file pointer in bytes relative to the start
1323      * of the file.  See getPosition() to determine the position
1324      * relative to the start of the sample buffer.
1325      *
1326      * @return The current file pointer in bytes relative to the
1327      * start of the file.  Returns 0 if the file is not open, is
1328      * empty, or an error has occured.
1329      */
1330     unsigned long getAbsolutePosition(void);
1331 
1332     /**
1333      * Get the current file pointer in samples relative to the
1334      * start of the sample buffer.  Note that you must multiply
1335      * this result by the result of a call to
1336      * toBytes(info.encoding, 1) in order to determine the offset
1337      * in bytes.
1338      *
1339      * @return the current file pointer in samples relative to the
1340      * start of the sample buffer.  Returns 0 if the file is not
1341      * open, is empty, or an error has occured.
1342      */
1343     unsigned long getPosition(void);
1344 
1345     /**
1346      * Test if the file is opened.
1347      *
1348      * @return true if a file is open.
1349      */
1350     virtual bool is_open(void) const;
1351 
1352     /**
1353      * Return audio encoding format for this audio file.
1354      *
1355      * @return audio encoding format.
1356      */
getEncoding(void)1357     inline Encoding getEncoding(void) const
1358         {return info.encoding;}
1359 
1360     /**
1361      * Return base file format of containing audio file.
1362      *
1363      * @return audio file container format.
1364      */
getFormat(void)1365     inline Format getFormat(void) const
1366         {return info.format;}
1367 
1368     /**
1369      * Get audio encoding sample rate, in samples per second, for
1370      * this audio file.
1371      *
1372      * @return sample rate.
1373      */
getSampleRate(void)1374     inline unsigned getSampleRate(void) const
1375         {return info.rate;}
1376 
1377     /**
1378      * Get annotation extracted from header of containing file.
1379      *
1380      * @return annotation text if any, else NULL.
1381      */
getAnnotation(void)1382     inline char *getAnnotation(void) const
1383         {return info.annotation;}
1384 
1385     /**
1386      * Get last error code.
1387      *
1388      * @return alst error code.
1389      */
getError(void)1390     inline Error getError(void) const
1391         {return error;}
1392 
1393     inline operator bool() const
1394         {return is_open();}
1395 
1396     inline bool operator!(void) const
1397         {return (bool)!is_open();}
1398 
1399     /**
1400      * Return if the current content is signed or unsigned samples.
1401      *
1402      * @return true if signed.
1403      */
1404     bool is_signed(void) const;
1405 };
1406 
1407 /**
1408  * AudioStream accesses AudioFile base class content as fixed frames
1409  * of streaming linear samples.  If a codec must be assigned to perform
1410  * conversion to/from linear data, AudioStream will handle conversion
1411  * automatically.  AudioStream will also convert between mono and stereo
1412  * audio content.  AudioStream uses linear samples in the native
1413  * machine endian format and perform endian byte swapping as needed.
1414  *
1415  * @author David Sugar <dyfet@ostel.com>
1416  * @short Audio Streaming with Linear Conversion
1417  */
1418 class __EXPORT AudioStream : public AudioFile
1419 {
1420 private:
1421     __DELETE_COPY(AudioStream);
1422 
1423 protected:
1424     AudioCodec *codec;  // if needed
1425     Encoded framebuf;
1426     bool streamable;
1427     Linear bufferFrame;
1428     unsigned bufferPosition;
1429     unsigned bufferChannels;
1430     Linear encBuffer, decBuffer;
1431     unsigned encSize, decSize;
1432 
1433     unsigned bufAudio(Linear samples, unsigned count, unsigned size);
1434 
1435 public:
1436     /**
1437      * Create a new audiostream object.
1438      */
1439     AudioStream();
1440 
1441     /**
1442      * Create an audio stream object and open an existing audio file.
1443      *
1444      * @param name of file to open.
1445      * @param mode of file access.
1446      * @param framing time in milliseconds.
1447      */
1448     AudioStream(const char *name, Mode mode = modeRead, timeout_t framing = 0);
1449 
1450     /**
1451      * Create an audio stream object and a new audio file.
1452      *
1453      * @param name of file to open.
1454      * @param info source description for properties of new file.
1455      * @param exclusive access if true.
1456      * @param framing time in milliseconds.
1457      */
1458     AudioStream(const char *name, Info *info, bool exclusive = false, timeout_t framing = 0);
1459 
1460     virtual ~AudioStream();
1461 
1462     /**
1463      * Virtual for packet i/o intercept.
1464      *
1465      * @return bytes read.
1466      * @param data encoding buffer.
1467      * @param count requested.
1468      */
1469     ssize_t getBuffer(Encoded data, size_t count);
1470 
1471     /**
1472      * Open existing audio file for streaming.
1473      *
1474      * @param name of file to open.
1475      * @param mode to use file.
1476      * @param framing timer in milliseconds.
1477      */
1478     void open(const char *name, Mode mode = modeRead, timeout_t framing = 0);
1479 
1480     /**
1481      * Create a new audio file for streaming.
1482      *
1483      * @param name of file to create.
1484      * @param info source description for file properties.
1485      * @param exclusive true for exclusive access.
1486      * @param framing timing in milliseconds.
1487      */
1488     void create(const char *name, Info *info, bool exclusive = false, timeout_t framing = 0);
1489 
1490     /**
1491      * Close the currently open audio file for streaming.
1492      */
1493     void close(void);
1494 
1495     /**
1496      * flush any unsaved buffered data to disk.
1497      */
1498     void flush(void);
1499 
1500     /**
1501      * Check if the audio file may be streamed.  Files can be
1502      * streamed if a codec is available or if they are linear.
1503      *
1504      * @return true if streamable.
1505      */
1506     bool is_streamable(void);
1507 
1508     /**
1509      * Get the number of samples expected in a frame.
1510      */
1511     unsigned getCount(void);    // frame count
1512 
1513     /**
1514      * Stream audio data from the file and convert into an alternate
1515      * encoding based on the codec supplied.
1516      *
1517      * @param codec to apply before saving.
1518      * @param address of data to save.
1519      * @param frames to stream by the codec.
1520      * @return number of frames processed.
1521      */
1522     unsigned getEncoded(AudioCodec *codec, Encoded address, unsigned frames = 1);
1523 
1524     /**
1525      * Stream audio data in an alternate codec into the currently
1526      * opened file.
1527      *
1528      * @param codec to convert incoming data from.
1529      * @param address of data to convert and stream.
1530      * @param frames of audio to stream.
1531      * @return number of frames processed.
1532      */
1533     unsigned putEncoded(AudioCodec *codec, Encoded address, unsigned frames = 1);
1534 
1535     /**
1536      * Get data from the streamed file in it's native encoding.
1537      *
1538      * @param address to save encoded audio.
1539      * @param frames of audio to load.
1540      * @return number of frames read.
1541      */
1542     unsigned getEncoded(Encoded address, unsigned frames = 1);
1543 
1544     /**
1545      * Put data encoded in the native format of the stream file.
1546      *
1547      * @param address to load encoded audio.
1548      * @param frames of audio to save.
1549      * @return number of frames written.
1550      */
1551     unsigned putEncoded(Encoded address, unsigned frames = 1);
1552 
1553     /**
1554      * Get a packet of data from the file.  This uses the codec
1555      * to determine what a true packet boundry is.
1556      *
1557      * @param buffer to save encoded data.
1558      * @return number of bytes read as packet.
1559      */
1560     ssize_t getPacket(Encoded data);
1561 
1562     /**
1563      * Get and automatically convert audio file data into
1564      * mono linear audio samples.
1565      *
1566      * @param buffer to save linear audio into.
1567      * @param frames of audio to read.
1568      * @return number of frames read from file.
1569      */
1570     unsigned getMono(Linear buffer, unsigned frames = 1);
1571 
1572     /**
1573      * Get and automatically convert audio file data into
1574      * stereo (two channel) linear audio samples.
1575      *
1576      * @param buffer to save linear audio into.
1577      * @param frames of audio to read.
1578      * @return number of frames read from file.
1579      */
1580     unsigned getStereo(Linear buffer, unsigned frames = 1);
1581 
1582     /**
1583      * Automatically convert and put mono linear audio data into
1584      * the audio file.  Convert to stereo as needed by file format.
1585      *
1586      * @param buffer to save linear audio from.
1587      * @param frames of audio to write.
1588      * @return number of frames written to file.
1589      */
1590     unsigned putMono(Linear buffer, unsigned frames = 1);
1591 
1592     /**
1593      * Automatically convert and put stereo linear audio data into
1594      * the audio file.  Convert to mono as needed by file format.
1595      *
1596      * @param buffer to save linear audio from.
1597      * @param frames of audio to write.
1598      * @return number of frames written to file.
1599      */
1600     unsigned putStereo(Linear buffer, unsigned frames = 1);
1601 
1602     /**
1603      * Automatically convert and put arbitrary linear mono data
1604      * into the audio file.  Convert to stereo and buffer incomplete
1605      * frames as needed by the streaming file.
1606      *
1607      * @param buffer to save linear audio from.
1608      * @param count of linear audio to write.
1609      * @return number of linear audio samples written to file.
1610      */
1611     unsigned bufMono(Linear buffer, unsigned count);
1612 
1613     /**
1614      * Automatically convert and put arbitrary linear stereo data
1615      * into the audio file.  Convert to mono and buffer incomplete
1616      * frames as needed by the streaming file.
1617      *
1618      * @param buffer to save linear audio from.
1619      * @param count of linear audio to write.
1620      * @return number of linear audio samples written to file.
1621      */
1622     unsigned bufStereo(Linear buffer, unsigned count);
1623 
1624     /**
1625      * Return the codec being used if there is one.
1626      *
1627      * @return codec used.
1628      */
getCodec(void)1629     inline AudioCodec *getCodec(void)
1630         {return codec;}
1631 };
1632 
1633 /**
1634  * The codec class is a virtual used for transcoding audio samples between
1635  * linear frames (or other known format) and an encoded "sample" buffer.
1636  * This class is only abstract and describes the core interface for
1637  * loadable codec modules.  This class is normally merged with AudioSample.
1638  * A derived AudioCodecXXX will typically include a AudioRegisterXXX static
1639  * class to automatically initialize and register the codec with the codec
1640  * registry.
1641  *
1642  * @author David Sugar <dyfet@ostel.com>
1643  * @short process codec interface.
1644  */
1645 class __EXPORT AudioCodec : public Audio, public LinkedObject
1646 {
1647 private:
1648     __DELETE_COPY(AudioCodec);
1649 
1650 protected:
1651     Encoding encoding;
1652     const char *name;
1653     Info info;
1654 
1655     AudioCodec();
1656 
1657     /**
1658      * often used to create a "new" codec of a subtype based on
1659      * encoding format, default returns the current codec entity.
1660      *
1661      * @return pointer to an active codec or NULL if not found.
1662      * @param format name from spd.
1663      */
getByFormat(const char * format)1664     virtual AudioCodec *getByFormat(const char *format)
1665         {return this;}
1666 
1667     /**
1668      * get a codec by audio source info descriptor.
1669      *
1670      * @return pointer to an active codec or NULL if not found.
1671      * @param info audio source descriptor.
1672      */
getByInfo(Info & info)1673     virtual AudioCodec *getByInfo(Info &info)
1674         {return this;}
1675 
1676 public:
1677     /**
1678      * Base for codecs, create a named coded of a specific encoding.
1679      *
1680      * @param name of codec.
1681      * @param encoding type of codec.
1682      */
1683     AudioCodec(const char *name, Encoding encoding);
1684 
getName(void)1685     inline const char *getName(void) const
1686         {return name;}
1687 
getDescription(void)1688     inline const char *getDescription(void) const
1689         {return info.annotation;}
1690 
~AudioCodec()1691     virtual ~AudioCodec() {}
1692 
1693     /**
1694      * End use of a requested codec.  If constructed then will be
1695      * deleted.
1696      *
1697      * @param codec pointer to getCodec returned coded pointer.
1698      */
1699     static void release(AudioCodec *codec);
1700 
1701     static AudioCodec *begin(void);
1702 
1703     /**
1704      * Get the codec base class for accessing a specific derived
1705      * codec identified by encoding type and optional spd info.
1706      *
1707      * @return pointer to codec for processing.
1708      * @param encoding format requested.
1709      * @param format spd options to pass to codec being created.
1710      */
1711     static AudioCodec *get(Encoding encoding, const char *format = NULL);
1712 
1713     /**
1714      * Get the codec base class for accessing a specific derived
1715      * codec identified by audio source descriptor.
1716      *
1717      * @return pointer to codec for processing.
1718      * @param info source descriptor for codec being requested.
1719      */
1720     static AudioCodec *get(Info &info);
1721 
1722     /**
1723      * Get the impulse energy level of a frame of X samples in
1724      * the specified codec format.
1725      *
1726      * @return average impulse energy of frame (sumnation).
1727      * @param buffer of encoded samples.
1728      * @param number of encoded samples.
1729      */
1730     virtual Level impulse(void *buffer, unsigned number = 0);
1731 
1732     /**
1733      * Get the peak energy level within the frame of X samples.
1734      *
1735      * @return peak energy impulse in frame (largest).
1736      * @param buffer of encoded samples.
1737      * @param number of encoded samples.
1738      */
1739     virtual Level peak(void *buffer, unsigned number = 0);
1740 
1741     /**
1742      * Signal if the current audio frame is silent.  This can be
1743      * deterimed either by an impulse computation, or, in some
1744      * cases, some codecs may signal and flag silent packets.
1745      *
1746      * @return true if silent
1747      * @param threashold to use if not signaled.
1748      * @param buffer of encoded samples.
1749      * @param number of encoded samples.
1750      */
1751     virtual bool is_silent(Level threashold, void *buffer, unsigned number = 0);
1752 
1753     /**
1754      * Encode a linear sample frame into the codec sample buffer.
1755      *
1756      * @return number of bytes written.
1757      * @param buffer linear sample buffer to use.
1758      * @param dest buffer to store encoded results.
1759      * @param number of samples.
1760      */
1761     virtual unsigned encode(Linear buffer, void *dest, unsigned number = 0) = 0;
1762 
1763     /**
1764      * Encode linear samples buffered into the coded.
1765      *
1766      * @return number of bytes written or 0 if incomplete.
1767      * @param buffer linear samples to post.
1768      * @param destination of encoded audio.
1769      * @param number of samples being buffered.
1770      */
1771     virtual unsigned encodeBuffered(Linear Buffer, Encoded dest, unsigned number);
1772 
1773     /**
1774      * Decode the sample frame into linear samples.
1775      *
1776      * @return number of bytes scanned or returned
1777      * @param buffer sample buffer to save linear samples into.
1778      * @param source for encoded data.
1779      * @param number of samples to extract.
1780      */
1781     virtual unsigned decode(Linear buffer, void *source, unsigned number = 0) = 0;
1782 
1783     /**
1784      * Buffer and decode data into linear samples.  This is needed
1785      * for audio formats that have irregular packet sizes.
1786      *
1787      * @return number of samples actually decoded.
1788      * @param destination for decoded data.
1789      * @param source for encoded data.
1790      * @param number of bytes being sent.
1791      */
1792     virtual unsigned decodeBuffered(Linear buffer, Encoded source, unsigned len);
1793 
1794     /**
1795      * Get estimated data required for buffered operations.
1796      *
1797      * @return estimated number of bytes required for decode.
1798      */
1799     virtual unsigned getEstimated(void);
1800 
1801     /**
1802      * get required samples for encoding.
1803      *
1804      * @return required number of samples for encoder buffer.
1805      */
1806     virtual unsigned getRequired(void);
1807 
1808     /**
1809      * Get a packet of data rather than decode.  This is tied with
1810      * getEstimated.
1811      *
1812      * @return size of data packet or 0 if not complete.
1813      * @param destination to save.
1814      * @param data to push into buffer.
1815      * @param number of bytes to push.
1816      */
1817     virtual unsigned getPacket(Encoded destination, Encoded data, unsigned size);
1818 
1819     /**
1820      * Get an info description for this codec.
1821      *
1822      * @return info.
1823      */
getInfo(void)1824     inline Info getInfo(void) const
1825         {return info;}
1826 };
1827 
1828 class __EXPORT AudioDevice : public AudioBase
1829 {
1830 private:
1831     __DELETE_COPY(AudioDevice);
1832 
1833 protected:
1834     bool enabled;
1835 
1836 public:
AudioDevice()1837     inline AudioDevice() {}
1838 
~AudioDevice()1839     virtual ~AudioDevice() {}
1840 
1841     /**
1842      * Copy linear samples to an audio device through its virtual.
1843      *
1844      * @param buffer to linear audio data to play.
1845      * @param count of audio samples to play.
1846      * @return number of audio samples played.
1847      */
1848     virtual unsigned putSamples(Linear buffer, unsigned count) = 0;
1849 
1850     /**
1851      * Copy linear samples from an audio device through its virtual.
1852      *
1853      * @param buffer for recording.
1854      * @param count of audio samples to record.
1855      * @return number of audio samples recorded.
1856      */
1857     virtual unsigned getSamples(Linear buffer, unsigned count) = 0;
1858 
1859     /**
1860      * Copy audio encoded in the currently selected encoding for
1861      * the audio device.
1862      *
1863      * @param data pointer to encoded data to play.
1864      * @param count of encoded bytes to play.
1865      * @return number of encoded bytes played.
1866      */
1867     virtual ssize_t putBuffer(Encoded data, size_t count);
1868 
1869     /**
1870      * Record audio encoded in the currently selected encoding for
1871      * the audio device.
1872      *
1873      * @param data buffer for recording encoded audio.
1874      * @param count of encoded bytes to record.
1875      * @return number of encoded bytes recorded.
1876      */
1877     virtual ssize_t getBuffer(Encoded data, size_t count);
1878 
1879     /**
1880      * Use encoding source descriptor to select the audio encoding
1881      * format the audio device should be using.
1882      *
1883      * @return false if encoding format specified is unsupported by device
1884      * @param info source description for device settings.
1885      */
setEncoded(Info & info)1886     virtual bool setEncoded(Info &info)
1887         {return false;}
1888 
1889     /**
1890      * Set properties for audio device.
1891      *
1892      * @param rate of audio samples device should operate at.
1893      * @param stereo flag.
1894      * @param framing timer for default i/o framing for device.
1895      * @return false if settings not supported by device.
1896      */
1897     virtual bool setAudio(Rate rate = rate8khz, bool stereo = false, timeout_t framing = 20) = 0;
1898 
1899     /**
1900      * Synchronize timing for audio device to next audio frame.
1901      * this is needed for audio devices which do not block i/o to
1902      * assure one does not push too much data before the device
1903      * can handle it.
1904      */
sync(void)1905     virtual void sync(void)
1906         {return;}
1907 
1908     /**
1909      * Flush any pending buffered samples in audio device.
1910      */
1911     virtual void flush(void) = 0;
1912 
1913     /**
1914      * Process linear mono audio and automatically convert to the
1915      * encoding format the audio device is currently using.
1916      * If needed, automatically convert from mono to stereo.
1917      *
1918      * @return number of samples played.
1919      * @param buffer to linear mono audio data to play.
1920      * @param count of linear mono audio samples to play.
1921      */
1922     unsigned bufMono(Linear buffer, unsigned count);
1923 
1924     /**
1925      * Process linear stereo audio and automatically convert to the
1926      * encoding format the audio device is currently using.
1927      * If needed, automatically convert from stereo to mono.
1928      *
1929      * @return number of samples played.
1930      * @param buffer to linear stereo audio data to play.
1931      * @param count of linear stereo audio samples to play.
1932      */
1933     unsigned bufStereo(Linear buffer, unsigned count);
1934 
1935     /**
1936      * Get audio device source descriptor in effect for the device.
1937      *
1938      * @return audio device descriptor.
1939      */
getInfo(void)1940     inline const Info *getInfo(void) const
1941         {return &info;}
1942 
1943     /**
1944      * Whether device is currently enabled.  If invalid audio
1945      * settings are selected, it will be disabled until supported
1946      * values are supplied.
1947      *
1948      * @return enable state.
1949      * @see #setAudio #setInfo
1950      */
is_enabled(void)1951     inline bool is_enabled(void) const
1952         {return enabled;}
1953 };
1954 
1955 /**
1956  * An object that is used to sequence and extract telephony tones
1957  * based on a telephony tone descriptor retrieved from the parsed
1958  * international telephony tone database.
1959  *
1960  * @author David Sugar <dyfet@ostel.com>
1961  * @short telephony tone sequencing object.
1962  */
1963 class __EXPORT TelTone : public AudioTone
1964 {
1965 private:
1966     __DELETE_DEFAULTS(TelTone);
1967 
1968 public:
1969     typedef struct _tonedef {
1970         struct _tonedef *next;
1971         timeout_t duration, silence;
1972         unsigned count;
1973         unsigned short f1, f2;
1974     } tonedef_t;
1975 
1976     typedef struct _tonekey {
1977         struct _tonekey *next;
1978         struct _tonedef *first;
1979         struct _tonedef *last;
1980         char id[1];
1981     } tonekey_t;
1982 
1983     /**
1984      * Create a tone sequencing object for a specific telephony tone
1985      * key id.
1986      *
1987      * @param key for telephony tone.
1988      * @param level for generated tones.
1989      * @param frame timing to use in processing.
1990      */
1991     TelTone(tonekey_t *key, Level level, timeout_t frame = 20);
1992     ~TelTone();
1993 
1994     /**
1995      * Generate and retrieve one frame of linear audio data for
1996      * the telephony tone sequence being created.
1997      *
1998      * @return pointer to samples generated.
1999      */
2000     Linear getFrame(void) __OVERRIDE;
2001 
2002     /**
2003      * Check if all audio frames for this tone has been created.
2004      * Some telephony tones, such as dialtone, may be infinite...
2005      *
2006      * @return true if audio is complete.
2007      */
2008     bool is_complete(void) __OVERRIDE;
2009 
2010 
2011     /**
2012      * Load a teltones database file into memory.
2013      *
2014      * @return true if successful
2015      * @param pathname of file to load.
2016      * @param locale to optionally load.
2017      */
2018     static bool load(const char *pathname, const char *locale = NULL);
2019 
2020     /**
2021      * find an entry in the teltones database.
2022      *
2023      * @return key of tone list if found, else NULL
2024      * @param tone name
2025      * @param locale to optionally search under
2026      */
2027     static tonekey_t *find(const char *tone, const char *locale = NULL);
2028 
2029 protected:
2030     tonekey_t *tone;
2031     tonedef_t *def;
2032     unsigned remaining, silent, count;
2033     timeout_t framing;
2034     Level level;
2035     bool complete;
2036 };
2037 
2038 /**
2039  * DTMFTones is used to generate a series of dtmf audio data from a
2040  * "telephone" number passed as an ASCII string.  Each time getFrame()
2041  * is called, the next audio frame of dtmf audio will be created
2042  * and pulled.
2043  *
2044  * @author David Sugar <dyfet@ostel.com>
2045  * @short Generate DTMF audio
2046  */
2047 class __EXPORT DTMFTones : public AudioTone
2048 {
2049 private:
2050     __DELETE_DEFAULTS(DTMFTones);
2051 
2052 protected:
2053     unsigned remaining, dtmfframes;
2054     timeout_t frametime;
2055     const char *digits;
2056     Level level;
2057     bool complete;
2058 
2059 public:
2060     /**
2061      * Generate a dtmf dialer for a specified dialing string.
2062      *
2063      * @param digits to generate tone dialing for.
2064      * @param level for dtmf.
2065      * @param duration timing for generated audio.
2066      * @param interdigit timing, should be multiple of frame.
2067      */
2068     DTMFTones(const char *digits, Level level, timeout_t duration = 20, timeout_t interdigit = 60);
2069 
2070     ~DTMFTones();
2071 
2072     Linear getFrame(void) __OVERRIDE;
2073     bool is_complete(void) __OVERRIDE;
2074 };
2075 
2076 /**
2077  * MFTones is used to generate a series of mf audio data from a
2078  * "telephone" number passed as an ASCII string.  Each time getFrame()
2079  * is called, the next audio frame of dtmf audio will be created
2080  * and pulled.
2081  *
2082  * @author David Sugar <dyfet@ostel.com>
2083  * @short Generate MF audio
2084  */
2085 class __EXPORT MFTones : public AudioTone
2086 {
2087 private:
2088     __DELETE_DEFAULTS(MFTones);
2089 
2090 protected:
2091     unsigned remaining, mfframes;
2092     timeout_t frametime;
2093     const char *digits;
2094     Level level;
2095     bool complete, kflag;
2096 
2097 public:
2098     /**
2099      * Generate a mf dialer for a specified dialing string.
2100      *
2101      * @param digits to generate tone dialing for.
2102      * @param level for mf.
2103      * @param duration timing for generated audio.
2104      * @param interdigit timing, should be multiple of frame.
2105      */
2106     MFTones(const char *digits, Level level, timeout_t duration = 20, timeout_t interdigit = 60);
2107 
2108     ~MFTones();
2109 
2110     Linear getFrame(void) __OVERRIDE;
2111     bool is_complete(void) __OVERRIDE;
2112 };
2113 
2114 
2115 /**
2116  * DTMFDetect is used for detecting DTMF tones in a stream of audio.
2117  * It currently only supports 8000Hz input.
2118  */
2119 class __EXPORT DTMFDetect : public Audio
2120 {
2121 private:
2122     __DELETE_COPY(DTMFDetect);
2123 
2124 public:
2125     DTMFDetect();
2126     ~DTMFDetect();
2127 
2128     /**
2129      * This routine is used to push linear audio data into the
2130      * dtmf tone detection analysizer.  It may be called multiple
2131      * times and results fetched later.
2132      *
2133      * @param buffer of audio data in native machine endian to analysize.
2134      * @param count of samples to analysize from buffer.
2135      */
2136     int putSamples(Linear buffer, int count);
2137 
2138     /**
2139      * Copy detected dtmf results into a data buffer.
2140      *
2141      * @param data buffer to copy into.
2142      * @param size of data buffer to copy into.
2143      */
2144     int getResult(char *data, int size);
2145 
2146 protected:
2147     void goertzelInit(goertzel_state_t *s, tone_detection_descriptor_t *t);
2148     void goertzelUpdate(goertzel_state_t *s, Sample x[], int samples);
2149     float goertzelResult(goertzel_state_t *s);
2150 
2151 private:
2152     dtmf_detect_state_t *state;
2153     tone_detection_descriptor_t dtmf_detect_row[4];
2154     tone_detection_descriptor_t dtmf_detect_col[4];
2155     tone_detection_descriptor_t dtmf_detect_row_2nd[4];
2156     tone_detection_descriptor_t dtmf_detect_col_2nd[4];
2157     tone_detection_descriptor_t fax_detect;
2158     tone_detection_descriptor_t fax_detect_2nd;
2159 };
2160 
2161 } // namespace ucommon
2162 
2163 #endif
2164 
2165