r793: Small API addon, so plugins can 'see' camera and projector automation
[cinelerra_cv/mob.git] / cinelerra / exportedl.C
blobb84bed6b195fd68c9d3abf95c2c2c757e487e196
1 #include "asset.h"
2 #include "condition.h"
3 #include "confirmsave.h"
4 #include "defaults.h"
5 #include "edits.h"
6 #include "edl.h"
7 #include "edlsession.h"
8 #include "errorbox.h"
9 #include "file.h"
10 #include "filesystem.h"
11 #include "filexml.h"
12 #include "language.h"
13 #include "localsession.h"
14 #include "mainsession.h"
15 #include "mutex.h"
16 #include "mwindowgui.h"
17 #include "mwindow.h"
18 #include "exportedl.h"
19 #include "tracks.h"
20 #include "transition.h"
23 #include <ctype.h>
24 #include <string.h>
26 ExportEDLAsset::ExportEDLAsset(MWindow *mwindow, EDL *edl)
28         this->mwindow = mwindow;
29         this->edl = edl;
30         
31         path[0] = 0;
32         edl_type = EDLTYPE_CMX3600;
33         track_number = -1;
36 ExportEDLAsset::~ExportEDLAsset()
40 void ExportEDLAsset::double_to_CMX3600(double seconds, double frame_rate, char *str)
42         char tmp[20];
43         Units::totext(tmp, 
44                         seconds, 
45                         TIME_HMSF, 
46                         0, // sample_rate ... unnecessary 
47                         frame_rate, 
48                         0);    // frames per foot
49         if ((int)(seconds / 3600) <= 9)
50         {
51                 str[0]='0';
52                 strcpy(str+1, tmp);
53         } else
54         {
55                 strcpy(str, tmp);
56         }
57         
58 //      str[8]='.';
60         //sprintf(str, "%02d:%02d:%02d:%02d", hour, minute, second, hundredths);
63 int ExportEDLAsset::edit_to_timecodes(Edit *edit, char *sourceinpoint, char *sourceoutpoint, char *destinpoint, char *destoutpoint, char *reel_name)
65         Asset *asset = edit->asset;
66         Track *track = edit->track;
67         double frame_rate = edit->track->edl->session->frame_rate;
69         double edit_sourcestart;
70         double edit_sourceend;
71         double edit_deststart;
72         double edit_destend;
74         if (asset)
75         {
76                 // reelname should be 8 chars long
77                 
78                 strncpy(reel_name, asset->reel_name, 9);
79                 if (strlen(asset->reel_name) > 8)
80                 {
81                         printf(_("Warning: chopping the reel name to eight characters!\n"));
82                 };
83                 reel_name[8] = 0;
84                 for (int i = strlen(reel_name); i<8; i++)
85                         reel_name[i] = ' ';
86                         
87                 edit_sourcestart = (double)asset->tcstart / asset->frame_rate
88                         + track->from_units(edit->startsource);
89                 edit_sourceend = (double)asset->tcstart / asset->frame_rate
90                         + track->from_units(edit->startsource + edit->length);
92         } else
93         {
94                 strcpy(reel_name, "   BL   ");
95                 edit_sourcestart = 0;
96                 edit_sourceend = track->from_units(edit->length);
97         }
98         
99         edit_deststart = track->from_units(edit->startproject);
100         edit_destend = track->from_units(edit->startproject + edit->length);
102         double_to_CMX3600(edit_sourcestart, frame_rate, sourceinpoint);
103         double_to_CMX3600(edit_sourceend, frame_rate, sourceoutpoint);
104         double_to_CMX3600(edit_deststart, frame_rate, destinpoint);
105         double_to_CMX3600(edit_destend, frame_rate, destoutpoint);
106         
107         return 0;
111 int ExportEDLAsset::export_it()
113         FILE *fh;
114         fh = fopen(path, "w+");
116 // We currently only support exporting one track at a time
117 // Find the track...
118         int serial = 0;
119         Track *track;
120         for(track = edl->tracks->first;
121                 track;
122                 track = track->next)
123         {
124                 if (serial == track_number) 
125                         break;          
126                 serial ++;
127         }
128         
129         
130         int last_dissolve = 1;
132         if (edl_type == EDLTYPE_CMX3600) 
133         {
135                 // TODO: Find docs about exact header for CMX3600
136                 fprintf(fh, "TITLE: Cinproj   FORMAT: CMX 3600 4-Ch\n");
138                 int colnum = 1;
139                 
141                 for (Edit *edit = track->edits->first;
142                         edit;
143                         edit = edit->next)
144                 {
145                         char reel_name[BCTEXTLEN];
146                         char avselect[5];
147                         char edittype[5] = "C   ";
148                         char cutinfo[4] = "   ";
149                         char sourceinpoint[12];
150                         char sourceoutpoint[12];
151                         char destinpoint[12];
152                         char destoutpoint[12];
153                         if (track->data_type == TRACK_AUDIO)
154                                 strcpy(avselect, "A   ");
155                         else
156                                 strcpy(avselect, "V   ");
157                         
158                         //if (edit->transition)
159                         //      printf("title: %s, length: %i\n", edit->transition->title, edit->transition->length);
160                         if (edit->transition && !strcmp(edit->transition->title, "Dissolve"))
161                         {
162                                 char last_sourceout[12];
163                                 edit_to_timecodes(edit->previous, sourceinpoint, last_sourceout, destinpoint, destoutpoint, reel_name);
164                                 edit_to_timecodes(edit, sourceinpoint, sourceoutpoint, destinpoint, destoutpoint, reel_name);
166                                 if (last_dissolve)
167                                 {
168                                         fprintf(fh, "%03d %8s %s %4s %03s", colnum, reel_name, avselect, edittype, cutinfo);
169                                         fprintf(fh, " %s %s", last_sourceout, last_sourceout);
170                                         fprintf(fh, " %s %s", destinpoint, destinpoint);
171                                         fprintf(fh,"\n");               
172                                 } else
173                                 {
174                                         colnum --;
175                                 }
176                                 edittype[0] = 'D';
177                                 fprintf(fh, "%03d %8s %s %4s %03d", colnum, reel_name, avselect, edittype, edit->transition->length);
178                                 fprintf(fh, " %s %s", sourceinpoint, sourceoutpoint);
179                                 fprintf(fh, " %s %s", destinpoint, destoutpoint);
180                                 fprintf(fh,"\n");
181                                 last_dissolve = 1;              
182                         } else
183                         {
184                                                         edit_to_timecodes(edit, sourceinpoint, sourceoutpoint, destinpoint, destoutpoint, reel_name);
185                                 fprintf(fh, "%03d %8s %s %4s %3s", colnum, reel_name, avselect, edittype, cutinfo);
186                                 fprintf(fh, " %s %s", sourceinpoint, sourceoutpoint);
187                                 fprintf(fh, " %s %s", destinpoint, destoutpoint);
188                                 fprintf(fh,"\n");               
189                                 last_dissolve = 0;
190                         }
192                         colnum ++;
193                         
194                 }
195                 
196         }
197                 
198         fclose(fh);
205 int ExportEDLAsset::load_defaults()
207         mwindow->defaults->get("EDLEXPORT_PATH", path);
208         mwindow->defaults->get("EDLEXPORT_TYPE", edl_type);
209         mwindow->defaults->get("EDLEXPORT_TRACKNUMBER", track_number);
210         //load_mode = mwindow->defaults->get("RENDER_LOADMODE", LOAD_NEW_TRACKS);
213         return 0;
216 int ExportEDLAsset::save_defaults()
218         mwindow->defaults->update("EDLEXPORT_PATH", path);
219         mwindow->defaults->update("EDLEXPORT_TYPE", edl_type);
220         mwindow->defaults->update("EDLEXPORT_TRACKNUMBER", track_number);
221         return 0;
227 ExportEDLItem::ExportEDLItem(MWindow *mwindow)
228  : BC_MenuItem(_("Export EDL..."), "Shift+E", 'E')
230         this->mwindow = mwindow;
231         set_shift(1);
234 int ExportEDLItem::handle_event() 
236         mwindow->exportedl->start_interactive();
237         return 1;
244 ExportEDL::ExportEDL(MWindow *mwindow)
245  : Thread(0, 0, 0)
247         this->mwindow = mwindow;
248 //      package_lock = new Mutex("ExportEDL::package_lock");
249 //      counter_lock = new Mutex("ExportEDL::counter_lock");
250 //      completion = new Condition(0, "ExportEDL::completion");
251 //      progress_timer = new Timer;
254 ExportEDL::~ExportEDL()
256 //      delete package_lock;
257 //      delete counter_lock;
258 //      delete completion;
259 ///     if(preferences) delete preferences;
260 //      delete progress_timer;
263 void ExportEDL::start_interactive()
265         if(!Thread::running())
266         {
267                 Thread::start();
268         }
271 void ExportEDL::run()
273         int result = 0;
274         exportasset = new ExportEDLAsset(mwindow, mwindow->edl);
275         
276         exportasset->load_defaults();
278 // Get format from user
279                 result = 0;
280                 int filesok;
282                 do {
283                 // FIX
284                         filesok = 0;
285                         exportedl_window = new ExportEDLWindow(mwindow, this, exportasset);
286                         exportedl_window->create_objects();
287                         result = exportedl_window->run_window();
288                         if (! result) {
289                                 // add to recentlist only on OK
290                                 // Fix "EDL"!
291                                 exportedl_window->path_recent->add_item("EDLPATH", exportasset->path);
292                         }
293                         exportasset->track_number = exportedl_window->track_list->get_selection_number(0, 0);
295                         delete exportedl_window;
296                         exportedl_window = 0;
297                         if (!result)
298                         {
299                                 ArrayList<char*> paths;
300                         
301                                 paths.append(exportasset->path);
302                                 filesok = ConfirmSave::test_files(mwindow, &paths);
303                         }
304                         
305                 } while (!result && filesok);
306         mwindow->save_defaults();
307         exportasset->save_defaults();
309 // FIX
310         if(!result) exportasset->export_it();
313         delete exportasset;
324 #define WIDTH 410
325 #define HEIGHT 400
327 static char *list_titles[] = 
329         N_("No."),
330         N_("Track name")
334 static int list_widths[] = 
336         40,
337         200
339         
340 ExportEDLWindow::ExportEDLWindow(MWindow *mwindow, ExportEDL *exportedl, ExportEDLAsset *exportasset)
341  : BC_Window(PROGRAM_NAME ": Export EDL", 
342         mwindow->gui->get_root_w(0, 1) / 2 - WIDTH / 2,
343         mwindow->gui->get_root_h(1) / 2 - HEIGHT / 2,
344         WIDTH, 
345         HEIGHT,
346         (int)BC_INFINITY,
347         (int)BC_INFINITY,
348         0,
349         0,
350         1)
352         this->mwindow = mwindow;
353         this->exportasset = exportasset;
356 ExportEDLWindow::~ExportEDLWindow()
358 //      delete format_tools;
359 //      delete loadmode;
364 int ExportEDLWindow::create_objects()
366         int x = 5, y = 5;
367         add_subwindow(new BC_Title(x, 
368                 y, 
369                         _("Select a file to export to:")));
370         y += 25;
372         add_subwindow(path_textbox = new ExportEDLPathText(x, y, this));
373         x += 300;
374         path_recent = new BC_RecentList("EDLPATH", mwindow->defaults,
375                                         path_textbox, 10, x, y, 300, 100);
376         add_subwindow(path_recent);
377 // FIX
378         path_recent->load_items("EDLPATH");
380         x += 24;
381         add_subwindow(path_button = new BrowseButton(
382                 mwindow,
383                 this,
384                 path_textbox, 
385                 x, 
386                 y - 4, 
387                 exportasset->path,
388                 _("Output to file"),
389                 _("Select a file to write to:"),
390                 0));
391         
392         y += 34;
393         x = 5;
394         add_subwindow(new BC_Title(x, y, _("Select track to be exported:")));
395         y += 25;
397         
398         items_tracks[0].remove_all_objects();
399         items_tracks[1].remove_all_objects();
400         int serial = 0;
401         if (exportasset->track_number == -1)
402                 exportasset->track_number = 0;
403         for(Track *track = mwindow->edl->tracks->first;
404                 track;
405                 track = track->next)
406         {
407                 
408                 char tmp[10];
409                 sprintf(tmp, "%i\n", serial+1);
410                 
411                 BC_ListBoxItem *listitem = new BC_ListBoxItem(tmp);
412                 if (serial == exportasset->track_number)
413                         listitem->set_selected(1);
414                 items_tracks[0].append(listitem);
415                 items_tracks[1].append(new BC_ListBoxItem(track->title));
416                 serial ++;
417                 
418         }
420         
421         add_subwindow(track_list = new ExportEDLWindowTrackList(this, x, y, 400, 200, items_tracks));
423         y += 5 + track_list->get_h();
424         add_subwindow(new BC_Title(x, y, _("Currently only CMX 3600 format is supported")));
425         
427         add_subwindow(new BC_OKButton(this));
428         add_subwindow(new BC_CancelButton(this));
429         show_window();
430         return 0;
434 ExportEDLPathText::ExportEDLPathText(int x, int y, ExportEDLWindow *window)
435  : BC_TextBox(x, y, 300, 1, window->exportasset->path) 
437         this->window = window; 
439 ExportEDLPathText::~ExportEDLPathText() 
442 int ExportEDLPathText::handle_event() 
444         strcpy(window->exportasset->path, get_text());
445 //      window->handle_event();
448 ExportEDLWindowTrackList::ExportEDLWindowTrackList(ExportEDLWindow *window, 
449         int x, 
450         int y, 
451         int w, 
452         int h, 
453         ArrayList<BC_ListBoxItem*> *track_list)
454  : BC_ListBox(x, 
455                 y, 
456                 w, 
457                 h, 
458                 LISTBOX_TEXT, 
459                 track_list,
460                 list_titles,
461                 list_widths,
462                 2)
464         this->window = window; 
467 int ExportEDLWindowTrackList::handle_event() 
469 //      window->exportasset->track_number = get_selection_number(0, 0);
470 //      printf("aaaaa %i\n", window->exportasset->track_number );
471 //      window->set_done(0);