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"
29 #include "pbd/filesystem.h"
31 #include "ardour/export_profile_manager.h"
32 #include "ardour/export_format_specification.h"
33 #include "ardour/export_formats_search_path.h"
34 #include "ardour/export_timespan.h"
35 #include "ardour/export_channel_configuration.h"
36 #include "ardour/export_filename.h"
37 #include "ardour/export_preset.h"
38 #include "ardour/export_handler.h"
39 #include "ardour/export_failed.h"
40 #include "ardour/filename_extensions.h"
41 #include "ardour/route.h"
42 #include "ardour/session.h"
43 #include "ardour/broadcast_info.h"
54 ExportProfileManager::ExportProfileManager (Session & s, std::string xml_node_name)
55 : xml_node_name (xml_node_name)
56 , handler (s.get_export_handler())
59 , session_range (new Location (s))
60 , ranges (new LocationList ())
61 , single_range_mode (false)
63 , format_list (new FormatList ())
65 /* Initialize path variables */
67 export_config_dir = user_config_directory();
68 export_config_dir /= "export";
70 search_path += export_formats_search_path();
72 info << string_compose (_("Searching for export formats in %1"), search_path.to_string()) << endmsg;
74 /* create export config directory if necessary */
76 if (!sys::exists (export_config_dir)) {
77 sys::create_directory (export_config_dir);
83 /* Initialize all lists with an empty config */
86 init_timespans (dummy);
87 init_channel_configs (dummy);
89 init_filenames (dummy);
92 ExportProfileManager::~ExportProfileManager ()
94 XMLNode * instant_xml (new XMLNode (xml_node_name));
95 serialize_profile (*instant_xml);
96 session.add_instant_xml (*instant_xml, false);
100 ExportProfileManager::load_profile ()
102 XMLNode * instant_node = session.instant_xml (xml_node_name);
104 set_state (*instant_node);
106 XMLNode empty_node (xml_node_name);
107 set_state (empty_node);
112 ExportProfileManager::prepare_for_export ()
114 TimespanListPtr ts_list = timespans.front()->timespans;
116 FormatStateList::const_iterator format_it;
117 FilenameStateList::const_iterator filename_it;
120 for (TimespanList::iterator ts_it = ts_list->begin(); ts_it != ts_list->end(); ++ts_it) {
121 // ..., each format-filename pair
122 for (format_it = formats.begin(), filename_it = filenames.begin();
123 format_it != formats.end() && filename_it != filenames.end();
124 ++format_it, ++filename_it) {
126 ExportFilenamePtr filename = (*filename_it)->filename;
127 // filename->include_timespan = (ts_list->size() > 1); Disabled for now...
129 boost::shared_ptr<BroadcastInfo> b;
130 if ((*format_it)->format->has_broadcast_info()) {
131 b.reset (new BroadcastInfo);
132 b->set_from_session (session, (*ts_it)->get_start());
135 // ...and each channel config
136 filename->include_channel_config = (channel_configs.size() > 1);
137 for(ChannelConfigStateList::iterator cc_it = channel_configs.begin(); cc_it != channel_configs.end(); ++cc_it) {
138 handler->add_export_config (*ts_it, (*cc_it)->config, (*format_it)->format, filename, b);
145 ExportProfileManager::load_preset (ExportPresetPtr preset)
149 current_preset = preset;
150 if (!preset) { return false; }
152 XMLNode const * state;
153 if ((state = preset->get_local_state())) {
154 set_local_state (*state);
155 } else { ok = false; }
157 if ((state = preset->get_global_state())) {
158 if (!set_global_state (*state)) {
161 } else { ok = false; }
167 ExportProfileManager::load_presets ()
169 vector<std::string> found = find_file (string_compose (X_("*%1"),export_preset_suffix));
171 for (vector<std::string>::iterator it = found.begin(); it != found.end(); ++it) {
172 load_preset_from_disk (*it);
177 ExportProfileManager::preset_filename (std::string const & preset_name)
179 string safe_name = legalize_for_path (preset_name);
180 return export_config_dir.to_string() + "/" + safe_name + export_preset_suffix;
184 ExportProfileManager::new_preset (string const & name)
186 // Generate new ID and do regular save
187 string filename = preset_filename (name);
188 current_preset.reset (new ExportPreset (filename, session));
189 preset_list.push_back (current_preset);
190 return save_preset (name);
194 ExportProfileManager::save_preset (string const & name)
196 string filename = preset_filename (name);
198 if (!current_preset) {
199 current_preset.reset (new ExportPreset (filename, session));
200 preset_list.push_back (current_preset);
203 XMLNode * global_preset = new XMLNode ("ExportPreset");
204 XMLNode * local_preset = new XMLNode ("ExportPreset");
206 serialize_global_profile (*global_preset);
207 serialize_local_profile (*local_preset);
209 current_preset->set_name (name);
210 current_preset->set_global_state (*global_preset);
211 current_preset->set_local_state (*local_preset);
213 current_preset->save (filename);
215 return current_preset;
219 ExportProfileManager::remove_preset ()
221 if (!current_preset) { return; }
223 for (PresetList::iterator it = preset_list.begin(); it != preset_list.end(); ++it) {
224 if (*it == current_preset) {
225 preset_list.erase (it);
230 FileMap::iterator it = preset_file_map.find (current_preset->id());
231 if (it != preset_file_map.end()) {
232 sys::remove (it->second);
233 preset_file_map.erase (it);
236 current_preset->remove_local();
237 current_preset.reset();
241 ExportProfileManager::load_preset_from_disk (PBD::sys::path const & path)
243 ExportPresetPtr preset (new ExportPreset (path.to_string(), session));
245 /* Handle id to filename mapping and don't add duplicates to list */
247 FilePair pair (preset->id(), path);
248 if (preset_file_map.insert (pair).second) {
249 preset_list.push_back (preset);
254 ExportProfileManager::set_state (XMLNode const & root)
256 return set_global_state (root) & set_local_state (root);
260 ExportProfileManager::set_global_state (XMLNode const & root)
262 return init_filenames (root.children ("ExportFilename")) &
263 init_formats (root.children ("ExportFormat"));
267 ExportProfileManager::set_local_state (XMLNode const & root)
269 return init_timespans (root.children ("ExportTimespan")) &
270 init_channel_configs (root.children ("ExportChannelConfiguration"));
274 ExportProfileManager::serialize_profile (XMLNode & root)
276 serialize_local_profile (root);
277 serialize_global_profile (root);
281 ExportProfileManager::serialize_global_profile (XMLNode & root)
283 for (FormatStateList::iterator it = formats.begin(); it != formats.end(); ++it) {
284 root.add_child_nocopy (serialize_format (*it));
287 for (FilenameStateList::iterator it = filenames.begin(); it != filenames.end(); ++it) {
288 root.add_child_nocopy ((*it)->filename->get_state());
293 ExportProfileManager::serialize_local_profile (XMLNode & root)
295 for (TimespanStateList::iterator it = timespans.begin(); it != timespans.end(); ++it) {
296 root.add_child_nocopy (serialize_timespan (*it));
299 for (ChannelConfigStateList::iterator it = channel_configs.begin(); it != channel_configs.end(); ++it) {
300 root.add_child_nocopy ((*it)->config->get_state());
304 std::vector<std::string>
305 ExportProfileManager::find_file (std::string const & pattern)
307 vector<std::string> found;
309 Glib::PatternSpec pattern_spec (pattern);
310 find_matching_files_in_search_path (search_path, pattern_spec, found);
316 ExportProfileManager::set_selection_range (framepos_t start, framepos_t end)
320 selection_range.reset (new Location (session));
321 selection_range->set_name (_("Selection"));
322 selection_range->set (start, end);
324 selection_range.reset();
327 for (TimespanStateList::iterator it = timespans.begin(); it != timespans.end(); ++it) {
328 (*it)->selection_range = selection_range;
333 ExportProfileManager::set_single_range (framepos_t start, framepos_t end, string name)
335 single_range_mode = true;
337 single_range.reset (new Location (session));
338 single_range->set_name (name);
339 single_range->set (start, end);
343 return single_range->id().to_s();
347 ExportProfileManager::init_timespans (XMLNodeList nodes)
353 for (XMLNodeList::const_iterator it = nodes.begin(); it != nodes.end(); ++it) {
354 TimespanStatePtr span = deserialize_timespan (**it);
356 timespans.push_back (span);
357 } else { ok = false; }
360 if (timespans.empty()) {
361 TimespanStatePtr state (new TimespanState (session_range, selection_range, ranges));
362 timespans.push_back (state);
364 // Add session as default selection
365 ExportTimespanPtr timespan = handler->add_timespan();
366 timespan->set_name (session_range->name());
367 timespan->set_range_id ("session");
368 timespan->set_range (session_range->start(), session_range->end());
369 state->timespans->push_back (timespan);
376 ExportProfileManager::TimespanStatePtr
377 ExportProfileManager::deserialize_timespan (XMLNode & root)
379 TimespanStatePtr state (new TimespanState (session_range, selection_range, ranges));
380 XMLProperty const * prop;
382 XMLNodeList spans = root.children ("Range");
383 for (XMLNodeList::iterator node_it = spans.begin(); node_it != spans.end(); ++node_it) {
385 prop = (*node_it)->property ("id");
386 if (!prop) { continue; }
387 string id = prop->value();
389 for (LocationList::iterator it = ranges->begin(); it != ranges->end(); ++it) {
390 if ((!id.compare ("session") && *it == session_range.get()) ||
391 (!id.compare ("selection") && *it == selection_range.get()) ||
392 (!id.compare ((*it)->id().to_s()))) {
393 ExportTimespanPtr timespan = handler->add_timespan();
394 timespan->set_name ((*it)->name());
395 timespan->set_range_id (id);
396 timespan->set_range ((*it)->start(), (*it)->end());
397 state->timespans->push_back (timespan);
402 if ((prop = root.property ("format"))) {
403 state->time_format = (TimeFormat) string_2_enum (prop->value(), TimeFormat);
410 ExportProfileManager::serialize_timespan (TimespanStatePtr state)
412 XMLNode & root = *(new XMLNode ("ExportTimespan"));
417 for (TimespanList::iterator it = state->timespans->begin(); it != state->timespans->end(); ++it) {
418 if ((span = root.add_child ("Range"))) {
419 span->add_property ("id", (*it)->range_id());
423 root.add_property ("format", enum_2_string (state->time_format));
429 ExportProfileManager::update_ranges () {
432 if (single_range_mode) {
433 ranges->push_back (single_range.get());
439 session_range->set_name (_("Session"));
440 session_range->set (session.current_start_frame(), session.current_end_frame());
441 ranges->push_back (session_range.get());
445 if (selection_range) {
446 ranges->push_back (selection_range.get());
451 LocationList const & list (session.locations()->list());
452 for (LocationList::const_iterator it = list.begin(); it != list.end(); ++it) {
453 if ((*it)->is_range_marker()) {
454 ranges->push_back (*it);
459 ExportProfileManager::ChannelConfigStatePtr
460 ExportProfileManager::add_channel_config ()
462 ChannelConfigStatePtr ptr(new ChannelConfigState(handler->add_channel_config()));
463 channel_configs.push_back(ptr);
468 ExportProfileManager::init_channel_configs (XMLNodeList nodes)
470 channel_configs.clear();
473 ChannelConfigStatePtr config (new ChannelConfigState (handler->add_channel_config()));
474 channel_configs.push_back (config);
476 // Add master outs as default
477 IO* master_out = session.master_out()->output().get();
478 if (!master_out) { return false; }
480 for (uint32_t n = 0; n < master_out->n_ports().n_audio(); ++n) {
481 PortExportChannel * channel = new PortExportChannel ();
482 channel->add_port (master_out->audio (n));
484 ExportChannelPtr chan_ptr (channel);
485 config->config->register_channel (chan_ptr);
490 for (XMLNodeList::const_iterator it = nodes.begin(); it != nodes.end(); ++it) {
491 ChannelConfigStatePtr config (new ChannelConfigState (handler->add_channel_config()));
492 config->config->set_state (**it);
493 channel_configs.push_back (config);
499 ExportProfileManager::FormatStatePtr
500 ExportProfileManager::duplicate_format_state (FormatStatePtr state)
502 /* Note: The pointer in the new FormatState should point to the same format spec
503 as the original state's pointer. The spec itself should not be copied! */
505 FormatStatePtr format (new FormatState (format_list, state->format));
506 formats.push_back (format);
511 ExportProfileManager::remove_format_state (FormatStatePtr state)
513 for (FormatStateList::iterator it = formats.begin(); it != formats.end(); ++it) {
522 ExportProfileManager::save_format_to_disk (ExportFormatSpecPtr format)
524 // TODO filename character stripping
526 /* Get filename for file */
528 string new_name = format->name();
529 new_name += export_format_suffix;
531 /* make sure its legal for the filesystem */
533 new_name = legalize_for_path (new_name);
535 sys::path new_path (export_config_dir);
536 new_path /= new_name;
538 /* Check if format is on disk already */
539 FileMap::iterator it;
540 if ((it = format_file_map.find (format->id())) != format_file_map.end()) {
542 /* Check if config is not in user config dir */
543 if (it->second.branch_path().to_string().compare (export_config_dir.to_string())) {
547 XMLTree tree (new_path.to_string());
548 tree.set_root (&format->get_state());
553 /* Update file and rename if necessary */
555 XMLTree tree (it->second.to_string());
556 tree.set_root (&format->get_state());
559 if (new_name.compare (it->second.leaf())) {
560 sys::rename (it->second, new_path);
564 it->second = new_path;
569 XMLTree tree (new_path.to_string());
570 tree.set_root (&format->get_state());
574 FormatListChanged ();
579 ExportProfileManager::remove_format_profile (ExportFormatSpecPtr format)
581 for (FormatList::iterator it = format_list->begin(); it != format_list->end(); ++it) {
583 format_list->erase (it);
588 FileMap::iterator it = format_file_map.find (format->id());
589 if (it != format_file_map.end()) {
590 sys::remove (it->second);
591 format_file_map.erase (it);
594 FormatListChanged ();
598 ExportProfileManager::get_new_format (ExportFormatSpecPtr original)
600 ExportFormatSpecPtr format;
602 format.reset (new ExportFormatSpecification (*original));
604 format = handler->add_format();
605 format->set_name ("empty format");
608 sys::path path = save_format_to_disk (format);
609 FilePair pair (format->id(), path);
610 format_file_map.insert (pair);
612 format_list->push_back (format);
613 FormatListChanged ();
619 ExportProfileManager::init_formats (XMLNodeList nodes)
624 for (XMLNodeList::const_iterator it = nodes.begin(); it != nodes.end(); ++it) {
625 FormatStatePtr format = deserialize_format (**it);
627 formats.push_back (format);
628 } else { ok = false; }
631 if (formats.empty ()) {
632 FormatStatePtr format (new FormatState (format_list, ExportFormatSpecPtr ()));
633 formats.push_back (format);
640 ExportProfileManager::FormatStatePtr
641 ExportProfileManager::deserialize_format (XMLNode & root)
646 if ((prop = root.property ("id"))) {
650 for (FormatList::iterator it = format_list->begin(); it != format_list->end(); ++it) {
651 if ((*it)->id() == id) {
652 return FormatStatePtr (new FormatState (format_list, *it));
656 return FormatStatePtr ();
660 ExportProfileManager::serialize_format (FormatStatePtr state)
662 XMLNode * root = new XMLNode ("ExportFormat");
664 string id = state->format ? state->format->id().to_s() : "";
665 root->add_property ("id", id);
671 ExportProfileManager::load_formats ()
673 vector<std::string> found = find_file (string_compose ("*%1", export_format_suffix));
675 for (vector<std::string>::iterator it = found.begin(); it != found.end(); ++it) {
676 load_format_from_disk (*it);
681 ExportProfileManager::load_format_from_disk (PBD::sys::path const & path)
683 XMLTree const tree (path.to_string());
684 ExportFormatSpecPtr format = handler->add_format (*tree.root());
686 /* Handle id to filename mapping and don't add duplicates to list */
688 FilePair pair (format->id(), path);
689 if (format_file_map.insert (pair).second) {
690 format_list->push_back (format);
693 FormatListChanged ();
696 ExportProfileManager::FilenameStatePtr
697 ExportProfileManager::duplicate_filename_state (FilenameStatePtr state)
699 FilenameStatePtr filename (new FilenameState (handler->add_filename_copy (state->filename)));
700 filenames.push_back (filename);
705 ExportProfileManager::remove_filename_state (FilenameStatePtr state)
707 for (FilenameStateList::iterator it = filenames.begin(); it != filenames.end(); ++it) {
709 filenames.erase (it);
716 ExportProfileManager::get_sample_filename_for_format (ExportFilenamePtr filename, ExportFormatSpecPtr format)
720 if (channel_configs.empty()) { return ""; }
722 std::list<string> filenames;
723 build_filenames (filenames, filename, timespans.front()->timespans,
724 channel_configs.front()->config, format);
726 if (filenames.empty()) { return ""; }
727 return filenames.front();
731 ExportProfileManager::init_filenames (XMLNodeList nodes)
735 for (XMLNodeList::const_iterator it = nodes.begin(); it != nodes.end(); ++it) {
736 ExportFilenamePtr filename = handler->add_filename();
737 filename->set_state (**it);
738 filenames.push_back (FilenameStatePtr (new FilenameState (filename)));
741 if (filenames.empty()) {
742 FilenameStatePtr filename (new FilenameState (handler->add_filename()));
743 filenames.push_back (filename);
750 boost::shared_ptr<ExportProfileManager::Warnings>
751 ExportProfileManager::get_warnings ()
753 boost::shared_ptr<Warnings> warnings (new Warnings ());
755 ChannelConfigStatePtr channel_config_state;
756 if (!channel_configs.empty ()) {
757 channel_config_state = channel_configs.front();
760 TimespanStatePtr timespan_state = timespans.front();
762 /*** Check "global" config ***/
764 TimespanListPtr timespans = timespan_state->timespans;
766 ExportChannelConfigPtr channel_config;
767 if (channel_config_state) {
768 channel_config = channel_config_state->config;
771 /* Check Timespans are not empty */
773 if (timespans->empty()) {
774 warnings->errors.push_back (_("No timespan has been selected!"));
777 if (channel_config_state == 0) {
778 warnings->errors.push_back (_("No channels have been selected!"));
780 /* Check channel config ports */
781 if (!channel_config->all_channels_have_ports ()) {
782 warnings->warnings.push_back (_("Some channels are empty"));
786 /*** Check files ***/
788 if (channel_config_state) {
789 FormatStateList::const_iterator format_it;
790 FilenameStateList::const_iterator filename_it;
791 for (format_it = formats.begin(), filename_it = filenames.begin();
792 format_it != formats.end() && filename_it != filenames.end();
793 ++format_it, ++filename_it) {
794 check_config (warnings, timespan_state, channel_config_state, *format_it, *filename_it);
802 ExportProfileManager::check_config (boost::shared_ptr<Warnings> warnings,
803 TimespanStatePtr timespan_state,
804 ChannelConfigStatePtr channel_config_state,
805 FormatStatePtr format_state,
806 FilenameStatePtr filename_state)
808 TimespanListPtr timespans = timespan_state->timespans;
809 ExportChannelConfigPtr channel_config = channel_config_state->config;
810 ExportFormatSpecPtr format = format_state->format;
811 ExportFilenamePtr filename = filename_state->filename;
813 /* Check format and maximum channel count */
814 if (!format || !format->type()) {
815 warnings->errors.push_back (_("No format selected!"));
816 } else if (!channel_config->get_n_chans()) {
817 warnings->errors.push_back (_("All channels are empty!"));
818 } else if (!check_format (format, channel_config->get_n_chans())) {
819 warnings->errors.push_back (_("One or more of the selected formats is not compatible with this system!"));
820 } else if (format->channel_limit() < channel_config->get_n_chans()) {
821 warnings->errors.push_back
822 (string_compose (_("%1 supports only %2 channels, but you have %3 channels in your channel configuration"),
823 format->format_name(),
824 format->channel_limit(),
825 channel_config->get_n_chans()));
828 if (!warnings->errors.empty()) { return; }
830 /* Check filenames */
832 // filename->include_timespan = (timespans->size() > 1); Disabled for now...
834 std::list<string> paths;
835 build_filenames(paths, filename, timespans, channel_config, format);
837 for (std::list<string>::const_iterator path_it = paths.begin(); path_it != paths.end(); ++path_it) {
839 string path = *path_it;
841 if (sys::exists (sys::path (path))) {
842 warnings->conflicting_filenames.push_back (path);
845 if (format->with_toc()) {
846 string marker_file = handler->get_cd_marker_filename(path, CDMarkerTOC);
847 if (sys::exists (sys::path (marker_file))) {
848 warnings->conflicting_filenames.push_back (marker_file);
852 if (format->with_cue()) {
853 string marker_file = handler->get_cd_marker_filename(path, CDMarkerCUE);
854 if (sys::exists (sys::path (marker_file))) {
855 warnings->conflicting_filenames.push_back (marker_file);
862 ExportProfileManager::check_format (ExportFormatSpecPtr format, uint32_t channels)
864 switch (format->type()) {
865 case ExportFormatBase::T_Sndfile:
866 return check_sndfile_format (format, channels);
869 throw ExportFailed (X_("Invalid format given for ExportFileFactory::check!"));
874 ExportProfileManager::check_sndfile_format (ExportFormatSpecPtr format, unsigned int channels)
877 sf_info.channels = channels;
878 sf_info.samplerate = format->sample_rate ();
879 sf_info.format = format->format_id () | format->sample_format ();
881 return (sf_format_check (&sf_info) == SF_TRUE ? true : false);
885 ExportProfileManager::build_filenames(std::list<std::string> & result, ExportFilenamePtr filename,
886 TimespanListPtr timespans, ExportChannelConfigPtr channel_config,
887 ExportFormatSpecPtr format)
889 for (std::list<ExportTimespanPtr>::iterator timespan_it = timespans->begin();
890 timespan_it != timespans->end(); ++timespan_it) {
891 filename->set_timespan (*timespan_it);
893 if (channel_config->get_split()) {
894 filename->include_channel = true;
896 for (uint32_t chan = 1; chan <= channel_config->get_n_chans(); ++chan) {
897 filename->set_channel (chan);
898 result.push_back(filename->get_path (format));
902 filename->include_channel = false;
903 result.push_back(filename->get_path (format));
908 }; // namespace ARDOUR