source: roaraudio/roarclients/roardtmf.c @ 4014:2fee6bf2dc0c

Last change on this file since 4014:2fee6bf2dc0c was 4014:2fee6bf2dc0c, checked in by phi, 14 years ago

fixed some resurce leaks

File size: 4.3 KB
Line 
1//roardtmf.c:
2
3/*
4 *      Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2008-2010
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   RATE      - Set sample rate\n"
71//        "  --bits   BITS      - Set bits per sample\n"
72//        "  --codec  CODEC     - Set the codec\n"
73        "  --help             - Show this help\n"
74       );
75
76}
77
78int calc (int16_t * samples, size_t len, int rate, char c) {
79 struct tone * ct = NULL;
80 int i;
81 float t;
82 float t_inc = 1./rate;
83 float t_max = ON_TIME / 1000.;
84 float fc0, fc1;
85
86// printf("calc(*): t_inc=%f, t_max=%f\n", t_inc, t_max);
87
88 if ( c >= 'a' )
89  c -= 'a' - 'A';
90
91 for (i = 0; g_tones[i].c != 0; i++) {
92  if ( g_tones[i].c == c ) {
93   ct = &(g_tones[i]);
94   break;
95  }
96 }
97
98// printf("calc(*): ct=%p\n", ct);
99
100 if ( ct == NULL )
101  return -1;
102
103 fc0 = 2 * M_PI * ct->f0;
104 fc1 = 2 * M_PI * ct->f1;
105
106// printf("fc0=%f, fc1=%f\n", fc0, fc1);
107
108 memset(samples, 0, len);
109
110 for (i = 0, t = 0; t < t_max; t += t_inc, i++) {
111//  printf("i=%i, t=%f\n", i, t);
112  samples[i] = (sinf(fc0*t) + sinf(fc1*t))*8192.0;
113 }
114
115 return 0;
116}
117
118int main (int argc, char * argv[]) {
119 int    rate     = ROAR_RATE_DEFAULT;
120 int    bits     = ROAR_BITS_DEFAULT;
121 int    codec    = ROAR_CODEC_DEFAULT;
122 char * server   = NULL;
123 char * k;
124 int    i;
125 char * name = "roardtmf";
126 struct roar_vio_calls stream;
127 char * tones = NULL;
128 void * buf;
129 size_t samples;
130 size_t len;
131 char   c;
132
133 for (i = 1; i < argc; i++) {
134  k = argv[i];
135
136  if ( !strcmp(k, "--server") || !strcmp(k, "-s") ) {
137   server = argv[++i];
138  } else if ( !strcmp(k, "--rate") || !strcmp(k, "-r") ) {
139   rate = atoi(argv[++i]);
140  } else if ( !strcmp(k, "--bits") ) {
141   bits = atoi(argv[++i]);
142  } else if ( !strcmp(k, "--codec") ) {
143   codec = roar_str2codec(argv[++i]);
144  } else if ( !strcmp(k, "--help") ) {
145   usage();
146   return 0;
147  } else if ( tones == NULL ) {
148   tones = argv[i++];
149  } else {
150   fprintf(stderr, "Error: unknown argument: %s\n", k);
151   usage();
152   return 1;
153  }
154 }
155
156 if ( tones == NULL ) {
157  return 1;
158 }
159
160 if ( codec != ROAR_CODEC_DEFAULT )
161  return 2;
162 if ( bits  != 16 )
163  return 3;
164
165 samples = SUM_TIME * rate / 1000;
166
167/*
168 printf("samples=%llu\n", (long long unsigned int)samples);
169 return 0;
170*/
171
172 if ( (buf = roar_mm_malloc((len = bits*samples/8))) == NULL )
173  return 4;
174
175 if ( roar_vio_simple_stream(&stream, rate, 1, bits, codec, server, ROAR_DIR_PLAY, name) == -1 ) {
176  fprintf(stderr, "Error: can not start playback\n");
177  roar_mm_free(buf);
178  return 1;
179 }
180
181 while ( (c = *tones) != 0 ) {
182  tones++;
183
184  if ( c == ' ' || c == '+' || c == '.' || c == '_' )
185   continue;
186
187  if ( calc(buf, len, rate, c) == -1 ) {
188   roar_mm_free(buf);
189   return 5;
190  }
191  roar_vio_write(&stream, buf, len);
192 }
193
194 roar_vio_close(&stream);
195
196 roar_mm_free(buf);
197
198 return 0;
199}
200
201#else
202int main (void) {
203 fprintf(stderr, "Error: No Math library support compiled in.\n");
204 return 1;
205}
206#endif
207
208//ll
Note: See TracBrowser for help on using the repository browser.