source: roaraudio/roarclients/roardtmf.c @ 5547:3f5d08ba722e

Last change on this file since 5547:3f5d08ba722e was 5547:3f5d08ba722e, checked in by phi, 7 years ago

added an extended DTMF mode

File size: 4.0 KB
Line 
1//roardtmf.c:
2
3/*
4 *      Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2008-2012
5 *
6 *  This file is part of roarclients a part of RoarAudio,
7 *  a cross-platform sound system for both, home and professional use.
8 *  See README for details.
9 *
10 *  This file is free software; you can redistribute it and/or modify
11 *  it under the terms of the GNU General Public License version 3
12 *  as published by the Free Software Foundation.
13 *
14 *  RoarAudio is distributed in the hope that it will be useful,
15 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
16 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 *  GNU General Public License for more details.
18 *
19 *  You should have received a copy of the GNU General Public License
20 *  along with this software; see the file COPYING.  If not, write to
21 *  the Free Software Foundation, 51 Franklin Street, Fifth Floor,
22 *  Boston, MA 02110-1301, USA.
23 *
24 */
25
26#include <roaraudio.h>
27#include <libroardsp/libroardsp.h>
28
29#ifdef ROAR_HAVE_LIBM
30#include <math.h>
31
32// in ms:
33#if 0
34#define ON_TIME  (180)
35#define OFF_TIME ( 80)
36#else
37#define ON_TIME  (180)
38#define OFF_TIME ( 80)
39#endif
40#define SUM_TIME (ON_TIME+OFF_TIME)
41
42void usage (void) {
43 printf("roardtmf [OPTIONS]... PHONE NUMBER\n");
44
45 printf("\nOptions:\n\n");
46
47 printf("  --server SERVER    - Set server hostname\n"
48        "  --rate  -R RATE    - Set sample rate\n"
49        "  --help             - Show this help\n"
50        "  --mode-dtmf        - Use standard DTMF (default)\n"
51        "  --mode-roar        - Use RoarAudio extended DTMF\n"
52       );
53
54}
55
56int main (int argc, char * argv[]) {
57 enum {
58  MODE_DTMF,
59  MODE_ROAR
60 } mode = MODE_DTMF;
61 int    rate     = ROAR_RATE_DEFAULT;
62 const char * server   = NULL;
63 const char * k;
64 int    i;
65 const char * name = "roardtmf";
66 roar_vs_t * vss = NULL;
67 const char * tones = NULL;
68 void * buf;
69 size_t len_break;
70 size_t len_tone;
71 size_t samples;
72 size_t len;
73 uint16_t c;
74 int err;
75
76 for (i = 1; i < argc; i++) {
77  k = argv[i];
78
79  if ( !strcmp(k, "--server") || !strcmp(k, "-s") ) {
80   server = argv[++i];
81  } else if ( !strcmp(k, "--rate") || !strcmp(k, "-r") || !strcmp(k, "-R") ) {
82   rate = roar_str2rate(argv[++i]);
83  } else if ( !strcmp(k, "--mode-dtmf") ) {
84   mode = MODE_DTMF;
85  } else if ( !strcmp(k, "--mode-roar") ) {
86   mode = MODE_ROAR;
87  } else if ( !strcmp(k, "--help") ) {
88   usage();
89   return 0;
90  } else if ( tones == NULL ) {
91   tones = argv[i++];
92  } else {
93   fprintf(stderr, "Error: unknown argument: %s\n", k);
94   usage();
95   return 1;
96  }
97 }
98
99 if ( tones == NULL ) {
100  return 1;
101 }
102
103 len_break = roar_dtmf_mus2samples(OFF_TIME * 1000, rate);
104 len_tone  = roar_dtmf_mus2samples(ON_TIME  * 1000, rate);
105 samples = len_break + len_tone;
106
107/*
108 printf("samples=%llu\n", (long long unsigned int)samples);
109 return 0;
110*/
111
112 if ( (buf = roar_mm_malloc((len = 16*samples/8))) == NULL )
113  return 4;
114
115 if ( (vss = roar_vs_new_playback(server, name, rate, 1, ROAR_CODEC_DEFAULT, 16, &err)) == NULL ) {
116  fprintf(stderr, "Error: can not start playback: %s\n", roar_vs_strerr(err));
117  roar_mm_free(buf);
118  return 1;
119 }
120
121 while ( (c = *tones) != 0 ) {
122  tones++;
123
124  if ( mode == MODE_DTMF ) {
125   if ( c == '+' || c == '.' || c == '_' )
126    continue;
127  } else if ( mode == MODE_ROAR ) {
128   c = ROAR_DTMF_CHAR_ROAR(c);
129  }
130
131  if ( mode == MODE_DTMF && c == ' ' ) {
132   roar_dtmf_break(buf, len_tone, rate, ROAR_DTMF_OPTIONS_NONE);
133  } else {
134   if ( roar_dtmf_tone(buf, len_tone, rate, ROAR_DTMF_OPTIONS_NONE, c) == -1 ) {
135    roar_mm_free(buf);
136    return 5;
137   }
138  }
139  roar_dtmf_break(buf+len_tone*sizeof(int16_t), len_break, rate, ROAR_DTMF_OPTIONS_NONE);
140  if ( roar_vs_write(vss, buf, len, &err) != (ssize_t)len ) {
141   fprintf(stderr, "Error: can not write data: %s\n", roar_vs_strerr(err));
142   roar_mm_free(buf);
143   return 5;
144  }
145 }
146
147 roar_vs_sync(vss, ROAR_VS_WAIT, NULL);
148
149 roar_vs_close(vss, ROAR_VS_FALSE, 0);
150
151 roar_mm_free(buf);
152
153 return 0;
154}
155
156#else
157int main (void) {
158 fprintf(stderr, "Error: No Math library support compiled in.\n");
159 return 1;
160}
161#endif
162
163//ll
Note: See TracBrowser for help on using the repository browser.