igor ru.po
[goodguy/history.git] / cinelerra-5.1 / plugins / denoisefft / denoisefft.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 "clip.h"
24 #include "bchash.h"
25 #include "filesystem.h"
26 #include "filexml.h"
27 #include "guicast.h"
28 #include "language.h"
29 #include "mutex.h"
30 #include "fourier.h"
31 #include "pluginaclient.h"
32 #include "transportque.inc"
33 #include "units.h"
34 #include "vframe.h"
35
36 #include <errno.h>
37 #include <math.h>
38 #include <string.h>
39
40
41
42 #define WINDOW_SIZE 16384
43
44
45 // Noise collection is done either from the start of the effect or the start
46 // of the previous keyframe.  It always covers the higher numbered samples
47 // after the keyframe.
48
49 class DenoiseFFTEffect;
50 class DenoiseFFTWindow;
51
52
53
54
55 class DenoiseFFTConfig
56 {
57 public:
58         DenoiseFFTConfig();
59
60         int samples;
61         double level;
62 };
63
64 class DenoiseFFTLevel : public BC_FPot
65 {
66 public:
67         DenoiseFFTLevel(DenoiseFFTEffect *plugin, int x, int y);
68         int handle_event();
69         DenoiseFFTEffect *plugin;
70 };
71
72 class DenoiseFFTSamples : public BC_PopupMenu
73 {
74 public:
75         DenoiseFFTSamples(DenoiseFFTEffect *plugin, int x, int y, char *text);
76         int handle_event();
77         DenoiseFFTEffect *plugin;
78 };
79
80 class DenoiseFFTWindow : public PluginClientWindow
81 {
82 public:
83         DenoiseFFTWindow(DenoiseFFTEffect *plugin);
84         void create_objects();
85         DenoiseFFTLevel *level;
86         DenoiseFFTSamples *samples;
87         DenoiseFFTEffect *plugin;
88 };
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104 class DenoiseFFTRemove : public CrossfadeFFT
105 {
106 public:
107         DenoiseFFTRemove(DenoiseFFTEffect *plugin);
108         int signal_process();
109         int read_samples(int64_t output_sample,
110                 int samples,
111                 Samples *buffer);
112         DenoiseFFTEffect *plugin;
113 };
114
115 class DenoiseFFTCollect : public CrossfadeFFT
116 {
117 public:
118         DenoiseFFTCollect(DenoiseFFTEffect *plugin);
119         int signal_process();
120         int read_samples(int64_t output_sample,
121                 int samples,
122                 Samples *buffer);
123         DenoiseFFTEffect *plugin;
124 };
125
126 class DenoiseFFTEffect : public PluginAClient
127 {
128 public:
129         DenoiseFFTEffect(PluginServer *server);
130         ~DenoiseFFTEffect();
131
132         int is_realtime();
133         void read_data(KeyFrame *keyframe);
134         void save_data(KeyFrame *keyframe);
135         int process_buffer(int64_t size,
136                 Samples *buffer,
137                 int64_t start_position,
138                 int sample_rate);
139         void collect_noise();
140
141
142
143         void reset();
144         void update_gui();
145
146         void process_window();
147
148
149         PLUGIN_CLASS_MEMBERS(DenoiseFFTConfig)
150
151 // Need to sample noise now.
152         int need_collection;
153 // Start of sample of noise to collect
154         int64_t collection_sample;
155         double *reference;
156         DenoiseFFTRemove *remove_engine;
157         DenoiseFFTCollect *collect_engine;
158 };
159
160
161
162
163
164
165
166
167
168
169 REGISTER_PLUGIN(DenoiseFFTEffect)
170
171
172
173
174
175
176
177
178
179 DenoiseFFTConfig::DenoiseFFTConfig()
180 {
181         samples = WINDOW_SIZE;
182         level = 0.0;
183 }
184
185
186
187
188
189
190
191
192
193
194
195
196 DenoiseFFTLevel::DenoiseFFTLevel(DenoiseFFTEffect *plugin, int x, int y)
197  : BC_FPot(x, y, (float)plugin->config.level, INFINITYGAIN, 6.0)
198 {
199         this->plugin = plugin;
200         set_precision(0.1);
201 }
202
203 int DenoiseFFTLevel::handle_event()
204 {
205         plugin->config.level = get_value();
206         plugin->send_configure_change();
207         return 1;
208 }
209
210 DenoiseFFTSamples::DenoiseFFTSamples(DenoiseFFTEffect *plugin,
211         int x,
212         int y,
213         char *text)
214  : BC_PopupMenu(x, y, 100, text, 1)
215 {
216         this->plugin = plugin;
217 }
218
219 int DenoiseFFTSamples::handle_event()
220 {
221         plugin->config.samples = atol(get_text());
222         plugin->send_configure_change();
223         return 1;
224 }
225
226
227
228 DenoiseFFTWindow::DenoiseFFTWindow(DenoiseFFTEffect *plugin)
229  : PluginClientWindow(plugin,
230         300,
231         130,
232         300,
233         130,
234         0)
235 {
236         this->plugin = plugin;
237 }
238
239 void DenoiseFFTWindow::create_objects()
240 {
241         int x = 10, y = 10;
242
243         add_subwindow(new BC_Title(x, y, _("Denoise power:")));
244         add_subwindow(level = new DenoiseFFTLevel(plugin, x + 130, y));
245         y += level->get_h() + 10;
246         add_subwindow(new BC_Title(x, y, _("Number of samples for reference:")));
247         y += 20;
248         add_subwindow(new BC_Title(x, y, _("The keyframe is the start of the reference")));
249         y += 20;
250
251         char string[BCTEXTLEN];
252         sprintf(string, "%d\n", plugin->config.samples);
253         add_subwindow(samples = new DenoiseFFTSamples(plugin, x + 100, y, string));
254         for(int i = WINDOW_SIZE; i < 0x100000; )
255         {
256                 sprintf(string, "%d", i);
257                 samples->add_item(new BC_MenuItem(string));
258                 i *= 2;
259         }
260         show_window();
261         flush();
262 }
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283 DenoiseFFTEffect::DenoiseFFTEffect(PluginServer *server)
284  : PluginAClient(server)
285 {
286         reset();
287
288 }
289
290 DenoiseFFTEffect::~DenoiseFFTEffect()
291 {
292
293         if(reference) delete [] reference;
294         if(remove_engine) delete remove_engine;
295         if(collect_engine) delete collect_engine;
296 }
297
298 NEW_WINDOW_MACRO(DenoiseFFTEffect, DenoiseFFTWindow)
299
300
301 void DenoiseFFTEffect::reset()
302 {
303         reference = 0;
304         remove_engine = 0;
305         collect_engine = 0;
306         need_collection = 1;
307         collection_sample = 0;
308 }
309
310 int DenoiseFFTEffect::is_realtime() { return 1; }
311 const char* DenoiseFFTEffect::plugin_title() { return _("DenoiseFFT"); }
312
313
314
315
316
317 void DenoiseFFTEffect::read_data(KeyFrame *keyframe)
318 {
319         FileXML input;
320         input.set_shared_input(keyframe->get_data(), strlen(keyframe->get_data()));
321
322         int result = 0;
323         while(!result)
324         {
325                 result = input.read_tag();
326
327                 if(!result)
328                 {
329                         if(input.tag.title_is("DENOISEFFT"))
330                         {
331                                 config.samples = input.tag.get_property("SAMPLES", config.samples);
332                                 config.level = input.tag.get_property("LEVEL", config.level);
333                         }
334                 }
335         }
336 }
337
338 void DenoiseFFTEffect::save_data(KeyFrame *keyframe)
339 {
340         FileXML output;
341         output.set_shared_output(keyframe->get_data(), MESSAGESIZE);
342
343         output.tag.set_title("DENOISEFFT");
344         output.tag.set_property("SAMPLES", config.samples);
345         output.tag.set_property("LEVEL", config.level);
346         output.append_tag();
347         output.tag.set_title("/DENOISEFFT");
348         output.append_tag();
349         output.append_newline();
350         output.terminate_string();
351 }
352
353
354 void DenoiseFFTEffect::update_gui()
355 {
356         if(thread)
357         {
358                 load_configuration();
359                 ((DenoiseFFTWindow*)thread->window)->lock_window();
360                 ((DenoiseFFTWindow*)thread->window)->level->update(config.level);
361                 char string[BCTEXTLEN];
362                 sprintf(string, "%d", config.samples);
363                 ((DenoiseFFTWindow*)thread->window)->samples->set_text(string);
364                 ((DenoiseFFTWindow*)thread->window)->unlock_window();
365         }
366 }
367
368 int DenoiseFFTEffect::load_configuration()
369 {
370         KeyFrame *prev_keyframe = get_prev_keyframe(get_source_position());
371         int64_t prev_position = edl_to_local(prev_keyframe->position);
372         read_data(prev_keyframe);
373         if(prev_position == 0) prev_position = get_source_start();
374
375         if(prev_position != collection_sample)
376         {
377                 collection_sample = prev_position;
378                 need_collection = 1;
379         }
380         return 0;
381 }
382
383 int DenoiseFFTEffect::process_buffer(int64_t size,
384                 Samples *buffer,
385                 int64_t start_position,
386                 int sample_rate)
387 {
388         load_configuration();
389
390 // Do noise collection
391         if(need_collection)
392         {
393                 need_collection = 0;
394                 collect_noise();
395         }
396
397 // Remove noise
398         if(!remove_engine)
399         {
400                 remove_engine = new DenoiseFFTRemove(this);
401                 remove_engine->initialize(WINDOW_SIZE);
402         }
403         remove_engine->process_buffer(start_position,
404                 size,
405                 buffer,
406                 get_direction());
407
408         return 0;
409 }
410
411
412 void DenoiseFFTEffect::collect_noise()
413 {
414         if(!reference) reference = new double[WINDOW_SIZE / 2];
415         if(!collect_engine)
416         {
417                 collect_engine = new DenoiseFFTCollect(this);
418                 collect_engine->initialize(WINDOW_SIZE);
419         }
420         bzero(reference, sizeof(double) * WINDOW_SIZE / 2);
421
422         int64_t collection_start = collection_sample;
423         int step = 1;
424         int total_windows = 0;
425
426         if(get_direction() == PLAY_REVERSE)
427         {
428                 collection_start += config.samples;
429                 step = -1;
430         }
431
432         for(int i = 0; i < config.samples; i += WINDOW_SIZE)
433         {
434                 collect_engine->process_buffer(collection_start,
435                         WINDOW_SIZE,
436                         0,
437                         get_direction());
438
439                 collection_start += step * WINDOW_SIZE;
440                 total_windows++;
441         }
442
443         for(int i = 0; i < WINDOW_SIZE / 2; i++)
444         {
445                 reference[i] /= total_windows;
446         }
447 }
448
449
450
451
452
453
454
455
456 DenoiseFFTRemove::DenoiseFFTRemove(DenoiseFFTEffect *plugin)
457 {
458         this->plugin = plugin;
459 }
460
461 int DenoiseFFTRemove::signal_process()
462 {
463         double level = DB::fromdb(plugin->config.level);
464         for(int i = 0; i < window_size / 2; i++)
465         {
466                 double result = sqrt(freq_real[i] * freq_real[i] + freq_imag[i] * freq_imag[i]);
467                 double angle = atan2(freq_imag[i], freq_real[i]);
468                 result -= plugin->reference[i] * level;
469                 if(result < 0) result = 0;
470                 freq_real[i] = result * cos(angle);
471                 freq_imag[i] = result * sin(angle);
472         }
473         symmetry(window_size, freq_real, freq_imag);
474         return 0;
475 }
476
477 int DenoiseFFTRemove::read_samples(int64_t output_sample,
478         int samples,
479         Samples *buffer)
480 {
481         return plugin->read_samples(buffer,
482                 0,
483                 plugin->get_samplerate(),
484                 output_sample,
485                 samples);
486 }
487
488
489
490
491
492 DenoiseFFTCollect::DenoiseFFTCollect(DenoiseFFTEffect *plugin)
493 {
494         this->plugin = plugin;
495 }
496
497 int DenoiseFFTCollect::signal_process()
498 {
499         for(int i = 0; i < window_size / 2; i++)
500         {
501                 double result = sqrt(freq_real[i] * freq_real[i] + freq_imag[i] * freq_imag[i]);
502                 plugin->reference[i] += result;
503         }
504         return 0;
505 }
506
507 int DenoiseFFTCollect::read_samples(int64_t output_sample,
508         int samples,
509         Samples *buffer)
510 {
511         return plugin->read_samples(buffer,
512                 0,
513                 plugin->get_samplerate(),
514                 output_sample,
515                 samples);
516 }
517