Oops, don't enable timestretching on all samples by default.
[calfbox.git] / limiter.c
blob401dfc50c0a96a874e84779ed1acbc076732b686
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 "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 limiter_params
34 struct limiter_params
36 float threshold;
37 float attack;
38 float release;
41 struct limiter_module
43 struct cbox_module module;
45 struct limiter_params *params, *old_params;
47 double cur_gain;
48 double atk_coeff, rel_coeff;
51 gboolean limiter_process_cmd(struct cbox_command_target *ct, struct cbox_command_target *fb, struct cbox_osc_command *cmd, GError **error)
53 struct limiter_module *m = (struct limiter_module *)ct->user_data;
55 EFFECT_PARAM("/threshold", "f", threshold, double, , -100, 12) else
56 EFFECT_PARAM("/attack", "f", attack, double, , 1, 1000) else
57 EFFECT_PARAM("/release", "f", release, double, , 1, 5000) else
58 if (!strcmp(cmd->command, "/status") && !strcmp(cmd->arg_types, ""))
60 if (!cbox_check_fb_channel(fb, cmd->command, error))
61 return FALSE;
62 return
63 cbox_execute_on(fb, NULL, "/threshold", "f", error, m->params->threshold) &&
64 cbox_execute_on(fb, NULL, "/attack", "f", error, m->params->attack) &&
65 cbox_execute_on(fb, NULL, "/release", "f", error, m->params->release) &&
66 CBOX_OBJECT_DEFAULT_STATUS(&m->module, fb, error);
68 else
69 return cbox_object_default_process_cmd(ct, fb, cmd, error);
70 return TRUE;
73 void limiter_process_event(struct cbox_module *module, const uint8_t *data, uint32_t len)
75 // struct limiter_module *m = module->user_data;
78 void limiter_process_block(struct cbox_module *module, cbox_sample_t **inputs, cbox_sample_t **outputs)
80 struct limiter_module *m = module->user_data;
81 struct limiter_params *mp = m->params;
83 if (m->params != m->old_params)
85 m->atk_coeff = 1 - exp(-1000.0 / (mp->attack * m->module.srate));
86 m->rel_coeff = 1 - exp(-1000.0 / (mp->release * m->module.srate));
87 // update calculated values
89 const double minval = pow(2.0, -110.0);
90 for (int i = 0; i < CBOX_BLOCK_SIZE; ++i)
92 float left = inputs[0][i], right = inputs[1][i];
94 float level = fabs(left);
95 if (fabs(right) > level)
96 level = fabs(right);
97 if (level < minval)
98 level = minval;
99 level = log(level);
101 float gain = 0.0;
103 if (level > mp->threshold * 0.11552)
104 gain = mp->threshold * 0.11552 - level;
106 // instantaneous attack + slow release
107 if (gain >= m->cur_gain)
108 m->cur_gain += m->rel_coeff * (gain - m->cur_gain);
109 else
110 m->cur_gain += m->atk_coeff * (gain - m->cur_gain);
112 gain = exp(m->cur_gain);
113 //if (gain < 1)
114 // printf("level = %f gain = %f\n", m->cur_level, gain);
116 outputs[0][i] = left * gain;
117 outputs[1][i] = right * gain;
121 MODULE_SIMPLE_DESTROY_FUNCTION(limiter)
123 MODULE_CREATE_FUNCTION(limiter)
125 static int inited = 0;
126 if (!inited)
128 inited = 1;
131 struct limiter_module *m = malloc(sizeof(struct limiter_module));
132 CALL_MODULE_INIT(m, 2, 2, limiter);
133 m->module.process_event = limiter_process_event;
134 m->module.process_block = limiter_process_block;
135 struct limiter_params *p = malloc(sizeof(struct limiter_params));
136 p->threshold = -1;
137 p->attack = 10.f;
138 p->release = 2000.f;
139 m->params = p;
140 m->old_params = NULL;
141 m->cur_gain = 0.f;
143 return &m->module;
147 struct cbox_module_keyrange_metadata limiter_keyranges[] = {
150 struct cbox_module_livecontroller_metadata limiter_controllers[] = {
153 DEFINE_MODULE(limiter, 0, 2)