spandsp  3.0.0
telephony.h
1 /*
2  * SpanDSP - a series of DSP components for telephony
3  *
4  * telephony.h - some very basic telephony definitions
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 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 
26 #if !defined(_SPANDSP_TELEPHONY_H_)
27 #define _SPANDSP_TELEPHONY_H_
28 
29 #if defined(_M_IX86) || defined(_M_X64)
30 #if defined(LIBSPANDSP_EXPORTS)
31 #define SPAN_DECLARE(type) __declspec(dllexport) type
32 #define SPAN_DECLARE_DATA __declspec(dllexport)
33 #else
34 #define SPAN_DECLARE(type) __declspec(dllimport) type
35 #define SPAN_DECLARE_DATA __declspec(dllimport)
36 #endif
37 #elif defined(SPANDSP_USE_EXPORT_CAPABILITY) && (defined(__GNUC__) || defined(__SUNCC__))
38 #define SPAN_DECLARE(type) __attribute__((visibility("default"))) type
39 #define SPAN_DECLARE_DATA __attribute__((visibility("default")))
40 #else
41 #define SPAN_DECLARE(type) /**/ type
42 #define SPAN_DECLARE_DATA /**/
43 #endif
44 
45 #define span_container_of(ptr, type, member) ({ \
46  const typeof(((type *) 0)->member) *__mptr = (ptr); \
47  (type *) ((char *) __mptr - offsetof(type, member));})
48 
49 #define SAMPLE_RATE 8000
50 
51 /*! \brief A timer variable large enough that when in microseconds it just
52  won't overflow. Most things in spandsp are timed by audio samples,
53  but some things need access to global timers. */
54 typedef uint64_t span_timestamp_t;
55 
56 /*! \brief A timer variable for timing by counting audio samples. */
57 typedef int32_t span_sample_timer_t;
58 
59 /*! \brief A handler for pure receive. The buffer cannot be altered. */
60 typedef int (*span_rx_handler_t)(void *s, const int16_t amp[], int len);
61 
62 /*! \brief A handler for receive, where the buffer can be altered. */
63 typedef int (*span_mod_handler_t)(void *s, int16_t amp[], int len);
64 
65 /*! \brief A handler for missing receive data fill-in. */
66 typedef int (*span_rx_fillin_handler_t)(void *s, int len);
67 
68 /*! \brief A handler for transmit, where the buffer will be filled. */
69 typedef int (*span_tx_handler_t)(void *s, int16_t amp[], int max_len);
70 
71 #define seconds_to_samples(t) ((t)*SAMPLE_RATE)
72 #define milliseconds_to_samples(t) ((t)*(SAMPLE_RATE/1000))
73 #define microseconds_to_samples(t) ((t)/(1000000/SAMPLE_RATE))
74 
75 /* Fixed point constant macros for 16 bit values */
76 #define FP_Q16_0(x) ((int16_t) (1.0*(x) + (((x) >= 0.0) ? 0.5 : -0.5)))
77 #define FP_Q15_1(x) ((int16_t) (2.0*(x) + (((x) >= 0.0) ? 0.5 : -0.5)))
78 #define FP_Q14_2(x) ((int16_t) (4.0*(x) + (((x) >= 0.0) ? 0.5 : -0.5)))
79 #define FP_Q13_3(x) ((int16_t) (8.0*(x) + (((x) >= 0.0) ? 0.5 : -0.5)))
80 #define FP_Q12_4(x) ((int16_t) (16.0*(x) + (((x) >= 0.0) ? 0.5 : -0.5)))
81 #define FP_Q11_5(x) ((int16_t) (32.0*(x) + (((x) >= 0.0) ? 0.5 : -0.5)))
82 #define FP_Q10_6(x) ((int16_t) (64.0*(x) + (((x) >= 0.0) ? 0.5 : -0.5)))
83 #define FP_Q9_7(x) ((int16_t) (128.0*(x) + (((x) >= 0.0) ? 0.5 : -0.5)))
84 #define FP_Q8_8(x) ((int16_t) (256.0*(x) + (((x) >= 0.0) ? 0.5 : -0.5)))
85 #define FP_Q7_9(x) ((int16_t) (512.0*(x) + (((x) >= 0.0) ? 0.5 : -0.5)))
86 #define FP_Q6_10(x) ((int16_t) (1024.0*(x) + (((x) >= 0.0) ? 0.5 : -0.5)))
87 #define FP_Q5_11(x) ((int16_t) (2048.0*(x) + (((x) >= 0.0) ? 0.5 : -0.5)))
88 #define FP_Q4_12(x) ((int16_t) (4096.0*(x) + (((x) >= 0.0) ? 0.5 : -0.5)))
89 #define FP_Q3_13(x) ((int16_t) (8192.0*(x) + (((x) >= 0.0) ? 0.5 : -0.5)))
90 #define FP_Q2_14(x) ((int16_t) (16384.0*(x) + (((x) >= 0.0) ? 0.5 : -0.5)))
91 #define FP_Q1_15(x) ((int16_t) (32768.0*(x) + (((x) >= 0.0) ? 0.5 : -0.5)))
92 
93 /* Fixed point constant macros for 32 bit values */
94 #define FP_Q32_0(x) ((int32_t) (1.0*(x) + (((x) >= 0.0) ? 0.5 : -0.5)))
95 #define FP_Q31_1(x) ((int32_t) (2.0*(x) + (((x) >= 0.0) ? 0.5 : -0.5)))
96 #define FP_Q30_2(x) ((int32_t) (4.0*(x) + (((x) >= 0.0) ? 0.5 : -0.5)))
97 #define FP_Q29_3(x) ((int32_t) (8.0*(x) + (((x) >= 0.0) ? 0.5 : -0.5)))
98 #define FP_Q28_4(x) ((int32_t) (16.0*(x) + (((x) >= 0.0) ? 0.5 : -0.5)))
99 #define FP_Q27_5(x) ((int32_t) (32.0*(x) + (((x) >= 0.0) ? 0.5 : -0.5)))
100 #define FP_Q26_6(x) ((int32_t) (64.0*(x) + (((x) >= 0.0) ? 0.5 : -0.5)))
101 #define FP_Q25_7(x) ((int32_t) (128.0*(x) + (((x) >= 0.0) ? 0.5 : -0.5)))
102 #define FP_Q24_8(x) ((int32_t) (256.0*(x) + (((x) >= 0.0) ? 0.5 : -0.5)))
103 #define FP_Q23_9(x) ((int32_t) (512.0*(x) + (((x) >= 0.0) ? 0.5 : -0.5)))
104 #define FP_Q22_10(x) ((int32_t) (1024.0*(x) + (((x) >= 0.0) ? 0.5 : -0.5)))
105 #define FP_Q21_11(x) ((int32_t) (2048.0*(x) + (((x) >= 0.0) ? 0.5 : -0.5)))
106 #define FP_Q20_12(x) ((int32_t) (4096.0*(x) + (((x) >= 0.0) ? 0.5 : -0.5)))
107 #define FP_Q19_13(x) ((int32_t) (8192.0*(x) + (((x) >= 0.0) ? 0.5 : -0.5)))
108 #define FP_Q18_14(x) ((int32_t) (16384.0*(x) + (((x) >= 0.0) ? 0.5 : -0.5)))
109 #define FP_Q17_15(x) ((int32_t) (32768.0*(x) + (((x) >= 0.0) ? 0.5 : -0.5)))
110 #define FP_Q16_16(x) ((int32_t) (65536.0*1.0*(x) + (((x) >= 0.0) ? 0.5 : -0.5)))
111 #define FP_Q15_17(x) ((int32_t) (65536.0*2.0*(x) + (((x) >= 0.0) ? 0.5 : -0.5)))
112 #define FP_Q14_18(x) ((int32_t) (65536.0*4.0*(x) + (((x) >= 0.0) ? 0.5 : -0.5)))
113 #define FP_Q13_19(x) ((int32_t) (65536.0*8.0*(x) + (((x) >= 0.0) ? 0.5 : -0.5)))
114 #define FP_Q12_20(x) ((int32_t) (65536.0*16.0*(x) + (((x) >= 0.0) ? 0.5 : -0.5)))
115 #define FP_Q11_21(x) ((int32_t) (65536.0*32.0*(x) + (((x) >= 0.0) ? 0.5 : -0.5)))
116 #define FP_Q10_22(x) ((int32_t) (65536.0*64.0*(x) + (((x) >= 0.0) ? 0.5 : -0.5)))
117 #define FP_Q9_23(x) ((int32_t) (65536.0*128.0*(x) + (((x) >= 0.0) ? 0.5 : -0.5)))
118 #define FP_Q8_24(x) ((int32_t) (65536.0*256.0*(x) + (((x) >= 0.0) ? 0.5 : -0.5)))
119 #define FP_Q7_25(x) ((int32_t) (65536.0*512.0*(x) + (((x) >= 0.0) ? 0.5 : -0.5)))
120 #define FP_Q6_26(x) ((int32_t) (65536.0*1024.0*(x) + (((x) >= 0.0) ? 0.5 : -0.5)))
121 #define FP_Q5_27(x) ((int32_t) (65536.0*2048.0*(x) + (((x) >= 0.0) ? 0.5 : -0.5)))
122 #define FP_Q4_28(x) ((int32_t) (65536.0*4096.0*(x) + (((x) >= 0.0) ? 0.5 : -0.5)))
123 #define FP_Q3_29(x) ((int32_t) (65536.0*8192.0*(x) + (((x) >= 0.0) ? 0.5 : -0.5)))
124 #define FP_Q2_30(x) ((int32_t) (65536.0*16384.0*(x) + (((x) >= 0.0) ? 0.5 : -0.5)))
125 #define FP_Q1_31(x) ((int32_t) (65536.0*32768.0*(x) + (((x) >= 0.0) ? 0.5 : -0.5)))
126 
127 /* This is based on A-law, but u-law is only 0.03dB different */
128 #define DBM0_MAX_POWER (3.14f + 3.02f)
129 #define DBM0_MAX_SINE_POWER (3.14f)
130 /* This is based on the ITU definition of dbOv in G.100.1 */
131 #define DBOV_MAX_POWER (0.0f)
132 #define DBOV_MAX_SINE_POWER (-3.02f)
133 
134 #if defined(SPANDSP_USE_FIXED_POINT)
135 #define db_to_power_ratio(val) powf(10.0f, (val)/10.0f)
136 #define db_to_amplitude_ratio(val) powf(10.0f, (val)/20.0f)
137 #define power_ratio_to_db(val) (10.0f*log10f(val))
138 #define amplitude_ratio_to_db(val) (20.0f*log10f(val))
139 #else
140 #define db_to_power_ratio(val) powf(10.0f, (val)/10.0f)
141 #define db_to_amplitude_ratio(val) powf(10.0f, (val)/20.0f)
142 #define power_ratio_to_db(val) (10.0f*log10f(val))
143 #define amplitude_ratio_to_db(val) (20.0f*log10f(val))
144 #endif
145 
146 /* Convert a power level in dBm0 or dBov to the equivalent energy to expect from an integration over len samples. So, this is len
147  times the actual power. */
148 #if defined(SPANDSP_USE_FIXED_POINT)
149 #define energy_threshold_dbm0(len,thresh) (int) (((len)*256.0f*256.0f/2.0f)*powf(10.0f, ((thresh) - DBM0_MAX_SINE_POWER)/10.0f))
150 #define energy_threshold_dbmov(len,thresh) (int) (((len)*256.0f*256.0f/2.0f)*powf(10.0f, ((thresh) - DBMOV_MAX_SINE_POWER)/10.0f))
151 #else
152 #define energy_threshold_dbm0(len,thresh) (float) (((len)*32768.0f*32768.0f/2.0f)*powf(10.0f, ((thresh) - DBM0_MAX_SINE_POWER)/10.0f))
153 #define energy_threshold_dbmov(len,thresh) (float) (((len)*32768.0f*32768.0f/2.0f)*powf(10.0f, ((thresh) - DBMOV_MAX_SINE_POWER)/10.0f))
154 #endif
155 
156 #if defined(__cplusplus)
157 /* C++ doesn't seem to have sane rounding functions/macros yet */
158 #if !defined(WIN32)
159 #define lrint(x) ((long int) (x))
160 #define lrintf(x) ((long int) (x))
161 #endif
162 #endif
163 
164 #endif
165 /*- End of file ------------------------------------------------------------*/
t81_t82_arith_decode_state_t s
Definition: private/t85.h:202