6 #include "pbd/basename.h"
7 #include "pbd/stateful.h"
8 #include "ardour/filename_extensions.h"
9 #include "ardour/send.h"
10 #include "ardour/track.h"
17 using namespace ARDOUR;
18 using namespace SessionUtils;
20 static bool opt_debug_dump = false;
21 static bool opt_copy_busses = false;
22 static bool opt_verbose = false;
23 static bool opt_log = false;
25 /* this is copied from Session::new_route_from_template */
27 trim_state_for_mixer_copy (Session*s, XMLNode& node)
29 /* trim bitslots from listen sends so that new ones are used */
30 XMLNodeList children = node.children ();
31 for (XMLNodeList::iterator i = children.begin (); i != children.end (); ++i) {
32 if ((*i)->name() == X_("Processor")) {
33 /* ForceIDRegeneration does not catch the following */
34 XMLProperty const * role = (*i)->property (X_("role"));
35 XMLProperty const * type = (*i)->property (X_("type"));
36 if (role && role->value () == X_("Aux")) {
37 /* check if the target bus exists,
38 * HERE: we use the bus-name (not target-id)
40 XMLProperty const * target = (*i)->property (X_("name"));
42 (*i)->set_property ("type", "dangling-aux-send");
45 boost::shared_ptr<Route> r = s->route_by_name (target->value ());
46 if (!r || boost::dynamic_pointer_cast<Track> (r)) {
47 (*i)->set_property ("type", "dangling-aux-send");
50 (*i)->set_property ("target", r->id ().to_s ());
52 if (role && role->value () == X_("Listen")) {
53 (*i)->remove_property (X_("bitslot"));
55 else if (role && (role->value () == X_("Send") || role->value () == X_("Aux"))) {
58 xrole = Delivery::Role (string_2_enum (role->value (), xrole));
59 std::string name = Send::name_and_id_new_send (*s, xrole, bitslot, false);
60 (*i)->remove_property (X_("bitslot"));
61 (*i)->remove_property (X_("name"));
62 (*i)->set_property ("bitslot", bitslot);
63 (*i)->set_property ("name", name);
65 else if (type && type->value () == X_("intreturn")) {
66 // ignore, in case bus existed in old session,
67 // tracks in old session may be connected to it.
68 // if the bus is new, new_route_from_template()
69 // will have re-created an ID.
70 (*i)->set_property ("type", "ignore-aux-return");
72 else if (type && type->value () == X_("return")) {
73 // Return::set_state() generates a new one
74 (*i)->remove_property (X_("bitslot"));
76 else if (type && type->value () == X_("port")) {
77 // PortInsert::set_state() handles the bitslot
78 (*i)->remove_property (X_("bitslot"));
79 (*i)->set_property ("ignore-name", "1");
86 copy_mixer_settings (Session*s, boost::shared_ptr<Route> dst, XMLNode& state)
88 PBD::Stateful::ForceIDRegeneration force_ids;
90 trim_state_for_mixer_copy (s, state);
91 state.remove_nodes_and_delete ("Diskstream");
92 state.remove_nodes_and_delete ("Automation");
97 dst->set_state (state, PBD::Stateful::loading_state_version);
101 copy_session_routes (
102 const std::string& src_path, const std::string& src_name,
103 const std::string& dst_path, const std::string& dst_load, const std::string& dst_save)
105 SessionUtils::init (opt_log);
108 typedef std::map<std::string,XMLNode*> StateMap;
112 s = SessionUtils::load_session (src_path, src_name, false);
115 fprintf (stderr, "Cannot load source session %s/%s.\n", src_path.c_str (), src_name.c_str ());
116 SessionUtils::cleanup ();
120 /* get route state from first session */
121 boost::shared_ptr<RouteList> rl = s->get_routes ();
122 for (RouteList::iterator i = rl->begin (); i != rl->end (); ++i) {
123 boost::shared_ptr<Route> r = *i;
124 if (r->is_master () || r->is_monitor () || r->is_auditioner ()) {
127 XMLNode& state (r->get_state ());
128 routestate[r->name ()] = &state;
129 if (boost::dynamic_pointer_cast<Track> (r)) {
132 buslist[r->name ()] = &state;
135 SessionUtils::unload_session (s);
138 /* open target session */
139 s = SessionUtils::load_session (dst_path, dst_load);
141 fprintf (stderr, "Cannot load target session %s/%s.\n", dst_path.c_str (), dst_load.c_str ());
142 SessionUtils::cleanup ();
146 /* iterate over all busses in the src session, add missing ones to target */
147 if (opt_copy_busses) {
148 rl = s->get_routes ();
149 for (StateMap::const_iterator i = buslist.begin (); i != buslist.end (); ++i) {
150 if (s->route_by_name (i->first)) {
153 XMLNode& rs (*(i->second));
154 s->new_route_from_template (1, PresentationInfo::max_order, rs, rs.property (X_("name"))->value (), NewPlaylist);
158 /* iterate over all *busses* in the target session.
159 * setup internal return targets.
161 rl = s->get_routes ();
162 for (RouteList::iterator i = rl->begin (); i != rl->end (); ++i) {
163 boost::shared_ptr<Route> r = *i;
164 /* skip special busses */
165 if (r->is_master () || r->is_monitor () || r->is_auditioner ()) {
168 if (boost::dynamic_pointer_cast<Track> (r)) {
171 /* find matching route by name */
172 std::map<std::string,XMLNode*>::iterator it = routestate.find (r->name ());
173 if (it == routestate.end ()) {
175 printf (" -- no match for '%s'\n", (*i)->name ().c_str ());
180 printf ("-- found match '%s'\n", (*i)->name ().c_str ());
182 XMLNode *state = it->second;
184 copy_mixer_settings (s, r, *state);
187 /* iterate over all tracks in the target session.. */
188 rl = s->get_routes ();
189 for (RouteList::iterator i = rl->begin (); i != rl->end (); ++i) {
190 boost::shared_ptr<Route> r = *i;
191 /* skip special busses */
192 if (r->is_master () || r->is_monitor () || r->is_auditioner ()) {
195 if (!boost::dynamic_pointer_cast<Track> (r)) {
199 /* find matching route by name */
200 std::map<std::string,XMLNode*>::iterator it = routestate.find (r->name ());
201 if (it == routestate.end ()) {
203 printf (" -- no match for '%s'\n", (*i)->name ().c_str ());
208 printf ("-- found match '%s'\n", (*i)->name ().c_str ());
210 XMLNode *state = it->second;
212 copy_mixer_settings (s, r, *state);
215 s->save_state (dst_save);
218 SessionUtils::unload_session (s);
221 for (StateMap::iterator i = routestate.begin (); i != routestate.end (); ++i) {
222 XMLNode *state = i->second;
226 SessionUtils::cleanup ();
231 static void usage (int status) {
232 // help2man compatible format (standard GNU help-text)
233 printf (UTILNAME " - copy mixer settings from one session to another.\n\n");
234 printf ("Usage: " UTILNAME " [ OPTIONS ] <src> <dst>\n\n");
237 -h, --help display this help and exit\n\
238 -b, --bus-copy add busses present in src to dst\n\
239 -d, --debug print pre-processed XML for each route\n\
240 -l, --log-messages display libardour log messages\n\
241 -s, --snapshot <name> create a new snapshot in dst\n\
242 -v, --verbose show performed copy operations\n\
243 -V, --version print version information and exit\n\
247 This utility copies mixer-settings from the src-session to the dst-session.\n\
248 Both <src> and <dst> are paths to .ardour session files.\n\
249 If --snapshot is not given, the <dst> session file is overwritten.\n\
250 When --snapshot is set, a new snaphot in the <dst> session is created.\n\
253 printf ("Report bugs to <http://tracker.ardour.org/>\n"
254 "Website: <http://ardour.org/>\n");
258 static bool ends_with (std::string const& value, std::string const& ending)
260 if (ending.size() > value.size()) return false;
261 return std::equal(ending.rbegin(), ending.rend(), value.rbegin());
264 int main (int argc, char* argv[])
266 const char *optstring = "bhls:Vv";
268 const struct option longopts[] = {
269 { "bus-copy", required_argument, 0, 'b' },
270 { "debug", no_argument, 0, 'd' },
271 { "help", no_argument, 0, 'h' },
272 { "log-messages", no_argument, 0, 'l' },
273 { "snapshot", no_argument, 0, 's' },
274 { "version", no_argument, 0, 'V' },
275 { "vebose", no_argument, 0, 'v' },
279 std::string dst_snapshot_name = "";
281 while (EOF != (c = getopt_long (argc, argv,
282 optstring, longopts, (int *) 0))) {
285 opt_copy_busses = true;
289 opt_debug_dump = true;
301 dst_snapshot_name = optarg;
305 printf ("ardour-utils version %s\n\n", VERSIONSTRING);
306 printf ("Copyright (C) GPL 2016 Robin Gareus <robin@gareus.org>\n");
315 usage (EXIT_FAILURE);
320 // TODO parse path/name from a single argument.
322 if (optind + 2 > argc) {
323 usage (EXIT_FAILURE);
326 std::string src = argv[optind];
327 std::string dst = argv[optind + 1];
331 if (!ends_with (src, statefile_suffix)) {
332 fprintf (stderr, "source is not a .ardour session file.\n");
335 if (!ends_with (dst, statefile_suffix)) {
336 fprintf (stderr, "target is not a .ardour session file.\n");
339 if (!Glib::file_test (src, Glib::FILE_TEST_IS_REGULAR)) {
340 fprintf (stderr, "source is not a regular file.\n");
343 if (!Glib::file_test (dst, Glib::FILE_TEST_IS_REGULAR)) {
344 fprintf (stderr, "target is not a regular file.\n");
348 std::string src_path = Glib::path_get_dirname (src);
349 std::string src_name = PBD::basename_nosuffix (src);
350 std::string dst_path = Glib::path_get_dirname (dst);
351 std::string dst_name = PBD::basename_nosuffix (dst);
353 // TODO check if src != dst ..
354 return copy_session_routes (