In case of running out of prefetch pipes, truncate the sample to the preloaded part.
[calfbox.git] / gate.c
blobfac233ff2826aa070c7bb93fc093089a5d2143ff
1 /*
2 Calf Box, an open source musical instrument.
3 Copyright (C) 2010-2012 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 "config.h"
20 #include "config-api.h"
21 #include "dspmath.h"
22 #include "module.h"
23 #include "onepole-float.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 gate_params
34 struct gate_params
36 float threshold;
37 float ratio;
38 float attack;
39 float hold;
40 float release;
43 struct gate_module
45 struct cbox_module module;
47 struct gate_params *params, *old_params;
48 struct cbox_onepolef_coeffs attack_lp, release_lp, shifter_lp;
49 struct cbox_onepolef_state shifter1, shifter2;
50 struct cbox_onepolef_state tracker;
51 int hold_time, hold_threshold;
54 gboolean gate_process_cmd(struct cbox_command_target *ct, struct cbox_command_target *fb, struct cbox_osc_command *cmd, GError **error)
56 struct gate_module *m = (struct gate_module *)ct->user_data;
58 EFFECT_PARAM("/threshold", "f", threshold, double, dB2gain_simple, -100, 100) else
59 EFFECT_PARAM("/ratio", "f", ratio, double, , 1, 100) else
60 EFFECT_PARAM("/attack", "f", attack, double, , 1, 1000) else
61 EFFECT_PARAM("/hold", "f", hold, double, , 1, 1000) else
62 EFFECT_PARAM("/release", "f", release, double, , 1, 1000) else
63 if (!strcmp(cmd->command, "/status") && !strcmp(cmd->arg_types, ""))
65 if (!cbox_check_fb_channel(fb, cmd->command, error))
66 return FALSE;
67 return cbox_execute_on(fb, NULL, "/threshold", "f", error, gain2dB_simple(m->params->threshold))
68 && cbox_execute_on(fb, NULL, "/ratio", "f", error, m->params->ratio)
69 && cbox_execute_on(fb, NULL, "/attack", "f", error, m->params->attack)
70 && cbox_execute_on(fb, NULL, "/hold", "f", error, m->params->hold)
71 && cbox_execute_on(fb, NULL, "/release", "f", error, m->params->release)
72 && CBOX_OBJECT_DEFAULT_STATUS(&m->module, fb, error)
75 else
76 return cbox_object_default_process_cmd(ct, fb, cmd, error);
77 return TRUE;
80 void gate_process_event(struct cbox_module *module, const uint8_t *data, uint32_t len)
82 // struct gate_module *m = module->user_data;
85 void gate_process_block(struct cbox_module *module, cbox_sample_t **inputs, cbox_sample_t **outputs)
87 struct gate_module *m = module->user_data;
89 if (m->params != m->old_params)
91 float scale = M_PI * 1000 / m->module.srate;
92 cbox_onepolef_set_lowpass(&m->attack_lp, scale / m->params->attack);
93 cbox_onepolef_set_lowpass(&m->release_lp, scale / m->params->release);
94 cbox_onepolef_set_allpass(&m->shifter_lp, M_PI * 100 / m->module.srate);
95 m->hold_threshold = (int)(m->module.srate * m->params->hold * 0.001);
96 m->old_params = m->params;
99 float threshold = m->params->threshold;
100 float threshold2 = threshold * threshold * 1.73;
101 for (int i = 0; i < CBOX_BLOCK_SIZE; i++)
103 float left = inputs[0][i], right = inputs[1][i];
104 float sig = fabs(left) > fabs(right) ? fabs(left) : fabs(right);
106 // Primitive envelope detector - may not work so well with more interesting stereo signals
107 float shf1 = cbox_onepolef_process_sample(&m->shifter1, &m->shifter_lp, 0.5 * (left + right));
108 float shf2 = cbox_onepolef_process_sample(&m->shifter2, &m->shifter_lp, shf1);
109 sig = sig*sig + shf1*shf1 + shf2 * shf2;
111 // attack - hold - release logic based on signal envelope
112 int release = 1;
113 float gain = 1.0;
114 if (sig < threshold2)
116 // hold vs release
117 if (m->hold_time >= m->hold_threshold)
119 gain = powf(sig / threshold2, 0.5 * (m->params->ratio - 1));
120 // gain = powf(sqrt(sig) / threshold, (m->params->ratio - 1));
122 else
123 m->hold_time++;
125 else
127 // attack - going to 1 using attack rate
128 m->hold_time = 0;
129 gain = 1.0;
130 release = 0;
133 gain = cbox_onepolef_process_sample(&m->tracker, release ? &m->release_lp : &m->attack_lp, gain);
135 outputs[0][i] = left * gain;
136 outputs[1][i] = right * gain;
140 MODULE_SIMPLE_DESTROY_FUNCTION(gate)
142 MODULE_CREATE_FUNCTION(gate)
144 static int inited = 0;
145 if (!inited)
147 inited = 1;
150 struct gate_module *m = malloc(sizeof(struct gate_module));
151 CALL_MODULE_INIT(m, 2, 2, gate);
152 m->module.process_event = gate_process_event;
153 m->module.process_block = gate_process_block;
154 m->hold_time = 0;
155 m->hold_threshold = 0;
157 struct gate_params *p = malloc(sizeof(struct gate_params));
158 p->threshold = cbox_config_get_gain_db(cfg_section, "threshold", -28.0);
159 p->ratio = cbox_config_get_float(cfg_section, "ratio", 3.0);
160 p->attack = cbox_config_get_float(cfg_section, "attack", 3.0);
161 p->hold = cbox_config_get_float(cfg_section, "hold", 100.0);
162 p->release = cbox_config_get_float(cfg_section, "release", 100.0);
163 m->params = p;
164 m->old_params = NULL;
166 cbox_onepolef_reset(&m->tracker);
167 cbox_onepolef_reset(&m->shifter1);
168 cbox_onepolef_reset(&m->shifter2);
170 return &m->module;
174 struct cbox_module_keyrange_metadata gate_keyranges[] = {
177 struct cbox_module_livecontroller_metadata gate_controllers[] = {
180 DEFINE_MODULE(gate, 2, 2)