Use linked list for storing sources in MIDI merger.
[calfbox.git] / delay.c
blobc48ad7f8eb21f7e72b168f6a9185dfb4cff05b3d
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_DELAY_LENGTH 65536
33 struct delay_params
35 float time;
36 float wet_dry, fb_amt;
39 struct delay_module
41 struct cbox_module module;
43 float storage[MAX_DELAY_LENGTH][2];
44 struct delay_params *params;
45 int pos;
48 #define MODULE_PARAMS delay_params
50 MODULE_PROCESSCMD_FUNCTION(delay)
52 struct delay_module *m = (struct delay_module *)ct->user_data;
54 EFFECT_PARAM("/time", "f", time, double, , 1, 1000) else
55 EFFECT_PARAM("/fb_amt", "f", fb_amt, double, , 0, 1) else
56 EFFECT_PARAM("/wet_dry", "f", wet_dry, double, , 0, 1) else
57 if (!strcmp(cmd->command, "/status") && !strcmp(cmd->arg_types, ""))
59 if (!cbox_check_fb_channel(fb, cmd->command, error))
60 return FALSE;
61 return cbox_execute_on(fb, NULL, "/time", "f", error, m->params->time)
62 && cbox_execute_on(fb, NULL, "/fb_amt", "f", error, m->params->fb_amt)
63 && cbox_execute_on(fb, NULL, "/wet_dry", "f", error, m->params->wet_dry)
64 && CBOX_OBJECT_DEFAULT_STATUS(&m->module, fb, error)
67 else
68 return cbox_object_default_process_cmd(ct, fb, cmd, error);
69 return TRUE;
72 void delay_process_event(struct cbox_module *module, const uint8_t *data, uint32_t len)
74 // struct delay_module *m = (struct delay_module *)module;
77 void delay_process_block(struct cbox_module *module, cbox_sample_t **inputs, cbox_sample_t **outputs)
79 struct delay_module *m = (struct delay_module *)module;
81 int pos = m->pos;
82 int dv = m->params->time * m->module.srate / 1000.0;
83 float dryamt = 1 - m->params->wet_dry;
84 float wetamt = m->params->wet_dry;
85 float fbamt = m->params->fb_amt;
87 for (int i = 0; i < CBOX_BLOCK_SIZE; i++)
89 float dry[2] = { inputs[0][i], inputs[1][i] };
90 float *delayed = &m->storage[pos & (MAX_DELAY_LENGTH - 1)][0];
92 float wet[2] = { dryamt * dry[0] + wetamt * delayed[0], dryamt * dry[1] + wetamt * delayed[1] };
93 float fb[2] = { dry[0] + fbamt * delayed[0], dry[1] + fbamt * delayed[1] };
94 outputs[0][i] = sanef(wet[0]);
95 outputs[1][i] = sanef(wet[1]);
97 float *wcell = &m->storage[(pos + dv) & (MAX_DELAY_LENGTH - 1)][0];
98 wcell[0] = sanef(fb[0]);
99 wcell[1] = sanef(fb[1]);
100 pos++;
102 m->pos = pos;
105 MODULE_SIMPLE_DESTROY_FUNCTION(delay)
107 MODULE_CREATE_FUNCTION(delay)
109 static int inited = 0;
110 int i;
111 if (!inited)
113 inited = 1;
116 struct delay_module *m = malloc(sizeof(struct delay_module));
117 CALL_MODULE_INIT(m, 2, 2, delay);
118 struct delay_params *p = malloc(sizeof(struct delay_params));
119 m->params = p;
120 m->module.process_event = delay_process_event;
121 m->module.process_block = delay_process_block;
122 m->pos = 0;
123 p->time = cbox_config_get_float(cfg_section, "delay", 250);
124 p->wet_dry = cbox_config_get_float(cfg_section, "wet_dry", 0.3);
125 p->fb_amt = cbox_config_get_gain_db(cfg_section, "feedback_gain", -12.f);
126 for (i = 0; i < MAX_DELAY_LENGTH; i++)
127 m->storage[i][0] = m->storage[i][1] = 0.f;
129 return &m->module;
132 struct cbox_module_keyrange_metadata delay_keyranges[] = {
135 struct cbox_module_livecontroller_metadata delay_controllers[] = {
138 DEFINE_MODULE(delay, 2, 2)