*/
-#define __STDC_FORMAT_MACROS 1
#include <stdint.h>
#include <sstream>
#include <algorithm>
+#include <glibmm/miscutils.h>
+
#include "pbd/controllable_descriptor.h"
#include "pbd/error.h"
#include "pbd/failed_constructor.h"
#include "generic_midi_control_protocol.h"
#include "midicontrollable.h"
#include "midifunction.h"
+#include "midiaction.h"
using namespace ARDOUR;
using namespace PBD;
GenericMidiControlProtocol::GenericMidiControlProtocol (Session& s)
: ControlProtocol (s, _("Generic MIDI"), midi_ui_context())
+ , _motorised (false)
, gui (0)
{
-
- MIDI::Manager* mm = MIDI::Manager::instance();
-
- /* XXX it might be nice to run "control" through i18n, but thats a bit tricky because
- the name is defined in ardour.rc which is likely not internationalized.
- */
-
- _port = mm->port (Config->get_midi_port_name());
-
- if (_port == 0) {
- error << string_compose (_("no MIDI port named \"%1\" exists - generic MIDI control disabled"),
- Config->get_midi_port_name())
- << endmsg;
- throw failed_constructor();
- }
+ _input_port = MIDI::Manager::instance()->midi_input_port ();
+ _output_port = MIDI::Manager::instance()->midi_output_port ();
do_feedback = false;
_feedback_interval = 10000; // microseconds
tear_down_gui ();
}
+static const char * const midimap_env_variable_name = "ARDOUR_MIDIMAPS_PATH";
static const char* const midi_map_dir_name = "midi_maps";
static const char* const midi_map_suffix = ".map";
static sys::path
system_midi_map_search_path ()
{
- SearchPath spath(system_data_search_path());
+ bool midimap_path_defined = false;
+ sys::path spath_env (Glib::getenv (midimap_env_variable_name, midimap_path_defined));
+
+ if (midimap_path_defined) {
+ return spath_env;
+ }
+
+ SearchPath spath (system_data_search_path());
spath.add_subdirectory_to_paths(midi_map_dir_name);
// just return the first directory in the search path that exists
return;
}
- cerr << "Found " << midi_maps->size() << " MIDI maps along " << spath.to_string() << endl;
-
for (vector<string*>::iterator i = midi_maps->begin(); i != midi_maps->end(); ++i) {
string fullpath = *(*i);
delete *i;
}
functions.clear ();
+
+ for (MIDIActions::iterator i = actions.begin(); i != actions.end(); ++i) {
+ delete *i;
+ }
+ actions.clear ();
}
void
const int32_t bufsize = 16 * 1024; /* XXX too big */
MIDI::byte buf[bufsize];
int32_t bsize = bufsize;
- MIDI::byte* end = buf;
-
+
+ /* XXX: due to bugs in some ALSA / JACK MIDI bridges, we have to do separate
+ writes for each controllable here; if we send more than one MIDI message
+ in a single jack_midi_event_write then some bridges will only pass the
+ first on to ALSA.
+ */
for (MIDIControllables::iterator r = controllables.begin(); r != controllables.end(); ++r) {
- end = (*r)->write_feedback (end, bsize);
+ MIDI::byte* end = (*r)->write_feedback (buf, bsize);
+ if (end != buf) {
+ _output_port->write (buf, (int32_t) (end - buf), 0);
+ }
}
-
- if (end == buf) {
- return;
- }
-
- _port->write (buf, (int32_t) (end - buf), 0);
}
bool
}
if (!mc) {
- mc = new MIDIControllable (*_port, *c, false);
+ mc = new MIDIControllable (this, *_input_port, *c, false);
}
{
MIDI::byte value = control_number;
// Create a MIDIControllable
- MIDIControllable* mc = new MIDIControllable (*_port, *control, false);
+ MIDIControllable* mc = new MIDIControllable (this, *_input_port, *control, false);
// Remove any old binding for this midi channel/type/value pair
// Note: can't use delete_binding() here because we don't know the specific controllable we want to remove, only the midi information
node->add_property ("binding", _current_binding);
}
- XMLNode* children = new XMLNode (X_("controls"));
+ XMLNode* children = new XMLNode (X_("Controls"));
node->add_child_nocopy (*children);
{
Glib::Mutex::Lock lm2 (controllables_lock);
controllables.clear ();
- nlist = node.children(); // "controls"
+ nlist = node.children(); // "Controls"
if (nlist.empty()) {
return 0;
for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
if ((prop = (*niter)->property ("id")) != 0) {
+
+ cerr << "Looking for MIDI Controllable with ID " << prop->value() << endl;
ID id = prop->value ();
- c = session->controllable_by_id (id);
+ Controllable* c = Controllable::by_id (id);
+
+ cerr << "\tresult = " << c << endl;
if (c) {
- MIDIControllable* mc = new MIDIControllable (*_port, *c, false);
-
+ MIDIControllable* mc = new MIDIControllable (this, *_input_port, *c, false);
+
if (mc->set_state (**niter, version) == 0) {
controllables.push_back (mc);
}
return do_feedback;
}
-
-
-
int
GenericMidiControlProtocol::load_bindings (const string& xmlpath)
{
_bank_size = atoi (prop->value());
_current_bank = 0;
}
+
+ if ((prop = (*citer)->property ("motorised")) != 0) {
+ _motorised = string_is_affirmative (prop->value ());
+ } else {
+ _motorised = false;
+ }
}
if ((*citer)->name() == "Binding") {
if ((mf = create_function (*child)) != 0) {
functions.push_back (mf);
}
- }
+
+ } else if (child->property ("action")) {
+ MIDIAction* ma;
+
+ if ((ma = create_action (*child)) != 0) {
+ actions.push_back (ma);
+ }
+ }
}
}
ev = MIDI::on;
} else if ((prop = node.property (X_("pgm"))) != 0) {
ev = MIDI::program;
+ } else if ((prop = node.property (X_("pb"))) != 0) {
+ ev = MIDI::pitchbend;
} else {
return 0;
}
prop = node.property (X_("uri"));
uri = prop->value();
- MIDIControllable* mc = new MIDIControllable (*_port, momentary);
+ MIDIControllable* mc = new MIDIControllable (this, *_input_port, momentary);
if (mc->init (uri)) {
delete mc;
GenericMidiControlProtocol::reset_controllables ()
{
Glib::Mutex::Lock lm2 (controllables_lock);
-
- for (MIDIControllables::iterator iter = controllables.begin(); iter != controllables.end(); ++iter) {
+
+ for (MIDIControllables::iterator iter = controllables.begin(); iter != controllables.end(); ) {
MIDIControllable* existingBinding = (*iter);
+ MIDIControllables::iterator next = iter;
+ ++next;
if (!existingBinding->learned()) {
ControllableDescriptor& desc (existingBinding->descriptor());
desc.set_bank_offset (_current_bank * _bank_size);
}
+ /* its entirely possible that the session doesn't have
+ * the specified controllable (e.g. it has too few
+ * tracks). if we find this to be the case, drop any
+ * bindings that would be left without controllables.
+ */
+
boost::shared_ptr<Controllable> c = session->controllable_by_descriptor (desc);
- existingBinding->set_controllable (c.get());
+ if (c) {
+ existingBinding->set_controllable (c.get());
+ } else {
+ controllables.erase (iter);
+ }
}
+
+ iter = next;
}
}
MIDI::channel_t channel = 0;
string uri;
MIDI::eventType ev;
- MIDI::byte* sysex = 0;
- uint32_t sysex_size = 0;
+ MIDI::byte* data = 0;
+ uint32_t data_size = 0;
+ string argument;
if ((prop = node.property (X_("ctl"))) != 0) {
ev = MIDI::controller;
ev = MIDI::on;
} else if ((prop = node.property (X_("pgm"))) != 0) {
ev = MIDI::program;
- } else if ((prop = node.property (X_("sysex"))) != 0) {
+ } else if ((prop = node.property (X_("sysex"))) != 0 || (prop = node.property (X_("msg"))) != 0) {
+
+ if (prop->name() == X_("sysex")) {
+ ev = MIDI::sysex;
+ } else {
+ ev = MIDI::any;
+ }
- ev = MIDI::sysex;
int val;
uint32_t cnt;
return 0;
}
- sysex = new MIDI::byte[cnt];
- sysex_size = cnt;
+ data = new MIDI::byte[cnt];
+ data_size = cnt;
{
stringstream ss (prop->value());
cnt = 0;
while (ss >> val) {
- sysex[cnt++] = (MIDI::byte) val;
+ data[cnt++] = (MIDI::byte) val;
}
}
-
+
} else {
warning << "Binding ignored - unknown type" << endmsg;
return 0;
}
- if (sysex_size == 0) {
+ if (data_size == 0) {
if (sscanf (prop->value().c_str(), "%d", &intval) != 1) {
return 0;
}
}
}
+ if ((prop = node.property (X_("arg"))) != 0 || (prop = node.property (X_("argument"))) != 0 || (prop = node.property (X_("arguments"))) != 0) {
+ argument = prop->value ();
+ }
+
prop = node.property (X_("function"));
- MIDIFunction* mf = new MIDIFunction (*_port);
+ MIDIFunction* mf = new MIDIFunction (*_input_port);
- if (mf->init (*this, prop->value(), sysex, sysex_size)) {
+ if (mf->setup (*this, prop->value(), argument, data, data_size)) {
delete mf;
return 0;
}
return mf;
}
+MIDIAction*
+GenericMidiControlProtocol::create_action (const XMLNode& node)
+{
+ const XMLProperty* prop;
+ int intval;
+ MIDI::byte detail = 0;
+ MIDI::channel_t channel = 0;
+ string uri;
+ MIDI::eventType ev;
+ MIDI::byte* data = 0;
+ uint32_t data_size = 0;
+
+ if ((prop = node.property (X_("ctl"))) != 0) {
+ ev = MIDI::controller;
+ } else if ((prop = node.property (X_("note"))) != 0) {
+ ev = MIDI::on;
+ } else if ((prop = node.property (X_("pgm"))) != 0) {
+ ev = MIDI::program;
+ } else if ((prop = node.property (X_("sysex"))) != 0 || (prop = node.property (X_("msg"))) != 0) {
+
+ if (prop->name() == X_("sysex")) {
+ ev = MIDI::sysex;
+ } else {
+ ev = MIDI::any;
+ }
+
+ int val;
+ uint32_t cnt;
+
+ {
+ cnt = 0;
+ stringstream ss (prop->value());
+ ss << hex;
+
+ while (ss >> val) {
+ cnt++;
+ }
+ }
+
+ if (cnt == 0) {
+ return 0;
+ }
+
+ data = new MIDI::byte[cnt];
+ data_size = cnt;
+
+ {
+ stringstream ss (prop->value());
+ ss << hex;
+ cnt = 0;
+
+ while (ss >> val) {
+ data[cnt++] = (MIDI::byte) val;
+ }
+ }
+
+ } else {
+ warning << "Binding ignored - unknown type" << endmsg;
+ return 0;
+ }
+
+ if (data_size == 0) {
+ if (sscanf (prop->value().c_str(), "%d", &intval) != 1) {
+ return 0;
+ }
+
+ detail = (MIDI::byte) intval;
+
+ if ((prop = node.property (X_("channel"))) == 0) {
+ return 0;
+ }
+
+ if (sscanf (prop->value().c_str(), "%d", &intval) != 1) {
+ return 0;
+ }
+ channel = (MIDI::channel_t) intval;
+ /* adjust channel to zero-based counting */
+ if (channel > 0) {
+ channel -= 1;
+ }
+ }
+
+ prop = node.property (X_("action"));
+
+ MIDIAction* ma = new MIDIAction (*_input_port);
+
+ if (ma->init (*this, prop->value(), data, data_size)) {
+ delete ma;
+ return 0;
+ }
+
+ ma->bind_midi (channel, ev, detail);
+
+ return ma;
+}
+
void
GenericMidiControlProtocol::set_current_bank (uint32_t b)
{
reset_controllables ();
}
}
+
+void
+GenericMidiControlProtocol::set_motorised (bool m)
+{
+ _motorised = m;
+}