SVG of thorsten's editor track/bus list icons
[ardour2.git] / libs / surfaces / generic_midi / midicontrollable.cc
blob9060f010a0d654aab1b557194844400fe71426e1
1 /*
2 Copyright (C) 1998-2006 Paul Davis
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 #define __STDC_FORMAT_MACROS 1
21 #include <stdint.h>
22 #include <cmath>
23 #include <climits>
24 #include <iostream>
26 #include "pbd/error.h"
27 #include "pbd/controllable_descriptor.h"
28 #include "pbd/xml++.h"
30 #include "midi++/port.h"
31 #include "midi++/channel.h"
33 #include "ardour/automation_control.h"
34 #include "ardour/utils.h"
36 #include "midicontrollable.h"
38 using namespace std;
39 using namespace MIDI;
40 using namespace PBD;
41 using namespace ARDOUR;
43 MIDIControllable::MIDIControllable (Port& p, bool m)
44 : controllable (0)
45 , _descriptor (0)
46 , _port (p)
47 , _momentary (m)
49 _learned = false; /* from URI */
50 setting = false;
51 last_value = 0; // got a better idea ?
52 control_type = none;
53 _control_description = "MIDI Control: none";
54 control_additional = (byte) -1;
55 feedback = true; // for now
58 MIDIControllable::MIDIControllable (Port& p, Controllable& c, bool m)
59 : controllable (&c)
60 , _descriptor (0)
61 , _port (p)
62 , _momentary (m)
64 _learned = true; /* from controllable */
65 setting = false;
66 last_value = 0; // got a better idea ?
67 control_type = none;
68 _control_description = "MIDI Control: none";
69 control_additional = (byte) -1;
70 feedback = true; // for now
73 MIDIControllable::~MIDIControllable ()
75 drop_external_control ();
78 int
79 MIDIControllable::init (const std::string& s)
81 _current_uri = s;
82 delete _descriptor;
83 _descriptor = new ControllableDescriptor;
84 return _descriptor->set (s);
87 void
88 MIDIControllable::midi_forget ()
90 /* stop listening for incoming messages, but retain
91 our existing event + type information.
94 midi_sense_connection[0].disconnect ();
95 midi_sense_connection[1].disconnect ();
96 midi_learn_connection.disconnect ();
99 void
100 MIDIControllable::drop_external_control ()
102 midi_forget ();
103 control_type = none;
104 control_additional = (byte) -1;
107 void
108 MIDIControllable::set_controllable (Controllable* c)
110 controllable = c;
113 void
114 MIDIControllable::midi_rebind (channel_t c)
116 if (c >= 0) {
117 bind_midi (c, control_type, control_additional);
118 } else {
119 midi_forget ();
123 void
124 MIDIControllable::learn_about_external_control ()
126 drop_external_control ();
127 _port.parser()->any.connect_same_thread (midi_learn_connection, boost::bind (&MIDIControllable::midi_receiver, this, _1, _2, _3));
130 void
131 MIDIControllable::stop_learning ()
133 midi_learn_connection.disconnect ();
136 float
137 MIDIControllable::control_to_midi (float val)
139 const float midi_range = 127.0f; // TODO: NRPN etc.
141 if (controllable->is_gain_like()) {
142 return gain_to_slider_position (val/midi_range);
145 float control_min = controllable->lower ();
146 float control_max = controllable->upper ();
147 const float control_range = control_max - control_min;
149 return (val - control_min) / control_range * midi_range;
152 float
153 MIDIControllable::midi_to_control(float val)
155 /* fiddle with MIDI value so that we get an odd number of integer steps
156 and can thus represent "middle" precisely as 0.5. this maps to
157 the range 0..+1.0
159 TODO: 14bit values
162 val = (val == 0.0f ? 0.0f : (val-1.0f) / 126.0f);
164 if (controllable->is_gain_like()) {
165 return slider_position_to_gain (val);
168 float control_min = controllable->lower ();
169 float control_max = controllable->upper ();
170 const float control_range = control_max - control_min;
172 return (val * control_range) + control_min;
175 void
176 MIDIControllable::midi_sense_note_on (Parser &p, EventTwoBytes *tb)
178 midi_sense_note (p, tb, true);
181 void
182 MIDIControllable::midi_sense_note_off (Parser &p, EventTwoBytes *tb)
184 midi_sense_note (p, tb, false);
187 void
188 MIDIControllable::midi_sense_note (Parser &, EventTwoBytes *msg, bool /*is_on*/)
190 if (!controllable) {
191 return;
195 if (!controllable->is_toggle()) {
196 controllable->set_value (msg->note_number/127.0);
197 } else {
199 if (control_additional == msg->note_number) {
200 controllable->set_value (controllable->get_value() > 0.5f ? 0.0f : 1.0f);
204 last_value = (MIDI::byte) (controllable->get_value() * 127.0); // to prevent feedback fights
207 void
208 MIDIControllable::midi_sense_controller (Parser &, EventTwoBytes *msg)
210 if (!controllable) {
211 return;
214 if (controllable->touching()) {
215 return; // to prevent feedback fights when e.g. dragging a UI slider
218 if (control_additional == msg->controller_number) {
220 if (!controllable->is_toggle()) {
221 controllable->set_value (midi_to_control (msg->value));
222 } else {
223 if (msg->value > 64.0f) {
224 controllable->set_value (1);
225 } else {
226 controllable->set_value (0);
230 last_value = (MIDI::byte) (control_to_midi(controllable->get_value())); // to prevent feedback fights
234 void
235 MIDIControllable::midi_sense_program_change (Parser &, byte msg)
237 if (!controllable) {
238 return;
241 if (!controllable->is_toggle()) {
242 controllable->set_value (msg/127.0);
243 } else {
244 controllable->set_value (controllable->get_value() > 0.5f ? 0.0f : 1.0f);
247 last_value = (MIDI::byte) (controllable->get_value() * 127.0); // to prevent feedback fights
250 void
251 MIDIControllable::midi_sense_pitchbend (Parser &, pitchbend_t pb)
253 if (!controllable) {
254 return;
257 if (!controllable->is_toggle()) {
258 /* XXX gack - get rid of assumption about typeof pitchbend_t */
259 controllable->set_value ((pb/(float) SHRT_MAX));
260 } else {
261 controllable->set_value (controllable->get_value() > 0.5f ? 0.0f : 1.0f);
264 last_value = (MIDI::byte) (controllable->get_value() * 127.0); // to prevent feedback fights
267 void
268 MIDIControllable::midi_receiver (Parser &, byte *msg, size_t /*len*/)
270 /* we only respond to channel messages */
272 if ((msg[0] & 0xF0) < 0x80 || (msg[0] & 0xF0) > 0xE0) {
273 return;
276 /* if the our port doesn't do input anymore, forget it ... */
278 if (!_port.parser()) {
279 return;
282 bind_midi ((channel_t) (msg[0] & 0xf), eventType (msg[0] & 0xF0), msg[1]);
284 controllable->LearningFinished ();
287 void
288 MIDIControllable::bind_midi (channel_t chn, eventType ev, MIDI::byte additional)
290 char buf[64];
292 drop_external_control ();
294 control_type = ev;
295 control_channel = chn;
296 control_additional = additional;
298 if (_port.parser() == 0) {
299 return;
302 Parser& p = *_port.parser();
304 int chn_i = chn;
305 switch (ev) {
306 case MIDI::off:
307 p.channel_note_off[chn_i].connect_same_thread (midi_sense_connection[0], boost::bind (&MIDIControllable::midi_sense_note_off, this, _1, _2));
309 /* if this is a togglee, connect to noteOn as well,
310 and we'll toggle back and forth between the two.
313 if (_momentary) {
314 p.channel_note_on[chn_i].connect_same_thread (midi_sense_connection[1], boost::bind (&MIDIControllable::midi_sense_note_on, this, _1, _2));
317 _control_description = "MIDI control: NoteOff";
318 break;
320 case MIDI::on:
321 p.channel_note_on[chn_i].connect_same_thread (midi_sense_connection[0], boost::bind (&MIDIControllable::midi_sense_note_on, this, _1, _2));
322 if (_momentary) {
323 p.channel_note_off[chn_i].connect_same_thread (midi_sense_connection[1], boost::bind (&MIDIControllable::midi_sense_note_off, this, _1, _2));
325 _control_description = "MIDI control: NoteOn";
326 break;
328 case MIDI::controller:
329 p.channel_controller[chn_i].connect_same_thread (midi_sense_connection[0], boost::bind (&MIDIControllable::midi_sense_controller, this, _1, _2));
330 snprintf (buf, sizeof (buf), "MIDI control: Controller %d", control_additional);
331 _control_description = buf;
332 break;
334 case MIDI::program:
335 p.channel_program_change[chn_i].connect_same_thread (midi_sense_connection[0], boost::bind (&MIDIControllable::midi_sense_program_change, this, _1, _2));
336 _control_description = "MIDI control: ProgramChange";
337 break;
339 case MIDI::pitchbend:
340 p.channel_pitchbend[chn_i].connect_same_thread (midi_sense_connection[0], boost::bind (&MIDIControllable::midi_sense_pitchbend, this, _1, _2));
341 _control_description = "MIDI control: Pitchbend";
342 break;
344 default:
345 break;
349 void
350 MIDIControllable::send_feedback ()
352 byte msg[3];
354 if (!_learned || setting || !feedback || control_type == none) {
355 return;
358 msg[0] = (control_type & 0xF0) | (control_channel & 0xF);
359 msg[1] = control_additional;
361 if (controllable->is_gain_like()) {
362 msg[2] = (byte) lrintf (gain_to_slider_position (controllable->get_value()) * 127.0f);
363 } else {
364 msg[2] = (byte) (control_to_midi(controllable->get_value()));
367 _port.write (msg, 3, 0);
370 MIDI::byte*
371 MIDIControllable::write_feedback (MIDI::byte* buf, int32_t& bufsize, bool /*force*/)
373 if (control_type != none && feedback && bufsize > 2) {
375 MIDI::byte gm;
377 if (controllable->is_gain_like()) {
378 gm = (byte) lrintf (gain_to_slider_position (controllable->get_value()) * 127.0f);
379 } else {
380 gm = (byte) (control_to_midi(controllable->get_value()));
383 if (gm != last_value) {
384 *buf++ = (0xF0 & control_type) | (0xF & control_channel);
385 *buf++ = control_additional; /* controller number */
386 *buf++ = gm;
387 last_value = gm;
388 bufsize -= 3;
392 return buf;
396 MIDIControllable::set_state (const XMLNode& node, int /*version*/)
398 const XMLProperty* prop;
399 int xx;
401 if ((prop = node.property ("event")) != 0) {
402 sscanf (prop->value().c_str(), "0x%x", &xx);
403 control_type = (MIDI::eventType) xx;
404 } else {
405 return -1;
408 if ((prop = node.property ("channel")) != 0) {
409 sscanf (prop->value().c_str(), "%d", &xx);
410 control_channel = (MIDI::channel_t) xx;
411 } else {
412 return -1;
415 if ((prop = node.property ("additional")) != 0) {
416 sscanf (prop->value().c_str(), "0x%x", &xx);
417 control_additional = (MIDI::byte) xx;
418 } else {
419 return -1;
422 if ((prop = node.property ("feedback")) != 0) {
423 feedback = (prop->value() == "yes");
424 } else {
425 feedback = true; // default
428 bind_midi (control_channel, control_type, control_additional);
430 return 0;
433 XMLNode&
434 MIDIControllable::get_state ()
436 char buf[32];
438 XMLNode* node = new XMLNode ("MIDIControllable");
440 if (_current_uri.empty()) {
441 node->add_property ("id", controllable->id().to_s());
442 } else {
443 node->add_property ("uri", _current_uri);
446 if (controllable) {
447 snprintf (buf, sizeof(buf), "0x%x", (int) control_type);
448 node->add_property ("event", buf);
449 snprintf (buf, sizeof(buf), "%d", (int) control_channel);
450 node->add_property ("channel", buf);
451 snprintf (buf, sizeof(buf), "0x%x", (int) control_additional);
452 node->add_property ("additional", buf);
453 node->add_property ("feedback", (feedback ? "yes" : "no"));
456 return *node;