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