4 * Copyright (C) 2009 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"
24 #include "bcsignals.h"
27 #include "mainprogress.h"
28 #include "../../cinelerra/resample.h"
31 #include "transportque.inc"
37 PluginClient* new_plugin(PluginServer *server)
39 return new ResampleEffect(server);
44 ResampleFraction::ResampleFraction(ResampleEffect *plugin, int x, int y)
45 : BC_TextBox(x, y, xS(100), 1, (float)plugin->scale, 1, MEDIUMFONT, 6)
47 this->plugin = plugin;
50 int ResampleFraction::handle_event()
52 plugin->scale = atof(get_text());
63 ResampleWindow::ResampleWindow(ResampleEffect *plugin, int x, int y)
64 : BC_Window(_(PROGRAM_NAME ": Resample"),
75 this->plugin = plugin;
76 // *** CONTEXT_HELP ***
77 if(plugin) context_help_set_keyword(plugin->plugin_title());
78 else context_help_set_keyword("Rendered Audio Effects");
81 void ResampleWindow::create_objects()
84 int ys10 = yS(10), ys20 = yS(20);
85 int x = xs10, y = ys10;
86 lock_window("ResampleWindow::create_objects");
87 add_subwindow(new BC_Title(x, y, _("Scale factor:")));
89 add_subwindow(new ResampleFraction(plugin, x, y));
90 add_subwindow(new BC_OKButton(this));
91 add_subwindow(new BC_CancelButton(this));
103 ResampleResample::ResampleResample(ResampleEffect *plugin)
105 this->plugin = plugin;
108 int ResampleResample::read_samples(Samples *buffer, int64_t start, int64_t len)
110 //printf("ResampleResample::read_samples %d %lld\n", __LINE__, len);
112 return plugin->read_samples(buffer,
114 start + plugin->get_source_start(),
124 ResampleEffect::ResampleEffect(PluginServer *server)
125 : PluginAClient(server)
130 ResampleEffect::~ResampleEffect()
134 const char* ResampleEffect::plugin_title() { return N_("Resample"); }
136 void ResampleEffect::reset()
141 int ResampleEffect::get_parameters()
144 ResampleWindow window(this, info.get_abs_cursor_x(), info.get_abs_cursor_y());
145 window.create_objects();
146 int result = window.run_window();
152 int ResampleEffect::load_defaults()
154 char directory[BCTEXTLEN];
156 // set the default directory
157 sprintf(directory, "%s/resample.rc", File::get_config_path());
159 defaults = new BC_Hash(directory);
162 scale = defaults->get("SCALE", (double)1);
163 //printf("ResampleEffect::load_defaults %d %f\n", __LINE__, scale);
167 int ResampleEffect::save_defaults()
169 //printf("ResampleEffect::save_defaults %d %f\n", __LINE__, scale);
170 defaults->update("SCALE", scale);
177 int ResampleEffect::start_loop()
179 if(PluginClient::interactive)
181 char string[BCTEXTLEN];
182 sprintf(string, "%s...", plugin_title());
183 progress = start_progress(string,
184 (int64_t)((double)(PluginClient::end - PluginClient::start) / scale));
187 current_position = PluginClient::start;
190 resample = new ResampleResample(this);
194 int ResampleEffect::stop_loop()
196 if(PluginClient::interactive)
198 progress->stop_progress();
204 int ResampleEffect::process_loop(Samples *buffer, int64_t &write_length)
208 // Length to read based on desired output size
209 //printf("ResampleEffect::process_loop %d %d\n", __LINE__);
210 // int64_t size = (int64_t)((double)PluginAClient::in_buffer_size * scale);
211 int64_t predicted_total = (int64_t)((double)(PluginClient::end - PluginClient::start) / scale + 0.5);
213 // Samples *input = new Samples(size);
215 // read_samples(input, 0, current_position, size);
216 // current_position += size;
218 resample->resample(buffer,
219 PluginAClient::out_buffer_size,
221 (int)(1000000.0 / scale),
225 write_length = PluginAClient::out_buffer_size;
226 if(total_written + write_length >= predicted_total)
228 write_length = predicted_total - total_written;
231 total_written += write_length;
232 //printf("ResampleEffect::process_loop %d %lld %f\n", __LINE__, write_length, scale);
234 // resample->resample_chunk(input,
237 // (int)(1000000.0 / scale),
241 // if(resample->get_output_size(0))
243 // int64_t output_size = resample->get_output_size(0);
245 // // if(output_size)
247 // total_written += output_size;
250 // // // Trim output to predicted length of stretched selection.
251 // if(total_written > predicted_total)
253 // output_size -= total_written - predicted_total;
257 // // resample->read_output(buffer, 0, output_size);
259 // // write_length = output_size;
262 if(PluginClient::interactive) result |= progress->update(total_written);
263 //printf("ResampleEffect::process_loop %d %lld\n", __LINE__, total_written);