fix copy/paste issue, typename is not needed here
[ardour.git] / libs / ardour / vst_plugin.cc
1 /*
2     Copyright (C) 2010 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 <glib.h>
21 #include "pbd/gstdio_compat.h"
22
23 #include <glibmm/fileutils.h>
24 #include <glibmm/miscutils.h>
25 #include <glibmm/convert.h>
26
27 #include "pbd/floating.h"
28 #include "pbd/locale_guard.h"
29
30 #include "ardour/vst_plugin.h"
31 #include "ardour/vestige/aeffectx.h"
32 #include "ardour/session.h"
33 #include "ardour/vst_types.h"
34 #include "ardour/filesystem_paths.h"
35 #include "ardour/audio_buffer.h"
36
37 #include "pbd/i18n.h"
38
39 using namespace std;
40 using namespace PBD;
41 using namespace ARDOUR;
42
43 VSTPlugin::VSTPlugin (AudioEngine& engine, Session& session, VSTHandle* handle)
44         : Plugin (engine, session)
45         , _handle (handle)
46         , _state (0)
47         , _plugin (0)
48         , _pi (0)
49         , _num (0)
50         , _transport_frame (0)
51         , _transport_speed (0.f)
52         , _eff_bypassed (false)
53 {
54         memset (&_timeInfo, 0, sizeof(_timeInfo));
55 }
56
57 VSTPlugin::VSTPlugin (const VSTPlugin& other)
58         : Plugin (other)
59         , _handle (other._handle)
60         , _state (other._state)
61         , _plugin (other._plugin)
62         , _pi (other._pi)
63         , _num (other._num)
64         , _midi_out_buf (other._midi_out_buf)
65         , _transport_frame (0)
66         , _transport_speed (0.f)
67         , _parameter_defaults (other._parameter_defaults)
68         , _eff_bypassed (other._eff_bypassed)
69 {
70         memset (&_timeInfo, 0, sizeof(_timeInfo));
71 }
72
73 VSTPlugin::~VSTPlugin ()
74 {
75
76 }
77
78 void
79 VSTPlugin::open_plugin ()
80 {
81         _plugin = _state->plugin;
82         assert (_plugin->user == this); // should have been set by {mac_vst|fst|lxvst}_instantiate
83         _plugin->user = this;
84         _state->plugin->dispatcher (_plugin, effOpen, 0, 0, 0, 0);
85         _state->vst_version = _plugin->dispatcher (_plugin, effGetVstVersion, 0, 0, 0, 0);
86 }
87
88 void
89 VSTPlugin::init_plugin ()
90 {
91         /* set rate and blocksize */
92         _plugin->dispatcher (_plugin, effSetSampleRate, 0, 0, NULL, (float) _session.frame_rate());
93         _plugin->dispatcher (_plugin, effSetBlockSize, 0, _session.get_block_size(), NULL, 0.0f);
94 }
95
96
97 uint32_t
98 VSTPlugin::designated_bypass_port ()
99 {
100         if (_plugin->dispatcher (_plugin, effCanDo, 0, 0, const_cast<char*> ("bypass"), 0.0f) != 0) {
101 #ifdef ALLOW_VST_BYPASS_TO_FAIL // yet unused, see also plugin_insert.cc
102                 return UINT32_MAX - 1; // emulate a port
103 #else
104                 /* check if plugin actually supports it,
105                  * e.g. u-he Presswerk  CanDo "bypass"  but calling effSetBypass is a NO-OP.
106                  * (presumably the plugin-author thinks hard-bypassing is a bad idea,
107                  * particularly since the plugin itself provides a bypass-port)
108                  */
109                 intptr_t value = 0; // not bypassed
110                 if (0 != _plugin->dispatcher (_plugin, 44 /*effSetBypass*/, 0, value, NULL, 0)) {
111                         cerr << "Emulate VST Bypass Port for " << name() << endl; // XXX DEBUG
112                         return UINT32_MAX - 1; // emulate a port
113                 } else {
114                         cerr << "Do *not* Emulate VST Bypass Port for " << name() << endl; // XXX DEBUG
115                 }
116 #endif
117         }
118         return UINT32_MAX;
119 }
120
121 void
122 VSTPlugin::deactivate ()
123 {
124         _plugin->dispatcher (_plugin, effMainsChanged, 0, 0, NULL, 0.0f);
125 }
126
127 void
128 VSTPlugin::activate ()
129 {
130         _plugin->dispatcher (_plugin, effMainsChanged, 0, 1, NULL, 0.0f);
131 }
132
133 int
134 VSTPlugin::set_block_size (pframes_t nframes)
135 {
136         deactivate ();
137         _plugin->dispatcher (_plugin, effSetBlockSize, 0, nframes, NULL, 0.0f);
138         activate ();
139         return 0;
140 }
141
142 float
143 VSTPlugin::default_value (uint32_t which)
144 {
145         return _parameter_defaults[which];
146 }
147
148 float
149 VSTPlugin::get_parameter (uint32_t which) const
150 {
151         if (which == UINT32_MAX - 1) {
152                 // ardour uses enable-semantics: 1: enabled, 0: bypassed
153                 return _eff_bypassed ? 0.f : 1.f;
154         }
155         return _plugin->getParameter (_plugin, which);
156 }
157
158 void
159 VSTPlugin::set_parameter (uint32_t which, float newval)
160 {
161         if (which == UINT32_MAX - 1) {
162                 // ardour uses enable-semantics: 1: enabled, 0: bypassed
163                 intptr_t value = (newval <= 0.f) ? 1 : 0;
164                 cerr << "effSetBypass " << value << endl; // XXX DEBUG
165                 int rv = _plugin->dispatcher (_plugin, 44 /*effSetBypass*/, 0, value, NULL, 0);
166                 if (0 != rv) {
167                         _eff_bypassed = (value == 1);
168                 } else {
169                         cerr << "effSetBypass failed rv=" << rv << endl; // XXX DEBUG
170 #ifdef ALLOW_VST_BYPASS_TO_FAIL // yet unused, see also vst_plugin.cc
171                         // emit signal.. hard un/bypass from here?!
172 #endif
173                 }
174                 return;
175         }
176
177         float oldval = get_parameter (which);
178
179         if (PBD::floateq (oldval, newval, 1)) {
180                 return;
181         }
182
183         _plugin->setParameter (_plugin, which, newval);
184
185         float curval = get_parameter (which);
186
187         if (!PBD::floateq (curval, oldval, 1)) {
188                 /* value has changed, follow rest of the notification path */
189                 Plugin::set_parameter (which, newval);
190         }
191 }
192
193 void
194 VSTPlugin::parameter_changed_externally (uint32_t which, float value )
195 {
196         ParameterChangedExternally (which, value); /* EMIT SIGNAL */
197         Plugin::set_parameter (which, value);
198 }
199
200
201 uint32_t
202 VSTPlugin::nth_parameter (uint32_t n, bool& ok) const
203 {
204         ok = true;
205         return n;
206 }
207
208 /** Get VST chunk as base64-encoded data.
209  *  @param single true for single program, false for all programs.
210  *  @return 0-terminated base64-encoded data; must be passed to g_free () by caller.
211  */
212 gchar *
213 VSTPlugin::get_chunk (bool single) const
214 {
215         guchar* data;
216         int32_t data_size = _plugin->dispatcher (_plugin, 23 /* effGetChunk */, single ? 1 : 0, 0, &data, 0);
217         if (data_size == 0) {
218                 return 0;
219         }
220
221         return g_base64_encode (data, data_size);
222 }
223
224 /** Set VST chunk from base64-encoded data.
225  *  @param 0-terminated base64-encoded data.
226  *  @param single true for single program, false for all programs.
227  *  @return 0 on success, non-0 on failure
228  */
229 int
230 VSTPlugin::set_chunk (gchar const * data, bool single)
231 {
232         gsize size = 0;
233         int r = 0;
234         guchar* raw_data = g_base64_decode (data, &size);
235         {
236                 pthread_mutex_lock (&_state->state_lock);
237                 r = _plugin->dispatcher (_plugin, 24 /* effSetChunk */, single ? 1 : 0, size, raw_data, 0);
238                 pthread_mutex_unlock (&_state->state_lock);
239         }
240         g_free (raw_data);
241         return r;
242 }
243
244 void
245 VSTPlugin::add_state (XMLNode* root) const
246 {
247         LocaleGuard lg;
248
249         if (_plugin->flags & 32 /* effFlagsProgramsChunks */) {
250
251                 gchar* data = get_chunk (false);
252                 if (data == 0) {
253                         return;
254                 }
255
256                 /* store information */
257
258                 XMLNode* chunk_node = new XMLNode (X_("chunk"));
259
260                 chunk_node->add_content (data);
261                 g_free (data);
262
263                 root->add_child_nocopy (*chunk_node);
264
265         } else {
266
267                 XMLNode* parameters = new XMLNode ("parameters");
268
269                 for (int32_t n = 0; n < _plugin->numParams; ++n) {
270                         char index[64];
271                         char val[32];
272                         snprintf (index, sizeof (index), "param-%d", n);
273                         snprintf (val, sizeof (val), "%.12g", _plugin->getParameter (_plugin, n));
274                         parameters->add_property (index, val);
275                 }
276
277                 root->add_child_nocopy (*parameters);
278         }
279 }
280
281 int
282 VSTPlugin::set_state (const XMLNode& node, int version)
283 {
284         LocaleGuard lg;
285         int ret = -1;
286
287         if (node.name() != state_node_name()) {
288                 error << _("Bad node sent to VSTPlugin::set_state") << endmsg;
289                 return 0;
290         }
291
292 #ifndef NO_PLUGIN_STATE
293         XMLNode* child;
294
295         if ((child = find_named_node (node, X_("chunk"))) != 0) {
296
297                 XMLPropertyList::const_iterator i;
298                 XMLNodeList::const_iterator n;
299
300                 for (n = child->children ().begin (); n != child->children ().end (); ++n) {
301                         if ((*n)->is_content ()) {
302                                 /* XXX: this may be dubious for the same reasons that we delay
303                                          execution of load_preset.
304                                          */
305                                 ret = set_chunk ((*n)->content().c_str(), false);
306                         }
307                 }
308
309         } else if ((child = find_named_node (node, X_("parameters"))) != 0) {
310
311                 XMLPropertyList::const_iterator i;
312
313                 for (i = child->properties().begin(); i != child->properties().end(); ++i) {
314                         int32_t param;
315                         float val;
316
317                         sscanf ((*i)->name().c_str(), "param-%d", &param);
318                         sscanf ((*i)->value().c_str(), "%f", &val);
319
320                         _plugin->setParameter (_plugin, param, val);
321                 }
322
323                 ret = 0;
324
325         }
326 #endif
327
328         Plugin::set_state (node, version);
329         return ret;
330 }
331
332
333 int
334 VSTPlugin::get_parameter_descriptor (uint32_t which, ParameterDescriptor& desc) const
335 {
336         VstParameterProperties prop;
337
338         memset (&prop, 0, sizeof (VstParameterProperties));
339         desc.min_unbound = false;
340         desc.max_unbound = false;
341         prop.flags = 0;
342
343         if (_plugin->dispatcher (_plugin, effGetParameterProperties, which, 0, &prop, 0)) {
344
345                 /* i have yet to find or hear of a VST plugin that uses this */
346                 /* RG: faust2vsti does use this :) */
347
348                 if (prop.flags & kVstParameterUsesIntegerMinMax) {
349                         desc.lower = prop.minInteger;
350                         desc.upper = prop.maxInteger;
351                 } else {
352                         desc.lower = 0;
353                         desc.upper = 1.0;
354                 }
355
356                 if (prop.flags & kVstParameterUsesIntStep) {
357
358                         desc.step = prop.stepInteger;
359                         desc.smallstep = prop.stepInteger;
360                         desc.largestep = prop.stepInteger;
361
362                 } else if (prop.flags & kVstParameterUsesFloatStep) {
363
364                         desc.step = prop.stepFloat;
365                         desc.smallstep = prop.smallStepFloat;
366                         desc.largestep = prop.largeStepFloat;
367
368                 } else {
369
370                         float range = desc.upper - desc.lower;
371
372                         desc.step = range / 100.0f;
373                         desc.smallstep = desc.step / 2.0f;
374                         desc.largestep = desc.step * 10.0f;
375                 }
376
377                 if (strlen(prop.label) == 0) {
378                         _plugin->dispatcher (_plugin, effGetParamName, which, 0, prop.label, 0);
379                 }
380
381                 desc.toggled = prop.flags & kVstParameterIsSwitch;
382                 desc.logarithmic = false;
383                 desc.sr_dependent = false;
384                 desc.label = Glib::locale_to_utf8 (prop.label);
385
386         } else {
387
388                 /* old style */
389
390                 char label[64];
391                 /* some VST plugins expect this buffer to be zero-filled */
392                 memset (label, 0, sizeof (label));
393
394                 _plugin->dispatcher (_plugin, effGetParamName, which, 0, label, 0);
395
396                 desc.label = Glib::locale_to_utf8 (label);
397                 desc.integer_step = false;
398                 desc.lower = 0.0f;
399                 desc.upper = 1.0f;
400                 desc.step = 0.01f;
401                 desc.smallstep = 0.005f;
402                 desc.largestep = 0.1f;
403                 desc.toggled = false;
404                 desc.logarithmic = false;
405                 desc.sr_dependent = false;
406         }
407
408         desc.normal = get_parameter (which);
409         if (_parameter_defaults.find (which) == _parameter_defaults.end ()) {
410                 _parameter_defaults[which] = desc.normal;
411         }
412
413         return 0;
414 }
415
416 bool
417 VSTPlugin::load_preset (PresetRecord r)
418 {
419         bool s;
420
421         if (r.user) {
422                 s = load_user_preset (r);
423         } else {
424                 s = load_plugin_preset (r);
425         }
426
427         if (s) {
428                 Plugin::load_preset (r);
429         }
430
431         return s;
432 }
433
434 bool
435 VSTPlugin::load_plugin_preset (PresetRecord r)
436 {
437         /* This is a plugin-provided preset.
438            We can't dispatch directly here; too many plugins expects only one GUI thread.
439         */
440
441         /* Extract the index of this preset from the URI */
442         int id;
443         int index;
444 #ifndef NDEBUG
445         int const p = sscanf (r.uri.c_str(), "VST:%d:%d", &id, &index);
446         assert (p == 2);
447 #else
448         sscanf (r.uri.c_str(), "VST:%d:%d", &id, &index);
449 #endif
450         _state->want_program = index;
451         LoadPresetProgram (); /* EMIT SIGNAL */ /* used for macvst */
452         return true;
453 }
454
455 bool
456 VSTPlugin::load_user_preset (PresetRecord r)
457 {
458         /* This is a user preset; we load it, and this code also knows about the
459            non-direct-dispatch thing.
460         */
461
462         boost::shared_ptr<XMLTree> t (presets_tree ());
463         if (t == 0) {
464                 return false;
465         }
466
467         XMLNode* root = t->root ();
468
469         for (XMLNodeList::const_iterator i = root->children().begin(); i != root->children().end(); ++i) {
470                 XMLProperty const * label = (*i)->property (X_("label"));
471
472                 assert (label);
473
474                 if (label->value() != r.label) {
475                         continue;
476                 }
477
478                 if (_plugin->flags & 32 /* effFlagsProgramsChunks */) {
479
480                         /* Load a user preset chunk from our XML file and send it via a circuitous route to the plugin */
481
482                         if (_state->wanted_chunk) {
483                                 g_free (_state->wanted_chunk);
484                         }
485
486                         for (XMLNodeList::const_iterator j = (*i)->children().begin(); j != (*i)->children().end(); ++j) {
487                                 if ((*j)->is_content ()) {
488                                         /* we can't dispatch directly here; too many plugins expect only one GUI thread */
489                                         gsize size = 0;
490                                         guchar* raw_data = g_base64_decode ((*j)->content().c_str(), &size);
491                                         _state->wanted_chunk = raw_data;
492                                         _state->wanted_chunk_size = size;
493                                         _state->want_chunk = 1;
494                                         LoadPresetProgram (); /* EMIT SIGNAL */ /* used for macvst */
495                                         return true;
496                                 }
497                         }
498
499                         return false;
500
501                 } else {
502
503                         for (XMLNodeList::const_iterator j = (*i)->children().begin(); j != (*i)->children().end(); ++j) {
504                                 if ((*j)->name() == X_("Parameter")) {
505                                                 XMLProperty const * index = (*j)->property (X_("index"));
506                                                 XMLProperty const * value = (*j)->property (X_("value"));
507
508                                                 assert (index);
509                                                 assert (value);
510                                                 const uint32_t p = atoi (index->value().c_str());
511                                                 const float v = atof (value->value().c_str ());
512                                                 set_parameter (p, v);
513                                                 PresetPortSetValue (p, v); /* EMIT SIGNAL */
514                                 }
515                         }
516                         return true;
517                 }
518         }
519         return false;
520 }
521
522 #include "sha1.c"
523
524 string
525 VSTPlugin::do_save_preset (string name)
526 {
527         boost::shared_ptr<XMLTree> t (presets_tree ());
528         if (t == 0) {
529                 return "";
530         }
531
532         // prevent dups -- just in case
533         t->root()->remove_nodes_and_delete (X_("label"), name);
534
535         XMLNode* p = 0;
536
537         char tmp[32];
538         snprintf (tmp, 31, "%ld", _presets.size() + 1);
539         tmp[31] = 0;
540
541         char hash[41];
542         Sha1Digest s;
543         sha1_init (&s);
544         sha1_write (&s, (const uint8_t *) name.c_str(), name.size ());
545         sha1_write (&s, (const uint8_t *) tmp, strlen(tmp));
546         sha1_result_hash (&s, hash);
547
548         string const uri = string_compose (X_("VST:%1:x%2"), unique_id (), hash);
549
550         if (_plugin->flags & 32 /* effFlagsProgramsChunks */) {
551
552                 p = new XMLNode (X_("ChunkPreset"));
553                 p->add_property (X_("uri"), uri);
554                 p->add_property (X_("label"), name);
555                 gchar* data = get_chunk (true);
556                 p->add_content (string (data));
557                 g_free (data);
558
559         } else {
560
561                 p = new XMLNode (X_("Preset"));
562                 p->add_property (X_("uri"), uri);
563                 p->add_property (X_("label"), name);
564
565                 for (uint32_t i = 0; i < parameter_count(); ++i) {
566                         if (parameter_is_input (i)) {
567                                 XMLNode* c = new XMLNode (X_("Parameter"));
568                                 c->add_property (X_("index"), string_compose ("%1", i));
569                                 c->add_property (X_("value"), string_compose ("%1", get_parameter (i)));
570                                 p->add_child_nocopy (*c);
571                         }
572                 }
573         }
574
575         t->root()->add_child_nocopy (*p);
576
577         std::string f = Glib::build_filename (ARDOUR::user_config_directory (), "presets");
578         f = Glib::build_filename (f, presets_file ());
579
580         t->write (f);
581         return uri;
582 }
583
584 void
585 VSTPlugin::do_remove_preset (string name)
586 {
587         boost::shared_ptr<XMLTree> t (presets_tree ());
588         if (t == 0) {
589                 return;
590         }
591
592         t->root()->remove_nodes_and_delete (X_("label"), name);
593
594         std::string f = Glib::build_filename (ARDOUR::user_config_directory (), "presets");
595         f = Glib::build_filename (f, presets_file ());
596
597         t->write (f);
598 }
599
600 string
601 VSTPlugin::describe_parameter (Evoral::Parameter param)
602 {
603         char name[64];
604         if (param.id() == UINT32_MAX - 1) {
605                 strcpy (name, _("Plugin Enable"));
606                 return name;
607         }
608
609         memset (name, 0, sizeof (name));
610
611         /* some VST plugins expect this buffer to be zero-filled */
612
613         _plugin->dispatcher (_plugin, effGetParamName, param.id(), 0, name, 0);
614
615         if (name[0] == '\0') {
616                 strcpy (name, _("Unknown"));
617         }
618
619         return name;
620 }
621
622 framecnt_t
623 VSTPlugin::signal_latency () const
624 {
625         if (_user_latency) {
626                 return _user_latency;
627         }
628
629 #if ( defined(__x86_64__) || defined(_M_X64) )
630         return *((int32_t *) (((char *) &_plugin->flags) + 24)); /* initialDelay */
631 #else
632         return *((int32_t *) (((char *) &_plugin->flags) + 12)); /* initialDelay */
633 #endif
634 }
635
636 set<Evoral::Parameter>
637 VSTPlugin::automatable () const
638 {
639         set<Evoral::Parameter> ret;
640
641         for (uint32_t i = 0; i < parameter_count(); ++i) {
642                 ret.insert (ret.end(), Evoral::Parameter(PluginAutomation, 0, i));
643         }
644
645         return ret;
646 }
647
648 int
649 VSTPlugin::connect_and_run (BufferSet& bufs,
650                 framepos_t start, framepos_t end, double speed,
651                 ChanMapping in_map, ChanMapping out_map,
652                 pframes_t nframes, framecnt_t offset)
653 {
654         Plugin::connect_and_run(bufs, start, end, speed, in_map, out_map, nframes, offset);
655
656         if (pthread_mutex_trylock (&_state->state_lock)) {
657                 /* by convention 'effSetChunk' should not be called while processing
658                  * http://www.reaper.fm/sdk/vst/vst_ext.php
659                  *
660                  * All VSTs don't use in-place, PluginInsert::connect_and_run()
661                  * does clear output buffers, so we can just return.
662                  */
663                 return 0;
664         }
665
666         _transport_frame = start;
667         _transport_speed = speed;
668
669         ChanCount bufs_count;
670         bufs_count.set(DataType::AUDIO, 1);
671         bufs_count.set(DataType::MIDI, 1);
672         _midi_out_buf = 0;
673
674         BufferSet& silent_bufs  = _session.get_silent_buffers(bufs_count);
675         BufferSet& scratch_bufs = _session.get_scratch_buffers(bufs_count);
676
677         /* VC++ doesn't support the C99 extension that allows
678
679            typeName foo[variableDefiningSize];
680
681            Use alloca instead of dynamic array (rather than std::vector which
682            allocs on the heap) because this is realtime code.
683         */
684
685         float** ins = (float**)alloca(_plugin->numInputs*sizeof(float*));
686         float** outs = (float**)alloca(_plugin->numOutputs*sizeof(float*));
687
688         int32_t i;
689
690         uint32_t in_index = 0;
691         for (i = 0; i < (int32_t) _plugin->numInputs; ++i) {
692                 uint32_t  index;
693                 bool      valid = false;
694                 index = in_map.get(DataType::AUDIO, in_index++, &valid);
695                 ins[i] = (valid)
696                                         ? bufs.get_audio(index).data(offset)
697                                         : silent_bufs.get_audio(0).data(offset);
698         }
699
700         uint32_t out_index = 0;
701         for (i = 0; i < (int32_t) _plugin->numOutputs; ++i) {
702                 uint32_t  index;
703                 bool      valid = false;
704                 index = out_map.get(DataType::AUDIO, out_index++, &valid);
705                 outs[i] = (valid)
706                         ? bufs.get_audio(index).data(offset)
707                         : scratch_bufs.get_audio(0).data(offset);
708         }
709
710         if (bufs.count().n_midi() > 0) {
711                 VstEvents* v = 0;
712                 bool valid = false;
713                 const uint32_t buf_index_in = in_map.get(DataType::MIDI, 0, &valid);
714                 if (valid) {
715                         v = bufs.get_vst_midi (buf_index_in);
716                 }
717                 valid = false;
718                 const uint32_t buf_index_out = out_map.get(DataType::MIDI, 0, &valid);
719                 if (valid) {
720                         _midi_out_buf = &bufs.get_midi(buf_index_out);
721                         _midi_out_buf->silence(0, 0);
722                 } else {
723                         _midi_out_buf = 0;
724                 }
725                 if (v) {
726                         _plugin->dispatcher (_plugin, effProcessEvents, 0, 0, v, 0);
727                 }
728         }
729
730         /* we already know it can support processReplacing */
731         _plugin->processReplacing (_plugin, &ins[0], &outs[0], nframes);
732         _midi_out_buf = 0;
733
734         pthread_mutex_unlock (&_state->state_lock);
735         return 0;
736 }
737
738 string
739 VSTPlugin::unique_id () const
740 {
741         char buf[32];
742
743         snprintf (buf, sizeof (buf), "%d", _plugin->uniqueID);
744
745         return string (buf);
746 }
747
748
749 const char *
750 VSTPlugin::name () const
751 {
752         if (!_info->name.empty ()) {
753                 return _info->name.c_str();
754         }
755         return _handle->name;
756 }
757
758 const char *
759 VSTPlugin::maker () const
760 {
761         return _info->creator.c_str();
762 }
763
764 const char *
765 VSTPlugin::label () const
766 {
767         return _handle->name;
768 }
769
770 uint32_t
771 VSTPlugin::parameter_count () const
772 {
773         return _plugin->numParams;
774 }
775
776 bool
777 VSTPlugin::has_editor () const
778 {
779         return _plugin->flags & effFlagsHasEditor;
780 }
781
782 void
783 VSTPlugin::print_parameter (uint32_t param, char *buf, uint32_t /*len*/) const
784 {
785         char *first_nonws;
786
787         _plugin->dispatcher (_plugin, 7 /* effGetParamDisplay */, param, 0, buf, 0);
788
789         if (buf[0] == '\0') {
790                 return;
791         }
792
793         first_nonws = buf;
794         while (*first_nonws && isspace (*first_nonws)) {
795                 first_nonws++;
796         }
797
798         if (*first_nonws == '\0') {
799                 return;
800         }
801
802         memmove (buf, first_nonws, strlen (buf) - (first_nonws - buf) + 1);
803 }
804
805 void
806 VSTPlugin::find_presets ()
807 {
808         /* Built-in presets */
809
810         int const vst_version = _plugin->dispatcher (_plugin, effGetVstVersion, 0, 0, NULL, 0);
811         for (int i = 0; i < _plugin->numPrograms; ++i) {
812                 PresetRecord r (string_compose (X_("VST:%1:%2"), unique_id (), i), "", false);
813
814                 if (vst_version >= 2) {
815                         char buf[256];
816                         if (_plugin->dispatcher (_plugin, 29, i, 0, buf, 0) == 1) {
817                                 r.label = buf;
818                         } else {
819                                 r.label = string_compose (_("Preset %1"), i);
820                         }
821                 } else {
822                         r.label = string_compose (_("Preset %1"), i);
823                 }
824
825                 _presets.insert (make_pair (r.uri, r));
826         }
827
828         /* User presets from our XML file */
829
830         boost::shared_ptr<XMLTree> t (presets_tree ());
831
832         if (t) {
833                 XMLNode* root = t->root ();
834                 for (XMLNodeList::const_iterator i = root->children().begin(); i != root->children().end(); ++i) {
835
836                         XMLProperty const * uri = (*i)->property (X_("uri"));
837                         XMLProperty const * label = (*i)->property (X_("label"));
838
839                         assert (uri);
840                         assert (label);
841
842                         PresetRecord r (uri->value(), label->value(), true);
843                         _presets.insert (make_pair (r.uri, r));
844                 }
845         }
846
847 }
848
849 /** @return XMLTree with our user presets; could be a new one if no existing
850  *  one was found, or 0 if one was present but badly-formatted.
851  */
852 XMLTree *
853 VSTPlugin::presets_tree () const
854 {
855         XMLTree* t = new XMLTree;
856
857         std::string p = Glib::build_filename (ARDOUR::user_config_directory (), "presets");
858
859         if (!Glib::file_test (p, Glib::FILE_TEST_IS_DIR)) {
860                 if (g_mkdir_with_parents (p.c_str(), 0755) != 0) {
861                         error << _("Unable to make VST presets directory") << endmsg;
862                 };
863         }
864
865         p = Glib::build_filename (p, presets_file ());
866
867         if (!Glib::file_test (p, Glib::FILE_TEST_EXISTS)) {
868                 t->set_root (new XMLNode (X_("VSTPresets")));
869                 return t;
870         }
871
872         t->set_filename (p);
873         if (!t->read ()) {
874                 delete t;
875                 return 0;
876         }
877
878         return t;
879 }
880
881 /** @return Index of the first user preset in our lists */
882 int
883 VSTPlugin::first_user_preset_index () const
884 {
885         return _plugin->numPrograms;
886 }
887
888 string
889 VSTPlugin::presets_file () const
890 {
891         return string_compose ("vst-%1", unique_id ());
892 }
893