Debian Codegraph @

plugins/LadspaEffect/swh/util/db.h

raw
1 #ifndef _DB_H
2 #define _DB_H
3
4 #include "../ladspa-util.h"
5
6 void db_init();
7 static inline float f_lin2db_cube(float lin);
8 static inline float f_db2lin_cube(float db);
9 static inline float f_lin2db_lerp(float lin);
10 static inline float f_db2lin_lerp(float db);
11
12 extern float db_data[];
13 extern float lin_data[];
14
15 #define DB_TABLE_SIZE 1024
16 #define DB_MIN -60.0f
17 #define DB_MAX 24.0f
18 #define LIN_TABLE_SIZE 1024
19 #define LIN_MIN 0.0000000002f
20 #define LIN_MAX 9.0f
21
22 #ifdef DB_DEFAULT_CUBE
23 #define db2lin(a) f_db2lin_cube(a)
24 #define lin2db(a) f_lin2db_cube(a)
25 #else
26 #define db2lin(a) f_db2lin_lerp(a)
27 #define lin2db(a) f_lin2db_lerp(a)
28 #endif
29
30 static inline float f_db2lin_cube(float db)
31 {
32 float scale = (db - DB_MIN) * (float)LIN_TABLE_SIZE / (DB_MAX - DB_MIN);
33 int base = f_round(scale - 0.5f);
34 float ofs = scale - base;
35
36 if (base < 1) {
37 return 0.0f;
38 } else if (base > LIN_TABLE_SIZE - 3) {
39 return lin_data[LIN_TABLE_SIZE - 2];
40 }
41 return cube_interp(ofs, lin_data[base-1], lin_data[base], lin_data[base+1], lin_data[base+2]);
42 }
43
44 static inline float f_db2lin_lerp(float db)
45 {
46 float scale = (db - DB_MIN) * (float)LIN_TABLE_SIZE / (DB_MAX - DB_MIN);
47 int base = f_round(scale - 0.5f);
48 float ofs = scale - base;
49
50 if (base < 1) {
51 return 0.0f;
52 } else if (base > LIN_TABLE_SIZE - 3) {
53 return lin_data[LIN_TABLE_SIZE - 2];
54 }
55 return (1.0f - ofs) * lin_data[base] + ofs * lin_data[base+1];
56 }
57
58 static inline float f_lin2db_cube(float lin)
59 {
60 float scale = (lin - LIN_MIN) * (float)DB_TABLE_SIZE / (LIN_MAX - LIN_MIN);
61 int base = f_round(scale - 0.5f);
62 float ofs = scale - base;
63
64 if (base < 2) {
65 return db_data[2] * scale * 0.5f - 23 * (2.0f - scale);
66 } else if (base > DB_TABLE_SIZE - 3) {
67 return db_data[DB_TABLE_SIZE - 2];
68 }
69 return cube_interp(ofs, db_data[base-1], db_data[base], db_data[base+1], db_data[base+2]);
70 }
71
72 static inline float f_lin2db_lerp(float lin)
73 {
74 float scale = (lin - LIN_MIN) * (float)DB_TABLE_SIZE / (LIN_MAX - LIN_MIN);
75 int base = f_round(scale - 0.5f);
76 float ofs = scale - base;
77
78 if (base < 2) {
79 return db_data[2] * scale * 0.5f - 23.0f * (2.0f - scale);
80 } else if (base > DB_TABLE_SIZE - 2) {
81 return db_data[DB_TABLE_SIZE - 1];
82 }
83 return (1.0f - ofs) * db_data[base] + ofs * db_data[base+1];
84 }
85
86 #endif