7 #include "ardour/audioengine.h"
8 #include "ardour/filename_extensions.h"
9 #include "ardour/template_utils.h"
14 using namespace ARDOUR;
15 using namespace SessionUtils;
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");
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\
33 This tool creates a new Ardour session, optionally based on a\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\
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\
44 Note: this tool can only use static session templates.\n\
45 Interactive Lua init-scripts or dynamic templates are not supported.\n\
50 " UTILNAME " -s 44100 -m 4 /tmp/NewSession\n\
53 printf ("Report bugs to <http://tracker.ardour.org/>\n"
54 "Website: <http://ardour.org/>\n");
61 vector<TemplateInfo> templates;
62 find_session_templates (templates, false);
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";
72 template_path_from_name (std::string const& name)
74 vector<TemplateInfo> templates;
75 find_session_templates (templates, false);
77 for (vector<TemplateInfo>::iterator x = templates.begin (); x != templates.end (); ++x) {
78 if ((*x).name == name) {
86 create_new_session (string const& dir, string const& state, float sample_rate, int master_bus_chn, string const& template_path)
88 AudioEngine* engine = AudioEngine::create ();
90 if (!engine->set_backend ("None (Dummy)", "Unit-Test", "")) {
91 cerr << "Cannot create Audio/MIDI engine\n";
92 ::exit (EXIT_FAILURE);
95 engine->set_input_channels (256);
96 engine->set_output_channels (256);
98 if (engine->set_sample_rate (sample_rate)) {
99 cerr << "Cannot set session's samplerate.\n";
103 if (engine->start () != 0) {
104 cerr << "Cannot start Audio/MIDI engine\n";
108 string s = Glib::build_filename (dir, state + statefile_suffix);
110 if (Glib::file_test (dir, Glib::FILE_TEST_EXISTS)) {
111 cerr << "Session folder already exists '" << dir << "'\n";
113 if (Glib::file_test (s, Glib::FILE_TEST_EXISTS)) {
114 cerr << "Session file exists '" << s << "'\n";
118 BusProfile bus_profile;
119 BusProfile* bus_profile_ptr = NULL;
121 if (master_bus_chn > 0) {
122 bus_profile_ptr = &bus_profile;
123 bus_profile.master_out_channels = master_bus_chn;
126 if (!template_path.empty ()) {
127 bus_profile_ptr = NULL;
130 Session* session = new Session (*engine, dir, state, bus_profile_ptr, template_path);
131 engine->set_session (session);
136 main (int argc, char* argv[])
138 int sample_rate = 48000;
139 int master_bus_chn = 2;
140 string template_path;
142 const char* optstring = "Lm:hs:t:V";
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' },
153 /* clang-format on */
156 while (EOF != (c = getopt_long (argc, argv,
157 optstring, longopts, (int*)0))) {
164 const int mc = atoi (optarg);
165 if (mc >= 0 && mc < 128) {
168 cerr << "Invalid master bus channel count\n";
172 const int sr = atoi (optarg);
173 if (sr >= 8000 && sr <= 192000) {
176 cerr << "Invalid Samplerate\n";
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);
189 printf ("ardour-utils version %s\n\n", VERSIONSTRING);
190 printf ("Copyright (C) GPL 2019 Robin Gareus <robin@gareus.org>\n");
195 usage (EXIT_SUCCESS);
199 usage (EXIT_FAILURE);
204 string snapshot_name;
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]);
211 usage (EXIT_FAILURE);
214 if (snapshot_name.empty ()) {
215 cerr << "Error: Invalid empty session/snapshot name.\n";
216 ::exit (EXIT_FAILURE);
221 SessionUtils::init ();
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";
229 cerr << "Error: unknown exception.\n";
232 /* save is implicit when creating a new session */
235 cout << "Created session in '" << s->path () << "'" << endl;
238 SessionUtils::unload_session (s);
239 SessionUtils::cleanup ();