5
|
1 /*
|
|
2 * SpanDSP - a series of DSP components for telephony
|
|
3 *
|
|
4 * plc_tests.c
|
|
5 *
|
|
6 * Written by Steve Underwood <steveu@coppice.org>
|
|
7 *
|
|
8 * Copyright (C) 2004 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: plc_tests.c,v 1.18 2006/11/19 14:07:27 steveu Exp $
|
|
26 */
|
|
27
|
|
28 /*! \page plc_tests_page Packet loss concealment tests
|
|
29 \section plc_tests_page_sec_1 What does it do?
|
|
30 These tests run a speech file through the packet loss concealment routines.
|
|
31 The loss rate, in percent, and the packet size, in samples, may be specified
|
|
32 on the command line.
|
|
33
|
|
34 \section plc_tests_page_sec_2 How are the tests run?
|
|
35 These tests process a speech file called pre_plc.wav. This file should contain
|
|
36 8000 sample/second 16 bits/sample linear audio. The tests read this file in
|
|
37 blocks, of a size specified on the command line. Some of these blocks are
|
|
38 dropped, to simulate packet loss. The rate of loss is also specified on the
|
|
39 command line. The PLC module is then used to reconstruct an acceptable
|
|
40 approximation to the original signal. The resulting audio is written to a new
|
|
41 audio file, called post_plc.wav. This file contains 8000 sample/second
|
|
42 16 bits/sample linear audio.
|
|
43 */
|
|
44
|
|
45 #ifdef HAVE_CONFIG_H
|
|
46 #include "config.h"
|
|
47 #endif
|
|
48
|
|
49 #include <stdio.h>
|
|
50 #include <inttypes.h>
|
|
51 #include <stdlib.h>
|
|
52 #include <string.h>
|
|
53 #if defined(HAVE_TGMATH_H)
|
|
54 #include <tgmath.h>
|
|
55 #endif
|
|
56 #if defined(HAVE_MATH_H)
|
|
57 #include <math.h>
|
|
58 #endif
|
|
59 #include <tiffio.h>
|
|
60
|
|
61 #include <audiofile.h>
|
|
62
|
|
63 #include "spandsp.h"
|
|
64
|
|
65 #define INPUT_FILE_NAME "../localtests/short_nb_voice.wav"
|
|
66 #define OUTPUT_FILE_NAME "post_plc.wav"
|
|
67
|
|
68 int main(int argc, char *argv[])
|
|
69 {
|
|
70 AFfilehandle inhandle;
|
|
71 AFfilehandle outhandle;
|
|
72 AFfilesetup filesetup;
|
|
73 plc_state_t plc;
|
|
74 int inframes;
|
|
75 int outframes;
|
|
76 int16_t amp[1024];
|
|
77 int block_no;
|
|
78 int lost_blocks;
|
|
79 int block_len;
|
|
80 int loss_rate;
|
|
81 int dropit;
|
|
82 int block_real;
|
|
83 int block_synthetic;
|
|
84 int tone;
|
|
85 int i;
|
|
86 uint32_t phase_acc;
|
|
87 int32_t phase_rate;
|
|
88
|
|
89 loss_rate = 25;
|
|
90 block_len = 160;
|
|
91 block_real = FALSE;
|
|
92 block_synthetic = FALSE;
|
|
93 tone = -1;
|
|
94 for (i = 1; i < argc; i++)
|
|
95 {
|
|
96 if (strcmp(argv[i], "-l") == 0)
|
|
97 {
|
|
98 loss_rate = atoi(argv[++i]);
|
|
99 continue;
|
|
100 }
|
|
101 if (strcmp(argv[i], "-b") == 0)
|
|
102 {
|
|
103 block_len = atoi(argv[++i]);
|
|
104 continue;
|
|
105 }
|
|
106 if (strcmp(argv[i], "-t") == 0)
|
|
107 {
|
|
108 tone = atoi(argv[++i]);
|
|
109 continue;
|
|
110 }
|
|
111 if (strcmp(argv[i], "-r") == 0)
|
|
112 block_real = TRUE;
|
|
113 if (strcmp(argv[i], "-s") == 0)
|
|
114 block_synthetic = TRUE;
|
|
115 }
|
|
116 if ((filesetup = afNewFileSetup()) == AF_NULL_FILESETUP)
|
|
117 {
|
|
118 fprintf(stderr, " Failed to create file setup\n");
|
|
119 exit(2);
|
|
120 }
|
|
121 afInitSampleFormat(filesetup, AF_DEFAULT_TRACK, AF_SAMPFMT_TWOSCOMP, 16);
|
|
122 afInitRate(filesetup, AF_DEFAULT_TRACK, (float) SAMPLE_RATE);
|
|
123 afInitFileFormat(filesetup, AF_FILE_WAVE);
|
|
124 afInitChannels(filesetup, AF_DEFAULT_TRACK, 1);
|
|
125
|
|
126 phase_rate = 0;
|
|
127 inhandle = NULL;
|
|
128 if (tone < 0)
|
|
129 {
|
|
130 if ((inhandle = afOpenFile(INPUT_FILE_NAME, "r", NULL)) == AF_NULL_FILEHANDLE)
|
|
131 {
|
|
132 fprintf(stderr, " Failed to open wave file '%s'\n", INPUT_FILE_NAME);
|
|
133 exit(2);
|
|
134 }
|
|
135 }
|
|
136 else
|
|
137 {
|
|
138 phase_rate = dds_phase_ratef((float) tone);
|
|
139 }
|
|
140 if ((outhandle = afOpenFile(OUTPUT_FILE_NAME, "w", filesetup)) == AF_NULL_FILEHANDLE)
|
|
141 {
|
|
142 fprintf(stderr, " Failed to open wave file '%s'\n", OUTPUT_FILE_NAME);
|
|
143 exit(2);
|
|
144 }
|
|
145 plc_init(&plc);
|
|
146 lost_blocks = 0;
|
|
147 for (block_no = 0; ; block_no++)
|
|
148 {
|
|
149 if (tone < 0)
|
|
150 {
|
|
151 inframes = afReadFrames(inhandle,
|
|
152 AF_DEFAULT_TRACK,
|
|
153 amp,
|
|
154 block_len);
|
|
155 if (inframes != block_len)
|
|
156 break;
|
|
157 }
|
|
158 else
|
|
159 {
|
|
160 if (block_no > 10000)
|
|
161 break;
|
|
162 for (i = 0; i < block_len; i++)
|
|
163 amp[i] = (int16_t) dds_modf(&phase_acc, phase_rate, 10000.0, 0);
|
|
164 inframes = block_len;
|
|
165 }
|
|
166 dropit = rand()/(RAND_MAX/100);
|
|
167 if (dropit > loss_rate)
|
|
168 {
|
|
169 plc_rx(&plc, amp, inframes);
|
|
170 if (block_real)
|
|
171 memset(amp, 0, sizeof(int16_t)*inframes);
|
|
172 }
|
|
173 else
|
|
174 {
|
|
175 lost_blocks++;
|
|
176 plc_fillin(&plc, amp, inframes);
|
|
177 if (block_synthetic)
|
|
178 memset(amp, 0, sizeof(int16_t)*inframes);
|
|
179 }
|
|
180 outframes = afWriteFrames(outhandle,
|
|
181 AF_DEFAULT_TRACK,
|
|
182 amp,
|
|
183 inframes);
|
|
184 if (outframes != inframes)
|
|
185 {
|
|
186 fprintf(stderr, " Error writing out sound\n");
|
|
187 exit(2);
|
|
188 }
|
|
189 }
|
|
190 printf("Dropped %d of %d blocks\n", lost_blocks, block_no);
|
|
191 if (tone < 0)
|
|
192 {
|
|
193 if (afCloseFile(inhandle) != 0)
|
|
194 {
|
|
195 fprintf(stderr, " Cannot close wave file '%s'\n", INPUT_FILE_NAME);
|
|
196 exit(2);
|
|
197 }
|
|
198 }
|
|
199 if (afCloseFile(outhandle) != 0)
|
|
200 {
|
|
201 fprintf(stderr, " Cannot close wave file '%s'\n", OUTPUT_FILE_NAME);
|
|
202 exit(2);
|
|
203 }
|
|
204 afFreeFileSetup(filesetup);
|
|
205 return 0;
|
|
206 }
|
|
207 /*- End of function --------------------------------------------------------*/
|
|
208 /*- End of file ------------------------------------------------------------*/
|