Add support for scripted meta-templates.
authorRobin Gareus <robin@gareus.org>
Fri, 11 Aug 2017 12:29:08 +0000 (14:29 +0200)
committerRobin Gareus <robin@gareus.org>
Fri, 11 Aug 2017 13:24:05 +0000 (15:24 +0200)
gtk2_ardour/ardour_ui.cc
gtk2_ardour/ardour_ui.h
gtk2_ardour/session_dialog.cc
gtk2_ardour/session_dialog.h
scripts/_template_example.lua

index f09e2a41abc7d11ada811164e7785ea5df567d69..0748d418c269a17aea0ad7253ea5c957815e21cb 100644 (file)
@@ -3612,7 +3612,12 @@ ARDOUR_UI::get_session_parameters (bool quit_on_cancel, bool should_be_new, stri
                        _session_is_new = true;
                }
 
-               if (likely_new && template_name.empty()) {
+               if (!template_name.empty() && template_name.substr (0, 11) == "urn:ardour:") {
+
+                       ret = build_session_from_dialog (session_dialog, session_path, session_name);
+                       meta_session_setup (template_name.substr (11));
+
+               } else if (likely_new && template_name.empty()) {
 
                        ret = build_session_from_dialog (session_dialog, session_path, session_name);
 
@@ -3828,23 +3833,7 @@ ARDOUR_UI::load_session (const std::string& path, const std::string& snap_name,
        if (!mix_template.empty ()) {
                /* if mix_template is given, assume this is a new session */
                string metascript = Glib::build_filename (mix_template, "template.lua");
-               if (Glib::file_test (metascript, Glib::FILE_TEST_EXISTS | Glib::FILE_TEST_IS_REGULAR)) {
-                       LuaState lua;
-                       lua_State* L = lua.getState();
-                       lua.Print.connect (&LuaInstance::_lua_print);
-                       LuaInstance::register_classes (L);
-                       LuaBindings::set_session (L, _session);
-                       luabridge::push <PublicEditor *> (L, &PublicEditor::instance());
-                       lua_setglobal (L, "Editor");
-                       lua.sandbox (true);
-                       lua.do_file (metascript);
-                       try {
-                               luabridge::LuaRef fn = luabridge::getGlobal (L, "template_load");
-                               if (fn.isFunction()) {
-                                       fn ();
-                               }
-                       } catch (luabridge::LuaException const& e) { }
-               }
+               meta_session_setup (metascript);
        }
 
 
@@ -3938,6 +3927,34 @@ ARDOUR_UI::build_session (const std::string& path, const std::string& snap_name,
        return 0;
 }
 
+void
+ARDOUR_UI::meta_session_setup (const std::string& script_path)
+{
+       if (!Glib::file_test (script_path, Glib::FILE_TEST_EXISTS | Glib::FILE_TEST_IS_REGULAR)) {
+               return;
+       }
+
+       LuaState lua;
+       lua.Print.connect (&LuaInstance::_lua_print);
+       lua.sandbox (true);
+
+       lua_State* L = lua.getState();
+       LuaInstance::register_classes (L);
+       LuaBindings::set_session (L, _session);
+       luabridge::push <PublicEditor *> (L, &PublicEditor::instance());
+       lua_setglobal (L, "Editor");
+
+       lua.do_command ("function ardour () end");
+       lua.do_file (script_path);
+
+       try {
+               luabridge::LuaRef fn = luabridge::getGlobal (L, "session_setup");
+               if (fn.isFunction()) {
+                       fn ();
+               }
+       } catch (luabridge::LuaException const& e) { }
+}
+
 void
 ARDOUR_UI::launch_chat ()
 {
index 42a4f20c79865c06b7f194afc1d535b58b58d87e..e85135bf40331f2df35aa6e2765b213c2335a5d1 100644 (file)
@@ -656,6 +656,8 @@ private:
        void save_template ();
        void manage_templates ();
 
+       void meta_session_setup (const std::string& script_path);
+
        void edit_metadata ();
        void import_metadata ();
 
index 9594c14ff9261fe0b7e984bf387667d8ca3d8018..08f7b15c165e7ebdd3274927ee9998f8b594a88a 100644 (file)
@@ -48,6 +48,7 @@
 
 #include "ardour/audioengine.h"
 #include "ardour/filesystem_paths.h"
+#include "ardour/luascripting.h"
 #include "ardour/recent_sessions.h"
 #include "ardour/session.h"
 #include "ardour/session_state_utils.h"
@@ -516,6 +517,16 @@ SessionDialog::populate_session_templates ()
 
                row[session_template_columns.name] = (*x).name;
                row[session_template_columns.path] = (*x).path;
+               row[session_template_columns.desc] = (*x).description;
+       }
+
+       LuaScriptList& ms (LuaScripting::instance ().scripts (LuaScriptInfo::SessionSetup));
+       for (LuaScriptList::const_iterator s = ms.begin(); s != ms.end(); ++s) {
+               TreeModel::Row row;
+               row = *(template_model->append ());
+               row[session_template_columns.name] = "Meta: " + (*s)->name;
+               row[session_template_columns.path] = "urn:ardour:" + (*s)->path;
+               row[session_template_columns.desc] = "urn:ardour:" + (*s)->description;
        }
 
        if (!templates.empty()) {
index d27666e62b38780ac60f7b80336b63adac8c13ea..5f4062fc1bdd3a5badd783ec6e7472b761e8371f 100644 (file)
@@ -165,10 +165,12 @@ private:
                SessionTemplateColumns () {
                        add (name);
                        add (path);
+                       add (desc);
                }
 
                Gtk::TreeModelColumn<std::string> name;
                Gtk::TreeModelColumn<std::string> path;
+               Gtk::TreeModelColumn<std::string> desc;
        };
 
        SessionTemplateColumns session_template_columns;
index e219cb209e00113fdac8725290798eee86d088cc..bf895dccf24a599238cd72864fc0e9cbf40a32bc 100644 (file)
@@ -1,14 +1,27 @@
---
--- Session Template setup-hook
+ardour {
+       ["type"]    = "SessionSetup",
+       name        = "Recording Session",
+       description = [[Add as many mono tracks to the new session as there are physical audio inputs and optionally record-arm them.]]
+}
+
+---- For use with templates: Session Template setup-hook
 --
 -- If a script named 'template.lua' exists in a session-template folder
--- the `template_load` function of the script is called after
+-- the `session_setup` function of the script is called after
 -- creating the session from the template.
 --
--- (e.g. ~/.config/ardour5/templates/Template-Name/template.lua
+-- (e.g. ~/.config/ardour5/templates/Template-Name/template.lua)
+--
+--
+---- For use as meta-session
+--
+-- Every Lua script in the script-folder of type "SessionSetup"
+-- is listed as implicit template in the new-session dialog.
+-- The scripts 'session_setup' function  is called once after
+-- creating a new, empty session.
 --
 
-function template_load ()
+function session_setup ()
        local e = Session:engine()
        -- from the engine's POV readable/capture ports are "outputs"
        local _, t = e:get_backend_ports ("", ARDOUR.DataType("audio"), ARDOUR.PortFlags.IsOutput | ARDOUR.PortFlags.IsPhysical, C.StringVector())
@@ -17,7 +30,7 @@ function template_load ()
 
        local dialog_options = {
                { type = "heading", title = "Customize Session: " .. Session:name () },
-               { type = "number", key = "tracks", title = "Create Tracks",  min = 0, max = 128, step = 1, digits = 0, default = tracks },
+               { type = "number", key = "tracks", title = "Create Tracks",  min = 1, max = 128, step = 1, digits = 0, default = tracks },
                { type = "checkbox", key = "recarm", default = false, title = "Record Arm Tracks" },
        }