2 Copyright (C) 2008 Paul Davis
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 #include <ardour/export_handler.h>
23 #include <pbd/convert.h>
24 #include <pbd/filesystem.h>
26 #include <ardour/ardour.h>
27 #include <ardour/configuration.h>
28 #include <ardour/export_timespan.h>
29 #include <ardour/export_channel_configuration.h>
30 #include <ardour/export_status.h>
31 #include <ardour/export_format_specification.h>
32 #include <ardour/export_filename.h>
33 #include <ardour/export_processor.h>
34 #include <ardour/export_failed.h>
42 /*** ExportElementFactory ***/
44 ExportElementFactory::ExportElementFactory (Session & session) :
50 ExportElementFactory::~ExportElementFactory ()
55 ExportElementFactory::TimespanPtr
56 ExportElementFactory::add_timespan ()
58 return TimespanPtr (new ExportTimespan (session.get_export_status(), session.frame_rate()));
61 ExportElementFactory::ChannelConfigPtr
62 ExportElementFactory::add_channel_config ()
64 return ChannelConfigPtr (new ExportChannelConfiguration (session));
67 ExportElementFactory::FormatPtr
68 ExportElementFactory::add_format ()
70 return FormatPtr (new ExportFormatSpecification (session));
73 ExportElementFactory::FormatPtr
74 ExportElementFactory::add_format (XMLNode const & state)
76 return FormatPtr (new ExportFormatSpecification (session, state));
79 ExportElementFactory::FormatPtr
80 ExportElementFactory::add_format_copy (FormatPtr other)
82 return FormatPtr (new ExportFormatSpecification (*other));
85 ExportElementFactory::FilenamePtr
86 ExportElementFactory::add_filename ()
88 return FilenamePtr (new ExportFilename (session));
91 ExportElementFactory::FilenamePtr
92 ExportElementFactory::add_filename_copy (FilenamePtr other)
94 return FilenamePtr (new ExportFilename (*other));
97 /*** ExportHandler ***/
99 ExportHandler::ExportHandler (Session & session) :
100 ExportElementFactory (session),
102 export_status (session.get_export_status ()),
105 processor.reset (new ExportProcessor (session));
107 files_written_connection = ExportProcessor::WritingFile.connect (sigc::mem_fun (files_written, &std::list<Glib::ustring>::push_back));
110 ExportHandler::~ExportHandler ()
112 if (export_status->aborted()) {
113 for (std::list<Glib::ustring>::iterator it = files_written.begin(); it != files_written.end(); ++it) {
114 sys::remove (sys::path (*it));
118 channel_config_connection.disconnect();
119 files_written_connection.disconnect();
123 ExportHandler::add_export_config (TimespanPtr timespan, ChannelConfigPtr channel_config, FormatPtr format, FilenamePtr filename)
125 FileSpec spec (channel_config, format, filename);
126 ConfigPair pair (timespan, spec);
127 config_map.insert (pair);
132 /// Starts exporting the registered configurations
133 /** The following happens, when do_export is called:
134 * 1. Session is prepared in do_export
135 * 2. start_timespan is called, which then registers all necessary channel configs to a timespan
136 * 3. The timespan reads each unique channel into a tempfile and calls Session::stop_export when the end is reached
137 * 4. stop_export emits ExportReadFinished after stopping the transport, this ends up calling finish_timespan
138 * 5. finish_timespan registers all the relevant formats and filenames to relevant channel configurations
139 * 6. finish_timespan does a manual call to timespan_thread_finished, which gets the next channel configuration
140 * for the current timespan, calling write_files for it
141 * 7. write_files writes the actual export files, composing them from the individual channels from tempfiles and
142 * emits FilesWritten when it is done, which ends up calling timespan_thread_finished
143 * 8. Once all channel configs are written, a new timespan is started by calling start_timespan
144 * 9. When all timespans are written the session is taken out of export.
148 ExportHandler::do_export (bool rt)
150 /* Count timespans */
152 export_status->init();
153 std::set<TimespanPtr> timespan_set;
154 for (ConfigMap::iterator it = config_map.begin(); it != config_map.end(); ++it) {
155 timespan_set.insert (it->first);
157 export_status->total_timespans = timespan_set.size();
163 session.ExportReadFinished.connect (sigc::mem_fun (*this, &ExportHandler::finish_timespan));
167 struct LocationSortByStart {
168 bool operator() (Location *a, Location *b) {
169 return a->start() < b->start();
174 ExportHandler::export_cd_marker_file (TimespanPtr timespan, FormatPtr file_format, std::string filename, CDMarkerFormat format)
177 void (ExportHandler::*header_func) (CDMarkerStatus &);
178 void (ExportHandler::*track_func) (CDMarkerStatus &);
179 void (ExportHandler::*index_func) (CDMarkerStatus &);
183 filepath = filename + ".toc";
184 header_func = &ExportHandler::write_toc_header;
185 track_func = &ExportHandler::write_track_info_toc;
186 index_func = &ExportHandler::write_index_info_toc;
189 filepath = filename + ".cue";
190 header_func = &ExportHandler::write_cue_header;
191 track_func = &ExportHandler::write_track_info_cue;
192 index_func = &ExportHandler::write_index_info_cue;
198 CDMarkerStatus status (filepath, timespan, file_format, filename);
201 error << string_compose(_("Editor: cannot open \"%1\" as export file for CD marker file"), filepath) << endmsg;
205 (this->*header_func) (status);
207 /* Get locations and sort */
209 Locations::LocationList const & locations (session.locations()->list());
210 Locations::LocationList::const_iterator i;
211 Locations::LocationList temp;
213 for (i = locations.begin(); i != locations.end(); ++i) {
214 if ((*i)->start() >= timespan->get_start() && (*i)->end() <= timespan->get_end() && (*i)->is_cd_marker() && !(*i)->is_end()) {
220 // TODO One index marker for whole thing
224 LocationSortByStart cmp;
226 Locations::LocationList::const_iterator nexti;
228 /* Start actual marker stuff */
230 nframes_t last_end_time = timespan->get_start(), last_start_time = timespan->get_start();
231 status.track_position = last_start_time - timespan->get_start();
233 for (i = temp.begin(); i != temp.end(); ++i) {
237 if ((*i)->start() < last_end_time) {
238 if ((*i)->is_mark()) {
239 /* Index within track */
241 status.index_position = (*i)->start() - timespan->get_start();
242 (this->*index_func) (status);
248 /* A track, defined by a cd range marker or a cd location marker outside of a cd range */
250 status.track_position = last_end_time - timespan->get_start();
251 status.track_start_frame = (*i)->start() - timespan->get_start(); // everything before this is the pregap
252 status.track_duration = 0;
254 if ((*i)->is_mark()) {
255 // a mark track location needs to look ahead to the next marker's start to determine length
259 if (nexti != temp.end()) {
260 status.track_duration = (*nexti)->start() - last_end_time;
262 last_start_time = (*i)->start();
263 last_end_time = (*nexti)->start();
265 // this was the last marker, use timespan end
266 status.track_duration = timespan->get_end() - last_end_time;
268 last_start_time = (*i)->start();
269 last_end_time = timespan->get_end();
273 status.track_duration = (*i)->end() - last_end_time;
275 last_start_time = (*i)->start();
276 last_end_time = (*i)->end();
279 (this->*track_func) (status);
284 ExportHandler::write_cue_header (CDMarkerStatus & status)
286 Glib::ustring title = status.timespan->name().compare ("Session") ? status.timespan->name() : (Glib::ustring) session.name();
288 status.out << "REM Cue file generated by Ardour" << endl;
289 status.out << "TITLE \"" << title << "\"" << endl;
292 if (!status.format->format_name().compare ("WAV")) {
293 status.out << "FILE " << status.filename << " WAVE" << endl;
295 status.out << "FILE \"" << status.filename << "\" " << status.format->format_name() << endl;
300 ExportHandler::write_toc_header (CDMarkerStatus & status)
302 Glib::ustring title = status.timespan->name().compare ("Session") ? status.timespan->name() : (Glib::ustring) session.name();
304 status.out << "CD_DA" << endl;
305 status.out << "CD_TEXT {" << endl << " LANGUAGE_MAP {" << endl << " 0 : EN" << endl << " }" << endl;
306 status.out << " LANGUAGE 0 {" << endl << " TITLE \"" << title << "\"" << endl << " }" << endl << "}" << endl;
310 ExportHandler::write_track_info_cue (CDMarkerStatus & status)
314 status.out << endl << "TRACK " << status.track_number << " AUDIO" << endl;
315 status.out << "FLAGS " ;
317 if (status.marker->cd_info.find("scms") != status.marker->cd_info.end()) {
318 status.out << "SCMS ";
320 status.out << "DCP ";
323 if (status.marker->cd_info.find("preemph") != status.marker->cd_info.end()) {
328 if (status.marker->cd_info.find("isrc") != status.marker->cd_info.end()) {
329 status.out << "ISRC " << status.marker->cd_info["isrc"] << endl;
332 if (status.marker->name() != "") {
333 status.out << "TITLE \"" << status.marker->name() << "\"" << endl;
336 if (status.marker->cd_info.find("performer") != status.marker->cd_info.end()) {
337 status.out << "PERFORMER \"" << status.marker->cd_info["performer"] << "\"" << endl;
340 if (status.marker->cd_info.find("string_composer") != status.marker->cd_info.end()) {
341 status.out << "SONGWRITER \"" << status.marker->cd_info["string_composer"] << "\"" << endl;
344 frames_to_cd_frames_string (buf, status.track_position);
345 status.out << "INDEX 00" << buf << endl;
347 frames_to_cd_frames_string (buf, status.track_start_frame);
348 status.out << "INDEX 01" << buf << endl;
350 status.index_number = 2;
351 status.track_number++;
355 ExportHandler::write_track_info_toc (CDMarkerStatus & status)
359 status.out << endl << "TRACK AUDIO" << endl;
361 if (status.marker->cd_info.find("scms") != status.marker->cd_info.end()) {
364 status.out << "COPY" << endl;
366 if (status.marker->cd_info.find("preemph") != status.marker->cd_info.end()) {
367 status.out << "PRE_EMPHASIS" << endl;
369 status.out << "NO PRE_EMPHASIS" << endl;
372 if (status.marker->cd_info.find("isrc") != status.marker->cd_info.end()) {
373 status.out << "ISRC \"" << status.marker->cd_info["isrc"] << "\"" << endl;
376 status.out << "CD_TEXT {" << endl << " LANGUAGE 0 {" << endl << " TITLE \"" << status.marker->name() << "\"" << endl;
377 if (status.marker->cd_info.find("performer") != status.marker->cd_info.end()) {
378 status.out << " PERFORMER \"" << status.marker->cd_info["performer"] << "\"" << endl;
380 if (status.marker->cd_info.find("string_composer") != status.marker->cd_info.end()) {
381 status.out << " COMPOSER \"" << status.marker->cd_info["string_composer"] << "\"" << endl;
384 if (status.marker->cd_info.find("isrc") != status.marker->cd_info.end()) {
385 status.out << " ISRC \"";
386 status.out << status.marker->cd_info["isrc"].substr(0,2) << "-";
387 status.out << status.marker->cd_info["isrc"].substr(2,3) << "-";
388 status.out << status.marker->cd_info["isrc"].substr(5,2) << "-";
389 status.out << status.marker->cd_info["isrc"].substr(7,5) << "\"" << endl;
392 status.out << " }" << endl << "}" << endl;
394 frames_to_cd_frames_string (buf, status.track_position);
395 status.out << "FILE \"" << status.filename << "\" " << buf;
397 frames_to_cd_frames_string (buf, status.track_duration);
398 status.out << buf << endl;
400 frames_to_cd_frames_string (buf, status.track_start_frame - status.track_position);
401 status.out << "START" << buf << endl;
405 ExportHandler::write_index_info_cue (CDMarkerStatus & status)
409 snprintf (buf, sizeof(buf), "INDEX %02d", cue_indexnum);
411 frames_to_cd_frames_string (buf, status.index_position);
412 status.out << buf << endl;
418 ExportHandler::write_index_info_toc (CDMarkerStatus & status)
422 frames_to_cd_frames_string (buf, status.index_position - status.track_position);
423 status.out << "INDEX" << buf << endl;
427 ExportHandler::frames_to_cd_frames_string (char* buf, nframes_t when)
430 nframes_t fr = session.nominal_frame_rate();
431 int mins, secs, frames;
433 mins = when / (60 * fr);
434 remainder = when - (mins * 60 * fr);
435 secs = remainder / fr;
436 remainder -= secs * fr;
437 frames = remainder / (fr / 75);
438 sprintf (buf, " %02d:%02d:%02d", mins, secs, frames);
442 ExportHandler::start_timespan ()
444 export_status->timespan++;
446 if (config_map.empty()) {
447 export_status->finish ();
451 current_timespan = config_map.begin()->first;
453 /* Register channel configs with timespan */
455 timespan_bounds = config_map.equal_range (current_timespan);
457 for (ConfigMap::iterator it = timespan_bounds.first; it != timespan_bounds.second; ++it) {
458 it->second.channel_config->register_with_timespan (current_timespan);
461 /* connect stuff and start export */
463 current_timespan->process_connection = session.ProcessExport.connect (sigc::mem_fun (*current_timespan, &ExportTimespan::process));
464 session.start_audio_export (current_timespan->get_start(), realtime);
468 ExportHandler::finish_timespan ()
470 current_timespan->process_connection.disconnect ();
472 /* Register formats and filenames to relevant channel configs */
474 export_status->total_formats = 0;
475 export_status->format = 0;
477 for (ConfigMap::iterator it = timespan_bounds.first; it != timespan_bounds.second; ++it) {
479 export_status->total_formats++;
483 it->second.filename->set_timespan (current_timespan);
484 it->second.filename->set_channel_config (it->second.channel_config);
486 /* Do actual registration */
488 ChannelConfigPtr chan_config = it->second.channel_config;
489 chan_config->register_file_config (it->second.format, it->second.filename);
492 /* Start writing files by doing a manual call to timespan_thread_finished */
494 current_map_it = timespan_bounds.first;
495 timespan_thread_finished ();
499 ExportHandler::timespan_thread_finished ()
501 channel_config_connection.disconnect();
503 if (current_map_it != timespan_bounds.second) {
505 /* Get next configuration as long as no new export process is started */
507 ChannelConfigPtr cc = current_map_it->second.channel_config;
508 while (!cc->write_files(processor)) {
512 if (current_map_it == timespan_bounds.second) {
514 /* reached end of bounds, this call will end up in the else block below */
516 timespan_thread_finished ();
520 cc = current_map_it->second.channel_config;
523 channel_config_connection = cc->FilesWritten.connect (sigc::mem_fun (*this, &ExportHandler::timespan_thread_finished));
526 } else { /* All files are written from current timespan, reset timespan and start new */
528 /* Unregister configs and remove configs with this timespan */
530 for (ConfigMap::iterator it = timespan_bounds.first; it != timespan_bounds.second;) {
531 it->second.channel_config->unregister_all ();
533 ConfigMap::iterator to_erase = it;
535 config_map.erase (to_erase);
538 /* Start new timespan */
545 } // namespace ARDOUR