comparison spandsp-0.0.6pre17/tests/super_tone_tx_tests.c @ 4:26cd8f1ef0b1

import spandsp-0.0.6pre17
author Peter Meerwald <pmeerw@cosy.sbg.ac.at>
date Fri, 25 Jun 2010 15:50:58 +0200
parents
children
comparison
equal deleted inserted replaced
3:c6c5a16ce2f2 4:26cd8f1ef0b1
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.26 2009/05/30 15:23:14 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 #if defined(HAVE_CONFIG_H)
35 #include "config.h"
36 #endif
37
38 #include <stdlib.h>
39 #include <stdio.h>
40 #include <fcntl.h>
41 #include <string.h>
42 #include <strings.h>
43 #include <ctype.h>
44 #include <time.h>
45 #include <inttypes.h>
46 #include <sys/socket.h>
47 #include <sndfile.h>
48
49 #if defined(HAVE_LIBXML_XMLMEMORY_H)
50 #include <libxml/xmlmemory.h>
51 #endif
52 #if defined(HAVE_LIBXML_PARSER_H)
53 #include <libxml/parser.h>
54 #endif
55 #if defined(HAVE_LIBXML_XINCLUDE_H)
56 #include <libxml/xinclude.h>
57 #endif
58
59 //#if defined(WITH_SPANDSP_INTERNALS)
60 #define SPANDSP_EXPOSE_INTERNAL_STRUCTURES
61 //#endif
62
63 #include "spandsp.h"
64 #include "spandsp-sim.h"
65
66 #define OUT_FILE_NAME "super_tone.wav"
67
68 SNDFILE *outhandle;
69
70 super_tone_tx_step_t *tone_tree = NULL;
71
72 static void play_tones(super_tone_tx_state_t *tone, int max_samples)
73 {
74 int16_t amp[8000];
75 int len;
76 int outframes;
77 int i;
78 int total_length;
79
80 i = 500;
81 total_length = 0;
82 do
83 {
84 len = super_tone_tx(tone, amp, 160);
85 outframes = sf_writef_short(outhandle, amp, len);
86 if (outframes != len)
87 {
88 fprintf(stderr, " Error writing audio file\n");
89 exit(2);
90 }
91 total_length += len;
92 }
93 while (len > 0 && --i > 0);
94 printf("Tone length = %d samples (%dms)\n", total_length, total_length/8);
95 }
96 /*- End of function --------------------------------------------------------*/
97
98 #if defined(HAVE_LIBXML2)
99 static int parse_tone(super_tone_tx_step_t **tree, xmlDocPtr doc, xmlNsPtr ns, xmlNodePtr cur)
100 {
101 xmlChar *x;
102 float f1;
103 float f2;
104 float f_tol;
105 float l1;
106 float l2;
107 float length;
108 float length_tol;
109 int cycles;
110 super_tone_tx_step_t *treep;
111
112 cur = cur->xmlChildrenNode;
113 while (cur)
114 {
115 if (xmlStrcmp(cur->name, (const xmlChar *) "step") == 0)
116 {
117 printf("Step - ");
118 /* Set some defaults */
119 f1 = 0.0;
120 f2 = 0.0;
121 f_tol = 1.0;
122 l1 = -11.0;
123 l2 = -11.0;
124 length = 0.0;
125 length_tol = 10.0;
126 cycles = 1;
127 if ((x = xmlGetProp(cur, (const xmlChar *) "freq")))
128 {
129 sscanf((char *) x, "%f [%f%%]", &f1, &f_tol);
130 sscanf((char *) x, "%f+%f [%f%%]", &f1, &f2, &f_tol);
131 printf("Frequency=%.2f+%.2f [%.2f%%]", f1, f2, f_tol);
132 xmlFree(x);
133 }
134 if ((x = xmlGetProp(cur, (const xmlChar *) "level")))
135 {
136 if (sscanf((char *) x, "%f+%f", &l1, &l2) < 2)
137 l2 = l1;
138 printf("Level=%.2f+%.2f", l1, l2);
139 xmlFree(x);
140 }
141 if ((x = xmlGetProp(cur, (const xmlChar *) "length")))
142 {
143 sscanf((char *) x, "%f [%f%%]", &length, &length_tol);
144 printf("Length=%.2f [%.2f%%]", length, length_tol);
145 xmlFree(x);
146 }
147 if ((x = xmlGetProp(cur, (const xmlChar *) "recognition-length")))
148 {
149 printf("Recognition length='%s'", x);
150 xmlFree(x);
151 }
152 if ((x = xmlGetProp(cur, (const xmlChar *) "cycles")))
153 {
154 if (strcasecmp((char *) x, "endless") == 0)
155 cycles = 0;
156 else
157 cycles = atoi((char *) x);
158 printf("Cycles=%d ", cycles);
159 xmlFree(x);
160 }
161 if ((x = xmlGetProp(cur, (const xmlChar *) "recorded-announcement")))
162 {
163 printf("Recorded announcement='%s'", x);
164 xmlFree(x);
165 }
166 printf("\n");
167 treep = super_tone_tx_make_step(NULL,
168 f1,
169 l1,
170 f2,
171 l2,
172 length*1000.0 + 0.5,
173 cycles);
174 *tree = treep;
175 tree = &(treep->next);
176 parse_tone(&(treep->nest), doc, ns, cur);
177 }
178 /*endif*/
179 cur = cur->next;
180 }
181 /*endwhile*/
182 return 0;
183 }
184 /*- End of function --------------------------------------------------------*/
185
186 static void parse_tone_set(xmlDocPtr doc, xmlNsPtr ns, xmlNodePtr cur)
187 {
188 super_tone_tx_state_t tone;
189
190 printf("Parsing tone set\n");
191 cur = cur->xmlChildrenNode;
192 while (cur)
193 {
194 if (strcmp((char *) cur->name + strlen((char *) cur->name) - 5, "-tone") == 0)
195 {
196 printf("Hit %s\n", cur->name);
197 tone_tree = NULL;
198 parse_tone(&tone_tree, doc, ns, cur);
199 super_tone_tx_init(&tone, tone_tree);
200 //printf("Len %p %p %d %d\n", (void *) tone.levels[0], (void *) tone_tree, tone_tree->length, tone_tree->tone);
201 play_tones(&tone, 99999999);
202 super_tone_tx_free_tone(tone_tree);
203 }
204 /*endif*/
205 cur = cur->next;
206 }
207 /*endwhile*/
208 }
209 /*- End of function --------------------------------------------------------*/
210
211 static void get_tone_set(const char *tone_file, const char *set_id)
212 {
213 xmlDocPtr doc;
214 xmlNsPtr ns;
215 xmlNodePtr cur;
216 #if 0
217 xmlValidCtxt valid;
218 #endif
219 xmlChar *x;
220
221 ns = NULL;
222 xmlKeepBlanksDefault(0);
223 xmlCleanupParser();
224 doc = xmlParseFile(tone_file);
225 if (doc == NULL)
226 {
227 fprintf(stderr, "No document\n");
228 exit(2);
229 }
230 /*endif*/
231 xmlXIncludeProcess(doc);
232 #if 0
233 if (!xmlValidateDocument(&valid, doc))
234 {
235 fprintf(stderr, "Invalid document\n");
236 exit(2);
237 }
238 /*endif*/
239 #endif
240 /* Check the document is of the right kind */
241 if ((cur = xmlDocGetRootElement(doc)) == NULL)
242 {
243 fprintf(stderr, "Empty document\n");
244 xmlFreeDoc(doc);
245 exit(2);
246 }
247 /*endif*/
248 if (xmlStrcmp(cur->name, (const xmlChar *) "global-tones"))
249 {
250 fprintf(stderr, "Document of the wrong type, root node != global-tones");
251 xmlFreeDoc(doc);
252 exit(2);
253 }
254 /*endif*/
255 cur = cur->xmlChildrenNode;
256 while (cur && xmlIsBlankNode(cur))
257 cur = cur->next;
258 /*endwhile*/
259 if (cur == NULL)
260 exit(2);
261 /*endif*/
262 while (cur)
263 {
264 if (xmlStrcmp(cur->name, (const xmlChar *) "tone-set") == 0)
265 {
266 if ((x = xmlGetProp(cur, (const xmlChar *) "uncode")))
267 {
268 if (strcmp((char *) x, set_id) == 0)
269 parse_tone_set(doc, ns, cur);
270 /*endif*/
271 xmlFree(x);
272 }
273 /*endif*/
274 }
275 /*endif*/
276 cur = cur->next;
277 }
278 /*endwhile*/
279 xmlFreeDoc(doc);
280 }
281 /*- End of function --------------------------------------------------------*/
282 #endif
283
284 int main(int argc, char *argv[])
285 {
286 if ((outhandle = sf_open_telephony_write(OUT_FILE_NAME, 1)) == NULL)
287 {
288 fprintf(stderr, " Cannot open audio file '%s'\n", OUT_FILE_NAME);
289 exit(2);
290 }
291 #if defined(HAVE_LIBXML2)
292 get_tone_set("../spandsp/global-tones.xml", (argc > 1) ? argv[1] : "hk");
293 #endif
294 if (sf_close (outhandle) != 0)
295 {
296 fprintf(stderr, " Cannot close audio file '%s'\n", OUT_FILE_NAME);
297 exit(2);
298 }
299 printf("Done\n");
300 return 0;
301 }
302 /*- End of function --------------------------------------------------------*/
303 /*- End of file ------------------------------------------------------------*/

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