Remove ambiguous API implementation
[ardour.git] / libs / ardour / lxvst_plugin.cc
1 /*
2     Copyright (C) 2004 Paul Davis
3
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.
8
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.
13
14     You should have received a copy of the GNU General Public License
15     along with this program; if not, write to the Free Software
16     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17
18 */
19
20 #include <glibmm/fileutils.h>
21 #include <glibmm/miscutils.h>
22
23 #include "ardour/filesystem_paths.h"
24 #include "ardour/linux_vst_support.h"
25 #include "ardour/session.h"
26 #include "ardour/lxvst_plugin.h"
27
28 #include "pbd/i18n.h"
29
30 using namespace std;
31 using namespace ARDOUR;
32 using namespace PBD;
33
34 LXVSTPlugin::LXVSTPlugin (AudioEngine& e, Session& session, VSTHandle* h, int unique_id)
35         : VSTPlugin (e, session, h)
36 {
37         /* Instantiate the plugin and return a VSTState* */
38
39         Session::vst_current_loading_id = unique_id;
40         if ((_state = vstfx_instantiate (_handle, Session::vst_callback, this)) == 0) {
41                 throw failed_constructor();
42         }
43         open_plugin ();
44         Session::vst_current_loading_id = 0;
45
46         init_plugin ();
47 }
48
49 LXVSTPlugin::LXVSTPlugin (const LXVSTPlugin &other)
50         : VSTPlugin (other)
51 {
52         _handle = other._handle;
53
54         Session::vst_current_loading_id = PBD::atoi(other.unique_id());
55         if ((_state = vstfx_instantiate (_handle, Session::vst_callback, this)) == 0) {
56                 throw failed_constructor();
57         }
58         open_plugin ();
59         Session::vst_current_loading_id = 0;
60
61         XMLNode* root = new XMLNode (other.state_node_name ());
62         other.add_state (root);
63         set_state (*root, Stateful::loading_state_version);
64         delete root;
65
66         init_plugin ();
67 }
68
69 LXVSTPlugin::~LXVSTPlugin ()
70 {
71         vstfx_close (_state);
72 }
73
74 PluginPtr
75 LXVSTPluginInfo::load (Session& session)
76 {
77         try {
78                 PluginPtr plugin;
79
80                 if (Config->get_use_lxvst()) {
81                         VSTHandle* handle;
82
83                         handle = vstfx_load(path.c_str());
84
85                         if (handle == NULL) {
86                                 error << string_compose(_("LXVST: cannot load module from \"%1\""), path) << endmsg;
87                         }
88                         else {
89                                 plugin.reset (new LXVSTPlugin (session.engine(), session, handle, PBD::atoi(unique_id)));
90                         }
91                 }
92                 else {
93                         error << _("You asked ardour to not use any LXVST plugins") << endmsg;
94                         return PluginPtr ((Plugin*) 0);
95                 }
96
97                 plugin->set_info(PluginInfoPtr(new LXVSTPluginInfo(*this)));
98                 return plugin;
99         }
100
101         catch (failed_constructor &err) {
102                 return PluginPtr ((Plugin*) 0);
103         }
104 }
105
106 std::vector<Plugin::PresetRecord>
107 LXVSTPluginInfo::get_presets (bool user_only) const
108 {
109         std::vector<Plugin::PresetRecord> p;
110 #ifndef NO_PLUGIN_STATE
111         if (!Config->get_use_lxvst()) {
112                 return p;
113         }
114
115         if (!user_only) {
116                 // TODO - cache, instantiating the plugin can be heavy
117                 /* Built-in presets */
118                 VSTHandle* handle = vstfx_load(path.c_str());
119                 Session::vst_current_loading_id = atoi (unique_id);
120                 AEffect* plugin = handle->main_entry (Session::vst_callback);
121                 Session::vst_current_loading_id = 0;
122                 plugin->ptr1 = NULL;
123
124                 plugin->dispatcher (plugin, effOpen, 0, 0, 0, 0); // :(
125                 int const vst_version = plugin->dispatcher (plugin, effGetVstVersion, 0, 0, NULL, 0);
126
127                 for (int i = 0; i < plugin->numPrograms; ++i) {
128                         Plugin::PresetRecord r (string_compose (X_("VST:%1:%2"), unique_id, std::setw(4), std::setfill('0'), i), "", false);
129                         if (vst_version >= 2) {
130                                 char buf[256];
131                                 if (plugin->dispatcher (plugin, 29, i, 0, buf, 0) == 1) {
132                                         r.label = string_compose (_("%1 - %2"), i, buf);
133                                 } else {
134                                         r.label = string_compose (_("Preset %1"), i);
135                                 }
136                         } else {
137                                 r.label = string_compose (_("Preset %1"), i);
138                         }
139                         p.push_back (r);
140                 }
141
142                 plugin->dispatcher (plugin, effMainsChanged, 0, 0, 0, 0);
143                 plugin->dispatcher (plugin, effClose, 0, 0, 0, 0); // :(
144
145                 if (handle->plugincnt) {
146                         handle->plugincnt--;
147                 }
148                 vstfx_unload (handle);
149         }
150
151         /* user presets */
152         XMLTree* t = new XMLTree;
153         std::string pf = Glib::build_filename (ARDOUR::user_config_directory (), "presets", string_compose ("vst-%1", unique_id));
154         if (Glib::file_test (pf, Glib::FILE_TEST_EXISTS)) {
155                 t->set_filename (pf);
156                 if (t->read ()) { // TODO read names only. skip parsing the actual data
157                         XMLNode* root = t->root ();
158                         for (XMLNodeList::const_iterator i = root->children().begin(); i != root->children().end(); ++i) {
159                                 XMLProperty const * uri = (*i)->property (X_("uri"));
160                                 XMLProperty const * label = (*i)->property (X_("label"));
161                                 p.push_back (Plugin::PresetRecord (uri->value(), label->value(), true));
162                         }
163                 }
164         }
165         delete t;
166 #endif
167
168         return p;
169 }
170
171 LXVSTPluginInfo::LXVSTPluginInfo (_VSTInfo* nfo) : VSTPluginInfo (nfo)
172 {
173         type = ARDOUR::LXVST;
174 }
175