Fixed initialisation of tf in file_open(). Without setting the memory to 0,
[cinelerra_cv/mob.git] / plugins / resample / resample.C
blob4794b19858c39ec6b64472c942653b51549af09e
1 #include "bcdisplayinfo.h"
2 #include "bchash.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, 1, MEDIUMFONT, 6)
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() { return N_("Resample"); }
87 VFrame* ResampleEffect::new_picon()
89         return new VFrame(picon_png);
92 void ResampleEffect::reset()
94         resample = 0;
97 int ResampleEffect::get_parameters()
99         BC_DisplayInfo info;
100         ResampleWindow window(this, info.get_abs_cursor_x(), info.get_abs_cursor_y());
101         window.create_objects();
102         int result = window.run_window();
103         
104         return result;
108 int ResampleEffect::load_defaults()
110         char directory[BCTEXTLEN];
112 // set the default directory
113         sprintf(directory, "%sresample.rc", BCASTDIR);
114 // load the defaults
115         defaults = new BC_Hash(directory);
116         defaults->load();
118         scale = defaults->get("SCALE", (double)1);
119         return 0;
122 int ResampleEffect::save_defaults()
124         defaults->update("SCALE", scale);
125         defaults->save();
126         return 0;
131 int ResampleEffect::start_loop()
133         if(PluginClient::interactive)
134         {
135                 char string[BCTEXTLEN];
136                 sprintf(string, "%s...", plugin_title());
137                 progress = start_progress(string, 
138                         (int64_t)((double)(PluginClient::end - PluginClient::start) / scale));
139         }
141         current_position = PluginClient::start;
142         total_written = 0;
144         resample = new Resample(0, 1);
145         return 0;
148 int ResampleEffect::stop_loop()
150         if(PluginClient::interactive)
151         {
152                 progress->stop_progress();
153                 delete progress;
154         }
155         return 0;
158 int ResampleEffect::process_loop(double *buffer, int64_t &write_length)
160         int result = 0;
162 // Length to read based on desired output size
163         int64_t size = (int64_t)((double)PluginAClient::in_buffer_size * scale);
164         int64_t predicted_total = (int64_t)((double)(PluginClient::end - PluginClient::start) / scale + 0.5);
166         double *input = new double[size];
168         read_samples(input, 0, current_position, size);
169         current_position += size;
171         resample->resample_chunk(input, 
172                 size, 
173                 1000000, 
174                 (int)(1000000.0 / scale), 
175                 0);
178         if(resample->get_output_size(0))
179         {
180                 int64_t output_size = resample->get_output_size(0);
182                 if(output_size)
183                 {
184                         total_written += output_size;
185                 }
187 // Trim output to predicted length of stretched selection.
188                 if(total_written > predicted_total)
189                 {
190                         output_size -= total_written - predicted_total;
191                         result = 1;
192                 }
194                 resample->read_output(buffer, 0, output_size);
196                 write_length = output_size;
197         }
199         if(PluginClient::interactive) result = progress->update(total_written);
200 //printf("TimeStretch::process_loop 1\n");
202         delete [] input;
203 //printf("TimeStretch::process_loop 2\n");
204         return result;