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