bg/clr color tweaks, clear borders rework, fc31 depends
[goodguy/cinelerra.git] / cinelerra-5.1 / plugins / compressor / compressor.C
1
2 /*
3  * CINELERRA
4  * Copyright (C) 2008 Adam Williams <broadcast at earthling dot net>
5  *
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.
10  *
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.
15  *
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
19  *
20  */
21
22 #include "bcdisplayinfo.h"
23 #include "bcsignals.h"
24 #include "clip.h"
25 #include "compressor.h"
26 #include "cursors.h"
27 #include "bchash.h"
28 #include "filexml.h"
29 #include "language.h"
30 #include "samples.h"
31 #include "theme.h"
32 #include "units.h"
33 #include "vframe.h"
34
35 #include <math.h>
36 #include <string.h>
37
38
39
40
41
42 REGISTER_PLUGIN(CompressorEffect)
43
44
45
46
47
48
49 // More potential compressor algorithms:
50 // Use single reaction time parameter.  Negative reaction time uses
51 // readahead.  Positive reaction time uses slope.
52
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.
58
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.
65
66 // Gain stage.
67 // For every sample, calculate gain from smoothed input value.
68
69
70
71
72
73 CompressorEffect::CompressorEffect(PluginServer *server)
74  : PluginAClient(server)
75 {
76         reset();
77
78 }
79
80 CompressorEffect::~CompressorEffect()
81 {
82
83         delete_dsp();
84         levels.remove_all();
85 }
86
87 void CompressorEffect::delete_dsp()
88 {
89         if(input_buffer)
90         {
91                 for(int i = 0; i < PluginClient::total_in_buffers; i++)
92                         delete input_buffer[i];
93                 delete [] input_buffer;
94         }
95
96
97         input_buffer = 0;
98         input_size = 0;
99         input_allocated = 0;
100 }
101
102
103 void CompressorEffect::reset()
104 {
105         input_buffer = 0;
106         input_size = 0;
107         input_allocated = 0;
108         input_start = 0;
109
110         next_target = 1.0;
111         previous_target = 1.0;
112         target_samples = 1;
113         target_current_sample = -1;
114         current_value = 1.0;
115 }
116
117 const char* CompressorEffect::plugin_title() { return N_("Compressor"); }
118 int CompressorEffect::is_realtime() { return 1; }
119 int CompressorEffect::is_multichannel() { return 1; }
120
121
122
123 void CompressorEffect::read_data(KeyFrame *keyframe)
124 {
125         FileXML input;
126         input.set_shared_input(keyframe->xbuf);
127
128         int result = 0;
129         config.levels.remove_all();
130         while(!result)
131         {
132                 result = input.read_tag();
133
134                 if(!result)
135                 {
136                         if(input.tag.title_is("COMPRESSOR"))
137                         {
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);
143                         }
144                         else
145                         if(input.tag.title_is("LEVEL"))
146                         {
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 };
150
151                                 config.levels.append(point);
152                         }
153                 }
154         }
155 }
156
157 void CompressorEffect::save_data(KeyFrame *keyframe)
158 {
159         FileXML output;
160         output.set_shared_output(keyframe->xbuf);
161
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);
168         output.append_tag();
169         output.tag.set_title("/COMPRESSOR");
170         output.append_tag();
171         output.append_newline();
172
173
174         for(int i = 0; i < config.levels.total; i++)
175         {
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);
179
180                 output.append_tag();
181                 output.tag.set_title("/LEVEL");
182                 output.append_tag();
183                 output.append_newline();
184         }
185
186         output.terminate_string();
187 }
188
189
190 void CompressorEffect::update_gui()
191 {
192         if( !thread ) return;
193         CompressorWindow *window = (CompressorWindow*)thread->window;
194 // load_configuration,read_data deletes levels
195         window->lock_window("CompressorEffect::update_gui");
196         if( load_configuration() )
197                 window->update();
198         window->unlock_window();
199 }
200
201
202 LOAD_CONFIGURATION_MACRO(CompressorEffect, CompressorConfig)
203 NEW_WINDOW_MACRO(CompressorEffect, CompressorWindow)
204
205
206
207
208 int CompressorEffect::process_buffer(int64_t size,
209                 Samples **buffer,
210                 int64_t start_position,
211                 int sample_rate)
212 {
213         load_configuration();
214
215 // Calculate linear transfer from db
216         levels.remove_all();
217         for(int i = 0; i < config.levels.total; i++)
218         {
219                 levels.append();
220                 levels.values[i].x = DB::fromdb(config.levels.values[i].x);
221                 levels.values[i].y = DB::fromdb(config.levels.values[i].y);
222         }
223         min_x = DB::fromdb(config.min_db);
224         min_y = DB::fromdb(config.min_db);
225         max_x = 1.0;
226         max_y = 1.0;
227
228
229         int reaction_samples = (int)(config.reaction_len * sample_rate + 0.5);
230         int decay_samples = (int)(config.decay_len * sample_rate + 0.5);
231         int trigger = CLIP(config.trigger, 0, PluginAClient::total_in_buffers - 1);
232
233         CLAMP(reaction_samples, -1000000, 1000000);
234         CLAMP(decay_samples, reaction_samples, 1000000);
235         CLAMP(decay_samples, 1, 1000000);
236         if(labs(reaction_samples) < 1) reaction_samples = 1;
237         if(labs(decay_samples) < 1) decay_samples = 1;
238
239         int total_buffers = get_total_buffers();
240         if(reaction_samples >= 0)
241         {
242                 if(target_current_sample < 0) target_current_sample = reaction_samples;
243                 for(int i = 0; i < total_buffers; i++)
244                 {
245                         read_samples(buffer[i],
246                                 i,
247                                 sample_rate,
248                                 start_position,
249                                 size);
250                 }
251
252                 double current_slope = (next_target - previous_target) /
253                         reaction_samples;
254                 double *trigger_buffer = buffer[trigger]->get_data();
255                 for(int i = 0; i < size; i++)
256                 {
257 // Get slope required to reach current sample from smoothed sample over reaction
258 // length.
259                         double sample = 0.;
260                         switch(config.input)
261                         {
262                                 case CompressorConfig::MAX:
263                                 {
264                                         double max = 0;
265                                         for(int j = 0; j < total_buffers; j++)
266                                         {
267                                                 sample = fabs(buffer[j]->get_data()[i]);
268                                                 if(sample > max) max = sample;
269                                         }
270                                         sample = max;
271                                         break;
272                                 }
273
274                                 case CompressorConfig::TRIGGER:
275                                         sample = fabs(trigger_buffer[i]);
276                                         break;
277
278                                 case CompressorConfig::SUM:
279                                 {
280                                         double max = 0;
281                                         for(int j = 0; j < total_buffers; j++)
282                                         {
283                                                 sample = fabs(buffer[j]->get_data()[i]);
284                                                 max += sample;
285                                         }
286                                         sample = max;
287                                         break;
288                                 }
289                         }
290
291                         double new_slope = (sample - current_value) /
292                                 reaction_samples;
293
294 // Slope greater than current slope
295                         if(new_slope >= current_slope &&
296                                 (current_slope >= 0 ||
297                                 new_slope >= 0))
298                         {
299                                 next_target = sample;
300                                 previous_target = current_value;
301                                 target_current_sample = 0;
302                                 target_samples = reaction_samples;
303                                 current_slope = new_slope;
304                         }
305                         else
306                         if(sample > next_target && current_slope < 0)
307                         {
308                                 next_target = sample;
309                                 previous_target = current_value;
310                                 target_current_sample = 0;
311                                 target_samples = decay_samples;
312                                 current_slope = (sample - current_value) / decay_samples;
313                         }
314 // Current smoothed sample came up without finding higher slope
315                         if(target_current_sample >= target_samples)
316                         {
317                                 next_target = sample;
318                                 previous_target = current_value;
319                                 target_current_sample = 0;
320                                 target_samples = decay_samples;
321                                 current_slope = (sample - current_value) / decay_samples;
322                         }
323
324 // Update current value and store gain
325                         current_value = (next_target * target_current_sample +
326                                 previous_target * (target_samples - target_current_sample)) /
327                                 target_samples;
328
329                         target_current_sample++;
330
331                         if(config.smoothing_only)
332                         {
333                                 for(int j = 0; j < total_buffers; j++)
334                                         buffer[j]->get_data()[i] = current_value;
335                         }
336                         else
337                         {
338                                 double gain = calculate_gain(current_value);
339                                 for(int j = 0; j < total_buffers; j++)
340                                 {
341                                         buffer[j]->get_data()[i] *= gain;
342                                 }
343                         }
344                 }
345         }
346         else
347         {
348                 if(target_current_sample < 0) target_current_sample = target_samples;
349                 int64_t preview_samples = -reaction_samples;
350
351 // Start of new buffer is outside the current buffer.  Start buffer over.
352                 if(start_position < input_start ||
353                         start_position >= input_start + input_size)
354                 {
355                         input_size = 0;
356                         input_start = start_position;
357                 }
358                 else
359 // Shift current buffer so the buffer starts on start_position
360                 if(start_position > input_start &&
361                         start_position < input_start + input_size)
362                 {
363                         if(input_buffer)
364                         {
365                                 int len = input_start + input_size - start_position;
366                                 for(int i = 0; i < total_buffers; i++)
367                                 {
368                                         memcpy(input_buffer[i]->get_data(),
369                                                 input_buffer[i]->get_data() + (start_position - input_start),
370                                                 len * sizeof(double));
371                                 }
372                                 input_size = len;
373                                 input_start = start_position;
374                         }
375                 }
376
377 // Expand buffer to handle preview size
378                 if(size + preview_samples > input_allocated)
379                 {
380                         Samples **new_input_buffer = new Samples*[total_buffers];
381                         for(int i = 0; i < total_buffers; i++)
382                         {
383                                 new_input_buffer[i] = new Samples(size + preview_samples);
384                                 if(input_buffer)
385                                 {
386                                         memcpy(new_input_buffer[i]->get_data(),
387                                                 input_buffer[i]->get_data(),
388                                                 input_size * sizeof(double));
389                                         delete input_buffer[i];
390                                 }
391                         }
392                         if(input_buffer) delete [] input_buffer;
393
394                         input_allocated = size + preview_samples;
395                         input_buffer = new_input_buffer;
396                 }
397
398 // Append data to input buffer to construct readahead area.
399 #define MAX_FRAGMENT_SIZE 131072
400                 while(input_size < size + preview_samples)
401                 {
402                         int fragment_size = MAX_FRAGMENT_SIZE;
403                         if(fragment_size + input_size > size + preview_samples)
404                                 fragment_size = size + preview_samples - input_size;
405                         for(int i = 0; i < total_buffers; i++)
406                         {
407                                 input_buffer[i]->set_offset(input_size);
408 //printf("CompressorEffect::process_buffer %d %p %d\n", __LINE__, input_buffer[i], input_size);
409                                 read_samples(input_buffer[i],
410                                         i,
411                                         sample_rate,
412                                         input_start + input_size,
413                                         fragment_size);
414                                 input_buffer[i]->set_offset(0);
415                         }
416                         input_size += fragment_size;
417                 }
418
419
420                 double current_slope = (next_target - previous_target) /
421                         target_samples;
422                 double *trigger_buffer = input_buffer[trigger]->get_data();
423                 for(int i = 0; i < size; i++)
424                 {
425 // Get slope from current sample to every sample in preview_samples.
426 // Take highest one or first one after target_samples are up.
427
428 // For optimization, calculate the first slope we really need.
429 // Assume every slope up to the end of preview_samples has been calculated and
430 // found <= to current slope.
431             int first_slope = preview_samples - 1;
432 // Need new slope immediately
433                         if(target_current_sample >= target_samples)
434                                 first_slope = 1;
435                         for(int j = first_slope;
436                                 j < preview_samples;
437                                 j++)
438                         {
439                                 double sample = 0.;
440                                 switch(config.input)
441                                 {
442                                         case CompressorConfig::MAX:
443                                         {
444                                                 double max = 0;
445                                                 for(int k = 0; k < total_buffers; k++)
446                                                 {
447                                                         sample = fabs(input_buffer[k]->get_data()[i + j]);
448                                                         if(sample > max) max = sample;
449                                                 }
450                                                 sample = max;
451                                                 break;
452                                         }
453
454                                         case CompressorConfig::TRIGGER:
455                                                 sample = fabs(trigger_buffer[i + j]);
456                                                 break;
457
458                                         case CompressorConfig::SUM:
459                                         {
460                                                 double max = 0;
461                                                 for(int k = 0; k < total_buffers; k++)
462                                                 {
463                                                         sample = fabs(input_buffer[k]->get_data()[i + j]);
464                                                         max += sample;
465                                                 }
466                                                 sample = max;
467                                                 break;
468                                         }
469                                 }
470
471
472
473
474
475
476                                 double new_slope = (sample - current_value) /
477                                         j;
478 // Got equal or higher slope
479                                 if(new_slope >= current_slope &&
480                                         (current_slope >= 0 ||
481                                         new_slope >= 0))
482                                 {
483                                         target_current_sample = 0;
484                                         target_samples = j;
485                                         current_slope = new_slope;
486                                         next_target = sample;
487                                         previous_target = current_value;
488                                 }
489                                 else
490                                 if(sample > next_target && current_slope < 0)
491                                 {
492                                         target_current_sample = 0;
493                                         target_samples = decay_samples;
494                                         current_slope = (sample - current_value) /
495                                                 decay_samples;
496                                         next_target = sample;
497                                         previous_target = current_value;
498                                 }
499
500 // Hit end of current slope range without finding higher slope
501                                 if(target_current_sample >= target_samples)
502                                 {
503                                         target_current_sample = 0;
504                                         target_samples = decay_samples;
505                                         current_slope = (sample - current_value) / decay_samples;
506                                         next_target = sample;
507                                         previous_target = current_value;
508                                 }
509                         }
510
511 // Update current value and multiply gain
512                         current_value = (next_target * target_current_sample +
513                                 previous_target * (target_samples - target_current_sample)) /
514                                 target_samples;
515 //buffer[0][i] = current_value;
516                         target_current_sample++;
517
518                         if(config.smoothing_only)
519                         {
520                                 for(int j = 0; j < total_buffers; j++)
521                                 {
522                                         buffer[j]->get_data()[i] = current_value;
523                                 }
524                         }
525                         else
526                         {
527                                 double gain = calculate_gain(current_value);
528                                 for(int j = 0; j < total_buffers; j++)
529                                 {
530                                         buffer[j]->get_data()[i] = input_buffer[j]->get_data()[i] * gain;
531                                 }
532                         }
533                 }
534
535
536
537         }
538
539
540
541
542
543         return 0;
544 }
545
546 double CompressorEffect::calculate_output(double x)
547 {
548         if(x > 0.999) return 1.0;
549
550         for(int i = levels.total - 1; i >= 0; i--)
551         {
552                 if(levels.values[i].x <= x)
553                 {
554                         if(i < levels.total - 1)
555                         {
556                                 return levels.values[i].y +
557                                         (x - levels.values[i].x) *
558                                         (levels.values[i + 1].y - levels.values[i].y) /
559                                         (levels.values[i + 1].x - levels.values[i].x);
560                         }
561                         else
562                         {
563                                 return levels.values[i].y +
564                                         (x - levels.values[i].x) *
565                                         (max_y - levels.values[i].y) /
566                                         (max_x - levels.values[i].x);
567                         }
568                 }
569         }
570
571         if(levels.total)
572         {
573                 return min_y +
574                         (x - min_x) *
575                         (levels.values[0].y - min_y) /
576                         (levels.values[0].x - min_x);
577         }
578         else
579                 return x;
580 }
581
582
583 double CompressorEffect::calculate_gain(double input)
584 {
585 //      double x_db = DB::todb(input);
586 //      double y_db = config.calculate_db(x_db);
587 //      double y_linear = DB::fromdb(y_db);
588         double y_linear = calculate_output(input);
589         double gain;
590         if(input != 0)
591                 gain = y_linear / input;
592         else
593                 gain = 100000;
594         return gain;
595 }
596
597
598
599
600
601
602
603
604
605
606 CompressorConfig::CompressorConfig()
607 {
608         reaction_len = 1.0;
609         min_db = -80.0;
610         min_x = min_db;
611         min_y = min_db;
612         max_x = 0;
613         max_y = 0;
614         trigger = 0;
615         input = CompressorConfig::TRIGGER;
616         smoothing_only = 0;
617         decay_len = 1.0;
618 }
619
620 void CompressorConfig::copy_from(CompressorConfig &that)
621 {
622         this->reaction_len = that.reaction_len;
623         this->decay_len = that.decay_len;
624         this->min_db = that.min_db;
625         this->min_x = that.min_x;
626         this->min_y = that.min_y;
627         this->max_x = that.max_x;
628         this->max_y = that.max_y;
629         this->trigger = that.trigger;
630         this->input = that.input;
631         this->smoothing_only = that.smoothing_only;
632         levels.remove_all();
633         for(int i = 0; i < that.levels.total; i++)
634                 this->levels.append(that.levels.values[i]);
635 }
636
637 int CompressorConfig::equivalent(CompressorConfig &that)
638 {
639         if(!EQUIV(this->reaction_len, that.reaction_len) ||
640                 !EQUIV(this->decay_len, that.decay_len) ||
641                 this->trigger != that.trigger ||
642                 this->input != that.input ||
643                 this->smoothing_only != that.smoothing_only)
644                 return 0;
645         if(this->levels.total != that.levels.total) return 0;
646         for(int i = 0;
647                 i < this->levels.total && i < that.levels.total;
648                 i++)
649         {
650                 compressor_point_t *this_level = &this->levels.values[i];
651                 compressor_point_t *that_level = &that.levels.values[i];
652                 if(!EQUIV(this_level->x, that_level->x) ||
653                         !EQUIV(this_level->y, that_level->y))
654                         return 0;
655         }
656         return 1;
657 }
658
659 void CompressorConfig::interpolate(CompressorConfig &prev,
660         CompressorConfig &next,
661         int64_t prev_frame,
662         int64_t next_frame,
663         int64_t current_frame)
664 {
665         copy_from(prev);
666 }
667
668 int CompressorConfig::total_points()
669 {
670         if(!levels.total)
671                 return 1;
672         else
673                 return levels.total;
674 }
675
676 void CompressorConfig::dump()
677 {
678         printf("CompressorConfig::dump\n");
679         for(int i = 0; i < levels.total; i++)
680         {
681                 printf("        %f %f\n", levels.values[i].x, levels.values[i].y);
682         }
683 }
684
685
686 double CompressorConfig::get_y(int number)
687 {
688         if(!levels.total)
689                 return 1.0;
690         else
691         if(number >= levels.total)
692                 return levels.values[levels.total - 1].y;
693         else
694                 return levels.values[number].y;
695 }
696
697 double CompressorConfig::get_x(int number)
698 {
699         if(!levels.total)
700                 return 0.0;
701         else
702         if(number >= levels.total)
703                 return levels.values[levels.total - 1].x;
704         else
705                 return levels.values[number].x;
706 }
707
708 double CompressorConfig::calculate_db(double x)
709 {
710         if(x > -0.001) return 0.0;
711
712         for(int i = levels.total - 1; i >= 0; i--)
713         {
714                 if(levels.values[i].x <= x)
715                 {
716                         if(i < levels.total - 1)
717                         {
718                                 return levels.values[i].y +
719                                         (x - levels.values[i].x) *
720                                         (levels.values[i + 1].y - levels.values[i].y) /
721                                         (levels.values[i + 1].x - levels.values[i].x);
722                         }
723                         else
724                         {
725                                 return levels.values[i].y +
726                                         (x - levels.values[i].x) *
727                                         (max_y - levels.values[i].y) /
728                                         (max_x - levels.values[i].x);
729                         }
730                 }
731         }
732
733         if(levels.total)
734         {
735                 return min_y +
736                         (x - min_x) *
737                         (levels.values[0].y - min_y) /
738                         (levels.values[0].x - min_x);
739         }
740         else
741                 return x;
742 }
743
744
745 int CompressorConfig::set_point(double x, double y)
746 {
747         for(int i = levels.total - 1; i >= 0; i--)
748         {
749                 if(levels.values[i].x < x)
750                 {
751                         levels.append();
752                         i++;
753                         for(int j = levels.total - 2; j >= i; j--)
754                         {
755                                 levels.values[j + 1] = levels.values[j];
756                         }
757                         levels.values[i].x = x;
758                         levels.values[i].y = y;
759
760                         return i;
761                 }
762         }
763
764         levels.append();
765         for(int j = levels.total - 2; j >= 0; j--)
766         {
767                 levels.values[j + 1] = levels.values[j];
768         }
769         levels.values[0].x = x;
770         levels.values[0].y = y;
771         return 0;
772 }
773
774 void CompressorConfig::remove_point(int number)
775 {
776         for(int j = number; j < levels.total - 1; j++)
777         {
778                 levels.values[j] = levels.values[j + 1];
779         }
780         levels.remove();
781 }
782
783 void CompressorConfig::optimize()
784 {
785         int done = 0;
786
787         while(!done)
788         {
789                 done = 1;
790
791
792                 for(int i = 0; i < levels.total - 1; i++)
793                 {
794                         if(levels.values[i].x >= levels.values[i + 1].x)
795                         {
796                                 done = 0;
797                                 for(int j = i + 1; j < levels.total - 1; j++)
798                                 {
799                                         levels.values[j] = levels.values[j + 1];
800                                 }
801                                 levels.remove();
802                         }
803                 }
804
805         }
806 }
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834 CompressorWindow::CompressorWindow(CompressorEffect *plugin)
835  : PluginClientWindow(plugin,
836         xS(650),
837         yS(480),
838         xS(650),
839         yS(480),
840         0)
841 {
842         this->plugin = plugin;
843 }
844
845 void CompressorWindow::create_objects()
846 {
847         int xs10 = xS(10), xs20 = xS(20), xs50 = xS(50), xs110 = xS(110);
848         int ys20 = yS(20), ys30 = yS(30), ys40 = yS(40), ys60 = yS(60), ys70 = yS(70);
849         int x = xS(35), y = yS(10);
850         int control_margin = xS(130);
851
852         add_subwindow(canvas = new CompressorCanvas(plugin,
853                 x,
854                 y,
855                 get_w() - x - control_margin - xs10,
856                 get_h() - y - ys70));
857         canvas->set_cursor(CROSS_CURSOR, 0, 0);
858         x = get_w() - control_margin;
859         add_subwindow(new BC_Title(x, y, _("Reaction secs:")));
860         y += ys20;
861         add_subwindow(reaction = new CompressorReaction(plugin, x, y));
862         y += ys30;
863         add_subwindow(new BC_Title(x, y, _("Decay secs:")));
864         y += ys20;
865         add_subwindow(decay = new CompressorDecay(plugin, x, y));
866         y += ys30;
867         add_subwindow(new BC_Title(x, y, _("Trigger Type:")));
868         y += ys20;
869         add_subwindow(input = new CompressorInput(plugin, x, y));
870         input->create_objects();
871         y += ys30;
872         add_subwindow(new BC_Title(x, y, _("Trigger:")));
873         y += ys20;
874         add_subwindow(trigger = new CompressorTrigger(plugin, x, y));
875         if(plugin->config.input != CompressorConfig::TRIGGER) trigger->disable();
876         y += ys30;
877         add_subwindow(smooth = new CompressorSmooth(plugin, x, y));
878         y += ys60;
879         add_subwindow(clear = new CompressorClear(plugin, x, y));
880         x = xs10;
881         y = get_h() - ys40;
882         add_subwindow(new BC_Title(x, y, _("Point:")));
883         x += xs50;
884         add_subwindow(x_text = new CompressorX(plugin, x, y));
885         x += xs110;
886         add_subwindow(new BC_Title(x, y, _("x")));
887         x += xs20;
888         add_subwindow(y_text = new CompressorY(plugin, x, y));
889         draw_scales();
890
891         update_canvas();
892         show_window();
893 }
894
895 void CompressorWindow::draw_scales()
896 {
897         draw_3d_border(canvas->get_x() - 2,
898                 canvas->get_y() - 2,
899                 canvas->get_w() + 4,
900                 canvas->get_h() + 4,
901                 get_bg_color(),
902                 BLACK,
903                 MDGREY,
904                 get_bg_color());
905
906
907         set_font(SMALLFONT);
908         set_color(get_resources()->default_text_color);
909
910 #define DIVISIONS 8
911         for(int i = 0; i <= DIVISIONS; i++)
912         {
913                 int y = canvas->get_y() + yS(10) + canvas->get_h() / DIVISIONS * i;
914                 int x = canvas->get_x() - xS(30);
915                 char string[BCTEXTLEN];
916
917                 sprintf(string, "%.0f", (float)i / DIVISIONS * plugin->config.min_db);
918                 draw_text(x, y, string);
919
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++)
923                 {
924                         y = y1 + (y2 - y1) * j / 10;
925                         if(j == 0)
926                         {
927                                 draw_line(canvas->get_x() - xS(10), y, canvas->get_x(), y);
928                         }
929                         else
930                         if(i < DIVISIONS)
931                         {
932                                 draw_line(canvas->get_x() - xS(5), y, canvas->get_x(), y);
933                         }
934                 }
935         }
936
937
938         for(int i = 0; i <= DIVISIONS; i++)
939         {
940                 int y = canvas->get_h() + yS(30);
941                 int x = canvas->get_x() + (canvas->get_w() - xS(10)) / DIVISIONS * i;
942                 char string[BCTEXTLEN];
943
944                 sprintf(string, "%.0f", (1.0 - (float)i / DIVISIONS) * plugin->config.min_db);
945                 draw_text(x, y, string);
946
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++)
950                 {
951                         x = x1 + (x2 - x1) * j / 10;
952                         if(j == 0)
953                         {
954                                 draw_line(x, canvas->get_y() + canvas->get_h(), x, canvas->get_y() + canvas->get_h() + yS(10));
955                         }
956                         else
957                         if(i < DIVISIONS)
958                         {
959                                 draw_line(x, canvas->get_y() + canvas->get_h(), x, canvas->get_y() + canvas->get_h() + yS(5));
960                         }
961                 }
962         }
963
964
965
966         flash();
967 }
968
969 void CompressorWindow::update()
970 {
971         update_textboxes();
972         update_canvas();
973 }
974
975 void CompressorWindow::update_textboxes()
976 {
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));
981
982         if(plugin->config.input != CompressorConfig::TRIGGER && trigger->get_enabled())
983                 trigger->disable();
984         else
985         if(plugin->config.input == CompressorConfig::TRIGGER && !trigger->get_enabled())
986                 trigger->enable();
987
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)
994         {
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);
997         }
998 }
999
1000 #define POINT_W xS(10)
1001 void CompressorWindow::update_canvas()
1002 {
1003         canvas->clear_box(0, 0, canvas->get_w(), canvas->get_h());
1004         canvas->set_line_dashes(1);
1005         canvas->set_color(GREEN);
1006
1007         for(int i = 1; i < DIVISIONS; i++)
1008         {
1009                 int y = canvas->get_h() * i / DIVISIONS;
1010                 canvas->draw_line(0, y, canvas->get_w(), y);
1011
1012                 int x = canvas->get_w() * i / DIVISIONS;
1013                 canvas->draw_line(x, 0, x, canvas->get_h());
1014         }
1015         canvas->set_line_dashes(0);
1016
1017
1018         canvas->set_font(MEDIUMFONT);
1019         canvas->draw_text(plugin->get_theme()->widget_border,
1020                 canvas->get_h() / 2,
1021                 _("Output"));
1022         canvas->draw_text(canvas->get_w() / 2 - canvas->get_text_width(MEDIUMFONT, _("Input")) / 2,
1023                 canvas->get_h() - plugin->get_theme()->widget_border,
1024                 _("Input"));
1025
1026
1027         canvas->set_color(WHITE);
1028         canvas->set_line_width(2);
1029
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());
1033
1034         for(int i=1; i<canvas->get_w(); i++)
1035         {
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);
1040                 y1 = y2;
1041         }
1042         canvas->set_line_width(1);
1043
1044         int total = plugin->config.levels.total ? plugin->config.levels.total : 1;
1045         for(int i=0; i < total; i++)
1046         {
1047                 x_db = plugin->config.get_x(i);
1048                 y_db = plugin->config.get_y(i);
1049
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());
1052
1053                 canvas->draw_box(x - POINT_W / 2, y - POINT_W / 2, POINT_W, POINT_W);
1054         }
1055
1056         canvas->flash();
1057 }
1058
1059 int CompressorWindow::resize_event(int w, int h)
1060 {
1061         return 1;
1062 }
1063
1064
1065
1066
1067
1068
1069
1070
1071 CompressorCanvas::CompressorCanvas(CompressorEffect *plugin, int x, int y, int w, int h)
1072  : BC_SubWindow(x, y, w, h, BLACK)
1073 {
1074         this->plugin = plugin;
1075         current_operation = NONE;
1076         current_point = 0;
1077 }
1078
1079 int CompressorCanvas::button_press_event()
1080 {
1081 // Check existing points
1082         if(is_event_win() && cursor_inside())
1083         {
1084                 for(int i = 0; i < plugin->config.levels.total; i++)
1085                 {
1086                         double x_db = plugin->config.get_x(i);
1087                         double y_db = plugin->config.get_y(i);
1088
1089                         int x = (int)(((double)1 - x_db / plugin->config.min_db) * get_w());
1090                         int y = (int)(y_db / plugin->config.min_db * get_h());
1091
1092                         if(get_cursor_x() < x + POINT_W / 2 && get_cursor_x() >= x - POINT_W / 2 &&
1093                                 get_cursor_y() < y + POINT_W / 2 && get_cursor_y() >= y - POINT_W / 2)
1094                         {
1095                                 current_operation = DRAG;
1096                                 current_point = i;
1097                                 return 1;
1098                         }
1099                 }
1100
1101
1102
1103 // Create new point
1104                 double x_db = (double)(1 - (double)get_cursor_x() / get_w()) * plugin->config.min_db;
1105                 double y_db = (double)get_cursor_y() / get_h() * plugin->config.min_db;
1106
1107                 current_point = plugin->config.set_point(x_db, y_db);
1108                 current_operation = DRAG;
1109                 ((CompressorWindow*)plugin->thread->window)->update();
1110                 plugin->send_configure_change();
1111                 return 1;
1112         }
1113         return 0;
1114 //plugin->config.dump();
1115 }
1116
1117 int CompressorCanvas::button_release_event()
1118 {
1119         if(current_operation == DRAG)
1120         {
1121                 if(current_point > 0)
1122                 {
1123                         if(plugin->config.levels.values[current_point].x <
1124                                 plugin->config.levels.values[current_point - 1].x)
1125                                 plugin->config.remove_point(current_point);
1126                 }
1127
1128                 if(current_point < plugin->config.levels.total - 1)
1129                 {
1130                         if(plugin->config.levels.values[current_point].x >=
1131                                 plugin->config.levels.values[current_point + 1].x)
1132                                 plugin->config.remove_point(current_point);
1133                 }
1134
1135                 ((CompressorWindow*)plugin->thread->window)->update();
1136                 plugin->send_configure_change();
1137                 current_operation = NONE;
1138                 return 1;
1139         }
1140
1141         return 0;
1142 }
1143
1144 int CompressorCanvas::cursor_motion_event()
1145 {
1146         if(current_operation == DRAG)
1147         {
1148                 int x = get_cursor_x();
1149                 int y = get_cursor_y();
1150                 CLAMP(x, 0, get_w());
1151                 CLAMP(y, 0, get_h());
1152                 double x_db = (double)(1 - (double)x / get_w()) * plugin->config.min_db;
1153                 double y_db = (double)y / get_h() * plugin->config.min_db;
1154                 plugin->config.levels.values[current_point].x = x_db;
1155                 plugin->config.levels.values[current_point].y = y_db;
1156                 ((CompressorWindow*)plugin->thread->window)->update();
1157                 plugin->send_configure_change();
1158                 return 1;
1159 //plugin->config.dump();
1160         }
1161         else
1162 // Change cursor over points
1163         if(is_event_win() && cursor_inside())
1164         {
1165                 int new_cursor = CROSS_CURSOR;
1166
1167                 for(int i = 0; i < plugin->config.levels.total; i++)
1168                 {
1169                         double x_db = plugin->config.get_x(i);
1170                         double y_db = plugin->config.get_y(i);
1171
1172                         int x = (int)(((double)1 - x_db / plugin->config.min_db) * get_w());
1173                         int y = (int)(y_db / plugin->config.min_db * get_h());
1174
1175                         if(get_cursor_x() < x + POINT_W / 2 && get_cursor_x() >= x - POINT_W / 2 &&
1176                                 get_cursor_y() < y + POINT_W / 2 && get_cursor_y() >= y - POINT_W / 2)
1177                         {
1178                                 new_cursor = UPRIGHT_ARROW_CURSOR;
1179                                 break;
1180                         }
1181                 }
1182
1183
1184                 if(new_cursor != get_cursor())
1185                 {
1186                         set_cursor(new_cursor, 0, 1);
1187                 }
1188         }
1189         return 0;
1190 }
1191
1192
1193
1194
1195
1196 CompressorReaction::CompressorReaction(CompressorEffect *plugin, int x, int y)
1197  : BC_TextBox(x, y, xS(100), 1, (float)plugin->config.reaction_len)
1198 {
1199         this->plugin = plugin;
1200 }
1201
1202 int CompressorReaction::handle_event()
1203 {
1204         plugin->config.reaction_len = atof(get_text());
1205         plugin->send_configure_change();
1206         return 1;
1207 }
1208
1209 int CompressorReaction::button_press_event()
1210 {
1211         if(is_event_win())
1212         {
1213                 if(get_buttonpress() < 4) return BC_TextBox::button_press_event();
1214                 if(get_buttonpress() == 4)
1215                 {
1216                         plugin->config.reaction_len += 0.1;
1217                 }
1218                 else
1219                 if(get_buttonpress() == 5)
1220                 {
1221                         plugin->config.reaction_len -= 0.1;
1222                 }
1223                 update((float)plugin->config.reaction_len);
1224                 plugin->send_configure_change();
1225                 return 1;
1226         }
1227         return 0;
1228 }
1229
1230 CompressorDecay::CompressorDecay(CompressorEffect *plugin, int x, int y)
1231  : BC_TextBox(x, y, xS(100), 1, (float)plugin->config.decay_len)
1232 {
1233         this->plugin = plugin;
1234 }
1235 int CompressorDecay::handle_event()
1236 {
1237         plugin->config.decay_len = atof(get_text());
1238         plugin->send_configure_change();
1239         return 1;
1240 }
1241
1242 int CompressorDecay::button_press_event()
1243 {
1244         if(is_event_win())
1245         {
1246                 if(get_buttonpress() < 4) return BC_TextBox::button_press_event();
1247                 if(get_buttonpress() == 4)
1248                 {
1249                         plugin->config.decay_len += 0.1;
1250                 }
1251                 else
1252                 if(get_buttonpress() == 5)
1253                 {
1254                         plugin->config.decay_len -= 0.1;
1255                 }
1256                 update((float)plugin->config.decay_len);
1257                 plugin->send_configure_change();
1258                 return 1;
1259         }
1260         return 0;
1261 }
1262
1263
1264
1265 CompressorX::CompressorX(CompressorEffect *plugin, int x, int y)
1266  : BC_TextBox(x, y, xS(100), 1, "")
1267 {
1268         this->plugin = plugin;
1269 }
1270 int CompressorX::handle_event()
1271 {
1272         int current_point = ((CompressorWindow*)plugin->thread->window)->canvas->current_point;
1273         if(current_point < plugin->config.levels.total)
1274         {
1275                 plugin->config.levels.values[current_point].x = atof(get_text());
1276                 ((CompressorWindow*)plugin->thread->window)->update_canvas();
1277                 plugin->send_configure_change();
1278         }
1279         return 1;
1280 }
1281
1282
1283
1284 CompressorY::CompressorY(CompressorEffect *plugin, int x, int y)
1285  : BC_TextBox(x, y, xS(100), 1, "")
1286 {
1287         this->plugin = plugin;
1288 }
1289 int CompressorY::handle_event()
1290 {
1291         int current_point = ((CompressorWindow*)plugin->thread->window)->canvas->current_point;
1292         if(current_point < plugin->config.levels.total)
1293         {
1294                 plugin->config.levels.values[current_point].y = atof(get_text());
1295                 ((CompressorWindow*)plugin->thread->window)->update_canvas();
1296                 plugin->send_configure_change();
1297         }
1298         return 1;
1299 }
1300
1301
1302
1303
1304
1305 CompressorTrigger::CompressorTrigger(CompressorEffect *plugin, int x, int y)
1306  : BC_TextBox(x, y, xS(100), 1, (int64_t)plugin->config.trigger)
1307 {
1308         this->plugin = plugin;
1309 }
1310 int CompressorTrigger::handle_event()
1311 {
1312         plugin->config.trigger = atol(get_text());
1313         plugin->send_configure_change();
1314         return 1;
1315 }
1316
1317 int CompressorTrigger::button_press_event()
1318 {
1319         if(is_event_win())
1320         {
1321                 if(get_buttonpress() < 4) return BC_TextBox::button_press_event();
1322                 if(get_buttonpress() == 4)
1323                 {
1324                         plugin->config.trigger++;
1325                 }
1326                 else
1327                 if(get_buttonpress() == 5)
1328                 {
1329                         plugin->config.trigger--;
1330                 }
1331                 update((int64_t)plugin->config.trigger);
1332                 plugin->send_configure_change();
1333                 return 1;
1334         }
1335         return 0;
1336 }
1337
1338
1339
1340
1341
1342 CompressorInput::CompressorInput(CompressorEffect *plugin, int x, int y)
1343  : BC_PopupMenu(x,
1344         y,
1345         xS(120),
1346         CompressorInput::value_to_text(plugin->config.input),
1347         1)
1348 {
1349         this->plugin = plugin;
1350 }
1351 int CompressorInput::handle_event()
1352 {
1353         plugin->config.input = text_to_value(get_text());
1354         ((CompressorWindow*)plugin->thread->window)->update();
1355         plugin->send_configure_change();
1356         return 1;
1357 }
1358
1359 void CompressorInput::create_objects()
1360 {
1361         for(int i = 0; i < 3; i++)
1362         {
1363                 add_item(new BC_MenuItem(value_to_text(i)));
1364         }
1365 }
1366
1367 const char* CompressorInput::value_to_text(int value)
1368 {
1369         switch(value)
1370         {
1371                 case CompressorConfig::TRIGGER: return _("Trigger");
1372                 case CompressorConfig::MAX: return _("Maximum");
1373                 case CompressorConfig::SUM: return _("Total");
1374         }
1375
1376         return _("Trigger");
1377 }
1378
1379 int CompressorInput::text_to_value(char *text)
1380 {
1381         for(int i = 0; i < 3; i++)
1382         {
1383                 if(!strcmp(value_to_text(i), text)) return i;
1384         }
1385
1386         return CompressorConfig::TRIGGER;
1387 }
1388
1389
1390
1391
1392
1393
1394 CompressorClear::CompressorClear(CompressorEffect *plugin, int x, int y)
1395  : BC_GenericButton(x, y, _("Clear"))
1396 {
1397         this->plugin = plugin;
1398 }
1399
1400 int CompressorClear::handle_event()
1401 {
1402         plugin->config.levels.remove_all();
1403 //plugin->config.dump();
1404         ((CompressorWindow*)plugin->thread->window)->update();
1405         plugin->send_configure_change();
1406         return 1;
1407 }
1408
1409
1410
1411 CompressorSmooth::CompressorSmooth(CompressorEffect *plugin, int x, int y)
1412  : BC_CheckBox(x, y, plugin->config.smoothing_only, _("Smooth only"))
1413 {
1414         this->plugin = plugin;
1415 }
1416
1417 int CompressorSmooth::handle_event()
1418 {
1419         plugin->config.smoothing_only = get_value();
1420         plugin->send_configure_change();
1421         return 1;
1422 }
1423
1424
1425
1426