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
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"
41 using namespace ARDOUR
;
43 MIDIControllable::MIDIControllable (Port
& p
, bool m
)
49 _learned
= false; /* from URI */
51 last_value
= 0; // got a better idea ?
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
)
64 _learned
= true; /* from controllable */
66 last_value
= 0; // got a better idea ?
68 _control_description
= "MIDI Control: none";
69 control_additional
= (byte
) -1;
70 feedback
= true; // for now
73 MIDIControllable::~MIDIControllable ()
75 drop_external_control ();
79 MIDIControllable::init (const std::string
& s
)
83 _descriptor
= new ControllableDescriptor
;
84 return _descriptor
->set (s
);
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 ();
100 MIDIControllable::drop_external_control ()
104 control_additional
= (byte
) -1;
108 MIDIControllable::set_controllable (Controllable
* c
)
114 MIDIControllable::midi_rebind (channel_t c
)
117 bind_midi (c
, control_type
, control_additional
);
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
));
131 MIDIControllable::stop_learning ()
133 midi_learn_connection
.disconnect ();
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
;
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
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
;
176 MIDIControllable::midi_sense_note_on (Parser
&p
, EventTwoBytes
*tb
)
178 midi_sense_note (p
, tb
, true);
182 MIDIControllable::midi_sense_note_off (Parser
&p
, EventTwoBytes
*tb
)
184 midi_sense_note (p
, tb
, false);
188 MIDIControllable::midi_sense_note (Parser
&, EventTwoBytes
*msg
, bool /*is_on*/)
195 if (!controllable
->is_toggle()) {
196 controllable
->set_value (msg
->note_number
/127.0);
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
208 MIDIControllable::midi_sense_controller (Parser
&, EventTwoBytes
*msg
)
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
));
223 if (msg
->value
> 64.0f
) {
224 controllable
->set_value (1);
226 controllable
->set_value (0);
230 last_value
= (MIDI::byte
) (control_to_midi(controllable
->get_value())); // to prevent feedback fights
235 MIDIControllable::midi_sense_program_change (Parser
&, byte msg
)
241 if (!controllable
->is_toggle()) {
242 controllable
->set_value (msg
/127.0);
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
251 MIDIControllable::midi_sense_pitchbend (Parser
&, pitchbend_t pb
)
257 if (!controllable
->is_toggle()) {
258 /* XXX gack - get rid of assumption about typeof pitchbend_t */
259 controllable
->set_value ((pb
/(float) SHRT_MAX
));
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
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) {
276 /* if the our port doesn't do input anymore, forget it ... */
278 if (!_port
.parser()) {
282 bind_midi ((channel_t
) (msg
[0] & 0xf), eventType (msg
[0] & 0xF0), msg
[1]);
284 controllable
->LearningFinished ();
288 MIDIControllable::bind_midi (channel_t chn
, eventType ev
, MIDI::byte additional
)
292 drop_external_control ();
295 control_channel
= chn
;
296 control_additional
= additional
;
298 if (_port
.parser() == 0) {
302 Parser
& p
= *_port
.parser();
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.
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";
321 p
.channel_note_on
[chn_i
].connect_same_thread (midi_sense_connection
[0], boost::bind (&MIDIControllable::midi_sense_note_on
, this, _1
, _2
));
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";
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
;
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";
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";
350 MIDIControllable::send_feedback ()
354 if (!_learned
|| setting
|| !feedback
|| control_type
== none
) {
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
);
364 msg
[2] = (byte
) (control_to_midi(controllable
->get_value()));
367 _port
.write (msg
, 3, 0);
371 MIDIControllable::write_feedback (MIDI::byte
* buf
, int32_t& bufsize
, bool /*force*/)
373 if (control_type
!= none
&& feedback
&& bufsize
> 2) {
377 if (controllable
->is_gain_like()) {
378 gm
= (byte
) lrintf (gain_to_slider_position (controllable
->get_value()) * 127.0f
);
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 */
396 MIDIControllable::set_state (const XMLNode
& node
, int /*version*/)
398 const XMLProperty
* prop
;
401 if ((prop
= node
.property ("event")) != 0) {
402 sscanf (prop
->value().c_str(), "0x%x", &xx
);
403 control_type
= (MIDI::eventType
) xx
;
408 if ((prop
= node
.property ("channel")) != 0) {
409 sscanf (prop
->value().c_str(), "%d", &xx
);
410 control_channel
= (MIDI::channel_t
) xx
;
415 if ((prop
= node
.property ("additional")) != 0) {
416 sscanf (prop
->value().c_str(), "0x%x", &xx
);
417 control_additional
= (MIDI::byte
) xx
;
422 if ((prop
= node
.property ("feedback")) != 0) {
423 feedback
= (prop
->value() == "yes");
425 feedback
= true; // default
428 bind_midi (control_channel
, control_type
, control_additional
);
434 MIDIControllable::get_state ()
438 XMLNode
* node
= new XMLNode ("MIDIControllable");
440 if (_current_uri
.empty()) {
441 node
->add_property ("id", controllable
->id().to_s());
443 node
->add_property ("uri", _current_uri
);
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"));