2 * Copyright (C) 2016-2018 Robin Gareus <robin@gareus.org>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 #include <glibmm/fileutils.h>
20 #include <glibmm/miscutils.h>
22 #include "ardour/filesystem_paths.h"
23 #include "ardour/mac_vst_plugin.h"
24 #include "ardour/mac_vst_support.h"
25 #include "ardour/session.h"
30 using namespace ARDOUR;
33 MacVSTPlugin::MacVSTPlugin (AudioEngine& e, Session& session, VSTHandle* h, int unique_id)
34 : VSTPlugin (e, session, h)
36 /* Instantiate the plugin and return a VSTState* */
38 Session::vst_current_loading_id = unique_id;
39 if ((_state = mac_vst_instantiate (_handle, Session::vst_callback, this)) == 0) {
40 throw failed_constructor ();
43 Session::vst_current_loading_id = 0;
48 MacVSTPlugin::MacVSTPlugin (const MacVSTPlugin &other)
51 _handle = other._handle;
53 Session::vst_current_loading_id = PBD::atoi (other.unique_id ());
54 if ((_state = mac_vst_instantiate (_handle, Session::vst_callback, this)) == 0) {
55 throw failed_constructor ();
58 Session::vst_current_loading_id = 0;
60 XMLNode* root = new XMLNode (other.state_node_name ());
61 other.add_state (root);
62 set_state (*root, Stateful::loading_state_version);
68 MacVSTPlugin::~MacVSTPlugin ()
70 mac_vst_close (_state);
74 MacVSTPlugin::open_plugin ()
76 VSTPlugin::open_plugin ();
77 _plugin->dispatcher (_plugin, effCanDo, 0, 0, const_cast<char*> ("hasCockosViewAsConfig"), 0.0f);
81 MacVSTPluginInfo::load (Session& session)
86 if (Config->get_use_macvst ()) {
89 handle = mac_vst_load (path.c_str ());
92 error << string_compose (_("MacVST: cannot load module from \"%1\""), path) << endmsg;
95 plugin.reset (new MacVSTPlugin (session.engine (), session, handle, PBD::atoi (unique_id)));
99 error << _("You asked ardour to not use any MacVST plugins") << endmsg;
100 return PluginPtr ((Plugin*) 0);
103 plugin->set_info (PluginInfoPtr (new MacVSTPluginInfo (*this)));
107 catch (failed_constructor &err) {
108 return PluginPtr ((Plugin*) 0);
112 std::vector<Plugin::PresetRecord>
113 MacVSTPluginInfo::get_presets (bool user_only) const
115 std::vector<Plugin::PresetRecord> p;
116 #ifndef NO_PLUGIN_STATE
117 if (!Config->get_use_macvst ()) {
122 // TODO - cache, instantiating the plugin can be heavy
123 /* Built-in presets */
124 VSTHandle* handle = mac_vst_load (path.c_str ());
125 Session::vst_current_loading_id = atoi (unique_id);
126 AEffect* plugin = handle->main_entry (Session::vst_callback);
127 Session::vst_current_loading_id = 0;
130 plugin->dispatcher (plugin, effOpen, 0, 0, 0, 0); // :(
131 int const vst_version = plugin->dispatcher (plugin, effGetVstVersion, 0, 0, NULL, 0);
133 for (int i = 0; i < plugin->numPrograms; ++i) {
134 Plugin::PresetRecord r (string_compose (X_("VST:%1:%2"), unique_id, std::setw(4), std::setfill('0'), i), "", false);
135 if (vst_version >= 2) {
137 if (plugin->dispatcher (plugin, 29, i, 0, buf, 0) == 1) {
140 r.label = string_compose (_("Preset %1"), i);
143 r.label = string_compose (_("Preset %1"), i);
148 plugin->dispatcher (plugin, effMainsChanged, 0, 0, 0, 0);
149 plugin->dispatcher (plugin, effClose, 0, 0, 0, 0); // :(
151 if (handle->plugincnt) {
154 mac_vst_unload (handle);
158 XMLTree* t = new XMLTree;
159 std::string pf = Glib::build_filename (ARDOUR::user_config_directory (), "presets", string_compose ("vst-%1", unique_id));
160 if (Glib::file_test (pf, Glib::FILE_TEST_EXISTS)) {
161 t->set_filename (pf);
162 if (t->read ()) { // TODO read names only. skip parsing the actual data
163 XMLNode* root = t->root ();
164 for (XMLNodeList::const_iterator i = root->children ().begin (); i != root->children ().end (); ++i) {
165 XMLProperty const * uri = (*i)->property (X_("uri"));
166 XMLProperty const * label = (*i)->property (X_("label"));
167 p.push_back (Plugin::PresetRecord (uri->value (), label->value (), true));
177 MacVSTPluginInfo::MacVSTPluginInfo (_VSTInfo* nfo) : VSTPluginInfo (nfo)
179 type = ARDOUR::MacVST;