Merge CV, ver=5.1; ops/methods from HV, and interface from CV where possible
[goodguy/history.git] / cinelerra-5.1 / plugins / resample / resample.C
1
2 /*
3  * CINELERRA
4  * Copyright (C) 2009 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 "bcdisplayinfo.h"
23 #include "bchash.h"
24 #include "bcsignals.h"
25 #include "language.h"
26 #include "mainprogress.h"
27 #include "../../cinelerra/resample.h"
28 #include "resample.h"
29 #include "samples.h"
30 #include "transportque.inc"
31 #include "vframe.h"
32
33
34
35
36 PluginClient* new_plugin(PluginServer *server)
37 {
38         return new ResampleEffect(server);
39 }
40
41
42
43 ResampleFraction::ResampleFraction(ResampleEffect *plugin, int x, int y)
44  : BC_TextBox(x, y, 100, 1, (float)plugin->scale, 1, MEDIUMFONT, 6)
45 {
46         this->plugin = plugin;
47 }
48
49 int ResampleFraction::handle_event()
50 {
51         plugin->scale = atof(get_text());
52         return 1;
53 }
54
55
56
57
58
59
60
61
62 ResampleWindow::ResampleWindow(ResampleEffect *plugin, int x, int y)
63  : BC_Window(_(PROGRAM_NAME ": Resample"), 
64                                 x - 160,
65                                 y - 75,
66                                 320, 
67                                 150, 
68                                 320, 
69                                 150,
70                                 0,
71                                 0,
72                                 1)
73 {
74         this->plugin = plugin;
75 }
76
77 void ResampleWindow::create_objects()
78 {
79         int x = 10, y = 10;
80         lock_window("ResampleWindow::create_objects");
81         add_subwindow(new BC_Title(x, y, _("Scale factor:")));
82         y += 20;
83         add_subwindow(new ResampleFraction(plugin, x, y));
84         add_subwindow(new BC_OKButton(this));
85         add_subwindow(new BC_CancelButton(this));
86         show_window();
87         unlock_window();
88 }
89
90
91
92
93
94
95
96
97 ResampleResample::ResampleResample(ResampleEffect *plugin)
98 {
99         this->plugin = plugin;
100 }
101
102 int ResampleResample::read_samples(Samples *buffer, int64_t start, int64_t len)
103 {
104 //printf("ResampleResample::read_samples %d %lld\n", __LINE__, len);
105
106         return plugin->read_samples(buffer, 
107                 0, 
108                 start + plugin->get_source_start(), 
109                 len);
110 }
111
112
113
114
115
116
117
118 ResampleEffect::ResampleEffect(PluginServer *server)
119  : PluginAClient(server)
120 {
121         reset();
122 }
123
124 ResampleEffect::~ResampleEffect()
125 {
126 }
127
128 const char* ResampleEffect::plugin_title() { return _("Resample"); }
129
130 void ResampleEffect::reset()
131 {
132         resample = 0;
133 }
134
135 int ResampleEffect::get_parameters()
136 {
137         BC_DisplayInfo info;
138         ResampleWindow window(this, info.get_abs_cursor_x(), info.get_abs_cursor_y());
139         window.create_objects();
140         int result = window.run_window();
141         
142         return result;
143 }
144
145
146 int ResampleEffect::load_defaults()
147 {
148         char directory[BCTEXTLEN];
149
150 // set the default directory
151         sprintf(directory, "%sresample.rc", BCASTDIR);
152 // load the defaults
153         defaults = new BC_Hash(directory);
154         defaults->load();
155
156         scale = defaults->get("SCALE", (double)1);
157 //printf("ResampleEffect::load_defaults %d %f\n", __LINE__, scale);
158         return 0;
159 }
160
161 int ResampleEffect::save_defaults()
162 {
163 //printf("ResampleEffect::save_defaults %d %f\n", __LINE__, scale);
164         defaults->update("SCALE", scale);
165         defaults->save();
166         return 0;
167 }
168
169
170
171 int ResampleEffect::start_loop()
172 {
173         if(PluginClient::interactive)
174         {
175                 char string[BCTEXTLEN];
176                 sprintf(string, "%s...", plugin_title());
177                 progress = start_progress(string, 
178                         (int64_t)((double)(PluginClient::end - PluginClient::start) / scale));
179         }
180
181         current_position = PluginClient::start;
182         total_written = 0;
183
184         resample = new ResampleResample(this);
185         return 0;
186 }
187
188 int ResampleEffect::stop_loop()
189 {
190         if(PluginClient::interactive)
191         {
192                 progress->stop_progress();
193                 delete progress;
194         }
195         return 0;
196 }
197
198 int ResampleEffect::process_loop(Samples *buffer, int64_t &write_length)
199 {
200         int result = 0;
201
202 // Length to read based on desired output size
203 //printf("ResampleEffect::process_loop %d %d\n", __LINE__);
204 //      int64_t size = (int64_t)((double)PluginAClient::in_buffer_size * scale);
205         int64_t predicted_total = (int64_t)((double)(PluginClient::end - PluginClient::start) / scale + 0.5);
206
207 //      Samples *input = new Samples(size);
208         
209 //      read_samples(input, 0, current_position, size);
210 //      current_position += size;
211
212         resample->resample(buffer,
213                 PluginAClient::out_buffer_size,
214                 1000000, 
215                 (int)(1000000.0 / scale), 
216                 total_written,
217                 PLAY_FORWARD);
218
219         write_length = PluginAClient::out_buffer_size;
220         if(total_written + write_length >= predicted_total)
221         {
222                 write_length = predicted_total - total_written;
223                 result = 1;
224         }
225         total_written += write_length;
226 //printf("ResampleEffect::process_loop %d %lld %f\n", __LINE__, write_length, scale);
227
228 //      resample->resample_chunk(input, 
229 //              size, 
230 //              1000000, 
231 //              (int)(1000000.0 / scale), 
232 //              0);
233 // 
234 // // 
235 //      if(resample->get_output_size(0))
236 //      {
237 //              int64_t output_size = resample->get_output_size(0);
238 // 
239 // //           if(output_size)
240 //              {
241 //                      total_written += output_size;
242 //              }
243 // 
244 // // // Trim output to predicted length of stretched selection.
245 //              if(total_written > predicted_total)
246 //              {
247 //                      output_size -= total_written - predicted_total;
248 //                      result = 1;
249 //              }
250 // 
251 // //           resample->read_output(buffer, 0, output_size);
252 // 
253 // //           write_length = output_size;
254 //      }
255
256         if(PluginClient::interactive) result |= progress->update(total_written);
257 //printf("ResampleEffect::process_loop %d %lld\n", __LINE__, total_written);
258
259 //      delete input;
260         return result;
261 }
262
263
264
265
266
267
268
269
270
271
272
273