comparison spandsp-0.0.6pre17/tests/ima_adpcm_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 * ima_adpcm_tests.c - Test the IMA/DVI/Intel ADPCM encode and decode
5 * software.
6 *
7 * Written by Steve Underwood <steveu@coppice.org>
8 *
9 * Copyright (C) 2004 Steve Underwood
10 *
11 * All rights reserved.
12 *
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License version 2, as
15 * published by the Free Software Foundation.
16 *
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25 *
26 * $Id: ima_adpcm_tests.c,v 1.36 2009/05/30 15:23:14 steveu Exp $
27 */
28
29 /*! \file */
30
31 /*! \page ima_adpcm_tests_page IMA ADPCM tests
32 \section ima_adpcm_tests_page_sec_1 What does it do?
33 To perform a general audio quality test, ima_adpcm_tests should be run. The test file
34 ../test-data/local/short_nb_voice.wav will be compressed to the specified bit rate,
35 decompressed, and the resulting audio stored in post_ima_adpcm.wav. A simple SNR test
36 is automatically performed. Listening tests may be used for a more detailed evaluation
37 of the degradation in quality caused by the compression.
38
39 \section ima_adpcm_tests_page_sec_2 How is it used?
40 */
41
42 #if defined(HAVE_CONFIG_H)
43 #include "config.h"
44 #endif
45
46 #include <stdlib.h>
47 #include <stdio.h>
48 #include <fcntl.h>
49 #include <unistd.h>
50 #include <string.h>
51 #include <time.h>
52 #include <sndfile.h>
53
54 //#if defined(WITH_SPANDSP_INTERNALS)
55 #define SPANDSP_EXPOSE_INTERNAL_STRUCTURES
56 //#endif
57
58 #include "spandsp.h"
59 #include "spandsp-sim.h"
60
61 #define IN_FILE_NAME "../test-data/local/short_nb_voice.wav"
62 #define OUT_FILE_NAME "post_ima_adpcm.wav"
63
64 #define HIST_LEN 2000
65
66 int main(int argc, char *argv[])
67 {
68 int i;
69 SNDFILE *inhandle;
70 SNDFILE *outhandle;
71 int frames;
72 int dec_frames;
73 int outframes;
74 int ima_bytes;
75 double pre_energy;
76 double post_energy;
77 double diff_energy;
78 int16_t pre_amp[HIST_LEN];
79 int16_t post_amp[HIST_LEN];
80 uint8_t ima_data[HIST_LEN];
81 int16_t history[HIST_LEN];
82 int hist_in;
83 int hist_out;
84 ima_adpcm_state_t *ima_enc_state;
85 ima_adpcm_state_t *ima_dec_state;
86 int xx;
87 int total_pre_samples;
88 int total_compressed_bytes;
89 int total_post_samples;
90 const char *in_file_name;
91 int variant;
92 int chunk_size;
93 int enc_chunk_size;
94 int log_encoded_data;
95 int opt;
96
97 variant = IMA_ADPCM_DVI4;
98 in_file_name = IN_FILE_NAME;
99 chunk_size = 160;
100 enc_chunk_size = 0;
101 log_encoded_data = FALSE;
102 while ((opt = getopt(argc, argv, "ac:i:lv")) != -1)
103 {
104 switch (opt)
105 {
106 case 'a':
107 variant = IMA_ADPCM_IMA4;
108 chunk_size = 505;
109 break;
110 case 'c':
111 enc_chunk_size = atoi(optarg);
112 break;
113 case 'i':
114 in_file_name = optarg;
115 break;
116 case 'l':
117 log_encoded_data = TRUE;
118 break;
119 case 'v':
120 variant = IMA_ADPCM_VDVI;
121 break;
122 default:
123 //usage();
124 exit(2);
125 break;
126 }
127 }
128
129 if ((inhandle = sf_open_telephony_read(in_file_name, 1)) == NULL)
130 {
131 fprintf(stderr, " Cannot open audio file '%s'\n", in_file_name);
132 exit(2);
133 }
134
135 if ((outhandle = sf_open_telephony_write(OUT_FILE_NAME, 1)) == NULL)
136 {
137 fprintf(stderr, " Cannot create audio file '%s'\n", OUT_FILE_NAME);
138 exit(2);
139 }
140
141 if ((ima_enc_state = ima_adpcm_init(NULL, variant, enc_chunk_size)) == NULL)
142 {
143 fprintf(stderr, " Cannot create encoder\n");
144 exit(2);
145 }
146
147 if ((ima_dec_state = ima_adpcm_init(NULL, variant, enc_chunk_size)) == NULL)
148 {
149 fprintf(stderr, " Cannot create decoder\n");
150 exit(2);
151 }
152
153 hist_in = 0;
154 hist_out = 0;
155 pre_energy = 0.0;
156 post_energy = 0.0;
157 diff_energy = 0.0;
158 total_pre_samples = 0;
159 total_compressed_bytes = 0;
160 total_post_samples = 0;
161 while ((frames = sf_readf_short(inhandle, pre_amp, chunk_size)))
162 {
163 total_pre_samples += frames;
164 ima_bytes = ima_adpcm_encode(ima_enc_state, ima_data, pre_amp, frames);
165 if (log_encoded_data)
166 write(1, ima_data, ima_bytes);
167 total_compressed_bytes += ima_bytes;
168 dec_frames = ima_adpcm_decode(ima_dec_state, post_amp, ima_data, ima_bytes);
169 total_post_samples += dec_frames;
170 for (i = 0; i < frames; i++)
171 {
172 history[hist_in++] = pre_amp[i];
173 if (hist_in >= HIST_LEN)
174 hist_in = 0;
175 pre_energy += (double) pre_amp[i] * (double) pre_amp[i];
176 }
177 for (i = 0; i < dec_frames; i++)
178 {
179 post_energy += (double) post_amp[i] * (double) post_amp[i];
180 xx = post_amp[i] - history[hist_out++];
181 if (hist_out >= HIST_LEN)
182 hist_out = 0;
183 diff_energy += (double) xx * (double) xx;
184 }
185 outframes = sf_writef_short(outhandle, post_amp, dec_frames);
186 }
187 if (sf_close(inhandle) != 0)
188 {
189 fprintf(stderr, " Cannot close audio file '%s'\n", in_file_name);
190 exit(2);
191 }
192 if (sf_close(outhandle) != 0)
193 {
194 fprintf(stderr, " Cannot close audio file '%s'\n", OUT_FILE_NAME);
195 exit(2);
196 }
197 ima_adpcm_release(ima_enc_state);
198 ima_adpcm_release(ima_dec_state);
199
200 printf("Pre samples: %d\n", total_pre_samples);
201 printf("Compressed bytes: %d\n", total_compressed_bytes);
202 printf("Post samples: %d\n", total_post_samples);
203
204 printf("Output energy is %f%% of input energy.\n", 100.0*post_energy/pre_energy);
205 printf("Residual energy is %f%% of the total.\n", 100.0*diff_energy/post_energy);
206 if (fabs(1.0 - post_energy/pre_energy) > 0.05
207 ||
208 fabs(diff_energy/post_energy) > 0.03)
209 {
210 printf("Tests failed.\n");
211 exit(2);
212 }
213
214 printf("Tests passed.\n");
215 return 0;
216 }
217 /*- End of function --------------------------------------------------------*/
218 /*- End of file ------------------------------------------------------------*/

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