In case of running out of prefetch pipes, truncate the sample to the preloaded part.
[calfbox.git] / chorus.c
blob06945da74742eb8a9acc808246b05a952c892da5
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 MAX_CHORUS_LENGTH 4096
33 #define MODULE_PARAMS chorus_params
35 static float sine_table[2049];
37 struct chorus_params
39 float lfo_freq;
40 float min_delay;
41 float mod_depth;
42 float wet_dry;
43 float sphase;
46 struct chorus_module
48 struct cbox_module module;
50 float storage[MAX_CHORUS_LENGTH][2];
51 struct chorus_params *params;
52 int pos;
53 float tp32dsr;
54 uint32_t phase;
57 MODULE_PROCESSCMD_FUNCTION(chorus)
59 struct chorus_module *m = (struct chorus_module *)ct->user_data;
61 EFFECT_PARAM("/min_delay", "f", min_delay, double, , 1, 20) else
62 EFFECT_PARAM("/mod_depth", "f", mod_depth, double, , 1, 20) else
63 EFFECT_PARAM("/lfo_freq", "f", lfo_freq, double, , 0, 20) else
64 EFFECT_PARAM("/stereo_phase", "f", sphase, double, , 0, 360) else
65 EFFECT_PARAM("/wet_dry", "f", wet_dry, double, , 0, 1) else
66 if (!strcmp(cmd->command, "/status") && !strcmp(cmd->arg_types, ""))
68 if (!cbox_check_fb_channel(fb, cmd->command, error))
69 return FALSE;
70 return cbox_execute_on(fb, NULL, "/min_delay", "f", error, m->params->min_delay) &&
71 cbox_execute_on(fb, NULL, "/mod_depth", "f", error, m->params->mod_depth) &&
72 cbox_execute_on(fb, NULL, "/lfo_freq", "f", error, m->params->lfo_freq) &&
73 cbox_execute_on(fb, NULL, "/stereo_phase", "f", error, m->params->sphase) &&
74 cbox_execute_on(fb, NULL, "/wet_dry", "f", error, m->params->wet_dry) &&
75 CBOX_OBJECT_DEFAULT_STATUS(&m->module, fb, error);
77 else
78 return cbox_object_default_process_cmd(ct, fb, cmd, error);
79 return TRUE;
82 void chorus_process_event(struct cbox_module *module, const uint8_t *data, uint32_t len)
84 // struct chorus_module *m = (struct chorus_module *)module;
87 void chorus_process_block(struct cbox_module *module, cbox_sample_t **inputs, cbox_sample_t **outputs)
89 struct chorus_module *m = (struct chorus_module *)module;
90 struct chorus_params *p = m->params;
92 float min_delay = p->min_delay;
93 float mod_depth = p->mod_depth;
94 float wet_dry = p->wet_dry;
95 int i, c;
96 int mask = MAX_CHORUS_LENGTH - 1;
97 uint32_t sphase = (uint32_t)(p->sphase * 65536.0 * 65536.0 / 360);
98 uint32_t dphase = (uint32_t)(p->lfo_freq * m->tp32dsr);
99 const int fracbits = 32 - 11;
100 const int fracscale = 1 << fracbits;
102 for (c = 0; c < 2; c++)
104 int pos = m->pos;
105 uint32_t phase = m->phase + c * sphase;
106 for (i = 0; i < CBOX_BLOCK_SIZE; i++)
108 float dry = inputs[c][i];
109 float v0 = sine_table[phase >> fracbits];
110 float v1 = sine_table[1 + (phase >> fracbits)];
111 float lfo = v0 + (v1 - v0) * ((phase & (fracscale - 1)) * (1.0 / fracscale));
113 m->storage[pos & mask][c] = dry;
115 float dva = min_delay + mod_depth * lfo;
116 int dv = (int)dva;
117 float frac = dva - dv;
118 float smp0 = m->storage[(pos - dv) & mask][c];
119 float smp1 = m->storage[(pos - dv - 1) & mask][c];
121 float smp = smp0 + (smp1 - smp0) * frac;
123 outputs[c][i] = sanef(dry + (smp - dry) * wet_dry);
125 pos++;
126 phase += dphase;
130 m->phase += CBOX_BLOCK_SIZE * dphase;
131 m->pos += CBOX_BLOCK_SIZE;
134 MODULE_SIMPLE_DESTROY_FUNCTION(chorus)
136 MODULE_CREATE_FUNCTION(chorus)
138 static int inited = 0;
139 int i;
140 if (!inited)
142 inited = 1;
143 for (i = 0; i < 2049; i++)
144 sine_table[i] = 1 + sin(i * M_PI / 1024);
147 struct chorus_module *m = malloc(sizeof(struct chorus_module));
148 CALL_MODULE_INIT(m, 2, 2, chorus);
149 m->module.process_event = chorus_process_event;
150 m->module.process_block = chorus_process_block;
151 m->pos = 0;
152 m->phase = 0;
153 m->tp32dsr = 65536.0 * 65536.0 * m->module.srate_inv;
154 struct chorus_params *p = malloc(sizeof(struct chorus_params));
155 m->params = p;
156 p->sphase = cbox_config_get_float(cfg_section, "stereo_phase", 90.f);
157 p->lfo_freq = cbox_config_get_float(cfg_section, "lfo_freq", 1.f);
158 p->min_delay = cbox_config_get_float(cfg_section, "min_delay", 20.f);
159 p->mod_depth = cbox_config_get_float(cfg_section, "mod_depth", 15.f);
160 p->wet_dry = cbox_config_get_float(cfg_section, "wet_dry", 0.5f);
161 for (i = 0; i < MAX_CHORUS_LENGTH; i++)
162 m->storage[i][0] = m->storage[i][1] = 0.f;
164 return &m->module;
168 struct cbox_module_keyrange_metadata chorus_keyranges[] = {
171 struct cbox_module_livecontroller_metadata chorus_controllers[] = {
174 DEFINE_MODULE(chorus, 2, 2)