4 * Copyright (C) 2017-2019 Adam Williams <broadcast at earthling dot net>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 #include "bcdisplayinfo.h"
25 #include "bcsignals.h"
32 #include "transportque.inc"
44 #define MIN_OFFSET 0.0
45 #define MAX_OFFSET 100.0
47 #define MAX_DEPTH 100.0
48 #define MIN_STARTING_PHASE 0
49 #define MAX_STARTING_PHASE 100
52 PluginClient* new_plugin(PluginServer *server)
54 return new Flanger(server);
59 Flanger::Flanger(PluginServer *server)
60 : PluginAClient(server)
81 delete [] history_buffer;
84 delete [] flanging_table;
87 const char* Flanger::plugin_title() { return N_("Flanger"); }
88 int Flanger::is_realtime() { return 1; }
89 int Flanger::is_multichannel() { return 0; }
90 int Flanger::is_synthesis() { return 0; }
91 // phyllis VFrame* Flanger::new_picon() { return 0; }
94 int Flanger::process_buffer(int64_t size,
96 int64_t start_position,
99 need_reconfigure |= load_configuration();
100 // printf("Flanger::process_buffer %d start_position=%ld size=%ld\n",
107 // reset after seeking & configuring
108 if(last_position != start_position || need_reconfigure)
110 need_reconfigure = 0;
114 delete [] flanging_table;
117 // flanging waveform is a whole number of samples that repeats
118 table_size = (int)((double)sample_rate / config.rate);
124 flanging_table = new flange_sample_t[table_size];
125 double depth_samples = config.depth *
127 // read behind so the flange can work in realtime
128 double ratio = (double)depth_samples /
130 // printf("Flanger::process_buffer %d %f %f\n",
133 // sample_rate / 2 - depth_samples);
134 for(int i = 0; i <= table_size / 2; i++)
136 double input_sample = -i * ratio;
137 // printf("Flanger::process_buffer %d i=%d input_sample=%f ratio=%f\n",
142 flanging_table[i].input_sample = input_sample;
143 flanging_table[i].input_period = ratio;
146 for(int i = table_size / 2 + 1; i < table_size; i++)
148 double input_sample = -ratio * (table_size - i);
149 flanging_table[i].input_sample = input_sample;
150 flanging_table[i].input_period = ratio;
151 // printf("Flanger::process_buffer %d i=%d input_sample=%f ratio=%f\n",
160 // compute the phase position from the keyframe position & the phase offset
161 int64_t prev_position = edl_to_local(
163 get_source_position())->position);
165 if(prev_position == 0)
167 prev_position = get_source_start();
170 voice.table_offset = (int64_t)(start_position -
172 config.starting_phase * table_size / 100) % table_size;
173 // printf("Flanger::process_buffer %d start_position=%ld table_offset=%d table_size=%d\n",
176 // voice.table_offset,
180 int starting_offset = (int)(config.offset * sample_rate / 1000);
181 //phyllis int depth_offset = (int)(config.depth * sample_rate / 1000);
182 reallocate_dsp(size);
183 // reallocate_history(starting_offset + depth_offset + 1);
184 // always use the maximum history, in case of keyframes
185 reallocate_history((MAX_OFFSET + MAX_DEPTH) * sample_rate / 1000 + 1);
197 double wetness = DB::fromdb(config.wetness);
198 if(config.wetness <= INFINITYGAIN)
203 double *output = dsp_in;
204 double *input = buffer->get_data();
207 for(int j = 0; j < size; j++)
209 output[j] = input[j] * wetness;
214 int table_offset = voice.table_offset;
215 for(int j = 0; j < size; j++)
217 flange_sample_t *table = &flanging_table[table_offset];
218 double input_sample = j - starting_offset + table->input_sample;
219 //phyllis double input_period = table->input_period;
222 // printf("Flanger::process_buffer %d input_sample=%f\n",
226 // values to interpolate
229 int input_sample1 = (int)(input_sample);
230 int input_sample2 = (int)(input_sample + 1);
231 double fraction1 = (double)((int)(input_sample + 1)) - input_sample;
232 double fraction2 = 1.0 - fraction1;
233 if(input_sample1 < 0)
235 sample1 = history_buffer[history_size + input_sample1];
239 sample1 = input[input_sample1];
242 if(input_sample2 < 0)
244 sample2 = history_buffer[history_size + input_sample2];
248 sample2 = input[input_sample2];
250 output[j] += sample1 * fraction1 + sample2 * fraction2;
253 table_offset %= table_size;
255 voice.table_offset = table_offset;
257 // history is bigger than input buffer. Copy entire input buffer.
258 if(history_size > size)
260 memcpy(history_buffer,
261 history_buffer + size,
262 (history_size - size) * sizeof(double));
263 memcpy(history_buffer + (history_size - size),
265 size * sizeof(double));
269 // input is bigger than history buffer. Copy only history size
270 memcpy(history_buffer,
271 buffer->get_data() + size - history_size,
272 history_size * sizeof(double));
274 //printf("Flanger::process_buffer %d\n",
278 // copy the DSP buffer to the output
279 memcpy(buffer->get_data(), dsp_in, size * sizeof(double));
282 if(get_direction() == PLAY_FORWARD)
284 last_position = start_position + size;
288 last_position = start_position - size;
297 void Flanger::reallocate_dsp(int new_dsp_allocated)
299 if(new_dsp_allocated > dsp_in_allocated)
305 dsp_in = new double[new_dsp_allocated];
306 dsp_in_allocated = new_dsp_allocated;
310 void Flanger::reallocate_history(int new_size)
312 if(new_size != history_size)
314 // copy samples already read into the new buffers
315 double *new_history = new double[new_size];
316 bzero(new_history, sizeof(double) * new_size);
319 int copy_size = MIN(new_size, history_size);
321 history_buffer + history_size - copy_size,
322 sizeof(double) * copy_size);
323 delete [] history_buffer;
325 history_buffer = new_history;
326 history_size = new_size;
331 NEW_WINDOW_MACRO(Flanger, FlangerWindow)
332 LOAD_CONFIGURATION_MACRO(Flanger, FlangerConfig)
335 void Flanger::save_data(KeyFrame *keyframe)
339 // cause xml file to store data directly in text
340 output.set_shared_output(keyframe->xbuf);
342 output.tag.set_title("FLANGER");
343 output.tag.set_property("OFFSET", config.offset);
344 output.tag.set_property("STARTING_PHASE", config.starting_phase);
345 output.tag.set_property("DEPTH", config.depth);
346 output.tag.set_property("RATE", config.rate);
347 output.tag.set_property("WETNESS", config.wetness);
349 output.append_newline();
353 output.terminate_string();
356 void Flanger::read_data(KeyFrame *keyframe)
359 // cause xml file to read directly from text
360 input.set_shared_input(keyframe->xbuf);
363 result = input.read_tag();
367 if(input.tag.title_is("FLANGER"))
369 config.offset = input.tag.get_property("OFFSET", config.offset);
370 config.starting_phase = input.tag.get_property("STARTING_PHASE", config.starting_phase);
371 config.depth = input.tag.get_property("DEPTH", config.depth);
372 config.rate = input.tag.get_property("RATE", config.rate);
373 config.wetness = input.tag.get_property("WETNESS", config.wetness);
380 void Flanger::update_gui()
384 if(load_configuration())
386 thread->window->lock_window("Flanger::update_gui 1");
387 ((FlangerWindow*)thread->window)->update();
388 thread->window->unlock_window();
405 FlangerConfig::FlangerConfig()
414 int FlangerConfig::equivalent(FlangerConfig &that)
416 return EQUIV(offset, that.offset) &&
417 EQUIV(starting_phase, that.starting_phase) &&
418 EQUIV(depth, that.depth) &&
419 EQUIV(rate, that.rate) &&
420 EQUIV(wetness, that.wetness);
423 void FlangerConfig::copy_from(FlangerConfig &that)
425 offset = that.offset;
426 starting_phase = that.starting_phase;
429 wetness = that.wetness;
432 void FlangerConfig::interpolate(FlangerConfig &prev,
436 int64_t current_frame)
441 void FlangerConfig::boundaries()
443 CLAMP(offset, MIN_OFFSET, MAX_OFFSET);
444 CLAMP(starting_phase, MIN_STARTING_PHASE, MAX_STARTING_PHASE);
445 CLAMP(depth, MIN_DEPTH, MAX_DEPTH);
446 CLAMP(rate, MIN_RATE, MAX_RATE);
447 CLAMP(wetness, INFINITYGAIN, 0.0);
457 #define WINDOW_W xS(400)
458 #define WINDOW_H yS(165)
460 FlangerWindow::FlangerWindow(Flanger *plugin)
461 : PluginClientWindow(plugin,
468 this->plugin = plugin;
471 FlangerWindow::~FlangerWindow()
474 delete starting_phase;
480 void FlangerWindow::create_objects()
482 int margin = client->get_theme()->widget_border + xS(4);
484 int x2 = xS(200), y = margin - xS(4);
485 int x3 = x2 + BC_Pot::calculate_w() + margin;
486 int x4 = x3 + BC_Pot::calculate_w() + margin;
487 int text_w = get_w() - margin - x4;
488 int height = BC_TextBox::calculate_h(this, MEDIUMFONT, 1, 1) + margin - xS(4);
491 offset = new PluginParam(plugin,
499 &plugin->config.offset, // output_f
501 "Phase offset (ms):",
504 offset->set_precision(3);
505 offset->initialize();
509 starting_phase = new PluginParam(plugin,
517 &plugin->config.starting_phase, // output_f
519 "Starting phase (%):",
520 MIN_STARTING_PHASE, // min
521 MAX_STARTING_PHASE); // max
522 starting_phase->set_precision(3);
523 starting_phase->initialize();
528 depth = new PluginParam(plugin,
536 &plugin->config.depth, // output_f
541 depth->set_precision(3);
547 rate = new PluginParam(plugin,
555 &plugin->config.rate, // output_f
560 rate->set_precision(3);
566 wetness = new PluginParam(plugin,
574 &plugin->config.wetness, // output_f
579 wetness->set_precision(3);
580 wetness->initialize();
586 void FlangerWindow::update()
588 offset->update(0, 0);
589 starting_phase->update(0, 0);
592 wetness->update(0, 0);
595 void FlangerWindow::param_updated()