Merge pull request #1 from atsampson/master
[calfbox.git] / distortion.c
blob8251e894897d164eae3db6d2ed992131cd795422
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 <glib.h>
24 #include <malloc.h>
25 #include <math.h>
26 #include <memory.h>
27 #include <sndfile.h>
28 #include <stdio.h>
29 #include <stdlib.h>
31 #define MODULE_PARAMS distortion_params
33 struct distortion_params
35 float drive;
36 float shape;
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))
55 return FALSE;
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)
61 else
62 return cbox_object_default_process_cmd(ct, fb, cmd, error);
63 return TRUE;
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;
83 float a0 = 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];
95 val *= drive;
97 if (fabs(val) > 1.0)
98 val = (val > 0) ? 1 : -1;
99 else
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;
112 if (!inited)
114 inited = 1;
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);
124 m->params = p;
125 m->old_params = NULL;
127 return &m->module;
131 struct cbox_module_keyrange_metadata distortion_keyranges[] = {
134 struct cbox_module_livecontroller_metadata distortion_controllers[] = {
137 DEFINE_MODULE(distortion, 2, 2)