Move panner bypass state up to the PannerShell so that it is preserved even when...
[ardour2.git] / libs / ardour / plugin.cc
blob90e9146af10882e3d9c828cb28c8623094632eaf
1 /*
2 Copyright (C) 2000-2002 Paul Davis
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
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 #ifdef WAF_BUILD
21 #include "libardour-config.h"
22 #endif
24 #include <vector>
25 #include <string>
27 #include <cstdlib>
28 #include <cstdio> // so libraptor doesn't complain
29 #include <cmath>
30 #include <dirent.h>
31 #include <sys/stat.h>
32 #include <cerrno>
33 #include <utility>
35 #include <lrdf.h>
37 #include "pbd/compose.h"
38 #include "pbd/error.h"
39 #include "pbd/xml++.h"
41 #include "ardour/ardour.h"
42 #include "ardour/session.h"
43 #include "ardour/audioengine.h"
44 #include "ardour/plugin.h"
45 #include "ardour/ladspa_plugin.h"
46 #include "ardour/plugin_manager.h"
48 #ifdef HAVE_AUDIOUNITS
49 #include "ardour/audio_unit.h"
50 #endif
52 #ifdef LV2_SUPPORT
53 #include "ardour/lv2_plugin.h"
54 #endif
56 #include "pbd/stl_delete.h"
58 #include "i18n.h"
59 #include <locale.h>
61 using namespace std;
62 using namespace ARDOUR;
63 using namespace PBD;
65 Plugin::Plugin (AudioEngine& e, Session& s)
66 : _engine (e)
67 , _session (s)
68 , _cycles (0)
69 , _have_presets (false)
70 , _have_pending_stop_events (false)
71 , _parameter_changed_since_last_preset (false)
75 Plugin::Plugin (const Plugin& other)
76 : StatefulDestructible()
77 , Latent()
78 , _engine (other._engine)
79 , _session (other._session)
80 , _info (other._info)
81 , _cycles (0)
82 , _have_presets (false)
83 , _have_pending_stop_events (false)
84 , _parameter_changed_since_last_preset (false)
89 Plugin::~Plugin ()
94 void
95 Plugin::remove_preset (string name)
97 do_remove_preset (name);
98 _presets.erase (preset_by_label (name)->uri);
100 _last_preset.uri = "";
101 _parameter_changed_since_last_preset = false;
102 PresetRemoved (); /* EMIT SIGNAL */
105 /** @return PresetRecord with empty URI on failure */
106 Plugin::PresetRecord
107 Plugin::save_preset (string name)
109 string const uri = do_save_preset (name);
111 if (!uri.empty()) {
112 _presets.insert (make_pair (uri, PresetRecord (uri, name)));
113 PresetAdded (); /* EMIT SIGNAL */
116 return PresetRecord (uri, name);
119 PluginPtr
120 ARDOUR::find_plugin(Session& session, string identifier, PluginType type)
122 PluginManager *mgr = PluginManager::the_manager();
123 PluginInfoList plugs;
125 switch (type) {
126 case ARDOUR::LADSPA:
127 plugs = mgr->ladspa_plugin_info();
128 break;
130 #ifdef LV2_SUPPORT
131 case ARDOUR::LV2:
132 plugs = mgr->lv2_plugin_info();
133 break;
134 #endif
136 #ifdef VST_SUPPORT
137 case ARDOUR::VST:
138 plugs = mgr->vst_plugin_info();
139 break;
140 #endif
142 #ifdef HAVE_AUDIOUNITS
143 case ARDOUR::AudioUnit:
144 plugs = mgr->au_plugin_info();
145 break;
146 #endif
148 default:
149 return PluginPtr ((Plugin *) 0);
152 PluginInfoList::iterator i;
154 for (i = plugs.begin(); i != plugs.end(); ++i) {
155 if (identifier == (*i)->unique_id){
156 return (*i)->load (session);
160 #ifdef VST_SUPPORT
161 /* hmm, we didn't find it. could be because in older versions of Ardour.
162 we used to store the name of a VST plugin, not its unique ID. so try
163 again.
166 for (i = plugs.begin(); i != plugs.end(); ++i) {
167 if (identifier == (*i)->name){
168 return (*i)->load (session);
171 #endif
173 return PluginPtr ((Plugin*) 0);
176 ChanCount
177 Plugin::output_streams () const
179 /* LADSPA & VST should not get here because they do not
180 return "infinite" i/o counts.
182 return ChanCount::ZERO;
185 ChanCount
186 Plugin::input_streams () const
188 /* LADSPA & VST should not get here because they do not
189 return "infinite" i/o counts.
191 return ChanCount::ZERO;
194 const Plugin::PresetRecord *
195 Plugin::preset_by_label (const string& label)
197 // FIXME: O(n)
198 for (map<string, PresetRecord>::const_iterator i = _presets.begin(); i != _presets.end(); ++i) {
199 if (i->second.label == label) {
200 return &i->second;
204 return 0;
207 const Plugin::PresetRecord *
208 Plugin::preset_by_uri (const string& uri)
210 map<string, PresetRecord>::const_iterator pr = _presets.find (uri);
211 if (pr != _presets.end()) {
212 return &pr->second;
213 } else {
214 return 0;
219 Plugin::connect_and_run (BufferSet& bufs,
220 ChanMapping in_map, ChanMapping out_map,
221 pframes_t nframes, framecnt_t offset)
223 if (bufs.count().n_midi() > 0) {
225 /* Track notes that we are sending to the plugin */
226 MidiBuffer& b = bufs.get_midi (0);
227 bool looped;
228 _tracker.track (b.begin(), b.end(), looped);
230 if (_have_pending_stop_events) {
231 /* Transmit note-offs that are pending from the last transport stop */
232 bufs.merge_from (_pending_stop_events, 0);
233 _have_pending_stop_events = false;
237 return 0;
240 void
241 Plugin::realtime_handle_transport_stopped ()
243 /* Create note-offs for any active notes and put them in _pending_stop_events, to be picked
244 up on the next call to connect_and_run ().
247 _pending_stop_events.ensure_buffers (DataType::MIDI, 1, 4096);
248 _pending_stop_events.get_midi(0).clear ();
249 _tracker.resolve_notes (_pending_stop_events.get_midi (0), 0);
250 _have_pending_stop_events = true;
253 vector<Plugin::PresetRecord>
254 Plugin::get_presets ()
256 if (!_have_presets) {
257 find_presets ();
258 _have_presets = true;
261 vector<PresetRecord> p;
262 for (map<string, PresetRecord>::const_iterator i = _presets.begin(); i != _presets.end(); ++i) {
263 p.push_back (i->second);
266 return p;
269 /** Set parameters using a preset */
270 bool
271 Plugin::load_preset (PresetRecord r)
273 _last_preset = r;
274 _parameter_changed_since_last_preset = false;
276 PresetLoaded (); /* EMIT SIGNAL */
277 return true;
280 /** @param val `plugin' value */
281 void
282 Plugin::set_parameter (uint32_t which, float val)
284 _parameter_changed_since_last_preset = true;
285 _session.set_dirty ();
286 ParameterChanged (which, val); /* EMIT SIGNAL */
290 Plugin::set_state (const XMLNode& node, int version)
292 XMLProperty const * p = node.property (X_("last-preset-uri"));
293 if (p) {
294 _last_preset.uri = p->value ();
297 p = node.property (X_("last-preset-label"));
298 if (p) {
299 _last_preset.label = p->value ();
302 p = node.property (X_("parameter-changed-since-last-preset"));
303 if (p) {
304 _parameter_changed_since_last_preset = string_is_affirmative (p->value ());
307 return 0;
310 XMLNode &
311 Plugin::get_state ()
313 XMLNode* root = new XMLNode (state_node_name ());
314 LocaleGuard lg (X_("POSIX"));
316 root->add_property (X_("last-preset-uri"), _last_preset.uri);
317 root->add_property (X_("last-preset-label"), _last_preset.label);
318 root->add_property (X_("parameter-changed-since-last-preset"), _parameter_changed_since_last_preset ? X_("yes") : X_("no"));
320 add_state (root);
321 return *root;