5
|
1 /*
|
|
2 * SpanDSP - a series of DSP components for telephony
|
|
3 *
|
|
4 * test_sig_tone.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: sig_tone_tests.c,v 1.15 2006/11/19 14:07:27 steveu Exp $
|
|
26 */
|
|
27
|
|
28 /*! \file */
|
|
29
|
|
30 /*! \page sig_tone_tests_page The signaling tone processor tests
|
|
31 \section sig_tone_tests_sec_1 What does it do?
|
|
32 ???.
|
|
33
|
|
34 \section sig_tone_tests_sec_2 How does it work?
|
|
35 ???.
|
|
36 */
|
|
37
|
|
38 #ifdef HAVE_CONFIG_H
|
|
39 #include "config.h"
|
|
40 #endif
|
|
41
|
|
42 #include <stdlib.h>
|
|
43 #include <stdio.h>
|
|
44 #include <inttypes.h>
|
|
45 #include <memory.h>
|
|
46 #if defined(HAVE_TGMATH_H)
|
|
47 #include <tgmath.h>
|
|
48 #endif
|
|
49 #if defined(HAVE_MATH_H)
|
|
50 #include <math.h>
|
|
51 #endif
|
|
52 #include <audiofile.h>
|
|
53 #include <tiffio.h>
|
|
54
|
|
55 #include "spandsp.h"
|
|
56
|
|
57 #define OUT_FILE_NAME "sig_tone.wav"
|
|
58
|
|
59 static int sampleno = 0;
|
|
60 static int tone_1_present = 0;
|
|
61 static int tone_2_present = 0;
|
|
62 static int ping = 0;
|
|
63
|
|
64 void map_frequency_response(sig_tone_state_t *s);
|
|
65
|
|
66 static int handler(void *user_data, int what)
|
|
67 {
|
|
68 //printf("What - %d\n", what);
|
|
69 if ((what & SIG_TONE_1_CHANGE))
|
|
70 {
|
|
71 tone_1_present = what & SIG_TONE_1_PRESENT;
|
|
72 printf("Tone 1 is %s after %d samples\n", (tone_1_present) ? "on" : "off", (what >> 16) & 0xFFFF);
|
|
73 }
|
|
74 /*endif*/
|
|
75 if ((what & SIG_TONE_2_CHANGE))
|
|
76 {
|
|
77 tone_2_present = what & SIG_TONE_2_PRESENT;
|
|
78 printf("Tone 2 is %s after %d samples\n", (tone_2_present) ? "on" : "off", (what >> 16) & 0xFFFF);
|
|
79 }
|
|
80 /*endif*/
|
|
81 if ((what & SIG_TONE_UPDATE_REQUEST))
|
|
82 {
|
|
83 /* The signaling processor wants to know what to do next */
|
|
84 if (sampleno < 800)
|
|
85 {
|
|
86 /* 100ms off-hook */
|
|
87 printf("100ms off-hook - %d samples\n", 800 - sampleno);
|
|
88 return 0x02 | ((800 - sampleno) << 16) | SIG_TONE_RX_PASSTHROUGH;
|
|
89 }
|
|
90 /*endif*/
|
|
91 if (sampleno < 4800)
|
|
92 {
|
|
93 /* 500ms idle */
|
|
94 printf("500ms idle - %d samples\n", 4800 - sampleno);
|
|
95 return 0x02 | SIG_TONE_1_PRESENT | ((4800 - sampleno) << 16) | SIG_TONE_RX_PASSTHROUGH;
|
|
96 }
|
|
97 /*endif*/
|
|
98 if (sampleno < 5600)
|
|
99 {
|
|
100 /* 100ms seize */
|
|
101 printf("100ms seize - %d samples\n", 5600 - sampleno);
|
|
102 return 0x02 | ((5600 - sampleno) << 16) | SIG_TONE_RX_PASSTHROUGH;
|
|
103 }
|
|
104 /*endif*/
|
|
105 if (ping)
|
|
106 {
|
|
107 printf("33ms break - 262 samples\n");
|
|
108 ping = !ping;
|
|
109 return 0x02 | (262 << 16) | SIG_TONE_RX_PASSTHROUGH;
|
|
110 }
|
|
111 else
|
|
112 {
|
|
113 printf("67ms make - 528 samples\n");
|
|
114 ping = !ping;
|
|
115 return 0x02 | SIG_TONE_1_PRESENT | (528 << 16) | SIG_TONE_RX_PASSTHROUGH;
|
|
116 }
|
|
117 /*endif*/
|
|
118 }
|
|
119 /*endif*/
|
|
120 return 0;
|
|
121 }
|
|
122 /*- End of function --------------------------------------------------------*/
|
|
123
|
|
124 void map_frequency_response(sig_tone_state_t *s)
|
|
125 {
|
|
126 int16_t buf[8192];
|
|
127 awgn_state_t noise_source;
|
|
128 int i;
|
|
129 int f;
|
|
130 uint32_t phase_acc;
|
|
131 int32_t phase_rate;
|
|
132 int32_t scaling;
|
|
133 double sum;
|
|
134
|
|
135 /* Things like noise don't highlight the frequency response of the high Q notch
|
|
136 very well. We use a slowly swept frequency to check it. */
|
|
137 awgn_init_dbm0(&noise_source, 1234567, -10.0f);
|
|
138 for (f = 1; f < 4000; f++)
|
|
139 {
|
|
140 phase_rate = dds_phase_rate(f);
|
|
141 scaling = dds_scaling_dbm0(-10);
|
|
142 phase_acc = 0;
|
|
143 for (i = 0; i < 8192; i++)
|
|
144 buf[i] = dds_mod(&phase_acc, phase_rate, scaling, 0);
|
|
145 /*endfor*/
|
|
146 sig_tone_rx(s, buf, 8192);
|
|
147 sum = 0.0;
|
|
148 for (i = 1000; i < 8192; i++)
|
|
149 sum += (double) buf[i]*(double) buf[i];
|
|
150 /*endfor*/
|
|
151 sum = sqrt(sum);
|
|
152 printf("%7d %f\n", f, sum);
|
|
153 }
|
|
154 /*endfor*/
|
|
155 }
|
|
156 /*- End of function --------------------------------------------------------*/
|
|
157
|
|
158 int main(int argc, char *argv[])
|
|
159 {
|
|
160 int16_t amp[160];
|
|
161 int16_t out_amp[2*160];
|
|
162 AFfilehandle outhandle;
|
|
163 AFfilesetup filesetup;
|
|
164 int outframes;
|
|
165 int i;
|
|
166 int rx_samples;
|
|
167 int tx_samples;
|
|
168 sig_tone_state_t state;
|
|
169 awgn_state_t noise_source;
|
|
170
|
|
171 filesetup = afNewFileSetup();
|
|
172 if (filesetup == AF_NULL_FILESETUP)
|
|
173 {
|
|
174 fprintf(stderr, " Failed to create file setup\n");
|
|
175 exit(2);
|
|
176 }
|
|
177 /*endif*/
|
|
178 afInitSampleFormat(filesetup, AF_DEFAULT_TRACK, AF_SAMPFMT_TWOSCOMP, 16);
|
|
179 afInitRate(filesetup, AF_DEFAULT_TRACK, (float) SAMPLE_RATE);
|
|
180 afInitFileFormat(filesetup, AF_FILE_WAVE);
|
|
181 afInitChannels(filesetup, AF_DEFAULT_TRACK, 2);
|
|
182
|
|
183 outhandle = afOpenFile(OUT_FILE_NAME, "w", filesetup);
|
|
184 if (outhandle == AF_NULL_FILEHANDLE)
|
|
185 {
|
|
186 fprintf(stderr, " Cannot create wave file '%s'\n", OUT_FILE_NAME);
|
|
187 exit(2);
|
|
188 }
|
|
189 /*endif*/
|
|
190
|
|
191 awgn_init_dbm0(&noise_source, 1234567, -30.0f);
|
|
192
|
|
193 printf("2400Hz/26000Hz tests.\n");
|
|
194 sig_tone_init(&state, SIG_TONE_2400HZ_2600HZ, handler, NULL);
|
|
195 state.current_tx_tone |= SIG_TONE_RX_PASSTHROUGH;
|
|
196
|
|
197 map_frequency_response(&state);
|
|
198
|
|
199 for (sampleno = 0; sampleno < 20000; sampleno += 160)
|
|
200 {
|
|
201 tx_samples = 160;
|
|
202 for (i = 0; i < tx_samples; i++)
|
|
203 amp[i] = alaw_to_linear(linear_to_alaw(awgn(&noise_source)));
|
|
204 /*endfor*/
|
|
205 for (i = 0; i < tx_samples; i++)
|
|
206 out_amp[2*i] = amp[i];
|
|
207 /*endfor*/
|
|
208 rx_samples = sig_tone_rx(&state, amp, tx_samples);
|
|
209 for (i = 0; i < rx_samples; i++)
|
|
210 out_amp[2*i + 1] = amp[i];
|
|
211 /*endfor*/
|
|
212 outframes = afWriteFrames(outhandle,
|
|
213 AF_DEFAULT_TRACK,
|
|
214 out_amp,
|
|
215 rx_samples);
|
|
216 if (outframes != rx_samples)
|
|
217 {
|
|
218 fprintf(stderr, " Error writing wave file\n");
|
|
219 exit(2);
|
|
220 }
|
|
221 /*endif*/
|
|
222 }
|
|
223
|
|
224 printf("2280Hz tests.\n");
|
|
225 sig_tone_init(&state, SIG_TONE_2280HZ, handler, NULL);
|
|
226 state.current_tx_tone |= SIG_TONE_RX_PASSTHROUGH;
|
|
227
|
|
228 map_frequency_response(&state);
|
|
229
|
|
230 for (sampleno = 0; sampleno < 20000; sampleno += 160)
|
|
231 {
|
|
232 memset(amp, 0, sizeof(int16_t)*160);
|
|
233 tx_samples = sig_tone_tx(&state, amp, 160);
|
|
234 for (i = 0; i < tx_samples; i++)
|
|
235 out_amp[2*i] = amp[i];
|
|
236 /*endfor*/
|
|
237 rx_samples = sig_tone_rx(&state, amp, tx_samples);
|
|
238 for (i = 0; i < rx_samples; i++)
|
|
239 out_amp[2*i + 1] = amp[i];
|
|
240 /*endfor*/
|
|
241 outframes = afWriteFrames(outhandle,
|
|
242 AF_DEFAULT_TRACK,
|
|
243 out_amp,
|
|
244 rx_samples);
|
|
245 if (outframes != rx_samples)
|
|
246 {
|
|
247 fprintf(stderr, " Error writing wave file\n");
|
|
248 exit(2);
|
|
249 }
|
|
250 /*endif*/
|
|
251 }
|
|
252 /*endfor*/
|
|
253 if (afCloseFile(outhandle) != 0)
|
|
254 {
|
|
255 fprintf(stderr, " Cannot close wave file '%s'\n", OUT_FILE_NAME);
|
|
256 exit(2);
|
|
257 }
|
|
258 /*endif*/
|
|
259 afFreeFileSetup(filesetup);
|
|
260
|
|
261 printf("Tests completed.\n");
|
|
262 return 0;
|
|
263 }
|
|
264 /*- End of function --------------------------------------------------------*/
|
|
265 /*- End of file ------------------------------------------------------------*/
|