2 * Copyright (C) 2019 Robin Gareus <robin@gareus.org>
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.
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.
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.
25 #include "ardour/audioengine.h"
26 #include "ardour/filename_extensions.h"
27 #include "ardour/template_utils.h"
32 using namespace ARDOUR;
33 using namespace SessionUtils;
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");
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\
50 This tool creates a new Ardour session, optionally based on a\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\
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\
61 Note: this tool can only use static session templates.\n\
62 Interactive Lua init-scripts or dynamic templates are not supported.\n\
67 " UTILNAME " -s 44100 -m 4 /tmp/NewSession\n\
70 printf ("Report bugs to <http://tracker.ardour.org/>\n"
71 "Website: <http://ardour.org/>\n");
72 ::exit (EXIT_SUCCESS);
78 vector<TemplateInfo> templates;
79 find_session_templates (templates, false);
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";
89 template_path_from_name (std::string const& name)
91 vector<TemplateInfo> templates;
92 find_session_templates (templates, false);
94 for (vector<TemplateInfo>::iterator x = templates.begin (); x != templates.end (); ++x) {
95 if ((*x).name == name) {
103 create_new_session (string const& dir, string const& state, float sample_rate, int master_bus_chn, string const& template_path)
105 AudioEngine* engine = AudioEngine::create ();
107 if (!engine->set_backend ("None (Dummy)", "Unit-Test", "")) {
108 cerr << "Cannot create Audio/MIDI engine\n";
109 ::exit (EXIT_FAILURE);
112 engine->set_input_channels (256);
113 engine->set_output_channels (256);
115 if (engine->set_sample_rate (sample_rate)) {
116 cerr << "Cannot set session's samplerate.\n";
120 if (engine->start () != 0) {
121 cerr << "Cannot start Audio/MIDI engine\n";
125 string s = Glib::build_filename (dir, state + statefile_suffix);
127 if (Glib::file_test (dir, Glib::FILE_TEST_EXISTS)) {
128 cerr << "Session folder already exists '" << dir << "'\n";
130 if (Glib::file_test (s, Glib::FILE_TEST_EXISTS)) {
131 cerr << "Session file exists '" << s << "'\n";
135 BusProfile bus_profile;
136 BusProfile* bus_profile_ptr = NULL;
138 if (master_bus_chn > 0) {
139 bus_profile_ptr = &bus_profile;
140 bus_profile.master_out_channels = master_bus_chn;
143 if (!template_path.empty ()) {
144 bus_profile_ptr = NULL;
147 Session* session = new Session (*engine, dir, state, bus_profile_ptr, template_path);
148 engine->set_session (session);
153 main (int argc, char* argv[])
155 int sample_rate = 48000;
156 int master_bus_chn = 2;
157 string template_path;
159 const char* optstring = "Lm:hs:t:V";
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' },
170 /* clang-format on */
173 while (EOF != (c = getopt_long (argc, argv,
174 optstring, longopts, (int*)0))) {
181 const int mc = atoi (optarg);
182 if (mc >= 0 && mc < 128) {
185 cerr << "Invalid master bus channel count\n";
189 const int sr = atoi (optarg);
190 if (sr >= 8000 && sr <= 192000) {
193 cerr << "Invalid Samplerate\n";
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);
206 printf ("ardour-utils version %s\n\n", VERSIONSTRING);
207 printf ("Copyright (C) GPL 2019 Robin Gareus <robin@gareus.org>\n");
216 cerr << "Error: unrecognized option. See --help for usage information.\n";
217 ::exit (EXIT_FAILURE);
222 string snapshot_name;
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]);
229 cerr << "Error: Missing parameter. See --help for usage information.\n";
230 ::exit (EXIT_FAILURE);
233 if (snapshot_name.empty ()) {
234 cerr << "Error: Invalid empty session/snapshot name.\n";
235 ::exit (EXIT_FAILURE);
240 SessionUtils::init ();
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";
248 cerr << "Error: unknown exception.\n";
251 /* save is implicit when creating a new session */
254 cout << "Created session in '" << s->path () << "'" << endl;
257 SessionUtils::unload_session (s);
258 SessionUtils::cleanup ();