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 MAX_CHORUS_LENGTH 4096
33 #define MODULE_PARAMS chorus_params
35 static float sine_table
[2049];
48 struct cbox_module module
;
50 float storage
[MAX_CHORUS_LENGTH
][2];
51 struct chorus_params
*params
;
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
))
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
);
78 return cbox_object_default_process_cmd(ct
, fb
, cmd
, error
);
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
;
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
++)
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
;
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
);
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;
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
;
153 m
->tp32dsr
= 65536.0 * 65536.0 * m
->module
.srate_inv
;
154 struct chorus_params
*p
= malloc(sizeof(struct chorus_params
));
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
;
168 struct cbox_module_keyrange_metadata chorus_keyranges
[] = {
171 struct cbox_module_livecontroller_metadata chorus_controllers
[] = {
174 DEFINE_MODULE(chorus
, 2, 2)