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