Add missing clean-up code.
[calfbox.git] / phaser.c
blob76ae4085b2b122606b6561338efc08b457c1177b
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 NO_STAGES 12
34 #define MODULE_PARAMS phaser_params
36 struct phaser_params
38 float center;
39 float mdepth;
40 float fb_amt;
41 float lfo_freq;
42 float sphase;
43 float wet_dry;
44 int stages;
47 struct phaser_module
49 struct cbox_module module;
51 struct cbox_onepolef_state state[NO_STAGES][2];
52 struct cbox_onepolef_coeffs coeffs[2];
53 float fb[2];
54 float tpdsr;
55 struct phaser_params *params;
57 float phase;
60 gboolean phaser_process_cmd(struct cbox_command_target *ct, struct cbox_command_target *fb, struct cbox_osc_command *cmd, GError **error)
62 struct phaser_module *m = (struct phaser_module *)ct->user_data;
64 EFFECT_PARAM("/center_freq", "f", center, double, , 10, 20000) else
65 EFFECT_PARAM("/mod_depth", "f", mdepth, double, , 0, 10000) else
66 EFFECT_PARAM("/fb_amt", "f", fb_amt, double, , -1, 1) else
67 EFFECT_PARAM("/lfo_freq", "f", lfo_freq, double, , 0, 20) else
68 EFFECT_PARAM("/stereo_phase", "f", sphase, double, deg2rad, 0, 360) else
69 EFFECT_PARAM("/wet_dry", "f", wet_dry, double, , 0, 1) else
70 EFFECT_PARAM("/stages", "i", stages, int, , 1, 12) else
71 if (!strcmp(cmd->command, "/status") && !strcmp(cmd->arg_types, ""))
73 if (!cbox_check_fb_channel(fb, cmd->command, error))
74 return FALSE;
75 return cbox_execute_on(fb, NULL, "/center_freq", "f", error, m->params->center) &&
76 cbox_execute_on(fb, NULL, "/mod_depth", "f", error, m->params->mdepth) &&
77 cbox_execute_on(fb, NULL, "/fb_amt", "f", error, m->params->fb_amt) &&
78 cbox_execute_on(fb, NULL, "/lfo_freq", "f", error, m->params->lfo_freq) &&
79 cbox_execute_on(fb, NULL, "/stereo_phase", "f", error, rad2deg(m->params->sphase)) &&
80 cbox_execute_on(fb, NULL, "/wet_dry", "f", error, m->params->wet_dry) &&
81 cbox_execute_on(fb, NULL, "/stages", "i", error, m->params->stages) &&
82 CBOX_OBJECT_DEFAULT_STATUS(&m->module, fb, error);
84 else
85 return cbox_object_default_process_cmd(ct, fb, cmd, error);
86 return TRUE;
89 void phaser_process_event(struct cbox_module *module, const uint8_t *data, uint32_t len)
91 // struct phaser_module *m = (struct phaser_module *)module;
94 static inline float clip_w(float w)
96 if (w > 0.9 * M_PI)
97 return 0.9 * M_PI;
98 return w;
101 void phaser_process_block(struct cbox_module *module, cbox_sample_t **inputs, cbox_sample_t **outputs)
103 struct phaser_module *m = (struct phaser_module *)module;
104 struct phaser_params *p = m->params;
105 int s, c, i;
106 int stages = p->stages;
107 float fb_amt = p->fb_amt;
108 if (stages < 0 || stages > NO_STAGES)
109 stages = 0;
111 if (p->mdepth)
113 cbox_onepolef_set_allpass(&m->coeffs[0], clip_w(m->tpdsr * p->center * cent2factor(p->mdepth * sin(m->phase))));
114 cbox_onepolef_set_allpass(&m->coeffs[1], clip_w(m->tpdsr * p->center * cent2factor(p->mdepth * sin(m->phase + p->sphase))));
116 else
118 cbox_onepolef_set_allpass(&m->coeffs[0], m->tpdsr * p->center);
119 cbox_onepolef_set_allpass(&m->coeffs[1], m->tpdsr * p->center);
121 m->phase += p->lfo_freq * CBOX_BLOCK_SIZE * m->tpdsr;
123 for (c = 0; c < 2; c++)
125 for (i = 0; i < CBOX_BLOCK_SIZE; i++)
127 float dry = inputs[c][i];
128 float wet = dry - m->fb[c] * fb_amt;
129 for (s = 0; s < stages; s++)
130 wet = cbox_onepolef_process_sample(&m->state[s][c], &m->coeffs[c], wet);
131 m->fb[c] = wet;
132 outputs[c][i] = dry + (wet - dry) * p->wet_dry;
137 MODULE_SIMPLE_DESTROY_FUNCTION(phaser)
139 MODULE_CREATE_FUNCTION(phaser)
141 int b, c;
143 static int inited = 0;
144 if (!inited)
146 inited = 1;
149 struct phaser_module *m = malloc(sizeof(struct phaser_module));
150 CALL_MODULE_INIT(m, 2, 2, phaser);
151 m->module.process_event = phaser_process_event;
152 m->module.process_block = phaser_process_block;
153 m->tpdsr = 2.0 * M_PI / m->module.srate;
154 m->phase = 0;
155 struct phaser_params *p = malloc(sizeof(struct phaser_params));
156 m->params = p;
157 p->sphase = deg2rad(cbox_config_get_float(cfg_section, "stereo_phase", 90.f));
158 p->lfo_freq = cbox_config_get_float(cfg_section, "lfo_freq", 1.f);
159 p->center = cbox_config_get_float(cfg_section, "center_freq", 1500.f);
160 p->mdepth = cbox_config_get_float(cfg_section, "mod_depth", 1200.f);
161 p->fb_amt = cbox_config_get_float(cfg_section, "feedback", 0.f);
162 p->wet_dry = cbox_config_get_float(cfg_section, "wet_dry", 0.5f);
163 p->stages = cbox_config_get_int(cfg_section, "stages", NO_STAGES);
165 for (b = 0; b < NO_STAGES; b++)
166 for (c = 0; c < 2; c++)
167 cbox_onepolef_reset(&m->state[b][c]);
169 return &m->module;
173 struct cbox_module_keyrange_metadata phaser_keyranges[] = {
176 struct cbox_module_livecontroller_metadata phaser_controllers[] = {
179 DEFINE_MODULE(phaser, 2, 2)