comparison spandsp-0.0.3/spandsp-0.0.3/tests/line_model_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 * line_model_tests.c - Tests for the telephone line model.
5 *
6 * Written by Steve Underwood <steveu@coppice.org>
7 *
8 * Copyright (C) 2004 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: line_model_tests.c,v 1.15 2006/11/19 14:07:27 steveu Exp $
26 */
27
28 /*! \page line_model_tests_page Telephony line model tests
29 \section line_model_tests_page_sec_1 What does it do?
30 ???.
31
32 \section line_model_tests_page_sec_2 How does it work?
33 ???.
34 */
35
36 #ifdef HAVE_CONFIG_H
37 #include "config.h"
38 #endif
39
40 #include <stdlib.h>
41 #include <unistd.h>
42 #include <inttypes.h>
43 #include <string.h>
44 #include <time.h>
45 #include <stdio.h>
46 #include <fcntl.h>
47 #include <audiofile.h>
48 #include <tiffio.h>
49 #if defined(HAVE_TGMATH_H)
50 #include <tgmath.h>
51 #endif
52 #if defined(HAVE_MATH_H)
53 #define GEN_CONST
54 #include <math.h>
55 #endif
56
57 #include "spandsp.h"
58 #include "test_utils.h"
59 #include "line_model.h"
60
61 #if !defined(NULL)
62 #define NULL (void *) 0
63 #endif
64
65 #define BLOCK_LEN 160
66
67 #define IN_FILE_NAME1 "line_model_test_in1.wav"
68 #define IN_FILE_NAME2 "line_model_test_in2.wav"
69 #define OUT_FILE_NAME1 "line_model_one_way_test_out.wav"
70 #define OUT_FILE_NAME "line_model_test_out.wav"
71
72 static void test_one_way_model(int line_model_no, int speech_test)
73 {
74 one_way_line_model_state_t *model;
75 int16_t input1[BLOCK_LEN];
76 int16_t output1[BLOCK_LEN];
77 int16_t amp[2*BLOCK_LEN];
78 AFfilehandle inhandle1;
79 AFfilehandle outhandle;
80 AFfilesetup filesetup;
81 int outframes;
82 int samples;
83 int i;
84 int j;
85 awgn_state_t noise1;
86
87 if ((model = one_way_line_model_init(line_model_no, -50, MUNGE_CODEC_ALAW)) == NULL)
88 {
89 fprintf(stderr, " Failed to create line model\n");
90 exit(2);
91 }
92
93 awgn_init_dbm0(&noise1, 1234567, -10.0f);
94
95 if ((filesetup = afNewFileSetup()) == AF_NULL_FILESETUP)
96 {
97 fprintf(stderr, " Failed to create file setup\n");
98 exit(2);
99 }
100 afInitSampleFormat(filesetup, AF_DEFAULT_TRACK, AF_SAMPFMT_TWOSCOMP, 16);
101 afInitRate(filesetup, AF_DEFAULT_TRACK, (float) SAMPLE_RATE);
102 afInitFileFormat(filesetup, AF_FILE_WAVE);
103 afInitChannels(filesetup, AF_DEFAULT_TRACK, 1);
104
105 if ((inhandle1 = afOpenFile(IN_FILE_NAME1, "r", NULL)) == AF_NULL_FILEHANDLE)
106 {
107 fprintf(stderr, " Cannot open wave file '%s'\n", IN_FILE_NAME1);
108 exit(2);
109 }
110 if ((outhandle = afOpenFile(OUT_FILE_NAME1, "w", filesetup)) == AF_NULL_FILEHANDLE)
111 {
112 fprintf(stderr, " Cannot create wave file '%s'\n", OUT_FILE_NAME1);
113 exit(2);
114 }
115 for (i = 0; i < 10000; i++)
116 {
117 if (speech_test)
118 {
119 samples = afReadFrames(inhandle1,
120 AF_DEFAULT_TRACK,
121 input1,
122 BLOCK_LEN);
123 if (samples == 0)
124 break;
125 }
126 else
127 {
128 for (j = 0; j < BLOCK_LEN; j++)
129 input1[j] = awgn(&noise1);
130 samples = BLOCK_LEN;
131 }
132 for (j = 0; j < samples; j++)
133 {
134 one_way_line_model(model,
135 &output1[j],
136 &input1[j],
137 1);
138 amp[j] = output1[j];
139 }
140 outframes = afWriteFrames(outhandle,
141 AF_DEFAULT_TRACK,
142 amp,
143 samples);
144 if (outframes != samples)
145 {
146 fprintf(stderr, " Error writing wave file\n");
147 exit(2);
148 }
149 }
150 if (afCloseFile(inhandle1))
151 {
152 fprintf(stderr, " Cannot close wave file '%s'\n", IN_FILE_NAME1);
153 exit(2);
154 }
155 if (afCloseFile(outhandle))
156 {
157 fprintf(stderr, " Cannot close wave file '%s'\n", OUT_FILE_NAME1);
158 exit(2);
159 }
160 afFreeFileSetup(filesetup);
161 one_way_line_model_release(model);
162 }
163
164 static void test_both_ways_model(int line_model_no, int speech_test)
165 {
166 both_ways_line_model_state_t *model;
167 int16_t input1[BLOCK_LEN];
168 int16_t input2[BLOCK_LEN];
169 int16_t output1[BLOCK_LEN];
170 int16_t output2[BLOCK_LEN];
171 int16_t amp[2*BLOCK_LEN];
172 AFfilehandle inhandle1;
173 AFfilehandle inhandle2;
174 AFfilehandle outhandle;
175 AFfilesetup filesetup;
176 int outframes;
177 int samples;
178 int i;
179 int j;
180 awgn_state_t noise1;
181 awgn_state_t noise2;
182
183 if ((model = both_ways_line_model_init(line_model_no, -50, line_model_no + 1, -35, MUNGE_CODEC_ALAW)) == NULL)
184 {
185 fprintf(stderr, " Failed to create line model\n");
186 exit(2);
187 }
188
189 awgn_init_dbm0(&noise1, 1234567, -10.0f);
190 awgn_init_dbm0(&noise2, 1234567, -10.0f);
191
192 filesetup = afNewFileSetup();
193 if (filesetup == AF_NULL_FILESETUP)
194 {
195 fprintf(stderr, " Failed to create file setup\n");
196 exit(2);
197 }
198 afInitSampleFormat(filesetup, AF_DEFAULT_TRACK, AF_SAMPFMT_TWOSCOMP, 16);
199 afInitRate(filesetup, AF_DEFAULT_TRACK, (float) SAMPLE_RATE);
200 afInitFileFormat(filesetup, AF_FILE_WAVE);
201 afInitChannels(filesetup, AF_DEFAULT_TRACK, 2);
202
203 inhandle1 = afOpenFile(IN_FILE_NAME1, "r", NULL);
204 if (inhandle1 == AF_NULL_FILEHANDLE)
205 {
206 fprintf(stderr, " Cannot open wave file '%s'\n", IN_FILE_NAME1);
207 exit(2);
208 }
209 inhandle2 = afOpenFile(IN_FILE_NAME2, "r", NULL);
210 if (inhandle2 == AF_NULL_FILEHANDLE)
211 {
212 fprintf(stderr, " Cannot open wave file '%s'\n", IN_FILE_NAME2);
213 exit(2);
214 }
215 outhandle = afOpenFile(OUT_FILE_NAME, "w", filesetup);
216 if (outhandle == AF_NULL_FILEHANDLE)
217 {
218 fprintf(stderr, " Cannot create wave file '%s'\n", OUT_FILE_NAME);
219 exit(2);
220 }
221 for (i = 0; i < 10000; i++)
222 {
223 if (speech_test)
224 {
225 samples = afReadFrames(inhandle1,
226 AF_DEFAULT_TRACK,
227 input1,
228 BLOCK_LEN);
229 if (samples == 0)
230 break;
231 samples = afReadFrames(inhandle2,
232 AF_DEFAULT_TRACK,
233 input2,
234 samples);
235 if (samples == 0)
236 break;
237 }
238 else
239 {
240 for (j = 0; j < BLOCK_LEN; j++)
241 {
242 input1[j] = awgn(&noise1);
243 input2[j] = awgn(&noise2);
244 }
245 samples = BLOCK_LEN;
246 }
247 for (j = 0; j < samples; j++)
248 {
249 both_ways_line_model(model,
250 &output1[j],
251 &input1[j],
252 &output2[j],
253 &input2[j],
254 1);
255 amp[2*j] = output1[j];
256 amp[2*j + 1] = output2[j];
257 }
258 outframes = afWriteFrames(outhandle,
259 AF_DEFAULT_TRACK,
260 amp,
261 samples);
262 if (outframes != samples)
263 {
264 fprintf(stderr, " Error writing wave file\n");
265 exit(2);
266 }
267 }
268 if (afCloseFile(inhandle1))
269 {
270 fprintf(stderr, " Cannot close wave file '%s'\n", IN_FILE_NAME1);
271 exit(2);
272 }
273 if (afCloseFile(inhandle2))
274 {
275 fprintf(stderr, " Cannot close wave file '%s'\n", IN_FILE_NAME2);
276 exit(2);
277 }
278 if (afCloseFile(outhandle))
279 {
280 fprintf(stderr, " Cannot close wave file '%s'\n", OUT_FILE_NAME);
281 exit(2);
282 }
283 afFreeFileSetup(filesetup);
284 both_ways_line_model_release(model);
285 }
286 /*- End of function --------------------------------------------------------*/
287
288 int main(int argc, char *argv[])
289 {
290 int line_model_no;
291 int speech_test;
292
293 line_model_no = 5;
294 speech_test = TRUE;
295 if (argc > 1)
296 line_model_no = atoi(argv[1]);
297 if (argc > 2)
298 speech_test = FALSE;
299 test_one_way_model(line_model_no, speech_test);
300 test_both_ways_model(line_model_no, speech_test);
301 }
302 /*- End of function --------------------------------------------------------*/
303 /*- End of file ------------------------------------------------------------*/

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