Add options to set bit-depth and bwf to session-export util.
[ardour.git] / session_utils / copy-mixer.cc
1 #include <iostream>
2 #include <cstdlib>
3 #include <getopt.h>
4 #include <glibmm.h>
5
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"
11
12 #include "common.h"
13
14 #define X_(Text) Text
15
16 using namespace std;
17 using namespace ARDOUR;
18 using namespace SessionUtils;
19
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;
24
25 /* this is copied from  Session::new_route_from_template */
26 static void
27 trim_state_for_mixer_copy (Session*s, XMLNode& node)
28 {
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)
39                                  */
40                                 XMLProperty const * target = (*i)->property (X_("name"));
41                                 if (!target) {
42                                         (*i)->set_property ("type", "dangling-aux-send");
43                                         continue;
44                                 }
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");
48                                         continue;
49                                 }
50                                 (*i)->set_property ("target", r->id ().to_s ());
51                         }
52                         if (role && role->value () == X_("Listen")) {
53                                 (*i)->remove_property (X_("bitslot"));
54                         }
55                         else if (role && (role->value () == X_("Send") || role->value () == X_("Aux"))) {
56                                 Delivery::Role xrole;
57                                 uint32_t bitslot = 0;
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);
64                         }
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");
71                         }
72                         else if (type && type->value () == X_("return")) {
73                                 // Return::set_state() generates a new one
74                                 (*i)->remove_property (X_("bitslot"));
75                         }
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");
80                         }
81                 }
82         }
83 }
84
85 static void
86 copy_mixer_settings (Session*s, boost::shared_ptr<Route> dst, XMLNode& state)
87 {
88         PBD::Stateful::ForceIDRegeneration force_ids;
89
90         trim_state_for_mixer_copy (s, state);
91         state.remove_nodes_and_delete ("Diskstream");
92         state.remove_nodes_and_delete ("Automation");
93         if (opt_debug_dump) {
94                 state.dump (cout);
95         }
96
97         dst->set_state (state, PBD::Stateful::loading_state_version);
98 }
99
100 static int
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)
104 {
105         SessionUtils::init (opt_log);
106         Session* s = 0;
107
108         typedef std::map<std::string,XMLNode*> StateMap;
109         StateMap routestate;
110         StateMap buslist;
111
112         s = SessionUtils::load_session (src_path, src_name, false);
113
114         if (!s) {
115                 fprintf (stderr, "Cannot load source session %s/%s.\n", src_path.c_str (), src_name.c_str ());
116                 SessionUtils::cleanup ();
117                 return -1;
118         }
119
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 ()) {
125                         continue;
126                 }
127                 XMLNode& state (r->get_state ());
128                 routestate[r->name ()] = &state;
129                 if (boost::dynamic_pointer_cast<Track> (r)) {
130                         continue;
131                 }
132                 buslist[r->name ()] = &state;
133         }
134         rl.reset ();
135         SessionUtils::unload_session (s);
136
137
138         /* open target session */
139         s = SessionUtils::load_session (dst_path, dst_load);
140         if (!s) {
141                 fprintf (stderr, "Cannot load target session %s/%s.\n", dst_path.c_str (), dst_load.c_str ());
142                 SessionUtils::cleanup ();
143                 return -1;
144         }
145
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)) {
151                                 continue;
152                         }
153                         XMLNode& rs (*(i->second));
154                         s->new_route_from_template (1, PresentationInfo::max_order, rs, rs.property (X_("name"))->value (), NewPlaylist);
155                 }
156         }
157
158         /* iterate over all *busses* in the target session.
159          * setup internal return targets.
160          */
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 ()) {
166                         continue;
167                 }
168                 if (boost::dynamic_pointer_cast<Track> (r)) {
169                         continue;
170                 }
171                 /* find matching route by name */
172                 std::map<std::string,XMLNode*>::iterator it = routestate.find (r->name ());
173                 if (it == routestate.end ()) {
174                         if (opt_verbose) {
175                                 printf (" -- no match for '%s'\n", (*i)->name ().c_str ());
176                         }
177                         continue;
178                 }
179                 if (opt_verbose) {
180                         printf ("-- found match '%s'\n", (*i)->name ().c_str ());
181                 }
182                 XMLNode *state = it->second;
183                 // copy state
184                 copy_mixer_settings (s, r, *state);
185         }
186
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 ()) {
193                         continue;
194                 }
195                 if (!boost::dynamic_pointer_cast<Track> (r)) {
196                         continue;
197                 }
198
199                 /* find matching route by name */
200                 std::map<std::string,XMLNode*>::iterator it = routestate.find (r->name ());
201                 if (it == routestate.end ()) {
202                         if (opt_verbose) {
203                                 printf (" -- no match for '%s'\n", (*i)->name ().c_str ());
204                         }
205                         continue;
206                 }
207                 if (opt_verbose) {
208                         printf ("-- found match '%s'\n", (*i)->name ().c_str ());
209                 }
210                 XMLNode *state = it->second;
211                 /* copy state */
212                 copy_mixer_settings (s, r, *state);
213         }
214
215         s->save_state (dst_save);
216
217         rl.reset ();
218         SessionUtils::unload_session (s);
219
220         // clean up.
221         for (StateMap::iterator i = routestate.begin (); i != routestate.end (); ++i) {
222                 XMLNode *state = i->second;
223                 delete state;
224         }
225
226         SessionUtils::cleanup ();
227         return 0;
228 }
229
230
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");
235
236         printf ("Options:\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 perfomed copy opeations\n\
243   -V, --version              print version information and exit\n\
244 \n");
245
246         printf ("\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\
251 \n");
252
253         printf ("Report bugs to <http://tracker.ardour.org/>\n"
254                 "Website: <http://ardour.org/>\n");
255         ::exit (status);
256 }
257
258 static bool ends_with (std::string const& value, std::string const& ending)
259 {
260         if (ending.size() > value.size()) return false;
261         return std::equal(ending.rbegin(), ending.rend(), value.rbegin());
262 }
263
264 int main (int argc, char* argv[])
265 {
266         const char *optstring = "bhls:Vv";
267
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' },
276         };
277
278         int c = 0;
279         std::string dst_snapshot_name = "";
280
281         while (EOF != (c = getopt_long (argc, argv,
282                                         optstring, longopts, (int *) 0))) {
283                 switch (c) {
284                         case 'b':
285                                 opt_copy_busses = true;
286                                 break;
287
288                         case 'd':
289                                 opt_debug_dump = true;
290                                 break;
291
292                         case 'h':
293                                 usage (0);
294                                 break;
295
296                         case 'l':
297                                 opt_log = true;
298                                 break;
299
300                         case 's':
301                                 dst_snapshot_name = optarg;
302                                 break;
303
304                         case 'V':
305                                 printf ("ardour-utils version %s\n\n", VERSIONSTRING);
306                                 printf ("Copyright (C) GPL 2016 Robin Gareus <robin@gareus.org>\n");
307                                 exit (0);
308                                 break;
309
310                         case 'v':
311                                 opt_verbose = true;
312                                 break;
313
314                         default:
315                                         usage (EXIT_FAILURE);
316                                         break;
317                 }
318         }
319
320         // TODO parse path/name  from a single argument.
321
322         if (optind + 2 > argc) {
323                 usage (EXIT_FAILURE);
324         }
325
326         std::string src = argv[optind];
327         std::string dst = argv[optind + 1];
328
329         // statefile_suffix
330
331         if (!ends_with (src, statefile_suffix)) {
332                 fprintf (stderr, "source is not a .ardour session file.\n");
333                 exit (1);
334         }
335         if (!ends_with (dst, statefile_suffix)) {
336                 fprintf (stderr, "target is not a .ardour session file.\n");
337                 exit (1);
338         }
339         if (!Glib::file_test (src, Glib::FILE_TEST_IS_REGULAR)) {
340                 fprintf (stderr, "source is not a regular file.\n");
341                 exit (1);
342         }
343         if (!Glib::file_test (dst, Glib::FILE_TEST_IS_REGULAR)) {
344                 fprintf (stderr, "target is not a regular file.\n");
345                 exit (1);
346         }
347
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);
352
353         // TODO check if src != dst ..
354         return copy_session_routes (
355                         src_path, src_name,
356                         dst_path, dst_name,
357                         dst_snapshot_name);
358 }