4 * Copyright (C) 2008 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
22 #include "bcdisplayinfo.h"
23 #include "bcsignals.h"
25 #include "compressor.h"
42 REGISTER_PLUGIN(CompressorEffect)
49 // More potential compressor algorithms:
50 // Use single reaction time parameter. Negative reaction time uses
51 // readahead. Positive reaction time uses slope.
53 // Smooth input stage if readahead.
54 // Determine slope from current smoothed sample to every sample in readahead area.
55 // Once highest slope is found, count of number of samples remaining until it is
56 // reached. Only search after this count for the next highest slope.
57 // Use highest slope to determine smoothed value.
59 // Smooth input stage if not readahead.
60 // For every sample, calculate slope needed to reach current sample from
61 // current smoothed value in the reaction time. If higher than current slope,
62 // make it the current slope and count number of samples remaining until it is
63 // reached. If this count is met and no higher slopes are found, base slope
64 // on current sample when count is met.
67 // For every sample, calculate gain from smoothed input value.
73 CompressorEffect::CompressorEffect(PluginServer *server)
74 : PluginAClient(server)
80 CompressorEffect::~CompressorEffect()
87 void CompressorEffect::delete_dsp()
91 for(int i = 0; i < PluginClient::total_in_buffers; i++)
92 delete input_buffer[i];
93 delete [] input_buffer;
103 void CompressorEffect::reset()
111 previous_target = 1.0;
113 target_current_sample = -1;
117 const char* CompressorEffect::plugin_title() { return _("Compressor"); }
118 int CompressorEffect::is_realtime() { return 1; }
119 int CompressorEffect::is_multichannel() { return 1; }
123 void CompressorEffect::read_data(KeyFrame *keyframe)
126 input.set_shared_input(keyframe->get_data(), strlen(keyframe->get_data()));
129 config.levels.remove_all();
132 result = input.read_tag();
136 if(input.tag.title_is("COMPRESSOR"))
138 config.reaction_len = input.tag.get_property("REACTION_LEN", config.reaction_len);
139 config.decay_len = input.tag.get_property("DECAY_LEN", config.decay_len);
140 config.trigger = input.tag.get_property("TRIGGER", config.trigger);
141 config.smoothing_only = input.tag.get_property("SMOOTHING_ONLY", config.smoothing_only);
142 config.input = input.tag.get_property("INPUT", config.input);
145 if(input.tag.title_is("LEVEL"))
147 double x = input.tag.get_property("X", (double)0);
148 double y = input.tag.get_property("Y", (double)0);
149 compressor_point_t point = { x, y };
151 config.levels.append(point);
157 void CompressorEffect::save_data(KeyFrame *keyframe)
160 output.set_shared_output(keyframe->get_data(), MESSAGESIZE);
162 output.tag.set_title("COMPRESSOR");
163 output.tag.set_property("TRIGGER", config.trigger);
164 output.tag.set_property("REACTION_LEN", config.reaction_len);
165 output.tag.set_property("DECAY_LEN", config.decay_len);
166 output.tag.set_property("SMOOTHING_ONLY", config.smoothing_only);
167 output.tag.set_property("INPUT", config.input);
169 output.tag.set_title("/COMPRESSOR");
171 output.append_newline();
174 for(int i = 0; i < config.levels.total; i++)
176 output.tag.set_title("LEVEL");
177 output.tag.set_property("X", config.levels.values[i].x);
178 output.tag.set_property("Y", config.levels.values[i].y);
181 output.tag.set_title("/LEVEL");
183 output.append_newline();
186 output.terminate_string();
190 void CompressorEffect::update_gui()
194 if(load_configuration())
196 thread->window->lock_window("CompressorEffect::update_gui");
197 ((CompressorWindow*)thread->window)->update();
198 thread->window->unlock_window();
204 LOAD_CONFIGURATION_MACRO(CompressorEffect, CompressorConfig)
205 NEW_WINDOW_MACRO(CompressorEffect, CompressorWindow)
210 int CompressorEffect::process_buffer(int64_t size,
212 int64_t start_position,
215 load_configuration();
217 // Calculate linear transfer from db
219 for(int i = 0; i < config.levels.total; i++)
222 levels.values[i].x = DB::fromdb(config.levels.values[i].x);
223 levels.values[i].y = DB::fromdb(config.levels.values[i].y);
225 min_x = DB::fromdb(config.min_db);
226 min_y = DB::fromdb(config.min_db);
231 int reaction_samples = (int)(config.reaction_len * sample_rate + 0.5);
232 int decay_samples = (int)(config.decay_len * sample_rate + 0.5);
233 int trigger = CLIP(config.trigger, 0, PluginAClient::total_in_buffers - 1);
235 CLAMP(reaction_samples, -1000000, 1000000);
236 CLAMP(decay_samples, reaction_samples, 1000000);
237 CLAMP(decay_samples, 1, 1000000);
238 if(labs(reaction_samples) < 1) reaction_samples = 1;
239 if(labs(decay_samples) < 1) decay_samples = 1;
241 int total_buffers = get_total_buffers();
242 if(reaction_samples >= 0)
244 if(target_current_sample < 0) target_current_sample = reaction_samples;
245 for(int i = 0; i < total_buffers; i++)
247 read_samples(buffer[i],
254 double current_slope = (next_target - previous_target) /
256 double *trigger_buffer = buffer[trigger]->get_data();
257 for(int i = 0; i < size; i++)
259 // Get slope required to reach current sample from smoothed sample over reaction
264 case CompressorConfig::MAX:
267 for(int j = 0; j < total_buffers; j++)
269 sample = fabs(buffer[j]->get_data()[i]);
270 if(sample > max) max = sample;
276 case CompressorConfig::TRIGGER:
277 sample = fabs(trigger_buffer[i]);
280 case CompressorConfig::SUM:
283 for(int j = 0; j < total_buffers; j++)
285 sample = fabs(buffer[j]->get_data()[i]);
293 double new_slope = (sample - current_value) /
296 // Slope greater than current slope
297 if(new_slope >= current_slope &&
298 (current_slope >= 0 ||
301 next_target = sample;
302 previous_target = current_value;
303 target_current_sample = 0;
304 target_samples = reaction_samples;
305 current_slope = new_slope;
308 if(sample > next_target && current_slope < 0)
310 next_target = sample;
311 previous_target = current_value;
312 target_current_sample = 0;
313 target_samples = decay_samples;
314 current_slope = (sample - current_value) / decay_samples;
316 // Current smoothed sample came up without finding higher slope
317 if(target_current_sample >= target_samples)
319 next_target = sample;
320 previous_target = current_value;
321 target_current_sample = 0;
322 target_samples = decay_samples;
323 current_slope = (sample - current_value) / decay_samples;
326 // Update current value and store gain
327 current_value = (next_target * target_current_sample +
328 previous_target * (target_samples - target_current_sample)) /
331 target_current_sample++;
333 if(config.smoothing_only)
335 for(int j = 0; j < total_buffers; j++)
336 buffer[j]->get_data()[i] = current_value;
340 double gain = calculate_gain(current_value);
341 for(int j = 0; j < total_buffers; j++)
343 buffer[j]->get_data()[i] *= gain;
350 if(target_current_sample < 0) target_current_sample = target_samples;
351 int64_t preview_samples = -reaction_samples;
353 // Start of new buffer is outside the current buffer. Start buffer over.
354 if(start_position < input_start ||
355 start_position >= input_start + input_size)
358 input_start = start_position;
361 // Shift current buffer so the buffer starts on start_position
362 if(start_position > input_start &&
363 start_position < input_start + input_size)
367 int len = input_start + input_size - start_position;
368 for(int i = 0; i < total_buffers; i++)
370 memcpy(input_buffer[i]->get_data(),
371 input_buffer[i]->get_data() + (start_position - input_start),
372 len * sizeof(double));
375 input_start = start_position;
379 // Expand buffer to handle preview size
380 if(size + preview_samples > input_allocated)
382 Samples **new_input_buffer = new Samples*[total_buffers];
383 for(int i = 0; i < total_buffers; i++)
385 new_input_buffer[i] = new Samples(size + preview_samples);
388 memcpy(new_input_buffer[i]->get_data(),
389 input_buffer[i]->get_data(),
390 input_size * sizeof(double));
391 delete input_buffer[i];
394 if(input_buffer) delete [] input_buffer;
396 input_allocated = size + preview_samples;
397 input_buffer = new_input_buffer;
400 // Append data to input buffer to construct readahead area.
401 #define MAX_FRAGMENT_SIZE 131072
402 while(input_size < size + preview_samples)
404 int fragment_size = MAX_FRAGMENT_SIZE;
405 if(fragment_size + input_size > size + preview_samples)
406 fragment_size = size + preview_samples - input_size;
407 for(int i = 0; i < total_buffers; i++)
409 input_buffer[i]->set_offset(input_size);
410 //printf("CompressorEffect::process_buffer %d %p %d\n", __LINE__, input_buffer[i], input_size);
411 read_samples(input_buffer[i],
414 input_start + input_size,
416 input_buffer[i]->set_offset(0);
418 input_size += fragment_size;
422 double current_slope = (next_target - previous_target) /
424 double *trigger_buffer = input_buffer[trigger]->get_data();
425 for(int i = 0; i < size; i++)
427 // Get slope from current sample to every sample in preview_samples.
428 // Take highest one or first one after target_samples are up.
430 // For optimization, calculate the first slope we really need.
431 // Assume every slope up to the end of preview_samples has been calculated and
432 // found <= to current slope.
433 int first_slope = preview_samples - 1;
434 // Need new slope immediately
435 if(target_current_sample >= target_samples)
437 for(int j = first_slope;
444 case CompressorConfig::MAX:
447 for(int k = 0; k < total_buffers; k++)
449 sample = fabs(input_buffer[k]->get_data()[i + j]);
450 if(sample > max) max = sample;
456 case CompressorConfig::TRIGGER:
457 sample = fabs(trigger_buffer[i + j]);
460 case CompressorConfig::SUM:
463 for(int k = 0; k < total_buffers; k++)
465 sample = fabs(input_buffer[k]->get_data()[i + j]);
478 double new_slope = (sample - current_value) /
480 // Got equal or higher slope
481 if(new_slope >= current_slope &&
482 (current_slope >= 0 ||
485 target_current_sample = 0;
487 current_slope = new_slope;
488 next_target = sample;
489 previous_target = current_value;
492 if(sample > next_target && current_slope < 0)
494 target_current_sample = 0;
495 target_samples = decay_samples;
496 current_slope = (sample - current_value) /
498 next_target = sample;
499 previous_target = current_value;
502 // Hit end of current slope range without finding higher slope
503 if(target_current_sample >= target_samples)
505 target_current_sample = 0;
506 target_samples = decay_samples;
507 current_slope = (sample - current_value) / decay_samples;
508 next_target = sample;
509 previous_target = current_value;
513 // Update current value and multiply gain
514 current_value = (next_target * target_current_sample +
515 previous_target * (target_samples - target_current_sample)) /
517 //buffer[0][i] = current_value;
518 target_current_sample++;
520 if(config.smoothing_only)
522 for(int j = 0; j < total_buffers; j++)
524 buffer[j]->get_data()[i] = current_value;
529 double gain = calculate_gain(current_value);
530 for(int j = 0; j < total_buffers; j++)
532 buffer[j]->get_data()[i] = input_buffer[j]->get_data()[i] * gain;
548 double CompressorEffect::calculate_output(double x)
550 if(x > 0.999) return 1.0;
552 for(int i = levels.total - 1; i >= 0; i--)
554 if(levels.values[i].x <= x)
556 if(i < levels.total - 1)
558 return levels.values[i].y +
559 (x - levels.values[i].x) *
560 (levels.values[i + 1].y - levels.values[i].y) /
561 (levels.values[i + 1].x - levels.values[i].x);
565 return levels.values[i].y +
566 (x - levels.values[i].x) *
567 (max_y - levels.values[i].y) /
568 (max_x - levels.values[i].x);
577 (levels.values[0].y - min_y) /
578 (levels.values[0].x - min_x);
585 double CompressorEffect::calculate_gain(double input)
587 // double x_db = DB::todb(input);
588 // double y_db = config.calculate_db(x_db);
589 // double y_linear = DB::fromdb(y_db);
590 double y_linear = calculate_output(input);
593 gain = y_linear / input;
608 CompressorConfig::CompressorConfig()
617 input = CompressorConfig::TRIGGER;
622 void CompressorConfig::copy_from(CompressorConfig &that)
624 this->reaction_len = that.reaction_len;
625 this->decay_len = that.decay_len;
626 this->min_db = that.min_db;
627 this->min_x = that.min_x;
628 this->min_y = that.min_y;
629 this->max_x = that.max_x;
630 this->max_y = that.max_y;
631 this->trigger = that.trigger;
632 this->input = that.input;
633 this->smoothing_only = that.smoothing_only;
635 for(int i = 0; i < that.levels.total; i++)
636 this->levels.append(that.levels.values[i]);
639 int CompressorConfig::equivalent(CompressorConfig &that)
641 if(!EQUIV(this->reaction_len, that.reaction_len) ||
642 !EQUIV(this->decay_len, that.decay_len) ||
643 this->trigger != that.trigger ||
644 this->input != that.input ||
645 this->smoothing_only != that.smoothing_only)
647 if(this->levels.total != that.levels.total) return 0;
649 i < this->levels.total && i < that.levels.total;
652 compressor_point_t *this_level = &this->levels.values[i];
653 compressor_point_t *that_level = &that.levels.values[i];
654 if(!EQUIV(this_level->x, that_level->x) ||
655 !EQUIV(this_level->y, that_level->y))
661 void CompressorConfig::interpolate(CompressorConfig &prev,
662 CompressorConfig &next,
665 int64_t current_frame)
670 int CompressorConfig::total_points()
678 void CompressorConfig::dump()
680 printf("CompressorConfig::dump\n");
681 for(int i = 0; i < levels.total; i++)
683 printf(" %f %f\n", levels.values[i].x, levels.values[i].y);
688 double CompressorConfig::get_y(int number)
693 if(number >= levels.total)
694 return levels.values[levels.total - 1].y;
696 return levels.values[number].y;
699 double CompressorConfig::get_x(int number)
704 if(number >= levels.total)
705 return levels.values[levels.total - 1].x;
707 return levels.values[number].x;
710 double CompressorConfig::calculate_db(double x)
712 if(x > -0.001) return 0.0;
714 for(int i = levels.total - 1; i >= 0; i--)
716 if(levels.values[i].x <= x)
718 if(i < levels.total - 1)
720 return levels.values[i].y +
721 (x - levels.values[i].x) *
722 (levels.values[i + 1].y - levels.values[i].y) /
723 (levels.values[i + 1].x - levels.values[i].x);
727 return levels.values[i].y +
728 (x - levels.values[i].x) *
729 (max_y - levels.values[i].y) /
730 (max_x - levels.values[i].x);
739 (levels.values[0].y - min_y) /
740 (levels.values[0].x - min_x);
747 int CompressorConfig::set_point(double x, double y)
749 for(int i = levels.total - 1; i >= 0; i--)
751 if(levels.values[i].x < x)
755 for(int j = levels.total - 2; j >= i; j--)
757 levels.values[j + 1] = levels.values[j];
759 levels.values[i].x = x;
760 levels.values[i].y = y;
767 for(int j = levels.total - 2; j >= 0; j--)
769 levels.values[j + 1] = levels.values[j];
771 levels.values[0].x = x;
772 levels.values[0].y = y;
776 void CompressorConfig::remove_point(int number)
778 for(int j = number; j < levels.total - 1; j++)
780 levels.values[j] = levels.values[j + 1];
785 void CompressorConfig::optimize()
794 for(int i = 0; i < levels.total - 1; i++)
796 if(levels.values[i].x >= levels.values[i + 1].x)
799 for(int j = i + 1; j < levels.total - 1; j++)
801 levels.values[j] = levels.values[j + 1];
836 CompressorWindow::CompressorWindow(CompressorEffect *plugin)
837 : PluginClientWindow(plugin,
844 this->plugin = plugin;
847 void CompressorWindow::create_objects()
850 int control_margin = 130;
852 add_subwindow(canvas = new CompressorCanvas(plugin,
855 get_w() - x - control_margin - 10,
857 canvas->set_cursor(CROSS_CURSOR, 0, 0);
858 x = get_w() - control_margin;
859 add_subwindow(new BC_Title(x, y, _("Reaction secs:")));
861 add_subwindow(reaction = new CompressorReaction(plugin, x, y));
863 add_subwindow(new BC_Title(x, y, _("Decay secs:")));
865 add_subwindow(decay = new CompressorDecay(plugin, x, y));
867 add_subwindow(new BC_Title(x, y, _("Trigger Type:")));
869 add_subwindow(input = new CompressorInput(plugin, x, y));
870 input->create_objects();
872 add_subwindow(new BC_Title(x, y, _("Trigger:")));
874 add_subwindow(trigger = new CompressorTrigger(plugin, x, y));
875 if(plugin->config.input != CompressorConfig::TRIGGER) trigger->disable();
877 add_subwindow(smooth = new CompressorSmooth(plugin, x, y));
879 add_subwindow(clear = new CompressorClear(plugin, x, y));
882 add_subwindow(new BC_Title(x, y, _("Point:")));
884 add_subwindow(x_text = new CompressorX(plugin, x, y));
886 add_subwindow(new BC_Title(x, y, _("x")));
888 add_subwindow(y_text = new CompressorY(plugin, x, y));
895 void CompressorWindow::draw_scales()
897 draw_3d_border(canvas->get_x() - 2,
908 set_color(get_resources()->default_text_color);
911 for(int i = 0; i <= DIVISIONS; i++)
913 int y = canvas->get_y() + 10 + canvas->get_h() / DIVISIONS * i;
914 int x = canvas->get_x() - 30;
915 char string[BCTEXTLEN];
917 sprintf(string, "%.0f", (float)i / DIVISIONS * plugin->config.min_db);
918 draw_text(x, y, string);
920 int y1 = canvas->get_y() + canvas->get_h() / DIVISIONS * i;
921 int y2 = canvas->get_y() + canvas->get_h() / DIVISIONS * (i + 1);
922 for(int j = 0; j < 10; j++)
924 y = y1 + (y2 - y1) * j / 10;
927 draw_line(canvas->get_x() - 10, y, canvas->get_x(), y);
932 draw_line(canvas->get_x() - 5, y, canvas->get_x(), y);
938 for(int i = 0; i <= DIVISIONS; i++)
940 int y = canvas->get_h() + 30;
941 int x = canvas->get_x() + (canvas->get_w() - 10) / DIVISIONS * i;
942 char string[BCTEXTLEN];
944 sprintf(string, "%.0f", (1.0 - (float)i / DIVISIONS) * plugin->config.min_db);
945 draw_text(x, y, string);
947 int x1 = canvas->get_x() + canvas->get_w() / DIVISIONS * i;
948 int x2 = canvas->get_x() + canvas->get_w() / DIVISIONS * (i + 1);
949 for(int j = 0; j < 10; j++)
951 x = x1 + (x2 - x1) * j / 10;
954 draw_line(x, canvas->get_y() + canvas->get_h(), x, canvas->get_y() + canvas->get_h() + 10);
959 draw_line(x, canvas->get_y() + canvas->get_h(), x, canvas->get_y() + canvas->get_h() + 5);
969 void CompressorWindow::update()
975 void CompressorWindow::update_textboxes()
977 if(atol(trigger->get_text()) != plugin->config.trigger)
978 trigger->update((int64_t)plugin->config.trigger);
979 if(strcmp(input->get_text(), CompressorInput::value_to_text(plugin->config.input)))
980 input->set_text(CompressorInput::value_to_text(plugin->config.input));
982 if(plugin->config.input != CompressorConfig::TRIGGER && trigger->get_enabled())
985 if(plugin->config.input == CompressorConfig::TRIGGER && !trigger->get_enabled())
988 if(!EQUIV(atof(reaction->get_text()), plugin->config.reaction_len))
989 reaction->update((float)plugin->config.reaction_len);
990 if(!EQUIV(atof(decay->get_text()), plugin->config.decay_len))
991 decay->update((float)plugin->config.decay_len);
992 smooth->update(plugin->config.smoothing_only);
993 if(canvas->current_operation == CompressorCanvas::DRAG)
995 x_text->update((float)plugin->config.levels.values[canvas->current_point].x);
996 y_text->update((float)plugin->config.levels.values[canvas->current_point].y);
1001 void CompressorWindow::update_canvas()
1003 canvas->clear_box(0, 0, canvas->get_w(), canvas->get_h());
1004 canvas->set_line_dashes(1);
1005 canvas->set_color(GREEN);
1007 for(int i = 1; i < DIVISIONS; i++)
1009 int y = canvas->get_h() * i / DIVISIONS;
1010 canvas->draw_line(0, y, canvas->get_w(), y);
1012 int x = canvas->get_w() * i / DIVISIONS;
1013 canvas->draw_line(x, 0, x, canvas->get_h());
1015 canvas->set_line_dashes(0);
1018 canvas->set_font(MEDIUMFONT);
1019 canvas->draw_text(plugin->get_theme()->widget_border,
1020 canvas->get_h() / 2,
1022 canvas->draw_text(canvas->get_w() / 2 - canvas->get_text_width(MEDIUMFONT, _("Input")) / 2,
1023 canvas->get_h() - plugin->get_theme()->widget_border,
1027 canvas->set_color(WHITE);
1028 canvas->set_line_width(2);
1030 double x_db = plugin->config.min_db;
1031 double y_db = plugin->config.calculate_db(x_db);
1032 int y1 = (int)(y_db / plugin->config.min_db * canvas->get_h());
1034 for(int i=1; i<canvas->get_w(); i++)
1036 x_db = (1. - (double)i / canvas->get_w()) * plugin->config.min_db;
1037 y_db = plugin->config.calculate_db(x_db);
1038 int y2 = (int)(y_db / plugin->config.min_db * canvas->get_h());
1039 canvas->draw_line(i-1, y1, i, y2);
1042 canvas->set_line_width(1);
1044 int total = plugin->config.levels.total ? plugin->config.levels.total : 1;
1045 for(int i=0; i < total; i++)
1047 x_db = plugin->config.get_x(i);
1048 y_db = plugin->config.get_y(i);
1050 int x = (int)((1. - x_db / plugin->config.min_db) * canvas->get_w());
1051 int y = (int)(y_db / plugin->config.min_db * canvas->get_h());
1053 canvas->draw_box(x - POINT_W / 2, y - POINT_W / 2, POINT_W, POINT_W);
1059 int CompressorWindow::resize_event(int w, int h)
1071 CompressorCanvas::CompressorCanvas(CompressorEffect *plugin, int x, int y, int w, int h)
1072 : BC_SubWindow(x, y, w, h, BLACK)
1074 this->plugin = plugin;
1075 current_operation = NONE;
1078 int CompressorCanvas::button_press_event()
1080 // Check existing points
1081 if(is_event_win() && cursor_inside())
1083 for(int i = 0; i < plugin->config.levels.total; i++)
1085 double x_db = plugin->config.get_x(i);
1086 double y_db = plugin->config.get_y(i);
1088 int x = (int)(((double)1 - x_db / plugin->config.min_db) * get_w());
1089 int y = (int)(y_db / plugin->config.min_db * get_h());
1091 if(get_cursor_x() < x + POINT_W / 2 && get_cursor_x() >= x - POINT_W / 2 &&
1092 get_cursor_y() < y + POINT_W / 2 && get_cursor_y() >= y - POINT_W / 2)
1094 current_operation = DRAG;
1103 double x_db = (double)(1 - (double)get_cursor_x() / get_w()) * plugin->config.min_db;
1104 double y_db = (double)get_cursor_y() / get_h() * plugin->config.min_db;
1106 current_point = plugin->config.set_point(x_db, y_db);
1107 current_operation = DRAG;
1108 ((CompressorWindow*)plugin->thread->window)->update();
1109 plugin->send_configure_change();
1113 //plugin->config.dump();
1116 int CompressorCanvas::button_release_event()
1118 if(current_operation == DRAG)
1120 if(current_point > 0)
1122 if(plugin->config.levels.values[current_point].x <
1123 plugin->config.levels.values[current_point - 1].x)
1124 plugin->config.remove_point(current_point);
1127 if(current_point < plugin->config.levels.total - 1)
1129 if(plugin->config.levels.values[current_point].x >=
1130 plugin->config.levels.values[current_point + 1].x)
1131 plugin->config.remove_point(current_point);
1134 ((CompressorWindow*)plugin->thread->window)->update();
1135 plugin->send_configure_change();
1136 current_operation = NONE;
1143 int CompressorCanvas::cursor_motion_event()
1145 if(current_operation == DRAG)
1147 int x = get_cursor_x();
1148 int y = get_cursor_y();
1149 CLAMP(x, 0, get_w());
1150 CLAMP(y, 0, get_h());
1151 double x_db = (double)(1 - (double)x / get_w()) * plugin->config.min_db;
1152 double y_db = (double)y / get_h() * plugin->config.min_db;
1153 plugin->config.levels.values[current_point].x = x_db;
1154 plugin->config.levels.values[current_point].y = y_db;
1155 ((CompressorWindow*)plugin->thread->window)->update();
1156 plugin->send_configure_change();
1158 //plugin->config.dump();
1161 // Change cursor over points
1162 if(is_event_win() && cursor_inside())
1164 int new_cursor = CROSS_CURSOR;
1166 for(int i = 0; i < plugin->config.levels.total; i++)
1168 double x_db = plugin->config.get_x(i);
1169 double y_db = plugin->config.get_y(i);
1171 int x = (int)(((double)1 - x_db / plugin->config.min_db) * get_w());
1172 int y = (int)(y_db / plugin->config.min_db * get_h());
1174 if(get_cursor_x() < x + POINT_W / 2 && get_cursor_x() >= x - POINT_W / 2 &&
1175 get_cursor_y() < y + POINT_W / 2 && get_cursor_y() >= y - POINT_W / 2)
1177 new_cursor = UPRIGHT_ARROW_CURSOR;
1183 if(new_cursor != get_cursor())
1185 set_cursor(new_cursor, 0, 1);
1195 CompressorReaction::CompressorReaction(CompressorEffect *plugin, int x, int y)
1196 : BC_TextBox(x, y, 100, 1, (float)plugin->config.reaction_len)
1198 this->plugin = plugin;
1201 int CompressorReaction::handle_event()
1203 plugin->config.reaction_len = atof(get_text());
1204 plugin->send_configure_change();
1208 int CompressorReaction::button_press_event()
1212 if(get_buttonpress() < 4) return BC_TextBox::button_press_event();
1213 if(get_buttonpress() == 4)
1215 plugin->config.reaction_len += 0.1;
1218 if(get_buttonpress() == 5)
1220 plugin->config.reaction_len -= 0.1;
1222 update((float)plugin->config.reaction_len);
1223 plugin->send_configure_change();
1229 CompressorDecay::CompressorDecay(CompressorEffect *plugin, int x, int y)
1230 : BC_TextBox(x, y, 100, 1, (float)plugin->config.decay_len)
1232 this->plugin = plugin;
1234 int CompressorDecay::handle_event()
1236 plugin->config.decay_len = atof(get_text());
1237 plugin->send_configure_change();
1241 int CompressorDecay::button_press_event()
1245 if(get_buttonpress() < 4) return BC_TextBox::button_press_event();
1246 if(get_buttonpress() == 4)
1248 plugin->config.decay_len += 0.1;
1251 if(get_buttonpress() == 5)
1253 plugin->config.decay_len -= 0.1;
1255 update((float)plugin->config.decay_len);
1256 plugin->send_configure_change();
1264 CompressorX::CompressorX(CompressorEffect *plugin, int x, int y)
1265 : BC_TextBox(x, y, 100, 1, "")
1267 this->plugin = plugin;
1269 int CompressorX::handle_event()
1271 int current_point = ((CompressorWindow*)plugin->thread->window)->canvas->current_point;
1272 if(current_point < plugin->config.levels.total)
1274 plugin->config.levels.values[current_point].x = atof(get_text());
1275 ((CompressorWindow*)plugin->thread->window)->update_canvas();
1276 plugin->send_configure_change();
1283 CompressorY::CompressorY(CompressorEffect *plugin, int x, int y)
1284 : BC_TextBox(x, y, 100, 1, "")
1286 this->plugin = plugin;
1288 int CompressorY::handle_event()
1290 int current_point = ((CompressorWindow*)plugin->thread->window)->canvas->current_point;
1291 if(current_point < plugin->config.levels.total)
1293 plugin->config.levels.values[current_point].y = atof(get_text());
1294 ((CompressorWindow*)plugin->thread->window)->update_canvas();
1295 plugin->send_configure_change();
1304 CompressorTrigger::CompressorTrigger(CompressorEffect *plugin, int x, int y)
1305 : BC_TextBox(x, y, (int64_t)100, (int64_t)1, (int64_t)plugin->config.trigger)
1307 this->plugin = plugin;
1309 int CompressorTrigger::handle_event()
1311 plugin->config.trigger = atol(get_text());
1312 plugin->send_configure_change();
1316 int CompressorTrigger::button_press_event()
1320 if(get_buttonpress() < 4) return BC_TextBox::button_press_event();
1321 if(get_buttonpress() == 4)
1323 plugin->config.trigger++;
1326 if(get_buttonpress() == 5)
1328 plugin->config.trigger--;
1330 update((int64_t)plugin->config.trigger);
1331 plugin->send_configure_change();
1341 CompressorInput::CompressorInput(CompressorEffect *plugin, int x, int y)
1345 CompressorInput::value_to_text(plugin->config.input),
1348 this->plugin = plugin;
1350 int CompressorInput::handle_event()
1352 plugin->config.input = text_to_value(get_text());
1353 ((CompressorWindow*)plugin->thread->window)->update();
1354 plugin->send_configure_change();
1358 void CompressorInput::create_objects()
1360 for(int i = 0; i < 3; i++)
1362 add_item(new BC_MenuItem(value_to_text(i)));
1366 const char* CompressorInput::value_to_text(int value)
1370 case CompressorConfig::TRIGGER: return _("Trigger");
1371 case CompressorConfig::MAX: return _("Maximum");
1372 case CompressorConfig::SUM: return _("Total");
1375 return _("Trigger");
1378 int CompressorInput::text_to_value(char *text)
1380 for(int i = 0; i < 3; i++)
1382 if(!strcmp(value_to_text(i), text)) return i;
1385 return CompressorConfig::TRIGGER;
1393 CompressorClear::CompressorClear(CompressorEffect *plugin, int x, int y)
1394 : BC_GenericButton(x, y, _("Clear"))
1396 this->plugin = plugin;
1399 int CompressorClear::handle_event()
1401 plugin->config.levels.remove_all();
1402 //plugin->config.dump();
1403 ((CompressorWindow*)plugin->thread->window)->update();
1404 plugin->send_configure_change();
1410 CompressorSmooth::CompressorSmooth(CompressorEffect *plugin, int x, int y)
1411 : BC_CheckBox(x, y, plugin->config.smoothing_only, _("Smooth only"))
1413 this->plugin = plugin;
1416 int CompressorSmooth::handle_event()
1418 plugin->config.smoothing_only = get_value();
1419 plugin->send_configure_change();