comparison spandsp-0.0.6pre17/tests/lpc10_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 * 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.24 2009/05/30 15:23:14 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 ../test-data/local/short_nb_voice.wav
35 will be compressed to LPC10 data, decompressed, and the resulting audio stored in post_lpc10.wav.
36 */
37
38 #if defined(HAVE_CONFIG_H)
39 #include "config.h"
40 #endif
41
42 #include <stdlib.h>
43 #include <stdio.h>
44 #include <fcntl.h>
45 #include <unistd.h>
46 #include <string.h>
47 #include <assert.h>
48 #include <ctype.h>
49 #include <sndfile.h>
50
51 //#if defined(WITH_SPANDSP_INTERNALS)
52 #define SPANDSP_EXPOSE_INTERNAL_STRUCTURES
53 //#endif
54
55 #include "spandsp.h"
56 #include "spandsp-sim.h"
57
58 #define BLOCK_LEN 180
59
60 #define BLOCKS_PER_READ 5
61
62 #define IN_FILE_NAME "../test-data/local/dam9.wav"
63 #define REF_FILE_NAME "../test-data/local/dam9_lpc55.wav"
64 #define COMPRESS_FILE_NAME "lpc10_out.lpc10"
65 #define DECOMPRESS_FILE_NAME "lpc10_in.lpc10"
66 #define OUT_FILE_NAME "post_lpc10.wav"
67
68 int main(int argc, char *argv[])
69 {
70 SNDFILE *inhandle;
71 SNDFILE *refhandle;
72 SNDFILE *outhandle;
73 int frames;
74 int outframes;
75 double pre_energy;
76 double post_energy;
77 double ref_energy;
78 double diff_energy;
79 int16_t pre_amp[BLOCKS_PER_READ*BLOCK_LEN];
80 int16_t post_amp[BLOCKS_PER_READ*BLOCK_LEN];
81 int16_t ref_amp[BLOCKS_PER_READ*BLOCK_LEN];
82 int16_t log_amp[BLOCKS_PER_READ*BLOCK_LEN*3];
83 uint8_t lpc10_data[BLOCKS_PER_READ*7];
84 double xx;
85 lpc10_encode_state_t *lpc10_enc_state;
86 lpc10_decode_state_t *lpc10_dec_state;
87 int i;
88 int block_no;
89 int log_error;
90 int compress;
91 int decompress;
92 const char *in_file_name;
93 int compress_file;
94 int decompress_file;
95 int len;
96 int opt;
97 int enc_len;
98 int dec_len;
99
100 compress = FALSE;
101 decompress = FALSE;
102 log_error = TRUE;
103 in_file_name = IN_FILE_NAME;
104 while ((opt = getopt(argc, argv, "cdi:l")) != -1)
105 {
106 switch (opt)
107 {
108 case 'c':
109 compress = TRUE;
110 break;
111 case 'd':
112 decompress = TRUE;
113 break;
114 case 'i':
115 in_file_name = optarg;
116 break;
117 case 'l':
118 log_error = FALSE;
119 break;
120 default:
121 //usage();
122 exit(2);
123 }
124 }
125
126 compress_file = -1;
127 decompress_file = -1;
128 inhandle = NULL;
129 refhandle = NULL;
130 outhandle = NULL;
131 if (!decompress)
132 {
133 if ((inhandle = sf_open_telephony_read(in_file_name, 1)) == NULL)
134 {
135 fprintf(stderr, " Cannot open audio file '%s'\n", in_file_name);
136 exit(2);
137 }
138
139 if ((refhandle = sf_open_telephony_read(REF_FILE_NAME, 1)) == NULL)
140 {
141 fprintf(stderr, " Cannot open audio file '%s'\n", REF_FILE_NAME);
142 exit(2);
143 }
144 }
145 else
146 {
147 if ((decompress_file = open(DECOMPRESS_FILE_NAME, O_RDONLY)) < 0)
148 {
149 fprintf(stderr, " Cannot open decompressed data file '%s'\n", DECOMPRESS_FILE_NAME);
150 exit(2);
151 }
152 }
153
154 if ((outhandle = sf_open_telephony_write(OUT_FILE_NAME, 1)) == NULL)
155 {
156 fprintf(stderr, " Cannot create audio file '%s'\n", OUT_FILE_NAME);
157 exit(2);
158 }
159
160 if ((lpc10_enc_state = lpc10_encode_init(NULL, TRUE)) == NULL)
161 {
162 fprintf(stderr, " Cannot create encoder\n");
163 exit(2);
164 }
165
166 if ((lpc10_dec_state = lpc10_decode_init(NULL, TRUE)) == NULL)
167 {
168 fprintf(stderr, " Cannot create decoder\n");
169 exit(2);
170 }
171
172 if (compress)
173 {
174 if ((compress_file = open(COMPRESS_FILE_NAME, O_WRONLY | O_CREAT | O_TRUNC, 0666)) < 0)
175 {
176 fprintf(stderr, " Cannot create compressed data file '%s'\n", COMPRESS_FILE_NAME);
177 exit(2);
178 }
179 }
180
181 pre_energy = 0.0;
182 post_energy = 0.0;
183 ref_energy = 0.0;
184 diff_energy = 0.0;
185
186 if (decompress)
187 {
188 while ((len = read(decompress_file, lpc10_data, BLOCKS_PER_READ*7)) > 0)
189 {
190 lpc10_decode(lpc10_dec_state, post_amp, lpc10_data, len/7);
191 outframes = sf_writef_short(outhandle, post_amp, BLOCK_LEN*len/7);
192 }
193 }
194 else
195 {
196 block_no = 0;
197 while ((frames = sf_readf_short(inhandle, pre_amp, BLOCKS_PER_READ*BLOCK_LEN)) == BLOCKS_PER_READ*BLOCK_LEN
198 &&
199 (frames = sf_readf_short(refhandle, ref_amp, BLOCKS_PER_READ*BLOCK_LEN)) == BLOCKS_PER_READ*BLOCK_LEN)
200 {
201 enc_len = lpc10_encode(lpc10_enc_state, lpc10_data, pre_amp, BLOCKS_PER_READ*BLOCK_LEN);
202 if (compress)
203 write(compress_file, lpc10_data, enc_len);
204 dec_len = lpc10_decode(lpc10_dec_state, post_amp, lpc10_data, enc_len);
205 for (i = 0; i < dec_len; i++)
206 {
207 pre_energy += (double) pre_amp[i]*(double) pre_amp[i];
208 post_energy += (double) post_amp[i]*(double) post_amp[i];
209 ref_energy += (double) ref_amp[i]*(double) ref_amp[i];
210 /* The reference file has some odd clipping, so eliminate this from the
211 energy measurement. */
212 if (ref_amp[i] == 32767 || ref_amp[i] == -32768)
213 xx = 0.0;
214 else
215 xx = post_amp[i] - ref_amp[i];
216 diff_energy += (double) xx*(double) xx;
217 log_amp[i] = xx;
218 }
219 block_no++;
220 if (log_error)
221 outframes = sf_writef_short(outhandle, log_amp, dec_len);
222 else
223 outframes = sf_writef_short(outhandle, post_amp, dec_len);
224 }
225 if (sf_close(inhandle) != 0)
226 {
227 fprintf(stderr, " Cannot close audio file '%s'\n", in_file_name);
228 exit(2);
229 }
230 if (sf_close(refhandle) != 0)
231 {
232 fprintf(stderr, " Cannot close audio file '%s'\n", REF_FILE_NAME);
233 exit(2);
234 }
235 }
236
237 if (sf_close(outhandle) != 0)
238 {
239 fprintf(stderr, " Cannot close audio file '%s'\n", OUT_FILE_NAME);
240 exit(2);
241 }
242 if (compress)
243 close(compress_file);
244 if (decompress)
245 close(decompress_file);
246 lpc10_encode_release(lpc10_enc_state);
247 lpc10_decode_release(lpc10_dec_state);
248
249 if (!decompress)
250 {
251 printf("Output energy is %f%% of input energy.\n", 100.0*post_energy/pre_energy);
252 printf("Difference energy is %f%% of the total.\n", 100.0*diff_energy/ref_energy);
253 if (fabs(1.0 - post_energy/pre_energy) > 0.05
254 ||
255 fabs(diff_energy/post_energy) > 0.03)
256 {
257 printf("Tests failed.\n");
258 exit(2);
259 }
260 printf("Tests passed.\n");
261 }
262 return 0;
263 }
264 /*- End of function --------------------------------------------------------*/
265 /*- End of file ------------------------------------------------------------*/

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