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/>.
20 #include "config-api.h"
23 #include "onepole-float.h"
32 #define MODULE_PARAMS limiter_params
43 struct cbox_module module
;
45 struct limiter_params
*params
, *old_params
;
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
))
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
);
69 return cbox_object_default_process_cmd(ct
, fb
, cmd
, error
);
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
)
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
);
110 m
->cur_gain
+= m
->atk_coeff
* (gain
- m
->cur_gain
);
112 gain
= exp(m
->cur_gain
);
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;
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
));
140 m
->old_params
= NULL
;
147 struct cbox_module_keyrange_metadata limiter_keyranges
[] = {
150 struct cbox_module_livecontroller_metadata limiter_controllers
[] = {
153 DEFINE_MODULE(limiter
, 0, 2)