r125: This commit was manufactured by cvs2svn to create tag 'r1_1_7-last'.
[cinelerra_cv/mob.git] / hvirtual / plugins / delayaudio / delayaudio.C
blobf6862a1f520ff388600a2b38548bba43c661ba7b
1 #include "bcdisplayinfo.h"
2 #include "clip.h"
3 #include "defaults.h"
4 #include "delayaudio.h"
5 #include "filexml.h"
6 #include "picon_png.h"
7 #include "vframe.h"
9 #include <string.h>
11 #include <libintl.h>
12 #define _(String) gettext(String)
13 #define gettext_noop(String) String
14 #define N_(String) gettext_noop (String)
17 PluginClient* new_plugin(PluginServer *server)
19         return new DelayAudio(server);
23 DelayAudio::DelayAudio(PluginServer *server)
24  : PluginAClient(server)
26         reset();
27         load_defaults();
30 DelayAudio::~DelayAudio()
32         if(thread)
33         {
34                 thread->window->set_done(0);
35                 thread->completion.lock();
36                 delete thread;
37         }
39         save_defaults();
40         delete defaults;
41         
42         if(buffer) delete [] buffer;
47 VFrame* DelayAudio::new_picon()
49         return new VFrame(picon_png);
52 char* DelayAudio::plugin_title()
54         return _("Delay audio");
57 void DelayAudio::reset()
59         need_reconfigure = 1;
60         buffer = 0;
61         thread = 0;
64 int DelayAudio::is_realtime()
66         return 1;
69 void DelayAudio::load_configuration()
71         KeyFrame *prev_keyframe;
72         prev_keyframe = get_prev_keyframe(get_source_position());
73         
74         DelayAudioConfig old_config;
75         old_config.copy_from(config);
76         read_data(prev_keyframe);
78         if(!old_config.equivalent(config))
79         {
80 // Reconfigure
81                 need_reconfigure = 1;
82         }
85 int DelayAudio::load_defaults()
87         char directory[BCTEXTLEN];
89         sprintf(directory, "%sdelayaudio.rc", BCASTDIR);
90         defaults = new Defaults(directory);
91         defaults->load();
92         config.length = defaults->get("LENGTH", (double)1);
93         return 0;
98 int DelayAudio::save_defaults()
100         defaults->update("LENGTH", config.length);
101         defaults->save();
102         return 0;
105 void DelayAudio::read_data(KeyFrame *keyframe)
107         FileXML input;
108         input.set_shared_string(keyframe->data, strlen(keyframe->data));
110         int result = 0;
111         while(!result)
112         {
113                 result = input.read_tag();
115                 if(!result)
116                 {
117                         if(input.tag.title_is("DELAYAUDIO"))
118                         {
119                                 config.length = input.tag.get_property("LENGTH", (double)config.length);
120                         }
121                 }
122         }
126 void DelayAudio::save_data(KeyFrame *keyframe)
128         FileXML output;
129         output.set_shared_string(keyframe->data, MESSAGESIZE);
131         output.tag.set_title("DELAYAUDIO");
132         output.tag.set_property("LENGTH", (double)config.length);
133         output.append_tag();
134         output.append_newline();
135         output.terminate_string();
138 void DelayAudio::reconfigure()
140         input_start = (int64_t)(config.length * PluginAClient::project_sample_rate + 0.5);
141         int64_t new_allocation = input_start + PluginClient::in_buffer_size;
142         double *new_buffer = new double[new_allocation];
143         bzero(new_buffer, sizeof(double) * new_allocation);
145 // printf("DelayAudio::reconfigure %f %d %d %d\n", 
146 // config.length, 
147 // PluginAClient::project_sample_rate, 
148 // PluginClient::in_buffer_size,
149 // new_allocation);
150 // 
153         if(buffer)
154         {
155                 int size = MIN(new_allocation, allocation);
157                 memcpy(new_buffer, 
158                         buffer, 
159                         (size - PluginClient::in_buffer_size) * sizeof(double));
160                 delete [] buffer;
161         }
163         allocation = new_allocation;
164         buffer = new_buffer;
165         allocation = new_allocation;
166         need_reconfigure = 0;
169 int DelayAudio::process_realtime(int64_t size, double *input_ptr, double *output_ptr)
171         load_configuration();
172         if(need_reconfigure) reconfigure();
174 // printf("DelayAudio::process_realtime %d %d\n",
175 // input_start, size);
179         memcpy(buffer + input_start, input_ptr, size * sizeof(double));
180         memcpy(output_ptr, buffer, size * sizeof(double));
182         for(int i = size, j = 0; i < allocation; i++, j++)
183         {
184                 buffer[j] = buffer[i];
185         }
187         return 0;
190 int DelayAudio::show_gui()
192         load_configuration();
193         
194         thread = new DelayAudioThread(this);
195         thread->start();
196         return 0;
199 int DelayAudio::set_string()
201         if(thread) thread->window->set_title(gui_string);
202         return 0;
205 void DelayAudio::raise_window()
207         if(thread)
208         {
209                 thread->window->raise_window();
210                 thread->window->flush();
211         }
214 void DelayAudio::update_gui()
216         if(thread)
217         {
218                 load_configuration();
219                 thread->window->lock_window();
220                 thread->window->update_gui();
221                 thread->window->unlock_window();
222         }
236 DelayAudioThread::DelayAudioThread(DelayAudio *plugin)
237  : Thread()
239         this->plugin = plugin;
240         set_synchronous(0);
241         completion.lock();
247 DelayAudioThread::~DelayAudioThread()
249         delete window;
253 void DelayAudioThread::run()
255         BC_DisplayInfo info;
256         
257         window = new DelayAudioWindow(plugin,
258                 info.get_abs_cursor_x() - 125, 
259                 info.get_abs_cursor_y() - 115);
260         
261         window->create_objects();
262         int result = window->run_window();
263         completion.unlock();
264 // Last command executed in thread
265         if(result) plugin->client_side_close();
277 DelayAudioWindow::DelayAudioWindow(DelayAudio *plugin, int x, int y)
278  : BC_Window(plugin->gui_string, 
279         x, 
280         y, 
281         200, 
282         80, 
283         200, 
284         80, 
285         0, 
286         0,
287         1)
289         this->plugin = plugin;
292 DelayAudioWindow::~DelayAudioWindow()
296 int DelayAudioWindow::create_objects()
298         add_subwindow(new BC_Title(10, 10, _("Delay seconds:")));
299         add_subwindow(length = new DelayAudioTextBox(plugin, 10, 40));
300         update_gui();
301         show_window();
302         flush();
303         return 0;
306 int DelayAudioWindow::close_event()
308 // Set result to 1 to indicate a client side close
309         set_done(1);
310         return 1;
313 void DelayAudioWindow::update_gui()
315         char string[BCTEXTLEN];
316         sprintf(string, "%.04f", plugin->config.length);
317         length->update(string);
331 DelayAudioTextBox::DelayAudioTextBox(DelayAudio *plugin, int x, int y)
332  : BC_TextBox(x, y, 150, 1, "")
334         this->plugin = plugin;
337 DelayAudioTextBox::~DelayAudioTextBox()
341 int DelayAudioTextBox::handle_event()
343         plugin->config.length = atof(get_text());
344         if(plugin->config.length < 0) plugin->config.length = 0;
345         plugin->send_configure_change();
346         return 1;
356 DelayAudioConfig::DelayAudioConfig()
358         length = 1;
360         
361 int DelayAudioConfig::equivalent(DelayAudioConfig &that)
363         return(EQUIV(this->length, that.length));
366 void DelayAudioConfig::copy_from(DelayAudioConfig &that)
368         this->length = that.length;