Add session-util to create new session
[ardour.git] / session_utils / new_session.cc
1 #include <cstdlib>
2 #include <getopt.h>
3 #include <iostream>
4
5 #include <glibmm.h>
6
7 #include "ardour/audioengine.h"
8 #include "ardour/filename_extensions.h"
9 #include "ardour/template_utils.h"
10
11 #include "common.h"
12
13 using namespace std;
14 using namespace ARDOUR;
15 using namespace SessionUtils;
16
17 static void
18 usage (int status)
19 {
20         // help2man compatible format (standard GNU help-text)
21         printf (UTILNAME " - create a new session from the commandline.\n\n");
22         printf ("Usage: " UTILNAME " [ OPTIONS ] <session-dir> [session-name]\n\n");
23         printf ("Options:\n\
24   -L, --list-templates          List available templates and exit\n\
25   -h, --help                    Display this help and exit\n\
26   -m, --master-channels <chn>   Master-bus channel count (default 2)\n\
27   -s, --samplerate <rate>       Samplerate to use (default 48000)\n\
28   -t, --template <template>     Use given template for new session\n\
29   -V, --version                 Print version information and exit\n\
30 \n");
31
32         printf ("\n\
33 This tool creates a new Ardour session, optionally based on a\n\
34 session-template.\n\
35 \n\
36 If the session-name is unspecified, the sesion-dir-name is used.\n\
37 If specified, the tool expects a session-name without .ardour\n\
38 file-name extension.\n\
39 \n\
40 If no template is specified, an empty session with a stereo master\n\
41 bus is created. The -m option allows to specify the master-bus channel\n\
42 count. If zero is used as channel count, no master-bus is created.\n\
43 \n\
44 Note: this tool can only use static session templates.\n\
45 Interactive Lua init-scripts or dynamic templates are not supported.\n\
46 \n");
47
48         printf ("\n\
49 Examples:\n\
50 " UTILNAME " -s 44100 -m 4 /tmp/NewSession\n\
51 \n");
52
53         printf ("Report bugs to <http://tracker.ardour.org/>\n"
54                 "Website: <http://ardour.org/>\n");
55         ::exit (status);
56 }
57
58 static void
59 list_templates ()
60 {
61         vector<TemplateInfo> templates;
62         find_session_templates (templates, false);
63
64         cout << "---- List of Session Templates ----\n";
65         for (vector<TemplateInfo>::iterator x = templates.begin (); x != templates.end (); ++x) {
66                 cout << "[TPL] " << (*x).name << "\n";
67         }
68         cout << "----\n";
69 }
70
71 static std::string
72 template_path_from_name (std::string const& name)
73 {
74         vector<TemplateInfo> templates;
75         find_session_templates (templates, false);
76
77         for (vector<TemplateInfo>::iterator x = templates.begin (); x != templates.end (); ++x) {
78                 if ((*x).name == name) {
79                         return (*x).path;
80                 }
81         }
82         return "";
83 }
84
85 static Session*
86 create_new_session (string const& dir, string const& state, float sample_rate, int master_bus_chn, string const& template_path)
87 {
88         AudioEngine* engine = AudioEngine::create ();
89
90         if (!engine->set_backend ("None (Dummy)", "Unit-Test", "")) {
91                 cerr << "Cannot create Audio/MIDI engine\n";
92                 ::exit (EXIT_FAILURE);
93         }
94
95         engine->set_input_channels (256);
96         engine->set_output_channels (256);
97
98         if (engine->set_sample_rate (sample_rate)) {
99                 cerr << "Cannot set session's samplerate.\n";
100                 return 0;
101         }
102
103         if (engine->start () != 0) {
104                 cerr << "Cannot start Audio/MIDI engine\n";
105                 return 0;
106         }
107
108         string s = Glib::build_filename (dir, state + statefile_suffix);
109
110         if (Glib::file_test (dir, Glib::FILE_TEST_EXISTS)) {
111                 cerr << "Session folder already exists '" << dir << "'\n";
112         }
113         if (Glib::file_test (s, Glib::FILE_TEST_EXISTS)) {
114                 cerr << "Session file exists '" << s << "'\n";
115                 return 0;
116         }
117
118         BusProfile  bus_profile;
119         BusProfile* bus_profile_ptr = NULL;
120
121         if (master_bus_chn > 0) {
122                 bus_profile_ptr = &bus_profile;
123                 bus_profile.master_out_channels = master_bus_chn;
124         }
125
126         if (!template_path.empty ()) {
127                 bus_profile_ptr = NULL;
128         }
129
130         Session* session = new Session (*engine, dir, state, bus_profile_ptr, template_path);
131         engine->set_session (session);
132         return session;
133 }
134
135 int
136 main (int argc, char* argv[])
137 {
138         int    sample_rate    = 48000;
139         int    master_bus_chn = 2;
140         string template_path;
141
142         const char* optstring = "Lm:hs:t:V";
143
144         /* clang-format off */
145         const struct option longopts[] = {
146                 { "list-templates",  no_argument,       0, 'L' },
147                 { "help",            no_argument,       0, 'h' },
148                 { "master-channels", no_argument,       0, 'm' },
149                 { "samplerate",      required_argument, 0, 's' },
150                 { "template",        required_argument, 0, 't' },
151                 { "version",         no_argument,       0, 'V' },
152         };
153         /* clang-format on */
154
155         int c = 0;
156         while (EOF != (c = getopt_long (argc, argv,
157                                         optstring, longopts, (int*)0))) {
158                 switch (c) {
159                         case 'L':
160                                 list_templates ();
161                                 exit (EXIT_SUCCESS);
162                                 break;
163                         case 'm': {
164                                 const int mc = atoi (optarg);
165                                 if (mc >= 0 && mc < 128) {
166                                         master_bus_chn = mc;
167                                 } else {
168                                         cerr << "Invalid master bus channel count\n";
169                                 }
170                         } break;
171                         case 's': {
172                                 const int sr = atoi (optarg);
173                                 if (sr >= 8000 && sr <= 192000) {
174                                         sample_rate = sr;
175                                 } else {
176                                         cerr << "Invalid Samplerate\n";
177                                 }
178                         } break;
179
180                         case 't':
181                                 template_path = template_path_from_name (optarg);
182                                 if (template_path.empty ()) {
183                                         cerr << "Invalid (non-existent) template:" << optarg << "\n";
184                                         ::exit (EXIT_FAILURE);
185                                 }
186                                 break;
187
188                         case 'V':
189                                 printf ("ardour-utils version %s\n\n", VERSIONSTRING);
190                                 printf ("Copyright (C) GPL 2019 Robin Gareus <robin@gareus.org>\n");
191                                 exit (EXIT_SUCCESS);
192                                 break;
193
194                         case 'h':
195                                 usage (EXIT_SUCCESS);
196                                 break;
197
198                         default:
199                                 usage (EXIT_FAILURE);
200                                 break;
201                 }
202         }
203
204         string snapshot_name;
205
206         if (optind + 2 == argc) {
207                 snapshot_name = argv[optind + 1];
208         } else if (optind + 1 == argc) {
209                 snapshot_name = Glib::path_get_basename (argv[optind]);
210         } else {
211                 usage (EXIT_FAILURE);
212         }
213
214         if (snapshot_name.empty ()) {
215                 cerr << "Error: Invalid empty session/snapshot name.\n";
216                 ::exit (EXIT_FAILURE);
217         }
218
219         /* all systems go */
220
221         SessionUtils::init ();
222         Session* s = 0;
223
224         try {
225                 s = create_new_session (argv[optind], snapshot_name, sample_rate, master_bus_chn, template_path);
226         } catch (ARDOUR::SessionException& e) {
227                 cerr << "Error: " << e.what () << "\n";
228         } catch (...) {
229                 cerr << "Error: unknown exception.\n";
230         }
231
232         /* save is implicit when creating a new session */
233
234         if (s) {
235                 cout << "Created session in '" << s->path () << "'" << endl;
236         }
237
238         SessionUtils::unload_session (s);
239         SessionUtils::cleanup ();
240
241         return 0;
242 }