id_t becomes a fully-fledged object, UUID's used for IDs, generic MIDI now owns bindi...
[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     $Id$
19 */
20
21 #include <algorithm>
22
23 #include <midi++/port.h>
24 #include <midi++/manager.h>
25 #include <midi++/port_request.h>
26
27 #include <ardour/route.h>
28 #include <ardour/session.h>
29
30 #include "generic_midi_control_protocol.h"
31 #include "midicontrollable.h"
32
33 using namespace ARDOUR;
34 using namespace PBD;
35
36 #include "i18n.h"
37
38 GenericMidiControlProtocol::GenericMidiControlProtocol (Session& s)
39         : ControlProtocol  (s, _("GenericMIDI"))
40 {
41         MIDI::Manager* mm = MIDI::Manager::instance();
42         MIDI::PortRequest pr ("ardour:MIDI control", "ardour:MIDI control", "duplex", "alsa/seq");
43         
44         _port = mm->add_port (pr);
45
46         _feedback_interval = 10000; // microseconds
47         last_feedback_time = 0;
48
49         Controllable::StartLearning.connect (mem_fun (*this, &GenericMidiControlProtocol::start_learning));
50         Controllable::StopLearning.connect (mem_fun (*this, &GenericMidiControlProtocol::stop_learning));
51         Session::SendFeedback.connect (mem_fun (*this, &GenericMidiControlProtocol::send_feedback));
52 }
53
54 GenericMidiControlProtocol::~GenericMidiControlProtocol ()
55 {
56 }
57
58 int
59 GenericMidiControlProtocol::set_active (bool yn)
60 {
61         /* start/stop delivery/outbound thread */
62         return 0;
63 }
64
65 void
66 GenericMidiControlProtocol::set_feedback_interval (microseconds_t ms)
67 {
68         _feedback_interval = ms;
69 }
70
71 void 
72 GenericMidiControlProtocol::send_feedback ()
73 {
74         microseconds_t now = get_microseconds ();
75
76         if (last_feedback_time != 0) {
77                 if ((now - last_feedback_time) < _feedback_interval) {
78                         return;
79                 }
80         }
81
82         _send_feedback ();
83         
84         last_feedback_time = now;
85 }
86
87 void 
88 GenericMidiControlProtocol::_send_feedback ()
89 {
90         const int32_t bufsize = 16 * 1024;
91         MIDI::byte buf[bufsize];
92         int32_t bsize = bufsize;
93         MIDI::byte* end = buf;
94         
95         for (MIDIControllables::iterator r = controllables.begin(); r != controllables.end(); ++r) {
96                 end = (*r)->write_feedback (end, bsize);
97         }
98         
99         if (end == buf) {
100                 return;
101         } 
102         
103         _port->write (buf, (int32_t) (end - buf));
104 }
105
106 bool
107 GenericMidiControlProtocol::start_learning (Controllable* c)
108 {
109         if (c == 0) {
110                 return false;
111         }
112
113         MIDIControllable* mc = new MIDIControllable (*_port, *c);
114         
115
116         {
117                 Glib::Mutex::Lock lm (pending_lock);
118                 pending_controllables.push_back (mc);
119                 mc->learning_stopped.connect (bind (mem_fun (*this, &GenericMidiControlProtocol::learning_stopped), mc));
120         }
121
122         mc->learn_about_external_control ();
123         return true;
124 }
125
126 void
127 GenericMidiControlProtocol::learning_stopped (MIDIControllable* mc)
128 {
129         Glib::Mutex::Lock lm (pending_lock);
130         Glib::Mutex::Lock lm2 (controllables_lock);
131         
132         MIDIControllables::iterator i = find (pending_controllables.begin(), pending_controllables.end(), mc);
133
134         if (i != pending_controllables.end()) {
135                 pending_controllables.erase (i);
136         }
137
138         controllables.push_back (mc);
139 }
140
141 void
142 GenericMidiControlProtocol::stop_learning (Controllable* c)
143 {
144         Glib::Mutex::Lock lm (pending_lock);
145
146         /* learning timed out, and we've been told to consider this attempt to learn to be cancelled. find the
147            relevant MIDIControllable and remove it from the pending list.
148         */
149
150         for (MIDIControllables::iterator i = pending_controllables.begin(); i != pending_controllables.end(); ++i) {
151                 if (&(*i)->get_controllable() == c) {
152                         (*i)->stop_learning ();
153                         delete (*i);
154                         pending_controllables.erase (i);
155                         break;
156                 }
157         }
158 }