[40448f]: trunk / src / plugins / reversedelay.cc Maximize Restore History

Download this file

reversedelay.cc    260 lines (218 with data), 6.9 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
#include "gx_plugin.h"
#include <cmath>
#include <stdlib.h>
namespace pluginlib {
namespace reversedelay {
#define N_(x) (x)
class ReverseDelay : public PluginDef {
private:
float sample_rate;
float *buffer;
unsigned int counter;
unsigned int buf_size;
unsigned int cur_buf_size;
float feedback_buf;
//Params
float time, feedback, window, drywet;
//Params buffs
float time_old, window_old;
class overlap_window
{
private:
float val;
float step;
float acc;
unsigned int active_samples;
unsigned int full_samples;
unsigned int counter;
public:
overlap_window() {
val = 0;
step = 0;
acc = 0;
active_samples = 0;
full_samples = 0;
counter = 0;
}
void set_coef(float t /* 0..1 */, unsigned int full_samples) {
set_full(0, full_samples, t*full_samples);
}
bool set_full(float min_val, unsigned int full_samples, unsigned int active_samples) {
if(active_samples >= full_samples) return false;
acc = min_val;
val = min_val;
this->full_samples = full_samples;
this->active_samples = active_samples;
counter = 0;
step = (1 - min_val)/(active_samples/2);
return true;
}
float get() {
if(counter >= 0 && counter < active_samples/2) {
acc += step;
counter++;
return acc;
}
else if(counter >= active_samples/2 && counter <= full_samples - active_samples/2) {
counter++;
return 1;
}
else if(counter > full_samples - active_samples/2 && counter < full_samples) {
acc -= step;
counter++;
return acc;
}
else if(counter >= full_samples) {
float ret_val = acc;
acc = val;
counter = 0;
return ret_val;
}
return 1;
}
};
overlap_window ow;
static float reverse_delay_line_impl(float in, float* buf, unsigned int* counter, unsigned int length)
{
float out = 0;
//Read data
if(*counter < length - 1)
{
unsigned int read_counter = length - 1 - (*counter);
out = buf[read_counter];
}
//Write data
*(buf + (*counter)) = in;
(*counter)++;
if ((*counter) > length-1)
(*counter) = 0;
return (out);
}
public:
ReverseDelay();
static void init(unsigned int samplingFreq, PluginDef *plugin);
static void process(int count, float *input, float *output, PluginDef *plugin);
static int registerparam(const ParamReg& reg);
static int uiloader(const UiBuilder& builder, int form);
static void del_instance(PluginDef *plugin);
};
ReverseDelay::ReverseDelay():
PluginDef(),
sample_rate(0) {
version = PLUGINDEF_VERSION;
id = "reversedelay";
name = N_("ReverseDelay");
category = N_("Echo / Delay");
mono_audio = process;
set_samplerate = init;
register_params = registerparam;
load_ui = uiloader;
delete_instance = del_instance;
buffer = NULL;
counter = 0;
buf_size = 0;
cur_buf_size = 0;
feedback_buf = 0;
time_old = 0;
window_old = 0;
}
int ReverseDelay::registerparam(const ParamReg& reg) {
ReverseDelay& self = *static_cast<ReverseDelay*>(reg.plugin);
reg.registerVar("reversedelay.time", N_("Time (ms)"), "S", N_("Delay time in milliseconds"), &self.time, 500, 200, 2000, 1);
reg.registerVar("reversedelay.feedback", N_("Feedback"), "S", N_("Feedback"), &self.feedback, 0, 0, 1, 0.05);
reg.registerVar("reversedelay.window", N_("Window (%)"), "S", N_("Crossfade between delayed chunks in percents"), &self.window, 50, 0, 100, 1);
reg.registerVar("reversedelay.drywet", N_("Dry/Wet"), "S", "Dey/Wet", &self.drywet, 0.5, 0, 1, 0.05);
return 0;
}
void ReverseDelay::init(unsigned int samplingFreq, PluginDef *plugin) {
ReverseDelay& self = *static_cast<ReverseDelay*>(plugin);
self.sample_rate = samplingFreq;
float* old_buf = self.buffer;
//Provide dual buf size, with 2 seconds length for every part
unsigned int new_buf_size = (unsigned int)(samplingFreq * 2 * 2);
float *new_buf = new float[new_buf_size];
for (size_t i = 0; i < new_buf_size; i++)
new_buf[i] = 0.0f;
// Assign new pointer and size
self.buffer = new_buf;
self.buf_size = new_buf_size;
// Delete old buffer
if (old_buf != NULL)
delete [] old_buf;
}
void ReverseDelay::process(int count, float *input, float *output, PluginDef *plugin) {
ReverseDelay& self = *static_cast<ReverseDelay*>(plugin);
//Update params
if(self.time_old != self.time) {
self.cur_buf_size = (self.time/1000.0)*self.sample_rate;
self.counter = 0;
self.ow.set_coef((self.window)/(100.0 + 1.0), self.cur_buf_size/2); //Avoid to pass 1
self.time_old = self.time;
self.window_old = self.window;
}
else if(self.window_old != self.window)
{
self.ow.set_coef((self.window)/(100.0 + 1.0), self.cur_buf_size/2);
self.window_old = self.window;
}
for (int i = 0; i < count; ++i) {
float in = input[i];
float out = 0;
// Can be added -> //Update tempo led
//if(counter < cur_buf_size/4)
// *params[param_tempo_led] = true;
//else
// *params[param_tempo_led] = false;
//Process
out = reverse_delay_line_impl(in + self.feedback_buf * self.feedback, self.buffer, &self.counter, self.cur_buf_size);
self.feedback_buf = out;
out*= self.ow.get();
out = out* self.drywet + in* (1 - self.drywet);
output[i] = out;
}
}
int ReverseDelay::uiloader(const UiBuilder& b, int form) {
if (!(form & UI_FORM_STACK)) {
return -1;
}
b.openHorizontalhideBox("");
{
b.create_master_slider("reversedelay.drywet",0);
}
b.closeBox();
b.openHorizontalBox("");
{
b.create_small_rackknob("reversedelay.time",0);
b.create_small_rackknob("reversedelay.feedback",0);
b.create_small_rackknob("reversedelay.window",0);
b.create_small_rackknobr("reversedelay.drywet",0);
}
b.closeBox();
return 0;
}
void ReverseDelay::del_instance(PluginDef *plugin)
{
ReverseDelay& self = *static_cast<ReverseDelay*>(plugin);
delete [] self.buffer;
delete static_cast<ReverseDelay*>(plugin);
}
#if true
PluginDef *plugin() {
return new ReverseDelay;
}
#else
extern "C" __attribute__ ((visibility ("default"))) int
get_gx_plugin(unsigned int idx, PluginDef **pplugin)
{
const int count = 1;
if (!pplugin) {
return count;
}
switch (idx) {
case 0: *pplugin = new ReverseDelay; return count;
default: *pplugin = 0; return -1;
}
}
#endif
} // end namespace reverse_delay
} // end namespace pluginlib