provide a real and usable MuteControllable for Routes (so that MIDI can use it)
[ardour.git] / libs / surfaces / generic_midi / generic_midi_control_protocol.cc
1 /*
2     Copyright (C) 2006 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 #define __STDC_FORMAT_MACROS 1
21 #include <stdint.h>
22
23 #include <sstream>
24 #include <algorithm>
25
26 #include "pbd/controllable_descriptor.h"
27 #include "pbd/error.h"
28 #include "pbd/failed_constructor.h"
29 #include "pbd/pathscanner.h"
30 #include "pbd/xml++.h"
31
32 #include "midi++/port.h"
33 #include "midi++/manager.h"
34
35 #include "ardour/filesystem_paths.h"
36 #include "ardour/session.h"
37 #include "ardour/route.h"
38 #include "ardour/midi_ui.h"
39
40 #include "generic_midi_control_protocol.h"
41 #include "midicontrollable.h"
42 #include "midifunction.h"
43
44 using namespace ARDOUR;
45 using namespace PBD;
46 using namespace std;
47
48 #include "i18n.h"
49
50 #define midi_ui_context() MidiControlUI::instance() /* a UICallback-derived object that specifies the event loop for signal handling */
51 #define ui_bind(x) boost::protect (boost::bind ((x)))
52
53 GenericMidiControlProtocol::GenericMidiControlProtocol (Session& s)
54         : ControlProtocol (s, _("Generic MIDI"), midi_ui_context())
55         , gui (0)
56 {
57
58         MIDI::Manager* mm = MIDI::Manager::instance();
59         
60         /* XXX it might be nice to run "control" through i18n, but thats a bit tricky because
61            the name is defined in ardour.rc which is likely not internationalized.
62         */
63         
64         _port = mm->port (X_("control"));
65
66         if (_port == 0) {
67                 error << _("no MIDI port named \"control\" exists - generic MIDI control disabled") << endmsg;
68                 throw failed_constructor();
69         }
70
71         do_feedback = false;
72         _feedback_interval = 10000; // microseconds
73         last_feedback_time = 0;
74
75         _current_bank = 0;
76         _bank_size = 0;
77
78         /* XXX is it right to do all these in the same thread as whatever emits the signal? */
79
80         Controllable::StartLearning.connect_same_thread (*this, boost::bind (&GenericMidiControlProtocol::start_learning, this, _1));
81         Controllable::StopLearning.connect_same_thread (*this, boost::bind (&GenericMidiControlProtocol::stop_learning, this, _1));
82         Controllable::CreateBinding.connect_same_thread (*this, boost::bind (&GenericMidiControlProtocol::create_binding, this, _1, _2, _3));
83         Controllable::DeleteBinding.connect_same_thread (*this, boost::bind (&GenericMidiControlProtocol::delete_binding, this, _1));
84
85         Session::SendFeedback.connect (*this, boost::bind (&GenericMidiControlProtocol::send_feedback, this), midi_ui_context());;
86         Route::RemoteControlIDChange.connect (*this, boost::bind (&GenericMidiControlProtocol::reset_controllables, this), midi_ui_context());
87
88         reload_maps ();
89 }
90
91 GenericMidiControlProtocol::~GenericMidiControlProtocol ()
92 {
93         drop_all ();
94         tear_down_gui ();
95 }
96
97 static const char* const midi_map_dir_name = "midi_maps";
98 static const char* const midi_map_suffix = ".map";
99
100 static sys::path
101 system_midi_map_search_path ()
102 {
103         SearchPath spath(system_data_search_path());
104         spath.add_subdirectory_to_paths(midi_map_dir_name);
105
106         // just return the first directory in the search path that exists
107         SearchPath::const_iterator i = std::find_if(spath.begin(), spath.end(), sys::exists);
108
109         if (i == spath.end()) return sys::path();
110
111         return *i;
112 }
113
114 static sys::path
115 user_midi_map_directory ()
116 {
117         sys::path p(user_config_directory());
118         p /= midi_map_dir_name;
119
120         return p;
121 }
122
123 static bool
124 midi_map_filter (const string &str, void */*arg*/)
125 {
126         return (str.length() > strlen(midi_map_suffix) &&
127                 str.find (midi_map_suffix) == (str.length() - strlen (midi_map_suffix)));
128 }
129
130 void
131 GenericMidiControlProtocol::reload_maps ()
132 {
133         vector<string *> *midi_maps;
134         PathScanner scanner;
135         SearchPath spath (system_midi_map_search_path());
136         spath += user_midi_map_directory ();
137
138         midi_maps = scanner (spath.to_string(), midi_map_filter, 0, false, true);
139
140         if (!midi_maps) {
141                 cerr << "No MIDI maps found using " << spath.to_string() << endl;
142                 return;
143         }
144
145         cerr << "Found " << midi_maps->size() << " MIDI maps along " << spath.to_string() << endl;
146
147         for (vector<string*>::iterator i = midi_maps->begin(); i != midi_maps->end(); ++i) {
148                 string fullpath = *(*i);
149
150                 XMLTree tree;
151
152                 if (!tree.read (fullpath.c_str())) {
153                         continue;
154                 }
155
156                 MapInfo mi;
157
158                 XMLProperty* prop = tree.root()->property ("name");
159
160                 if (!prop) {
161                         continue;
162                 }
163
164                 mi.name = prop->value ();
165                 mi.path = fullpath;
166                 
167                 map_info.push_back (mi);
168         }
169
170         delete midi_maps;
171 }
172         
173 void
174 GenericMidiControlProtocol::drop_all ()
175 {
176         Glib::Mutex::Lock lm (pending_lock);
177         Glib::Mutex::Lock lm2 (controllables_lock);
178
179         for (MIDIControllables::iterator i = controllables.begin(); i != controllables.end(); ++i) {
180                 delete *i;
181         }
182         controllables.clear ();
183
184         for (MIDIPendingControllables::iterator i = pending_controllables.begin(); i != pending_controllables.end(); ++i) {
185                 delete *i;
186         }
187         pending_controllables.clear ();
188
189         for (MIDIFunctions::iterator i = functions.begin(); i != functions.end(); ++i) {
190                 delete *i;
191         }
192         functions.clear ();
193 }
194
195 void
196 GenericMidiControlProtocol::drop_bindings ()
197 {
198         Glib::Mutex::Lock lm2 (controllables_lock);
199
200         for (MIDIControllables::iterator i = controllables.begin(); i != controllables.end(); ) {
201                 if (!(*i)->learned()) {
202                         delete *i;
203                         i = controllables.erase (i);
204                 } else {
205                         ++i;
206                 }
207         }
208
209         for (MIDIFunctions::iterator i = functions.begin(); i != functions.end(); ++i) {
210                 delete *i;
211         }
212         functions.clear ();
213
214         _current_binding = "";
215         _bank_size = 0;
216         _current_bank = 0;
217 }
218
219 int
220 GenericMidiControlProtocol::set_active (bool /*yn*/)
221 {
222         /* start/stop delivery/outbound thread */
223         return 0;
224 }
225
226 void
227 GenericMidiControlProtocol::set_feedback_interval (microseconds_t ms)
228 {
229         _feedback_interval = ms;
230 }
231
232 void 
233 GenericMidiControlProtocol::send_feedback ()
234 {
235         if (!do_feedback) {
236                 return;
237         }
238
239         microseconds_t now = get_microseconds ();
240
241         if (last_feedback_time != 0) {
242                 if ((now - last_feedback_time) < _feedback_interval) {
243                         return;
244                 }
245         }
246
247         _send_feedback ();
248         
249         last_feedback_time = now;
250 }
251
252 void 
253 GenericMidiControlProtocol::_send_feedback ()
254 {
255         const int32_t bufsize = 16 * 1024; /* XXX too big */
256         MIDI::byte buf[bufsize];
257         int32_t bsize = bufsize;
258         MIDI::byte* end = buf;
259         
260         for (MIDIControllables::iterator r = controllables.begin(); r != controllables.end(); ++r) {
261                 end = (*r)->write_feedback (end, bsize);
262         }
263         
264         if (end == buf) {
265                 return;
266         } 
267
268         _port->write (buf, (int32_t) (end - buf), 0);
269 }
270
271 bool
272 GenericMidiControlProtocol::start_learning (Controllable* c)
273 {
274         if (c == 0) {
275                 return false;
276         }
277
278         Glib::Mutex::Lock lm2 (controllables_lock);
279
280         MIDIControllables::iterator tmp;
281         for (MIDIControllables::iterator i = controllables.begin(); i != controllables.end(); ) {
282                 tmp = i;
283                 ++tmp;
284                 if ((*i)->get_controllable() == c) {
285                         delete (*i);
286                         controllables.erase (i);
287                 }
288                 i = tmp;
289         }
290
291         {
292                 Glib::Mutex::Lock lm (pending_lock);
293                 
294                 MIDIPendingControllables::iterator ptmp;
295                 for (MIDIPendingControllables::iterator i = pending_controllables.begin(); i != pending_controllables.end(); ) {
296                         ptmp = i;
297                         ++ptmp;
298                         if (((*i)->first)->get_controllable() == c) {
299                                 (*i)->second.disconnect();
300                                 delete (*i)->first;
301                                 delete *i;
302                                 pending_controllables.erase (i);
303                         }
304                         i = ptmp;
305                 }
306         }
307
308         MIDIControllable* mc = 0;
309
310         for (MIDIControllables::iterator i = controllables.begin(); i != controllables.end(); ++i) {
311                 if ((*i)->get_controllable() && ((*i)->get_controllable()->id() == c->id())) {
312                         mc = *i;
313                         break;
314                 }
315         }
316
317         if (!mc) {
318                 mc = new MIDIControllable (*_port, *c, false);
319         }
320         
321         {
322                 Glib::Mutex::Lock lm (pending_lock);
323
324                 MIDIPendingControllable* element = new MIDIPendingControllable;
325                 element->first = mc;
326                 c->LearningFinished.connect_same_thread (element->second, boost::bind (&GenericMidiControlProtocol::learning_stopped, this, mc));
327
328                 pending_controllables.push_back (element);
329         }
330
331         mc->learn_about_external_control ();
332         return true;
333 }
334
335 void
336 GenericMidiControlProtocol::learning_stopped (MIDIControllable* mc)
337 {
338         Glib::Mutex::Lock lm (pending_lock);
339         Glib::Mutex::Lock lm2 (controllables_lock);
340         
341         MIDIPendingControllables::iterator tmp;
342
343         for (MIDIPendingControllables::iterator i = pending_controllables.begin(); i != pending_controllables.end(); ) {
344                 tmp = i;
345                 ++tmp;
346
347                 if ( (*i)->first == mc) {
348                         (*i)->second.disconnect();
349                         delete *i;
350                         pending_controllables.erase(i);
351                 }
352
353                 i = tmp;
354         }
355
356         controllables.push_back (mc);
357 }
358
359 void
360 GenericMidiControlProtocol::stop_learning (Controllable* c)
361 {
362         Glib::Mutex::Lock lm (pending_lock);
363         Glib::Mutex::Lock lm2 (controllables_lock);
364         MIDIControllable* dptr = 0;
365
366         /* learning timed out, and we've been told to consider this attempt to learn to be cancelled. find the
367            relevant MIDIControllable and remove it from the pending list.
368         */
369
370         for (MIDIPendingControllables::iterator i = pending_controllables.begin(); i != pending_controllables.end(); ++i) {
371                 if (((*i)->first)->get_controllable() == c) {
372                         (*i)->first->stop_learning ();
373                         dptr = (*i)->first;
374                         (*i)->second.disconnect();
375
376                         delete *i;
377                         pending_controllables.erase (i);
378                         break;
379                 }
380         }
381         
382         delete dptr;
383 }
384
385 void
386 GenericMidiControlProtocol::delete_binding (PBD::Controllable* control)
387 {
388         if (control != 0) {
389                 Glib::Mutex::Lock lm2 (controllables_lock);
390                 
391                 for (MIDIControllables::iterator iter = controllables.begin(); iter != controllables.end(); ++iter) {
392                         MIDIControllable* existingBinding = (*iter);
393                         
394                         if (control == (existingBinding->get_controllable())) {
395                                 delete existingBinding;
396                                 controllables.erase (iter);
397                         }
398                         
399                 }
400         }
401 }
402
403 void
404 GenericMidiControlProtocol::create_binding (PBD::Controllable* control, int pos, int control_number)
405 {
406         if (control != NULL) {
407                 Glib::Mutex::Lock lm2 (controllables_lock);
408                 
409                 MIDI::channel_t channel = (pos & 0xf);
410                 MIDI::byte value = control_number;
411                 
412                 // Create a MIDIControllable
413                 MIDIControllable* mc = new MIDIControllable (*_port, *control, false);
414
415                 // Remove any old binding for this midi channel/type/value pair
416                 // Note:  can't use delete_binding() here because we don't know the specific controllable we want to remove, only the midi information
417                 for (MIDIControllables::iterator iter = controllables.begin(); iter != controllables.end(); ++iter) {
418                         MIDIControllable* existingBinding = (*iter);
419                         
420                         if ((existingBinding->get_control_channel() & 0xf ) == channel &&
421                             existingBinding->get_control_additional() == value &&
422                             (existingBinding->get_control_type() & 0xf0 ) == MIDI::controller) {
423                                 
424                                 delete existingBinding;
425                                 controllables.erase (iter);
426                         }
427                         
428                 }
429                 
430                 // Update the MIDI Controllable based on the the pos param
431                 // Here is where a table lookup for user mappings could go; for now we'll just wing it...
432                 mc->bind_midi(channel, MIDI::controller, value);
433
434                 controllables.push_back (mc);
435         }
436 }
437
438 XMLNode&
439 GenericMidiControlProtocol::get_state () 
440 {
441         XMLNode* node = new XMLNode ("Protocol"); 
442         char buf[32];
443
444         node->add_property (X_("name"), _name);
445         node->add_property (X_("feedback"), do_feedback ? "1" : "0");
446         snprintf (buf, sizeof (buf), "%" PRIu64, _feedback_interval);
447         node->add_property (X_("feedback_interval"), buf);
448
449         if (!_current_binding.empty()) {
450                 node->add_property ("binding", _current_binding);
451         }
452
453         XMLNode* children = new XMLNode (X_("controls"));
454
455         node->add_child_nocopy (*children);
456
457         Glib::Mutex::Lock lm2 (controllables_lock);
458         for (MIDIControllables::iterator i = controllables.begin(); i != controllables.end(); ++i) {
459
460                 /* we don't care about bindings that come from a bindings map, because
461                    they will all be reset/recreated when we load the relevant bindings
462                    file.
463                 */
464
465                 if ((*i)->learned()) {
466                         children->add_child_nocopy ((*i)->get_state());
467                 }
468         }
469
470         return *node;
471 }
472
473 int
474 GenericMidiControlProtocol::set_state (const XMLNode& node, int version)
475 {
476         XMLNodeList nlist;
477         XMLNodeConstIterator niter;
478         const XMLProperty* prop;
479
480         if ((prop = node.property ("feedback")) != 0) {
481                 do_feedback = (bool) atoi (prop->value().c_str());
482         } else {
483                 do_feedback = false;
484         }
485
486         if ((prop = node.property ("feedback_interval")) != 0) {
487                 if (sscanf (prop->value().c_str(), "%" PRIu64, &_feedback_interval) != 1) {
488                         _feedback_interval = 10000;
489                 }
490         } else {
491                 _feedback_interval = 10000;
492         }
493
494         boost::shared_ptr<Controllable> c;
495         
496         {
497                 Glib::Mutex::Lock lm (pending_lock);
498                 for (MIDIPendingControllables::iterator i = pending_controllables.begin(); i != pending_controllables.end(); ++i) {
499                         delete *i;
500                 }
501                 pending_controllables.clear ();
502         }
503
504         {
505                 Glib::Mutex::Lock lm2 (controllables_lock);
506                 controllables.clear ();
507                 nlist = node.children(); // "controls"
508                 
509                 if (nlist.empty()) {
510                         return 0;
511                 }
512                 
513                 nlist = nlist.front()->children ();
514                 
515                 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
516                         
517                         if ((prop = (*niter)->property ("id")) != 0) {
518                                 
519                                 ID id = prop->value ();
520                                 c = session->controllable_by_id (id);
521                                 
522                                 if (c) {
523                                         MIDIControllable* mc = new MIDIControllable (*_port, *c, false);
524
525                                         if (mc->set_state (**niter, version) == 0) {
526                                                 controllables.push_back (mc);
527                                         }
528                                         
529                                 } else {
530                                         warning << string_compose (
531                                                 _("Generic MIDI control: controllable %1 not found in session (ignored)"),
532                                                 id) << endmsg;
533                                 }
534                         }
535                 }
536
537         }
538
539         if ((prop = node.property ("binding")) != 0) {
540                 for (list<MapInfo>::iterator x = map_info.begin(); x != map_info.end(); ++x) {
541                         if (prop->value() == (*x).name) {
542                                 load_bindings ((*x).path);
543                                 break;
544                         }
545                 }
546         }
547
548         return 0;
549 }
550
551 int
552 GenericMidiControlProtocol::set_feedback (bool yn)
553 {
554         do_feedback = yn;
555         last_feedback_time = 0;
556         return 0;
557 }
558
559 bool
560 GenericMidiControlProtocol::get_feedback () const
561 {
562         return do_feedback;
563 }
564
565
566
567
568 int
569 GenericMidiControlProtocol::load_bindings (const string& xmlpath)
570 {
571         XMLTree state_tree;
572
573         if (!state_tree.read (xmlpath.c_str())) {
574                 error << string_compose(_("Could not understand MIDI bindings file %1"), xmlpath) << endmsg;
575                 return -1;
576         }
577
578         XMLNode* root = state_tree.root();
579
580         if (root->name() != X_("ArdourMIDIBindings")) {
581                 error << string_compose (_("MIDI Bindings file %1 is not really a MIDI bindings file"), xmlpath) << endmsg;
582                 return -1;
583         }
584
585         const XMLProperty* prop;
586
587         if ((prop = root->property ("version")) == 0) {
588                 return -1;
589         } else {
590                 int major;
591                 int minor;
592                 int micro;
593
594                 sscanf (prop->value().c_str(), "%d.%d.%d", &major, &minor, &micro);
595                 Stateful::loading_state_version = (major * 1000) + minor;
596         }
597         
598         const XMLNodeList& children (root->children());
599         XMLNodeConstIterator citer;
600         XMLNodeConstIterator gciter;
601
602         MIDIControllable* mc;
603
604         drop_all ();
605
606         for (citer = children.begin(); citer != children.end(); ++citer) {
607                 
608                 if ((*citer)->name() == "DeviceInfo") {
609                         const XMLProperty* prop;
610
611                         if ((prop = (*citer)->property ("bank-size")) != 0) {
612                                 _bank_size = atoi (prop->value());
613                                 _current_bank = 0;
614                         }
615                 }
616
617                 if ((*citer)->name() == "Binding") {
618                         const XMLNode* child = *citer;
619
620                         if (child->property ("uri")) {
621                                 /* controllable */
622                                 
623                                 if ((mc = create_binding (*child)) != 0) {
624                                         Glib::Mutex::Lock lm2 (controllables_lock);
625                                         controllables.push_back (mc);
626                                 }
627
628                         } else if (child->property ("function")) {
629
630                                 /* function */
631                                 MIDIFunction* mf;
632
633                                 if ((mf = create_function (*child)) != 0) {
634                                         functions.push_back (mf);
635                                 }
636                         }
637                 }
638         }
639         
640         if ((prop = root->property ("name")) != 0) {
641                 _current_binding = prop->value ();
642         }
643
644         reset_controllables ();
645
646         return 0;
647 }
648
649 MIDIControllable*
650 GenericMidiControlProtocol::create_binding (const XMLNode& node)
651 {
652         const XMLProperty* prop;
653         MIDI::byte detail;
654         MIDI::channel_t channel;
655         string uri;
656         MIDI::eventType ev;
657         int intval;
658         bool momentary;
659
660         if ((prop = node.property (X_("ctl"))) != 0) {
661                 ev = MIDI::controller;
662         } else if ((prop = node.property (X_("note"))) != 0) {
663                 ev = MIDI::on;
664         } else if ((prop = node.property (X_("pgm"))) != 0) {
665                 ev = MIDI::program;
666         } else {
667                 return 0;
668         }
669         
670         if (sscanf (prop->value().c_str(), "%d", &intval) != 1) {
671                 return 0;
672         }
673         
674         detail = (MIDI::byte) intval;
675
676         if ((prop = node.property (X_("channel"))) == 0) {
677                 return 0;
678         }
679         
680         if (sscanf (prop->value().c_str(), "%d", &intval) != 1) {
681                 return 0;
682         }
683         channel = (MIDI::channel_t) intval;
684         /* adjust channel to zero-based counting */
685         if (channel > 0) {
686                 channel -= 1;
687         }
688
689         if ((prop = node.property (X_("momentary"))) != 0) {
690                 momentary = string_is_affirmative (prop->value());
691         } else {
692                 momentary = false;
693         }
694         
695         prop = node.property (X_("uri"));
696         uri = prop->value();
697
698         MIDIControllable* mc = new MIDIControllable (*_port, momentary);
699
700         if (mc->init (uri)) {
701                 delete mc;
702                 return 0;
703         }
704
705         mc->bind_midi (channel, ev, detail);
706
707         return mc;
708 }
709
710 void
711 GenericMidiControlProtocol::reset_controllables ()
712 {
713         Glib::Mutex::Lock lm2 (controllables_lock);
714         
715         for (MIDIControllables::iterator iter = controllables.begin(); iter != controllables.end(); ++iter) {
716                 MIDIControllable* existingBinding = (*iter);
717
718                 if (!existingBinding->learned()) {
719                         ControllableDescriptor& desc (existingBinding->descriptor());
720
721                         if (desc.banked()) {
722                                 desc.set_bank_offset (_current_bank * _bank_size);
723                         }
724
725                         boost::shared_ptr<Controllable> c = session->controllable_by_descriptor (desc);
726                         existingBinding->set_controllable (c.get());
727                 }
728         }
729 }
730
731 MIDIFunction*
732 GenericMidiControlProtocol::create_function (const XMLNode& node)
733 {
734         const XMLProperty* prop;
735         int intval;
736         MIDI::byte detail = 0;
737         MIDI::channel_t channel = 0;
738         string uri;
739         MIDI::eventType ev;
740         MIDI::byte* sysex = 0;
741         uint32_t sysex_size = 0;
742
743         if ((prop = node.property (X_("ctl"))) != 0) {
744                 ev = MIDI::controller;
745         } else if ((prop = node.property (X_("note"))) != 0) {
746                 ev = MIDI::on;
747         } else if ((prop = node.property (X_("pgm"))) != 0) {
748                 ev = MIDI::program;
749         } else if ((prop = node.property (X_("sysex"))) != 0) {
750
751                 ev = MIDI::sysex;
752                 int val;
753                 uint32_t cnt;
754
755                 {
756                         cnt = 0;
757                         stringstream ss (prop->value());
758                         ss << hex;
759                         
760                         while (ss >> val) {
761                                 cnt++;
762                         }
763                 }
764
765                 if (cnt == 0) {
766                         return 0;
767                 }
768
769                 sysex = new MIDI::byte[cnt];
770                 sysex_size = cnt;
771                 
772                 {
773                         stringstream ss (prop->value());
774                         ss << hex;
775                         cnt = 0;
776                         
777                         while (ss >> val) {
778                                 sysex[cnt++] = (MIDI::byte) val;
779                         }
780                 }
781                 
782         } else {
783                 warning << "Binding ignored - unknown type" << endmsg;
784                 return 0;
785         }
786
787         if (sysex_size == 0) {
788                 if (sscanf (prop->value().c_str(), "%d", &intval) != 1) {
789                         return 0;
790                 }
791                 
792                 detail = (MIDI::byte) intval;
793
794                 if ((prop = node.property (X_("channel"))) == 0) {
795                         return 0;
796                 }
797         
798                 if (sscanf (prop->value().c_str(), "%d", &intval) != 1) {
799                         return 0;
800                 }
801                 channel = (MIDI::channel_t) intval;
802                 /* adjust channel to zero-based counting */
803                 if (channel > 0) {
804                         channel -= 1;
805                 }
806         }
807
808         prop = node.property (X_("function"));
809         
810         MIDIFunction* mf = new MIDIFunction (*_port);
811         
812         if (mf->init (*this, prop->value(), sysex, sysex_size)) {
813                 delete mf;
814                 return 0;
815         }
816
817         mf->bind_midi (channel, ev, detail);
818
819         return mf;
820 }
821
822 void
823 GenericMidiControlProtocol::set_current_bank (uint32_t b)
824 {
825         _current_bank = b;
826         reset_controllables ();
827 }
828
829 void
830 GenericMidiControlProtocol::next_bank ()
831 {
832         _current_bank++;
833         reset_controllables ();
834 }
835
836 void
837 GenericMidiControlProtocol::prev_bank()
838 {
839         if (_current_bank) {
840                 _current_bank--;
841                 reset_controllables ();
842         }
843 }