Add support for scripted meta-templates.

This commit is contained in:
Robin Gareus 2017-08-11 14:29:08 +02:00
parent 62ce5465ca
commit f27e53f485
5 changed files with 69 additions and 24 deletions

View file

@ -3612,7 +3612,12 @@ ARDOUR_UI::get_session_parameters (bool quit_on_cancel, bool should_be_new, stri
_session_is_new = true; _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); 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.empty ()) {
/* if mix_template is given, assume this is a new session */ /* if mix_template is given, assume this is a new session */
string metascript = Glib::build_filename (mix_template, "template.lua"); string metascript = Glib::build_filename (mix_template, "template.lua");
if (Glib::file_test (metascript, Glib::FILE_TEST_EXISTS | Glib::FILE_TEST_IS_REGULAR)) { meta_session_setup (metascript);
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) { }
}
} }
@ -3938,6 +3927,34 @@ ARDOUR_UI::build_session (const std::string& path, const std::string& snap_name,
return 0; 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 void
ARDOUR_UI::launch_chat () ARDOUR_UI::launch_chat ()
{ {

View file

@ -656,6 +656,8 @@ private:
void save_template (); void save_template ();
void manage_templates (); void manage_templates ();
void meta_session_setup (const std::string& script_path);
void edit_metadata (); void edit_metadata ();
void import_metadata (); void import_metadata ();

View file

@ -48,6 +48,7 @@
#include "ardour/audioengine.h" #include "ardour/audioengine.h"
#include "ardour/filesystem_paths.h" #include "ardour/filesystem_paths.h"
#include "ardour/luascripting.h"
#include "ardour/recent_sessions.h" #include "ardour/recent_sessions.h"
#include "ardour/session.h" #include "ardour/session.h"
#include "ardour/session_state_utils.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.name] = (*x).name;
row[session_template_columns.path] = (*x).path; 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()) { if (!templates.empty()) {

View file

@ -165,10 +165,12 @@ private:
SessionTemplateColumns () { SessionTemplateColumns () {
add (name); add (name);
add (path); add (path);
add (desc);
} }
Gtk::TreeModelColumn<std::string> name; Gtk::TreeModelColumn<std::string> name;
Gtk::TreeModelColumn<std::string> path; Gtk::TreeModelColumn<std::string> path;
Gtk::TreeModelColumn<std::string> desc;
}; };
SessionTemplateColumns session_template_columns; SessionTemplateColumns session_template_columns;

View file

@ -1,14 +1,27 @@
-- ardour {
-- Session Template setup-hook ["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 -- 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. -- 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() local e = Session:engine()
-- from the engine's POV readable/capture ports are "outputs" -- 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()) 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 = { local dialog_options = {
{ type = "heading", title = "Customize Session: " .. Session:name () }, { 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" }, { type = "checkbox", key = "recarm", default = false, title = "Record Arm Tracks" },
} }