Condense the xml-cleanup into a short feature-branch
[cinelerra_cv/mob.git] / plugins / overlayaudio / overlayaudio.C
blob3bec3d753e95821a99cbf80770f2e4e90863ff3e
1 #include "bcdisplayinfo.h"
2 #include "bchash.h"
3 #include "filexml.h"
4 #include "language.h"
5 #include "picon_png.h"
6 #include "pluginaclient.h"
7 #include <string.h>
10 class OverlayAudioWindow;
11 class OverlayAudio;
13 class OverlayAudioConfig
15 public:
16         OverlayAudioConfig();
17         int equivalent(OverlayAudioConfig &that);
18         void copy_from(OverlayAudioConfig &that);
19         void interpolate(OverlayAudioConfig &prev, 
20                 OverlayAudioConfig &next, 
21                 int64_t prev_frame, 
22                 int64_t next_frame, 
23                 int64_t current_frame);
24         static char* output_to_text(int output_layer);
25         int output_track;
26         enum
27         {
28                 TOP,
29                 BOTTOM
30         };
31         
34 class OutputTrack : public BC_PopupMenu
36 public:
37         OutputTrack(OverlayAudio *plugin, int x, int y);
38         void create_objects();
39         int handle_event();
40         OverlayAudio *plugin;
43 class OverlayAudioWindow : public BC_Window
45 public:
46         OverlayAudioWindow(OverlayAudio *plugin, int x, int y);
48         int create_objects();
49         int close_event();
51         OverlayAudio *plugin;
52         OutputTrack *output;
55 PLUGIN_THREAD_HEADER(OverlayAudio, OverlayAudioThread, OverlayAudioWindow)
57 class OverlayAudio : public PluginAClient
59 public:
60         OverlayAudio(PluginServer *server);
61         ~OverlayAudio();
63         int is_multichannel();
64         int is_realtime();
65         void read_data(KeyFrame *keyframe);
66         void save_data(KeyFrame *keyframe);
67         int process_buffer(int64_t size, 
68                 double **buffer,
69                 int64_t start_position,
70                 int sample_rate);
71         int load_defaults();
72         int save_defaults();
73         void update_gui();
76         PLUGIN_CLASS_MEMBERS(OverlayAudioConfig, OverlayAudioThread)
86 OverlayAudioConfig::OverlayAudioConfig()
88         output_track = OverlayAudioConfig::TOP;
91 int OverlayAudioConfig::equivalent(OverlayAudioConfig &that)
93         return that.output_track == output_track;
96 void OverlayAudioConfig::copy_from(OverlayAudioConfig &that)
98         output_track = that.output_track;
101 void OverlayAudioConfig::interpolate(OverlayAudioConfig &prev, 
102         OverlayAudioConfig &next, 
103         int64_t prev_frame, 
104         int64_t next_frame, 
105         int64_t current_frame)
107         output_track = prev.output_track;
110 char* OverlayAudioConfig::output_to_text(int output_layer)
112         switch(output_layer)
113         {
114                 case OverlayAudioConfig::TOP:    return _("Top");
115                 case OverlayAudioConfig::BOTTOM: return _("Bottom");
116         }
117         return "";
126 OverlayAudioWindow::OverlayAudioWindow(OverlayAudio *plugin, int x, int y)
127  : BC_Window(plugin->gui_string, 
128         x, 
129         y, 
130         400, 
131         100, 
132         400, 
133         100, 
134         0, 
135         0,
136         1)
138         this->plugin = plugin;
141 int OverlayAudioWindow::create_objects()
143         int x = 10, y = 10;
144         BC_Title *title;
145         add_subwindow(title = new BC_Title(x, y, "Output track:"));
146         x += title->get_w() + 10;
147         add_subwindow(output = new OutputTrack(plugin, x, y));
148         output->create_objects();
149         show_window();
150         return 0;
153 WINDOW_CLOSE_EVENT(OverlayAudioWindow)
158 OutputTrack::OutputTrack(OverlayAudio *plugin, int x , int y)
159  : BC_PopupMenu(x, 
160         y, 
161         100,
162         OverlayAudioConfig::output_to_text(plugin->config.output_track),
163         1)
165         this->plugin = plugin;
168 void OutputTrack::create_objects()
170         add_item(new BC_MenuItem(
171                 OverlayAudioConfig::output_to_text(
172                         OverlayAudioConfig::TOP)));
173         add_item(new BC_MenuItem(
174                 OverlayAudioConfig::output_to_text(
175                         OverlayAudioConfig::BOTTOM)));
178 int OutputTrack::handle_event()
180         char *text = get_text();
182         if(!strcmp(text, 
183                 OverlayAudioConfig::output_to_text(
184                         OverlayAudioConfig::TOP)))
185                 plugin->config.output_track = OverlayAudioConfig::TOP;
186         else
187         if(!strcmp(text, 
188                 OverlayAudioConfig::output_to_text(
189                         OverlayAudioConfig::BOTTOM)))
190                 plugin->config.output_track = OverlayAudioConfig::BOTTOM;
192         plugin->send_configure_change();
193         return 1;
197 PLUGIN_THREAD_OBJECT(OverlayAudio, OverlayAudioThread, OverlayAudioWindow)
202 REGISTER_PLUGIN(OverlayAudio)
207 OverlayAudio::OverlayAudio(PluginServer *server)
208  : PluginAClient(server)
210         PLUGIN_CONSTRUCTOR_MACRO
213 OverlayAudio::~OverlayAudio()
215         PLUGIN_DESTRUCTOR_MACRO
218 char* OverlayAudio::plugin_title() { return N_("Overlay"); }
219 int OverlayAudio::is_realtime() { return 1; }
220 int OverlayAudio::is_multichannel() { return 1; }
224 void OverlayAudio::read_data(KeyFrame *keyframe)
226         FileXML input;
227         input.set_shared_string(keyframe->data, strlen(keyframe->data));
229         int result = 0;
230         while(!result)
231         {
232                 result = input.read_tag();
234                 if(!result)
235                 {
236                         if(input.tag.title_is("OVERLAY"))
237                         {
238                                 config.output_track = input.tag.get_property("OUTPUT", config.output_track);
239                         }
240                 }
241         }
244 void OverlayAudio::save_data(KeyFrame *keyframe)
246         FileXML output;
247         output.set_shared_string(keyframe->data, MESSAGESIZE);
249         output.tag.set_title("OVERLAY");
250         output.tag.set_property("OUTPUT", config.output_track);
251         output.append_tag();
252         output.tag.set_title("/OVERLAY");
253         output.append_tag();
254         output.append_newline();
255         output.terminate_string();
258 int OverlayAudio::load_defaults()
260         char directory[BCTEXTLEN];
261         sprintf(directory, "%soverlayaudio.rc", BCASTDIR);
262         defaults = new BC_Hash(directory);
263         defaults->load();
265         config.output_track = defaults->get("OUTPUT", config.output_track);
266         return 0;
269 int OverlayAudio::save_defaults()
271         defaults->update("OUTPUT", config.output_track);
272         defaults->save();
274         return 0;
278 void OverlayAudio::update_gui()
280         if(thread)
281         {
282                 if(load_configuration())
283                 {
284                         thread->window->lock_window("OverlayAudio::update_gui");
285                         thread->window->output->set_text(
286                                 OverlayAudioConfig::output_to_text(config.output_track));
287                         thread->window->unlock_window();
288                 }
289         }
292 NEW_PICON_MACRO(OverlayAudio)
293 SHOW_GUI_MACRO(OverlayAudio, OverlayAudioThread)
294 RAISE_WINDOW_MACRO(OverlayAudio)
295 SET_STRING_MACRO(OverlayAudio)
296 LOAD_CONFIGURATION_MACRO(OverlayAudio, OverlayAudioConfig)
299 int OverlayAudio::process_buffer(int64_t size, 
300         double **buffer,
301         int64_t start_position,
302         int sample_rate)
304         load_configuration();
307         int output_track = 0;
308         if(config.output_track == OverlayAudioConfig::BOTTOM)
309                 output_track = get_total_buffers() - 1;
311 // Direct copy the output track
312         read_samples(buffer[output_track],
313                 output_track,
314                 sample_rate,
315                 start_position,
316                 size);
318 // Add remaining tracks
319         double *output_buffer = buffer[output_track];
320         for(int i = 0; i < get_total_buffers(); i++)
321         {
322                 if(i != output_track)
323                 {
324                         double *input_buffer = buffer[i];
325                         read_samples(buffer[i],
326                                 i,
327                                 sample_rate,
328                                 start_position,
329                                 size);
330                         for(int j = 0; j < size; j++)
331                         {
332                                 output_buffer[j] += input_buffer[j];
333                         }
334                 }
335         }
337         return 0;