2 * Copyright (C) 2016 Robin Gareus <robin@gareus.org>
3 * Copyright (C) 2004 Paul Davis
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2, or (at your option)
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 #include <glibmm/fileutils.h>
21 #include <glibmm/miscutils.h>
23 #include "ardour/filesystem_paths.h"
24 #include "ardour/mac_vst_plugin.h"
25 #include "ardour/mac_vst_support.h"
26 #include "ardour/session.h"
31 using namespace ARDOUR;
34 MacVSTPlugin::MacVSTPlugin (AudioEngine& e, Session& session, VSTHandle* h, int unique_id)
35 : VSTPlugin (e, session, h)
37 /* Instantiate the plugin and return a VSTState* */
39 Session::vst_current_loading_id = unique_id;
40 if ((_state = mac_vst_instantiate (_handle, Session::vst_callback, this)) == 0) {
41 throw failed_constructor ();
43 Session::vst_current_loading_id = 0;
45 set_plugin (_state->plugin);
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 ();
57 Session::vst_current_loading_id = 0;
59 _plugin = _state->plugin;
61 XMLNode* root = new XMLNode (other.state_node_name ());
63 other.add_state (root);
64 set_state (*root, Stateful::loading_state_version);
68 MacVSTPlugin::~MacVSTPlugin ()
70 mac_vst_close (_state);
74 MacVSTPluginInfo::load (Session& session)
79 if (Config->get_use_macvst ()) {
82 handle = mac_vst_load (path.c_str ());
85 error << string_compose (_("MacVST: cannot load module from \"%1\""), path) << endmsg;
88 plugin.reset (new MacVSTPlugin (session.engine (), session, handle, PBD::atoi (unique_id)));
92 error << _("You asked ardour to not use any MacVST plugins") << endmsg;
93 return PluginPtr ((Plugin*) 0);
96 plugin->set_info (PluginInfoPtr (new MacVSTPluginInfo (*this)));
100 catch (failed_constructor &err) {
101 return PluginPtr ((Plugin*) 0);
105 std::vector<Plugin::PresetRecord>
106 MacVSTPluginInfo::get_presets (bool user_only) const
108 std::vector<Plugin::PresetRecord> p;
109 #ifndef NO_PLUGIN_STATE
110 if (!Config->get_use_macvst ()) {
115 // TODO - cache, instantiating the plugin can be heavy
116 /* Built-in presets */
117 VSTHandle* handle = mac_vst_load (path.c_str ());
118 Session::vst_current_loading_id = atoi (unique_id);
119 AEffect* plugin = handle->main_entry (Session::vst_callback);
120 Session::vst_current_loading_id = 0;
122 plugin->dispatcher (plugin, effOpen, 0, 0, 0, 0); // :(
123 int const vst_version = plugin->dispatcher (plugin, effGetVstVersion, 0, 0, NULL, 0);
125 for (int i = 0; i < plugin->numPrograms; ++i) {
126 Plugin::PresetRecord r (string_compose (X_("VST:%1:%2"), unique_id, i), "", false);
127 if (vst_version >= 2) {
129 if (plugin->dispatcher (plugin, 29, i, 0, buf, 0) == 1) {
132 r.label = string_compose (_("Preset %1"), i);
135 r.label = string_compose (_("Preset %1"), i);
140 plugin->dispatcher (plugin, effMainsChanged, 0, 0, 0, 0);
141 plugin->dispatcher (plugin, effClose, 0, 0, 0, 0); // :(
143 if (handle->plugincnt) {
146 mac_vst_unload (handle);
150 XMLTree* t = new XMLTree;
151 std::string pf = Glib::build_filename (ARDOUR::user_config_directory (), "presets", string_compose ("vst-%1", unique_id));
152 if (Glib::file_test (pf, Glib::FILE_TEST_EXISTS)) {
153 t->set_filename (pf);
154 if (t->read ()) { // TODO read names only. skip parsing the actual data
155 XMLNode* root = t->root ();
156 for (XMLNodeList::const_iterator i = root->children ().begin (); i != root->children ().end (); ++i) {
157 XMLProperty const * uri = (*i)->property (X_("uri"));
158 XMLProperty const * label = (*i)->property (X_("label"));
159 p.push_back (Plugin::PresetRecord (uri->value (), label->value (), true));
169 MacVSTPluginInfo::MacVSTPluginInfo ()
171 type = ARDOUR::MacVST;