source: roaraudio/roarclients/roardtmf.c @ 5533:42f48072307c

Last change on this file since 5533:42f48072307c was 5533:42f48072307c, checked in by phi, 12 years ago

Fixed usage of -R/-B/-C/-E as well as --aiprofile in roarclients (Closes: #176)

File size: 4.3 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
28#ifdef ROAR_HAVE_LIBM
29#include <math.h>
30
31// in ms:
32#define ON_TIME  (180)
33#define OFF_TIME ( 80)
34#define SUM_TIME (ON_TIME+OFF_TIME)
35
36struct tone {
37 char c;
38 float f0;
39 float f1;
40} g_tones[] = {
41 {'1', 697, 1209},
42 {'2', 697, 1336},
43 {'3', 697, 1477},
44 {'A', 697, 1633},
45
46 {'4', 770, 1209},
47 {'5', 770, 1336},
48 {'6', 770, 1477},
49 {'B', 770, 1633},
50
51 {'7', 852, 1209},
52 {'8', 852, 1336},
53 {'9', 852, 1477},
54 {'C', 852, 1633},
55
56 {'*', 941, 1209},
57 {'0', 941, 1336},
58 {'#', 941, 1477},
59 {'D', 941, 1633},
60
61 {0, -1, -1}
62};
63
64void usage (void) {
65 printf("roardtmf [OPTIONS]... PHONE NUMBER\n");
66
67 printf("\nOptions:\n\n");
68
69 printf("  --server SERVER    - Set server hostname\n"
70        "  --rate  -R RATE    - Set sample rate\n"
71        "  --help             - Show this help\n"
72       );
73
74}
75
76int calc_break (int16_t * samples, size_t len, int rate, char c) {
77 (void)rate, (void)c;
78 memset(samples, 0, len);
79 return 0;
80}
81
82int calc_tone (int16_t * samples, size_t len, int rate, char c) {
83 struct tone * ct = NULL;
84 int i;
85 float t;
86 float t_inc = 1./rate;
87 float t_max = ON_TIME / 1000.;
88 float fc0, fc1;
89
90// printf("calc(*): t_inc=%f, t_max=%f\n", t_inc, t_max);
91
92 if ( c >= 'a' )
93  c -= 'a' - 'A';
94
95 for (i = 0; g_tones[i].c != 0; i++) {
96  if ( g_tones[i].c == c ) {
97   ct = &(g_tones[i]);
98   break;
99  }
100 }
101
102// printf("calc(*): ct=%p\n", ct);
103
104 if ( ct == NULL )
105  return -1;
106
107 fc0 = 2 * M_PI * ct->f0;
108 fc1 = 2 * M_PI * ct->f1;
109
110// printf("fc0=%f, fc1=%f\n", fc0, fc1);
111
112 memset(samples, 0, len);
113
114 for (i = 0, t = 0; t < t_max; t += t_inc, i++) {
115//  printf("i=%i, t=%f\n", i, t);
116  samples[i] = (sinf(fc0*t) + sinf(fc1*t))*8192.0;
117 }
118
119 return 0;
120}
121
122int main (int argc, char * argv[]) {
123 int    rate     = ROAR_RATE_DEFAULT;
124 char * server   = NULL;
125 char * k;
126 int    i;
127 char * name = "roardtmf";
128 roar_vs_t * vss = NULL;
129 char * tones = NULL;
130 void * buf;
131 size_t samples;
132 size_t len;
133 char   c;
134 int err;
135
136 for (i = 1; i < argc; i++) {
137  k = argv[i];
138
139  if ( !strcmp(k, "--server") || !strcmp(k, "-s") ) {
140   server = argv[++i];
141  } else if ( !strcmp(k, "--rate") || !strcmp(k, "-r") || !strcmp(k, "-R") ) {
142   rate = roar_str2rate(argv[++i]);
143  } else if ( !strcmp(k, "--help") ) {
144   usage();
145   return 0;
146  } else if ( tones == NULL ) {
147   tones = argv[i++];
148  } else {
149   fprintf(stderr, "Error: unknown argument: %s\n", k);
150   usage();
151   return 1;
152  }
153 }
154
155 if ( tones == NULL ) {
156  return 1;
157 }
158
159 samples = SUM_TIME * rate / 1000;
160
161/*
162 printf("samples=%llu\n", (long long unsigned int)samples);
163 return 0;
164*/
165
166 if ( (buf = roar_mm_malloc((len = 16*samples/8))) == NULL )
167  return 4;
168
169 if ( (vss = roar_vs_new_playback(server, name, rate, 1, ROAR_CODEC_DEFAULT, 16, &err)) == NULL ) {
170  fprintf(stderr, "Error: can not start playback: %s\n", roar_vs_strerr(err));
171  roar_mm_free(buf);
172  return 1;
173 }
174
175 while ( (c = *tones) != 0 ) {
176  tones++;
177
178  if ( c == '+' || c == '.' || c == '_' )
179   continue;
180
181  if ( c == ' ' ) {
182   calc_break(buf, len, rate, c);
183  } else {
184   if ( calc_tone(buf, len, rate, c) == -1 ) {
185    roar_mm_free(buf);
186    return 5;
187   }
188  }
189  if ( roar_vs_write(vss, buf, len, &err) != (ssize_t)len ) {
190   fprintf(stderr, "Error: can not write data: %s\n", roar_vs_strerr(err));
191   roar_mm_free(buf);
192   return 5;
193  }
194 }
195
196 roar_vs_sync(vss, ROAR_VS_WAIT, NULL);
197
198 roar_vs_close(vss, ROAR_VS_FALSE, 0);
199
200 roar_mm_free(buf);
201
202 return 0;
203}
204
205#else
206int main (void) {
207 fprintf(stderr, "Error: No Math library support compiled in.\n");
208 return 1;
209}
210#endif
211
212//ll
Note: See TracBrowser for help on using the repository browser.