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_formats_search_path.h"
33 #include "ardour/export_timespan.h"
34 #include "ardour/export_channel_configuration.h"
35 #include "ardour/export_filename.h"
36 #include "ardour/export_preset.h"
37 #include "ardour/export_handler.h"
38 #include "ardour/export_failed.h"
39 #include "ardour/filename_extensions.h"
40 #include "ardour/route.h"
41 #include "ardour/session.h"
42 #include "ardour/broadcast_info.h"
53 ExportProfileManager::ExportProfileManager (Session & s, std::string xml_node_name)
54 : xml_node_name (xml_node_name)
55 , handler (s.get_export_handler())
58 , session_range (new Location (s))
59 , ranges (new LocationList ())
60 , single_range_mode (false)
62 , format_list (new FormatList ())
64 /* Initialize path variables */
66 export_config_dir = user_config_directory();
67 export_config_dir /= "export";
69 search_path += export_formats_search_path();
71 info << string_compose (_("Searching for export formats in %1"), search_path.to_string()) << endmsg;
73 /* create export config directory if necessary */
75 if (!sys::exists (export_config_dir)) {
76 sys::create_directory (export_config_dir);
82 /* Initialize all lists with an empty config */
85 init_timespans (dummy);
86 init_channel_configs (dummy);
88 init_filenames (dummy);
91 ExportProfileManager::~ExportProfileManager ()
93 XMLNode * instant_xml (new XMLNode (xml_node_name));
94 serialize_profile (*instant_xml);
95 session.add_instant_xml (*instant_xml, false);
99 ExportProfileManager::load_profile ()
101 XMLNode * instant_node = session.instant_xml (xml_node_name);
103 set_state (*instant_node);
105 XMLNode empty_node (xml_node_name);
106 set_state (empty_node);
111 ExportProfileManager::prepare_for_export ()
113 TimespanListPtr ts_list = timespans.front()->timespans;
115 FormatStateList::const_iterator format_it;
116 FilenameStateList::const_iterator filename_it;
119 for (TimespanList::iterator ts_it = ts_list->begin(); ts_it != ts_list->end(); ++ts_it) {
120 // ..., each format-filename pair
121 for (format_it = formats.begin(), filename_it = filenames.begin();
122 format_it != formats.end() && filename_it != filenames.end();
123 ++format_it, ++filename_it) {
125 ExportFilenamePtr filename = (*filename_it)->filename;
126 // filename->include_timespan = (ts_list->size() > 1); Disabled for now...
128 boost::shared_ptr<BroadcastInfo> b;
129 if ((*format_it)->format->has_broadcast_info()) {
130 b.reset (new BroadcastInfo);
131 b->set_from_session (session, (*ts_it)->get_start());
134 // ...and each channel config
135 filename->include_channel_config = (channel_configs.size() > 1);
136 for(ChannelConfigStateList::iterator cc_it = channel_configs.begin(); cc_it != channel_configs.end(); ++cc_it) {
137 handler->add_export_config (*ts_it, (*cc_it)->config, (*format_it)->format, filename, b);
144 ExportProfileManager::load_preset (ExportPresetPtr preset)
148 current_preset = preset;
149 if (!preset) { return false; }
151 XMLNode const * state;
152 if ((state = preset->get_local_state())) {
153 set_local_state (*state);
154 } else { ok = false; }
156 if ((state = preset->get_global_state())) {
157 if (!set_global_state (*state)) {
160 } else { ok = false; }
166 ExportProfileManager::load_presets ()
168 vector<sys::path> found = find_file (string_compose (X_("*%1"),export_preset_suffix));
170 for (vector<sys::path>::iterator it = found.begin(); it != found.end(); ++it) {
171 load_preset_from_disk (*it);
176 ExportProfileManager::preset_filename (std::string const & preset_name)
178 string safe_name = legalize_for_path (preset_name);
179 return export_config_dir.to_string() + "/" + safe_name + export_preset_suffix;
183 ExportProfileManager::new_preset (string const & name)
185 // Generate new ID and do regular save
186 string filename = preset_filename (name);
187 current_preset.reset (new ExportPreset (filename, session));
188 preset_list.push_back (current_preset);
189 return save_preset (name);
193 ExportProfileManager::save_preset (string const & name)
195 string filename = preset_filename (name);
197 if (!current_preset) {
198 current_preset.reset (new ExportPreset (filename, session));
199 preset_list.push_back (current_preset);
202 XMLNode * global_preset = new XMLNode ("ExportPreset");
203 XMLNode * local_preset = new XMLNode ("ExportPreset");
205 serialize_global_profile (*global_preset);
206 serialize_local_profile (*local_preset);
208 current_preset->set_name (name);
209 current_preset->set_global_state (*global_preset);
210 current_preset->set_local_state (*local_preset);
212 current_preset->save (filename);
214 return current_preset;
218 ExportProfileManager::remove_preset ()
220 if (!current_preset) { return; }
222 for (PresetList::iterator it = preset_list.begin(); it != preset_list.end(); ++it) {
223 if (*it == current_preset) {
224 preset_list.erase (it);
229 FileMap::iterator it = preset_file_map.find (current_preset->id());
230 if (it != preset_file_map.end()) {
231 sys::remove (it->second);
232 preset_file_map.erase (it);
235 current_preset->remove_local();
236 current_preset.reset();
240 ExportProfileManager::load_preset_from_disk (PBD::sys::path const & path)
242 ExportPresetPtr preset (new ExportPreset (path.to_string(), session));
244 /* Handle id to filename mapping and don't add duplicates to list */
246 FilePair pair (preset->id(), path);
247 if (preset_file_map.insert (pair).second) {
248 preset_list.push_back (preset);
253 ExportProfileManager::set_state (XMLNode const & root)
255 return set_global_state (root) & set_local_state (root);
259 ExportProfileManager::set_global_state (XMLNode const & root)
261 return init_filenames (root.children ("ExportFilename")) &
262 init_formats (root.children ("ExportFormat"));
266 ExportProfileManager::set_local_state (XMLNode const & root)
268 return init_timespans (root.children ("ExportTimespan")) &
269 init_channel_configs (root.children ("ExportChannelConfiguration"));
273 ExportProfileManager::serialize_profile (XMLNode & root)
275 serialize_local_profile (root);
276 serialize_global_profile (root);
280 ExportProfileManager::serialize_global_profile (XMLNode & root)
282 for (FormatStateList::iterator it = formats.begin(); it != formats.end(); ++it) {
283 root.add_child_nocopy (serialize_format (*it));
286 for (FilenameStateList::iterator it = filenames.begin(); it != filenames.end(); ++it) {
287 root.add_child_nocopy ((*it)->filename->get_state());
292 ExportProfileManager::serialize_local_profile (XMLNode & root)
294 for (TimespanStateList::iterator it = timespans.begin(); it != timespans.end(); ++it) {
295 root.add_child_nocopy (serialize_timespan (*it));
298 for (ChannelConfigStateList::iterator it = channel_configs.begin(); it != channel_configs.end(); ++it) {
299 root.add_child_nocopy ((*it)->config->get_state());
303 std::vector<sys::path>
304 ExportProfileManager::find_file (std::string const & pattern)
306 vector<sys::path> found;
308 Glib::PatternSpec pattern_spec (pattern);
309 find_matching_files_in_search_path (search_path, pattern_spec, found);
315 ExportProfileManager::set_selection_range (framepos_t start, framepos_t end)
319 selection_range.reset (new Location (session));
320 selection_range->set_name (_("Selection"));
321 selection_range->set (start, end);
323 selection_range.reset();
326 for (TimespanStateList::iterator it = timespans.begin(); it != timespans.end(); ++it) {
327 (*it)->selection_range = selection_range;
332 ExportProfileManager::set_single_range (framepos_t start, framepos_t end, string name)
334 single_range_mode = true;
336 single_range.reset (new Location (session));
337 single_range->set_name (name);
338 single_range->set (start, end);
342 return single_range->id().to_s();
346 ExportProfileManager::init_timespans (XMLNodeList nodes)
352 for (XMLNodeList::const_iterator it = nodes.begin(); it != nodes.end(); ++it) {
353 TimespanStatePtr span = deserialize_timespan (**it);
355 timespans.push_back (span);
356 } else { ok = false; }
359 if (timespans.empty()) {
360 TimespanStatePtr state (new TimespanState (session_range, selection_range, ranges));
361 timespans.push_back (state);
363 // Add session as default selection
364 ExportTimespanPtr timespan = handler->add_timespan();
365 timespan->set_name (session_range->name());
366 timespan->set_range_id ("session");
367 timespan->set_range (session_range->start(), session_range->end());
368 state->timespans->push_back (timespan);
375 ExportProfileManager::TimespanStatePtr
376 ExportProfileManager::deserialize_timespan (XMLNode & root)
378 TimespanStatePtr state (new TimespanState (session_range, selection_range, ranges));
379 XMLProperty const * prop;
381 XMLNodeList spans = root.children ("Range");
382 for (XMLNodeList::iterator node_it = spans.begin(); node_it != spans.end(); ++node_it) {
384 prop = (*node_it)->property ("id");
385 if (!prop) { continue; }
386 string id = prop->value();
388 for (LocationList::iterator it = ranges->begin(); it != ranges->end(); ++it) {
389 if ((!id.compare ("session") && *it == session_range.get()) ||
390 (!id.compare ("selection") && *it == selection_range.get()) ||
391 (!id.compare ((*it)->id().to_s()))) {
392 ExportTimespanPtr timespan = handler->add_timespan();
393 timespan->set_name ((*it)->name());
394 timespan->set_range_id (id);
395 timespan->set_range ((*it)->start(), (*it)->end());
396 state->timespans->push_back (timespan);
401 if ((prop = root.property ("format"))) {
402 state->time_format = (TimeFormat) string_2_enum (prop->value(), TimeFormat);
409 ExportProfileManager::serialize_timespan (TimespanStatePtr state)
411 XMLNode & root = *(new XMLNode ("ExportTimespan"));
416 for (TimespanList::iterator it = state->timespans->begin(); it != state->timespans->end(); ++it) {
417 if ((span = root.add_child ("Range"))) {
418 span->add_property ("id", (*it)->range_id());
422 root.add_property ("format", enum_2_string (state->time_format));
428 ExportProfileManager::update_ranges () {
431 if (single_range_mode) {
432 ranges->push_back (single_range.get());
438 session_range->set_name (_("Session"));
439 session_range->set (session.current_start_frame(), session.current_end_frame());
440 ranges->push_back (session_range.get());
444 if (selection_range) {
445 ranges->push_back (selection_range.get());
450 LocationList const & list (session.locations()->list());
451 for (LocationList::const_iterator it = list.begin(); it != list.end(); ++it) {
452 if ((*it)->is_range_marker()) {
453 ranges->push_back (*it);
458 ExportProfileManager::ChannelConfigStatePtr
459 ExportProfileManager::add_channel_config ()
461 ChannelConfigStatePtr ptr(new ChannelConfigState(handler->add_channel_config()));
462 channel_configs.push_back(ptr);
467 ExportProfileManager::init_channel_configs (XMLNodeList nodes)
469 channel_configs.clear();
472 ChannelConfigStatePtr config (new ChannelConfigState (handler->add_channel_config()));
473 channel_configs.push_back (config);
475 // Add master outs as default
476 IO* master_out = session.master_out()->output().get();
477 if (!master_out) { return false; }
479 for (uint32_t n = 0; n < master_out->n_ports().n_audio(); ++n) {
480 PortExportChannel * channel = new PortExportChannel ();
481 channel->add_port (master_out->audio (n));
483 ExportChannelPtr chan_ptr (channel);
484 config->config->register_channel (chan_ptr);
489 for (XMLNodeList::const_iterator it = nodes.begin(); it != nodes.end(); ++it) {
490 ChannelConfigStatePtr config (new ChannelConfigState (handler->add_channel_config()));
491 config->config->set_state (**it);
492 channel_configs.push_back (config);
498 ExportProfileManager::FormatStatePtr
499 ExportProfileManager::duplicate_format_state (FormatStatePtr state)
501 /* Note: The pointer in the new FormatState should point to the same format spec
502 as the original state's pointer. The spec itself should not be copied! */
504 FormatStatePtr format (new FormatState (format_list, state->format));
505 formats.push_back (format);
510 ExportProfileManager::remove_format_state (FormatStatePtr state)
512 for (FormatStateList::iterator it = formats.begin(); it != formats.end(); ++it) {
521 ExportProfileManager::save_format_to_disk (ExportFormatSpecPtr format)
523 // TODO filename character stripping
525 /* Get filename for file */
527 string new_name = format->name();
528 new_name += export_format_suffix;
530 /* make sure its legal for the filesystem */
532 new_name = legalize_for_path (new_name);
534 sys::path new_path (export_config_dir);
535 new_path /= new_name;
537 /* Check if format is on disk already */
538 FileMap::iterator it;
539 if ((it = format_file_map.find (format->id())) != format_file_map.end()) {
541 /* Check if config is not in user config dir */
542 if (it->second.branch_path().to_string().compare (export_config_dir.to_string())) {
546 XMLTree tree (new_path.to_string());
547 tree.set_root (&format->get_state());
552 /* Update file and rename if necessary */
554 XMLTree tree (it->second.to_string());
555 tree.set_root (&format->get_state());
558 if (new_name.compare (it->second.leaf())) {
559 sys::rename (it->second, new_path);
563 it->second = new_path;
568 XMLTree tree (new_path.to_string());
569 tree.set_root (&format->get_state());
573 FormatListChanged ();
578 ExportProfileManager::remove_format_profile (ExportFormatSpecPtr format)
580 for (FormatList::iterator it = format_list->begin(); it != format_list->end(); ++it) {
582 format_list->erase (it);
587 FileMap::iterator it = format_file_map.find (format->id());
588 if (it != format_file_map.end()) {
589 sys::remove (it->second);
590 format_file_map.erase (it);
593 FormatListChanged ();
597 ExportProfileManager::get_new_format (ExportFormatSpecPtr original)
599 ExportFormatSpecPtr format;
601 format.reset (new ExportFormatSpecification (*original));
603 format = handler->add_format();
604 format->set_name ("empty format");
607 sys::path path = save_format_to_disk (format);
608 FilePair pair (format->id(), path);
609 format_file_map.insert (pair);
611 format_list->push_back (format);
612 FormatListChanged ();
618 ExportProfileManager::init_formats (XMLNodeList nodes)
623 for (XMLNodeList::const_iterator it = nodes.begin(); it != nodes.end(); ++it) {
624 FormatStatePtr format = deserialize_format (**it);
626 formats.push_back (format);
627 } else { ok = false; }
630 if (formats.empty ()) {
631 FormatStatePtr format (new FormatState (format_list, ExportFormatSpecPtr ()));
632 formats.push_back (format);
639 ExportProfileManager::FormatStatePtr
640 ExportProfileManager::deserialize_format (XMLNode & root)
645 if ((prop = root.property ("id"))) {
649 for (FormatList::iterator it = format_list->begin(); it != format_list->end(); ++it) {
650 if ((*it)->id() == id) {
651 return FormatStatePtr (new FormatState (format_list, *it));
655 return FormatStatePtr ();
659 ExportProfileManager::serialize_format (FormatStatePtr state)
661 XMLNode * root = new XMLNode ("ExportFormat");
663 string id = state->format ? state->format->id().to_s() : "";
664 root->add_property ("id", id);
670 ExportProfileManager::load_formats ()
672 vector<sys::path> found = find_file (string_compose ("*%1", export_format_suffix));
674 for (vector<sys::path>::iterator it = found.begin(); it != found.end(); ++it) {
675 load_format_from_disk (*it);
680 ExportProfileManager::load_format_from_disk (PBD::sys::path const & path)
682 XMLTree const tree (path.to_string());
683 ExportFormatSpecPtr format = handler->add_format (*tree.root());
685 /* Handle id to filename mapping and don't add duplicates to list */
687 FilePair pair (format->id(), path);
688 if (format_file_map.insert (pair).second) {
689 format_list->push_back (format);
692 FormatListChanged ();
695 ExportProfileManager::FilenameStatePtr
696 ExportProfileManager::duplicate_filename_state (FilenameStatePtr state)
698 FilenameStatePtr filename (new FilenameState (handler->add_filename_copy (state->filename)));
699 filenames.push_back (filename);
704 ExportProfileManager::remove_filename_state (FilenameStatePtr state)
706 for (FilenameStateList::iterator it = filenames.begin(); it != filenames.end(); ++it) {
708 filenames.erase (it);
715 ExportProfileManager::get_sample_filename_for_format (ExportFilenamePtr filename, ExportFormatSpecPtr format)
719 if (channel_configs.empty()) { return ""; }
721 std::list<string> filenames;
722 build_filenames (filenames, filename, timespans.front()->timespans,
723 channel_configs.front()->config, format);
725 if (filenames.empty()) { return ""; }
726 return filenames.front();
730 ExportProfileManager::init_filenames (XMLNodeList nodes)
734 for (XMLNodeList::const_iterator it = nodes.begin(); it != nodes.end(); ++it) {
735 ExportFilenamePtr filename = handler->add_filename();
736 filename->set_state (**it);
737 filenames.push_back (FilenameStatePtr (new FilenameState (filename)));
740 if (filenames.empty()) {
741 FilenameStatePtr filename (new FilenameState (handler->add_filename()));
742 filenames.push_back (filename);
749 boost::shared_ptr<ExportProfileManager::Warnings>
750 ExportProfileManager::get_warnings ()
752 boost::shared_ptr<Warnings> warnings (new Warnings ());
754 ChannelConfigStatePtr channel_config_state;
755 if (!channel_configs.empty ()) {
756 channel_config_state = channel_configs.front();
759 TimespanStatePtr timespan_state = timespans.front();
761 /*** Check "global" config ***/
763 TimespanListPtr timespans = timespan_state->timespans;
765 ExportChannelConfigPtr channel_config;
766 if (channel_config_state) {
767 channel_config = channel_config_state->config;
770 /* Check Timespans are not empty */
772 if (timespans->empty()) {
773 warnings->errors.push_back (_("No timespan has been selected!"));
776 if (channel_config_state == 0) {
777 warnings->errors.push_back (_("No channels have been selected!"));
779 /* Check channel config ports */
780 if (!channel_config->all_channels_have_ports ()) {
781 warnings->warnings.push_back (_("Some channels are empty"));
785 /*** Check files ***/
787 if (channel_config_state) {
788 FormatStateList::const_iterator format_it;
789 FilenameStateList::const_iterator filename_it;
790 for (format_it = formats.begin(), filename_it = filenames.begin();
791 format_it != formats.end() && filename_it != filenames.end();
792 ++format_it, ++filename_it) {
793 check_config (warnings, timespan_state, channel_config_state, *format_it, *filename_it);
801 ExportProfileManager::check_config (boost::shared_ptr<Warnings> warnings,
802 TimespanStatePtr timespan_state,
803 ChannelConfigStatePtr channel_config_state,
804 FormatStatePtr format_state,
805 FilenameStatePtr filename_state)
807 TimespanListPtr timespans = timespan_state->timespans;
808 ExportChannelConfigPtr channel_config = channel_config_state->config;
809 ExportFormatSpecPtr format = format_state->format;
810 ExportFilenamePtr filename = filename_state->filename;
812 /* Check format and maximum channel count */
813 if (!format || !format->type()) {
814 warnings->errors.push_back (_("No format selected!"));
815 } else if (!channel_config->get_n_chans()) {
816 warnings->errors.push_back (_("All channels are empty!"));
817 } else if (!check_format (format, channel_config->get_n_chans())) {
818 warnings->errors.push_back (_("One or more of the selected formats is not compatible with this system!"));
819 } else if (format->channel_limit() < channel_config->get_n_chans()) {
820 warnings->errors.push_back
821 (string_compose (_("%1 supports only %2 channels, but you have %3 channels in your channel configuration"),
822 format->format_name(),
823 format->channel_limit(),
824 channel_config->get_n_chans()));
827 if (!warnings->errors.empty()) { return; }
829 /* Check filenames */
831 // filename->include_timespan = (timespans->size() > 1); Disabled for now...
833 std::list<string> paths;
834 build_filenames(paths, filename, timespans, channel_config, format);
836 for (std::list<string>::const_iterator path_it = paths.begin(); path_it != paths.end(); ++path_it) {
838 string path = *path_it;
840 if (sys::exists (sys::path (path))) {
841 warnings->conflicting_filenames.push_back (path);
844 if (format->with_toc()) {
845 string marker_file = handler->get_cd_marker_filename(path, CDMarkerTOC);
846 if (sys::exists (sys::path (marker_file))) {
847 warnings->conflicting_filenames.push_back (marker_file);
851 if (format->with_cue()) {
852 string marker_file = handler->get_cd_marker_filename(path, CDMarkerCUE);
853 if (sys::exists (sys::path (marker_file))) {
854 warnings->conflicting_filenames.push_back (marker_file);
861 ExportProfileManager::check_format (ExportFormatSpecPtr format, uint32_t channels)
863 switch (format->type()) {
864 case ExportFormatBase::T_Sndfile:
865 return check_sndfile_format (format, channels);
868 throw ExportFailed (X_("Invalid format given for ExportFileFactory::check!"));
873 ExportProfileManager::check_sndfile_format (ExportFormatSpecPtr format, unsigned int channels)
876 sf_info.channels = channels;
877 sf_info.samplerate = format->sample_rate ();
878 sf_info.format = format->format_id () | format->sample_format ();
880 return (sf_format_check (&sf_info) == SF_TRUE ? true : false);
884 ExportProfileManager::build_filenames(std::list<std::string> & result, ExportFilenamePtr filename,
885 TimespanListPtr timespans, ExportChannelConfigPtr channel_config,
886 ExportFormatSpecPtr format)
888 for (std::list<ExportTimespanPtr>::iterator timespan_it = timespans->begin();
889 timespan_it != timespans->end(); ++timespan_it) {
890 filename->set_timespan (*timespan_it);
892 if (channel_config->get_split()) {
893 filename->include_channel = true;
895 for (uint32_t chan = 1; chan <= channel_config->get_n_chans(); ++chan) {
896 filename->set_channel (chan);
897 result.push_back(filename->get_path (format));
901 filename->include_channel = false;
902 result.push_back(filename->get_path (format));
907 }; // namespace ARDOUR