Attempt to fix the clip offset bug.
[calfbox.git] / fuzz.c
blob677d3d079e224102e0f125d65b0f6424407e02d1
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 "module.h"
24 #include <glib.h>
25 #include <malloc.h>
26 #include <math.h>
27 #include <memory.h>
28 #include <sndfile.h>
29 #include <stdio.h>
30 #include <stdlib.h>
32 #define MODULE_PARAMS fuzz_params
34 struct fuzz_params
36 float drive;
37 float wet_dry;
38 float rectify;
39 float band;
40 float bandwidth;
41 float band2;
42 float bandwidth2;
45 struct fuzz_module
47 struct cbox_module module;
49 struct fuzz_params *params, *old_params;
51 struct cbox_biquadf_coeffs split_coeffs;
52 struct cbox_biquadf_coeffs post_coeffs;
53 struct cbox_biquadf_state split_state[2];
54 struct cbox_biquadf_state post_state[2];
57 gboolean fuzz_process_cmd(struct cbox_command_target *ct, struct cbox_command_target *fb, struct cbox_osc_command *cmd, GError **error)
59 struct fuzz_module *m = (struct fuzz_module *)ct->user_data;
61 EFFECT_PARAM("/drive", "f", drive, double, dB2gain_simple, -36, 36) else
62 EFFECT_PARAM("/wet_dry", "f", wet_dry, double, , 0, 1) else
63 EFFECT_PARAM("/rectify", "f", rectify, double, , 0, 1) else
64 EFFECT_PARAM("/band", "f", band, double, , 100, 5000) else
65 EFFECT_PARAM("/bandwidth", "f", bandwidth, double, , 0.25, 4) else
66 EFFECT_PARAM("/band2", "f", band2, double, , 100, 5000) else
67 EFFECT_PARAM("/bandwidth2", "f", bandwidth2, double, , 0.25, 4) else
68 if (!strcmp(cmd->command, "/status") && !strcmp(cmd->arg_types, ""))
70 if (!cbox_check_fb_channel(fb, cmd->command, error))
71 return FALSE;
72 return cbox_execute_on(fb, NULL, "/drive", "f", error, gain2dB_simple(m->params->drive))
73 && cbox_execute_on(fb, NULL, "/wet_dry", "f", error, m->params->wet_dry)
74 && cbox_execute_on(fb, NULL, "/rectify", "f", error, m->params->rectify)
75 && cbox_execute_on(fb, NULL, "/band", "f", error, m->params->band)
76 && cbox_execute_on(fb, NULL, "/bandwidth", "f", error, m->params->bandwidth)
77 && cbox_execute_on(fb, NULL, "/band2", "f", error, m->params->band2)
78 && cbox_execute_on(fb, NULL, "/bandwidth2", "f", error, m->params->bandwidth2)
79 && CBOX_OBJECT_DEFAULT_STATUS(&m->module, fb, error)
82 else
83 return cbox_object_default_process_cmd(ct, fb, cmd, error);
84 return TRUE;
87 void fuzz_process_event(struct cbox_module *module, const uint8_t *data, uint32_t len)
89 // struct fuzz_module *m = module->user_data;
92 void fuzz_process_block(struct cbox_module *module, cbox_sample_t **inputs, cbox_sample_t **outputs)
94 struct fuzz_module *m = module->user_data;
96 if (m->params != m->old_params)
98 // update calculated values
101 cbox_biquadf_set_bp_rbj(&m->split_coeffs, m->params->band, 0.7 / m->params->bandwidth, m->module.srate);
102 cbox_biquadf_set_bp_rbj(&m->post_coeffs, m->params->band2, 0.7 / m->params->bandwidth2, m->module.srate);
104 float splitbuf[2][CBOX_BLOCK_SIZE];
105 float drive = m->params->drive;
106 float sdrive = pow(drive, -0.7);
107 for (int c = 0; c < 2; c++)
109 cbox_biquadf_process_to(&m->split_state[c], &m->split_coeffs, inputs[c], splitbuf[c]);
110 for (int i = 0; i < CBOX_BLOCK_SIZE; i++)
112 float in = inputs[c][i];
114 float val = splitbuf[c][i];
116 val *= drive;
118 val += m->params->rectify;
119 if (fabs(val) > 1.0)
120 val = (val > 0) ? 1 : -1;
121 else
122 val = val * (3 - val * val) * 0.5;
124 val *= sdrive;
126 val = cbox_biquadf_process_sample(&m->post_state[c], &m->post_coeffs, val);
128 outputs[c][i] = in + (val - in) * m->params->wet_dry;
133 MODULE_SIMPLE_DESTROY_FUNCTION(fuzz)
135 MODULE_CREATE_FUNCTION(fuzz)
137 static int inited = 0;
138 if (!inited)
140 inited = 1;
143 struct fuzz_module *m = malloc(sizeof(struct fuzz_module));
144 CALL_MODULE_INIT(m, 2, 2, fuzz);
145 m->module.process_event = fuzz_process_event;
146 m->module.process_block = fuzz_process_block;
147 struct fuzz_params *p = malloc(sizeof(struct fuzz_params));
148 p->drive = cbox_config_get_gain_db(cfg_section, "drive", 0.f);
149 p->wet_dry = cbox_config_get_float(cfg_section, "wet_dry", 0.5f);
150 p->rectify = cbox_config_get_float(cfg_section, "rectify", 0.5f);
151 p->band = cbox_config_get_float(cfg_section, "band", 1000.f);
152 p->bandwidth = cbox_config_get_float(cfg_section, "bandwidth", 1);
153 p->band2 = cbox_config_get_float(cfg_section, "band2", 2000.f);
154 p->bandwidth2 = cbox_config_get_float(cfg_section, "bandwidth2", 1);
155 m->params = p;
156 m->old_params = NULL;
157 cbox_biquadf_reset(&m->split_state[0]);
158 cbox_biquadf_reset(&m->split_state[1]);
159 cbox_biquadf_reset(&m->post_state[0]);
160 cbox_biquadf_reset(&m->post_state[1]);
162 return &m->module;
166 struct cbox_module_keyrange_metadata fuzz_keyranges[] = {
169 struct cbox_module_livecontroller_metadata fuzz_controllers[] = {
172 DEFINE_MODULE(fuzz, 2, 2)