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