minor fixes
[goodguy/cinelerra.git] / cinelerra-5.1 / cinelerra / pluginarray.C
1
2 /*
3  * CINELERRA
4  * Copyright (C) 2008 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 "atrack.h"
23 #include "cache.h"
24 #include "edl.h"
25 #include "edlsession.h"
26 #include "file.h"
27 #include "keyframe.h"
28 #include "language.h"
29 #include "mainprogress.h"
30 #include "mwindow.h"
31 #include "mwindowgui.h"
32 #include "pluginarray.h"
33 #include "pluginserver.h"
34 #include "preferences.h"
35 #include "bcprogressbox.h"
36
37
38
39
40 PluginArray::PluginArray(int data_type)
41  : ArrayList<PluginServer*>()
42 {
43         this->data_type = data_type;
44     cache = 0;
45     modules = 0;
46     file = 0;
47     done = 0;
48     plugin_server = 0;
49     end = 0;
50     buffer_size = 0;
51     keyframe = 0;
52     mwindow = 0;
53     start = 0;
54     error = 0;
55     edl = 0;
56 }
57
58 PluginArray::~PluginArray()
59 {
60         remove_all_objects();
61         delete [] modules;
62 }
63
64
65 PluginServer* PluginArray::scan_plugindb(char *title)
66 {
67         return mwindow->scan_plugindb(title, data_type);
68 }
69
70 int PluginArray::start_plugins(MWindow *mwindow,
71         EDL *edl,
72         PluginServer *plugin_server,
73         KeyFrame *keyframe,
74         int64_t start,
75         int64_t end,
76         File *file)
77 {
78         this->mwindow = mwindow;
79         this->edl = edl;
80         this->plugin_server = plugin_server;
81         this->keyframe = keyframe;
82         this->start = start;
83         this->end = end;
84         this->file = file;
85
86         cache = new CICache(mwindow->preferences);
87         buffer_size = get_bufsize();
88         get_recordable_tracks();
89         create_modules();
90         create_buffers();
91
92         if(!plugin_server->realtime)
93         {
94                 PluginServer *plugin;
95                 int i;
96
97                 if(!plugin_server->multichannel)
98                 {
99 // ============================ single channel plugins
100 // start 1 plugin for each track
101                         for(i = 0; i < total_tracks(); i++)
102                         {
103                                 append(plugin = new PluginServer(*plugin_server));
104                                 plugin->set_mwindow(mwindow);
105                                 plugin->set_keyframe(keyframe);
106                                 plugin->append_module(modules[i]);
107                                 plugin->open_plugin(0,
108                                         mwindow->preferences,
109                                         mwindow->edl,
110                                         0);
111                                 if(i == 0) plugin->set_interactive();
112                                 plugin->start_loop(start, end, buffer_size, 1);
113                         }
114                 }
115                 else
116                 {
117 // ============================ multichannel
118 // start 1 plugin for all tracks
119                         append(plugin = new PluginServer(*plugin_server));
120                         plugin->set_mwindow(mwindow);
121                         plugin->set_keyframe(keyframe);
122                         for(i = 0; i < total_tracks(); i++)
123                                 plugin->append_module(modules[i]);
124                         plugin->open_plugin(0,
125                                 mwindow->preferences,
126                                 mwindow->edl,
127                                 0);
128 // set one plugin for progress bars
129                         plugin->set_interactive();
130                         plugin->start_loop(start, end, buffer_size, total_tracks());
131                 }
132
133 //printf("PluginArray::start_plugins 5\n");
134         }
135         else
136         {
137                 PluginServer *plugin;
138                 int i;
139
140                 if(!plugin_server->multichannel)
141                 {
142 // single channel plugins
143 // start 1 plugin for each track
144                         for(i = 0; i < total_tracks(); i++)
145                         {
146                                 append(plugin = new PluginServer(*plugin_server));
147                                 plugin->set_mwindow(mwindow);
148                                 plugin->set_keyframe(keyframe);
149                                 plugin->append_module(modules[i]);
150                                 plugin->open_plugin(0,
151                                         mwindow->preferences,
152                                         mwindow->edl,
153                                         0);
154                                 plugin->get_parameters(start, end, 1);
155                                 plugin->init_realtime(0, 1, get_bufsize());
156                         }
157                 }
158                 else
159                 {
160 // multichannel
161 // start 1 plugin for all tracks
162                         append(plugin = new PluginServer(*plugin_server));
163                         plugin->set_mwindow(mwindow);
164                         plugin->set_keyframe(keyframe);
165                         for(i = 0; i < total_tracks(); i++)
166                                 plugin->append_module(modules[i]);
167                         plugin->open_plugin(0,
168                                 mwindow->preferences,
169                                 mwindow->edl,
170                                 0);
171                         plugin->get_parameters(start, end, total_tracks());
172                         plugin->init_realtime(0, total_tracks(), get_bufsize());
173                 }
174         }
175 //printf("PluginArray::start_plugins 8\n");
176         return 0;
177 }
178
179
180
181
182
183 int PluginArray::run_plugins()
184 {
185 // Length to write after process_loop
186         int64_t write_length;
187
188         done = 0;     // for when done
189         error = 0;
190         if(plugin_server->realtime)
191         {
192                 int64_t len = 0;
193                 MainProgressBar *progress;
194                 char string[BCTEXTLEN], string2[BCTEXTLEN];
195
196                 mwindow->gui->lock_window();
197                 sprintf(string, "%s...", _(plugin_server->title));
198                 progress = mwindow->mainprogress->start_progress(string, end - start);
199                 mwindow->gui->unlock_window();
200
201                 for(int64_t current_position = start;
202                         current_position < end && !done && !error;
203                         current_position += len)
204                 {
205                         len = buffer_size;
206                         if(current_position + len > end) len = end - current_position;
207
208 // Process in plugin.  This pulls data from the modules
209                         get_buffers();
210                         for(int i = 0; i < total; i++)
211                         {
212                                 process_realtime(i, current_position, len);
213                         }
214
215 // Write to file
216                         error = write_buffers(len);
217                         done = progress->update(current_position - start + len);
218                 }
219
220                 progress->get_time(string2);
221                 progress->stop_progress();
222                 delete progress;
223
224                 sprintf(string, _("%s took %s"), _(plugin_server->title), string2);
225                 mwindow->gui->lock_window();
226                 mwindow->gui->show_message(string2);
227                 mwindow->gui->unlock_window();
228         }
229         else
230         {
231 // Run main loop once for multichannel plugins.
232 // Run multiple times for single channel plugins.
233 // Each write to the file must contain all the channels
234                 while(!done && !error)
235                 {
236                         for(int i = 0; i < total; i++)
237                         {
238                                 write_length = 0;
239                                 done += process_loop(i, write_length);
240                         }
241
242
243                         if(write_length)
244                                 error = write_buffers(write_length);
245                 }
246         }
247
248         return error;
249 }
250
251
252 int PluginArray::stop_plugins()
253 {
254         if(plugin_server->realtime)
255         {
256                 for(int i = 0; i < total; i++)
257                 {
258                         values[i]->close_plugin();
259                 }
260         }
261         else
262         {
263                 for(int i = 0; i < total; i++)
264                 {
265                         values[i]->stop_loop();
266                         values[i]->close_plugin();
267                 }
268         }
269         if( cache ) {
270                 cache->remove_user();
271                 cache = 0;
272         }
273         return 0;
274 }
275