Mercurial > hg > audiostuff
comparison spandsp-0.0.6pre17/tests/plc_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 * 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.26 2009/05/30 15:23:14 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 #if defined(HAVE_CONFIG_H) | |
46 #include "config.h" | |
47 #endif | |
48 | |
49 #include <stdlib.h> | |
50 #include <stdio.h> | |
51 #include <unistd.h> | |
52 #include <string.h> | |
53 | |
54 #include <sndfile.h> | |
55 | |
56 #include "spandsp.h" | |
57 #include "spandsp-sim.h" | |
58 | |
59 #define INPUT_FILE_NAME "../test-data/local/short_nb_voice.wav" | |
60 #define OUTPUT_FILE_NAME "post_plc.wav" | |
61 | |
62 int main(int argc, char *argv[]) | |
63 { | |
64 SNDFILE *inhandle; | |
65 SNDFILE *outhandle; | |
66 plc_state_t plc; | |
67 int inframes; | |
68 int outframes; | |
69 int16_t amp[1024]; | |
70 int block_no; | |
71 int lost_blocks; | |
72 int block_len; | |
73 int loss_rate; | |
74 int dropit; | |
75 int block_real; | |
76 int block_synthetic; | |
77 int tone; | |
78 int i; | |
79 uint32_t phase_acc; | |
80 int32_t phase_rate; | |
81 int opt; | |
82 | |
83 loss_rate = 25; | |
84 block_len = 160; | |
85 block_real = FALSE; | |
86 block_synthetic = FALSE; | |
87 tone = -1; | |
88 while ((opt = getopt(argc, argv, "b:l:rst:")) != -1) | |
89 { | |
90 switch (opt) | |
91 { | |
92 case 'b': | |
93 block_len = atoi(optarg); | |
94 break; | |
95 case 'l': | |
96 loss_rate = atoi(optarg); | |
97 break; | |
98 case 'r': | |
99 block_real = TRUE; | |
100 break; | |
101 case 's': | |
102 block_synthetic = TRUE; | |
103 break; | |
104 case 't': | |
105 tone = atoi(optarg); | |
106 break; | |
107 } | |
108 } | |
109 phase_rate = 0; | |
110 inhandle = NULL; | |
111 if (tone < 0) | |
112 { | |
113 if ((inhandle = sf_open_telephony_read(INPUT_FILE_NAME, 1)) == NULL) | |
114 { | |
115 fprintf(stderr, " Failed to open audio file '%s'\n", INPUT_FILE_NAME); | |
116 exit(2); | |
117 } | |
118 } | |
119 else | |
120 { | |
121 phase_rate = dds_phase_ratef((float) tone); | |
122 } | |
123 if ((outhandle = sf_open_telephony_write(OUTPUT_FILE_NAME, 1)) == NULL) | |
124 { | |
125 fprintf(stderr, " Failed to open audio file '%s'\n", OUTPUT_FILE_NAME); | |
126 exit(2); | |
127 } | |
128 plc_init(&plc); | |
129 lost_blocks = 0; | |
130 for (block_no = 0; ; block_no++) | |
131 { | |
132 if (tone < 0) | |
133 { | |
134 inframes = sf_readf_short(inhandle, amp, block_len); | |
135 if (inframes != block_len) | |
136 break; | |
137 } | |
138 else | |
139 { | |
140 if (block_no > 10000) | |
141 break; | |
142 for (i = 0; i < block_len; i++) | |
143 amp[i] = (int16_t) dds_modf(&phase_acc, phase_rate, 10000.0, 0); | |
144 inframes = block_len; | |
145 } | |
146 dropit = rand()/(RAND_MAX/100); | |
147 if (dropit > loss_rate) | |
148 { | |
149 plc_rx(&plc, amp, inframes); | |
150 if (block_real) | |
151 memset(amp, 0, sizeof(int16_t)*inframes); | |
152 } | |
153 else | |
154 { | |
155 lost_blocks++; | |
156 plc_fillin(&plc, amp, inframes); | |
157 if (block_synthetic) | |
158 memset(amp, 0, sizeof(int16_t)*inframes); | |
159 } | |
160 outframes = sf_writef_short(outhandle, amp, inframes); | |
161 if (outframes != inframes) | |
162 { | |
163 fprintf(stderr, " Error writing out sound\n"); | |
164 exit(2); | |
165 } | |
166 } | |
167 printf("Dropped %d of %d blocks\n", lost_blocks, block_no); | |
168 if (tone < 0) | |
169 { | |
170 if (sf_close(inhandle) != 0) | |
171 { | |
172 fprintf(stderr, " Cannot close audio file '%s'\n", INPUT_FILE_NAME); | |
173 exit(2); | |
174 } | |
175 } | |
176 if (sf_close(outhandle) != 0) | |
177 { | |
178 fprintf(stderr, " Cannot close audio file '%s'\n", OUTPUT_FILE_NAME); | |
179 exit(2); | |
180 } | |
181 return 0; | |
182 } | |
183 /*- End of function --------------------------------------------------------*/ | |
184 /*- End of file ------------------------------------------------------------*/ |