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"
21 #include "config-api.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
];
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
))
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
))
81 if (!cbox_execute_on(fb
, NULL
, "/center", "if", error
, i
, m
->params
->bands
[i
].center
))
83 if (!cbox_execute_on(fb
, NULL
, "/q", "if", error
, i
, m
->params
->bands
[i
].q
))
85 if (!cbox_execute_on(fb
, NULL
, "/gain", "if", error
, i
, gain2dB_simple(m
->params
->bands
[i
].gain
)))
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
);
92 return cbox_object_default_process_cmd(ct
, fb
, cmd
, error
);
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
)
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
)
117 cbox_biquadf_process_to(&m
->state
[i
][c
], &m
->coeffs
[i
], inputs
[c
], outputs
[c
]);
122 cbox_biquadf_process(&m
->state
[i
][c
], &m
->coeffs
[i
], outputs
[c
]);
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
);
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
);
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;
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
));
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
);
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)