source: roaraudio/roarclients/roardtmf.c

tip
Last change on this file was 6052:d48765b2475e, checked in by phi, 6 years ago

updated copyright headers

File size: 4.1 KB
Line 
1//roardtmf.c:
2
3/*
4 *      Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2008-2015
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   ROAR_CKHAVEARGS(1);
81   server = argv[++i];
82  } else if ( !strcmp(k, "--rate") || !strcmp(k, "-r") || !strcmp(k, "-R") ) {
83   ROAR_CKHAVEARGS(1);
84   rate = roar_str2rate(argv[++i]);
85  } else if ( !strcmp(k, "--mode-dtmf") ) {
86   mode = MODE_DTMF;
87  } else if ( !strcmp(k, "--mode-roar") ) {
88   mode = MODE_ROAR;
89  } else if ( !strcmp(k, "--help") ) {
90   usage();
91   return 0;
92  } else if ( tones == NULL ) {
93   tones = argv[i++];
94  } else {
95   fprintf(stderr, "Error: unknown argument: %s\n", k);
96   usage();
97   return 1;
98  }
99 }
100
101 if ( tones == NULL ) {
102  return 1;
103 }
104
105 len_break = roar_dtmf_mus2samples(OFF_TIME * 1000, rate);
106 len_tone  = roar_dtmf_mus2samples(ON_TIME  * 1000, rate);
107 samples = len_break + len_tone;
108
109/*
110 printf("samples=%llu\n", (long long unsigned int)samples);
111 return 0;
112*/
113
114 if ( (buf = roar_mm_malloc((len = 16*samples/8))) == NULL )
115  return 4;
116
117 if ( (vss = roar_vs_new_playback(server, name, rate, 1, ROAR_CODEC_DEFAULT, 16, &err)) == NULL ) {
118  fprintf(stderr, "Error: can not start playback: %s\n", roar_vs_strerr(err));
119  roar_mm_free(buf);
120  return 1;
121 }
122
123 while ( (c = *tones) != 0 ) {
124  tones++;
125
126  if ( mode == MODE_DTMF ) {
127   if ( c == '+' || c == '.' || c == '_' )
128    continue;
129  } else if ( mode == MODE_ROAR ) {
130   c = ROAR_DTMF_CHAR_ROAR(c);
131  }
132
133  if ( mode == MODE_DTMF && c == ' ' ) {
134   roar_dtmf_break(buf, len_tone, rate, ROAR_DTMF_OPTIONS_NONE);
135  } else {
136   if ( roar_dtmf_tone(buf, len_tone, rate, ROAR_DTMF_OPTIONS_NONE, c) == -1 ) {
137    roar_mm_free(buf);
138    return 5;
139   }
140  }
141  roar_dtmf_tone(buf+len_tone*sizeof(int16_t), len_break, rate, ROAR_DTMF_OPTIONS_NONE,
142                 mode == MODE_DTMF ? ROAR_DTMF_CHAR_BREAK : ROAR_DTMF_CHAR_NOOP);
143  if ( roar_vs_write(vss, buf, len, &err) != (ssize_t)len ) {
144   fprintf(stderr, "Error: can not write data: %s\n", roar_vs_strerr(err));
145   roar_mm_free(buf);
146   return 5;
147  }
148 }
149
150 roar_vs_sync(vss, ROAR_VS_WAIT, NULL);
151
152 roar_vs_close(vss, ROAR_VS_FALSE, 0);
153
154 roar_mm_free(buf);
155
156 return 0;
157}
158
159#else
160int main (void) {
161 fprintf(stderr, "Error: No Math library support compiled in.\n");
162 return 1;
163}
164#endif
165
166//ll
Note: See TracBrowser for help on using the repository browser.