source: roaraudio/roarclients/roardtmf.c @ 3343:b3d4fc313dc6

Last change on this file since 3343:b3d4fc313dc6 was 3343:b3d4fc313dc6, checked in by phi, 14 years ago

added roardtmf

File size: 4.0 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, 675 Mass Ave, Cambridge, MA 02139, USA.
22 *
23 */
24
25#include <roaraudio.h>
26#include <math.h>
27
28// in ms:
29#define ON_TIME  (180)
30#define OFF_TIME ( 80)
31#define SUM_TIME (ON_TIME+OFF_TIME)
32
33struct tone {
34 char c;
35 float f0;
36 float f1;
37} g_tones[] = {
38 {'1', 697, 1209},
39 {'2', 697, 1336},
40 {'3', 697, 1477},
41 {'A', 697, 1633},
42
43 {'4', 770, 1209},
44 {'5', 770, 1336},
45 {'6', 770, 1477},
46 {'B', 770, 1633},
47
48 {'7', 852, 1209},
49 {'8', 852, 1336},
50 {'9', 852, 1477},
51 {'C', 852, 1633},
52
53 {'*', 941, 1209},
54 {'0', 941, 1336},
55 {'#', 941, 1477},
56 {'D', 941, 1633},
57
58 {0, -1, -1}
59};
60
61void usage (void) {
62 printf("roarcatvio [OPTIONS]... [FILE]\n");
63
64 printf("\nOptions:\n\n");
65
66 printf("  --server SERVER    - Set server hostname\n"
67        "  --rate   RATE      - Set sample rate\n"
68        "  --bits   BITS      - Set bits per sample\n"
69        "  --codec  CODEC     - Set the codec\n"
70        "  --help             - Show this help\n"
71       );
72
73}
74
75int calc (int16_t * samples, size_t len, int rate, char c) {
76 struct tone * ct = NULL;
77 int i;
78 float t;
79 float t_inc = 1./rate;
80 float t_max = ON_TIME / 1000.;
81 float fc0, fc1;
82
83// printf("calc(*): t_inc=%f, t_max=%f\n", t_inc, t_max);
84
85 if ( c >= 'a' )
86  c -= 'a' - 'A';
87
88 for (i = 0; g_tones[i].c != 0; i++) {
89  if ( g_tones[i].c == c ) {
90   ct = &(g_tones[i]);
91   break;
92  }
93 }
94
95// printf("calc(*): ct=%p\n", ct);
96
97 if ( ct == NULL )
98  return -1;
99
100 fc0 = 2 * M_PI * ct->f0;
101 fc1 = 2 * M_PI * ct->f1;
102
103// printf("fc0=%f, fc1=%f\n", fc0, fc1);
104
105 memset(samples, 0, len);
106
107 for (i = 0, t = 0; t < t_max; t += t_inc, i++) {
108//  printf("i=%i, t=%f\n", i, t);
109  samples[i] = (sinf(fc0*t) + sinf(fc1*t))*8192.0;
110 }
111
112 return 0;
113}
114
115int main (int argc, char * argv[]) {
116 int    rate     = ROAR_RATE_DEFAULT;
117 int    bits     = ROAR_BITS_DEFAULT;
118 int    codec    = ROAR_CODEC_DEFAULT;
119 char * server   = NULL;
120 char * k;
121 int    i;
122 char * name = "roardtmf";
123 struct roar_vio_calls stream;
124 char * tones = NULL;
125 void * buf;
126 size_t samples;
127 size_t len;
128
129 for (i = 1; i < argc; i++) {
130  k = argv[i];
131
132  if ( !strcmp(k, "--server") || !strcmp(k, "-s") ) {
133   server = argv[++i];
134  } else if ( !strcmp(k, "--rate") || !strcmp(k, "-r") ) {
135   rate = atoi(argv[++i]);
136  } else if ( !strcmp(k, "--bits") ) {
137   bits = atoi(argv[++i]);
138  } else if ( !strcmp(k, "--codec") ) {
139   codec = roar_str2codec(argv[++i]);
140  } else if ( !strcmp(k, "--help") ) {
141   usage();
142   return 0;
143  } else if ( tones == NULL ) {
144   tones = argv[i++];
145  } else {
146   fprintf(stderr, "Error: unknown argument: %s\n", k);
147   usage();
148   return 1;
149  }
150 }
151
152 if ( tones == NULL ) {
153  return 1;
154 }
155
156 if ( codec != ROAR_CODEC_DEFAULT )
157  return 2;
158 if ( bits  != 16 )
159  return 3;
160
161 samples = SUM_TIME * rate / 1000;
162
163/*
164 printf("samples=%llu\n", (long long unsigned int)samples);
165 return 0;
166*/
167
168 if ( (buf = malloc((len = bits*samples/8))) == NULL )
169  return 4;
170
171 if ( roar_vio_simple_stream(&stream, rate, 1, bits, codec, server, ROAR_DIR_PLAY, name) == -1 ) {
172  fprintf(stderr, "Error: can not start playback\n");
173  return 1;
174 }
175
176 while (*tones != 0 ) {
177  if ( calc(buf, len, rate, *tones) == -1 )
178   return 5;
179  roar_vio_write(&stream, buf, len);
180  tones++;
181 }
182
183 roar_vio_close(&stream);
184
185 return 0;
186}
187
188//ll
Note: See TracBrowser for help on using the repository browser.