Mercurial > hg > audiostuff
comparison spandsp-0.0.3/spandsp-0.0.3/tests/make_g168_css.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 * makecss.c - Create the composite source signal (CSS) for G.168 testing. | |
5 * | |
6 * Written by Steve Underwood <steveu@coppice.org> | |
7 * | |
8 * Copyright (C) 2003 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: make_g168_css.c,v 1.9 2006/11/19 14:07:27 steveu Exp $ | |
26 */ | |
27 | |
28 /*! \page makecss_page CSS construction for G.168 testing | |
29 \section makecss_page_sec_1 What does it do? | |
30 ???. | |
31 | |
32 \section makecss_page_sec_2 How does it work? | |
33 ???. | |
34 */ | |
35 | |
36 #ifdef HAVE_CONFIG_H | |
37 #include "config.h" | |
38 #endif | |
39 | |
40 #include <stdlib.h> | |
41 #include <unistd.h> | |
42 #include <inttypes.h> | |
43 #include <string.h> | |
44 #include <time.h> | |
45 #include <stdio.h> | |
46 #include <fcntl.h> | |
47 #include <audiofile.h> | |
48 #include <tiffio.h> | |
49 #if defined(HAVE_FFTW3_H) | |
50 #include <fftw3.h> | |
51 #else | |
52 #include <fftw.h> | |
53 #endif | |
54 #if defined(HAVE_TGMATH_H) | |
55 #include <tgmath.h> | |
56 #endif | |
57 #if defined(HAVE_MATH_H) | |
58 #define GEN_CONST | |
59 #include <math.h> | |
60 #endif | |
61 | |
62 #include "spandsp.h" | |
63 #include "spandsp/g168models.h" | |
64 | |
65 #if !defined(NULL) | |
66 #define NULL (void *) 0 | |
67 #endif | |
68 | |
69 #define FAST_SAMPLE_RATE 44100.0 | |
70 | |
71 static double scaling(double f, double start, double end, double start_gain, double end_gain) | |
72 { | |
73 double scale; | |
74 | |
75 scale = start_gain + (f - start)*(end_gain - start_gain)/(end - start); | |
76 return scale; | |
77 } | |
78 | |
79 int main(int argc, char *argv[]) | |
80 { | |
81 #if defined(HAVE_FFTW3_H) | |
82 double in[8192][2]; | |
83 double out[8192][2]; | |
84 #else | |
85 fftw_complex in[8192]; | |
86 fftw_complex out[8192]; | |
87 #endif | |
88 fftw_plan p; | |
89 int16_t voiced_sound[8192]; | |
90 int16_t noise_sound[8192]; | |
91 int16_t silence_sound[8192]; | |
92 int i; | |
93 int j; | |
94 int outframes; | |
95 int voiced_length; | |
96 float f; | |
97 double peak; | |
98 double ms; | |
99 double scale; | |
100 AFfilehandle filehandle; | |
101 AFfilesetup filesetup; | |
102 awgn_state_t noise_source; | |
103 | |
104 filesetup = afNewFileSetup(); | |
105 if (filesetup == AF_NULL_FILESETUP) | |
106 { | |
107 fprintf(stderr, " Failed to create file setup\n"); | |
108 exit(2); | |
109 } | |
110 afInitSampleFormat(filesetup, AF_DEFAULT_TRACK, AF_SAMPFMT_TWOSCOMP, 16); | |
111 afInitRate(filesetup, AF_DEFAULT_TRACK, FAST_SAMPLE_RATE); | |
112 afInitFileFormat(filesetup, AF_FILE_WAVE); | |
113 afInitChannels(filesetup, AF_DEFAULT_TRACK, 1); | |
114 filehandle = afOpenFile("sound_c1.wav", "w", filesetup); | |
115 if (filehandle == AF_NULL_FILEHANDLE) | |
116 { | |
117 fprintf(stderr, " Failed to open result file\n"); | |
118 exit(2); | |
119 } | |
120 | |
121 for (i = 0; i < (int)(sizeof(css_c1)/sizeof(css_c1[0])); i++) | |
122 voiced_sound[i] = css_c1[i]; | |
123 voiced_length = i; | |
124 | |
125 ms = 0; | |
126 peak = 0; | |
127 for (i = 0; i < voiced_length; i++) | |
128 { | |
129 if (abs(voiced_sound[i]) > peak) | |
130 peak = abs(voiced_sound[i]); | |
131 ms += voiced_sound[i]*voiced_sound[i]; | |
132 } | |
133 ms = 20.0*log10(sqrt(ms/voiced_length)/32767.0) + DBM0_MAX_POWER; | |
134 printf("Voiced level = %.2fdB\n", ms); | |
135 | |
136 #if defined(HAVE_FFTW3_H) | |
137 p = fftw_plan_dft_1d(8192, in, out, FFTW_BACKWARD, FFTW_ESTIMATE); | |
138 #else | |
139 p = fftw_create_plan(8192, FFTW_BACKWARD, FFTW_ESTIMATE); | |
140 #endif | |
141 for (i = 0; i < 8192; i++) | |
142 { | |
143 #if defined(HAVE_FFTW3_H) | |
144 in[i][0] = 0.0; | |
145 in[i][1] = 0.0; | |
146 #else | |
147 in[i].re = 0.0; | |
148 in[i].im = 0.0; | |
149 #endif | |
150 } | |
151 for (i = 1; i <= 3715; i++) | |
152 { | |
153 f = FAST_SAMPLE_RATE*i/8192.0; | |
154 | |
155 #if 1 | |
156 if (f < 50.0) | |
157 scale = -60.0; | |
158 else if (f < 100.0) | |
159 scale = scaling(f, 50.0, 100.0, -25.8, -12.8); | |
160 else if (f < 200.0) | |
161 scale = scaling(f, 100.0, 200.0, -12.8, 17.4); | |
162 else if (f < 215.0) | |
163 scale = scaling(f, 200.0, 215.0, 17.4, 17.8); | |
164 else if (f < 500.0) | |
165 scale = scaling(f, 215.0, 500.0, 17.8, 12.2); | |
166 else if (f < 1000.0) | |
167 scale = scaling(f, 500.0, 1000.0, 12.2, 7.2); | |
168 else if (f < 2850.0) | |
169 scale = scaling(f, 1000.0, 2850.0, 7.2, 0.0); | |
170 else if (f < 3600.0) | |
171 scale = scaling(f, 2850.0, 3600.0, 0.0, -2.0); | |
172 else if (f < 3660.0) | |
173 scale = scaling(f, 3600.0, 3660.0, -2.0, -20.0); | |
174 else if (f < 3680.0) | |
175 scale = scaling(f, 3600.0, 3680.0, -20.0, -30.0); | |
176 else | |
177 scale = -60.0; | |
178 #else | |
179 scale = 0.0; | |
180 #endif | |
181 #if defined(HAVE_FFTW3_H) | |
182 in[i][0] = 0.0; | |
183 in[i][1] = (rand() & 0x1) ? 1.0 : -1.0; | |
184 in[i][1] *= pow(10.0, scale/20.0); | |
185 in[i][1] *= 35.0; //305360 | |
186 //printf("%10d %15.5f %15.5f\n", i, in[i].re, in[i].im); | |
187 in[8192 - i][0] = 0.0; | |
188 in[8192 - i][1] = -in[i][1]; | |
189 //printf("%10d %15.5f %15.5f\n", i, in[i][0], in[i][1]); | |
190 #else | |
191 in[i].re = 0.0; | |
192 in[i].im = (rand() & 0x1) ? 1.0 : -1.0; | |
193 in[i].im *= pow(10.0, scale/20.0); | |
194 in[i].im *= 35.0; //305360 | |
195 in[8192 - i].re = 0.0; | |
196 in[8192 - i].im = -in[i].im; | |
197 //printf("%10d %15.5f %15.5f\n", i, in[i].re, in[i].im); | |
198 #endif | |
199 } | |
200 #if defined(HAVE_FFTW3_H) | |
201 fftw_execute(p); | |
202 #else | |
203 fftw_one(p, in, out); | |
204 #endif | |
205 for (i = 0; i < 8192; i++) | |
206 { | |
207 //printf("%10d %15.5f %15.5f\n", i, out[i].re, out[i].im); | |
208 #if defined(HAVE_FFTW3_H) | |
209 noise_sound[i] = out[i][0]; | |
210 #else | |
211 noise_sound[i] = out[i].re; | |
212 #endif | |
213 } | |
214 | |
215 peak = 0; | |
216 ms = 0; | |
217 for (i = 0; i < 8192; i++) | |
218 { | |
219 if (abs(noise_sound[i]) > peak) | |
220 peak = abs(noise_sound[i]); | |
221 ms += noise_sound[i]*noise_sound[i]; | |
222 } | |
223 printf("Noise level = %.2fdB\n", 20.0*log10(sqrt(ms/8192.0)/32767.0) + DBM0_MAX_POWER); | |
224 printf("Crest factor = %.2fdB\n", 20.0*log10(peak/sqrt(ms/8192.0))); | |
225 | |
226 for (i = 0; i < 8192; i++) | |
227 silence_sound[i] = 0.0; | |
228 | |
229 for (j = 0; j < 16; j++) | |
230 { | |
231 outframes = afWriteFrames(filehandle, | |
232 AF_DEFAULT_TRACK, | |
233 voiced_sound, | |
234 voiced_length); | |
235 } | |
236 printf("%d samples of voice\n", 16*voiced_length); | |
237 outframes = afWriteFrames(filehandle, | |
238 AF_DEFAULT_TRACK, | |
239 noise_sound, | |
240 8192); | |
241 outframes = afWriteFrames(filehandle, | |
242 AF_DEFAULT_TRACK, | |
243 noise_sound, | |
244 8820 - 8192); | |
245 printf("%d samples of noise\n", 8820); | |
246 outframes = afWriteFrames(filehandle, | |
247 AF_DEFAULT_TRACK, | |
248 silence_sound, | |
249 4471); | |
250 printf("%d samples of silence\n", 4471); | |
251 | |
252 for (j = 0; j < voiced_length; j++) | |
253 voiced_sound[j] = -voiced_sound[j]; | |
254 for (j = 0; j < 8192; j++) | |
255 noise_sound[j] = -noise_sound[j]; | |
256 | |
257 for (j = 0; j < 16; j++) | |
258 { | |
259 outframes = afWriteFrames(filehandle, | |
260 AF_DEFAULT_TRACK, | |
261 voiced_sound, | |
262 voiced_length); | |
263 } | |
264 printf("%d samples of voice\n", 16*voiced_length); | |
265 outframes = afWriteFrames(filehandle, | |
266 AF_DEFAULT_TRACK, | |
267 noise_sound, | |
268 8192); | |
269 outframes = afWriteFrames(filehandle, | |
270 AF_DEFAULT_TRACK, | |
271 noise_sound, | |
272 8820 - 8192); | |
273 printf("%d samples of noise\n", 8820); | |
274 outframes = afWriteFrames(filehandle, | |
275 AF_DEFAULT_TRACK, | |
276 silence_sound, | |
277 4471); | |
278 printf("%d samples of silence\n", 4471); | |
279 | |
280 if (afCloseFile(filehandle) != 0) | |
281 { | |
282 fprintf(stderr, " Cannot close speech file '%s'\n", "sound_c1.wav"); | |
283 exit(2); | |
284 } | |
285 | |
286 afInitSampleFormat(filesetup, AF_DEFAULT_TRACK, AF_SAMPFMT_TWOSCOMP, 16); | |
287 afInitRate(filesetup, AF_DEFAULT_TRACK, FAST_SAMPLE_RATE); | |
288 afInitFileFormat(filesetup, AF_FILE_WAVE); | |
289 afInitChannels(filesetup, AF_DEFAULT_TRACK, 1); | |
290 filehandle = afOpenFile("sound_c3.wav", "w", filesetup); | |
291 if (filehandle == AF_NULL_FILEHANDLE) | |
292 { | |
293 fprintf(stderr, " Failed to open result file\n"); | |
294 exit(2); | |
295 } | |
296 | |
297 for (i = 0; i < (int) (sizeof(css_c3)/sizeof(css_c3[0])); i++) | |
298 voiced_sound[i] = css_c3[i]; | |
299 voiced_length = i; | |
300 | |
301 ms = 0; | |
302 for (i = 0; i < voiced_length; i++) | |
303 { | |
304 if (abs(voiced_sound[i]) > peak) | |
305 peak = abs(voiced_sound[i]); | |
306 ms += voiced_sound[i]*voiced_sound[i]; | |
307 } | |
308 ms = 20.0*log10(sqrt(ms/voiced_length)/32767.0) + DBM0_MAX_POWER; | |
309 printf("Voiced level = %.2fdB\n", ms); | |
310 | |
311 awgn_init_dbm0(&noise_source, 7162534, ms); | |
312 for (i = 0; i < 8192; i++) | |
313 noise_sound[i] = awgn(&noise_source); | |
314 peak = 0; | |
315 ms = 0; | |
316 for (i = 0; i < 8192; i++) | |
317 { | |
318 if (abs(noise_sound[i]) > peak) | |
319 peak = abs(noise_sound[i]); | |
320 ms += noise_sound[i]*noise_sound[i]; | |
321 } | |
322 printf("Noise level = %.2fdB\n", 20.0*log10(sqrt(ms/8192.0)/32767.0) + DBM0_MAX_POWER); | |
323 printf("Crest factor = %.2fdB\n", 20.0*log10(peak/sqrt(ms/8192.0))); | |
324 | |
325 for (j = 0; j < 14; j++) | |
326 { | |
327 outframes = afWriteFrames(filehandle, | |
328 AF_DEFAULT_TRACK, | |
329 voiced_sound, | |
330 voiced_length); | |
331 } | |
332 printf("%d samples of voice\n", 14*i); | |
333 outframes = afWriteFrames(filehandle, | |
334 AF_DEFAULT_TRACK, | |
335 noise_sound, | |
336 8192); | |
337 outframes = afWriteFrames(filehandle, | |
338 AF_DEFAULT_TRACK, | |
339 noise_sound, | |
340 8820 - 8192); | |
341 printf("%d samples of noise\n", 8820); | |
342 outframes = afWriteFrames(filehandle, | |
343 AF_DEFAULT_TRACK, | |
344 silence_sound, | |
345 5614); | |
346 printf("%d samples of silence\n", 5614); | |
347 | |
348 for (j = 0; j < voiced_length; j++) | |
349 voiced_sound[j] = -voiced_sound[j]; | |
350 for (j = 0; j < 8192; j++) | |
351 noise_sound[j] = -noise_sound[j]; | |
352 | |
353 for (j = 0; j < 14; j++) | |
354 { | |
355 outframes = afWriteFrames(filehandle, | |
356 AF_DEFAULT_TRACK, | |
357 voiced_sound, | |
358 voiced_length); | |
359 } | |
360 printf("%d samples of voice\n", 14*i); | |
361 outframes = afWriteFrames(filehandle, | |
362 AF_DEFAULT_TRACK, | |
363 noise_sound, | |
364 8192); | |
365 outframes = afWriteFrames(filehandle, | |
366 AF_DEFAULT_TRACK, | |
367 noise_sound, | |
368 8820 - 8192); | |
369 printf("%d samples of noise\n", 8820); | |
370 outframes = afWriteFrames(filehandle, | |
371 AF_DEFAULT_TRACK, | |
372 silence_sound, | |
373 5614); | |
374 printf("%d samples of silence\n", 5614); | |
375 | |
376 if (afCloseFile(filehandle) != 0) | |
377 { | |
378 fprintf(stderr, " Cannot close speech file '%s'\n", "sound_c3.wav"); | |
379 exit(2); | |
380 } | |
381 afFreeFileSetup(filesetup); | |
382 | |
383 fftw_destroy_plan(p); | |
384 return 0; | |
385 } | |
386 /*- End of function --------------------------------------------------------*/ | |
387 /*- End of file ------------------------------------------------------------*/ |