File: db.h

package info (click to toggle)
swh-plugins 0.4.17-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 3,264 kB
  • sloc: ansic: 23,551; xml: 12,633; perl: 1,114; sh: 964; makefile: 218
file content (86 lines) | stat: -rw-r--r-- 2,250 bytes parent folder | download | duplicates (13)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#ifndef _DB_H
#define _DB_H

#include "../ladspa-util.h"

void db_init();
static inline float f_lin2db_cube(float lin);
static inline float f_db2lin_cube(float db);
static inline float f_lin2db_lerp(float lin);
static inline float f_db2lin_lerp(float db);

extern float db_data[];
extern float lin_data[];

#define DB_TABLE_SIZE 1024
#define DB_MIN -60.0f
#define DB_MAX 24.0f
#define LIN_TABLE_SIZE 1024
#define LIN_MIN 0.0000000002f
#define LIN_MAX 9.0f

#ifdef DB_DEFAULT_CUBE
#define db2lin(a) f_db2lin_cube(a)
#define lin2db(a) f_lin2db_cube(a)
#else
#define db2lin(a) f_db2lin_lerp(a)
#define lin2db(a) f_lin2db_lerp(a)
#endif

static inline float f_db2lin_cube(float db)
{
	float scale = (db - DB_MIN) * (float)LIN_TABLE_SIZE / (DB_MAX - DB_MIN);
	int base = f_round(scale - 0.5f);
	float ofs = scale - base;

	if (base < 1) {
		return 0.0f;
	} else if (base > LIN_TABLE_SIZE - 3) {
		return lin_data[LIN_TABLE_SIZE - 2];
	}
	return cube_interp(ofs, lin_data[base-1], lin_data[base], lin_data[base+1], lin_data[base+2]);
}

static inline float f_db2lin_lerp(float db)
{
	float scale = (db - DB_MIN) * (float)LIN_TABLE_SIZE / (DB_MAX - DB_MIN);
	int base = f_round(scale - 0.5f);
	float ofs = scale - base;

	if (base < 1) {
		return 0.0f;
	} else if (base > LIN_TABLE_SIZE - 3) {
		return lin_data[LIN_TABLE_SIZE - 2];
	}
	return (1.0f - ofs) * lin_data[base] + ofs * lin_data[base+1];
}

static inline float f_lin2db_cube(float lin)
{
	float scale = (lin - LIN_MIN) * (float)DB_TABLE_SIZE / (LIN_MAX - LIN_MIN);
	int base = f_round(scale - 0.5f);
	float ofs = scale - base;

	if (base < 2) {
		return db_data[2] * scale * 0.5f - 23 * (2.0f - scale);
	} else if (base > DB_TABLE_SIZE - 3) {
		return db_data[DB_TABLE_SIZE - 2];
	}
	return cube_interp(ofs, db_data[base-1], db_data[base], db_data[base+1], db_data[base+2]);
}

static inline float f_lin2db_lerp(float lin)
{
	float scale = (lin - LIN_MIN) * (float)DB_TABLE_SIZE / (LIN_MAX - LIN_MIN);
	int base = f_round(scale - 0.5f);
	float ofs = scale - base;

	if (base < 2) {
		return db_data[2] * scale * 0.5f - 23.0f * (2.0f - scale);
	} else if (base > DB_TABLE_SIZE - 2) {
		return db_data[DB_TABLE_SIZE - 1];
	}
	return (1.0f - ofs) * db_data[base] + ofs * db_data[base+1];
}

#endif