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"
31 #define MODULE_PARAMS distortion_params
33 struct distortion_params
39 struct distortion_module
41 struct cbox_module module
;
43 struct distortion_params
*params
, *old_params
;
46 gboolean
distortion_process_cmd(struct cbox_command_target
*ct
, struct cbox_command_target
*fb
, struct cbox_osc_command
*cmd
, GError
**error
)
48 struct distortion_module
*m
= (struct distortion_module
*)ct
->user_data
;
50 EFFECT_PARAM("/drive", "f", drive
, double, dB2gain_simple
, -36, 36) else
51 EFFECT_PARAM("/shape", "f", shape
, double, , -1, 2) else
52 if (!strcmp(cmd
->command
, "/status") && !strcmp(cmd
->arg_types
, ""))
54 if (!cbox_check_fb_channel(fb
, cmd
->command
, error
))
56 return cbox_execute_on(fb
, NULL
, "/drive", "f", error
, gain2dB_simple(m
->params
->drive
))
57 && cbox_execute_on(fb
, NULL
, "/shape", "f", error
, m
->params
->shape
)
58 && CBOX_OBJECT_DEFAULT_STATUS(&m
->module
, fb
, error
)
62 return cbox_object_default_process_cmd(ct
, fb
, cmd
, error
);
66 void distortion_process_event(struct cbox_module
*module
, const uint8_t *data
, uint32_t len
)
68 // struct distortion_module *m = module->user_data;
71 void distortion_process_block(struct cbox_module
*module
, cbox_sample_t
**inputs
, cbox_sample_t
**outputs
)
73 struct distortion_module
*m
= module
->user_data
;
75 if (m
->params
!= m
->old_params
)
77 // update calculated values
80 float drive
= m
->params
->drive
;
81 float shape
= m
->params
->shape
;
84 float a1
= -2 * shape
- 0.5;
85 float a2
= 1.5 + shape
;
87 float post
= pow(drive
, -0.7);
89 for (int i
= 0; i
< CBOX_BLOCK_SIZE
; i
++)
91 for (int c
= 0; c
< 2; c
++)
93 float val
= inputs
[c
][i
];
98 val
= (val
> 0) ? 1 : -1;
100 val
= a0
* val
* val
* val
* val
* val
+ a1
* val
* val
* val
+ a2
* val
;
102 outputs
[c
][i
] = val
* post
;
107 MODULE_SIMPLE_DESTROY_FUNCTION(distortion
)
109 MODULE_CREATE_FUNCTION(distortion
)
111 static int inited
= 0;
117 struct distortion_module
*m
= malloc(sizeof(struct distortion_module
));
118 CALL_MODULE_INIT(m
, 2, 2, distortion
);
119 m
->module
.process_event
= distortion_process_event
;
120 m
->module
.process_block
= distortion_process_block
;
121 struct distortion_params
*p
= malloc(sizeof(struct distortion_params
));
122 p
->drive
= cbox_config_get_gain_db(cfg_section
, "drive", 0.f
);
123 p
->shape
= cbox_config_get_gain_db(cfg_section
, "shape", 0.f
);
125 m
->old_params
= NULL
;
131 struct cbox_module_keyrange_metadata distortion_keyranges
[] = {
134 struct cbox_module_livecontroller_metadata distortion_controllers
[] = {
137 DEFINE_MODULE(distortion
, 2, 2)