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.
24 #include <glibmm/fileutils.h>
26 #include "pbd/enumwriter.h"
27 #include "pbd/xml++.h"
28 #include "pbd/convert.h"
30 #include "ardour/export_profile_manager.h"
31 #include "ardour/export_format_specification.h"
32 #include "ardour/export_timespan.h"
33 #include "ardour/export_channel_configuration.h"
34 #include "ardour/export_filename.h"
35 #include "ardour/export_preset.h"
36 #include "ardour/export_handler.h"
37 #include "ardour/export_failed.h"
38 #include "ardour/filename_extensions.h"
39 #include "ardour/route.h"
40 #include "ardour/session.h"
41 #include "ardour/broadcast_info.h"
52 ExportProfileManager::ExportProfileManager (Session & s) :
53 handler (s.get_export_handler()),
56 session_range (new Location (s)),
57 ranges (new LocationList ()),
58 single_range_mode (false),
60 format_list (new FormatList ())
63 /* Initialize path variables */
65 export_config_dir = user_config_directory();
66 export_config_dir /= "export";
67 search_path += export_config_dir;
69 search_path += ardour_search_path().add_subdirectory_to_paths("export");
71 sys::path sys_export = ardour_module_directory();
72 sys_export /= "export";
74 search_path += sys_export;
76 info << string_compose (_("Searching for export formats in %1"), search_path.to_string()) << endmsg;
77 cerr << string_compose (_("Searching for export formats in %1"), search_path.to_string()) << endl;
79 /* create export config directory if necessary */
81 if (!sys::exists (export_config_dir)) {
82 sys::create_directory (export_config_dir);
88 /* Initialize all lists with an empty config */
91 init_timespans (dummy);
92 init_channel_configs (dummy);
94 init_filenames (dummy);
97 ExportProfileManager::~ExportProfileManager ()
99 if (single_range_mode) { return; }
101 XMLNode * instant_xml (new XMLNode ("ExportProfile"));
102 serialize_profile (*instant_xml);
103 session.add_instant_xml (*instant_xml, false);
107 ExportProfileManager::load_profile ()
109 XMLNode * instant_node = session.instant_xml ("ExportProfile");
111 set_state (*instant_node);
113 XMLNode empty_node ("ExportProfile");
114 set_state (empty_node);
119 ExportProfileManager::prepare_for_export ()
121 ChannelConfigPtr channel_config = channel_configs.front()->config;
122 TimespanListPtr ts_list = timespans.front()->timespans;
124 FormatStateList::const_iterator format_it;
125 FilenameStateList::const_iterator filename_it;
127 for (TimespanList::iterator ts_it = ts_list->begin(); ts_it != ts_list->end(); ++ts_it) {
128 for (format_it = formats.begin(), filename_it = filenames.begin();
129 format_it != formats.end() && filename_it != filenames.end();
130 ++format_it, ++filename_it) {
132 // filename->include_timespan = (ts_list->size() > 1); Disabled for now...
134 boost::shared_ptr<BroadcastInfo> b;
135 if ((*format_it)->format->has_broadcast_info()) {
136 b.reset (new BroadcastInfo);
137 b->set_from_session (session, (*ts_it)->get_start());
140 handler->add_export_config (*ts_it, channel_config, (*format_it)->format, (*filename_it)->filename, b);
146 ExportProfileManager::load_preset (PresetPtr preset)
150 current_preset = preset;
151 if (!preset) { return false; }
153 XMLNode const * state;
154 if ((state = preset->get_local_state())) {
155 set_local_state (*state);
156 } else { ok = false; }
158 if ((state = preset->get_global_state())) {
159 if (!set_global_state (*state)) {
162 } else { ok = false; }
168 ExportProfileManager::load_presets ()
170 vector<sys::path> found = find_file (string_compose (X_("*%1"),export_preset_suffix));
172 for (vector<sys::path>::iterator it = found.begin(); it != found.end(); ++it) {
173 load_preset_from_disk (*it);
177 ExportProfileManager::PresetPtr
178 ExportProfileManager::save_preset (string const & name)
180 string filename = export_config_dir.to_string() + "/" + name + export_preset_suffix;
182 if (!current_preset) {
183 current_preset.reset (new ExportPreset (filename, session));
184 preset_list.push_back (current_preset);
187 XMLNode * global_preset = new XMLNode ("ExportPreset");
188 XMLNode * local_preset = new XMLNode ("ExportPreset");
190 serialize_global_profile (*global_preset);
191 serialize_local_profile (*local_preset);
193 current_preset->set_name (name);
194 current_preset->set_global_state (*global_preset);
195 current_preset->set_local_state (*local_preset);
197 current_preset->save (filename);
199 return current_preset;
203 ExportProfileManager::remove_preset ()
205 if (!current_preset) { return; }
207 for (PresetList::iterator it = preset_list.begin(); it != preset_list.end(); ++it) {
208 if (*it == current_preset) {
209 preset_list.erase (it);
214 FileMap::iterator it = preset_file_map.find (current_preset->id());
215 if (it != preset_file_map.end()) {
216 sys::remove (it->second);
217 preset_file_map.erase (it);
220 current_preset->remove_local();
221 current_preset.reset();
225 ExportProfileManager::load_preset_from_disk (PBD::sys::path const & path)
227 PresetPtr preset (new ExportPreset (path.to_string(), session));
229 /* Handle id to filename mapping and don't add duplicates to list */
231 FilePair pair (preset->id(), path);
232 if (preset_file_map.insert (pair).second) {
233 preset_list.push_back (preset);
238 ExportProfileManager::set_state (XMLNode const & root)
240 return set_global_state (root) & set_local_state (root);
244 ExportProfileManager::set_global_state (XMLNode const & root)
246 return init_filenames (root.children ("ExportFilename")) &
247 init_formats (root.children ("ExportFormat"));
251 ExportProfileManager::set_local_state (XMLNode const & root)
253 return init_timespans (root.children ("ExportTimespan")) &
254 init_channel_configs (root.children ("ExportChannelConfiguration"));
258 ExportProfileManager::serialize_profile (XMLNode & root)
260 serialize_local_profile (root);
261 serialize_global_profile (root);
265 ExportProfileManager::serialize_global_profile (XMLNode & root)
267 for (FormatStateList::iterator it = formats.begin(); it != formats.end(); ++it) {
268 root.add_child_nocopy (serialize_format (*it));
271 for (FilenameStateList::iterator it = filenames.begin(); it != filenames.end(); ++it) {
272 root.add_child_nocopy ((*it)->filename->get_state());
277 ExportProfileManager::serialize_local_profile (XMLNode & root)
279 for (TimespanStateList::iterator it = timespans.begin(); it != timespans.end(); ++it) {
280 root.add_child_nocopy (serialize_timespan (*it));
283 for (ChannelConfigStateList::iterator it = channel_configs.begin(); it != channel_configs.end(); ++it) {
284 root.add_child_nocopy ((*it)->config->get_state());
288 std::vector<sys::path>
289 ExportProfileManager::find_file (std::string const & pattern)
291 vector<sys::path> found;
293 Glib::PatternSpec pattern_spec (pattern);
294 find_matching_files_in_search_path (search_path, pattern_spec, found);
300 ExportProfileManager::set_selection_range (framepos_t start, framepos_t end)
304 selection_range.reset (new Location (session));
305 selection_range->set_name (_("Selection"));
306 selection_range->set (start, end);
308 selection_range.reset();
311 for (TimespanStateList::iterator it = timespans.begin(); it != timespans.end(); ++it) {
312 (*it)->selection_range = selection_range;
317 ExportProfileManager::set_single_range (framepos_t start, framepos_t end, string name)
319 single_range_mode = true;
321 single_range.reset (new Location (session));
322 single_range->set_name (name);
323 single_range->set (start, end);
327 return single_range->id().to_s();
331 ExportProfileManager::init_timespans (XMLNodeList nodes)
337 for (XMLNodeList::const_iterator it = nodes.begin(); it != nodes.end(); ++it) {
338 TimespanStatePtr span = deserialize_timespan (**it);
340 timespans.push_back (span);
341 } else { ok = false; }
344 if (timespans.empty()) {
345 TimespanStatePtr state (new TimespanState (session_range, selection_range, ranges));
346 timespans.push_back (state);
348 // Add session as default selection
349 TimespanPtr timespan = handler->add_timespan();
350 timespan->set_name (session_range->name());
351 timespan->set_range_id ("session");
352 timespan->set_range (session_range->start(), session_range->end());
353 state->timespans->push_back (timespan);
360 ExportProfileManager::TimespanStatePtr
361 ExportProfileManager::deserialize_timespan (XMLNode & root)
363 TimespanStatePtr state (new TimespanState (session_range, selection_range, ranges));
364 XMLProperty const * prop;
366 XMLNodeList spans = root.children ("Range");
367 for (XMLNodeList::iterator node_it = spans.begin(); node_it != spans.end(); ++node_it) {
369 prop = (*node_it)->property ("id");
370 if (!prop) { continue; }
371 string id = prop->value();
373 for (LocationList::iterator it = ranges->begin(); it != ranges->end(); ++it) {
374 if ((!id.compare ("session") && *it == session_range.get()) ||
375 (!id.compare ("selection") && *it == selection_range.get()) ||
376 (!id.compare ((*it)->id().to_s()))) {
377 TimespanPtr timespan = handler->add_timespan();
378 timespan->set_name ((*it)->name());
379 timespan->set_range_id (id);
380 timespan->set_range ((*it)->start(), (*it)->end());
381 state->timespans->push_back (timespan);
386 if ((prop = root.property ("format"))) {
387 state->time_format = (TimeFormat) string_2_enum (prop->value(), TimeFormat);
394 ExportProfileManager::serialize_timespan (TimespanStatePtr state)
396 XMLNode & root = *(new XMLNode ("ExportTimespan"));
401 for (TimespanList::iterator it = state->timespans->begin(); it != state->timespans->end(); ++it) {
402 if ((span = root.add_child ("Range"))) {
403 span->add_property ("id", (*it)->range_id());
407 root.add_property ("format", enum_2_string (state->time_format));
413 ExportProfileManager::update_ranges () {
416 if (single_range_mode) {
417 ranges->push_back (single_range.get());
423 session_range->set_name (_("Session"));
424 session_range->set (session.current_start_frame(), session.current_end_frame());
425 ranges->push_back (session_range.get());
429 if (selection_range) {
430 ranges->push_back (selection_range.get());
435 LocationList const & list (session.locations()->list());
436 for (LocationList::const_iterator it = list.begin(); it != list.end(); ++it) {
437 if ((*it)->is_range_marker()) {
438 ranges->push_back (*it);
444 ExportProfileManager::init_channel_configs (XMLNodeList nodes)
446 channel_configs.clear();
449 ChannelConfigStatePtr config (new ChannelConfigState (handler->add_channel_config()));
450 channel_configs.push_back (config);
452 // Add master outs as default
453 IO* master_out = session.master_out()->output().get();
454 if (!master_out) { return false; }
456 for (uint32_t n = 0; n < master_out->n_ports().n_audio(); ++n) {
457 PortExportChannel * channel = new PortExportChannel ();
458 channel->add_port (master_out->audio (n));
460 ExportChannelPtr chan_ptr (channel);
461 config->config->register_channel (chan_ptr);
466 for (XMLNodeList::const_iterator it = nodes.begin(); it != nodes.end(); ++it) {
467 ChannelConfigStatePtr config (new ChannelConfigState (handler->add_channel_config()));
468 config->config->set_state (**it);
469 channel_configs.push_back (config);
475 ExportProfileManager::FormatStatePtr
476 ExportProfileManager::duplicate_format_state (FormatStatePtr state)
478 /* Note: The pointer in the new FormatState should point to the same format spec
479 as the original state's pointer. The spec itself should not be copied! */
481 FormatStatePtr format (new FormatState (format_list, state->format));
482 formats.push_back (format);
487 ExportProfileManager::remove_format_state (FormatStatePtr state)
489 for (FormatStateList::iterator it = formats.begin(); it != formats.end(); ++it) {
498 ExportProfileManager::save_format_to_disk (FormatPtr format)
500 // TODO filename character stripping
502 /* Get filename for file */
504 string new_name = format->name();
505 new_name += export_format_suffix;
507 sys::path new_path (export_config_dir);
508 new_path /= new_name;
510 /* Check if format is on disk already */
511 FileMap::iterator it;
512 if ((it = format_file_map.find (format->id())) != format_file_map.end()) {
514 /* Check if config is not in user config dir */
515 if (it->second.branch_path().to_string().compare (export_config_dir.to_string())) {
519 XMLTree tree (new_path.to_string());
520 tree.set_root (&format->get_state());
525 /* Update file and rename if necessary */
527 XMLTree tree (it->second.to_string());
528 tree.set_root (&format->get_state());
531 if (new_name.compare (it->second.leaf())) {
532 sys::rename (it->second, new_path);
536 it->second = new_path;
541 XMLTree tree (new_path.to_string());
542 tree.set_root (&format->get_state());
546 FormatListChanged ();
551 ExportProfileManager::remove_format_profile (FormatPtr format)
553 for (FormatList::iterator it = format_list->begin(); it != format_list->end(); ++it) {
555 format_list->erase (it);
560 FileMap::iterator it = format_file_map.find (format->id());
561 if (it != format_file_map.end()) {
562 sys::remove (it->second);
563 format_file_map.erase (it);
566 FormatListChanged ();
569 ExportProfileManager::FormatPtr
570 ExportProfileManager::get_new_format (FormatPtr original)
574 format.reset (new ExportFormatSpecification (*original));
576 format = handler->add_format();
577 format->set_name ("empty format");
580 sys::path path = save_format_to_disk (format);
581 FilePair pair (format->id(), path);
582 format_file_map.insert (pair);
584 format_list->push_back (format);
585 FormatListChanged ();
591 ExportProfileManager::init_formats (XMLNodeList nodes)
596 for (XMLNodeList::const_iterator it = nodes.begin(); it != nodes.end(); ++it) {
597 FormatStatePtr format = deserialize_format (**it);
599 formats.push_back (format);
600 } else { ok = false; }
603 if (formats.empty ()) {
604 FormatStatePtr format (new FormatState (format_list, FormatPtr ()));
605 formats.push_back (format);
612 ExportProfileManager::FormatStatePtr
613 ExportProfileManager::deserialize_format (XMLNode & root)
618 if ((prop = root.property ("id"))) {
622 for (FormatList::iterator it = format_list->begin(); it != format_list->end(); ++it) {
623 if ((*it)->id() == id) {
624 return FormatStatePtr (new FormatState (format_list, *it));
628 return FormatStatePtr ();
632 ExportProfileManager::serialize_format (FormatStatePtr state)
634 XMLNode * root = new XMLNode ("ExportFormat");
636 string id = state->format ? state->format->id().to_s() : "";
637 root->add_property ("id", id);
643 ExportProfileManager::load_formats ()
645 vector<sys::path> found = find_file (string_compose ("*%1", export_format_suffix));
647 for (vector<sys::path>::iterator it = found.begin(); it != found.end(); ++it) {
648 load_format_from_disk (*it);
653 ExportProfileManager::load_format_from_disk (PBD::sys::path const & path)
655 XMLTree const tree (path.to_string());
656 FormatPtr format = handler->add_format (*tree.root());
658 /* Handle id to filename mapping and don't add duplicates to list */
660 FilePair pair (format->id(), path);
661 if (format_file_map.insert (pair).second) {
662 format_list->push_back (format);
665 FormatListChanged ();
668 ExportProfileManager::FilenameStatePtr
669 ExportProfileManager::duplicate_filename_state (FilenameStatePtr state)
671 FilenameStatePtr filename (new FilenameState (handler->add_filename_copy (state->filename)));
672 filenames.push_back (filename);
677 ExportProfileManager::remove_filename_state (FilenameStatePtr state)
679 for (FilenameStateList::iterator it = filenames.begin(); it != filenames.end(); ++it) {
681 filenames.erase (it);
688 ExportProfileManager::init_filenames (XMLNodeList nodes)
692 for (XMLNodeList::const_iterator it = nodes.begin(); it != nodes.end(); ++it) {
693 FilenamePtr filename = handler->add_filename();
694 filename->set_state (**it);
695 filenames.push_back (FilenameStatePtr (new FilenameState (filename)));
698 if (filenames.empty()) {
699 FilenameStatePtr filename (new FilenameState (handler->add_filename()));
700 filenames.push_back (filename);
707 boost::shared_ptr<ExportProfileManager::Warnings>
708 ExportProfileManager::get_warnings ()
710 boost::shared_ptr<Warnings> warnings (new Warnings ());
712 ChannelConfigStatePtr channel_config_state = channel_configs.front();
713 TimespanStatePtr timespan_state = timespans.front();
715 /*** Check "global" config ***/
717 TimespanListPtr timespans = timespan_state->timespans;
718 ChannelConfigPtr channel_config = channel_config_state->config;
720 /* Check Timespans are not empty */
722 if (timespans->empty()) {
723 warnings->errors.push_back (_("No timespan has been selected!"));
726 /* Check channel config ports */
728 if (!channel_config->all_channels_have_ports ()) {
729 warnings->warnings.push_back (_("Some channels are empty"));
732 /*** Check files ***/
734 FormatStateList::const_iterator format_it;
735 FilenameStateList::const_iterator filename_it;
736 for (format_it = formats.begin(), filename_it = filenames.begin();
737 format_it != formats.end() && filename_it != filenames.end();
738 ++format_it, ++filename_it) {
739 check_config (warnings, timespan_state, channel_config_state, *format_it, *filename_it);
746 ExportProfileManager::check_config (boost::shared_ptr<Warnings> warnings,
747 TimespanStatePtr timespan_state,
748 ChannelConfigStatePtr channel_config_state,
749 FormatStatePtr format_state,
750 FilenameStatePtr filename_state)
752 TimespanListPtr timespans = timespan_state->timespans;
753 ChannelConfigPtr channel_config = channel_config_state->config;
754 FormatPtr format = format_state->format;
755 FilenamePtr filename = filename_state->filename;
757 /* Check format and maximum channel count */
758 if (!format || !format->type()) {
759 warnings->errors.push_back (_("No format selected!"));
760 } else if (!channel_config->get_n_chans()) {
761 warnings->errors.push_back (_("All channels are empty!"));
762 } else if (!check_format (format, channel_config->get_n_chans())) {
763 warnings->errors.push_back (_("One or more of the selected formats is not compatible with this system!"));
764 } else if (format->channel_limit() < channel_config->get_n_chans()) {
765 warnings->errors.push_back
766 (string_compose (_("%1 supports only %2 channels, but you have %3 channels in your channel configuration"),
767 format->format_name(),
768 format->channel_limit(),
769 channel_config->get_n_chans()));
772 if (!warnings->errors.empty()) { return; }
774 /* Check filenames */
776 // filename->include_timespan = (timespans->size() > 1); Disabled for now...
778 for (std::list<TimespanPtr>::iterator timespan_it = timespans->begin(); timespan_it != timespans->end(); ++timespan_it) {
779 filename->set_timespan (*timespan_it);
781 if (channel_config->get_split()) {
782 filename->include_channel = true;
784 for (uint32_t chan = 1; chan <= channel_config->get_n_chans(); ++chan) {
785 filename->set_channel (chan);
787 string path = filename->get_path (format);
789 if (sys::exists (sys::path (path))) {
790 warnings->conflicting_filenames.push_back (path);
795 string path = filename->get_path (format);
797 if (sys::exists (sys::path (path))) {
798 warnings->conflicting_filenames.push_back (path);
805 ExportProfileManager::check_format (FormatPtr format, uint32_t channels)
807 switch (format->type()) {
808 case ExportFormatBase::T_Sndfile:
809 return check_sndfile_format (format, channels);
812 throw ExportFailed (X_("Invalid format given for ExportFileFactory::check!"));
817 ExportProfileManager::check_sndfile_format (FormatPtr format, unsigned int channels)
820 sf_info.channels = channels;
821 sf_info.samplerate = format->sample_rate ();
822 sf_info.format = format->format_id () | format->sample_format ();
824 return (sf_format_check (&sf_info) == SF_TRUE ? true : false);
827 }; // namespace ARDOUR