comparison spandsp-0.0.3/spandsp-0.0.3/tests/super_tone_tx_tests.c @ 5:f762bf195c4b

import spandsp-0.0.3
author Peter Meerwald <pmeerw@cosy.sbg.ac.at>
date Fri, 25 Jun 2010 16:00:21 +0200
parents
children
comparison
equal deleted inserted replaced
4:26cd8f1ef0b1 5:f762bf195c4b
1 /*
2 * SpanDSP - a series of DSP components for telephony
3 *
4 * super_tone_generate_tests.c
5 *
6 * Written by Steve Underwood <steveu@coppice.org>
7 *
8 * Copyright (C) 2003 Steve Underwood
9 *
10 * All rights reserved.
11 *
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License version 2, as
14 * published by the Free Software Foundation.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 *
25 * $Id: super_tone_tx_tests.c,v 1.16 2006/12/01 18:00:48 steveu Exp $
26 */
27
28 /*! \file */
29
30 /*! \page super_tone_tx_tests_page Supervisory tone generation tests
31 \section super_tone_tx_tests_page_sec_1 What does it do?
32 */
33
34 #ifdef HAVE_CONFIG_H
35 #include "config.h"
36 #endif
37
38 #include <stdlib.h>
39 #include <string.h>
40 #include <strings.h>
41 #include <stdio.h>
42 #include <fcntl.h>
43 #include <ctype.h>
44 #include <time.h>
45 #include <inttypes.h>
46 #include <sys/socket.h>
47 #if defined(HAVE_TGMATH_H)
48 #include <tgmath.h>
49 #endif
50 #if defined(HAVE_MATH_H)
51 #include <math.h>
52 #endif
53
54 #if defined(HAVE_LIBXML_XMLMEMORY_H)
55 #include <libxml/xmlmemory.h>
56 #endif
57 #if defined(HAVE_LIBXML_PARSER_H)
58 #include <libxml/parser.h>
59 #endif
60 #if defined(HAVE_LIBXML_XINCLUDE_H)
61 #include <libxml/xinclude.h>
62 #endif
63
64 #include <audiofile.h>
65 #include <tiffio.h>
66
67 #include "spandsp.h"
68
69 #define OUT_FILE_NAME "super_tone.wav"
70
71 AFfilehandle outhandle;
72 AFfilesetup filesetup;
73
74 super_tone_tx_step_t *tone_tree = NULL;
75
76 static void play_tones(super_tone_tx_state_t *tone, int max_samples)
77 {
78 int16_t amp[8000];
79 int len;
80 int outframes;
81 int i;
82 int total_length;
83
84 i = 500;
85 total_length = 0;
86 do
87 {
88 len = super_tone_tx(tone, amp, 160);
89 outframes = afWriteFrames(outhandle,
90 AF_DEFAULT_TRACK,
91 amp,
92 len);
93 if (outframes != len)
94 {
95 fprintf(stderr, " Error writing wave file\n");
96 exit(2);
97 }
98 total_length += len;
99 }
100 while (len > 0 && --i > 0);
101 printf("Tone length = %d samples (%dms)\n", total_length, total_length/8);
102 }
103 /*- End of function --------------------------------------------------------*/
104
105 #if defined(HAVE_LIBXML2)
106 static int parse_tone(super_tone_tx_step_t **tree, xmlDocPtr doc, xmlNsPtr ns, xmlNodePtr cur)
107 {
108 xmlChar *x;
109 float f1;
110 float f2;
111 float f_tol;
112 float l1;
113 float l2;
114 float length;
115 float length_tol;
116 int cycles;
117 super_tone_tx_step_t *treep;
118
119 cur = cur->xmlChildrenNode;
120 while (cur)
121 {
122 if (xmlStrcmp(cur->name, (const xmlChar *) "step") == 0)
123 {
124 printf("Step - ");
125 /* Set some defaults */
126 f1 = 0.0;
127 f2 = 0.0;
128 f_tol = 1.0;
129 l1 = -11.0;
130 l2 = -11.0;
131 length = 0.0;
132 length_tol = 10.0;
133 cycles = 1;
134 if ((x = xmlGetProp(cur, (const xmlChar *) "freq")))
135 {
136 sscanf((char *) x, "%f [%f%%]", &f1, &f_tol);
137 sscanf((char *) x, "%f+%f [%f%%]", &f1, &f2, &f_tol);
138 printf("Frequency=%.2f+%.2f [%.2f%%]", f1, f2, f_tol);
139 xmlFree(x);
140 }
141 if ((x = xmlGetProp(cur, (const xmlChar *) "level")))
142 {
143 if (sscanf((char *) x, "%f+%f", &l1, &l2) < 2)
144 l2 = l1;
145 printf("Level=%.2f+%.2f", l1, l2);
146 xmlFree(x);
147 }
148 if ((x = xmlGetProp(cur, (const xmlChar *) "length")))
149 {
150 sscanf((char *) x, "%f [%f%%]", &length, &length_tol);
151 printf("Length=%.2f [%.2f%%]", length, length_tol);
152 xmlFree(x);
153 }
154 if ((x = xmlGetProp(cur, (const xmlChar *) "recognition-length")))
155 {
156 printf("Recognition length='%s'", x);
157 xmlFree(x);
158 }
159 if ((x = xmlGetProp(cur, (const xmlChar *) "cycles")))
160 {
161 if (strcasecmp((char *) x, "endless") == 0)
162 cycles = 0;
163 else
164 cycles = atoi((char *) x);
165 printf("Cycles=%d ", cycles);
166 xmlFree(x);
167 }
168 if ((x = xmlGetProp(cur, (const xmlChar *) "recorded-announcement")))
169 {
170 printf("Recorded announcement='%s'", x);
171 xmlFree(x);
172 }
173 printf("\n");
174 treep = super_tone_tx_make_step(NULL,
175 f1,
176 l1,
177 f2,
178 l2,
179 length*1000.0 + 0.5,
180 cycles);
181 *tree = treep;
182 tree = &(treep->next);
183 parse_tone(&(treep->nest), doc, ns, cur);
184 }
185 /*endif*/
186 cur = cur->next;
187 }
188 /*endwhile*/
189 return 0;
190 }
191 /*- End of function --------------------------------------------------------*/
192
193 static void parse_tone_set(xmlDocPtr doc, xmlNsPtr ns, xmlNodePtr cur)
194 {
195 super_tone_tx_state_t tone;
196
197 printf("Parsing tone set\n");
198 cur = cur->xmlChildrenNode;
199 while (cur)
200 {
201 if (strcmp((char *) cur->name + strlen((char *) cur->name) - 5, "-tone") == 0)
202 {
203 printf("Hit %s\n", cur->name);
204 tone_tree = NULL;
205 parse_tone(&tone_tree, doc, ns, cur);
206 super_tone_tx_init(&tone, tone_tree);
207 printf("Len %p %p %d %d\n", (void *) tone.levels[0], (void *) tone_tree, tone_tree->length, tone_tree->tone);
208 play_tones(&tone, 99999999);
209 super_tone_tx_free(tone_tree);
210 }
211 /*endif*/
212 cur = cur->next;
213 }
214 /*endwhile*/
215 }
216 /*- End of function --------------------------------------------------------*/
217
218 static void get_tone_set(const char *tone_file, const char *set_id)
219 {
220 xmlDocPtr doc;
221 xmlNsPtr ns;
222 xmlNodePtr cur;
223 #if 0
224 xmlValidCtxt valid;
225 #endif
226 xmlChar *x;
227
228 ns = NULL;
229 xmlKeepBlanksDefault(0);
230 xmlCleanupParser();
231 doc = xmlParseFile(tone_file);
232 if (doc == NULL)
233 {
234 fprintf(stderr, "No document\n");
235 exit(2);
236 }
237 /*endif*/
238 xmlXIncludeProcess(doc);
239 #if 0
240 if (!xmlValidateDocument(&valid, doc))
241 {
242 fprintf(stderr, "Invalid document\n");
243 exit(2);
244 }
245 /*endif*/
246 #endif
247 /* Check the document is of the right kind */
248 if ((cur = xmlDocGetRootElement(doc)) == NULL)
249 {
250 fprintf(stderr, "Empty document\n");
251 xmlFreeDoc(doc);
252 exit(2);
253 }
254 /*endif*/
255 if (xmlStrcmp(cur->name, (const xmlChar *) "global-tones"))
256 {
257 fprintf(stderr, "Document of the wrong type, root node != global-tones");
258 xmlFreeDoc(doc);
259 exit(2);
260 }
261 /*endif*/
262 cur = cur->xmlChildrenNode;
263 while (cur && xmlIsBlankNode(cur))
264 cur = cur->next;
265 /*endwhile*/
266 if (cur == NULL)
267 exit(2);
268 /*endif*/
269 while (cur)
270 {
271 if (xmlStrcmp(cur->name, (const xmlChar *) "tone-set") == 0)
272 {
273 if ((x = xmlGetProp(cur, (const xmlChar *) "uncode")))
274 {
275 if (strcmp((char *) x, set_id) == 0)
276 parse_tone_set(doc, ns, cur);
277 /*endif*/
278 xmlFree(x);
279 }
280 /*endif*/
281 }
282 /*endif*/
283 cur = cur->next;
284 }
285 /*endwhile*/
286 xmlFreeDoc(doc);
287 }
288 /*- End of function --------------------------------------------------------*/
289 #endif
290
291 int main(int argc, char *argv[])
292 {
293 if ((filesetup = afNewFileSetup ()) == AF_NULL_FILESETUP)
294 {
295 fprintf(stderr, " Failed to create file setup\n");
296 exit(2);
297 }
298 afInitSampleFormat(filesetup, AF_DEFAULT_TRACK, AF_SAMPFMT_TWOSCOMP, 16);
299 afInitRate(filesetup, AF_DEFAULT_TRACK, 8000.0);
300 afInitFileFormat(filesetup, AF_FILE_WAVE);
301 afInitChannels(filesetup, AF_DEFAULT_TRACK, 1);
302
303 if ((outhandle = afOpenFile(OUT_FILE_NAME, "w", filesetup)) == AF_NULL_FILEHANDLE)
304 {
305 fprintf(stderr, " Cannot open audio file '%s'\n", OUT_FILE_NAME);
306 exit(2);
307 }
308 #if defined(HAVE_LIBXML2)
309 get_tone_set("../spandsp/global-tones.xml", (argc > 1) ? argv[1] : "hk");
310 #endif
311 if (afCloseFile (outhandle) != 0)
312 {
313 fprintf(stderr, " Cannot close audio file '%s'\n", OUT_FILE_NAME);
314 exit(2);
315 }
316 afFreeFileSetup(filesetup);
317 printf("Done\n");
318 return 0;
319 }
320 /*- End of function --------------------------------------------------------*/
321 /*- End of file ------------------------------------------------------------*/

Repositories maintained by Peter Meerwald, pmeerw@pmeerw.net.