source: roaraudio/roarclients/roardtmf.c @ 5546:faff3e9677c4

Last change on this file since 5546:faff3e9677c4 was 5546:faff3e9677c4, checked in by phi, 12 years ago

Started DTMF library

File size: 3.5 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       );
51
52}
53
54int main (int argc, char * argv[]) {
55 int    rate     = ROAR_RATE_DEFAULT;
56 const char * server   = NULL;
57 const char * k;
58 int    i;
59 const char * name = "roardtmf";
60 roar_vs_t * vss = NULL;
61 const char * tones = NULL;
62 void * buf;
63 size_t len_break;
64 size_t len_tone;
65 size_t samples;
66 size_t len;
67 char   c;
68 int err;
69
70 for (i = 1; i < argc; i++) {
71  k = argv[i];
72
73  if ( !strcmp(k, "--server") || !strcmp(k, "-s") ) {
74   server = argv[++i];
75  } else if ( !strcmp(k, "--rate") || !strcmp(k, "-r") || !strcmp(k, "-R") ) {
76   rate = roar_str2rate(argv[++i]);
77  } else if ( !strcmp(k, "--help") ) {
78   usage();
79   return 0;
80  } else if ( tones == NULL ) {
81   tones = argv[i++];
82  } else {
83   fprintf(stderr, "Error: unknown argument: %s\n", k);
84   usage();
85   return 1;
86  }
87 }
88
89 if ( tones == NULL ) {
90  return 1;
91 }
92
93 len_break = roar_dtmf_mus2samples(OFF_TIME * 1000, rate);
94 len_tone  = roar_dtmf_mus2samples(ON_TIME  * 1000, rate);
95 samples = len_break + len_tone;
96
97 printf("samples=%llu\n", (long long unsigned int)samples);
98/*
99 return 0;
100*/
101
102 if ( (buf = roar_mm_malloc((len = 16*samples/8))) == NULL )
103  return 4;
104
105 if ( (vss = roar_vs_new_playback(server, name, rate, 1, ROAR_CODEC_DEFAULT, 16, &err)) == NULL ) {
106  fprintf(stderr, "Error: can not start playback: %s\n", roar_vs_strerr(err));
107  roar_mm_free(buf);
108  return 1;
109 }
110
111 while ( (c = *tones) != 0 ) {
112  tones++;
113
114  if ( c == '+' || c == '.' || c == '_' )
115   continue;
116
117  if ( c == ' ' ) {
118   roar_dtmf_break(buf, len_tone, rate, ROAR_DTMF_OPTIONS_NONE);
119  } else {
120   if ( roar_dtmf_tone(buf, len_tone, rate, ROAR_DTMF_OPTIONS_NONE, c) == -1 ) {
121    roar_mm_free(buf);
122    return 5;
123   }
124  }
125  roar_dtmf_break(buf+len_tone*sizeof(int16_t), len_break, rate, ROAR_DTMF_OPTIONS_NONE);
126  if ( roar_vs_write(vss, buf, len, &err) != (ssize_t)len ) {
127   fprintf(stderr, "Error: can not write data: %s\n", roar_vs_strerr(err));
128   roar_mm_free(buf);
129   return 5;
130  }
131 }
132
133 roar_vs_sync(vss, ROAR_VS_WAIT, NULL);
134
135 roar_vs_close(vss, ROAR_VS_FALSE, 0);
136
137 roar_mm_free(buf);
138
139 return 0;
140}
141
142#else
143int main (void) {
144 fprintf(stderr, "Error: No Math library support compiled in.\n");
145 return 1;
146}
147#endif
148
149//ll
Note: See TracBrowser for help on using the repository browser.