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;
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");
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\
51 This tool creates a new Ardour session, optionally based on a\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\
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\
62 Note: this tool can only use static session templates.\n\
63 Interactive Lua init-scripts or dynamic templates are not supported.\n\
68 " UTILNAME " -s 44100 -m 4 /tmp/NewSession\n\
71 printf ("Report bugs to <http://tracker.ardour.org/>\n"
72 "Website: <http://ardour.org/>\n");
79 vector<TemplateInfo> templates;
80 find_session_templates (templates, false);
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";
90 template_path_from_name (std::string const& name)
92 vector<TemplateInfo> templates;
93 find_session_templates (templates, false);
95 for (vector<TemplateInfo>::iterator x = templates.begin (); x != templates.end (); ++x) {
96 if ((*x).name == name) {
104 create_new_session (string const& dir, string const& state, float sample_rate, int master_bus_chn, string const& template_path)
106 AudioEngine* engine = AudioEngine::create ();
108 if (!engine->set_backend ("None (Dummy)", "Unit-Test", "")) {
109 cerr << "Cannot create Audio/MIDI engine\n";
110 ::exit (EXIT_FAILURE);
113 engine->set_input_channels (256);
114 engine->set_output_channels (256);
116 if (engine->set_sample_rate (sample_rate)) {
117 cerr << "Cannot set session's samplerate.\n";
121 if (engine->start () != 0) {
122 cerr << "Cannot start Audio/MIDI engine\n";
126 string s = Glib::build_filename (dir, state + statefile_suffix);
128 if (Glib::file_test (dir, Glib::FILE_TEST_EXISTS)) {
129 cerr << "Session folder already exists '" << dir << "'\n";
131 if (Glib::file_test (s, Glib::FILE_TEST_EXISTS)) {
132 cerr << "Session file exists '" << s << "'\n";
136 BusProfile bus_profile;
137 BusProfile* bus_profile_ptr = NULL;
139 if (master_bus_chn > 0) {
140 bus_profile_ptr = &bus_profile;
141 bus_profile.master_out_channels = master_bus_chn;
144 if (!template_path.empty ()) {
145 bus_profile_ptr = NULL;
148 Session* session = new Session (*engine, dir, state, bus_profile_ptr, template_path);
149 engine->set_session (session);
154 main (int argc, char* argv[])
156 int sample_rate = 48000;
157 int master_bus_chn = 2;
158 string template_path;
160 const char* optstring = "Lm:hs:t:V";
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' },
171 /* clang-format on */
174 while (EOF != (c = getopt_long (argc, argv,
175 optstring, longopts, (int*)0))) {
182 const int mc = atoi (optarg);
183 if (mc >= 0 && mc < 128) {
186 cerr << "Invalid master bus channel count\n";
190 const int sr = atoi (optarg);
191 if (sr >= 8000 && sr <= 192000) {
194 cerr << "Invalid Samplerate\n";
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);
207 printf ("ardour-utils version %s\n\n", VERSIONSTRING);
208 printf ("Copyright (C) GPL 2019 Robin Gareus <robin@gareus.org>\n");
213 usage (EXIT_SUCCESS);
217 usage (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 usage (EXIT_FAILURE);
232 if (snapshot_name.empty ()) {
233 cerr << "Error: Invalid empty session/snapshot name.\n";
234 ::exit (EXIT_FAILURE);
239 SessionUtils::init ();
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";
247 cerr << "Error: unknown exception.\n";
250 /* save is implicit when creating a new session */
253 cout << "Created session in '" << s->path () << "'" << endl;
256 SessionUtils::unload_session (s);
257 SessionUtils::cleanup ();