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