Bayonne 3 - API
 All Classes Namespaces Files Functions Variables Typedefs Macros
bayonne.h
Go to the documentation of this file.
1 // Copyright (C) 2008-2009 David Sugar, Tycho Softworks.
2 //
3 // This program is free software; you can redistribute it and/or modify
4 // it under the terms of the GNU General Public License as published by
5 // the Free Software Foundation; either version 3 of the License, or
6 // (at your option) any later version.
7 //
8 // This program is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.See the
11 // GNU General Public License for more details.
12 //
13 // You should have received a copy of the GNU General Public License
14 // along with this program. If not, see <http://www.gnu.org/licenses/>.
15 
22 #ifndef _BAYONNE_H_
23 #define _BAYONNE_H_
24 
25 #define BAYONNE_NAMESPACE bayonne
26 #define NAMESPACE_BAYONNE namespace bayonne {
27 
28 #ifndef UCOMMON_UCOMMON_H_
29 #include <ucommon/ucommon.h>
30 #endif
31 
49 using namespace UCOMMON_NAMESPACE;
50 
56 class __EXPORT Script : public CountedObject, private memalloc
57 {
58 public:
59  class interp;
60  class header;
61  class checks;
62 
66  typedef bool (Script::interp::*method_t)(void);
67 
74  typedef struct line {
75  struct line *next;
76  union {
77  const char *cmd;
78  header *sub;
79  };
80  char **argv;
81  unsigned short loop, argc;
82  unsigned lnum, mask;
83  method_t method;
84  } line_t;
85 
89  typedef const char *(*check_t)(Script *img, Script::header *scr, Script::line_t *line);
90 
96  typedef struct keyword
97  {
98  public:
99  const char *name;
100  method_t method;
101  check_t check;
102  struct keyword *next;
103  } keyword_t;
104 
112  class __EXPORT strict : public LinkedObject
113  {
114  public:
115  const char *id;
116 
117  public:
118  static bool find(Script *img, header *scr, const char *id);
119  static void createVar(Script *img, header *scr, const char *id);
120  static void createSym(Script *img, header *scr, const char *id);
121  static void createAny(Script *img, header *scr, const char *id);
122  static void createGlobal(Script *img, const char *id);
123 
124  void put(FILE *fp, const char *header);
125  };
126 
134  class __EXPORT symbol : public LinkedObject
135  {
136  public:
137  const char *name;
138  char *data;
139  unsigned size;
140  header *scope;
141  };
142 
148  class __EXPORT event : public LinkedObject
149  {
150  public:
151  line_t *first;
152  const char *name;
153  };
154 
158  typedef event event_t;
159 
168  class __EXPORT header : public LinkedObject
169  {
170  public:
171  LinkedObject *scoped;
172  LinkedObject *events;
173  LinkedObject *methods;
174  line_t *first;
175  const char *name;
176  unsigned resmask;
182  inline void link(header *scr)
183  {next = scr;};
184  };
185 
193  class __EXPORT checks
194  {
195  public:
196  static bool isValue(const char *text);
197  static bool isText(const char *text);
198 
199  static const char *chkPush(Script *img, header *scr, line_t *line);
200  static const char *chkApply(Script *img, header *scr, line_t *line);
201  static const char *chkIgnore(Script *img, header *scr, line_t *line);
202  static const char *chkNop(Script *img, header *scr, line_t *line);
203  static const char *chkExit(Script *img, header *scr, line_t *line);
204  static const char *chkVar(Script *img, header *scr, line_t *line);
205  static const char *chkConst(Script *img, header *scr, line_t *line);
206  static const char *chkSet(Script *img, header *scr, line_t *line);
207  static const char *chkClear(Script *img, header *scr, line_t *line);
208  static const char *chkError(Script *img, header *scr, line_t *line);
209  static const char *chkPack(Script *img, header *scr, line_t *line);
210  static const char *chkExpand(Script *img, header *scr, line_t *line);
211  static const char *chkGosub(Script *img, header *src, line_t *line);
212  static const char *chkGoto(Script *img, header *scr, line_t *line);
213  static const char *chkDo(Script *img, header *scr, line_t *line);
214  static const char *chkUntil(Script *img, header *scr, line_t *line);
215  static const char *chkWhile(Script *ing, header *scr, line_t *line);
216  static const char *chkConditional(Script *img, header *scr, line_t *line);
217  static const char *chkContinue(Script *img, header *scr, line_t *line);
218  static const char *chkBreak(Script *img, header *scr, line_t *line);
219  static const char *chkLoop(Script *img, header *scr, line_t *line);
220  static const char *chkPrevious(Script *img, header *scr, line_t *line);
221  static const char *chkIndex(Script *img, header *scr, line_t *line);
222  static const char *chkForeach(Script *img, header *scr, line_t *line);
223  static const char *chkCase(Script *img, header *scr, line_t *line);
224  static const char *chkEndcase(Script *img, header *scr, line_t *line);
225  static const char *chkOtherwise(Script *img, header *scr, line_t *line);
226  static const char *chkIf(Script *img, header *scr, line_t *line);
227  static const char *chkElif(Script *img, header *scr, line_t *line);
228  static const char *chkElse(Script *img, header *scr, line_t *line);
229  static const char *chkEndif(Script *img, header *scr, line_t *line);
230  static const char *chkDefine(Script *img, header *scr, line_t *line);
231  static const char *chkInvoke(Script *img, header *scr, line_t *line);
232  static const char *chkWhen(Script *img, header *scr, line_t *line);
233  static const char *chkStrict(Script *img, header *scr, line_t *line);
234  static const char *chkExpr(Script *img, header *scr, line_t *line);
235  static const char *chkRef(Script *ing, header *scr, line_t *line);
236  };
237 
242  typedef struct {
243  header *scr;
244  header *scope;
245  event_t *event;
246  line_t *line;
247  unsigned short index;
248  unsigned short base;
249  unsigned short resmask;
250  } stack_t;
251 
262  class __EXPORT interp : protected memalloc
263  {
264  public:
265  typedef char num_t[16];
266 
267  interp();
268  virtual ~interp();
269 
275  bool step(void);
276 
285  bool attach(Script *image, const char *entry = NULL);
286 
290  void detach(void);
291 
297  void initialize(void);
298 
303  bool error(const char *text);
304 
309  unsigned getResource(void);
310 
311  protected:
312  symbol *find(const char *id);
313  void skip(void);
314  void push(void);
315  bool trylabel(const char *id);
316  bool tryexit(void);
317  void pullScope(void);
318  void pullBase(void);
319  void pullLoop(void);
320  bool pop(void);
321  void setStack(header *scr, event *ev = NULL);
322 
330  virtual bool match(const char *pattern, const char *name);
331 
339  virtual bool isInherited(const char *name);
340 
348  bool scriptEvent(const char *name);
349 
355  event *scriptMethod(const char *name);
356 
357  stack_t *stack;
358  object_pointer<Script> image;
359  LinkedObject **syms;
360  unsigned frame;
361 
362  char *getTemp(void);
363  bool setConst(const char *id, const char *value);
364  symbol *createSymbol(const char *id);
365  symbol *getVar(const char *id, const char *value = NULL);
366  const char *getValue(void);
367  const char *getContent(void);
368  const char *getContent(const char *text);
369  const char *getKeyword(const char *id);
370  method_t getLooping(void);
371  bool isConditional(unsigned index);
372  void setRef(header *scope, const char *id, char *data, unsigned size);
373  void getParams(header *scope, line_t *line);
374  void startScript(header *scr);
375 
376  virtual unsigned getTypesize(const char *type_id);
377  virtual const char *getTypeinit(const char *type_id);
378  virtual const char *getFormat(symbol *sym, const char *id, char *temp);
379  virtual bool getCondition(const char *test, const char *value);
380  const char *getIndex(void);
381 
382  private:
383  bool getExpression(unsigned index);
384 
385  char *errmsg;
386  char *temps[3];
387  unsigned tempindex;
388  };
389 
396  class __EXPORT error : public OrderedObject
397  {
398  private:
399  friend class Script;
400  error(Script *img, unsigned line, const char *str);
401 
402  public:
403  char *errmsg;
404  unsigned errline;
405  };
406 
414  class __EXPORT methods : public interp
415  {
416  public:
417  bool scrPush(void);
418  bool scrApply(void);
419  bool scrExpr(void);
420  bool scrVar(void);
421  bool scrSet(void);
422  bool scrAdd(void);
423  bool scrClear(void);
424  bool scrConst(void);
425  bool scrPause(void);
426  bool scrNop(void);
427  bool scrPack(void);
428  bool scrExpand(void);
429  bool scrExit(void);
430  bool scrReturn(void);
431  bool scrError(void);
432  bool scrRestart(void);
433  bool scrGosub(void);
434  bool scrGoto(void);
435  bool scrDo(void);
436  bool scrLoop(void);
437  bool scrUntil(void);
438  bool scrWhile(void);
439  bool scrBreak(void);
440  bool scrContinue(void);
441  bool scrForeach(void);
442  bool scrPrevious(void);
443  bool scrRepeat(void);
444  bool scrIndex(void);
445  bool scrCase(void);
446  bool scrEndcase(void);
447  bool scrOtherwise(void);
448  bool scrIf(void);
449  bool scrElif(void);
450  bool scrElse(void);
451  bool scrEndif(void);
452  bool scrDefine(void);
453  bool scrInvoke(void);
454  bool scrWhen(void);
455  bool scrRef(void);
456  };
457 
458  ~Script();
459 
460  static unsigned stepping;
461  static unsigned indexing;
462  static size_t paging;
463  static unsigned sizing;
464  static unsigned stacking;
465  static unsigned decimals;
476  static Script *append(Script *merge, const char *filename, Script *config = NULL);
477 
486  static Script *compile(const char *filename, Script *config = NULL);
487 
488 
489 
499  static Script *merge(const char *filename, Script *root = NULL);
500 
508  static Script *merge(Script *image, Script *root);
509 
515  static void assign(keyword_t *list);
516 
524  static keyword_t *find(const char *id);
525 
529  static void init(void);
530 
531  static unsigned offset(const char *list, unsigned index);
532  static void copy(const char *list, char *item, unsigned size);
533  static unsigned count(const char *list);
534  static const char *get(const char *list, unsigned offset);
535  static char *get(char *list, unsigned offset);
536  static header *find(Script *img, const char *id);
537  static bool isEvent(header *scr, const char *id);
538 
539  header *first;
540  LinkedObject **scripts;
541 
542  bool push(line_t *line);
543  method_t pull(void);
544  method_t looping(void);
545 
546  inline unsigned getErrors(void)
547  {return errors;};
548 
549  inline LinkedObject *getListing(void)
550  {return errlist.begin();};
551 
552  inline const char *getFilename(void)
553  {return filename;};
554 
555  inline bool isStrict(void)
556  {return global != NULL;};
557 
558  inline unsigned getLines(void)
559  {return lines;};
560 
561 private:
562  friend class strict;
563  friend class checks;
564  friend class error;
565  friend class interp;
566  friend class methods;
567 
568  Script();
569 
570  void errlog(unsigned line, const char *fmt, ...);
571 
572  unsigned errors;
573  unsigned loop;
574  unsigned lines;
575  bool thencheck;
576  line_t **stack;
577  LinkedObject *global;
578  OrderedIndex errlist;
579  object_pointer<Script> shared;
580  const char *filename;
581  LinkedObject *headers;
582 };
583 
592 class __EXPORT Audio
593 {
594 public:
595  typedef int16_t snd16_t;
596  typedef int32_t snd32_t;
597  typedef int16_t level_t;
598  typedef int16_t sample_t;
599  typedef int16_t *linear_t;
600 
601 #if _MSC_VER > 1400 // windows broken dll linkage issue...
602  const static unsigned ndata = (-1);
603 #else
604  const static unsigned ndata;
605 #endif
606 
607  typedef struct {
608  float v2;
609  float v3;
610  float fac;
611  } goertzel_state_t;
612 
613  typedef struct {
614  int hit1;
615  int hit2;
616  int hit3;
617  int hit4;
618  int mhit;
619 
620  goertzel_state_t row_out[4];
621  goertzel_state_t col_out[4];
622  goertzel_state_t row_out2nd[4];
623  goertzel_state_t col_out2nd[4];
624  goertzel_state_t fax_tone;
625  goertzel_state_t fax_tone2nd;
626  float energy;
627 
628  int current_sample;
629  char digits[129];
630  int current_digits;
631  int detected_digits;
632  int lost_digits;
633  int digit_hits[16];
634  int fax_hits;
635  } dtmf_detect_state_t;
636 
637  typedef struct {
638  float fac;
639  } tone_detection_descriptor_t;
640 
641  typedef unsigned char *encoded_t;
642 
646  enum Rate {
647  rateUnknown = 0,
648  rate6khz = 6000,
649  rate8khz = 8000,
650  rate11khz = 11025,
651  rate16khz = 16000,
652  rate22khz = 22050,
653  rate32khz = 32000,
654  rate44khz = 44100
655  };
656 
657  typedef enum Rate rate_t;
658 
662  enum Mode {
663  modeRead,
664  modeReadAny,
665  modeReadOne,
666  modeWrite,
667  modeCache,
668  modeInfo,
669  modeFeed,
670 
671  modeAppend, // app specific placeholders...
672  modeCreate
673  };
674 
675  typedef enum Mode mode_t;
676 
680  enum Encoding {
681  unknownEncoding = 0,
682  g721ADPCM,
683  g722Audio,
684  g722_7bit,
685  g722_6bit,
686  g723_2bit,
687  g723_3bit,
688  g723_5bit,
689  gsmVoice,
690  msgsmVoice,
691  mulawAudio,
692  alawAudio,
693  mp1Audio,
694  mp2Audio,
695  mp3Audio,
696  okiADPCM,
697  voxADPCM,
698  sx73Voice,
699  sx96Voice,
700 
701  // Please keep the PCM types at the end of the list -
702  // see the "is this PCM or not?" code in
703  // AudioFile::close for why.
704  cdaStereo,
705  cdaMono,
706  pcm8Stereo,
707  pcm8Mono,
708  pcm16Stereo,
709  pcm16Mono,
710  pcm32Stereo,
711  pcm32Mono,
712 
713  // speex codecs
714  speexVoice, // narrow band
715  speexAudio,
716 
717  g729Audio,
718  ilbcAudio,
719  speexUltra,
720 
721  speexNarrow = speexVoice,
722  speexWide = speexAudio,
723  g723_4bit = g721ADPCM
724  };
725  typedef enum Encoding encoding_t;
726 
730  enum Format {
731  raw,
732  snd,
733  riff,
734  wave
735  };
736  typedef enum Format format_t;
737 
741  enum Error {
742  errSuccess = 0,
743  errReadLast,
744  errNotOpened,
745  errEndOfFile,
746  errStartOfFile,
747  errRateInvalid,
748  errEncodingInvalid,
749  errReadInterrupt,
750  errWriteInterrupt,
751  errReadFailure,
752  errWriteFailure,
753  errReadIncomplete,
754  errWriteIncomplete,
755  errRequestInvalid,
756  errTOCFailed,
757  errStatFailed,
758  errInvalidTrack,
759  errPlaybackFailed,
760  errNotPlaying,
761  errNoCodec
762  };
763  typedef enum Error error_t;
764 
768  class __EXPORT Info
769  {
770  public:
771  format_t format;
772  encoding_t encoding;
773  unsigned long rate;
774  unsigned long bitrate;
775  unsigned order;
776  unsigned framesize, framecount, headersize, padding;
777  timeout_t framing;
778  char *annotation;
779 
780  Info();
781  void clear(void);
782  void set(void);
783  void setFraming(timeout_t frame);
784  void setRate(rate_t rate);
785  };
786 
787  typedef Info info_t;
788 
795  static level_t tolevel(float dbm);
796 
803  static float todbm(level_t power);
804 
812  static bool is_available(unsigned device = 0);
813 
821  static const char *getMIME(Info &info);
822 
830  static const char *getName(encoding_t encoding);
831 
839  static const char *getExtension(encoding_t encoding);
840 
851  static encoding_t getEncoding(const char *name);
852 
859  static encoding_t getStereo(encoding_t encoding);
860 
867  static encoding_t getMono(encoding_t encoding);
868 
875  static bool is_linear(encoding_t encoding);
876 
885  static bool is_buffered(encoding_t encoding);
886 
893  static bool is_mono(encoding_t encoding);
894 
901  static bool is_stereo(encoding_t encoding);
902 
910  static rate_t getRate(encoding_t encoding);
911 
920  static rate_t getRate(encoding_t e, rate_t request);
921 
929  static timeout_t getFraming(encoding_t encoding, timeout_t timeout = 0);
930 
938  static timeout_t getFraming(Info &info, timeout_t timeout = 0);
939 
947  static bool is_endian(encoding_t encoding);
948 
956  static bool is_endian(Info &info);
957 
967  static bool swapEndian(encoding_t encoding, void *buffer, unsigned number);
968 
977  static void swapEncoded(Info &info, encoded_t data, size_t bytes);
978 
989  static bool swapEndian(Info &info, void *buffer, unsigned number);
990 
999  static level_t impulse(encoding_t encoding, void *buffer, unsigned number);
1000 
1009  static level_t impulse(Info &info, void *buffer, unsigned number = 0);
1010 
1020  static level_t peak(encoding_t encoding, void *buffer, unsigned number);
1021 
1031  static level_t peak(Info &info, void *buffer, unsigned number = 0);
1032 
1040  static void toTimestamp(timeout_t duration, char *address, size_t size);
1041 
1048  static timeout_t toTimeout(const char *timestamp);
1049 
1072  static int getFrame(encoding_t encoding, int samples = 0);
1073 
1086  static int getCount(encoding_t encoding);
1087 
1096  static unsigned long toSamples(encoding_t encoding, size_t bytes);
1097 
1106  static unsigned long toSamples(Info &info, size_t bytes);
1107 
1116  static size_t toBytes(Info &info, unsigned long number);
1117 
1126  static size_t toBytes(encoding_t encoding, unsigned long number);
1127 
1136  static void fill(unsigned char *address, int number, encoding_t encoding);
1137 
1145  static size_t maxFramesize(Info &info);
1146 
1154  class __EXPORT resample
1155  {
1156  protected:
1157  unsigned mfact, dfact, max;
1158  unsigned gpos, ppos;
1159  sample_t last;
1160  linear_t buffer;
1161 
1162  public:
1163  resample(rate_t mul, rate_t div);
1164  ~resample();
1165 
1166  size_t process(linear_t from, linear_t to, size_t count);
1167  size_t estimate(size_t count);
1168  };
1169 };
1170 
1177 class __EXPORT Phrasebook : public LinkedObject
1178 {
1179 public:
1180  typedef struct
1181  {
1182  bool zeroflag;
1183  unsigned long last;
1184  unsigned pos;
1185  unsigned max;
1186  size_t size;
1187  char *bp;
1188  const char *list[1];
1189  } rule_t;
1190 
1191 protected:
1192  static void _add(const char *text, rule_t *state);
1193 
1194  static void _dup(const char *text, rule_t *state);
1195 
1196  static void _lownumber(int num, rule_t *state);
1197 
1198 public:
1199  Phrasebook(bool primary);
1200 
1201  virtual bool id(const char *lang) = 0;
1202 
1203  virtual const char *path(void);
1204 
1205  virtual void number(const char *text, rule_t *state);
1206 
1207  virtual void order(const char *text, rule_t *state);
1208 
1209  virtual void spell(const char *text, rule_t *state);
1210 
1211  virtual void literal(const char *text, rule_t *state);
1212 
1213  virtual void weekday(const char *text, rule_t *state);
1214 
1215  virtual void date(const char *text, rule_t *state);
1216 
1217  virtual void fulldate(const char *text, rule_t *state);
1218 
1219  virtual void year(const char *text, rule_t *state);
1220 
1221  virtual void time(const char *text, rule_t *state);
1222 
1223  virtual void zero(const char *text, rule_t *state);
1224 
1225  virtual void single(const char *text, rule_t *state);
1226 
1227  virtual void plural(const char *text, rule_t *state);
1228 
1229  virtual void nonzero(const char *text, rule_t *state);
1230 
1231  static Phrasebook *find(const char *locale = NULL);
1232 
1233  static void init(rule_t *state, size_t size);
1234 
1235  static void reset(rule_t *state);
1236 
1237 };
1238 
1243 class __EXPORT Env
1244 {
1245 private:
1246  static shell_t *sys;
1247  static bool daemon_flag;
1248  static bool tool_flag;
1249 
1250 public:
1251  class __LOCAL pathinfo_t
1252  {
1253  public:
1254  Phrasebook *book;
1255  const char *voices;
1256  const char *appname;
1257 
1258  inline pathinfo_t()
1259  {book = NULL; voices = appname = NULL;};
1260 
1261  inline void reset(void)
1262  {book = NULL; voices = appname = NULL;};
1263 
1264  inline Phrasebook *operator->()
1265  {return book;};
1266  };
1267 
1268  static void init(shell_t *args);
1269 
1270  static void tool(shell_t *args);
1271 
1272  static inline void set(const char *id, const char *value)
1273  {sys->setsym(id, value);};
1274 
1275  static inline const char *get(const char *id)
1276  {return sys->getsym(id);};
1277 
1278  static inline const char *env(const char *id)
1279  {return sys->getsym(id);};
1280 
1281  static const char *config(const char *name);
1282 
1283  static const char *path(pathinfo_t& pathinfo, const char *path, char *buffer, size_t size, bool write = false);
1284 };
1285 
1294 class __EXPORT Tonegen : public Audio, protected Env
1295 {
1296 public:
1297  typedef struct _tonedef {
1298  struct _tonedef *next;
1299  timeout_t duration, silence;
1300  unsigned count;
1301  unsigned short f1, f2;
1302  } def_t;
1303 
1304  typedef struct _tonekey {
1305  struct _tonekey *next;
1306  struct _tonedef *first;
1307  struct _tonedef *last;
1308  char id[1];
1309  } key_t;
1310 
1311 protected:
1312  rate_t rate;
1313  unsigned samples;
1314  linear_t frame;
1315  double df1, df2, p1, p2;
1316  level_t m1, m2;
1317  bool silencer;
1318  bool complete;
1319  key_t *tone;
1320  def_t *def;
1321  unsigned remaining, silent, count;
1322  timeout_t framing;
1323  level_t level;
1324 
1328  void silence(void);
1329 
1333  void reset(void);
1334 
1338  void cleanup(void);
1339 
1346  void single(unsigned freq, level_t level);
1347 
1356  void dual(unsigned f1, unsigned f2, level_t l1, level_t l2);
1357 
1358 public:
1365  Tonegen(timeout_t duration = 20, rate_t rate = rate8khz);
1366 
1375  Tonegen(key_t *key, level_t level, timeout_t frame = 20);
1376 
1387  Tonegen(unsigned f1, unsigned f2, level_t l1, level_t l2,
1388  timeout_t duration = 20, rate_t sample = rate8khz);
1389 
1398  Tonegen(unsigned freq, level_t level, timeout_t duration = 20, rate_t sample = rate8khz);
1399 
1400  virtual ~Tonegen();
1401 
1407  inline rate_t getRate(void) const
1408  {return rate;};
1409 
1415  inline size_t getSamples(void) const
1416  {return samples;};
1417 
1423  bool is_silent(void);
1424 
1432  virtual linear_t getFrame(void);
1433 
1442  unsigned getFrames(linear_t buffer, unsigned number);
1443 
1450  inline bool is_complete(void)
1451  {return complete;};
1452 
1453  static bool load(const char *locale = NULL);
1454 
1455  static key_t *find(const char *id, const char *locale = NULL);
1456 };
1457 
1467 class __EXPORT DTMFDialer : public Tonegen
1468 {
1469 protected:
1470  unsigned dtmfframes;
1471  const char *digits;
1472 
1473 public:
1482  DTMFDialer(const char *digits, level_t level, timeout_t duration = 20, timeout_t interdigit = 60);
1483 
1484  ~DTMFDialer();
1485 
1486  linear_t getFrame(void);
1487 };
1488 
1498 class __EXPORT MFDialer : public Tonegen
1499 {
1500 protected:
1501  unsigned mfframes;
1502  const char *digits;
1503  bool kflag;
1504 
1505 public:
1514  MFDialer(const char *digits, level_t level, timeout_t duration = 20, timeout_t interdigit = 60);
1515 
1516  ~MFDialer();
1517 
1518  linear_t getFrame(void);
1519 };
1520 
1525 class __EXPORT DTMFDetect : public Audio
1526 {
1527 public:
1528  DTMFDetect();
1529  ~DTMFDetect();
1530 
1539  int putSamples(linear_t buffer, int count);
1540 
1547  int getResult(char *data, int size);
1548 
1549 protected:
1550  void goertzelInit(goertzel_state_t *s, tone_detection_descriptor_t *t);
1551  void goertzelUpdate(goertzel_state_t *s, sample_t x[], int samples);
1552  float goertzelResult(goertzel_state_t *s);
1553 
1554 private:
1555  dtmf_detect_state_t *state;
1556  tone_detection_descriptor_t dtmf_detect_row[4];
1557  tone_detection_descriptor_t dtmf_detect_col[4];
1558  tone_detection_descriptor_t dtmf_detect_row_2nd[4];
1559  tone_detection_descriptor_t dtmf_detect_col_2nd[4];
1560  tone_detection_descriptor_t fax_detect;
1561  tone_detection_descriptor_t fax_detect_2nd;
1562 };
1563 
1576 class __EXPORT AudioCodec : public Audio, public LinkedObject
1577 {
1578 protected:
1579  encoding_t encoding;
1580  const char *name;
1581  info_t info;
1582 
1583  AudioCodec();
1584 
1592  virtual AudioCodec *getByFormat(const char *format)
1593  {return this;};
1594 
1601  virtual AudioCodec *getByInfo(info_t& info)
1602  {return this;};
1603 
1604 public:
1611  AudioCodec(const char *name, encoding_t encoding);
1612 
1613  inline const char *getName(void) const
1614  {return name;};
1615 
1616  inline const char *getDescription(void) const
1617  {return info.annotation;};
1618 
1619  virtual ~AudioCodec() {};
1620 
1627  static void release(AudioCodec *codec);
1628 
1629  static AudioCodec *begin(void);
1638  static AudioCodec *get(encoding_t encoding, const char *format = NULL);
1639 
1647  static AudioCodec *get(info_t& info);
1648 
1657  virtual level_t impulse(void *buffer, unsigned number = 0);
1658 
1666  virtual level_t peak(void *buffer, unsigned number = 0);
1667 
1678  virtual bool is_silent(level_t threashold, void *buffer, unsigned number = 0);
1679 
1688  virtual unsigned encode(linear_t buffer, void *dest, unsigned number = 0) = 0;
1689 
1698  virtual unsigned encodeBuffered(linear_t Buffer, encoded_t dest, unsigned number);
1699 
1708  virtual unsigned decode(linear_t buffer, void *source, unsigned number = 0) = 0;
1709 
1719  virtual unsigned decodeBuffered(linear_t buffer, encoded_t source, unsigned len);
1720 
1726  virtual unsigned getEstimated(void);
1727 
1733  virtual unsigned getRequired(void);
1734 
1744  virtual unsigned getPacket(encoded_t destination, encoded_t data, unsigned size);
1745 
1751  inline info_t getInfo(void) const
1752  {return info;};
1753 };
1754 
1763 class __EXPORT AudioFile: public Audio
1764 {
1765 protected:
1766  char *pathname;
1767  size_t header; // offset to start of audio
1768  size_t minimum; // minimum sample size required
1769  size_t iolimit;
1770  size_t pos, eof; // saved position mark...
1771  info_t info;
1772  mode_t mode;
1773  fsys_t fs;
1774 
1775  void initialize(void);
1776  void getWaveFormat(int size);
1777  AudioCodec *getCodec(void);
1778 
1779  ssize_t read(void *buf, size_t len);
1780  ssize_t write(void *buf, size_t len);
1781  ssize_t peek(void *buf, size_t len);
1782  bool seek(size_t pos);
1783 
1784  virtual const char *getContinuation();
1785 
1795  unsigned short getShort(unsigned char *data) const;
1796 
1805  void setShort(unsigned char *data, unsigned short value);
1806 
1816  unsigned long getLong(unsigned char *data) const;
1817 
1826  void setLong(unsigned char *data, unsigned long value);
1827 
1828 public:
1835  AudioFile(const char *name, unsigned long offset = 0);
1836 
1844  AudioFile(const char *name, info_t& info, unsigned long minimum = 0);
1845 
1849  AudioFile();
1850 
1851  virtual ~AudioFile();
1852 
1863  void open(const char *name, mode_t mode = modeWrite, timeout_t framing = 0);
1864 
1874  void create(const char *name, info_t& info, timeout_t framing = 0);
1875 
1882  time_t age(void);
1883 
1889  inline size_t getSize(void)
1890  {return maxFramesize(info);};
1891 
1897  void close(void);
1898 
1906  void clear(void);
1907 
1908  inline int err(void)
1909  {return fs.err();};
1910 
1922  ssize_t getBuffer(encoded_t buffer, size_t len = 0);
1923 
1932  unsigned getLinear(linear_t buffer, unsigned request = 0);
1933 
1941  ssize_t getNative(encoded_t data, size_t size);
1942 
1954  ssize_t putBuffer(encoded_t buffer, size_t len = 0);
1955 
1964  ssize_t putNative(encoded_t data, size_t size);
1965 
1974  unsigned putLinear(linear_t buffer, unsigned request = 0);
1975 
1988  int getSamples(void *buffer, unsigned samples = 0);
1989 
2001  int putSamples(void *buffer, unsigned samples = 0);
2002 
2010  int skip(long number);
2011 
2019  int setPosition(unsigned long samples = ~0l);
2020 
2028  int position(const char *timestamp);
2029 
2036  void getPosition(char *timestamp, size_t size);
2037 
2044  void setLimit(unsigned long maximum = 0l);
2045 
2052  inline void getInfo(info_t& output) const
2053  {output = info;};
2054 
2063  int setMinimum(unsigned long minimum);
2064 
2074  inline size_t getAbsolutePosition(void)
2075  {return pos;}
2076 
2088  unsigned long getPosition(void);
2089 
2090  inline bool operator!() const
2091  {return !fs;};
2092 
2093  inline operator bool() const
2094  {return is(fs);};
2095 
2096  inline bool is_open(void) const
2097  {return is(fs);};
2098 
2104  inline encoding_t getEncoding(void) const
2105  {return info.encoding;};
2106 
2112  inline format_t getFormat(void) const
2113  {return info.format;};
2114 
2121  inline unsigned getSampleRate(void) const
2122  {return info.rate;};
2123 
2129  inline char *getAnnotation(void) const
2130  {return info.annotation;};
2131 
2137  bool is_signed(void) const;
2138 };
2139 
2151 class __EXPORT AudioStream : public AudioFile
2152 {
2153 protected:
2154  AudioCodec *codec; // if needed
2155  encoded_t framebuf;
2156  bool streamable;
2157  linear_t bufferFrame;
2158  unsigned bufferPosition;
2159  unsigned bufferChannels;
2160  linear_t encBuffer, decBuffer;
2161  unsigned encSize, decSize;
2162 
2163  unsigned bufAudio(linear_t samples, unsigned count, unsigned size);
2164 
2165 public:
2169  AudioStream();
2170 
2178  AudioStream(const char *name, mode_t mode = modeRead, timeout_t framing = 0);
2179 
2188  AudioStream(const char *name, info_t& info, timeout_t framing = 0);
2189 
2190  virtual ~AudioStream();
2191 
2199  ssize_t getBuffer(encoded_t data, size_t count);
2200 
2208  void open(const char *name, mode_t mode = modeRead, timeout_t framing = 0);
2209 
2218  void create(const char *name, info_t& info, timeout_t framing = 0);
2219 
2223  void close(void);
2224 
2228  void flush(void);
2229 
2236  bool is_streamable(void);
2237 
2241  unsigned getCount(void); // frame count
2242 
2252  unsigned getEncoded(AudioCodec *codec, encoded_t address, unsigned frames = 1);
2253 
2263  unsigned putEncoded(AudioCodec *codec, encoded_t address, unsigned frames = 1);
2264 
2272  unsigned getEncoded(encoded_t address, unsigned frames = 1);
2273 
2281  unsigned putEncoded(encoded_t address, unsigned frames = 1);
2282 
2290  ssize_t getPacket(encoded_t data);
2291 
2300  unsigned getMono(linear_t buffer, unsigned frames = 1);
2301 
2310  unsigned getStereo(linear_t buffer, unsigned frames = 1);
2311 
2320  unsigned putMono(linear_t buffer, unsigned frames = 1);
2321 
2330  unsigned putStereo(linear_t buffer, unsigned frames = 1);
2331 
2341  unsigned bufMono(linear_t buffer, unsigned count);
2342 
2352  unsigned bufStereo(linear_t buffer, unsigned count);
2353 
2359  inline AudioCodec *getCodec(void)
2360  {return codec;};
2361 };
2362 
2370 class __EXPORT Serial
2371 {
2372 protected:
2373  int error;
2374 
2375  Serial();
2376  Serial(const Serial&);
2377 
2378 public:
2379  virtual ~Serial();
2380 
2381  inline int err(void)
2382  {return error;};
2383 
2384  virtual operator bool() = 0;
2385  virtual bool operator!() = 0;
2386  virtual void restore(void) = 0;
2387  virtual bool set(const char *format) = 0;
2388  virtual void dtr(timeout_t toggle_time) = 0;
2389  virtual size_t get(void *addr, size_t len) = 0;
2390  virtual size_t put(void *addr, size_t len) = 0;
2391  virtual void clear(void) = 0;
2392  virtual bool flush(timeout_t timeout) = 0;
2393  virtual bool wait(timeout_t timeout) = 0;
2394  virtual void text(char nl1 = 0x13, char nl2 = 0x10) = 0;
2395  virtual void bin(size_t size, timeout_t timeout = 0) = 0;
2396  virtual void sync(void) = 0;
2397 
2398  static Serial *create(const char *name);
2399 
2400  static stringpager *list(void);
2401 };
2402 
2410 class __EXPORT Module : public LinkedObject, protected Env
2411 {
2412 protected:
2413  virtual void start(void);
2414 
2415  virtual void stop(void);
2416 
2417 public:
2418  Module();
2419 
2420  static void startup(void);
2421  static void shutdown(void);
2422 };
2423 
2424 typedef Serial *serial_t;
2425 
2426 END_NAMESPACE
2427 
2428 #endif
2429 
#define NAMESPACE_BAYONNE
Definition: bayonne.h:26