use pbd's gstdio compatibility wrapper
[ardour.git] / libs / ardour / export_handler.cc
index b45edc9b9eb48e0b4b099883dded168f1ad2dc20..0def821ef623b5bb874268a74c40098cf96b9940 100644 (file)
@@ -20,7 +20,7 @@
 
 #include "ardour/export_handler.h"
 
-#include <glib/gstdio.h>
+#include <pbd/gstdio_compat.h>
 #include <glibmm.h>
 #include <glibmm/convert.h>
 
@@ -118,7 +118,7 @@ ExportHandler::ExportHandler (Session & session)
 
 ExportHandler::~ExportHandler ()
 {
-       // TODO remove files that were written but not finished
+       graph_builder->cleanup (export_status->aborted () );
 }
 
 /** Add an export to the `to-do' list */
@@ -305,7 +305,18 @@ ExportHandler::finish_timespan ()
                        export_cd_marker_file (current_timespan, fmt, filename, CDMarkerTOC);
                }
 
+               if (fmt->with_mp4chaps()) {
+                       export_cd_marker_file (current_timespan, fmt, filename, MP4Chaps);
+               }
+
                if (fmt->tag()) {
+                       /* close file first, otherwise TagLib enounters an ERROR_SHARING_VIOLATION
+                        * The process cannot access the file because it is being used.
+                        *
+                        * TODO: check Umlauts and encoding in filename.
+                        * TagLib eventually calls CreateFileA(),
+                        */
+                       graph_builder->reset ();
                        AudiofileTagger::tag_file(filename, *SessionMetadata::Metadata());
                }
 
@@ -403,6 +414,11 @@ ExportHandler::export_cd_marker_file (ExportTimespanPtr timespan, ExportFormatSp
                        track_func = &ExportHandler::write_track_info_cue;
                        index_func = &ExportHandler::write_index_info_cue;
                        break;
+               case MP4Chaps:
+                       header_func = &ExportHandler::write_mp4ch_header;
+                       track_func = &ExportHandler::write_track_info_mp4ch;
+                       index_func = &ExportHandler::write_index_info_mp4ch;
+                       break;
                default:
                        return;
                }
@@ -500,17 +516,22 @@ ExportHandler::export_cd_marker_file (ExportTimespanPtr timespan, ExportFormatSp
 string
 ExportHandler::get_cd_marker_filename(std::string filename, CDMarkerFormat format)
 {
-       /* do not strip file suffix because there may be more than one format, 
+       /* do not strip file suffix because there may be more than one format,
           and we do not want the CD marker file from one format to overwrite
           another (e.g. foo.wav.cue > foo.aiff.cue)
        */
 
        switch (format) {
-         case CDMarkerTOC:
+       case CDMarkerTOC:
                return filename + ".toc";
-         case CDMarkerCUE:
+       case CDMarkerCUE:
                return filename + ".cue";
-         default:
+       case MP4Chaps:
+       {
+               unsigned lastdot = filename.find_last_of('.');
+               return filename.substr(0,lastdot) + ".chapters.txt";
+       }
+       default:
                return filename + ".marker"; // Should not be reached when actually creating a file
        }
 }
@@ -519,15 +540,26 @@ void
 ExportHandler::write_cue_header (CDMarkerStatus & status)
 {
        string title = status.timespan->name().compare ("Session") ? status.timespan->name() : (string) session.name();
-       string catalog = SessionMetadata::Metadata()->catalog();
+
+       // Album metadata
+       string barcode      = SessionMetadata::Metadata()->barcode();
+       string album_artist = SessionMetadata::Metadata()->album_artist();
+       string album_title  = SessionMetadata::Metadata()->album();
 
        status.out << "REM Cue file generated by " << PROGRAM_NAME << endl;
-       if (catalog != "")
-               status.out << "CATALOG " << catalog << endl;
+
+       if (barcode != "")
+               status.out << "CATALOG " << barcode << endl;
+
+       if (album_artist != "")
+               status.out << "PERFORMER " << cue_escape_cdtext (album_artist) << endl;
+
+       if (album_title != "")
+               title = album_title;
 
        status.out << "TITLE " << cue_escape_cdtext (title) << endl;
 
-       /*  The original cue sheet sepc metions five file types
+       /*  The original cue sheet spec mentions five file types
                WAVE, AIFF,
                BINARY   = "header-less" audio (44.1 kHz, 16 Bit, little endian),
                MOTOROLA = "header-less" audio (44.1 kHz, 16 Bit, big endian),
@@ -559,15 +591,29 @@ void
 ExportHandler::write_toc_header (CDMarkerStatus & status)
 {
        string title = status.timespan->name().compare ("Session") ? status.timespan->name() : (string) session.name();
-       string catalog = SessionMetadata::Metadata()->catalog();
 
-       if (catalog != "")
-               status.out << "CATALOG " << catalog << endl;
+       // Album metadata
+       string barcode      = SessionMetadata::Metadata()->barcode();
+       string album_artist = SessionMetadata::Metadata()->album_artist();
+       string album_title  = SessionMetadata::Metadata()->album();
+
+       if (barcode != "")
+               status.out << "CATALOG \"" << barcode << "\"" << endl;
+
+       if (album_title != "")
+               title = album_title;
 
        status.out << "CD_DA" << endl;
        status.out << "CD_TEXT {" << endl << "  LANGUAGE_MAP {" << endl << "    0 : EN" << endl << "  }" << endl;
        status.out << "  LANGUAGE 0 {" << endl << "    TITLE " << toc_escape_cdtext (title) << endl ;
-       status.out << "    PERFORMER \"\"" << endl << "  }" << endl << "}" << endl;
+       status.out << "    PERFORMER " << toc_escape_cdtext (album_artist) << endl;
+       status.out << "  }" << endl << "}" << endl;
+}
+
+void
+ExportHandler::write_mp4ch_header (CDMarkerStatus & status)
+{
+       status.out << "00:00:00.000 Intro" << endl;
 }
 
 void
@@ -674,6 +720,14 @@ ExportHandler::write_track_info_toc (CDMarkerStatus & status)
        status.out << "START" << buf << endl;
 }
 
+void ExportHandler::write_track_info_mp4ch (CDMarkerStatus & status)
+{
+       gchar buf[18];
+
+       frames_to_chapter_marks_string(buf, status.track_start_frame);
+       status.out << buf << " " << status.marker->name() << endl;
+}
+
 void
 ExportHandler::write_index_info_cue (CDMarkerStatus & status)
 {
@@ -696,6 +750,11 @@ ExportHandler::write_index_info_toc (CDMarkerStatus & status)
        status.out << "INDEX" << buf << endl;
 }
 
+void
+ExportHandler::write_index_info_mp4ch (CDMarkerStatus & status)
+{
+}
+
 void
 ExportHandler::frames_to_cd_frames_string (char* buf, framepos_t when)
 {
@@ -711,6 +770,23 @@ ExportHandler::frames_to_cd_frames_string (char* buf, framepos_t when)
        sprintf (buf, " %02d:%02d:%02d", mins, secs, frames);
 }
 
+void
+ExportHandler::frames_to_chapter_marks_string (char* buf, framepos_t when)
+{
+       framecnt_t remainder;
+       framecnt_t fr = session.nominal_frame_rate();
+       int hours, mins, secs, msecs;
+
+       hours = when / (3600 * fr);
+       remainder = when - (hours * 3600 * fr);
+       mins = remainder / (60 * fr);
+       remainder -= mins * 60 * fr;
+       secs = remainder / fr;
+       remainder -= secs * fr;
+       msecs = (remainder * 1000) / fr;
+       sprintf (buf, "%02d:%02d:%02d.%03d", hours, mins, secs, msecs);
+}
+
 std::string
 ExportHandler::toc_escape_cdtext (const std::string& txt)
 {