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.
26 #include <glib/gstdio.h>
28 #include <glibmm/fileutils.h>
29 #include <glibmm/miscutils.h>
31 #include "pbd/enumwriter.h"
32 #include "pbd/xml++.h"
33 #include "pbd/convert.h"
35 #include "ardour/export_profile_manager.h"
36 #include "ardour/export_format_specification.h"
37 #include "ardour/export_formats_search_path.h"
38 #include "ardour/export_timespan.h"
39 #include "ardour/export_channel_configuration.h"
40 #include "ardour/export_filename.h"
41 #include "ardour/export_preset.h"
42 #include "ardour/export_handler.h"
43 #include "ardour/export_failed.h"
44 #include "ardour/directory_names.h"
45 #include "ardour/filename_extensions.h"
46 #include "ardour/route.h"
47 #include "ardour/session.h"
48 #include "ardour/broadcast_info.h"
59 ExportProfileManager::ExportProfileManager (Session & s, ExportType type)
61 , handler (s.get_export_handler())
64 , ranges (new LocationList ())
65 , single_range_mode (false)
67 , format_list (new FormatList ())
71 xml_node_name = X_("ExportProfile");
74 xml_node_name = X_("RangeExportProfile");
77 xml_node_name = X_("SelectionExportProfile");
80 xml_node_name = X_("RegionExportProfile");
83 xml_node_name = X_("StemExportProfile");
87 /* Initialize path variables */
89 export_config_dir = Glib::build_filename (user_config_directory(), export_dir_name);
91 search_path += export_formats_search_path();
93 info << string_compose (_("Searching for export formats in %1"), search_path.to_string()) << endmsg;
95 /* create export config directory if necessary */
97 if (!Glib::file_test (export_config_dir, Glib::FILE_TEST_EXISTS)) {
98 if (g_mkdir_with_parents (export_config_dir.c_str(), 0755) != 0) {
99 error << string_compose (_("Unable to create export format directory %1: %2"), export_config_dir, g_strerror(errno)) << endmsg;
106 /* Initialize all lists with an empty config */
109 init_timespans (dummy);
110 init_channel_configs (dummy);
111 init_formats (dummy);
112 init_filenames (dummy);
115 ExportProfileManager::~ExportProfileManager ()
117 XMLNode * instant_xml (new XMLNode (xml_node_name));
118 serialize_profile (*instant_xml);
119 session.add_instant_xml (*instant_xml, false);
123 ExportProfileManager::load_profile ()
125 XMLNode * instant_node = session.instant_xml (xml_node_name);
127 set_state (*instant_node);
129 XMLNode empty_node (xml_node_name);
130 set_state (empty_node);
135 ExportProfileManager::prepare_for_export ()
137 TimespanListPtr ts_list = timespans.front()->timespans;
139 FormatStateList::const_iterator format_it;
140 FilenameStateList::const_iterator filename_it;
143 for (TimespanList::iterator ts_it = ts_list->begin(); ts_it != ts_list->end(); ++ts_it) {
144 // ..., each format-filename pair
145 for (format_it = formats.begin(), filename_it = filenames.begin();
146 format_it != formats.end() && filename_it != filenames.end();
147 ++format_it, ++filename_it) {
149 ExportFilenamePtr filename = (*filename_it)->filename;
150 // filename->include_timespan = (ts_list->size() > 1); Disabled for now...
152 boost::shared_ptr<BroadcastInfo> b;
153 if ((*format_it)->format->has_broadcast_info()) {
154 b.reset (new BroadcastInfo);
155 b->set_from_session (session, (*ts_it)->get_start());
158 // ...and each channel config
159 filename->include_channel_config = (type == StemExport) ||
160 (channel_configs.size() > 1);
161 for(ChannelConfigStateList::iterator cc_it = channel_configs.begin(); cc_it != channel_configs.end(); ++cc_it) {
162 handler->add_export_config (*ts_it, (*cc_it)->config, (*format_it)->format, filename, b);
169 ExportProfileManager::load_preset (ExportPresetPtr preset)
173 current_preset = preset;
174 if (!preset) { return false; }
176 XMLNode const * state;
177 if ((state = preset->get_local_state())) {
178 set_local_state (*state);
179 } else { ok = false; }
181 if ((state = preset->get_global_state())) {
182 if (!set_global_state (*state)) {
185 } else { ok = false; }
191 ExportProfileManager::load_presets ()
193 vector<std::string> found = find_file (string_compose (X_("*%1"),export_preset_suffix));
195 for (vector<std::string>::iterator it = found.begin(); it != found.end(); ++it) {
196 load_preset_from_disk (*it);
201 ExportProfileManager::preset_filename (std::string const & preset_name)
203 string safe_name = legalize_for_path (preset_name);
204 return Glib::build_filename (export_config_dir, safe_name + export_preset_suffix);
208 ExportProfileManager::new_preset (string const & name)
210 // Generate new ID and do regular save
211 string filename = preset_filename (name);
212 current_preset.reset (new ExportPreset (filename, session));
213 preset_list.push_back (current_preset);
214 return save_preset (name);
218 ExportProfileManager::save_preset (string const & name)
220 string filename = preset_filename (name);
222 if (!current_preset) {
223 current_preset.reset (new ExportPreset (filename, session));
224 preset_list.push_back (current_preset);
227 XMLNode * global_preset = new XMLNode ("ExportPreset");
228 XMLNode * local_preset = new XMLNode ("ExportPreset");
230 serialize_global_profile (*global_preset);
231 serialize_local_profile (*local_preset);
233 current_preset->set_name (name);
234 current_preset->set_global_state (*global_preset);
235 current_preset->set_local_state (*local_preset);
237 current_preset->save (filename);
239 return current_preset;
243 ExportProfileManager::remove_preset ()
245 if (!current_preset) { return; }
247 for (PresetList::iterator it = preset_list.begin(); it != preset_list.end(); ++it) {
248 if (*it == current_preset) {
249 preset_list.erase (it);
254 FileMap::iterator it = preset_file_map.find (current_preset->id());
255 if (it != preset_file_map.end()) {
256 if (g_remove (it->second.c_str()) != 0) {
257 error << string_compose (_("Unable to remove export preset %1: %2"), it->second, g_strerror(errno)) << endmsg;
259 preset_file_map.erase (it);
262 current_preset->remove_local();
263 current_preset.reset();
267 ExportProfileManager::load_preset_from_disk (std::string const & path)
269 ExportPresetPtr preset (new ExportPreset (path, session));
271 /* Handle id to filename mapping and don't add duplicates to list */
273 FilePair pair (preset->id(), path);
274 if (preset_file_map.insert (pair).second) {
275 preset_list.push_back (preset);
280 ExportProfileManager::set_state (XMLNode const & root)
282 return set_global_state (root) & set_local_state (root);
286 ExportProfileManager::set_global_state (XMLNode const & root)
288 return init_filenames (root.children ("ExportFilename")) &
289 init_formats (root.children ("ExportFormat"));
293 ExportProfileManager::set_local_state (XMLNode const & root)
295 return init_timespans (root.children ("ExportTimespan")) &
296 init_channel_configs (root.children ("ExportChannelConfiguration"));
300 ExportProfileManager::serialize_profile (XMLNode & root)
302 serialize_local_profile (root);
303 serialize_global_profile (root);
307 ExportProfileManager::serialize_global_profile (XMLNode & root)
309 for (FormatStateList::iterator it = formats.begin(); it != formats.end(); ++it) {
310 root.add_child_nocopy (serialize_format (*it));
313 for (FilenameStateList::iterator it = filenames.begin(); it != filenames.end(); ++it) {
314 root.add_child_nocopy ((*it)->filename->get_state());
319 ExportProfileManager::serialize_local_profile (XMLNode & root)
321 for (TimespanStateList::iterator it = timespans.begin(); it != timespans.end(); ++it) {
322 root.add_child_nocopy (serialize_timespan (*it));
325 for (ChannelConfigStateList::iterator it = channel_configs.begin(); it != channel_configs.end(); ++it) {
326 root.add_child_nocopy ((*it)->config->get_state());
330 std::vector<std::string>
331 ExportProfileManager::find_file (std::string const & pattern)
333 vector<std::string> found;
335 Glib::PatternSpec pattern_spec (pattern);
336 find_matching_files_in_search_path (search_path, pattern_spec, found);
342 ExportProfileManager::set_selection_range (framepos_t start, framepos_t end)
346 selection_range.reset (new Location (session));
347 selection_range->set_name (_("Selection"));
348 selection_range->set (start, end);
350 selection_range.reset();
353 for (TimespanStateList::iterator it = timespans.begin(); it != timespans.end(); ++it) {
354 (*it)->selection_range = selection_range;
359 ExportProfileManager::set_single_range (framepos_t start, framepos_t end, string name)
361 single_range_mode = true;
363 single_range.reset (new Location (session));
364 single_range->set_name (name);
365 single_range->set (start, end);
369 return single_range->id().to_s();
373 ExportProfileManager::init_timespans (XMLNodeList nodes)
379 for (XMLNodeList::const_iterator it = nodes.begin(); it != nodes.end(); ++it) {
380 TimespanStatePtr span = deserialize_timespan (**it);
382 timespans.push_back (span);
383 } else { ok = false; }
386 if (timespans.empty()) {
387 TimespanStatePtr state (new TimespanState (selection_range, ranges));
388 timespans.push_back (state);
390 // Add session as default selection
391 Location * session_range = session.locations()->session_range_location();
392 if (!session_range) { return false; }
394 ExportTimespanPtr timespan = handler->add_timespan();
395 timespan->set_name (session_range->name());
396 timespan->set_range_id (session_range->id().to_s());
397 timespan->set_range (session_range->start(), session_range->end());
398 state->timespans->push_back (timespan);
405 ExportProfileManager::TimespanStatePtr
406 ExportProfileManager::deserialize_timespan (XMLNode & root)
408 TimespanStatePtr state (new TimespanState (selection_range, ranges));
409 XMLProperty const * prop;
411 XMLNodeList spans = root.children ("Range");
412 for (XMLNodeList::iterator node_it = spans.begin(); node_it != spans.end(); ++node_it) {
414 prop = (*node_it)->property ("id");
415 if (!prop) { continue; }
416 string id = prop->value();
418 Location * location = 0;
419 for (LocationList::iterator it = ranges->begin(); it != ranges->end(); ++it) {
420 if ((id == "selection" && *it == selection_range.get()) ||
421 (id == (*it)->id().to_s())) {
427 if (!location) { continue; }
429 ExportTimespanPtr timespan = handler->add_timespan();
430 timespan->set_name (location->name());
431 timespan->set_range_id (location->id().to_s());
432 timespan->set_range (location->start(), location->end());
433 state->timespans->push_back (timespan);
436 if ((prop = root.property ("format"))) {
437 state->time_format = (TimeFormat) string_2_enum (prop->value(), TimeFormat);
440 if (state->timespans->empty()) {
441 return TimespanStatePtr();
448 ExportProfileManager::serialize_timespan (TimespanStatePtr state)
450 XMLNode & root = *(new XMLNode ("ExportTimespan"));
454 for (TimespanList::iterator it = state->timespans->begin(); it != state->timespans->end(); ++it) {
455 if ((span = root.add_child ("Range"))) {
456 span->add_property ("id", (*it)->range_id());
460 root.add_property ("format", enum_2_string (state->time_format));
466 ExportProfileManager::update_ranges () {
469 if (single_range_mode) {
470 ranges->push_back (single_range.get());
476 Location * session_range = session.locations()->session_range_location();
478 ranges->push_back (session_range);
483 if (selection_range) {
484 ranges->push_back (selection_range.get());
489 LocationList const & list (session.locations()->list());
490 for (LocationList::const_iterator it = list.begin(); it != list.end(); ++it) {
491 if ((*it)->is_range_marker()) {
492 ranges->push_back (*it);
497 ExportProfileManager::ChannelConfigStatePtr
498 ExportProfileManager::add_channel_config ()
500 ChannelConfigStatePtr ptr(new ChannelConfigState(handler->add_channel_config()));
501 channel_configs.push_back(ptr);
506 ExportProfileManager::init_channel_configs (XMLNodeList nodes)
508 channel_configs.clear();
511 ChannelConfigStatePtr config (new ChannelConfigState (handler->add_channel_config()));
512 channel_configs.push_back (config);
514 // Add master outs as default
515 if (!session.master_out()) { return false; }
517 IO* master_out = session.master_out()->output().get();
518 if (!master_out) { return false; }
520 for (uint32_t n = 0; n < master_out->n_ports().n_audio(); ++n) {
521 PortExportChannel * channel = new PortExportChannel ();
522 channel->add_port (master_out->audio (n));
524 ExportChannelPtr chan_ptr (channel);
525 config->config->register_channel (chan_ptr);
530 for (XMLNodeList::const_iterator it = nodes.begin(); it != nodes.end(); ++it) {
531 ChannelConfigStatePtr config (new ChannelConfigState (handler->add_channel_config()));
532 config->config->set_state (**it);
533 channel_configs.push_back (config);
539 ExportProfileManager::FormatStatePtr
540 ExportProfileManager::duplicate_format_state (FormatStatePtr state)
542 /* Note: The pointer in the new FormatState should point to the same format spec
543 as the original state's pointer. The spec itself should not be copied! */
545 FormatStatePtr format (new FormatState (format_list, state->format));
546 formats.push_back (format);
551 ExportProfileManager::remove_format_state (FormatStatePtr state)
553 for (FormatStateList::iterator it = formats.begin(); it != formats.end(); ++it) {
562 ExportProfileManager::save_format_to_disk (ExportFormatSpecPtr format)
564 // TODO filename character stripping
566 /* Get filename for file */
568 string new_name = format->name();
569 new_name += export_format_suffix;
571 /* make sure its legal for the filesystem */
573 new_name = legalize_for_path (new_name);
575 std::string new_path = Glib::build_filename (export_config_dir, new_name);
577 /* Check if format is on disk already */
578 FileMap::iterator it;
579 if ((it = format_file_map.find (format->id())) != format_file_map.end()) {
581 /* Check if config is not in user config dir */
582 if (Glib::path_get_dirname (it->second) != export_config_dir) {
586 XMLTree tree (new_path);
587 tree.set_root (&format->get_state());
592 /* Update file and rename if necessary */
594 XMLTree tree (it->second);
595 tree.set_root (&format->get_state());
598 if (new_name != Glib::path_get_basename (it->second)) {
599 if (g_rename (it->second.c_str(), new_path.c_str()) != 0) {
600 error << string_compose (_("Unable to rename export format %1 to %2: %3"), it->second, new_path, g_strerror(errno)) << endmsg;
605 it->second = new_path;
610 XMLTree tree (new_path);
611 tree.set_root (&format->get_state());
615 FormatListChanged ();
620 ExportProfileManager::remove_format_profile (ExportFormatSpecPtr format)
622 for (FormatList::iterator it = format_list->begin(); it != format_list->end(); ++it) {
624 format_list->erase (it);
629 FileMap::iterator it = format_file_map.find (format->id());
630 if (it != format_file_map.end()) {
631 if (g_remove (it->second.c_str()) != 0) {
632 error << string_compose (_("Unable to remove export profile %1: %2"), it->second, g_strerror(errno)) << endmsg;
635 format_file_map.erase (it);
638 FormatListChanged ();
642 ExportProfileManager::get_new_format (ExportFormatSpecPtr original)
644 ExportFormatSpecPtr format;
646 format.reset (new ExportFormatSpecification (*original));
648 format = handler->add_format();
649 format->set_name (_("empty format"));
652 std::string path = save_format_to_disk (format);
653 FilePair pair (format->id(), path);
654 format_file_map.insert (pair);
656 format_list->push_back (format);
657 FormatListChanged ();
663 ExportProfileManager::init_formats (XMLNodeList nodes)
668 for (XMLNodeList::const_iterator it = nodes.begin(); it != nodes.end(); ++it) {
669 FormatStatePtr format = deserialize_format (**it);
671 formats.push_back (format);
672 } else { ok = false; }
675 if (formats.empty ()) {
676 FormatStatePtr format (new FormatState (format_list, ExportFormatSpecPtr ()));
677 formats.push_back (format);
684 ExportProfileManager::FormatStatePtr
685 ExportProfileManager::deserialize_format (XMLNode & root)
690 if ((prop = root.property ("id"))) {
694 for (FormatList::iterator it = format_list->begin(); it != format_list->end(); ++it) {
695 if ((*it)->id() == id) {
696 return FormatStatePtr (new FormatState (format_list, *it));
700 return FormatStatePtr ();
704 ExportProfileManager::serialize_format (FormatStatePtr state)
706 XMLNode * root = new XMLNode ("ExportFormat");
708 string id = state->format ? state->format->id().to_s() : "";
709 root->add_property ("id", id);
715 ExportProfileManager::load_formats ()
717 vector<std::string> found = find_file (string_compose ("*%1", export_format_suffix));
719 for (vector<std::string>::iterator it = found.begin(); it != found.end(); ++it) {
720 load_format_from_disk (*it);
725 ExportProfileManager::load_format_from_disk (std::string const & path)
727 XMLTree const tree (path);
728 ExportFormatSpecPtr format = handler->add_format (*tree.root());
730 /* Handle id to filename mapping and don't add duplicates to list */
732 FilePair pair (format->id(), path);
733 if (format_file_map.insert (pair).second) {
734 format_list->push_back (format);
737 FormatListChanged ();
740 ExportProfileManager::FilenameStatePtr
741 ExportProfileManager::duplicate_filename_state (FilenameStatePtr state)
743 FilenameStatePtr filename (new FilenameState (handler->add_filename_copy (state->filename)));
744 filenames.push_back (filename);
749 ExportProfileManager::remove_filename_state (FilenameStatePtr state)
751 for (FilenameStateList::iterator it = filenames.begin(); it != filenames.end(); ++it) {
753 filenames.erase (it);
760 ExportProfileManager::get_sample_filename_for_format (ExportFilenamePtr filename, ExportFormatSpecPtr format)
764 if (channel_configs.empty()) { return ""; }
766 std::list<string> filenames;
767 build_filenames (filenames, filename, timespans.front()->timespans,
768 channel_configs.front()->config, format);
770 if (filenames.empty()) { return ""; }
771 return filenames.front();
775 ExportProfileManager::init_filenames (XMLNodeList nodes)
779 for (XMLNodeList::const_iterator it = nodes.begin(); it != nodes.end(); ++it) {
780 ExportFilenamePtr filename = handler->add_filename();
781 filename->set_state (**it);
782 filenames.push_back (FilenameStatePtr (new FilenameState (filename)));
785 if (filenames.empty()) {
786 FilenameStatePtr filename (new FilenameState (handler->add_filename()));
787 filenames.push_back (filename);
794 boost::shared_ptr<ExportProfileManager::Warnings>
795 ExportProfileManager::get_warnings ()
797 boost::shared_ptr<Warnings> warnings (new Warnings ());
799 ChannelConfigStatePtr channel_config_state;
800 if (!channel_configs.empty ()) {
801 channel_config_state = channel_configs.front();
804 TimespanStatePtr timespan_state = timespans.front();
806 /*** Check "global" config ***/
808 TimespanListPtr timespans = timespan_state->timespans;
810 ExportChannelConfigPtr channel_config;
811 if (channel_config_state) {
812 channel_config = channel_config_state->config;
815 /* Check Timespans are not empty */
817 if (timespans->empty()) {
818 warnings->errors.push_back (_("No timespan has been selected!"));
821 if (channel_config_state == 0) {
822 warnings->errors.push_back (_("No channels have been selected!"));
824 /* Check channel config ports */
825 if (!channel_config->all_channels_have_ports ()) {
826 warnings->warnings.push_back (_("Some channels are empty"));
830 /*** Check files ***/
832 if (channel_config_state) {
833 FormatStateList::const_iterator format_it;
834 FilenameStateList::const_iterator filename_it;
835 for (format_it = formats.begin(), filename_it = filenames.begin();
836 format_it != formats.end() && filename_it != filenames.end();
837 ++format_it, ++filename_it) {
838 check_config (warnings, timespan_state, channel_config_state, *format_it, *filename_it);
846 ExportProfileManager::check_config (boost::shared_ptr<Warnings> warnings,
847 TimespanStatePtr timespan_state,
848 ChannelConfigStatePtr channel_config_state,
849 FormatStatePtr format_state,
850 FilenameStatePtr filename_state)
852 TimespanListPtr timespans = timespan_state->timespans;
853 ExportChannelConfigPtr channel_config = channel_config_state->config;
854 ExportFormatSpecPtr format = format_state->format;
855 ExportFilenamePtr filename = filename_state->filename;
857 /* Check format and maximum channel count */
858 if (!format || !format->type()) {
859 warnings->errors.push_back (_("No format selected!"));
860 } else if (!channel_config->get_n_chans()) {
861 warnings->errors.push_back (_("All channels are empty!"));
862 } else if (!check_format (format, channel_config->get_n_chans())) {
863 warnings->errors.push_back (_("One or more of the selected formats is not compatible with this system!"));
864 } else if (format->channel_limit() < channel_config->get_n_chans()) {
865 warnings->errors.push_back
866 (string_compose (_("%1 supports only %2 channels, but you have %3 channels in your channel configuration"),
867 format->format_name(),
868 format->channel_limit(),
869 channel_config->get_n_chans()));
872 if (!warnings->errors.empty()) { return; }
874 /* Check filenames */
876 // filename->include_timespan = (timespans->size() > 1); Disabled for now...
878 std::list<string> paths;
879 build_filenames(paths, filename, timespans, channel_config, format);
881 for (std::list<string>::const_iterator path_it = paths.begin(); path_it != paths.end(); ++path_it) {
883 string path = *path_it;
885 if (Glib::file_test (path, Glib::FILE_TEST_EXISTS)) {
886 warnings->conflicting_filenames.push_back (path);
889 if (format->with_toc()) {
890 string marker_file = handler->get_cd_marker_filename(path, CDMarkerTOC);
891 if (Glib::file_test (marker_file, Glib::FILE_TEST_EXISTS)) {
892 warnings->conflicting_filenames.push_back (marker_file);
896 if (format->with_cue()) {
897 string marker_file = handler->get_cd_marker_filename(path, CDMarkerCUE);
898 if (Glib::file_test (marker_file, Glib::FILE_TEST_EXISTS)) {
899 warnings->conflicting_filenames.push_back (marker_file);
906 ExportProfileManager::check_format (ExportFormatSpecPtr format, uint32_t channels)
908 switch (format->type()) {
909 case ExportFormatBase::T_Sndfile:
910 return check_sndfile_format (format, channels);
913 throw ExportFailed (X_("Invalid format given for ExportFileFactory::check!"));
918 ExportProfileManager::check_sndfile_format (ExportFormatSpecPtr format, unsigned int channels)
921 sf_info.channels = channels;
922 sf_info.samplerate = format->sample_rate ();
923 sf_info.format = format->format_id () | format->sample_format ();
925 return (sf_format_check (&sf_info) == SF_TRUE ? true : false);
929 ExportProfileManager::build_filenames(std::list<std::string> & result, ExportFilenamePtr filename,
930 TimespanListPtr timespans, ExportChannelConfigPtr channel_config,
931 ExportFormatSpecPtr format)
933 for (std::list<ExportTimespanPtr>::iterator timespan_it = timespans->begin();
934 timespan_it != timespans->end(); ++timespan_it) {
935 filename->set_timespan (*timespan_it);
937 if (channel_config->get_split()) {
938 filename->include_channel = true;
940 for (uint32_t chan = 1; chan <= channel_config->get_n_chans(); ++chan) {
941 filename->set_channel (chan);
942 result.push_back(filename->get_path (format));
946 filename->include_channel = false;
947 result.push_back(filename->get_path (format));
952 }; // namespace ARDOUR