In case of running out of prefetch pipes, truncate the sample to the preloaded part.
[calfbox.git] / eq.c
blobf53c7ae1b0cc788e205dca8e3955a321576d7647
1 /*
2 Calf Box, an open source musical instrument.
3 Copyright (C) 2010-2011 Krzysztof Foltman
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
19 #include "biquad-float.h"
20 #include "config.h"
21 #include "config-api.h"
22 #include "dspmath.h"
23 #include "eq.h"
24 #include "module.h"
25 #include <glib.h>
26 #include <malloc.h>
27 #include <math.h>
28 #include <memory.h>
29 #include <sndfile.h>
30 #include <stdio.h>
31 #include <stdlib.h>
33 #define MODULE_PARAMS parametric_eq_params
34 #define MAX_EQ_BANDS 4
36 struct parametric_eq_params
38 struct eq_band bands[MAX_EQ_BANDS];
41 struct parametric_eq_module
43 struct cbox_module module;
45 struct parametric_eq_params *params, *old_params;
47 struct cbox_biquadf_state state[MAX_EQ_BANDS][2];
48 struct cbox_biquadf_coeffs coeffs[MAX_EQ_BANDS];
51 static void redo_filters(struct parametric_eq_module *m)
53 for (int i = 0; i < MAX_EQ_BANDS; i++)
55 struct eq_band *band = &m->params->bands[i];
56 if (band->active)
58 cbox_biquadf_set_peakeq_rbj(&m->coeffs[i], band->center, band->q, band->gain, m->module.srate);
61 m->old_params = m->params;
64 gboolean parametric_eq_process_cmd(struct cbox_command_target *ct, struct cbox_command_target *fb, struct cbox_osc_command *cmd, GError **error)
66 struct parametric_eq_module *m = (struct parametric_eq_module *)ct->user_data;
68 EFFECT_PARAM_ARRAY("/active", "i", bands, active, int, , 0, 1) else
69 EFFECT_PARAM_ARRAY("/center", "f", bands, center, double, , 10, 20000) else
70 EFFECT_PARAM_ARRAY("/q", "f", bands, q, double, , 0.01, 100) else
71 EFFECT_PARAM_ARRAY("/gain", "f", bands, gain, double, dB2gain_simple, -100, 100) else
72 if (!strcmp(cmd->command, "/status") && !strcmp(cmd->arg_types, ""))
74 if (!cbox_check_fb_channel(fb, cmd->command, error))
75 return FALSE;
77 for (int i = 0; i < MAX_EQ_BANDS; i++)
79 if (!cbox_execute_on(fb, NULL, "/active", "ii", error, i, (int)m->params->bands[i].active))
80 return FALSE;
81 if (!cbox_execute_on(fb, NULL, "/center", "if", error, i, m->params->bands[i].center))
82 return FALSE;
83 if (!cbox_execute_on(fb, NULL, "/q", "if", error, i, m->params->bands[i].q))
84 return FALSE;
85 if (!cbox_execute_on(fb, NULL, "/gain", "if", error, i, gain2dB_simple(m->params->bands[i].gain)))
86 return FALSE;
88 // return cbox_execute_on(fb, NULL, "/wet_dry", "f", error, m->params->wet_dry);
89 return CBOX_OBJECT_DEFAULT_STATUS(&m->module, fb, error);
91 else
92 return cbox_object_default_process_cmd(ct, fb, cmd, error);
93 return TRUE;
96 void parametric_eq_process_event(struct cbox_module *module, const uint8_t *data, uint32_t len)
98 // struct parametric_eq_module *m = (struct parametric_eq_module *)module;
101 void parametric_eq_process_block(struct cbox_module *module, cbox_sample_t **inputs, cbox_sample_t **outputs)
103 struct parametric_eq_module *m = (struct parametric_eq_module *)module;
105 if (m->params != m->old_params)
106 redo_filters(m);
108 for (int c = 0; c < 2; c++)
110 gboolean first = TRUE;
111 for (int i = 0; i < MAX_EQ_BANDS; i++)
113 if (!m->params->bands[i].active)
114 continue;
115 if (first)
117 cbox_biquadf_process_to(&m->state[i][c], &m->coeffs[i], inputs[c], outputs[c]);
118 first = FALSE;
120 else
122 cbox_biquadf_process(&m->state[i][c], &m->coeffs[i], outputs[c]);
125 if (first)
126 memcpy(outputs[c], inputs[c], sizeof(float) * CBOX_BLOCK_SIZE);
130 float cbox_eq_get_band_param(const char *cfg_section, int band, const char *param, float defvalue)
132 gchar *s = g_strdup_printf("band%d_%s", band + 1, param);
133 float v = cbox_config_get_float(cfg_section, s, defvalue);
134 g_free(s);
136 return v;
139 float cbox_eq_get_band_param_db(const char *cfg_section, int band, const char *param, float defvalue)
141 gchar *s = g_strdup_printf("band%d_%s", band + 1, param);
142 float v = cbox_config_get_gain_db(cfg_section, s, defvalue);
143 g_free(s);
145 return v;
148 void cbox_eq_reset_bands(struct cbox_biquadf_state state[1][2], int bands)
150 for (int b = 0; b < MAX_EQ_BANDS; b++)
151 for (int c = 0; c < 2; c++)
152 cbox_biquadf_reset(&state[b][c]);
155 MODULE_SIMPLE_DESTROY_FUNCTION(parametric_eq)
157 MODULE_CREATE_FUNCTION(parametric_eq)
159 static int inited = 0;
160 if (!inited)
162 inited = 1;
165 struct parametric_eq_module *m = malloc(sizeof(struct parametric_eq_module));
166 CALL_MODULE_INIT(m, 2, 2, parametric_eq);
167 m->module.process_event = parametric_eq_process_event;
168 m->module.process_block = parametric_eq_process_block;
169 struct parametric_eq_params *p = malloc(sizeof(struct parametric_eq_params));
170 m->params = p;
171 m->old_params = NULL;
173 for (int b = 0; b < MAX_EQ_BANDS; b++)
175 p->bands[b].active = cbox_eq_get_band_param(cfg_section, b, "active", 0) > 0;
176 p->bands[b].center = cbox_eq_get_band_param(cfg_section, b, "center", 50 * pow(4.0, b));
177 p->bands[b].q = cbox_eq_get_band_param(cfg_section, b, "q", 0.707);
178 p->bands[b].gain = cbox_eq_get_band_param_db(cfg_section, b, "gain", 0);
181 cbox_eq_reset_bands(m->state, MAX_EQ_BANDS);
183 return &m->module;
187 struct cbox_module_keyrange_metadata parametric_eq_keyranges[] = {
190 struct cbox_module_livecontroller_metadata parametric_eq_controllers[] = {
193 DEFINE_MODULE(parametric_eq, 2, 2)