Attempt at implementing per-channel output setting in the sampler.
[calfbox.git] / compressor.c
blob34b79e5fb7cd52fd6d760361d77fa4737db702c5
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 compressor_params
34 struct compressor_params
36 float threshold;
37 float ratio;
38 float attack;
39 float release;
40 float makeup;
43 struct compressor_module
45 struct cbox_module module;
47 struct compressor_params *params, *old_params;
48 struct cbox_onepolef_coeffs attack_lp, release_lp, fast_attack_lp;
49 struct cbox_onepolef_state tracker;
50 struct cbox_onepolef_state tracker2;
53 MODULE_PROCESSCMD_FUNCTION(compressor)
55 struct compressor_module *m = (struct compressor_module *)ct->user_data;
57 EFFECT_PARAM("/makeup", "f", makeup, double, dB2gain_simple, -100, 100) else
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("/release", "f", release, double, , 1, 1000) else
62 if (!strcmp(cmd->command, "/status") && !strcmp(cmd->arg_types, ""))
64 if (!cbox_check_fb_channel(fb, cmd->command, error))
65 return FALSE;
66 return cbox_execute_on(fb, NULL, "/makeup", "f", error, gain2dB_simple(m->params->makeup))
67 && 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, "/release", "f", error, m->params->release)
71 && CBOX_OBJECT_DEFAULT_STATUS(&m->module, fb, error)
74 else
75 return cbox_object_default_process_cmd(ct, fb, cmd, error);
76 return TRUE;
79 void compressor_process_event(struct cbox_module *module, const uint8_t *data, uint32_t len)
81 // struct compressor_module *m = module->user_data;
84 void compressor_process_block(struct cbox_module *module, cbox_sample_t **inputs, cbox_sample_t **outputs)
86 struct compressor_module *m = module->user_data;
88 if (m->params != m->old_params)
90 float scale = M_PI * 1000 / m->module.srate;
91 cbox_onepolef_set_lowpass(&m->fast_attack_lp, 2 * scale / m->params->attack);
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 m->old_params = m->params;
97 float threshold = m->params->threshold, invratio = 1.0 / m->params->ratio;
98 for (int i = 0; i < CBOX_BLOCK_SIZE; i++)
100 float left = inputs[0][i], right = inputs[1][i];
101 float sig = 0.5 * (fabs(left) > fabs(right) ? fabs(left) : fabs(right));
103 int falling = sig < m->tracker.y1 && sig < m->tracker.x1;
104 int rising_fast = sig > 4 * m->tracker.y1 && sig > 4 * m->tracker.x1;
105 sig = cbox_onepolef_process_sample(&m->tracker, falling ? &m->release_lp : (rising_fast * m->tracker.y1 ? &m->fast_attack_lp : &m->attack_lp), sig);
106 sig = cbox_onepolef_process_sample(&m->tracker2, falling ? &m->release_lp : (rising_fast * m->tracker2.y1 ? &m->fast_attack_lp : &m->attack_lp), sig);
107 float gain = 1.0;
108 if (sig > threshold)
109 gain = threshold * powf(sig / threshold, invratio) / sig;
110 gain *= m->params->makeup;
112 outputs[0][i] = left * gain;
113 outputs[1][i] = right * gain;
117 MODULE_SIMPLE_DESTROY_FUNCTION(compressor)
119 MODULE_CREATE_FUNCTION(compressor)
121 static int inited = 0;
122 if (!inited)
124 inited = 1;
127 struct compressor_module *m = malloc(sizeof(struct compressor_module));
128 CALL_MODULE_INIT(m, 2, 2, compressor);
129 m->module.process_event = compressor_process_event;
130 m->module.process_block = compressor_process_block;
132 struct compressor_params *p = malloc(sizeof(struct compressor_params));
133 p->threshold = cbox_config_get_gain_db(cfg_section, "threshold", -12.0);
134 p->ratio = cbox_config_get_float(cfg_section, "ratio", 2.0);
135 p->attack = cbox_config_get_float(cfg_section, "attack", 5.0);
136 p->release = cbox_config_get_float(cfg_section, "release", 100.0);
137 p->makeup = cbox_config_get_gain_db(cfg_section, "makeup", 6.0);
138 m->params = p;
139 m->old_params = NULL;
141 cbox_onepolef_reset(&m->tracker);
142 cbox_onepolef_reset(&m->tracker2);
144 return &m->module;
148 struct cbox_module_keyrange_metadata compressor_keyranges[] = {
151 struct cbox_module_livecontroller_metadata compressor_controllers[] = {
154 DEFINE_MODULE(compressor, 2, 2)