xref: /btstack/3rd-party/lc3-google/src/common.h (revision 4a9eead824c50b40e12b6f72611a74a3f57a47f6)
1 /******************************************************************************
2  *
3  *  Copyright 2021 Google, Inc.
4  *
5  *  Licensed under the Apache License, Version 2.0 (the "License");
6  *  you may not use this file except in compliance with the License.
7  *  You may obtain a copy of the License at:
8  *
9  *  http://www.apache.org/licenses/LICENSE-2.0
10  *
11  *  Unless required by applicable law or agreed to in writing, software
12  *  distributed under the License is distributed on an "AS IS" BASIS,
13  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  *  See the License for the specific language governing permissions and
15  *  limitations under the License.
16  *
17  ******************************************************************************/
18 
19 /**
20  * LC3 - Common constants and types
21  */
22 
23 #ifndef __LC3_COMMON_H
24 #define __LC3_COMMON_H
25 
26 #include <lc3.h>
27 
28 #include <limits.h>
29 #include <string.h>
30 #include <math.h>
31 
32 
33 /**
34  * Macros
35  * MIN/MAX  Minimum and maximum between 2 values
36  * CLIP     Clip a value between low and high limits
37  * ABS      Return the absolute value
38  */
39 
40 #define LC3_MIN(a, b)  ( (a) < (b) ? (a) : (b) )
41 #define LC3_MAX(a, b)  ( (a) > (b) ? (a) : (b) )
42 #define LC3_CLIP(v, min, max)  LC3_MIN(LC3_MAX(v, min), max)
43 
44 #define LC3_ABS(n)  ( (n) < 0 ? -(n) : (n) )
45 
46 
47 /**
48  * Convert `dt` in us and `sr` in KHz
49  */
50 
51 #define LC3_DT_US(dt) \
52     ( (3 + (dt)) * 2500 )
53 
54 #define LC3_SRATE_KHZ(sr) \
55     ( (1 + (sr) + ((sr) == LC3_SRATE_48K)) * 8 )
56 
57 
58 /**
59  * Return number of samples, delayed samples and
60  * encoded spectrum coefficients within a frame
61  * For decoding, add number of samples of 18 ms history
62  */
63 
64 #define LC3_NS(dt, sr) \
65     ( 20 * (3 + (dt)) * (1 + (sr) + ((sr) == LC3_SRATE_48K)) )
66 
67 #define LC3_ND(dt, sr) \
68     ( (dt) == LC3_DT_7M5 ? 23 * LC3_NS(dt, sr) / 30 \
69                          :  5 * LC3_NS(dt, sr) /  8 )
70 #define LC3_NE(dt, sr) \
71     ( 20 * (3 + (dt)) * (1 + (sr)) )
72 
73 #define LC3_MAX_NE \
74     LC3_NE(LC3_DT_10M, LC3_SRATE_48K)
75 
76 #define LC3_NH(sr) \
77     (18 * LC3_SRATE_KHZ(sr))
78 
79 
80 /**
81  * Bandwidth, mapped to Nyquist frequency of samplerates
82  */
83 
84 enum lc3_bandwidth {
85     LC3_BANDWIDTH_NB = LC3_SRATE_8K,
86     LC3_BANDWIDTH_WB = LC3_SRATE_16K,
87     LC3_BANDWIDTH_SSWB = LC3_SRATE_24K,
88     LC3_BANDWIDTH_SWB = LC3_SRATE_32K,
89     LC3_BANDWIDTH_FB = LC3_SRATE_48K,
90 
91     LC3_NUM_BANDWIDTH,
92 };
93 
94 
95 /**
96  * Complex floating point number
97  */
98 
99 struct lc3_complex
100 {
101     float re, im;
102 };
103 
104 
105 #endif /* __LC3_COMMON_H */
106