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.
25 #include <glib/gstdio.h>
27 #include <glibmm/fileutils.h>
28 #include <glibmm/miscutils.h>
30 #include "pbd/enumwriter.h"
31 #include "pbd/xml++.h"
32 #include "pbd/convert.h"
34 #include "ardour/export_profile_manager.h"
35 #include "ardour/export_format_specification.h"
36 #include "ardour/export_formats_search_path.h"
37 #include "ardour/export_timespan.h"
38 #include "ardour/export_channel_configuration.h"
39 #include "ardour/export_filename.h"
40 #include "ardour/export_preset.h"
41 #include "ardour/export_handler.h"
42 #include "ardour/export_failed.h"
43 #include "ardour/directory_names.h"
44 #include "ardour/filename_extensions.h"
45 #include "ardour/route.h"
46 #include "ardour/session.h"
47 #include "ardour/broadcast_info.h"
58 ExportProfileManager::ExportProfileManager (Session & s, std::string xml_node_name)
59 : xml_node_name (xml_node_name)
60 , handler (s.get_export_handler())
63 , session_range (new Location (s))
64 , ranges (new LocationList ())
65 , single_range_mode (false)
67 , format_list (new FormatList ())
69 /* Initialize path variables */
71 export_config_dir = Glib::build_filename (user_config_directory(), export_dir_name);
73 search_path += export_formats_search_path();
75 info << string_compose (_("Searching for export formats in %1"), search_path.to_string()) << endmsg;
77 /* create export config directory if necessary */
79 if (!Glib::file_test (export_config_dir, Glib::FILE_TEST_EXISTS)) {
80 if (g_mkdir_with_parents (export_config_dir.c_str(), 0755) != 0) {
81 error << string_compose (_("Unable to create export format directory %1: %2"), export_config_dir, g_strerror(errno)) << endmsg;
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 XMLNode * instant_xml (new XMLNode (xml_node_name));
100 serialize_profile (*instant_xml);
101 session.add_instant_xml (*instant_xml, false);
105 ExportProfileManager::load_profile ()
107 XMLNode * instant_node = session.instant_xml (xml_node_name);
109 set_state (*instant_node);
111 XMLNode empty_node (xml_node_name);
112 set_state (empty_node);
117 ExportProfileManager::prepare_for_export ()
119 TimespanListPtr ts_list = timespans.front()->timespans;
121 FormatStateList::const_iterator format_it;
122 FilenameStateList::const_iterator filename_it;
125 for (TimespanList::iterator ts_it = ts_list->begin(); ts_it != ts_list->end(); ++ts_it) {
126 // ..., each format-filename pair
127 for (format_it = formats.begin(), filename_it = filenames.begin();
128 format_it != formats.end() && filename_it != filenames.end();
129 ++format_it, ++filename_it) {
131 ExportFilenamePtr filename = (*filename_it)->filename;
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 // ...and each channel config
141 filename->include_channel_config = (channel_configs.size() > 1);
142 for(ChannelConfigStateList::iterator cc_it = channel_configs.begin(); cc_it != channel_configs.end(); ++cc_it) {
143 handler->add_export_config (*ts_it, (*cc_it)->config, (*format_it)->format, filename, b);
150 ExportProfileManager::load_preset (ExportPresetPtr preset)
154 current_preset = preset;
155 if (!preset) { return false; }
157 XMLNode const * state;
158 if ((state = preset->get_local_state())) {
159 set_local_state (*state);
160 } else { ok = false; }
162 if ((state = preset->get_global_state())) {
163 if (!set_global_state (*state)) {
166 } else { ok = false; }
172 ExportProfileManager::load_presets ()
174 vector<std::string> found = find_file (string_compose (X_("*%1"),export_preset_suffix));
176 for (vector<std::string>::iterator it = found.begin(); it != found.end(); ++it) {
177 load_preset_from_disk (*it);
182 ExportProfileManager::preset_filename (std::string const & preset_name)
184 string safe_name = legalize_for_path (preset_name);
185 return Glib::build_filename (export_config_dir, safe_name + export_preset_suffix);
189 ExportProfileManager::new_preset (string const & name)
191 // Generate new ID and do regular save
192 string filename = preset_filename (name);
193 current_preset.reset (new ExportPreset (filename, session));
194 preset_list.push_back (current_preset);
195 return save_preset (name);
199 ExportProfileManager::save_preset (string const & name)
201 string filename = preset_filename (name);
203 if (!current_preset) {
204 current_preset.reset (new ExportPreset (filename, session));
205 preset_list.push_back (current_preset);
208 XMLNode * global_preset = new XMLNode ("ExportPreset");
209 XMLNode * local_preset = new XMLNode ("ExportPreset");
211 serialize_global_profile (*global_preset);
212 serialize_local_profile (*local_preset);
214 current_preset->set_name (name);
215 current_preset->set_global_state (*global_preset);
216 current_preset->set_local_state (*local_preset);
218 current_preset->save (filename);
220 return current_preset;
224 ExportProfileManager::remove_preset ()
226 if (!current_preset) { return; }
228 for (PresetList::iterator it = preset_list.begin(); it != preset_list.end(); ++it) {
229 if (*it == current_preset) {
230 preset_list.erase (it);
235 FileMap::iterator it = preset_file_map.find (current_preset->id());
236 if (it != preset_file_map.end()) {
237 if (g_remove (it->second.c_str()) != 0) {
238 error << string_compose (_("Unable to remove export preset %1: %2"), it->second, g_strerror(errno)) << endmsg;
240 preset_file_map.erase (it);
243 current_preset->remove_local();
244 current_preset.reset();
248 ExportProfileManager::load_preset_from_disk (std::string const & path)
250 ExportPresetPtr preset (new ExportPreset (path, session));
252 /* Handle id to filename mapping and don't add duplicates to list */
254 FilePair pair (preset->id(), path);
255 if (preset_file_map.insert (pair).second) {
256 preset_list.push_back (preset);
261 ExportProfileManager::set_state (XMLNode const & root)
263 return set_global_state (root) & set_local_state (root);
267 ExportProfileManager::set_global_state (XMLNode const & root)
269 return init_filenames (root.children ("ExportFilename")) &
270 init_formats (root.children ("ExportFormat"));
274 ExportProfileManager::set_local_state (XMLNode const & root)
276 return init_timespans (root.children ("ExportTimespan")) &
277 init_channel_configs (root.children ("ExportChannelConfiguration"));
281 ExportProfileManager::serialize_profile (XMLNode & root)
283 serialize_local_profile (root);
284 serialize_global_profile (root);
288 ExportProfileManager::serialize_global_profile (XMLNode & root)
290 for (FormatStateList::iterator it = formats.begin(); it != formats.end(); ++it) {
291 root.add_child_nocopy (serialize_format (*it));
294 for (FilenameStateList::iterator it = filenames.begin(); it != filenames.end(); ++it) {
295 root.add_child_nocopy ((*it)->filename->get_state());
300 ExportProfileManager::serialize_local_profile (XMLNode & root)
302 for (TimespanStateList::iterator it = timespans.begin(); it != timespans.end(); ++it) {
303 root.add_child_nocopy (serialize_timespan (*it));
306 for (ChannelConfigStateList::iterator it = channel_configs.begin(); it != channel_configs.end(); ++it) {
307 root.add_child_nocopy ((*it)->config->get_state());
311 std::vector<std::string>
312 ExportProfileManager::find_file (std::string const & pattern)
314 vector<std::string> found;
316 Glib::PatternSpec pattern_spec (pattern);
317 find_matching_files_in_search_path (search_path, pattern_spec, found);
323 ExportProfileManager::set_selection_range (framepos_t start, framepos_t end)
327 selection_range.reset (new Location (session));
328 selection_range->set_name (_("Selection"));
329 selection_range->set (start, end);
331 selection_range.reset();
334 for (TimespanStateList::iterator it = timespans.begin(); it != timespans.end(); ++it) {
335 (*it)->selection_range = selection_range;
340 ExportProfileManager::set_single_range (framepos_t start, framepos_t end, string name)
342 single_range_mode = true;
344 single_range.reset (new Location (session));
345 single_range->set_name (name);
346 single_range->set (start, end);
350 return single_range->id().to_s();
354 ExportProfileManager::init_timespans (XMLNodeList nodes)
360 for (XMLNodeList::const_iterator it = nodes.begin(); it != nodes.end(); ++it) {
361 TimespanStatePtr span = deserialize_timespan (**it);
363 timespans.push_back (span);
364 } else { ok = false; }
367 if (timespans.empty()) {
368 TimespanStatePtr state (new TimespanState (session_range, selection_range, ranges));
369 timespans.push_back (state);
371 // Add session as default selection
372 ExportTimespanPtr timespan = handler->add_timespan();
373 timespan->set_name (session_range->name());
374 timespan->set_range_id ("session");
375 timespan->set_range (session_range->start(), session_range->end());
376 state->timespans->push_back (timespan);
383 ExportProfileManager::TimespanStatePtr
384 ExportProfileManager::deserialize_timespan (XMLNode & root)
386 TimespanStatePtr state (new TimespanState (session_range, selection_range, ranges));
387 XMLProperty const * prop;
389 XMLNodeList spans = root.children ("Range");
390 for (XMLNodeList::iterator node_it = spans.begin(); node_it != spans.end(); ++node_it) {
392 prop = (*node_it)->property ("id");
393 if (!prop) { continue; }
394 string id = prop->value();
396 for (LocationList::iterator it = ranges->begin(); it != ranges->end(); ++it) {
397 if ((!id.compare ("session") && *it == session_range.get()) ||
398 (!id.compare ("selection") && *it == selection_range.get()) ||
399 (!id.compare ((*it)->id().to_s()))) {
400 ExportTimespanPtr timespan = handler->add_timespan();
401 timespan->set_name ((*it)->name());
402 timespan->set_range_id (id);
403 timespan->set_range ((*it)->start(), (*it)->end());
404 state->timespans->push_back (timespan);
409 if ((prop = root.property ("format"))) {
410 state->time_format = (TimeFormat) string_2_enum (prop->value(), TimeFormat);
417 ExportProfileManager::serialize_timespan (TimespanStatePtr state)
419 XMLNode & root = *(new XMLNode ("ExportTimespan"));
424 for (TimespanList::iterator it = state->timespans->begin(); it != state->timespans->end(); ++it) {
425 if ((span = root.add_child ("Range"))) {
426 span->add_property ("id", (*it)->range_id());
430 root.add_property ("format", enum_2_string (state->time_format));
436 ExportProfileManager::update_ranges () {
439 if (single_range_mode) {
440 ranges->push_back (single_range.get());
446 session_range->set_name (_("Session"));
447 session_range->set (session.current_start_frame(), session.current_end_frame());
448 ranges->push_back (session_range.get());
452 if (selection_range) {
453 ranges->push_back (selection_range.get());
458 LocationList const & list (session.locations()->list());
459 for (LocationList::const_iterator it = list.begin(); it != list.end(); ++it) {
460 if ((*it)->is_range_marker()) {
461 ranges->push_back (*it);
466 ExportProfileManager::ChannelConfigStatePtr
467 ExportProfileManager::add_channel_config ()
469 ChannelConfigStatePtr ptr(new ChannelConfigState(handler->add_channel_config()));
470 channel_configs.push_back(ptr);
475 ExportProfileManager::init_channel_configs (XMLNodeList nodes)
477 channel_configs.clear();
480 ChannelConfigStatePtr config (new ChannelConfigState (handler->add_channel_config()));
481 channel_configs.push_back (config);
483 // Add master outs as default
484 IO* master_out = session.master_out()->output().get();
485 if (!master_out) { return false; }
487 for (uint32_t n = 0; n < master_out->n_ports().n_audio(); ++n) {
488 PortExportChannel * channel = new PortExportChannel ();
489 channel->add_port (master_out->audio (n));
491 ExportChannelPtr chan_ptr (channel);
492 config->config->register_channel (chan_ptr);
497 for (XMLNodeList::const_iterator it = nodes.begin(); it != nodes.end(); ++it) {
498 ChannelConfigStatePtr config (new ChannelConfigState (handler->add_channel_config()));
499 config->config->set_state (**it);
500 channel_configs.push_back (config);
506 ExportProfileManager::FormatStatePtr
507 ExportProfileManager::duplicate_format_state (FormatStatePtr state)
509 /* Note: The pointer in the new FormatState should point to the same format spec
510 as the original state's pointer. The spec itself should not be copied! */
512 FormatStatePtr format (new FormatState (format_list, state->format));
513 formats.push_back (format);
518 ExportProfileManager::remove_format_state (FormatStatePtr state)
520 for (FormatStateList::iterator it = formats.begin(); it != formats.end(); ++it) {
529 ExportProfileManager::save_format_to_disk (ExportFormatSpecPtr format)
531 // TODO filename character stripping
533 /* Get filename for file */
535 string new_name = format->name();
536 new_name += export_format_suffix;
538 /* make sure its legal for the filesystem */
540 new_name = legalize_for_path (new_name);
542 std::string new_path = Glib::build_filename (export_config_dir, new_name);
544 /* Check if format is on disk already */
545 FileMap::iterator it;
546 if ((it = format_file_map.find (format->id())) != format_file_map.end()) {
548 /* Check if config is not in user config dir */
549 if (Glib::path_get_dirname (it->second) != export_config_dir) {
553 XMLTree tree (new_path);
554 tree.set_root (&format->get_state());
559 /* Update file and rename if necessary */
561 XMLTree tree (it->second);
562 tree.set_root (&format->get_state());
565 if (new_name != Glib::path_get_basename (it->second)) {
566 if (g_rename (it->second.c_str(), new_path.c_str()) != 0) {
567 error << string_compose (_("Unable to rename export format %1 to %2: %3"), it->second, new_path, g_strerror(errno)) << endmsg;
572 it->second = new_path;
577 XMLTree tree (new_path);
578 tree.set_root (&format->get_state());
582 FormatListChanged ();
587 ExportProfileManager::remove_format_profile (ExportFormatSpecPtr format)
589 for (FormatList::iterator it = format_list->begin(); it != format_list->end(); ++it) {
591 format_list->erase (it);
596 FileMap::iterator it = format_file_map.find (format->id());
597 if (it != format_file_map.end()) {
598 if (g_remove (it->second.c_str()) != 0) {
599 error << string_compose (_("Unable to remove export profile %1: %2"), it->second, g_strerror(errno)) << endmsg;
602 format_file_map.erase (it);
605 FormatListChanged ();
609 ExportProfileManager::get_new_format (ExportFormatSpecPtr original)
611 ExportFormatSpecPtr format;
613 format.reset (new ExportFormatSpecification (*original));
615 format = handler->add_format();
616 format->set_name ("empty format");
619 std::string path = save_format_to_disk (format);
620 FilePair pair (format->id(), path);
621 format_file_map.insert (pair);
623 format_list->push_back (format);
624 FormatListChanged ();
630 ExportProfileManager::init_formats (XMLNodeList nodes)
635 for (XMLNodeList::const_iterator it = nodes.begin(); it != nodes.end(); ++it) {
636 FormatStatePtr format = deserialize_format (**it);
638 formats.push_back (format);
639 } else { ok = false; }
642 if (formats.empty ()) {
643 FormatStatePtr format (new FormatState (format_list, ExportFormatSpecPtr ()));
644 formats.push_back (format);
651 ExportProfileManager::FormatStatePtr
652 ExportProfileManager::deserialize_format (XMLNode & root)
657 if ((prop = root.property ("id"))) {
661 for (FormatList::iterator it = format_list->begin(); it != format_list->end(); ++it) {
662 if ((*it)->id() == id) {
663 return FormatStatePtr (new FormatState (format_list, *it));
667 return FormatStatePtr ();
671 ExportProfileManager::serialize_format (FormatStatePtr state)
673 XMLNode * root = new XMLNode ("ExportFormat");
675 string id = state->format ? state->format->id().to_s() : "";
676 root->add_property ("id", id);
682 ExportProfileManager::load_formats ()
684 vector<std::string> found = find_file (string_compose ("*%1", export_format_suffix));
686 for (vector<std::string>::iterator it = found.begin(); it != found.end(); ++it) {
687 load_format_from_disk (*it);
692 ExportProfileManager::load_format_from_disk (std::string const & path)
694 XMLTree const tree (path);
695 ExportFormatSpecPtr format = handler->add_format (*tree.root());
697 /* Handle id to filename mapping and don't add duplicates to list */
699 FilePair pair (format->id(), path);
700 if (format_file_map.insert (pair).second) {
701 format_list->push_back (format);
704 FormatListChanged ();
707 ExportProfileManager::FilenameStatePtr
708 ExportProfileManager::duplicate_filename_state (FilenameStatePtr state)
710 FilenameStatePtr filename (new FilenameState (handler->add_filename_copy (state->filename)));
711 filenames.push_back (filename);
716 ExportProfileManager::remove_filename_state (FilenameStatePtr state)
718 for (FilenameStateList::iterator it = filenames.begin(); it != filenames.end(); ++it) {
720 filenames.erase (it);
727 ExportProfileManager::get_sample_filename_for_format (ExportFilenamePtr filename, ExportFormatSpecPtr format)
731 if (channel_configs.empty()) { return ""; }
733 std::list<string> filenames;
734 build_filenames (filenames, filename, timespans.front()->timespans,
735 channel_configs.front()->config, format);
737 if (filenames.empty()) { return ""; }
738 return filenames.front();
742 ExportProfileManager::init_filenames (XMLNodeList nodes)
746 for (XMLNodeList::const_iterator it = nodes.begin(); it != nodes.end(); ++it) {
747 ExportFilenamePtr filename = handler->add_filename();
748 filename->set_state (**it);
749 filenames.push_back (FilenameStatePtr (new FilenameState (filename)));
752 if (filenames.empty()) {
753 FilenameStatePtr filename (new FilenameState (handler->add_filename()));
754 filenames.push_back (filename);
761 boost::shared_ptr<ExportProfileManager::Warnings>
762 ExportProfileManager::get_warnings ()
764 boost::shared_ptr<Warnings> warnings (new Warnings ());
766 ChannelConfigStatePtr channel_config_state;
767 if (!channel_configs.empty ()) {
768 channel_config_state = channel_configs.front();
771 TimespanStatePtr timespan_state = timespans.front();
773 /*** Check "global" config ***/
775 TimespanListPtr timespans = timespan_state->timespans;
777 ExportChannelConfigPtr channel_config;
778 if (channel_config_state) {
779 channel_config = channel_config_state->config;
782 /* Check Timespans are not empty */
784 if (timespans->empty()) {
785 warnings->errors.push_back (_("No timespan has been selected!"));
788 if (channel_config_state == 0) {
789 warnings->errors.push_back (_("No channels have been selected!"));
791 /* Check channel config ports */
792 if (!channel_config->all_channels_have_ports ()) {
793 warnings->warnings.push_back (_("Some channels are empty"));
797 /*** Check files ***/
799 if (channel_config_state) {
800 FormatStateList::const_iterator format_it;
801 FilenameStateList::const_iterator filename_it;
802 for (format_it = formats.begin(), filename_it = filenames.begin();
803 format_it != formats.end() && filename_it != filenames.end();
804 ++format_it, ++filename_it) {
805 check_config (warnings, timespan_state, channel_config_state, *format_it, *filename_it);
813 ExportProfileManager::check_config (boost::shared_ptr<Warnings> warnings,
814 TimespanStatePtr timespan_state,
815 ChannelConfigStatePtr channel_config_state,
816 FormatStatePtr format_state,
817 FilenameStatePtr filename_state)
819 TimespanListPtr timespans = timespan_state->timespans;
820 ExportChannelConfigPtr channel_config = channel_config_state->config;
821 ExportFormatSpecPtr format = format_state->format;
822 ExportFilenamePtr filename = filename_state->filename;
824 /* Check format and maximum channel count */
825 if (!format || !format->type()) {
826 warnings->errors.push_back (_("No format selected!"));
827 } else if (!channel_config->get_n_chans()) {
828 warnings->errors.push_back (_("All channels are empty!"));
829 } else if (!check_format (format, channel_config->get_n_chans())) {
830 warnings->errors.push_back (_("One or more of the selected formats is not compatible with this system!"));
831 } else if (format->channel_limit() < channel_config->get_n_chans()) {
832 warnings->errors.push_back
833 (string_compose (_("%1 supports only %2 channels, but you have %3 channels in your channel configuration"),
834 format->format_name(),
835 format->channel_limit(),
836 channel_config->get_n_chans()));
839 if (!warnings->errors.empty()) { return; }
841 /* Check filenames */
843 // filename->include_timespan = (timespans->size() > 1); Disabled for now...
845 std::list<string> paths;
846 build_filenames(paths, filename, timespans, channel_config, format);
848 for (std::list<string>::const_iterator path_it = paths.begin(); path_it != paths.end(); ++path_it) {
850 string path = *path_it;
852 if (Glib::file_test (path, Glib::FILE_TEST_EXISTS)) {
853 warnings->conflicting_filenames.push_back (path);
856 if (format->with_toc()) {
857 string marker_file = handler->get_cd_marker_filename(path, CDMarkerTOC);
858 if (Glib::file_test (marker_file, Glib::FILE_TEST_EXISTS)) {
859 warnings->conflicting_filenames.push_back (marker_file);
863 if (format->with_cue()) {
864 string marker_file = handler->get_cd_marker_filename(path, CDMarkerCUE);
865 if (Glib::file_test (marker_file, Glib::FILE_TEST_EXISTS)) {
866 warnings->conflicting_filenames.push_back (marker_file);
873 ExportProfileManager::check_format (ExportFormatSpecPtr format, uint32_t channels)
875 switch (format->type()) {
876 case ExportFormatBase::T_Sndfile:
877 return check_sndfile_format (format, channels);
880 throw ExportFailed (X_("Invalid format given for ExportFileFactory::check!"));
885 ExportProfileManager::check_sndfile_format (ExportFormatSpecPtr format, unsigned int channels)
888 sf_info.channels = channels;
889 sf_info.samplerate = format->sample_rate ();
890 sf_info.format = format->format_id () | format->sample_format ();
892 return (sf_format_check (&sf_info) == SF_TRUE ? true : false);
896 ExportProfileManager::build_filenames(std::list<std::string> & result, ExportFilenamePtr filename,
897 TimespanListPtr timespans, ExportChannelConfigPtr channel_config,
898 ExportFormatSpecPtr format)
900 for (std::list<ExportTimespanPtr>::iterator timespan_it = timespans->begin();
901 timespan_it != timespans->end(); ++timespan_it) {
902 filename->set_timespan (*timespan_it);
904 if (channel_config->get_split()) {
905 filename->include_channel = true;
907 for (uint32_t chan = 1; chan <= channel_config->get_n_chans(); ++chan) {
908 filename->set_channel (chan);
909 result.push_back(filename->get_path (format));
913 filename->include_channel = false;
914 result.push_back(filename->get_path (format));
919 }; // namespace ARDOUR