r125: This commit was manufactured by cvs2svn to create tag 'r1_1_7-last'.
[cinelerra_cv/mob.git] / hvirtual / plugins / resample / resample.C
blob153cfe703cc7db02f2a6fa6ea1185b2ba2980523
1 #include "bcdisplayinfo.h"
2 #include "defaults.h"
3 #include "mainprogress.h"
4 #include "picon_png.h"
5 #include "../../cinelerra/resample.h"
6 #include "resample.h"
7 #include "vframe.h"
12 PluginClient* new_plugin(PluginServer *server)
14         return new ResampleEffect(server);
19 ResampleFraction::ResampleFraction(ResampleEffect *plugin, int x, int y)
20  : BC_TextBox(x, y, 100, 1, (float)plugin->scale)
22         this->plugin = plugin;
25 int ResampleFraction::handle_event()
27         plugin->scale = atof(get_text());
28         return 1;
38 ResampleWindow::ResampleWindow(ResampleEffect *plugin, int x, int y)
39  : BC_Window(PROGRAM_NAME ": Resample", 
40                                 x - 160,
41                                 y - 75,
42                                 320, 
43                                 150, 
44                                 320, 
45                                 150,
46                                 0,
47                                 0,
48                                 1)
50         this->plugin = plugin;
53 void ResampleWindow::create_objects()
55         int x = 10, y = 10;
56         add_subwindow(new BC_Title(x, y, _("Scale factor:")));
57         y += 20;
58         add_subwindow(new ResampleFraction(plugin, x, y));
59         add_subwindow(new BC_OKButton(this));
60         add_subwindow(new BC_CancelButton(this));
61         show_window();
62         flush();
72 ResampleEffect::ResampleEffect(PluginServer *server)
73  : PluginAClient(server)
75         reset();
76         load_defaults();
79 ResampleEffect::~ResampleEffect()
81         save_defaults();
82         delete defaults;
85 char* ResampleEffect::plugin_title()
87         return _("Resample");
90 VFrame* ResampleEffect::new_picon()
92         return new VFrame(picon_png);
95 void ResampleEffect::reset()
97         resample = 0;
100 int ResampleEffect::get_parameters()
102         BC_DisplayInfo info;
103         ResampleWindow window(this, info.get_abs_cursor_x(), info.get_abs_cursor_y());
104         window.create_objects();
105         int result = window.run_window();
106         
107         return result;
111 int ResampleEffect::load_defaults()
113         char directory[BCTEXTLEN];
115 // set the default directory
116         sprintf(directory, "%sresample.rc", BCASTDIR);
117 // load the defaults
118         defaults = new Defaults(directory);
119         defaults->load();
121         scale = defaults->get("SCALE", (double)1);
122         return 0;
125 int ResampleEffect::save_defaults()
127         defaults->update("SCALE", scale);
128         defaults->save();
129         return 0;
134 int ResampleEffect::start_loop()
136         if(PluginClient::interactive)
137         {
138                 char string[BCTEXTLEN];
139                 sprintf(string, "%s...", plugin_title());
140                 progress = start_progress(string, 
141                         (int64_t)((double)(PluginClient::end - PluginClient::start) * scale));
142         }
144         current_position = PluginClient::start;
145         total_written = 0;
147         resample = new Resample(0, 1);
148         return 0;
151 int ResampleEffect::stop_loop()
153         if(PluginClient::interactive)
154         {
155                 progress->stop_progress();
156                 delete progress;
157         }
158         return 0;
161 int ResampleEffect::process_loop(double *buffer, int64_t &write_length)
163         int result = 0;
165 // Length to read based on desired output size
166         int64_t size = (int64_t)((double)PluginAClient::in_buffer_size / scale);
167         int64_t predicted_total = (int64_t)((double)(PluginClient::end - PluginClient::start) * scale + 0.5);
169         double *input = new double[size];
171         read_samples(input, 0, current_position, size);
172         current_position += size;
174         resample->resample_chunk(input, 
175                 size, 
176                 1000000, 
177                 (int)(1000000.0 * scale), 
178                 0);
181         if(resample->get_output_size(0))
182         {
183                 int64_t output_size = resample->get_output_size(0);
185                 if(output_size)
186                 {
187                         total_written += output_size;
188                 }
190 // Trim output to predicted length of stretched selection.
191                 if(total_written > predicted_total)
192                 {
193                         output_size -= total_written - predicted_total;
194                         result = 1;
195                 }
197                 resample->read_output(buffer, 0, output_size);
199                 write_length = output_size;
200         }
202         if(PluginClient::interactive) result = progress->update(total_written);
203 //printf("TimeStretch::process_loop 1\n");
205         delete [] input;
206 //printf("TimeStretch::process_loop 2\n");
207         return result;