2 Copyright (C) 2008 Paul Davis
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 #include <glibmm/fileutils.h>
26 #include "pbd/enumwriter.h"
27 #include "pbd/xml++.h"
28 #include "pbd/convert.h"
30 #include "ardour/export_profile_manager.h"
31 #include "ardour/export_format_specification.h"
32 #include "ardour/export_timespan.h"
33 #include "ardour/export_channel_configuration.h"
34 #include "ardour/export_filename.h"
35 #include "ardour/export_preset.h"
36 #include "ardour/export_handler.h"
37 #include "ardour/export_failed.h"
38 #include "ardour/filename_extensions.h"
39 #include "ardour/route.h"
40 #include "ardour/session.h"
41 #include "ardour/broadcast_info.h"
52 ExportProfileManager::ExportProfileManager (Session & s, std::string xml_node_name)
53 : xml_node_name (xml_node_name)
54 , handler (s.get_export_handler())
57 , session_range (new Location (s))
58 , ranges (new LocationList ())
59 , single_range_mode (false)
61 , format_list (new FormatList ())
64 /* Initialize path variables */
66 export_config_dir = user_config_directory();
67 export_config_dir /= "export";
68 search_path += export_config_dir;
70 search_path += ardour_search_path().add_subdirectory_to_paths("export");
72 sys::path sys_export = ardour_module_directory();
73 sys_export /= "export";
75 search_path += sys_export;
77 info << string_compose (_("Searching for export formats in %1"), search_path.to_string()) << endmsg;
78 cerr << string_compose (_("Searching for export formats in %1"), search_path.to_string()) << endl;
80 /* create export config directory if necessary */
82 if (!sys::exists (export_config_dir)) {
83 sys::create_directory (export_config_dir);
89 /* Initialize all lists with an empty config */
92 init_timespans (dummy);
93 init_channel_configs (dummy);
95 init_filenames (dummy);
98 ExportProfileManager::~ExportProfileManager ()
100 if (single_range_mode) { return; }
102 XMLNode * instant_xml (new XMLNode (xml_node_name));
103 serialize_profile (*instant_xml);
104 session.add_instant_xml (*instant_xml, false);
108 ExportProfileManager::load_profile ()
110 XMLNode * instant_node = session.instant_xml (xml_node_name);
112 set_state (*instant_node);
114 XMLNode empty_node (xml_node_name);
115 set_state (empty_node);
120 ExportProfileManager::prepare_for_export ()
122 TimespanListPtr ts_list = timespans.front()->timespans;
124 FormatStateList::const_iterator format_it;
125 FilenameStateList::const_iterator filename_it;
128 for (TimespanList::iterator ts_it = ts_list->begin(); ts_it != ts_list->end(); ++ts_it) {
129 // ..., each format-filename pair
130 for (format_it = formats.begin(), filename_it = filenames.begin();
131 format_it != formats.end() && filename_it != filenames.end();
132 ++format_it, ++filename_it) {
134 FilenamePtr filename = (*filename_it)->filename;
135 // filename->include_timespan = (ts_list->size() > 1); Disabled for now...
137 boost::shared_ptr<BroadcastInfo> b;
138 if ((*format_it)->format->has_broadcast_info()) {
139 b.reset (new BroadcastInfo);
140 b->set_from_session (session, (*ts_it)->get_start());
143 // ...and each channel config
144 filename->include_channel_config = (channel_configs.size() > 1);
145 for(ChannelConfigStateList::iterator cc_it = channel_configs.begin(); cc_it != channel_configs.end(); ++cc_it) {
146 handler->add_export_config (*ts_it, (*cc_it)->config, (*format_it)->format, filename, b);
153 ExportProfileManager::load_preset (PresetPtr preset)
157 current_preset = preset;
158 if (!preset) { return false; }
160 XMLNode const * state;
161 if ((state = preset->get_local_state())) {
162 set_local_state (*state);
163 } else { ok = false; }
165 if ((state = preset->get_global_state())) {
166 if (!set_global_state (*state)) {
169 } else { ok = false; }
175 ExportProfileManager::load_presets ()
177 vector<sys::path> found = find_file (string_compose (X_("*%1"),export_preset_suffix));
179 for (vector<sys::path>::iterator it = found.begin(); it != found.end(); ++it) {
180 load_preset_from_disk (*it);
184 ExportProfileManager::PresetPtr
185 ExportProfileManager::save_preset (string const & name)
187 string filename = export_config_dir.to_string() + "/" + name + export_preset_suffix;
189 if (!current_preset) {
190 current_preset.reset (new ExportPreset (filename, session));
191 preset_list.push_back (current_preset);
194 XMLNode * global_preset = new XMLNode ("ExportPreset");
195 XMLNode * local_preset = new XMLNode ("ExportPreset");
197 serialize_global_profile (*global_preset);
198 serialize_local_profile (*local_preset);
200 current_preset->set_name (name);
201 current_preset->set_global_state (*global_preset);
202 current_preset->set_local_state (*local_preset);
204 current_preset->save (filename);
206 return current_preset;
210 ExportProfileManager::remove_preset ()
212 if (!current_preset) { return; }
214 for (PresetList::iterator it = preset_list.begin(); it != preset_list.end(); ++it) {
215 if (*it == current_preset) {
216 preset_list.erase (it);
221 FileMap::iterator it = preset_file_map.find (current_preset->id());
222 if (it != preset_file_map.end()) {
223 sys::remove (it->second);
224 preset_file_map.erase (it);
227 current_preset->remove_local();
228 current_preset.reset();
232 ExportProfileManager::load_preset_from_disk (PBD::sys::path const & path)
234 PresetPtr preset (new ExportPreset (path.to_string(), session));
236 /* Handle id to filename mapping and don't add duplicates to list */
238 FilePair pair (preset->id(), path);
239 if (preset_file_map.insert (pair).second) {
240 preset_list.push_back (preset);
245 ExportProfileManager::set_state (XMLNode const & root)
247 return set_global_state (root) & set_local_state (root);
251 ExportProfileManager::set_global_state (XMLNode const & root)
253 return init_filenames (root.children ("ExportFilename")) &
254 init_formats (root.children ("ExportFormat"));
258 ExportProfileManager::set_local_state (XMLNode const & root)
260 return init_timespans (root.children ("ExportTimespan")) &
261 init_channel_configs (root.children ("ExportChannelConfiguration"));
265 ExportProfileManager::serialize_profile (XMLNode & root)
267 serialize_local_profile (root);
268 serialize_global_profile (root);
272 ExportProfileManager::serialize_global_profile (XMLNode & root)
274 for (FormatStateList::iterator it = formats.begin(); it != formats.end(); ++it) {
275 root.add_child_nocopy (serialize_format (*it));
278 for (FilenameStateList::iterator it = filenames.begin(); it != filenames.end(); ++it) {
279 root.add_child_nocopy ((*it)->filename->get_state());
284 ExportProfileManager::serialize_local_profile (XMLNode & root)
286 for (TimespanStateList::iterator it = timespans.begin(); it != timespans.end(); ++it) {
287 root.add_child_nocopy (serialize_timespan (*it));
290 for (ChannelConfigStateList::iterator it = channel_configs.begin(); it != channel_configs.end(); ++it) {
291 root.add_child_nocopy ((*it)->config->get_state());
295 std::vector<sys::path>
296 ExportProfileManager::find_file (std::string const & pattern)
298 vector<sys::path> found;
300 Glib::PatternSpec pattern_spec (pattern);
301 find_matching_files_in_search_path (search_path, pattern_spec, found);
307 ExportProfileManager::set_selection_range (framepos_t start, framepos_t end)
311 selection_range.reset (new Location (session));
312 selection_range->set_name (_("Selection"));
313 selection_range->set (start, end);
315 selection_range.reset();
318 for (TimespanStateList::iterator it = timespans.begin(); it != timespans.end(); ++it) {
319 (*it)->selection_range = selection_range;
324 ExportProfileManager::set_single_range (framepos_t start, framepos_t end, string name)
326 single_range_mode = true;
328 single_range.reset (new Location (session));
329 single_range->set_name (name);
330 single_range->set (start, end);
334 return single_range->id().to_s();
338 ExportProfileManager::init_timespans (XMLNodeList nodes)
344 for (XMLNodeList::const_iterator it = nodes.begin(); it != nodes.end(); ++it) {
345 TimespanStatePtr span = deserialize_timespan (**it);
347 timespans.push_back (span);
348 } else { ok = false; }
351 if (timespans.empty()) {
352 TimespanStatePtr state (new TimespanState (session_range, selection_range, ranges));
353 timespans.push_back (state);
355 // Add session as default selection
356 TimespanPtr timespan = handler->add_timespan();
357 timespan->set_name (session_range->name());
358 timespan->set_range_id ("session");
359 timespan->set_range (session_range->start(), session_range->end());
360 state->timespans->push_back (timespan);
367 ExportProfileManager::TimespanStatePtr
368 ExportProfileManager::deserialize_timespan (XMLNode & root)
370 TimespanStatePtr state (new TimespanState (session_range, selection_range, ranges));
371 XMLProperty const * prop;
373 XMLNodeList spans = root.children ("Range");
374 for (XMLNodeList::iterator node_it = spans.begin(); node_it != spans.end(); ++node_it) {
376 prop = (*node_it)->property ("id");
377 if (!prop) { continue; }
378 string id = prop->value();
380 for (LocationList::iterator it = ranges->begin(); it != ranges->end(); ++it) {
381 if ((!id.compare ("session") && *it == session_range.get()) ||
382 (!id.compare ("selection") && *it == selection_range.get()) ||
383 (!id.compare ((*it)->id().to_s()))) {
384 TimespanPtr timespan = handler->add_timespan();
385 timespan->set_name ((*it)->name());
386 timespan->set_range_id (id);
387 timespan->set_range ((*it)->start(), (*it)->end());
388 state->timespans->push_back (timespan);
393 if ((prop = root.property ("format"))) {
394 state->time_format = (TimeFormat) string_2_enum (prop->value(), TimeFormat);
401 ExportProfileManager::serialize_timespan (TimespanStatePtr state)
403 XMLNode & root = *(new XMLNode ("ExportTimespan"));
408 for (TimespanList::iterator it = state->timespans->begin(); it != state->timespans->end(); ++it) {
409 if ((span = root.add_child ("Range"))) {
410 span->add_property ("id", (*it)->range_id());
414 root.add_property ("format", enum_2_string (state->time_format));
420 ExportProfileManager::update_ranges () {
423 if (single_range_mode) {
424 ranges->push_back (single_range.get());
430 session_range->set_name (_("Session"));
431 session_range->set (session.current_start_frame(), session.current_end_frame());
432 ranges->push_back (session_range.get());
436 if (selection_range) {
437 ranges->push_back (selection_range.get());
442 LocationList const & list (session.locations()->list());
443 for (LocationList::const_iterator it = list.begin(); it != list.end(); ++it) {
444 if ((*it)->is_range_marker()) {
445 ranges->push_back (*it);
450 ExportProfileManager::ChannelConfigStatePtr
451 ExportProfileManager::add_channel_config ()
453 ChannelConfigStatePtr ptr(new ChannelConfigState(handler->add_channel_config()));
454 channel_configs.push_back(ptr);
459 ExportProfileManager::init_channel_configs (XMLNodeList nodes)
461 channel_configs.clear();
464 ChannelConfigStatePtr config (new ChannelConfigState (handler->add_channel_config()));
465 channel_configs.push_back (config);
467 // Add master outs as default
468 IO* master_out = session.master_out()->output().get();
469 if (!master_out) { return false; }
471 for (uint32_t n = 0; n < master_out->n_ports().n_audio(); ++n) {
472 PortExportChannel * channel = new PortExportChannel ();
473 channel->add_port (master_out->audio (n));
475 ExportChannelPtr chan_ptr (channel);
476 config->config->register_channel (chan_ptr);
481 for (XMLNodeList::const_iterator it = nodes.begin(); it != nodes.end(); ++it) {
482 ChannelConfigStatePtr config (new ChannelConfigState (handler->add_channel_config()));
483 config->config->set_state (**it);
484 channel_configs.push_back (config);
490 ExportProfileManager::FormatStatePtr
491 ExportProfileManager::duplicate_format_state (FormatStatePtr state)
493 /* Note: The pointer in the new FormatState should point to the same format spec
494 as the original state's pointer. The spec itself should not be copied! */
496 FormatStatePtr format (new FormatState (format_list, state->format));
497 formats.push_back (format);
502 ExportProfileManager::remove_format_state (FormatStatePtr state)
504 for (FormatStateList::iterator it = formats.begin(); it != formats.end(); ++it) {
513 ExportProfileManager::save_format_to_disk (FormatPtr format)
515 // TODO filename character stripping
517 /* Get filename for file */
519 string new_name = format->name();
520 new_name += export_format_suffix;
522 sys::path new_path (export_config_dir);
523 new_path /= new_name;
525 /* Check if format is on disk already */
526 FileMap::iterator it;
527 if ((it = format_file_map.find (format->id())) != format_file_map.end()) {
529 /* Check if config is not in user config dir */
530 if (it->second.branch_path().to_string().compare (export_config_dir.to_string())) {
534 XMLTree tree (new_path.to_string());
535 tree.set_root (&format->get_state());
540 /* Update file and rename if necessary */
542 XMLTree tree (it->second.to_string());
543 tree.set_root (&format->get_state());
546 if (new_name.compare (it->second.leaf())) {
547 sys::rename (it->second, new_path);
551 it->second = new_path;
556 XMLTree tree (new_path.to_string());
557 tree.set_root (&format->get_state());
561 FormatListChanged ();
566 ExportProfileManager::remove_format_profile (FormatPtr format)
568 for (FormatList::iterator it = format_list->begin(); it != format_list->end(); ++it) {
570 format_list->erase (it);
575 FileMap::iterator it = format_file_map.find (format->id());
576 if (it != format_file_map.end()) {
577 sys::remove (it->second);
578 format_file_map.erase (it);
581 FormatListChanged ();
584 ExportProfileManager::FormatPtr
585 ExportProfileManager::get_new_format (FormatPtr original)
589 format.reset (new ExportFormatSpecification (*original));
591 format = handler->add_format();
592 format->set_name ("empty format");
595 sys::path path = save_format_to_disk (format);
596 FilePair pair (format->id(), path);
597 format_file_map.insert (pair);
599 format_list->push_back (format);
600 FormatListChanged ();
606 ExportProfileManager::init_formats (XMLNodeList nodes)
611 for (XMLNodeList::const_iterator it = nodes.begin(); it != nodes.end(); ++it) {
612 FormatStatePtr format = deserialize_format (**it);
614 formats.push_back (format);
615 } else { ok = false; }
618 if (formats.empty ()) {
619 FormatStatePtr format (new FormatState (format_list, FormatPtr ()));
620 formats.push_back (format);
627 ExportProfileManager::FormatStatePtr
628 ExportProfileManager::deserialize_format (XMLNode & root)
633 if ((prop = root.property ("id"))) {
637 for (FormatList::iterator it = format_list->begin(); it != format_list->end(); ++it) {
638 if ((*it)->id() == id) {
639 return FormatStatePtr (new FormatState (format_list, *it));
643 return FormatStatePtr ();
647 ExportProfileManager::serialize_format (FormatStatePtr state)
649 XMLNode * root = new XMLNode ("ExportFormat");
651 string id = state->format ? state->format->id().to_s() : "";
652 root->add_property ("id", id);
658 ExportProfileManager::load_formats ()
660 vector<sys::path> found = find_file (string_compose ("*%1", export_format_suffix));
662 for (vector<sys::path>::iterator it = found.begin(); it != found.end(); ++it) {
663 load_format_from_disk (*it);
668 ExportProfileManager::load_format_from_disk (PBD::sys::path const & path)
670 XMLTree const tree (path.to_string());
671 FormatPtr format = handler->add_format (*tree.root());
673 /* Handle id to filename mapping and don't add duplicates to list */
675 FilePair pair (format->id(), path);
676 if (format_file_map.insert (pair).second) {
677 format_list->push_back (format);
680 FormatListChanged ();
683 ExportProfileManager::FilenameStatePtr
684 ExportProfileManager::duplicate_filename_state (FilenameStatePtr state)
686 FilenameStatePtr filename (new FilenameState (handler->add_filename_copy (state->filename)));
687 filenames.push_back (filename);
692 ExportProfileManager::remove_filename_state (FilenameStatePtr state)
694 for (FilenameStateList::iterator it = filenames.begin(); it != filenames.end(); ++it) {
696 filenames.erase (it);
703 ExportProfileManager::init_filenames (XMLNodeList nodes)
707 for (XMLNodeList::const_iterator it = nodes.begin(); it != nodes.end(); ++it) {
708 FilenamePtr filename = handler->add_filename();
709 filename->set_state (**it);
710 filenames.push_back (FilenameStatePtr (new FilenameState (filename)));
713 if (filenames.empty()) {
714 FilenameStatePtr filename (new FilenameState (handler->add_filename()));
715 filenames.push_back (filename);
722 boost::shared_ptr<ExportProfileManager::Warnings>
723 ExportProfileManager::get_warnings ()
725 boost::shared_ptr<Warnings> warnings (new Warnings ());
727 assert (!channel_configs.empty ());
729 ChannelConfigStatePtr channel_config_state = channel_configs.front();
730 TimespanStatePtr timespan_state = timespans.front();
732 /*** Check "global" config ***/
734 TimespanListPtr timespans = timespan_state->timespans;
735 ChannelConfigPtr channel_config = channel_config_state->config;
737 /* Check Timespans are not empty */
739 if (timespans->empty()) {
740 warnings->errors.push_back (_("No timespan has been selected!"));
743 /* Check channel config ports */
745 if (!channel_config->all_channels_have_ports ()) {
746 warnings->warnings.push_back (_("Some channels are empty"));
749 /*** Check files ***/
751 FormatStateList::const_iterator format_it;
752 FilenameStateList::const_iterator filename_it;
753 for (format_it = formats.begin(), filename_it = filenames.begin();
754 format_it != formats.end() && filename_it != filenames.end();
755 ++format_it, ++filename_it) {
756 check_config (warnings, timespan_state, channel_config_state, *format_it, *filename_it);
763 ExportProfileManager::check_config (boost::shared_ptr<Warnings> warnings,
764 TimespanStatePtr timespan_state,
765 ChannelConfigStatePtr channel_config_state,
766 FormatStatePtr format_state,
767 FilenameStatePtr filename_state)
769 TimespanListPtr timespans = timespan_state->timespans;
770 ChannelConfigPtr channel_config = channel_config_state->config;
771 FormatPtr format = format_state->format;
772 FilenamePtr filename = filename_state->filename;
774 /* Check format and maximum channel count */
775 if (!format || !format->type()) {
776 warnings->errors.push_back (_("No format selected!"));
777 } else if (!channel_config->get_n_chans()) {
778 warnings->errors.push_back (_("All channels are empty!"));
779 } else if (!check_format (format, channel_config->get_n_chans())) {
780 warnings->errors.push_back (_("One or more of the selected formats is not compatible with this system!"));
781 } else if (format->channel_limit() < channel_config->get_n_chans()) {
782 warnings->errors.push_back
783 (string_compose (_("%1 supports only %2 channels, but you have %3 channels in your channel configuration"),
784 format->format_name(),
785 format->channel_limit(),
786 channel_config->get_n_chans()));
789 if (!warnings->errors.empty()) { return; }
791 /* Check filenames */
793 // filename->include_timespan = (timespans->size() > 1); Disabled for now...
795 for (std::list<TimespanPtr>::iterator timespan_it = timespans->begin(); timespan_it != timespans->end(); ++timespan_it) {
796 filename->set_timespan (*timespan_it);
798 if (channel_config->get_split()) {
799 filename->include_channel = true;
801 for (uint32_t chan = 1; chan <= channel_config->get_n_chans(); ++chan) {
802 filename->set_channel (chan);
804 string path = filename->get_path (format);
806 if (sys::exists (sys::path (path))) {
807 warnings->conflicting_filenames.push_back (path);
812 string path = filename->get_path (format);
814 if (sys::exists (sys::path (path))) {
815 warnings->conflicting_filenames.push_back (path);
822 ExportProfileManager::check_format (FormatPtr format, uint32_t channels)
824 switch (format->type()) {
825 case ExportFormatBase::T_Sndfile:
826 return check_sndfile_format (format, channels);
829 throw ExportFailed (X_("Invalid format given for ExportFileFactory::check!"));
834 ExportProfileManager::check_sndfile_format (FormatPtr format, unsigned int channels)
837 sf_info.channels = channels;
838 sf_info.samplerate = format->sample_rate ();
839 sf_info.format = format->format_id () | format->sample_format ();
841 return (sf_format_check (&sf_info) == SF_TRUE ? true : false);
844 }; // namespace ARDOUR