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