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) :
53 handler (s.get_export_handler()),
56 session_range (new Location (s)),
57 ranges (new LocationList ()),
58 single_range_mode (false),
60 format_list (new FormatList ())
63 /* Initialize path variables */
65 export_config_dir = user_config_directory();
66 export_config_dir /= "export";
67 search_path += export_config_dir;
69 search_path += ardour_search_path().add_subdirectory_to_paths("export");
70 search_path += system_config_search_path().add_subdirectory_to_paths("export");;
72 /* create export config directory if necessary */
74 if (!sys::exists (export_config_dir)) {
75 sys::create_directory (export_config_dir);
81 /* Initialize all lists with an empty config */
84 init_timespans (dummy);
85 init_channel_configs (dummy);
87 init_filenames (dummy);
90 ExportProfileManager::~ExportProfileManager ()
92 if (single_range_mode) { return; }
94 XMLNode * instant_xml (new XMLNode ("ExportProfile"));
95 serialize_profile (*instant_xml);
96 session.add_instant_xml (*instant_xml, false);
100 ExportProfileManager::load_profile ()
102 XMLNode * instant_node = session.instant_xml ("ExportProfile");
104 set_state (*instant_node);
106 XMLNode empty_node ("ExportProfile");
107 set_state (empty_node);
112 ExportProfileManager::prepare_for_export ()
114 ChannelConfigPtr channel_config = channel_configs.front()->config;
115 TimespanListPtr ts_list = timespans.front()->timespans;
117 FormatStateList::const_iterator format_it;
118 FilenameStateList::const_iterator filename_it;
120 for (TimespanList::iterator ts_it = ts_list->begin(); ts_it != ts_list->end(); ++ts_it) {
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 // filename->include_timespan = (ts_list->size() > 1); Disabled for now...
127 boost::shared_ptr<BroadcastInfo> b;
128 if ((*format_it)->format->has_broadcast_info()) {
129 b.reset (new BroadcastInfo);
130 b->set_from_session (session, (*ts_it)->get_start());
133 handler->add_export_config (*ts_it, channel_config, (*format_it)->format, (*filename_it)->filename, b);
139 ExportProfileManager::load_preset (PresetPtr preset)
143 current_preset = preset;
144 if (!preset) { return false; }
146 XMLNode const * state;
147 if ((state = preset->get_local_state())) {
148 set_local_state (*state);
149 } else { ok = false; }
151 if ((state = preset->get_global_state())) {
152 if (!set_global_state (*state)) {
155 } else { ok = false; }
161 ExportProfileManager::load_presets ()
163 vector<sys::path> found = find_file (string_compose (X_("*%1"),export_preset_suffix));
165 for (vector<sys::path>::iterator it = found.begin(); it != found.end(); ++it) {
166 load_preset_from_disk (*it);
170 ExportProfileManager::PresetPtr
171 ExportProfileManager::save_preset (string const & name)
173 string filename = export_config_dir.to_string() + "/" + name + export_preset_suffix;
175 if (!current_preset) {
176 current_preset.reset (new ExportPreset (filename, session));
177 preset_list.push_back (current_preset);
180 XMLNode * global_preset = new XMLNode ("ExportPreset");
181 XMLNode * local_preset = new XMLNode ("ExportPreset");
183 serialize_global_profile (*global_preset);
184 serialize_local_profile (*local_preset);
186 current_preset->set_name (name);
187 current_preset->set_global_state (*global_preset);
188 current_preset->set_local_state (*local_preset);
190 current_preset->save (filename);
192 return current_preset;
196 ExportProfileManager::remove_preset ()
198 if (!current_preset) { return; }
200 for (PresetList::iterator it = preset_list.begin(); it != preset_list.end(); ++it) {
201 if (*it == current_preset) {
202 preset_list.erase (it);
207 FileMap::iterator it = preset_file_map.find (current_preset->id());
208 if (it != preset_file_map.end()) {
209 sys::remove (it->second);
210 preset_file_map.erase (it);
213 current_preset->remove_local();
214 current_preset.reset();
218 ExportProfileManager::load_preset_from_disk (PBD::sys::path const & path)
220 PresetPtr preset (new ExportPreset (path.to_string(), session));
222 /* Handle id to filename mapping and don't add duplicates to list */
224 FilePair pair (preset->id(), path);
225 if (preset_file_map.insert (pair).second) {
226 preset_list.push_back (preset);
231 ExportProfileManager::set_state (XMLNode const & root)
233 return set_global_state (root) & set_local_state (root);
237 ExportProfileManager::set_global_state (XMLNode const & root)
239 return init_filenames (root.children ("ExportFilename")) &
240 init_formats (root.children ("ExportFormat"));
244 ExportProfileManager::set_local_state (XMLNode const & root)
246 return init_timespans (root.children ("ExportTimespan")) &
247 init_channel_configs (root.children ("ExportChannelConfiguration"));
251 ExportProfileManager::serialize_profile (XMLNode & root)
253 serialize_local_profile (root);
254 serialize_global_profile (root);
258 ExportProfileManager::serialize_global_profile (XMLNode & root)
260 for (FormatStateList::iterator it = formats.begin(); it != formats.end(); ++it) {
261 root.add_child_nocopy (serialize_format (*it));
264 for (FilenameStateList::iterator it = filenames.begin(); it != filenames.end(); ++it) {
265 root.add_child_nocopy ((*it)->filename->get_state());
270 ExportProfileManager::serialize_local_profile (XMLNode & root)
272 for (TimespanStateList::iterator it = timespans.begin(); it != timespans.end(); ++it) {
273 root.add_child_nocopy (serialize_timespan (*it));
276 for (ChannelConfigStateList::iterator it = channel_configs.begin(); it != channel_configs.end(); ++it) {
277 root.add_child_nocopy ((*it)->config->get_state());
281 std::vector<sys::path>
282 ExportProfileManager::find_file (std::string const & pattern)
284 vector<sys::path> found;
286 Glib::PatternSpec pattern_spec (pattern);
287 find_matching_files_in_search_path (search_path, pattern_spec, found);
293 ExportProfileManager::set_selection_range (nframes_t start, nframes_t end)
297 selection_range.reset (new Location (session));
298 selection_range->set_name (_("Selection"));
299 selection_range->set (start, end);
301 selection_range.reset();
304 for (TimespanStateList::iterator it = timespans.begin(); it != timespans.end(); ++it) {
305 (*it)->selection_range = selection_range;
310 ExportProfileManager::set_single_range (nframes_t start, nframes_t end, string name)
312 single_range_mode = true;
314 single_range.reset (new Location (session));
315 single_range->set_name (name);
316 single_range->set (start, end);
320 return single_range->id().to_s();
324 ExportProfileManager::init_timespans (XMLNodeList nodes)
330 for (XMLNodeList::const_iterator it = nodes.begin(); it != nodes.end(); ++it) {
331 TimespanStatePtr span = deserialize_timespan (**it);
333 timespans.push_back (span);
334 } else { ok = false; }
337 if (timespans.empty()) {
338 TimespanStatePtr state (new TimespanState (session_range, selection_range, ranges));
339 timespans.push_back (state);
341 // Add session as default selection
342 TimespanPtr timespan = handler->add_timespan();
343 timespan->set_name (session_range->name());
344 timespan->set_range_id ("session");
345 timespan->set_range (session_range->start(), session_range->end());
346 state->timespans->push_back (timespan);
353 ExportProfileManager::TimespanStatePtr
354 ExportProfileManager::deserialize_timespan (XMLNode & root)
356 TimespanStatePtr state (new TimespanState (session_range, selection_range, ranges));
357 XMLProperty const * prop;
359 XMLNodeList spans = root.children ("Range");
360 for (XMLNodeList::iterator node_it = spans.begin(); node_it != spans.end(); ++node_it) {
362 prop = (*node_it)->property ("id");
363 if (!prop) { continue; }
364 string id = prop->value();
366 for (LocationList::iterator it = ranges->begin(); it != ranges->end(); ++it) {
367 if ((!id.compare ("session") && *it == session_range.get()) ||
368 (!id.compare ("selection") && *it == selection_range.get()) ||
369 (!id.compare ((*it)->id().to_s()))) {
370 TimespanPtr timespan = handler->add_timespan();
371 timespan->set_name ((*it)->name());
372 timespan->set_range_id (id);
373 timespan->set_range ((*it)->start(), (*it)->end());
374 state->timespans->push_back (timespan);
379 if ((prop = root.property ("format"))) {
380 state->time_format = (TimeFormat) string_2_enum (prop->value(), TimeFormat);
387 ExportProfileManager::serialize_timespan (TimespanStatePtr state)
389 XMLNode & root = *(new XMLNode ("ExportTimespan"));
394 for (TimespanList::iterator it = state->timespans->begin(); it != state->timespans->end(); ++it) {
395 if ((span = root.add_child ("Range"))) {
396 span->add_property ("id", (*it)->range_id());
400 root.add_property ("format", enum_2_string (state->time_format));
406 ExportProfileManager::update_ranges () {
409 if (single_range_mode) {
410 ranges->push_back (single_range.get());
416 session_range->set_name (_("Session"));
417 session_range->set (session.current_start_frame(), session.current_end_frame());
418 ranges->push_back (session_range.get());
422 if (selection_range) {
423 ranges->push_back (selection_range.get());
428 LocationList const & list (session.locations()->list());
429 for (LocationList::const_iterator it = list.begin(); it != list.end(); ++it) {
430 if ((*it)->is_range_marker()) {
431 ranges->push_back (*it);
437 ExportProfileManager::init_channel_configs (XMLNodeList nodes)
439 channel_configs.clear();
442 ChannelConfigStatePtr config (new ChannelConfigState (handler->add_channel_config()));
443 channel_configs.push_back (config);
445 // Add master outs as default
446 IO* master_out = session.master_out()->output().get();
447 if (!master_out) { return false; }
449 for (uint32_t n = 0; n < master_out->n_ports().n_audio(); ++n) {
450 PortExportChannel * channel = new PortExportChannel ();
451 channel->add_port (master_out->audio (n));
453 ExportChannelPtr chan_ptr (channel);
454 config->config->register_channel (chan_ptr);
459 for (XMLNodeList::const_iterator it = nodes.begin(); it != nodes.end(); ++it) {
460 ChannelConfigStatePtr config (new ChannelConfigState (handler->add_channel_config()));
461 config->config->set_state (**it);
462 channel_configs.push_back (config);
468 ExportProfileManager::FormatStatePtr
469 ExportProfileManager::duplicate_format_state (FormatStatePtr state)
471 /* Note: The pointer in the new FormatState should point to the same format spec
472 as the original state's pointer. The spec itself should not be copied! */
474 FormatStatePtr format (new FormatState (format_list, state->format));
475 formats.push_back (format);
480 ExportProfileManager::remove_format_state (FormatStatePtr state)
482 for (FormatStateList::iterator it = formats.begin(); it != formats.end(); ++it) {
491 ExportProfileManager::save_format_to_disk (FormatPtr format)
493 // TODO filename character stripping
495 /* Get filename for file */
497 string new_name = format->name();
498 new_name += export_format_suffix;
500 sys::path new_path (export_config_dir);
501 new_path /= new_name;
503 /* Check if format is on disk already */
504 FileMap::iterator it;
505 if ((it = format_file_map.find (format->id())) != format_file_map.end()) {
507 /* Check if config is not in user config dir */
508 if (it->second.branch_path().to_string().compare (export_config_dir.to_string())) {
512 XMLTree tree (new_path.to_string());
513 tree.set_root (&format->get_state());
518 /* Update file and rename if necessary */
520 XMLTree tree (it->second.to_string());
521 tree.set_root (&format->get_state());
524 if (new_name.compare (it->second.leaf())) {
525 sys::rename (it->second, new_path);
529 it->second = new_path;
534 XMLTree tree (new_path.to_string());
535 tree.set_root (&format->get_state());
539 FormatListChanged ();
544 ExportProfileManager::remove_format_profile (FormatPtr format)
546 for (FormatList::iterator it = format_list->begin(); it != format_list->end(); ++it) {
548 format_list->erase (it);
553 FileMap::iterator it = format_file_map.find (format->id());
554 if (it != format_file_map.end()) {
555 sys::remove (it->second);
556 format_file_map.erase (it);
559 FormatListChanged ();
562 ExportProfileManager::FormatPtr
563 ExportProfileManager::get_new_format (FormatPtr original)
567 format.reset (new ExportFormatSpecification (*original));
569 format = handler->add_format();
570 format->set_name ("empty format");
573 sys::path path = save_format_to_disk (format);
574 FilePair pair (format->id(), path);
575 format_file_map.insert (pair);
577 format_list->push_back (format);
578 FormatListChanged ();
584 ExportProfileManager::init_formats (XMLNodeList nodes)
589 for (XMLNodeList::const_iterator it = nodes.begin(); it != nodes.end(); ++it) {
590 FormatStatePtr format = deserialize_format (**it);
592 formats.push_back (format);
593 } else { ok = false; }
596 if (formats.empty ()) {
597 FormatStatePtr format (new FormatState (format_list, FormatPtr ()));
598 formats.push_back (format);
605 ExportProfileManager::FormatStatePtr
606 ExportProfileManager::deserialize_format (XMLNode & root)
611 if ((prop = root.property ("id"))) {
615 for (FormatList::iterator it = format_list->begin(); it != format_list->end(); ++it) {
616 if ((*it)->id() == id) {
617 return FormatStatePtr (new FormatState (format_list, *it));
621 return FormatStatePtr ();
625 ExportProfileManager::serialize_format (FormatStatePtr state)
627 XMLNode * root = new XMLNode ("ExportFormat");
629 string id = state->format ? state->format->id().to_s() : "";
630 root->add_property ("id", id);
636 ExportProfileManager::load_formats ()
638 vector<sys::path> found = find_file (string_compose ("*%1", export_format_suffix));
640 for (vector<sys::path>::iterator it = found.begin(); it != found.end(); ++it) {
641 load_format_from_disk (*it);
646 ExportProfileManager::load_format_from_disk (PBD::sys::path const & path)
648 XMLTree const tree (path.to_string());
649 FormatPtr format = handler->add_format (*tree.root());
651 /* Handle id to filename mapping and don't add duplicates to list */
653 FilePair pair (format->id(), path);
654 if (format_file_map.insert (pair).second) {
655 format_list->push_back (format);
658 FormatListChanged ();
661 ExportProfileManager::FilenameStatePtr
662 ExportProfileManager::duplicate_filename_state (FilenameStatePtr state)
664 FilenameStatePtr filename (new FilenameState (handler->add_filename_copy (state->filename)));
665 filenames.push_back (filename);
670 ExportProfileManager::remove_filename_state (FilenameStatePtr state)
672 for (FilenameStateList::iterator it = filenames.begin(); it != filenames.end(); ++it) {
674 filenames.erase (it);
681 ExportProfileManager::init_filenames (XMLNodeList nodes)
685 for (XMLNodeList::const_iterator it = nodes.begin(); it != nodes.end(); ++it) {
686 FilenamePtr filename = handler->add_filename();
687 filename->set_state (**it);
688 filenames.push_back (FilenameStatePtr (new FilenameState (filename)));
691 if (filenames.empty()) {
692 FilenameStatePtr filename (new FilenameState (handler->add_filename()));
693 filenames.push_back (filename);
700 boost::shared_ptr<ExportProfileManager::Warnings>
701 ExportProfileManager::get_warnings ()
703 boost::shared_ptr<Warnings> warnings (new Warnings ());
705 ChannelConfigStatePtr channel_config_state = channel_configs.front();
706 TimespanStatePtr timespan_state = timespans.front();
708 /*** Check "global" config ***/
710 TimespanListPtr timespans = timespan_state->timespans;
711 ChannelConfigPtr channel_config = channel_config_state->config;
713 /* Check Timespans are not empty */
715 if (timespans->empty()) {
716 warnings->errors.push_back (_("No timespan has been selected!"));
719 /* Check channel config ports */
721 if (!channel_config->all_channels_have_ports ()) {
722 warnings->warnings.push_back (_("Some channels are empty"));
725 /*** Check files ***/
727 FormatStateList::const_iterator format_it;
728 FilenameStateList::const_iterator filename_it;
729 for (format_it = formats.begin(), filename_it = filenames.begin();
730 format_it != formats.end() && filename_it != filenames.end();
731 ++format_it, ++filename_it) {
732 check_config (warnings, timespan_state, channel_config_state, *format_it, *filename_it);
739 ExportProfileManager::check_config (boost::shared_ptr<Warnings> warnings,
740 TimespanStatePtr timespan_state,
741 ChannelConfigStatePtr channel_config_state,
742 FormatStatePtr format_state,
743 FilenameStatePtr filename_state)
745 TimespanListPtr timespans = timespan_state->timespans;
746 ChannelConfigPtr channel_config = channel_config_state->config;
747 FormatPtr format = format_state->format;
748 FilenamePtr filename = filename_state->filename;
750 /* Check format and maximum channel count */
751 if (!format || !format->type()) {
752 warnings->errors.push_back (_("No format selected!"));
753 } else if (!channel_config->get_n_chans()) {
754 warnings->errors.push_back (_("All channels are empty!"));
755 } else if (!check_format (format, channel_config->get_n_chans())) {
756 warnings->errors.push_back (_("One or more of the selected formats is not compatible with this system!"));
757 } else if (format->channel_limit() < channel_config->get_n_chans()) {
758 warnings->errors.push_back
759 (string_compose (_("%1 supports only %2 channels, but you have %3 channels in your channel configuration"),
760 format->format_name(),
761 format->channel_limit(),
762 channel_config->get_n_chans()));
765 if (!warnings->errors.empty()) { return; }
767 /* Check filenames */
769 // filename->include_timespan = (timespans->size() > 1); Disabled for now...
771 for (std::list<TimespanPtr>::iterator timespan_it = timespans->begin(); timespan_it != timespans->end(); ++timespan_it) {
772 filename->set_timespan (*timespan_it);
774 if (channel_config->get_split()) {
775 filename->include_channel = true;
777 for (uint32_t chan = 1; chan <= channel_config->get_n_chans(); ++chan) {
778 filename->set_channel (chan);
780 string path = filename->get_path (format);
782 if (sys::exists (sys::path (path))) {
783 warnings->conflicting_filenames.push_back (path);
788 string path = filename->get_path (format);
790 if (sys::exists (sys::path (path))) {
791 warnings->conflicting_filenames.push_back (path);
798 ExportProfileManager::check_format (FormatPtr format, uint32_t channels)
800 switch (format->type()) {
801 case ExportFormatBase::T_Sndfile:
802 return check_sndfile_format (format, channels);
805 throw ExportFailed (X_("Invalid format given for ExportFileFactory::check!"));
810 ExportProfileManager::check_sndfile_format (FormatPtr format, unsigned int channels)
813 sf_info.channels = channels;
814 sf_info.samplerate = format->sample_rate ();
815 sf_info.format = format->format_id () | format->sample_format ();
817 return (sf_format_check (&sf_info) == SF_TRUE ? true : false);
820 }; // namespace ARDOUR