source: roaraudio/roarclients/roardtmf.c @ 5248:0133acb5ae31

Last change on this file since 5248:0133acb5ae31 was 4929:061614c260d8, checked in by phi, 13 years ago

use VS API

File size: 4.1 KB
RevLine 
[3343]1//roardtmf.c:
2
3/*
[4708]4 *      Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2008-2011
[3343]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
[3517]21 *  the Free Software Foundation, 51 Franklin Street, Fifth Floor,
22 *  Boston, MA 02110-1301, USA.
[3343]23 *
24 */
25
26#include <roaraudio.h>
[3344]27
28#ifdef ROAR_HAVE_LIBM
[3343]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) {
[3508]65 printf("roardtmf [OPTIONS]... PHONE NUMBER\n");
[3343]66
67 printf("\nOptions:\n\n");
68
69 printf("  --server SERVER    - Set server hostname\n"
70        "  --rate   RATE      - Set sample rate\n"
[3508]71//        "  --bits   BITS      - Set bits per sample\n"
72//        "  --codec  CODEC     - Set the codec\n"
[3343]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 char * server   = NULL;
121 char * k;
122 int    i;
123 char * name = "roardtmf";
[4929]124 roar_vs_t * vss = NULL;
[3343]125 char * tones = NULL;
126 void * buf;
127 size_t samples;
128 size_t len;
[3346]129 char   c;
[3343]130
131 for (i = 1; i < argc; i++) {
132  k = argv[i];
133
134  if ( !strcmp(k, "--server") || !strcmp(k, "-s") ) {
135   server = argv[++i];
136  } else if ( !strcmp(k, "--rate") || !strcmp(k, "-r") ) {
[4883]137   rate = roar_str2rate(argv[++i]);
[3343]138  } else if ( !strcmp(k, "--help") ) {
139   usage();
140   return 0;
141  } else if ( tones == NULL ) {
142   tones = argv[i++];
143  } else {
144   fprintf(stderr, "Error: unknown argument: %s\n", k);
145   usage();
146   return 1;
147  }
148 }
149
150 if ( tones == NULL ) {
151  return 1;
152 }
153
154 samples = SUM_TIME * rate / 1000;
155
156/*
157 printf("samples=%llu\n", (long long unsigned int)samples);
158 return 0;
159*/
160
[4929]161 if ( (buf = roar_mm_malloc((len = 16*samples/8))) == NULL )
[3343]162  return 4;
163
[4929]164 if ( (vss = roar_vs_new_playback(server, name, rate, 1, ROAR_CODEC_DEFAULT, 16, NULL)) == NULL ) {
[3343]165  fprintf(stderr, "Error: can not start playback\n");
[4014]166  roar_mm_free(buf);
[3343]167  return 1;
168 }
169
[3346]170 while ( (c = *tones) != 0 ) {
171  tones++;
172
173  if ( c == ' ' || c == '+' || c == '.' || c == '_' )
174   continue;
175
[4014]176  if ( calc(buf, len, rate, c) == -1 ) {
177   roar_mm_free(buf);
[3343]178   return 5;
[4014]179  }
[4929]180  roar_vs_write(vss, buf, len, NULL);
[3343]181 }
182
[4929]183 roar_vs_sync(vss, ROAR_VS_WAIT, NULL);
184
185 roar_vs_close(vss, ROAR_VS_FALSE, 0);
[3343]186
[4014]187 roar_mm_free(buf);
188
[3343]189 return 0;
190}
191
[3344]192#else
193int main (void) {
194 fprintf(stderr, "Error: No Math library support compiled in.\n");
195 return 1;
196}
197#endif
198
[3343]199//ll
Note: See TracBrowser for help on using the repository browser.