[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 | |
---|
| 36 | struct 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 | |
---|
| 64 | void 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 | |
---|
[5249] | 78 | int calc_break (int16_t * samples, size_t len, int rate, char c) { |
---|
| 79 | (void)rate, (void)c; |
---|
| 80 | memset(samples, 0, len); |
---|
| 81 | return 0; |
---|
| 82 | } |
---|
| 83 | |
---|
| 84 | int calc_tone (int16_t * samples, size_t len, int rate, char c) { |
---|
[3343] | 85 | struct tone * ct = NULL; |
---|
| 86 | int i; |
---|
| 87 | float t; |
---|
| 88 | float t_inc = 1./rate; |
---|
| 89 | float t_max = ON_TIME / 1000.; |
---|
| 90 | float fc0, fc1; |
---|
| 91 | |
---|
| 92 | // printf("calc(*): t_inc=%f, t_max=%f\n", t_inc, t_max); |
---|
| 93 | |
---|
| 94 | if ( c >= 'a' ) |
---|
| 95 | c -= 'a' - 'A'; |
---|
| 96 | |
---|
| 97 | for (i = 0; g_tones[i].c != 0; i++) { |
---|
| 98 | if ( g_tones[i].c == c ) { |
---|
| 99 | ct = &(g_tones[i]); |
---|
| 100 | break; |
---|
| 101 | } |
---|
| 102 | } |
---|
| 103 | |
---|
| 104 | // printf("calc(*): ct=%p\n", ct); |
---|
| 105 | |
---|
| 106 | if ( ct == NULL ) |
---|
| 107 | return -1; |
---|
| 108 | |
---|
| 109 | fc0 = 2 * M_PI * ct->f0; |
---|
| 110 | fc1 = 2 * M_PI * ct->f1; |
---|
| 111 | |
---|
| 112 | // printf("fc0=%f, fc1=%f\n", fc0, fc1); |
---|
| 113 | |
---|
| 114 | memset(samples, 0, len); |
---|
| 115 | |
---|
| 116 | for (i = 0, t = 0; t < t_max; t += t_inc, i++) { |
---|
| 117 | // printf("i=%i, t=%f\n", i, t); |
---|
| 118 | samples[i] = (sinf(fc0*t) + sinf(fc1*t))*8192.0; |
---|
| 119 | } |
---|
| 120 | |
---|
| 121 | return 0; |
---|
| 122 | } |
---|
| 123 | |
---|
| 124 | int main (int argc, char * argv[]) { |
---|
| 125 | int rate = ROAR_RATE_DEFAULT; |
---|
| 126 | char * server = NULL; |
---|
| 127 | char * k; |
---|
| 128 | int i; |
---|
| 129 | char * name = "roardtmf"; |
---|
[4929] | 130 | roar_vs_t * vss = NULL; |
---|
[3343] | 131 | char * tones = NULL; |
---|
| 132 | void * buf; |
---|
| 133 | size_t samples; |
---|
| 134 | size_t len; |
---|
[3346] | 135 | char c; |
---|
[5249] | 136 | int err; |
---|
[3343] | 137 | |
---|
| 138 | for (i = 1; i < argc; i++) { |
---|
| 139 | k = argv[i]; |
---|
| 140 | |
---|
| 141 | if ( !strcmp(k, "--server") || !strcmp(k, "-s") ) { |
---|
| 142 | server = argv[++i]; |
---|
| 143 | } else if ( !strcmp(k, "--rate") || !strcmp(k, "-r") ) { |
---|
[4883] | 144 | rate = roar_str2rate(argv[++i]); |
---|
[3343] | 145 | } else if ( !strcmp(k, "--help") ) { |
---|
| 146 | usage(); |
---|
| 147 | return 0; |
---|
| 148 | } else if ( tones == NULL ) { |
---|
| 149 | tones = argv[i++]; |
---|
| 150 | } else { |
---|
| 151 | fprintf(stderr, "Error: unknown argument: %s\n", k); |
---|
| 152 | usage(); |
---|
| 153 | return 1; |
---|
| 154 | } |
---|
| 155 | } |
---|
| 156 | |
---|
| 157 | if ( tones == NULL ) { |
---|
| 158 | return 1; |
---|
| 159 | } |
---|
| 160 | |
---|
| 161 | samples = SUM_TIME * rate / 1000; |
---|
| 162 | |
---|
| 163 | /* |
---|
| 164 | printf("samples=%llu\n", (long long unsigned int)samples); |
---|
| 165 | return 0; |
---|
| 166 | */ |
---|
| 167 | |
---|
[4929] | 168 | if ( (buf = roar_mm_malloc((len = 16*samples/8))) == NULL ) |
---|
[3343] | 169 | return 4; |
---|
| 170 | |
---|
[5249] | 171 | if ( (vss = roar_vs_new_playback(server, name, rate, 1, ROAR_CODEC_DEFAULT, 16, &err)) == NULL ) { |
---|
| 172 | fprintf(stderr, "Error: can not start playback: %s\n", roar_vs_strerr(err)); |
---|
[4014] | 173 | roar_mm_free(buf); |
---|
[3343] | 174 | return 1; |
---|
| 175 | } |
---|
| 176 | |
---|
[3346] | 177 | while ( (c = *tones) != 0 ) { |
---|
| 178 | tones++; |
---|
| 179 | |
---|
[5249] | 180 | if ( c == '+' || c == '.' || c == '_' ) |
---|
[3346] | 181 | continue; |
---|
| 182 | |
---|
[5249] | 183 | if ( c == ' ' ) { |
---|
| 184 | calc_break(buf, len, rate, c); |
---|
| 185 | } else { |
---|
| 186 | if ( calc_tone(buf, len, rate, c) == -1 ) { |
---|
| 187 | roar_mm_free(buf); |
---|
| 188 | return 5; |
---|
| 189 | } |
---|
| 190 | } |
---|
| 191 | if ( roar_vs_write(vss, buf, len, &err) != (ssize_t)len ) { |
---|
| 192 | fprintf(stderr, "Error: can not write data: %s\n", roar_vs_strerr(err)); |
---|
[4014] | 193 | roar_mm_free(buf); |
---|
[3343] | 194 | return 5; |
---|
[4014] | 195 | } |
---|
[3343] | 196 | } |
---|
| 197 | |
---|
[4929] | 198 | roar_vs_sync(vss, ROAR_VS_WAIT, NULL); |
---|
| 199 | |
---|
| 200 | roar_vs_close(vss, ROAR_VS_FALSE, 0); |
---|
[3343] | 201 | |
---|
[4014] | 202 | roar_mm_free(buf); |
---|
| 203 | |
---|
[3343] | 204 | return 0; |
---|
| 205 | } |
---|
| 206 | |
---|
[3344] | 207 | #else |
---|
| 208 | int main (void) { |
---|
| 209 | fprintf(stderr, "Error: No Math library support compiled in.\n"); |
---|
| 210 | return 1; |
---|
| 211 | } |
---|
| 212 | #endif |
---|
| 213 | |
---|
[3343] | 214 | //ll |
---|