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