4 * Copyright (C) 1997-2012 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"
27 #include "removegaps.h"
30 #include "transportque.inc"
35 // Maximum seconds to skip, if end of file
36 #define MAX_SKIPPED_DURATION 5.0
37 #define MIN_GAP_DURATION 0.01
38 #define MAX_GAP_DURATION 1.0
39 #define MIN_GAP_THRESHOLD INFINITYGAIN
40 #define MAX_GAP_THRESHOLD 0.0
42 REGISTER_PLUGIN(RemoveGaps);
46 RemoveGapsConfig::RemoveGapsConfig()
53 int RemoveGapsConfig::equivalent(RemoveGapsConfig &src)
55 return EQUIV(threshold, src.threshold) &&
56 EQUIV(duration, src.duration);
59 void RemoveGapsConfig::copy_from(RemoveGapsConfig &src)
61 this->threshold = src.threshold;
62 this->duration = src.duration;
65 void RemoveGapsConfig::interpolate(RemoveGapsConfig &prev,
66 RemoveGapsConfig &next,
69 int64_t current_frame)
71 this->threshold = prev.threshold;
72 this->duration = prev.duration;
76 void RemoveGapsConfig::boundaries()
78 CLAMP(threshold, MIN_GAP_THRESHOLD, MAX_GAP_THRESHOLD);
79 CLAMP(duration, MIN_GAP_DURATION, MAX_GAP_DURATION);
85 RemoveGapsWindow::RemoveGapsWindow(RemoveGaps *plugin)
86 : PluginClientWindow(plugin,
93 this->plugin = plugin;
96 RemoveGapsWindow::~RemoveGapsWindow()
100 void RemoveGapsWindow::create_objects()
105 add_subwindow(title = new BC_Title(x, y, _("Threshold of gap (DB):")));
107 add_subwindow(threshold = new RemoveGapsThreshold(this,
109 x + title->get_w() + plugin->get_theme()->widget_border,
111 y += threshold->get_h() + plugin->get_theme()->widget_border;
112 add_subwindow(title = new BC_Title(x, y, _("Max duration of gap (Seconds):")));
113 add_subwindow(duration = new RemoveGapsDuration(this,
115 x + title->get_w() + plugin->get_theme()->widget_border,
125 RemoveGapsThreshold::RemoveGapsThreshold(RemoveGapsWindow *window,
131 plugin->config.threshold,
135 this->plugin = plugin;
139 int RemoveGapsThreshold::handle_event()
141 plugin->config.threshold = get_value();
142 plugin->send_configure_change();
153 RemoveGapsDuration::RemoveGapsDuration(RemoveGapsWindow *window,
159 plugin->config.duration,
163 this->plugin = plugin;
167 int RemoveGapsDuration::handle_event()
169 plugin->config.duration = get_value();
170 plugin->send_configure_change();
180 RemoveGaps::RemoveGaps(PluginServer *server)
181 : PluginAClient(server)
183 need_reconfigure = 1;
192 RemoveGaps::~RemoveGaps()
197 const char* RemoveGaps::plugin_title() { return N_("Remove Gaps"); }
198 int RemoveGaps::is_realtime() { return 1; }
200 NEW_WINDOW_MACRO(RemoveGaps, RemoveGapsWindow)
202 LOAD_CONFIGURATION_MACRO(RemoveGaps, RemoveGapsConfig)
205 int RemoveGaps::process_buffer(int64_t size,
207 int64_t start_position,
210 need_reconfigure |= load_configuration();
213 if(need_reconfigure || start_position != dest_start)
215 source_start = start_position;
217 need_reconfigure = 0;
220 dest_start = start_position;
221 double *buffer_samples = buffer->get_data();
222 double *temp_samples = !temp ? 0 : temp->get_data();
224 double threshold = DB::fromdb(config.threshold);
225 int64_t duration = (int64_t)(config.duration * sample_rate);
226 for(int i = 0; i < size; )
228 if(!temp || temp_position >= temp->get_allocated())
230 // Expand temp buffer if smaller than buffer read
233 temp = new Samples(get_buffer_size());
234 temp_samples = temp->get_data();
238 // Fill new temp buffer
244 if(get_direction() == PLAY_FORWARD)
245 source_start += size;
247 source_start -= size;
250 double sample = temp_samples[temp_position];
251 if(fabs(sample) < threshold)
260 // Only copy sample if gap below duration or no more samples to read
261 if(gap_length < duration || gap_length > MAX_SKIPPED_DURATION * sample_rate)
263 buffer_samples[i++] = sample;
270 if(get_direction() == PLAY_FORWARD)
278 void RemoveGaps::render_stop()
280 need_reconfigure = 1;
286 void RemoveGaps::save_data(KeyFrame *keyframe)
290 // cause data to be stored directly in text
291 output.set_shared_output(keyframe->get_data(), MESSAGESIZE);
292 output.tag.set_title("REMOVEGAPS");
293 output.tag.set_property("DURATION", config.duration);
294 output.tag.set_property("THRESHOLD", config.threshold);
296 output.tag.set_title("/REMOVEGAPS");
298 output.append_newline();
299 output.terminate_string();
302 void RemoveGaps::read_data(KeyFrame *keyframe)
306 input.set_shared_input(keyframe->get_data(), strlen(keyframe->get_data()));
308 while(!input.read_tag())
310 if(input.tag.title_is("REMOVEGAPS"))
312 config.duration = input.tag.get_property("DURATION", config.duration);
313 config.threshold = input.tag.get_property("THRESHOLD", config.threshold);
319 void RemoveGaps::update_gui()
323 if(load_configuration())
325 thread->window->lock_window("RemoveGaps::update_gui");
326 ((RemoveGapsWindow*)thread->window)->duration->update((float)config.duration);
327 ((RemoveGapsWindow*)thread->window)->threshold->update((float)config.threshold);
328 thread->window->unlock_window();