From 476952f2b60f595c2d7378af1b3db4eb41e2c13c Mon Sep 17 00:00:00 2001 From: Robin Gareus Date: Wed, 27 Sep 2017 18:22:49 +0200 Subject: Add session-util to create a new empty session --- session_utils/common.cc | 40 ++++++++++++++++ session_utils/common.h | 9 +++- session_utils/new_empty_session.cc | 97 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 145 insertions(+), 1 deletion(-) create mode 100644 session_utils/new_empty_session.cc (limited to 'session_utils') diff --git a/session_utils/common.cc b/session_utils/common.cc index eb17cfdbeb..86e945f616 100644 --- a/session_utils/common.cc +++ b/session_utils/common.cc @@ -176,6 +176,46 @@ SessionUtils::load_session (string dir, string state, bool exit_at_failure) return s; } +Session * +SessionUtils::create_session (string dir, string state, float sample_rate) +{ + AudioEngine* engine = AudioEngine::create (); + + if (!engine->set_backend ("None (Dummy)", "Unit-Test", "")) { + std::cerr << "Cannot create Audio/MIDI engine\n"; + ::exit (EXIT_FAILURE); + } + + engine->set_input_channels (256); + engine->set_output_channels (256); + + if (engine->set_sample_rate (sample_rate)) { + std::cerr << "Cannot set session's samplerate.\n"; + return 0; + } + + init_post_engine (); + + if (engine->start () != 0) { + std::cerr << "Cannot start Audio/MIDI engine\n"; + return 0; + } + + std::string s = Glib::build_filename (dir, state + statefile_suffix); + + if (Glib::file_test (dir, Glib::FILE_TEST_EXISTS)) { + std::cerr << "Session folder already exists '"<< dir << "'\n"; + } + if (Glib::file_test (s, Glib::FILE_TEST_EXISTS)) { + std::cerr << "Session file exists '"<< s << "'\n"; + return 0; + } + + Session* session = new Session (*engine, dir, state); + engine->set_session (session); + return session; +} + void SessionUtils::unload_session (Session *s) { diff --git a/session_utils/common.h b/session_utils/common.h index fcfd043415..679e0b5333 100644 --- a/session_utils/common.h +++ b/session_utils/common.h @@ -25,8 +25,15 @@ namespace SessionUtils { /** @param dir Session directory. * @param state Session state file, without .ardour suffix. + * @returns an ardour session object (free with \ref unload_session) or NULL */ - ARDOUR::Session * load_session (std::string dir, std::string state, bool exit_at_failure = true); + ARDOUR::Session* load_session (std::string dir, std::string state, bool exit_at_failure = true); + + /** @param dir Session directory. + * @param state Session state file, without .ardour suffix. + * @returns an ardour session object (free with \ref unload_session) or NULL on error + */ + ARDOUR::Session* create_session (std::string dir, std::string state, float sample_rate); /** close session and stop engine * @param s Session to close (may me NULL) diff --git a/session_utils/new_empty_session.cc b/session_utils/new_empty_session.cc new file mode 100644 index 0000000000..21eb6879a2 --- /dev/null +++ b/session_utils/new_empty_session.cc @@ -0,0 +1,97 @@ +#include +#include +#include + +#include "common.h" + +using namespace std; +using namespace ARDOUR; +using namespace SessionUtils; + + +static void usage (int status) +{ + // help2man compatible format (standard GNU help-text) + printf (UTILNAME " - create a new empty session from the commandline.\n\n"); + printf ("Usage: " UTILNAME " [ OPTIONS ] \n\n"); + printf ("Options:\n\ + -h, --help display this help and exit\n\ + -s, --samplerate samplerate to use (default 48000)\n\ + -V, --version print version information and exit\n\ +\n"); + printf ("\n\ +This tool creates a new empty Ardour session.\n\ +\n\ +Note: the tool expects a session-name without .ardour file-name extension.\n\ +\n\ +Examples:\n\ +"UTILNAME " -s 44100 /tmp/TestSession TestSession\n\ +\n"); + + printf ("Report bugs to \n" + "Website: \n"); + ::exit (status); +} + +int main (int argc, char* argv[]) +{ + int sample_rate = 48000; + + const char *optstring = "hs:V"; + + const struct option longopts[] = { + { "help", 0, 0, 'h' }, + { "samplerate", 1, 0, 's' }, + { "version", 0, 0, 'V' }, + }; + + int c = 0; + while (EOF != (c = getopt_long (argc, argv, + optstring, longopts, (int *) 0))) { + switch (c) { + case 's': + { + const int sr = atoi (optarg); + if (sr >= 8000 && sr <= 192000) { + sample_rate = sr; + } else { + fprintf(stderr, "Invalid Samplerate\n"); + } + } + break; + + case 'V': + printf ("ardour-utils version %s\n\n", VERSIONSTRING); + printf ("Copyright (C) GPL 2017 Robin Gareus \n"); + exit (0); + break; + + case 'h': + usage (0); + break; + + default: + usage (EXIT_FAILURE); + break; + } + } + + // XXX perhaps allow to infer the session-name from the dir-name + if (optind + 2 > argc) { + usage (EXIT_FAILURE); + } + + /* all systems go */ + + SessionUtils::init(); + Session* s = 0; + + s = SessionUtils::create_session (argv[optind], argv[optind+1], sample_rate); + + /* save is implicit when creating a new session */ + + SessionUtils::unload_session(s); + SessionUtils::cleanup(); + + return 0; +} -- cgit v1.2.3