fix drop silence on track segv, dont deselect on drop, set expanders if plugins dropped
[goodguy/cinelerra.git] / cinelerra-5.1 / plugins / despike / despike.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 "clip.h"
23 #include "confirmsave.h"
24 #include "bchash.h"
25 #include "errorbox.h"
26 #include "filexml.h"
27 #include "language.h"
28 #include "despike.h"
29 #include "despikewindow.h"
30 #include "samples.h"
31
32 #include "vframe.h"
33
34 #include <string.h>
35
36
37
38 REGISTER_PLUGIN(Despike)
39
40
41
42 Despike::Despike(PluginServer *server)
43  : PluginAClient(server)
44 {
45
46         last_sample = 0;
47 }
48
49 Despike::~Despike()
50 {
51
52 }
53
54 const char* Despike::plugin_title() { return N_("Despike"); }
55 int Despike::is_realtime() { return 1; }
56
57 NEW_WINDOW_MACRO(Despike, DespikeWindow)
58
59 LOAD_CONFIGURATION_MACRO(Despike, DespikeConfig)
60
61
62 int Despike::process_realtime(int64_t size, Samples *input_ptr, Samples *output_ptr)
63 {
64         load_configuration();
65
66         double threshold = db.fromdb(config.level);
67         double change = db.fromdb(config.slope);
68
69 //printf("Despike::process_realtime 1\n");
70         double *output_samples = output_ptr->get_data();
71         double *input_samples = input_ptr->get_data();
72         for(int64_t i = 0; i < size; i++)
73         {
74                 if(fabs(input_samples[i]) > threshold ||
75                         fabs(input_samples[i]) - fabs(last_sample) > change)
76                 {
77                         output_samples[i] = last_sample;
78                 }
79                 else
80                 {
81                         output_samples[i] = input_samples[i];
82                         last_sample = input_samples[i];
83                 }
84         }
85 //printf("Despike::process_realtime 2\n");
86
87         return 0;
88 }
89
90
91
92 void Despike::save_data(KeyFrame *keyframe)
93 {
94         FileXML output;
95
96 // cause xml file to store data directly in text
97         output.set_shared_output(keyframe->xbuf);
98
99         output.tag.set_title("DESPIKE");
100         output.tag.set_property("LEVEL", config.level);
101         output.tag.set_property("SLOPE", config.slope);
102         output.append_tag();
103         output.tag.set_title("/DESPIKE");
104         output.append_tag();
105         output.append_newline();
106         output.terminate_string();
107 }
108
109 void Despike::read_data(KeyFrame *keyframe)
110 {
111         FileXML input;
112 // cause xml file to read directly from text
113         input.set_shared_input(keyframe->xbuf);
114         int result = 0;
115
116         result = input.read_tag();
117
118         if(!result)
119         {
120                 if(input.tag.title_is("DESPIKE"))
121                 {
122                         config.level = input.tag.get_property("LEVEL", config.level);
123                         config.slope = input.tag.get_property("SLOPE", config.slope);
124                 }
125         }
126 }
127
128 void Despike::update_gui()
129 {
130         if(thread)
131         {
132                 load_configuration();
133                 ((DespikeWindow*)thread->window)->lock_window();
134                 ((DespikeWindow*)thread->window)->level->update(config.level);
135                 ((DespikeWindow*)thread->window)->slope->update(config.slope);
136                 ((DespikeWindow*)thread->window)->unlock_window();
137         }
138 }
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155 DespikeConfig::DespikeConfig()
156 {
157         level = 0;
158         slope = 0;
159 }
160
161 int DespikeConfig::equivalent(DespikeConfig &that)
162 {
163         return EQUIV(level, that.level) &&
164                 EQUIV(slope, that.slope);
165 }
166
167 void DespikeConfig::copy_from(DespikeConfig &that)
168 {
169         level = that.level;
170         slope = that.slope;
171 }
172
173 void DespikeConfig::interpolate(DespikeConfig &prev,
174                 DespikeConfig &next,
175                 int64_t prev_frame,
176                 int64_t next_frame,
177                 int64_t current_frame)
178 {
179         double next_scale = (double)(current_frame - prev_frame) / (next_frame - prev_frame);
180         double prev_scale = (double)(next_frame - current_frame) / (next_frame - prev_frame);
181
182         this->level = prev.level * prev_scale + next.level * next_scale;
183         this->slope = prev.slope * prev_scale + next.slope * next_scale;
184 }
185
186
187
188
189
190