5
|
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.22 2006/11/19 14:07:27 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 ../localtests/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 #ifdef HAVE_CONFIG_H
|
|
43 #include "config.h"
|
|
44 #endif
|
|
45
|
|
46 #include <stdlib.h>
|
|
47 #include <inttypes.h>
|
|
48 #include <string.h>
|
|
49 #if defined(HAVE_TGMATH_H)
|
|
50 #include <tgmath.h>
|
|
51 #endif
|
|
52 #if defined(HAVE_MATH_H)
|
|
53 #include <math.h>
|
|
54 #endif
|
|
55 #include <stdio.h>
|
|
56 #include <time.h>
|
|
57 #include <fcntl.h>
|
|
58 #include <audiofile.h>
|
|
59 #include <tiffio.h>
|
|
60
|
|
61 #include "spandsp.h"
|
|
62
|
|
63 #define IN_FILE_NAME "../localtests/short_nb_voice.wav"
|
|
64 #define OUT_FILE_NAME "post_ima_adpcm.wav"
|
|
65
|
|
66 #define HIST_LEN 1000
|
|
67
|
|
68 int main(int argc, char *argv[])
|
|
69 {
|
|
70 int i;
|
|
71 AFfilehandle inhandle;
|
|
72 AFfilehandle outhandle;
|
|
73 AFfilesetup filesetup;
|
|
74 int frames;
|
|
75 int dec_frames;
|
|
76 int outframes;
|
|
77 int ima_bytes;
|
|
78 float x;
|
|
79 double pre_energy;
|
|
80 double post_energy;
|
|
81 double diff_energy;
|
|
82 int16_t pre_amp[HIST_LEN];
|
|
83 int16_t post_amp[HIST_LEN];
|
|
84 uint8_t ima_data[HIST_LEN];
|
|
85 int16_t history[HIST_LEN];
|
|
86 int hist_in;
|
|
87 int hist_out;
|
|
88 ima_adpcm_state_t *ima_enc_state;
|
|
89 ima_adpcm_state_t *ima_dec_state;
|
|
90 int xx;
|
|
91 int vbr;
|
|
92 const char *in_file_name;
|
|
93
|
|
94 vbr = FALSE;
|
|
95 in_file_name = IN_FILE_NAME;
|
|
96 for (i = 1; i < argc; i++)
|
|
97 {
|
|
98 if (strcmp(argv[i], "-v") == 0)
|
|
99 {
|
|
100 vbr = TRUE;
|
|
101 continue;
|
|
102 }
|
|
103 if (strcmp(argv[i], "-i") == 0)
|
|
104 {
|
|
105 in_file_name = argv[++i];
|
|
106 continue;
|
|
107 }
|
|
108 fprintf(stderr, "Unknown parameter %s specified.\n", argv[i]);
|
|
109 exit(2);
|
|
110 }
|
|
111
|
|
112 if ((inhandle = afOpenFile(in_file_name, "r", 0)) == AF_NULL_FILEHANDLE)
|
|
113 {
|
|
114 printf(" Cannot open wave file '%s'\n", in_file_name);
|
|
115 exit(2);
|
|
116 }
|
|
117 if ((x = afGetFrameSize(inhandle, AF_DEFAULT_TRACK, 1)) != 2.0)
|
|
118 {
|
|
119 printf(" Unexpected frame size in wave file '%s'\n", in_file_name);
|
|
120 exit(2);
|
|
121 }
|
|
122 if ((x = afGetRate(inhandle, AF_DEFAULT_TRACK)) != (float) SAMPLE_RATE)
|
|
123 {
|
|
124 printf(" Unexpected sample rate in wave file '%s'\n", in_file_name);
|
|
125 exit(2);
|
|
126 }
|
|
127 if ((x = afGetChannels(inhandle, AF_DEFAULT_TRACK)) != 1.0)
|
|
128 {
|
|
129 printf(" Unexpected number of channels in wave file '%s'\n", in_file_name);
|
|
130 exit(2);
|
|
131 }
|
|
132
|
|
133 if ((filesetup = afNewFileSetup()) == AF_NULL_FILESETUP)
|
|
134 {
|
|
135 fprintf(stderr, " Failed to create file setup\n");
|
|
136 exit(2);
|
|
137 }
|
|
138 afInitSampleFormat(filesetup, AF_DEFAULT_TRACK, AF_SAMPFMT_TWOSCOMP, 16);
|
|
139 afInitRate(filesetup, AF_DEFAULT_TRACK, (float) SAMPLE_RATE);
|
|
140 afInitFileFormat(filesetup, AF_FILE_WAVE);
|
|
141 afInitChannels(filesetup, AF_DEFAULT_TRACK, 1);
|
|
142 if ((outhandle = afOpenFile(OUT_FILE_NAME, "w", filesetup)) == AF_NULL_FILEHANDLE)
|
|
143 {
|
|
144 fprintf(stderr, " Cannot create wave file '%s'\n", OUT_FILE_NAME);
|
|
145 exit(2);
|
|
146 }
|
|
147
|
|
148 if ((ima_enc_state = ima_adpcm_init(NULL, (vbr) ? IMA_ADPCM_VDVI : IMA_ADPCM_DVI4)) == NULL)
|
|
149 {
|
|
150 fprintf(stderr, " Cannot create encoder\n");
|
|
151 exit(2);
|
|
152 }
|
|
153
|
|
154 if ((ima_dec_state = ima_adpcm_init(NULL, (vbr) ? IMA_ADPCM_VDVI : IMA_ADPCM_DVI4)) == NULL)
|
|
155 {
|
|
156 fprintf(stderr, " Cannot create decoder\n");
|
|
157 exit(2);
|
|
158 }
|
|
159
|
|
160 hist_in = 0;
|
|
161 hist_out = 0;
|
|
162 pre_energy = 0.0;
|
|
163 post_energy = 0.0;
|
|
164 diff_energy = 0.0;
|
|
165 while ((frames = afReadFrames(inhandle, AF_DEFAULT_TRACK, pre_amp, 159)))
|
|
166 {
|
|
167 ima_bytes = ima_adpcm_encode(ima_enc_state, ima_data, pre_amp, frames);
|
|
168 dec_frames = ima_adpcm_decode(ima_dec_state, post_amp, ima_data, ima_bytes);
|
|
169 for (i = 0; i < frames; i++)
|
|
170 {
|
|
171 history[hist_in++] = pre_amp[i];
|
|
172 if (hist_in >= HIST_LEN)
|
|
173 hist_in = 0;
|
|
174 pre_energy += (double) pre_amp[i] * (double) pre_amp[i];
|
|
175 }
|
|
176 for (i = 0; i < dec_frames; i++)
|
|
177 {
|
|
178 post_energy += (double) post_amp[i] * (double) post_amp[i];
|
|
179 xx = post_amp[i] - history[hist_out++];
|
|
180 if (hist_out >= HIST_LEN)
|
|
181 hist_out = 0;
|
|
182 diff_energy += (double) xx * (double) xx;
|
|
183 }
|
|
184 outframes = afWriteFrames(outhandle, AF_DEFAULT_TRACK, post_amp, dec_frames);
|
|
185 }
|
|
186 if (afCloseFile(inhandle) != 0)
|
|
187 {
|
|
188 printf(" Cannot close wave file '%s'\n", in_file_name);
|
|
189 exit(2);
|
|
190 }
|
|
191 if (afCloseFile(outhandle) != 0)
|
|
192 {
|
|
193 printf(" Cannot close wave file '%s'\n", OUT_FILE_NAME);
|
|
194 exit(2);
|
|
195 }
|
|
196 afFreeFileSetup(filesetup);
|
|
197 ima_adpcm_release(ima_enc_state);
|
|
198 ima_adpcm_release(ima_dec_state);
|
|
199
|
|
200 printf("Output energy is %f%% of input energy.\n", 100.0*post_energy/pre_energy);
|
|
201 printf("Residual energy is %f%% of the total.\n", 100.0*diff_energy/post_energy);
|
|
202 if (fabs(1.0 - post_energy/pre_energy) > 0.05
|
|
203 ||
|
|
204 fabs(diff_energy/post_energy) > 0.03)
|
|
205 {
|
|
206 printf("Tests failed.\n");
|
|
207 exit(2);
|
|
208 }
|
|
209
|
|
210 printf("Tests passed.\n");
|
|
211 return 0;
|
|
212 }
|
|
213 /*- End of function --------------------------------------------------------*/
|
|
214 /*- End of file ------------------------------------------------------------*/
|