1 /*
2  * SpanDSP - a series of DSP components for telephony
3  *
4  * line_model.h - Model a telephone line.
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 Lesser General Public License version 2.1,
14  * as 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 Lesser General Public License for more details.
20  *
21  * You should have received a copy of the GNU Lesser General Public
22  * License along with this program; if not, write to the Free Software
23  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24  *
25  * $Id: line_model.h,v 1.3 2008/04/17 18:03:23 steveu Exp $
26  */
27 
28 /*! \file */
29 
30 /*! \page line_model_page Telephone line model
31 \section line_model_page_sec_1 What does it do?
32 The telephone line modelling module provides simple modelling of one way and two
33 way telephone lines.
34 
35 The path being modelled is:
36 
37     -    terminal
38     -      | < hybrid echo (2-way models)
39     -      |
40     -      | < noise and filtering
41     -      |
42     -      | < hybrid echo (2-way models)
43     -     CO
44     -      |
45     -      | < A-law distortion + bulk delay
46     -      |
47     -     CO
48     -      | < hybrid echo (2-way models)
49     -      |
50     -      | < noise and filtering
51     -      |
52     -      | < hybrid echo (2-way models)
53     -    terminal
54 */
55 
56 #if !defined(_SPANDSP_LINE_MODEL_H_)
57 #define _SPANDSP_LINE_MODEL_H_
58 
59 #define LINE_FILTER_SIZE 129
60 
61 /*!
62     One way line model descriptor. This holds the complete state of
63     a line model with transmission in only one direction.
64 */
65 typedef struct
66 {
67     codec_munge_state_t *munge;
68 
69     /*! The coefficients for the near end analogue section simulation filter */
70     float *near_filter;
71     /*! The number of coefficients for the near end analogue section simulation filter */
72     int near_filter_len;
73     /*! Last transmitted samples (ring buffer, used by the line filter) */
74     float near_buf[LINE_FILTER_SIZE];
75     /*! Pointer of the last transmitted sample in buf */
76     int near_buf_ptr;
77     /*! The noise source for local analogue section of the line */
78     awgn_state_t near_noise;
79 
80     /*! The bulk delay of the path, in samples */
81     int bulk_delay;
82     /*! A pointer to the current write position in the bulk delay store. */
83     int bulk_delay_ptr;
84     /*! The data store for simulating the bulk delay */
85     int16_t bulk_delay_buf[8000];
86 
87     /*! The coefficients for the far end analogue section simulation filter */
88     float *far_filter;
89     /*! The number of coefficients for the far end analogue section simulation filter */
90     int far_filter_len;
91     /*! Last transmitted samples (ring buffer, used by the line filter) */
92     float far_buf[LINE_FILTER_SIZE];
93     /*! Pointer of the last transmitted sample in buf */
94     int far_buf_ptr;
95     /*! The noise source for distant analogue section of the line */
96     awgn_state_t far_noise;
97 
98     /*! The scaling factor for the local CPE hybrid echo */
99     float near_cpe_hybrid_echo;
100     /*! The scaling factor for the local CO hybrid echo */
101     float near_co_hybrid_echo;
102 
103     /*! The scaling factor for the far CPE hybrid echo */
104     float far_cpe_hybrid_echo;
105     /*! The scaling factor for the far CO hybrid echo */
106     float far_co_hybrid_echo;
107     /*! DC offset impairment */
108     float dc_offset;
109 
110     /*! Mains pickup impairment */
111     int mains_interference;
112     tone_gen_state_t mains_tone;
113 } one_way_line_model_state_t;
114 
115 /*!
116     Two way line model descriptor. This holds the complete state of
117     a line model with transmission in both directions.
118 */
119 typedef struct
120 {
121     one_way_line_model_state_t line1;
122     one_way_line_model_state_t line2;
123     float fout1;
124     float fout2;
125 } both_ways_line_model_state_t;
126 
127 #ifdef __cplusplus
128 extern "C"
129 {
130 #endif
131 
132 void both_ways_line_model(both_ways_line_model_state_t *s,
133                           int16_t output1[],
134                           const int16_t input1[],
135                           int16_t output2[],
136                           const int16_t input2[],
137                           int samples);
138 
139 void both_ways_line_model_set_dc(both_ways_line_model_state_t *s, float dc1, float dc2);
140 
141 void both_ways_line_model_set_mains_pickup(both_ways_line_model_state_t *s, int f, float level1, float level2);
142 
143 both_ways_line_model_state_t *both_ways_line_model_init(int model1,
144                                                         float noise1,
145                                                         int model2,
146                                                         float noise2,
147                                                         int codec,
148                                                         int rbs_pattern);
149 
150 int both_ways_line_model_release(both_ways_line_model_state_t *s);
151 
152 void one_way_line_model(one_way_line_model_state_t *s,
153                         int16_t output[],
154                         const int16_t input[],
155                         int samples);
156 
157 void one_way_line_model_set_dc(one_way_line_model_state_t *s, float dc);
158 
159 void one_way_line_model_set_mains_pickup(one_way_line_model_state_t *s, int f, float level);
160 
161 one_way_line_model_state_t *one_way_line_model_init(int model, float noise, int codec, int rbs_pattern);
162 
163 int one_way_line_model_release(one_way_line_model_state_t *s);
164 
165 #ifdef __cplusplus
166 }
167 #endif
168 
169 #endif
170 /*- End of file ------------------------------------------------------------*/
171