/*
Copyright (C) 1998-2006 Paul Davis
-
+
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
#include <cstdio> /* for sprintf, sigh */
#include <climits>
-#include <pbd/error.h>
-#include <pbd/xml++.h>
-#include <midi++/port.h>
-#include <midi++/channel.h>
+#include "pbd/error.h"
+#include "pbd/xml++.h"
+#include "midi++/port.h"
+#include "midi++/channel.h"
+#include "ardour/automation_control.h"
#include "midicontrollable.h"
: controllable (c), _port (p), bistate (is_bistate)
{
setting = false;
- last_written = 0; // got a better idea ?
+ last_value = 0; // got a better idea ?
control_type = none;
_control_description = "MIDI Control: none";
control_additional = (byte) -1;
connections = 0;
feedback = true; // for now
-
+
/* use channel 0 ("1") as the initial channel */
midi_rebind (0);
/* stop listening for incoming messages, but retain
our existing event + type information.
*/
-
+
if (connections > 0) {
midi_sense_connection[0].disconnect ();
- }
-
+ }
+
if (connections > 1) {
midi_sense_connection[1].disconnect ();
}
-
+
connections = 0;
midi_learn_connection.disconnect ();
-
+
}
void
void
MIDIControllable::drop_external_control ()
{
- cerr << "Dropping existing control using " << connections << " connections\n";
-
if (connections > 0) {
midi_sense_connection[0].disconnect ();
- }
+ }
if (connections > 1) {
midi_sense_connection[1].disconnect ();
}
control_additional = (byte) -1;
}
-void
-MIDIControllable::midi_sense_note_on (Parser &p, EventTwoBytes *tb)
+float
+MIDIControllable::control_to_midi(float val)
+{
+ float control_min = 0.0f;
+ float control_max = 1.0f;
+ ARDOUR::AutomationControl* ac = dynamic_cast<ARDOUR::AutomationControl*>(&controllable);
+ if (ac) {
+ control_min = ac->parameter().min();
+ control_max = ac->parameter().max();
+ }
+
+ const float control_range = control_max - control_min;
+ const float midi_range = 127.0f; // TODO: NRPN etc.
+
+ return (val - control_min) / control_range * midi_range;
+}
+
+float
+MIDIControllable::midi_to_control(float val)
+{
+ float control_min = 0.0f;
+ float control_max = 1.0f;
+ ARDOUR::AutomationControl* ac = dynamic_cast<ARDOUR::AutomationControl*>(&controllable);
+ if (ac) {
+ control_min = ac->parameter().min();
+ control_max = ac->parameter().max();
+ }
+
+ const float control_range = control_max - control_min;
+ const float midi_range = 127.0f; // TODO: NRPN etc.
+
+ return val / midi_range * control_range + control_min;
+}
+
+void
+MIDIControllable::midi_sense_note_on (Parser &p, EventTwoBytes *tb)
{
midi_sense_note (p, tb, true);
}
-void
-MIDIControllable::midi_sense_note_off (Parser &p, EventTwoBytes *tb)
+void
+MIDIControllable::midi_sense_note_off (Parser &p, EventTwoBytes *tb)
{
midi_sense_note (p, tb, false);
}
void
-MIDIControllable::midi_sense_note (Parser &p, EventTwoBytes *msg, bool is_on)
+MIDIControllable::midi_sense_note (Parser &, EventTwoBytes *msg, bool is_on)
{
if (!bistate) {
controllable.set_value (msg->note_number/127.0);
/* Note: parser handles the use of zero velocity to
mean note off. if we get called with is_on=true, then we
- got a *real* note on.
+ got a *real* note on.
*/
if (msg->note_number == control_additional) {
controllable.set_value (is_on ? 1 : 0);
}
}
+
+ last_value = (MIDI::byte) (controllable.get_value() * 127.0); // to prevent feedback fights
}
void
MIDIControllable::midi_sense_controller (Parser &, EventTwoBytes *msg)
{
+ if (controllable.touching()) {
+ return; // to prevent feedback fights when e.g. dragging a UI slider
+ }
+
if (control_additional == msg->controller_number) {
if (!bistate) {
- controllable.set_value (msg->value/127.0);
+ controllable.set_value (midi_to_control(msg->value));
} else {
if (msg->value > 64.0) {
controllable.set_value (1);
controllable.set_value (0);
}
}
+
+ last_value = (MIDI::byte) (control_to_midi(controllable.get_value())); // to prevent feedback fights
}
}
void
-MIDIControllable::midi_sense_program_change (Parser &p, byte msg)
+MIDIControllable::midi_sense_program_change (Parser &, byte msg)
{
/* XXX program change messages make no sense for bistates */
if (!bistate) {
controllable.set_value (msg/127.0);
- }
+ last_value = (MIDI::byte) (controllable.get_value() * 127.0); // to prevent feedback fights
+ }
}
void
-MIDIControllable::midi_sense_pitchbend (Parser &p, pitchbend_t pb)
+MIDIControllable::midi_sense_pitchbend (Parser &, pitchbend_t pb)
{
/* pitchbend messages make no sense for bistates */
/* XXX gack - get rid of assumption about typeof pitchbend_t */
controllable.set_value ((pb/(float) SHRT_MAX));
-}
+ last_value = (MIDI::byte) (controllable.get_value() * 127.0); // to prevent feedback fights
+}
void
-MIDIControllable::midi_receiver (Parser &p, byte *msg, size_t len)
+MIDIControllable::midi_receiver (Parser &, byte *msg, size_t /*len*/)
{
/* we only respond to channel messages */
if (_port.input() == 0) {
return;
}
-
+
Parser& p = *_port.input();
int chn_i = chn;
midi_sense_connection[0] = p.channel_note_on[chn_i].connect
(mem_fun (*this, &MIDIControllable::midi_sense_note_on));
if (bistate) {
- midi_sense_connection[1] = p.channel_note_off[chn_i].connect
+ midi_sense_connection[1] = p.channel_note_off[chn_i].connect
(mem_fun (*this, &MIDIControllable::midi_sense_note_off));
connections = 2;
} else {
break;
case MIDI::controller:
- midi_sense_connection[0] = p.channel_controller[chn_i].connect
+ midi_sense_connection[0] = p.channel_controller[chn_i].connect
(mem_fun (*this, &MIDIControllable::midi_sense_controller));
connections = 1;
snprintf (buf, sizeof (buf), "MIDI control: Controller %d", control_additional);
case MIDI::program:
if (!bistate) {
midi_sense_connection[0] = p.channel_program_change[chn_i].connect
- (mem_fun (*this,
+ (mem_fun (*this,
&MIDIControllable::midi_sense_program_change));
connections = 1;
_control_description = "MIDI control: ProgramChange";
default:
break;
}
-
- cerr << "MIDI bound with " << connections << endl;
}
void
if (setting || !feedback || control_type == none) {
return;
}
-
- msg[0] = (control_type & 0xF0) | (control_channel & 0xF);
+
+ msg[0] = (control_type & 0xF0) | (control_channel & 0xF);
msg[1] = control_additional;
- msg[2] = (byte) (controllable.get_value() * 127.0f);
+ msg[2] = (byte) (control_to_midi(controllable.get_value()));
- //_port.write (msg, 3);
+ _port.write (msg, 3, 0);
}
MIDI::byte*
-MIDIControllable::write_feedback (MIDI::byte* buf, int32_t& bufsize, bool force)
+MIDIControllable::write_feedback (MIDI::byte* buf, int32_t& bufsize, bool /*force*/)
{
if (control_type != none && feedback && bufsize > 2) {
- MIDI::byte gm = (MIDI::byte) (controllable.get_value() * 127.0);
-
- if (gm != last_written) {
+ MIDI::byte gm = (MIDI::byte) (control_to_midi(controllable.get_value()));
+
+ if (gm != last_value) {
*buf++ = (0xF0 & control_type) | (0xF & control_channel);
*buf++ = control_additional; /* controller number */
*buf++ = gm;
- last_written = gm;
+ last_value = gm;
bufsize -= 3;
}
}
-
+
return buf;
}
-int
-MIDIControllable::set_state (const XMLNode& node)
+int
+MIDIControllable::set_state (const XMLNode& node, int /*version*/)
{
const XMLProperty* prop;
int xx;
}
bind_midi (control_channel, control_type, control_additional);
-
+
return 0;
}