2 * Copyright (C) 2011-2016 Paul Davis <paul@linuxaudiosystems.com>
3 * Copyright (C) 2011 Carl Hetherington <carl@carlh.net>
4 * Copyright (C) 2013-2018 Robin Gareus <robin@gareus.org>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 #include <glibmm/fileutils.h>
22 #include <glibmm/miscutils.h>
24 #include "ardour/filesystem_paths.h"
25 #include "ardour/linux_vst_support.h"
26 #include "ardour/session.h"
27 #include "ardour/lxvst_plugin.h"
32 using namespace ARDOUR;
35 LXVSTPlugin::LXVSTPlugin (AudioEngine& e, Session& session, VSTHandle* h, int unique_id)
36 : VSTPlugin (e, session, h)
38 /* Instantiate the plugin and return a VSTState* */
40 Session::vst_current_loading_id = unique_id;
41 if ((_state = vstfx_instantiate (_handle, Session::vst_callback, this)) == 0) {
42 throw failed_constructor();
45 Session::vst_current_loading_id = 0;
50 LXVSTPlugin::LXVSTPlugin (const LXVSTPlugin &other)
53 _handle = other._handle;
55 Session::vst_current_loading_id = PBD::atoi(other.unique_id());
56 if ((_state = vstfx_instantiate (_handle, Session::vst_callback, this)) == 0) {
57 throw failed_constructor();
60 Session::vst_current_loading_id = 0;
62 XMLNode* root = new XMLNode (other.state_node_name ());
63 other.add_state (root);
64 set_state (*root, Stateful::loading_state_version);
70 LXVSTPlugin::~LXVSTPlugin ()
76 LXVSTPluginInfo::load (Session& session)
81 if (Config->get_use_lxvst()) {
84 handle = vstfx_load(path.c_str());
87 error << string_compose(_("LXVST: cannot load module from \"%1\""), path) << endmsg;
90 plugin.reset (new LXVSTPlugin (session.engine(), session, handle, PBD::atoi(unique_id)));
94 error << _("You asked ardour to not use any LXVST plugins") << endmsg;
95 return PluginPtr ((Plugin*) 0);
98 plugin->set_info(PluginInfoPtr(new LXVSTPluginInfo(*this)));
102 catch (failed_constructor &err) {
103 return PluginPtr ((Plugin*) 0);
107 std::vector<Plugin::PresetRecord>
108 LXVSTPluginInfo::get_presets (bool user_only) const
110 std::vector<Plugin::PresetRecord> p;
112 if (!Config->get_use_lxvst()) {
117 // TODO - cache, instantiating the plugin can be heavy
118 /* Built-in presets */
119 VSTHandle* handle = vstfx_load(path.c_str());
120 Session::vst_current_loading_id = atoi (unique_id);
121 AEffect* plugin = handle->main_entry (Session::vst_callback);
122 Session::vst_current_loading_id = 0;
125 plugin->dispatcher (plugin, effOpen, 0, 0, 0, 0); // :(
126 int const vst_version = plugin->dispatcher (plugin, effGetVstVersion, 0, 0, NULL, 0);
128 for (int i = 0; i < plugin->numPrograms; ++i) {
129 Plugin::PresetRecord r (string_compose (X_("VST:%1:%2"), unique_id, std::setw(4), std::setfill('0'), i), "", false);
130 if (vst_version >= 2) {
132 if (plugin->dispatcher (plugin, 29, i, 0, buf, 0) == 1) {
133 r.label = string_compose (_("%1 - %2"), i, buf);
135 r.label = string_compose (_("Preset %1"), i);
138 r.label = string_compose (_("Preset %1"), i);
143 plugin->dispatcher (plugin, effMainsChanged, 0, 0, 0, 0);
144 plugin->dispatcher (plugin, effClose, 0, 0, 0, 0); // :(
146 if (handle->plugincnt) {
149 vstfx_unload (handle);
153 XMLTree* t = new XMLTree;
154 std::string pf = Glib::build_filename (ARDOUR::user_config_directory (), "presets", string_compose ("vst-%1", unique_id));
155 if (Glib::file_test (pf, Glib::FILE_TEST_EXISTS)) {
156 t->set_filename (pf);
157 if (t->read ()) { // TODO read names only. skip parsing the actual data
158 XMLNode* root = t->root ();
159 for (XMLNodeList::const_iterator i = root->children().begin(); i != root->children().end(); ++i) {
160 XMLProperty const * uri = (*i)->property (X_("uri"));
161 XMLProperty const * label = (*i)->property (X_("label"));
162 p.push_back (Plugin::PresetRecord (uri->value(), label->value(), true));
171 LXVSTPluginInfo::LXVSTPluginInfo (_VSTInfo* nfo) : VSTPluginInfo (nfo)
173 type = ARDOUR::LXVST;