r1009: Move the dependencies to newer package names
[cinelerra_cv/mob.git] / cinelerra / exportedl.C
blob85aa1b5cfcc2ddf6f45495d6b396cf4d04b4900e
1 #include "asset.h"
2 #include "bchash.h"
3 #include "condition.h"
4 #include "confirmsave.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"
22 #include <libintl.h>
23 #define _(String) gettext(String)
24 #define gettext_noop(String) String
25 #define N_(String) gettext_noop (String)
29 #include <ctype.h>
30 #include <string.h>
32 ExportEDLAsset::ExportEDLAsset(MWindow *mwindow, EDL *edl)
34         this->mwindow = mwindow;
35         this->edl = edl;
36         
37         path[0] = 0;
38         edl_type = EDLTYPE_CMX3600;
39         track_number = -1;
42 ExportEDLAsset::~ExportEDLAsset()
46 void ExportEDLAsset::double_to_CMX3600(double seconds, double frame_rate, char *str)
48         char tmp[20];
49         Units::totext(tmp, 
50                         seconds, 
51                         TIME_HMSF, 
52                         0, // sample_rate ... unnecessary 
53                         frame_rate, 
54                         0);    // frames per foot
55         if ((int)(seconds / 3600) <= 9)
56         {
57                 str[0]='0';
58                 strcpy(str+1, tmp);
59         } else
60         {
61                 strcpy(str, tmp);
62         }
63         
64 //      str[8]='.';
66         //sprintf(str, "%02d:%02d:%02d:%02d", hour, minute, second, hundredths);
69 int ExportEDLAsset::edit_to_timecodes(Edit *edit, char *sourceinpoint, char *sourceoutpoint, char *destinpoint, char *destoutpoint, char *reel_name)
71         Asset *asset = edit->asset;
72         Track *track = edit->track;
73         double frame_rate = edit->track->edl->session->frame_rate;
75         double edit_sourcestart;
76         double edit_sourceend;
77         double edit_deststart;
78         double edit_destend;
80         if (asset)
81         {
82                 // reelname should be 8 chars long
83                 
84                 strncpy(reel_name, asset->reel_name, 9);
85                 if (strlen(asset->reel_name) > 8)
86                 {
87                         printf(_("Warning: chopping the reel name to eight characters!\n"));
88                 };
89                 reel_name[8] = 0;
90                 for (int i = strlen(reel_name); i<8; i++)
91                         reel_name[i] = ' ';
92                         
93                 edit_sourcestart = (double)asset->tcstart / asset->frame_rate
94                         + track->from_units(edit->startsource);
95                 edit_sourceend = (double)asset->tcstart / asset->frame_rate
96                         + track->from_units(edit->startsource + edit->length);
98         } else
99         {
100                 strcpy(reel_name, "   BL   ");
101                 edit_sourcestart = 0;
102                 edit_sourceend = track->from_units(edit->length);
103         }
104         
105         edit_deststart = track->from_units(edit->startproject);
106         edit_destend = track->from_units(edit->startproject + edit->length);
108         double_to_CMX3600(edit_sourcestart, frame_rate, sourceinpoint);
109         double_to_CMX3600(edit_sourceend, frame_rate, sourceoutpoint);
110         double_to_CMX3600(edit_deststart, frame_rate, destinpoint);
111         double_to_CMX3600(edit_destend, frame_rate, destoutpoint);
112         
113         return 0;
117 int ExportEDLAsset::export_it()
119         FILE *fh;
120         fh = fopen(path, "w+");
122 // We currently only support exporting one track at a time
123 // Find the track...
124         int serial = 0;
125         Track *track;
126         for(track = edl->tracks->first;
127                 track;
128                 track = track->next)
129         {
130                 if (serial == track_number) 
131                         break;          
132                 serial ++;
133         }
134         
135         
136         int last_dissolve = 1;
138         if (edl_type == EDLTYPE_CMX3600) 
139         {
141                 // TODO: Find docs about exact header for CMX3600
142                 fprintf(fh, "TITLE: Cinproj   FORMAT: CMX 3600 4-Ch\n");
144                 int colnum = 1;
145                 
147                 for (Edit *edit = track->edits->first;
148                         edit;
149                         edit = edit->next)
150                 {
151                         char reel_name[BCTEXTLEN];
152                         char avselect[5];
153                         char edittype[5] = "C   ";
154                         char cutinfo[4] = "   ";
155                         char sourceinpoint[12];
156                         char sourceoutpoint[12];
157                         char destinpoint[12];
158                         char destoutpoint[12];
159                         if (track->data_type == TRACK_AUDIO)
160                                 strcpy(avselect, "A   ");
161                         else
162                                 strcpy(avselect, "V   ");
163                         
164                         //if (edit->transition)
165                         //      printf("title: %s, length: %i\n", edit->transition->title, edit->transition->length);
166                         if (edit->transition && !strcmp(edit->transition->title, "Dissolve"))
167                         {
168                                 char last_sourceout[12];
169                                 edit_to_timecodes(edit->previous, sourceinpoint, last_sourceout, destinpoint, destoutpoint, reel_name);
170                                 edit_to_timecodes(edit, sourceinpoint, sourceoutpoint, destinpoint, destoutpoint, reel_name);
172                                 if (last_dissolve)
173                                 {
174                                         fprintf(fh, "%03d %8s %s %4s %03s", colnum, reel_name, avselect, edittype, cutinfo);
175                                         fprintf(fh, " %s %s", last_sourceout, last_sourceout);
176                                         fprintf(fh, " %s %s", destinpoint, destinpoint);
177                                         fprintf(fh,"\n");               
178                                 } else
179                                 {
180                                         colnum --;
181                                 }
182                                 edittype[0] = 'D';
183                                 fprintf(fh, "%03d %8s %s %4s %03d", colnum, reel_name, avselect, edittype, edit->transition->length);
184                                 fprintf(fh, " %s %s", sourceinpoint, sourceoutpoint);
185                                 fprintf(fh, " %s %s", destinpoint, destoutpoint);
186                                 fprintf(fh,"\n");
187                                 last_dissolve = 1;              
188                         } else
189                         {
190                                                         edit_to_timecodes(edit, sourceinpoint, sourceoutpoint, destinpoint, destoutpoint, reel_name);
191                                 fprintf(fh, "%03d %8s %s %4s %3s", colnum, reel_name, avselect, edittype, cutinfo);
192                                 fprintf(fh, " %s %s", sourceinpoint, sourceoutpoint);
193                                 fprintf(fh, " %s %s", destinpoint, destoutpoint);
194                                 fprintf(fh,"\n");               
195                                 last_dissolve = 0;
196                         }
198                         colnum ++;
199                         
200                 }
201                 
202         }
203                 
204         fclose(fh);
211 int ExportEDLAsset::load_defaults()
213         mwindow->defaults->get("EDLEXPORT_PATH", path);
214         mwindow->defaults->get("EDLEXPORT_TYPE", edl_type);
215         mwindow->defaults->get("EDLEXPORT_TRACKNUMBER", track_number);
216         //load_mode = mwindow->defaults->get("RENDER_LOADMODE", LOAD_NEW_TRACKS);
219         return 0;
222 int ExportEDLAsset::save_defaults()
224         mwindow->defaults->update("EDLEXPORT_PATH", path);
225         mwindow->defaults->update("EDLEXPORT_TYPE", edl_type);
226         mwindow->defaults->update("EDLEXPORT_TRACKNUMBER", track_number);
227         return 0;
233 ExportEDLItem::ExportEDLItem(MWindow *mwindow)
234  : BC_MenuItem(_("Export EDL..."), "Shift+E", 'E')
236         this->mwindow = mwindow;
237         set_shift(1);
240 int ExportEDLItem::handle_event() 
242         mwindow->exportedl->start_interactive();
243         return 1;
250 ExportEDL::ExportEDL(MWindow *mwindow)
251  : Thread(0, 0, 0)
253         this->mwindow = mwindow;
254 //      package_lock = new Mutex("ExportEDL::package_lock");
255 //      counter_lock = new Mutex("ExportEDL::counter_lock");
256 //      completion = new Condition(0, "ExportEDL::completion");
257 //      progress_timer = new Timer;
260 ExportEDL::~ExportEDL()
262 //      delete package_lock;
263 //      delete counter_lock;
264 //      delete completion;
265 ///     if(preferences) delete preferences;
266 //      delete progress_timer;
269 void ExportEDL::start_interactive()
271         if(!Thread::running())
272         {
273                 Thread::start();
274         }
277 void ExportEDL::run()
279         int result = 0;
280         exportasset = new ExportEDLAsset(mwindow, mwindow->edl);
281         
282         exportasset->load_defaults();
284 // Get format from user
285                 result = 0;
286                 int filesok;
288                 do {
289                 // FIX
290                         filesok = 0;
291                         exportedl_window = new ExportEDLWindow(mwindow, this, exportasset);
292                         exportedl_window->create_objects();
293                         result = exportedl_window->run_window();
294                         if (! result) {
295                                 // add to recentlist only on OK
296                                 // Fix "EDL"!
297                                 exportedl_window->path_recent->add_item("EDLPATH", exportasset->path);
298                         }
299                         exportasset->track_number = exportedl_window->track_list->get_selection_number(0, 0);
301                         delete exportedl_window;
302                         exportedl_window = 0;
303                         if (!result)
304                         {
305                                 ArrayList<char*> paths;
306                         
307                                 paths.append(exportasset->path);
308                                 filesok = ConfirmSave::test_files(mwindow, &paths);
309                         }
310                         
311                 } while (!result && filesok);
312         mwindow->save_defaults();
313         exportasset->save_defaults();
315 // FIX
316         if(!result) exportasset->export_it();
319         delete exportasset;
330 #define WIDTH 410
331 #define HEIGHT 400
333 static char *list_titles[] = 
335         N_("No."),
336         N_("Track name")
340 static int list_widths[] = 
342         40,
343         200
345         
346 ExportEDLWindow::ExportEDLWindow(MWindow *mwindow, ExportEDL *exportedl, ExportEDLAsset *exportasset)
347  : BC_Window(PROGRAM_NAME ": Export EDL", 
348         mwindow->gui->get_root_w(0, 1) / 2 - WIDTH / 2,
349         mwindow->gui->get_root_h(1) / 2 - HEIGHT / 2,
350         WIDTH, 
351         HEIGHT,
352         (int)BC_INFINITY,
353         (int)BC_INFINITY,
354         0,
355         0,
356         1)
358         this->mwindow = mwindow;
359         this->exportasset = exportasset;
362 ExportEDLWindow::~ExportEDLWindow()
364 //      delete format_tools;
365 //      delete loadmode;
370 int ExportEDLWindow::create_objects()
372         int x = 5, y = 5;
373         add_subwindow(new BC_Title(x, 
374                 y, 
375                         _("Select a file to export to:")));
376         y += 25;
378         add_subwindow(path_textbox = new ExportEDLPathText(x, y, this));
379         x += 300;
380         path_recent = new BC_RecentList("EDLPATH", mwindow->defaults,
381                                         path_textbox, 10, x, y, 300, 100);
382         add_subwindow(path_recent);
383 // FIX
384         path_recent->load_items("EDLPATH");
386         x += 24;
387         add_subwindow(path_button = new BrowseButton(
388                 mwindow,
389                 this,
390                 path_textbox, 
391                 x, 
392                 y - 4, 
393                 exportasset->path,
394                 _("Output to file"),
395                 _("Select a file to write to:"),
396                 0));
397         
398         y += 34;
399         x = 5;
400         add_subwindow(new BC_Title(x, y, _("Select track to be exported:")));
401         y += 25;
403         
404         items_tracks[0].remove_all_objects();
405         items_tracks[1].remove_all_objects();
406         int serial = 0;
407         if (exportasset->track_number == -1)
408                 exportasset->track_number = 0;
409         for(Track *track = mwindow->edl->tracks->first;
410                 track;
411                 track = track->next)
412         {
413                 
414                 char tmp[10];
415                 sprintf(tmp, "%i\n", serial+1);
416                 
417                 BC_ListBoxItem *listitem = new BC_ListBoxItem(tmp);
418                 if (serial == exportasset->track_number)
419                         listitem->set_selected(1);
420                 items_tracks[0].append(listitem);
421                 items_tracks[1].append(new BC_ListBoxItem(track->title));
422                 serial ++;
423                 
424         }
426         
427         add_subwindow(track_list = new ExportEDLWindowTrackList(this, x, y, 400, 200, items_tracks));
429         y += 5 + track_list->get_h();
430         add_subwindow(new BC_Title(x, y, _("Currently only CMX 3600 format is supported")));
431         
433         add_subwindow(new BC_OKButton(this));
434         add_subwindow(new BC_CancelButton(this));
435         show_window();
436         return 0;
440 ExportEDLPathText::ExportEDLPathText(int x, int y, ExportEDLWindow *window)
441  : BC_TextBox(x, y, 300, 1, window->exportasset->path) 
443         this->window = window; 
445 ExportEDLPathText::~ExportEDLPathText() 
448 int ExportEDLPathText::handle_event() 
450         strcpy(window->exportasset->path, get_text());
451 //      window->handle_event();
454 ExportEDLWindowTrackList::ExportEDLWindowTrackList(ExportEDLWindow *window, 
455         int x, 
456         int y, 
457         int w, 
458         int h, 
459         ArrayList<BC_ListBoxItem*> *track_list)
460  : BC_ListBox(x, 
461                 y, 
462                 w, 
463                 h, 
464                 LISTBOX_TEXT, 
465                 track_list,
466                 list_titles,
467                 list_widths,
468                 2)
470         this->window = window; 
473 int ExportEDLWindowTrackList::handle_event() 
475 //      window->exportasset->track_number = get_selection_number(0, 0);
476 //      printf("aaaaa %i\n", window->exportasset->track_number );
477 //      window->set_done(0);