-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy patheq.cpp
127 lines (97 loc) · 3.62 KB
/
eq.cpp
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
#include <cmath>
#include "eq.h"
#define CLIP(__x) (((__x) > 1.0f) ? 1.0f : ((__x) < -1.0f ? -1.0f : (__x)))
#define DB_TO_A(__db) (std::pow(10.0, ((__db) / 20.0)))
#define A_TO_DB(__a) (20.0 * std::log((__a)))
const char *g_eq_desc_temp = "cpu: %s\n"
"fft hw accel: %s\n"
"fft impl: %s\n";
#define DESC_BUF_LEN 64
vrok::EffectSSEQ::EffectSSEQ() : _sb_paramsroot(nullptr) {
ComponentManager *c = ComponentManager::GetSingleton();
c->RegisterComponent(this);
std::vector<char> bandname;
bandname.resize(16);
for (int i = 0; i < EQ_BAND_COUNT; i++) {
snprintf(bandname.data(), 16, "Band%02d", i);
c->RegisterProperty(this, bandname.data(), &_bands[i]);
}
memset(&_sb_state, 0, sizeof(SuperEqState));
c->RegisterProperty(this, "Preamp", &_preamp);
for (int i = 0; i < EQ_BAND_COUNT; i++) {
_bands[i].Set(0.0);
}
_preamp.Set(0.0);
_desc_buffer.resize(DESC_BUF_LEN);
_eq_amp = nullptr;
_shm = sharedmem_create("vrok.eq.band.values", EQ_BAND_COUNT * sizeof(float));
_eq_amp_shm = (float *)_shm->buffer;
}
bool vrok::EffectSSEQ::EffectRun(Buffer *out_buffer, Buffer **in_buffer_set, int buffer_count) {
std::lock_guard<std::mutex> lg(_eq_setting_guard);
BufferConfig *bc = in_buffer_set[0]->GetBufferConfig();
int len = bc->frames * bc->channels;
out_buffer->Reset(bc);
real_t *proc = in_buffer_set[0]->GetData();
real_t *proc_out = out_buffer->GetData();
memcpy(proc_out, proc, sizeof(real_t) * len);
equ_modifySamples_real<real_t>(&_sb_state, (char *)proc_out, bc->frames, bc->channels);
for (int i = 0; i < EQ_BAND_COUNT; i++) {
_eq_amp_shm[i] = _eq_amp[i];
}
return true;
}
void vrok::EffectSSEQ::PropertyChanged(PropertyBase *property) {
std::lock_guard<std::mutex> lg(_eq_setting_guard);
BufferConfig *bc = GetBufferConfig();
void *params = paramlist_alloc();
float sb_bands_copy[EQ_BAND_COUNT];
for (int i = 0; i < EQ_BAND_COUNT; i++) {
sb_bands_copy[i] = DB_TO_A(_bands[i].Get() + _preamp.Get());
}
DBG(0, "eq sr " << bc->samplerate)
equ_makeTable(&_sb_state, sb_bands_copy, params, bc->samplerate);
if (_sb_paramsroot)
paramlist_free(_sb_paramsroot);
_sb_paramsroot = params;
}
bool vrok::EffectSSEQ::BufferConfigChange(BufferConfig *config) {
std::lock_guard<std::mutex> lg(_eq_setting_guard);
DBG(0, "000000000oo");
//*config->Print();
void *params = paramlist_alloc();
float sb_bands_copy[EQ_BAND_COUNT];
for (int i = 0; i < EQ_BAND_COUNT; i++) {
sb_bands_copy[i] = DB_TO_A(_bands[i].Get() + _preamp.Get());
DBG(0, "band_" << i << " " << sb_bands_copy[i] << " " << _bands[i].Get() + _preamp.Get());
}
// equ_quit();
equ_clearbuf(&_sb_state);
equ_init(&_sb_state, 12, config->channels);
_eq_amp = equ_band_amplitudes(&_sb_state);
DBG(0, "eq sr " << config->samplerate)
equ_makeTable(&_sb_state, sb_bands_copy, params, config->samplerate);
if (_sb_paramsroot)
paramlist_free(_sb_paramsroot);
_sb_paramsroot = params;
return true;
}
vrok::EffectSSEQ::~EffectSSEQ() {
equ_quit(&_sb_state);
sharedmem_destroy(_shm);
}
const char *vrok::EffectSSEQ::Description() {
std::string cpu;
#if defined(__arm__)
cpu = "arm";
#elif defined(__aarch64__)
cpu = "aarch64";
#elif defined(__x86_64__)
cpu = "x86_64";
#else
cpu = "generic";
#endif
snprintf(_desc_buffer.data(), DESC_BUF_LEN, g_eq_desc_temp, cpu.c_str(), equ_fftAccel(&_sb_state),
equ_fftImpl(&_sb_state));
return _desc_buffer.data();
}