5
|
1 /*
|
|
2 * SpanDSP - a series of DSP components for telephony
|
|
3 *
|
|
4 * lpc10_tests.c - Test the LPC10 low bit rate speech codec.
|
|
5 *
|
|
6 * Written by Steve Underwood <steveu@coppice.org>
|
|
7 *
|
|
8 * Copyright (C) 2006 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: lpc10_tests.c,v 1.10 2006/11/20 13:58:57 steveu Exp $
|
|
26 */
|
|
27
|
|
28 /*! \file */
|
|
29
|
|
30 /*! \page lpc10_tests_page LPC10 codec tests
|
|
31 \section lpc10_tests_page_sec_1 What does it do?
|
|
32
|
|
33 \section lpc10_tests_page_sec_2 How is it used?
|
|
34 To perform a general audio quality test, lpc10 should be run. The file ../localtests/short_nb_voice.wav
|
|
35 will be compressed to LPC10 data, decompressed, and the resulting audio stored in post_lpc10.wav.
|
|
36 */
|
|
37
|
|
38 #ifdef HAVE_CONFIG_H
|
|
39 #include "config.h"
|
|
40 #endif
|
|
41
|
|
42 #include <inttypes.h>
|
|
43 #include <stdlib.h>
|
|
44 #include <unistd.h>
|
|
45 #include <stdio.h>
|
|
46 #include <string.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 #include <assert.h>
|
|
54 #include <fcntl.h>
|
|
55 #include <ctype.h>
|
|
56 #include <stdio.h>
|
|
57 #include <audiofile.h>
|
|
58 #include <tiffio.h>
|
|
59
|
|
60 #include "spandsp.h"
|
|
61
|
|
62 #define BLOCK_LEN 180
|
|
63
|
|
64 #define BLOCKS_PER_READ 1
|
|
65
|
|
66 #define IN_FILE_NAME "../localtests/dam9.wav"
|
|
67 #define REF_FILE_NAME "../localtests/dam9_lpc55.wav"
|
|
68 #define COMPRESS_FILE_NAME "lpc10_out.lpc10"
|
|
69 #define DECOMPRESS_FILE_NAME "lpc10_in.lpc10"
|
|
70 #define OUT_FILE_NAME "post_lpc10.wav"
|
|
71
|
|
72 int main(int argc, char *argv[])
|
|
73 {
|
|
74 AFfilehandle inhandle;
|
|
75 AFfilehandle refhandle;
|
|
76 AFfilehandle outhandle;
|
|
77 AFfilesetup filesetup;
|
|
78 int frames;
|
|
79 int outframes;
|
|
80 float x;
|
|
81 double pre_energy;
|
|
82 double post_energy;
|
|
83 double ref_energy;
|
|
84 double diff_energy;
|
|
85 int16_t pre_amp[BLOCKS_PER_READ*BLOCK_LEN];
|
|
86 int16_t post_amp[BLOCKS_PER_READ*BLOCK_LEN];
|
|
87 int16_t ref_amp[BLOCKS_PER_READ*BLOCK_LEN];
|
|
88 int16_t log_amp[BLOCKS_PER_READ*BLOCK_LEN*3];
|
|
89 uint8_t lpc10_data[BLOCKS_PER_READ*7];
|
|
90 double xx;
|
|
91 lpc10_encode_state_t *lpc10_enc_state;
|
|
92 lpc10_decode_state_t *lpc10_dec_state;
|
|
93 int i;
|
|
94 int block_no;
|
|
95 int log_error;
|
|
96 int compress;
|
|
97 int decompress;
|
|
98 const char *in_file_name;
|
|
99 int compress_file;
|
|
100 int decompress_file;
|
|
101 int len;
|
|
102
|
|
103 compress = FALSE;
|
|
104 decompress = FALSE;
|
|
105 log_error = TRUE;
|
|
106 in_file_name = IN_FILE_NAME;
|
|
107 for (i = 1; i < argc; i++)
|
|
108 {
|
|
109 if (strcmp(argv[i], "-c") == 0)
|
|
110 {
|
|
111 compress = TRUE;
|
|
112 continue;
|
|
113 }
|
|
114 if (strcmp(argv[i], "-d") == 0)
|
|
115 {
|
|
116 decompress = TRUE;
|
|
117 continue;
|
|
118 }
|
|
119 if (strcmp(argv[i], "-i") == 0)
|
|
120 {
|
|
121 in_file_name = argv[++i];
|
|
122 continue;
|
|
123 }
|
|
124 if (strcmp(argv[i], "-l") == 0)
|
|
125 {
|
|
126 log_error = FALSE;
|
|
127 continue;
|
|
128 }
|
|
129 }
|
|
130
|
|
131 compress_file = -1;
|
|
132 decompress_file = -1;
|
|
133 inhandle = AF_NULL_FILEHANDLE;
|
|
134 refhandle = AF_NULL_FILEHANDLE;
|
|
135 outhandle = AF_NULL_FILEHANDLE;
|
|
136 if (!decompress)
|
|
137 {
|
|
138 if ((inhandle = afOpenFile(in_file_name, "r", 0)) == AF_NULL_FILEHANDLE)
|
|
139 {
|
|
140 printf(" Cannot open wave file '%s'\n", in_file_name);
|
|
141 exit(2);
|
|
142 }
|
|
143 if ((x = afGetFrameSize(inhandle, AF_DEFAULT_TRACK, 1)) != 2.0)
|
|
144 {
|
|
145 printf(" Unexpected frame size in wave file '%s'\n", in_file_name);
|
|
146 exit(2);
|
|
147 }
|
|
148 if ((x = afGetRate(inhandle, AF_DEFAULT_TRACK)) != (float) SAMPLE_RATE)
|
|
149 {
|
|
150 printf(" Unexpected sample rate in wave file '%s'\n", in_file_name);
|
|
151 exit(2);
|
|
152 }
|
|
153 if ((x = afGetChannels(inhandle, AF_DEFAULT_TRACK)) != 1.0)
|
|
154 {
|
|
155 printf(" Unexpected number of channels in wave file '%s'\n", in_file_name);
|
|
156 exit(2);
|
|
157 }
|
|
158 if ((filesetup = afNewFileSetup()) == AF_NULL_FILESETUP)
|
|
159 {
|
|
160 fprintf(stderr, " Failed to create file setup\n");
|
|
161 exit(2);
|
|
162 }
|
|
163
|
|
164 if ((refhandle = afOpenFile(REF_FILE_NAME, "r", 0)) == AF_NULL_FILEHANDLE)
|
|
165 {
|
|
166 printf(" Cannot open wave file '%s'\n", REF_FILE_NAME);
|
|
167 exit(2);
|
|
168 }
|
|
169 if ((x = afGetFrameSize(refhandle, AF_DEFAULT_TRACK, 1)) != 2.0)
|
|
170 {
|
|
171 printf(" Unexpected frame size in wave file '%s'\n", REF_FILE_NAME);
|
|
172 exit(2);
|
|
173 }
|
|
174 if ((x = afGetRate(refhandle, AF_DEFAULT_TRACK)) != (float) SAMPLE_RATE)
|
|
175 {
|
|
176 printf(" Unexpected sample rate in wave file '%s'\n", REF_FILE_NAME);
|
|
177 exit(2);
|
|
178 }
|
|
179 if ((x = afGetChannels(refhandle, AF_DEFAULT_TRACK)) != 1.0)
|
|
180 {
|
|
181 printf(" Unexpected number of channels in wave file '%s'\n", REF_FILE_NAME);
|
|
182 exit(2);
|
|
183 }
|
|
184 }
|
|
185 else
|
|
186 {
|
|
187 if ((decompress_file = open(DECOMPRESS_FILE_NAME, O_RDONLY)) < 0)
|
|
188 {
|
|
189 fprintf(stderr, " Cannot open decompressed data file '%s'\n", DECOMPRESS_FILE_NAME);
|
|
190 exit(2);
|
|
191 }
|
|
192 }
|
|
193
|
|
194 if ((filesetup = afNewFileSetup()) == AF_NULL_FILESETUP)
|
|
195 {
|
|
196 fprintf(stderr, " Failed to create file setup\n");
|
|
197 exit(2);
|
|
198 }
|
|
199 afInitSampleFormat(filesetup, AF_DEFAULT_TRACK, AF_SAMPFMT_TWOSCOMP, 16);
|
|
200 afInitRate(filesetup, AF_DEFAULT_TRACK, (float) SAMPLE_RATE);
|
|
201 afInitFileFormat(filesetup, AF_FILE_WAVE);
|
|
202 afInitChannels(filesetup, AF_DEFAULT_TRACK, 1);
|
|
203
|
|
204 if ((outhandle = afOpenFile(OUT_FILE_NAME, "w", filesetup)) == AF_NULL_FILEHANDLE)
|
|
205 {
|
|
206 fprintf(stderr, " Cannot create wave file '%s'\n", OUT_FILE_NAME);
|
|
207 exit(2);
|
|
208 }
|
|
209
|
|
210 if ((lpc10_enc_state = lpc10_encode_init(NULL, TRUE)) == NULL)
|
|
211 {
|
|
212 fprintf(stderr, " Cannot create encoder\n");
|
|
213 exit(2);
|
|
214 }
|
|
215
|
|
216 if ((lpc10_dec_state = lpc10_decode_init(NULL, TRUE)) == NULL)
|
|
217 {
|
|
218 fprintf(stderr, " Cannot create decoder\n");
|
|
219 exit(2);
|
|
220 }
|
|
221
|
|
222 if (compress)
|
|
223 {
|
|
224 if ((compress_file = open(COMPRESS_FILE_NAME, O_WRONLY | O_CREAT | O_TRUNC, 0666)) < 0)
|
|
225 {
|
|
226 fprintf(stderr, " Cannot create compressed data file '%s'\n", COMPRESS_FILE_NAME);
|
|
227 exit(2);
|
|
228 }
|
|
229 }
|
|
230
|
|
231 pre_energy = 0.0;
|
|
232 post_energy = 0.0;
|
|
233 ref_energy = 0.0;
|
|
234 diff_energy = 0.0;
|
|
235
|
|
236 if (decompress)
|
|
237 {
|
|
238 while ((len = read(decompress_file, lpc10_data, BLOCKS_PER_READ*7)) > 0)
|
|
239 {
|
|
240 lpc10_decode(lpc10_dec_state, post_amp, lpc10_data, len/7);
|
|
241 outframes = afWriteFrames(outhandle, AF_DEFAULT_TRACK, post_amp, BLOCK_LEN*len/7);
|
|
242 }
|
|
243 }
|
|
244 else
|
|
245 {
|
|
246 block_no = 0;
|
|
247 while ((frames = afReadFrames(inhandle, AF_DEFAULT_TRACK, pre_amp, BLOCKS_PER_READ*BLOCK_LEN)) == BLOCKS_PER_READ*BLOCK_LEN
|
|
248 &&
|
|
249 (frames = afReadFrames(refhandle, AF_DEFAULT_TRACK, ref_amp, BLOCKS_PER_READ*BLOCK_LEN)) == BLOCKS_PER_READ*BLOCK_LEN)
|
|
250 {
|
|
251 lpc10_encode(lpc10_enc_state, lpc10_data, pre_amp, BLOCKS_PER_READ);
|
|
252 if (compress)
|
|
253 write(compress_file, lpc10_data, BLOCKS_PER_READ*7);
|
|
254 lpc10_decode(lpc10_dec_state, post_amp, lpc10_data, BLOCKS_PER_READ);
|
|
255 for (i = 0; i < BLOCK_LEN; i++)
|
|
256 {
|
|
257 pre_energy += (double) pre_amp[i]*(double) pre_amp[i];
|
|
258 post_energy += (double) post_amp[i]*(double) post_amp[i];
|
|
259 ref_energy += (double) ref_amp[i]*(double) ref_amp[i];
|
|
260 /* The reference file has some odd clipping, so eliminate this from the
|
|
261 energy measurement. */
|
|
262 if (ref_amp[i] == 32767 || ref_amp[i] == -32768)
|
|
263 xx = 0.0;
|
|
264 else
|
|
265 xx = post_amp[i] - ref_amp[i];
|
|
266 diff_energy += (double) xx*(double) xx;
|
|
267 log_amp[i] = xx;
|
|
268 }
|
|
269 block_no++;
|
|
270 if (log_error)
|
|
271 outframes = afWriteFrames(outhandle, AF_DEFAULT_TRACK, log_amp, frames);
|
|
272 else
|
|
273 outframes = afWriteFrames(outhandle, AF_DEFAULT_TRACK, post_amp, frames);
|
|
274 }
|
|
275 if (afCloseFile(inhandle) != 0)
|
|
276 {
|
|
277 printf(" Cannot close wave file '%s'\n", in_file_name);
|
|
278 exit(2);
|
|
279 }
|
|
280 if (afCloseFile(refhandle) != 0)
|
|
281 {
|
|
282 printf(" Cannot close wave file '%s'\n", REF_FILE_NAME);
|
|
283 exit(2);
|
|
284 }
|
|
285 }
|
|
286
|
|
287 if (afCloseFile(outhandle) != 0)
|
|
288 {
|
|
289 printf(" Cannot close wave file '%s'\n", OUT_FILE_NAME);
|
|
290 exit(2);
|
|
291 }
|
|
292 afFreeFileSetup(filesetup);
|
|
293 if (compress)
|
|
294 close(compress_file);
|
|
295 if (decompress)
|
|
296 close(decompress_file);
|
|
297 lpc10_encode_release(lpc10_enc_state);
|
|
298 lpc10_decode_release(lpc10_dec_state);
|
|
299
|
|
300 if (!decompress)
|
|
301 {
|
|
302 printf("Output energy is %f%% of input energy.\n", 100.0*post_energy/pre_energy);
|
|
303 printf("Difference energy is %f%% of the total.\n", 100.0*diff_energy/ref_energy);
|
|
304 if (fabs(1.0 - post_energy/pre_energy) > 0.05
|
|
305 ||
|
|
306 fabs(diff_energy/post_energy) > 0.03)
|
|
307 {
|
|
308 printf("Tests failed.\n");
|
|
309 exit(2);
|
|
310 }
|
|
311 printf("Tests passed.\n");
|
|
312 }
|
|
313 return 0;
|
|
314 }
|
|
315 /*- End of function --------------------------------------------------------*/
|
|
316 /*- End of file ------------------------------------------------------------*/
|