Mercurial > hg > audiostuff
comparison spandsp-0.0.3/spandsp-0.0.3/tests/g711_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 * g711_tests.c | |
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: g711_tests.c,v 1.4 2006/11/19 14:07:27 steveu Exp $ | |
26 */ | |
27 | |
28 /*! \page g711_tests_page A-law and u-law conversion tests | |
29 \section g711_tests_page_sec_1 What does it do? | |
30 | |
31 \section g711_tests_page_sec_2 How is it used? | |
32 */ | |
33 | |
34 #ifdef HAVE_CONFIG_H | |
35 #include "config.h" | |
36 #endif | |
37 | |
38 #include <inttypes.h> | |
39 #include <stdlib.h> | |
40 #include <stdio.h> | |
41 #include <fcntl.h> | |
42 #include <string.h> | |
43 #if defined(HAVE_TGMATH_H) | |
44 #include <tgmath.h> | |
45 #endif | |
46 #if defined(HAVE_MATH_H) | |
47 #include <math.h> | |
48 #endif | |
49 #include <assert.h> | |
50 #include <audiofile.h> | |
51 #include <tiffio.h> | |
52 | |
53 #include "spandsp.h" | |
54 | |
55 #define OUT_FILE_NAME "g711.wav" | |
56 | |
57 int16_t amp[65536]; | |
58 | |
59 const uint8_t alaw_1khz_sine[] = {0x34, 0x21, 0x21, 0x34, 0xB4, 0xA1, 0xA1, 0xB4}; | |
60 const uint8_t ulaw_1khz_sine[] = {0x1E, 0x0B, 0x0B, 0x1E, 0x9E, 0x8B, 0x8B, 0x9E}; | |
61 | |
62 int main(int argc, char *argv[]) | |
63 { | |
64 AFfilehandle outhandle; | |
65 AFfilesetup filesetup; | |
66 power_meter_t power_meter; | |
67 int outframes; | |
68 int i; | |
69 int block; | |
70 int pre; | |
71 int post; | |
72 int alaw_failures; | |
73 int ulaw_failures; | |
74 float worst_alaw; | |
75 float worst_ulaw; | |
76 float tmp; | |
77 | |
78 if ((filesetup = afNewFileSetup()) == AF_NULL_FILESETUP) | |
79 { | |
80 fprintf(stderr, " Failed to create file setup\n"); | |
81 exit(2); | |
82 } | |
83 afInitSampleFormat(filesetup, AF_DEFAULT_TRACK, AF_SAMPFMT_TWOSCOMP, 16); | |
84 afInitRate(filesetup, AF_DEFAULT_TRACK, (float) SAMPLE_RATE); | |
85 afInitFileFormat(filesetup, AF_FILE_WAVE); | |
86 afInitChannels(filesetup, AF_DEFAULT_TRACK, 1); | |
87 | |
88 if ((outhandle = afOpenFile(OUT_FILE_NAME, "w", filesetup)) == AF_NULL_FILEHANDLE) | |
89 { | |
90 fprintf(stderr, " Cannot create wave file '%s'\n", OUT_FILE_NAME); | |
91 exit(2); | |
92 } | |
93 | |
94 printf("Conversion accuracy tests.\n"); | |
95 alaw_failures = 0; | |
96 ulaw_failures = 0; | |
97 worst_alaw = 0.0; | |
98 worst_ulaw = 0.0; | |
99 for (block = 0; block < 1; block++) | |
100 { | |
101 for (i = 0; i < 65536; i++) | |
102 { | |
103 pre = i - 32768; | |
104 post = alaw_to_linear(linear_to_alaw(pre)); | |
105 if (abs(pre) > 140) | |
106 { | |
107 tmp = (float) abs(post - pre)/(float) abs(pre); | |
108 if (tmp > 0.10) | |
109 { | |
110 printf("A-law: Excessive error at %d (%d)\n", pre, post); | |
111 alaw_failures++; | |
112 } | |
113 if (tmp > worst_alaw) | |
114 worst_alaw = tmp; | |
115 } | |
116 else | |
117 { | |
118 /* Small values need different handling for sensible measurement */ | |
119 if (abs(post - pre) > 15) | |
120 { | |
121 printf("A-law: Excessive error at %d (%d)\n", pre, post); | |
122 alaw_failures++; | |
123 } | |
124 } | |
125 amp[i] = post; | |
126 } | |
127 outframes = afWriteFrames(outhandle, | |
128 AF_DEFAULT_TRACK, | |
129 amp, | |
130 65536); | |
131 if (outframes != 65536) | |
132 { | |
133 fprintf(stderr, " Error writing wave file\n"); | |
134 exit(2); | |
135 } | |
136 for (i = 0; i < 65536; i++) | |
137 { | |
138 pre = i - 32768; | |
139 post = ulaw_to_linear(linear_to_ulaw(pre)); | |
140 if (abs(pre) > 40) | |
141 { | |
142 tmp = (float) abs(post - pre)/(float) abs(pre); | |
143 if (tmp > 0.10) | |
144 { | |
145 printf("u-law: Excessive error at %d (%d)\n", pre, post); | |
146 ulaw_failures++; | |
147 } | |
148 if (tmp > worst_ulaw) | |
149 worst_ulaw = tmp; | |
150 } | |
151 else | |
152 { | |
153 /* Small values need different handling for sensible measurement */ | |
154 if (abs(post - pre) > 4) | |
155 { | |
156 printf("u-law: Excessive error at %d (%d)\n", pre, post); | |
157 ulaw_failures++; | |
158 } | |
159 } | |
160 amp[i] = post; | |
161 } | |
162 outframes = afWriteFrames(outhandle, | |
163 AF_DEFAULT_TRACK, | |
164 amp, | |
165 65536); | |
166 if (outframes != 65536) | |
167 { | |
168 fprintf(stderr, " Error writing wave file\n"); | |
169 exit(2); | |
170 } | |
171 } | |
172 printf("Worst A-law error (ignoring small values) %f%%\n", worst_alaw*100.0); | |
173 printf("Worst u-law error (ignoring small values) %f%%\n", worst_ulaw*100.0); | |
174 if (alaw_failures || ulaw_failures) | |
175 { | |
176 printf("%d A-law values with excessive error\n", alaw_failures); | |
177 printf("%d u-law values with excessive error\n", ulaw_failures); | |
178 printf("Tests failed\n"); | |
179 exit(2); | |
180 } | |
181 | |
182 printf("Reference power level tests.\n"); | |
183 power_meter_init(&power_meter, 7); | |
184 | |
185 for (i = 0; i < 8000; i++) | |
186 { | |
187 amp[i] = ulaw_to_linear(ulaw_1khz_sine[i & 7]); | |
188 power_meter_update(&power_meter, amp[i]); | |
189 } | |
190 printf("Reference u-law 1kHz tone is %fdBm0\n", power_meter_dbm0(&power_meter)); | |
191 outframes = afWriteFrames(outhandle, | |
192 AF_DEFAULT_TRACK, | |
193 amp, | |
194 8000); | |
195 if (outframes != 8000) | |
196 { | |
197 fprintf(stderr, " Error writing wave file\n"); | |
198 exit(2); | |
199 } | |
200 if (0.1f < fabs(power_meter_dbm0(&power_meter))) | |
201 { | |
202 printf("Test failed.\n"); | |
203 exit(2); | |
204 } | |
205 | |
206 for (i = 0; i < 8000; i++) | |
207 { | |
208 amp[i] = alaw_to_linear(alaw_1khz_sine[i & 7]); | |
209 power_meter_update(&power_meter, amp[i]); | |
210 } | |
211 printf("Reference A-law 1kHz tone is %fdBm0\n", power_meter_dbm0(&power_meter)); | |
212 outframes = afWriteFrames(outhandle, | |
213 AF_DEFAULT_TRACK, | |
214 amp, | |
215 8000); | |
216 if (outframes != 8000) | |
217 { | |
218 fprintf(stderr, " Error writing wave file\n"); | |
219 exit(2); | |
220 } | |
221 if (0.1f < fabs(power_meter_dbm0(&power_meter))) | |
222 { | |
223 printf("Test failed.\n"); | |
224 exit(2); | |
225 } | |
226 | |
227 /* Check the transcoding functions. */ | |
228 printf("Testing transcoding A-law -> u-law -> A-law\n"); | |
229 for (i = 0; i < 256; i++) | |
230 { | |
231 if (alaw_to_ulaw(ulaw_to_alaw(i)) != i) | |
232 { | |
233 if (abs(alaw_to_ulaw(ulaw_to_alaw(i)) - i) > 1) | |
234 { | |
235 printf("u-law -> A-law -> u-law gave %d -> %d\n", i, alaw_to_ulaw(ulaw_to_alaw(i))); | |
236 printf("Test failed\n"); | |
237 exit(2); | |
238 } | |
239 } | |
240 } | |
241 | |
242 printf("Testing transcoding u-law -> A-law -> u-law\n"); | |
243 for (i = 0; i < 256; i++) | |
244 { | |
245 if (ulaw_to_alaw(alaw_to_ulaw(i)) != i) | |
246 { | |
247 if (abs(alaw_to_ulaw(ulaw_to_alaw(i)) - i) > 1) | |
248 { | |
249 printf("A-law -> u-law -> A-law gave %d -> %d\n", i, ulaw_to_alaw(alaw_to_ulaw(i))); | |
250 printf("Test failed\n"); | |
251 exit(2); | |
252 } | |
253 } | |
254 } | |
255 | |
256 if (afCloseFile(outhandle)) | |
257 { | |
258 fprintf(stderr, " Cannot close wave file '%s'\n", OUT_FILE_NAME); | |
259 exit(2); | |
260 } | |
261 afFreeFileSetup(filesetup); | |
262 | |
263 printf("Tests passed.\n"); | |
264 return 0; | |
265 } | |
266 /*- End of function --------------------------------------------------------*/ | |
267 /*- End of file ------------------------------------------------------------*/ |