Catch new exception in other places.
[ardour.git] / libs / midi++2 / midnam_patch.cc
index 490baec8064720b6465a9f7911c4378e34e9f969..eb7274e69fec435e78f70646c75cf41766a8f87b 100644 (file)
     You should have received a copy of the GNU General Public License
     along with this program; if not, write to the Free Software
     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
-
-    $Id$
 */
 
+#include <stdlib.h>
+
 #include <algorithm>
 #include <iostream>
 
@@ -35,40 +35,54 @@ namespace MIDI
 
 namespace Name
 {
-       
+
 Patch::Patch (std::string name, uint8_t p_number, uint16_t b_number)
        : _name (name)
        , _id (p_number, b_number)
 {
 }
 
-int initialize_primary_key_from_commands (PatchPrimaryKey& id, const XMLNode* node)
+static int
+string_to_int(const XMLTree& tree, const std::string& str)
+{
+       char*     endptr = NULL;
+       const int i      = strtol(str.c_str(), &endptr, 10);
+       if (str.empty() || *endptr != '\0') {
+               PBD::error << string_compose("%1: Bad number `%2'", tree.filename(), str)
+                          << endmsg;
+       }
+       return i;
+}
+
+static int
+initialize_primary_key_from_commands (
+       const XMLTree& tree, PatchPrimaryKey& id, const XMLNode* node)
 {
-       id.bank_number = 0;
+       uint16_t bank    = 0;
+       uint8_t  program = 0;
 
        const XMLNodeList events = node->children();
        for (XMLNodeList::const_iterator i = events.begin(); i != events.end(); ++i) {
 
                XMLNode* node = *i;
                if (node->name() == "ControlChange") {
-                       string control = node->property("Control")->value();
-                       assert(control != "");
-                       string value = node->property("Value")->value();
-                       assert(value != "");
+                       const string& control = node->property("Control")->value();
+                       const string& value   = node->property("Value")->value();
 
                        if (control == "0") {
-                               id.bank_number |= (PBD::atoi (value)) << 7;
+                               bank |= string_to_int(tree, value) << 7;
                        } else if (control == "32") {
-                               id.bank_number |= PBD::atoi (value);
+                               bank |= string_to_int(tree, value);
                        }
 
                } else if (node->name() == "ProgramChange") {
-                       string number = node->property("Number")->value();
+                       const string& number = node->property("Number")->value();
                        assert(number != "");
-                       id.program_number = PBD::atoi(number);
+                       program = string_to_int(tree, number);
                }
        }
 
+       id = PatchPrimaryKey(program, bank);
        return 0;
 }
 
@@ -79,16 +93,16 @@ Patch::get_state (void)
 
        /* XXX this is totally wrong */
 
-       node->add_property("Number", string_compose ("%1", _id.program_number));
-       node->add_property("Name",   _name);
+       node->set_property("Number", _id.program());
+       node->set_property("Name",   _name);
 
        /*
-       typedef std::list< boost::shared_ptr< Evoral::MIDIEvent<double> > > PatchMidiCommands;
+       typedef std::list< boost::shared_ptr< Evoral::Event<Evoral::Beats> > > PatchMidiCommands;
        XMLNode* commands = node->add_child("PatchMIDICommands");
        for (PatchMidiCommands::const_iterator event = _patch_midi_commands.begin();
            event != _patch_midi_commands.end();
            ++event) {
-               commands->add_child_copy(*((((Evoral::MIDIEvent&)*event)).to_xml()));
+               commands->add_child_copy(Evoral::MIDIXML::midi_to_xml(*event));
        }
        */
 
@@ -96,39 +110,35 @@ Patch::get_state (void)
 }
 
 int
-Patch::set_state (const XMLTree&, const XMLNode& node)
+Patch::set_state (const XMLTree& tree, const XMLNode& node)
 {
        if (node.name() != "Patch") {
                cerr << "Incorrect node " << node.name() << " handed to Patch" << endl;
                return -1;
        }
 
-       const XMLProperty* prop = node.property ("Number");
+       /* Note there is a "Number" attribute, but it's really more like a label
+          and is often not numeric.  We currently do not use it. */
 
-       if (!prop) {
-               return -1;
+       const XMLProperty* program_change = node.property("ProgramChange");
+       if (program_change) {
+               _id = PatchPrimaryKey(string_to_int(tree, program_change->value()), _id.bank());
        }
-       _id.program_number = PBD::atoi (prop->value());
 
-       prop = node.property ("Name");
-
-       if (!prop) {
+       const XMLProperty* name = node.property("Name");
+       if (!name) {
                return -1;
        }
-       _name   = prop->value();
+       _name = name->value();
 
        XMLNode* commands = node.child("PatchMIDICommands");
-
        if (commands) {
-               if (initialize_primary_key_from_commands(_id, commands)) {
-                       return -1;
+               if (initialize_primary_key_from_commands(tree, _id, commands) &&
+                   !program_change) {
+                       return -1;  // Failed to find a program number anywhere
                }
-       } else {
-               string program_change = node.property("ProgramChange")->value();
-               assert(program_change.length());
-               _id.program_number = PBD::atoi(program_change);
        }
-       
+
        XMLNode* use_note_name_list = node.child("UsesNoteNameList");
        if (use_note_name_list) {
                _note_list_name = use_note_name_list->property ("Name")->value();
@@ -141,22 +151,26 @@ XMLNode&
 Note::get_state (void)
 {
        XMLNode* node = new XMLNode("Note");
-       node->add_property("Number", _number);
-       node->add_property("Name",   _name);
+       node->set_property("Number", _number);
+       node->set_property("Name",   _name);
 
        return *node;
 }
 
 int
-Note::set_state (const XMLTree&, const XMLNode& node)
+Note::set_state (const XMLTree& tree, const XMLNode& node)
 {
        assert(node.name() == "Note");
 
-       /* If the note number is junk, this will pull a number from the start, or
-          return zero if there isn't one.  Better error detection would be a good
-          idea, but the duplicate check in NoteNameList::set_state() will probably
-          catch really broken files anyway. */
-       _number = atoi(node.property("Number")->value().c_str());
+       const int num = string_to_int(tree, node.property("Number")->value());
+       if (num > 127) {
+               PBD::warning << string_compose("%1: Note number %2 (%3) out of range",
+                                              tree.filename(), num, _name)
+                            << endmsg;
+               return -1;
+       }
+
+       _number = num;
        _name   = node.property("Name")->value();
 
        return 0;
@@ -166,11 +180,27 @@ XMLNode&
 NoteNameList::get_state (void)
 {
        XMLNode* node = new XMLNode("NoteNameList");
-       node->add_property("Name", _name);
+       node->set_property("Name", _name);
 
        return *node;
 }
 
+static void
+add_note_from_xml (NoteNameList::Notes& notes, const XMLTree& tree, const XMLNode& node)
+{
+       boost::shared_ptr<Note> note(new Note());
+       if (!note->set_state (tree, node)) {
+               if (!notes[note->number()]) {
+                       notes[note->number()] = note;
+               } else {
+                       PBD::warning
+                               << string_compose("%1: Duplicate note number %2 (%3) ignored",
+                                                 tree.filename(), (int)note->number(), note->name())
+                               << endmsg;
+               }
+       }
+}
+
 int
 NoteNameList::set_state (const XMLTree& tree, const XMLNode& node)
 {
@@ -181,22 +211,19 @@ NoteNameList::set_state (const XMLTree& tree, const XMLNode& node)
 
        for (XMLNodeList::const_iterator i = node.children().begin();
             i != node.children().end(); ++i) {
-               if ((*i)->name() != "Note") {
-                       continue;
-               }
-               boost::shared_ptr<Note> note(new Note());
-               note->set_state (tree, *(*i));
-               if (note->number() > 127) {
-                       PBD::warning << string_compose("%1: Note number %2 (%3) out of range",
-                                                      tree.filename(), (int)note->number(), note->name())
-                                    << endmsg;
-               } else if (_notes[note->number()]) {
-                       PBD::warning <<
-                               string_compose("%1: Duplicate note number %2 (%3) ignored",
-                                              tree.filename(), (int)note->number(), note->name())
-                                    << endmsg;
-               } else {
-                       _notes[note->number()] = note;
+               if ((*i)->name() == "Note") {
+                       add_note_from_xml(_notes, tree, **i);
+               } else if ((*i)->name() == "NoteGroup") {
+                       for (XMLNodeList::const_iterator j = (*i)->children().begin();
+                            j != (*i)->children().end(); ++j) {
+                               if ((*j)->name() == "Note") {
+                                       add_note_from_xml(_notes, tree, **j);
+                               } else {
+                                       PBD::warning << string_compose("%1: Invalid NoteGroup child %2 ignored",
+                                                                      tree.filename(), (*j)->name())
+                                                    << endmsg;
+                               }
+                       }
                }
        }
 
@@ -207,21 +234,41 @@ XMLNode&
 Control::get_state (void)
 {
        XMLNode* node = new XMLNode("Control");
-       node->add_property("Type",   _type);
-       node->add_property("Number", _number);
-       node->add_property("Name",   _name);
+       node->set_property("Type",   _type);
+       node->set_property("Number", _number);
+       node->set_property("Name",   _name);
 
        return *node;
 }
 
 int
-Control::set_state (const XMLTree&, const XMLNode& node)
+Control::set_state (const XMLTree& tree, const XMLNode& node)
 {
        assert(node.name() == "Control");
-       _type   = node.property("Type")->value();
-       _number = node.property("Number")->value();
+       if (node.property("Type")) {
+               _type = node.property("Type")->value();
+       } else {
+               _type = "7bit";
+       }
+       _number = string_to_int(tree, node.property("Number")->value());
        _name   = node.property("Name")->value();
 
+       for (XMLNodeList::const_iterator i = node.children().begin();
+            i != node.children().end(); ++i) {
+               if ((*i)->name() == "Values") {
+                       // <Values> has Min and Max properties, but we don't care
+                       for (XMLNodeList::const_iterator j = (*i)->children().begin();
+                            j != (*i)->children().end(); ++j) {
+                               if ((*j)->name() == "ValueNameList") {
+                                       _value_name_list = boost::shared_ptr<ValueNameList>(new ValueNameList());
+                                       _value_name_list->set_state(tree, **j);
+                               } else if ((*j)->name() == "UsesValueNameList") {
+                                       _value_name_list_name = (*j)->property("Name")->value();
+                               }
+                       }
+               }
+       }
+
        return 0;
 }
 
@@ -229,7 +276,7 @@ XMLNode&
 ControlNameList::get_state (void)
 {
        XMLNode* node = new XMLNode("ControlNameList");
-       node->add_property("Name", _name);
+       node->set_property("Name", _name);
 
        return *node;
 }
@@ -240,23 +287,125 @@ ControlNameList::set_state (const XMLTree& tree, const XMLNode& node)
        assert(node.name() == "ControlNameList");
        _name = node.property("Name")->value();
 
+       _controls.clear();
        for (XMLNodeList::const_iterator i = node.children().begin();
             i != node.children().end(); ++i) {
-               if ((*i)->name() != "comment") {
+               if ((*i)->name() == "Control") {
                        boost::shared_ptr<Control> control(new Control());
                        control->set_state (tree, *(*i));
-                       _controls.push_back(control);
+                       if (_controls.find(control->number()) == _controls.end()) {
+                               _controls.insert(make_pair(control->number(), control));
+                       } else {
+                               PBD::warning << string_compose("%1: Duplicate control %2 ignored",
+                                                              tree.filename(), control->number())
+                                            << endmsg;
+                       }
                }
        }
 
        return 0;
 }
 
+boost::shared_ptr<const Control>
+ControlNameList::control(uint16_t num) const
+{
+       Controls::const_iterator i = _controls.find(num);
+       if (i != _controls.end()) {
+               return i->second;
+       }
+       return boost::shared_ptr<const Control>();
+}
+
+XMLNode&
+Value::get_state (void)
+{
+       XMLNode* node = new XMLNode("Value");
+       node->set_property("Number", _number);
+       node->set_property("Name",   _name);
+
+       return *node;
+}
+
+int
+Value::set_state (const XMLTree& tree, const XMLNode& node)
+{
+       assert(node.name() == "Value");
+       _number = string_to_int(tree, node.property("Number")->value());
+       _name   = node.property("Name")->value();
+
+       return 0;
+}
+
+XMLNode&
+ValueNameList::get_state (void)
+{
+       XMLNode* node = new XMLNode("ValueNameList");
+       node->set_property("Name", _name);
+
+       return *node;
+}
+
+int
+ValueNameList::set_state (const XMLTree& tree, const XMLNode& node)
+{
+       assert(node.name() == "ValueNameList");
+       const XMLProperty* name_prop = node.property("Name");
+       if (name_prop) {
+               // May be anonymous if written inline within a single <Control> tag
+               _name = name_prop->value();
+       }
+
+       _values.clear();
+       for (XMLNodeList::const_iterator i = node.children().begin();
+            i != node.children().end(); ++i) {
+               if ((*i)->name() == "Value") {
+                       boost::shared_ptr<Value> value(new Value());
+                       value->set_state (tree, *(*i));
+                       if (_values.find(value->number()) == _values.end()) {
+                               _values.insert(make_pair(value->number(), value));
+                       } else {
+                               PBD::warning << string_compose("%1: Duplicate value %2 ignored",
+                                                              tree.filename(), value->number())
+                                            << endmsg;
+                       }
+               }
+       }
+
+       return 0;
+}
+
+boost::shared_ptr<const Value>
+ValueNameList::value(uint16_t num) const
+{
+       Values::const_iterator i = _values.find(num);
+       if (i != _values.end()) {
+               return i->second;
+       }
+       return boost::shared_ptr<const Value>();
+}
+
+boost::shared_ptr<const Value>
+ValueNameList::max_value_below(uint16_t num) const
+{
+       Values::const_iterator i = _values.lower_bound(num);
+       if (i->first == num) {
+               // Exact match
+               return i->second;
+       } else if (i == _values.begin()) {
+               // No value is < num
+               return boost::shared_ptr<const Value>();
+       } else {
+               // Found the smallest element >= num, so the previous one is our result
+               --i;
+               return i->second;
+       }
+}
+
 XMLNode&
 PatchBank::get_state (void)
 {
        XMLNode* node = new XMLNode("PatchBank");
-       node->add_property("Name",   _name);
+       node->set_property("Name",   _name);
        XMLNode* patch_name_list = node->add_child("PatchNameList");
        for (PatchNameList::iterator patch = _patch_name_list.begin();
            patch != _patch_name_list.end();
@@ -276,10 +425,10 @@ PatchBank::set_state (const XMLTree& tree, const XMLNode& node)
        XMLNode* commands = node.child("MIDICommands");
        if (commands) {
                PatchPrimaryKey id (0, 0);
-               if (initialize_primary_key_from_commands (id, commands)) {
+               if (initialize_primary_key_from_commands (tree, id, commands)) {
                        return -1;
                }
-               _number = id.bank_number;
+               _number = id.bank();
        }
 
        XMLNode* patch_name_list = node.child("PatchNameList");
@@ -325,14 +474,14 @@ operator<< (std::ostream& os, const ChannelNameSet& cns)
           << "List size " << cns._patch_list.size() << endl
           << "Patch list name = [" << cns._patch_list_name << ']' << endl
           << "Available channels : ";
-       for (set<uint8_t>::iterator x = cns._available_for_channels.begin(); x != cns._available_for_channels.end(); ++x) {
+       for (set<uint8_t>::const_iterator x = cns._available_for_channels.begin(); x != cns._available_for_channels.end(); ++x) {
                os << (int) (*x) << ' ';
        }
        os << endl;
-       
+
        for (ChannelNameSet::PatchBanks::const_iterator pbi = cns._patch_banks.begin(); pbi != cns._patch_banks.end(); ++pbi) {
                os << "\tPatch Bank " << (*pbi)->name() << " with " << (*pbi)->patch_name_list().size() << " patches\n";
-               for (PatchBank::PatchNameList::const_iterator pni = (*pbi)->patch_name_list().begin(); pni != (*pbi)->patch_name_list().end(); ++pni) {
+               for (PatchNameList::const_iterator pni = (*pbi)->patch_name_list().begin(); pni != (*pbi)->patch_name_list().end(); ++pni) {
                        os << "\t\tPatch name " << (*pni)->name() << " prog " << (int) (*pni)->program_number() << " bank " << (*pni)->bank_number() << endl;
                }
        }
@@ -344,14 +493,14 @@ void
 ChannelNameSet::set_patch_banks (const ChannelNameSet::PatchBanks& pb)
 {
        _patch_banks = pb;
-       
+
        _patch_map.clear ();
        _patch_list.clear ();
        _patch_list_name = "";
        _available_for_channels.clear ();
-       
+
        for (PatchBanks::const_iterator pbi = _patch_banks.begin(); pbi != _patch_banks.end(); ++pbi) {
-               for (PatchBank::PatchNameList::const_iterator pni = (*pbi)->patch_name_list().begin(); pni != (*pbi)->patch_name_list().end(); ++pni) {
+               for (PatchNameList::const_iterator pni = (*pbi)->patch_name_list().begin(); pni != (*pbi)->patch_name_list().end(); ++pni) {
                        _patch_map[(*pni)->patch_primary_key()] = (*pni);
                        _patch_list.push_back ((*pni)->patch_primary_key());
                }
@@ -363,9 +512,9 @@ ChannelNameSet::set_patch_banks (const ChannelNameSet::PatchBanks& pb)
 }
 
 void
-ChannelNameSet::use_patch_name_list (const PatchBank::PatchNameList& pnl)
+ChannelNameSet::use_patch_name_list (const PatchNameList& pnl)
 {
-       for (PatchBank::PatchNameList::const_iterator p = pnl.begin(); p != pnl.end(); ++p) {
+       for (PatchNameList::const_iterator p = pnl.begin(); p != pnl.end(); ++p) {
                _patch_map[(*p)->patch_primary_key()] = (*p);
                _patch_list.push_back ((*p)->patch_primary_key());
        }
@@ -375,7 +524,7 @@ XMLNode&
 ChannelNameSet::get_state (void)
 {
        XMLNode* node = new XMLNode("ChannelNameSet");
-       node->add_property("Name",   _name);
+       node->set_property("Name",   _name);
 
        XMLNode* available_for_channels = node->add_child("AvailableForChannels");
        assert(available_for_channels);
@@ -384,12 +533,12 @@ ChannelNameSet::get_state (void)
                XMLNode* available_channel = available_for_channels->add_child("AvailableChannel");
                assert(available_channel);
 
-               available_channel->add_property("Channel", (long) channel);
+               available_channel->set_property("Channel", channel);
 
                if (_available_for_channels.find(channel) != _available_for_channels.end()) {
-                       available_channel->add_property("Available", "true");
+                       available_channel->set_property("Available", "true");
                } else {
-                       available_channel->add_property("Available", "false");
+                       available_channel->set_property("Available", "false");
                }
        }
 
@@ -406,7 +555,7 @@ int
 ChannelNameSet::set_state (const XMLTree& tree, const XMLNode& node)
 {
        assert(node.name() == "ChannelNameSet");
-       _name   = node.property("Name")->value();
+       _name = node.property("Name")->value();
 
        const XMLNodeList children = node.children();
        for (XMLNodeList::const_iterator i = children.begin(); i != children.end(); ++i) {
@@ -415,24 +564,31 @@ ChannelNameSet::set_state (const XMLTree& tree, const XMLNode& node)
                if (node->name() == "AvailableForChannels") {
                        boost::shared_ptr<XMLSharedNodeList> channels =
                                tree.find("//AvailableChannel[@Available = 'true']/@Channel", node);
-                       for(XMLSharedNodeList::const_iterator i = channels->begin();
+                       for (XMLSharedNodeList::const_iterator i = channels->begin();
                            i != channels->end();
                            ++i) {
-                               _available_for_channels.insert(atoi((*i)->attribute_value().c_str()));
+                               try {
+                                       _available_for_channels.insert(
+                                                       string_to_int(tree, (*i)->attribute_value()));
+                               } catch (XMLException &e) {
+                                       cerr << "ChannelNameSet::set_state: " << e.what () << endl;
+                               }
                        }
-               }
-
-               if (node->name() == "PatchBank") {
+               } else if (node->name() == "PatchBank") {
                        boost::shared_ptr<PatchBank> bank (new PatchBank ());
                        bank->set_state(tree, *node);
                        _patch_banks.push_back(bank);
-                       const PatchBank::PatchNameList& patches = bank->patch_name_list();
-                       for (PatchBank::PatchNameList::const_iterator patch = patches.begin();
+                       const PatchNameList& patches = bank->patch_name_list();
+                       for (PatchNameList::const_iterator patch = patches.begin();
                             patch != patches.end();
                             ++patch) {
                                _patch_map[(*patch)->patch_primary_key()] = *patch;
                                _patch_list.push_back((*patch)->patch_primary_key());
                        }
+               } else if (node->name() == "UsesNoteNameList") {
+                       _note_list_name = node->property ("Name")->value();
+               } else if (node->name() == "UsesControlNameList") {
+                       _control_list_name = node->property ("Name")->value();
                }
        }
 
@@ -448,11 +604,11 @@ CustomDeviceMode::set_state(const XMLTree& tree, const XMLNode& a_node)
 
        boost::shared_ptr<XMLSharedNodeList> channel_name_set_assignments =
                tree.find("//ChannelNameSetAssign", const_cast<XMLNode *>(&a_node));
-       for(XMLSharedNodeList::const_iterator i = channel_name_set_assignments->begin();
+       for (XMLSharedNodeList::const_iterator i = channel_name_set_assignments->begin();
            i != channel_name_set_assignments->end();
            ++i) {
-               int channel = atoi((*i)->property("Channel")->value().c_str());
-               string name_set = (*i)->property("NameSet")->value();
+               const int     channel  = string_to_int(tree, (*i)->property("Channel")->value());
+               const string& name_set = (*i)->property("NameSet")->value();
                assert( 1 <= channel && channel <= 16 );
                _channel_name_set_assignments[channel - 1] = name_set;
        }
@@ -463,37 +619,64 @@ XMLNode&
 CustomDeviceMode::get_state(void)
 {
        XMLNode* custom_device_mode = new XMLNode("CustomDeviceMode");
-       custom_device_mode->add_property("Name",   _name);
+       custom_device_mode->set_property("Name",   _name);
        XMLNode* channel_name_set_assignments =
                custom_device_mode->add_child("ChannelNameSetAssignments");
        for (int i = 0; i < 15 && !_channel_name_set_assignments[i].empty(); i++) {
                XMLNode* channel_name_set_assign =
                        channel_name_set_assignments->add_child("ChannelNameSetAssign");
-               channel_name_set_assign->add_property("Channel", i + 1);
-               channel_name_set_assign->add_property("NameSet", _channel_name_set_assignments[i]);
+               channel_name_set_assign->set_property("Channel", i + 1);
+               channel_name_set_assign->set_property("NameSet", _channel_name_set_assignments[i]);
        }
 
        return *custom_device_mode;
 }
 
-boost::shared_ptr<CustomDeviceMode> 
-MasterDeviceNames::custom_device_mode_by_name(std::string mode_name)
+boost::shared_ptr<const ValueNameList>
+MasterDeviceNames::value_name_list_by_control(const std::string& mode, uint8_t channel, uint8_t number)
+{
+       boost::shared_ptr<ChannelNameSet> chan_names = channel_name_set_by_channel(mode, channel);
+       if (!chan_names) {
+               return boost::shared_ptr<const ValueNameList>();
+       }
+
+       boost::shared_ptr<ControlNameList> control_names = control_name_list(chan_names->control_list_name());
+       if (!control_names) {
+               return boost::shared_ptr<const ValueNameList>();
+       }
+
+       boost::shared_ptr<const Control> control = control_names->control(number);
+       if (!control) {
+               return boost::shared_ptr<const ValueNameList>();
+       }
+
+       if (!control->value_name_list_name().empty()) {
+               return value_name_list(control->value_name_list_name());
+       } else {
+               return control->value_name_list();
+       }
+}
+
+boost::shared_ptr<CustomDeviceMode>
+MasterDeviceNames::custom_device_mode_by_name(const std::string& mode_name)
 {
        return _custom_device_modes[mode_name];
 }
 
-boost::shared_ptr<ChannelNameSet> 
-MasterDeviceNames::channel_name_set_by_device_mode_and_channel(std::string mode, uint8_t channel)
+boost::shared_ptr<ChannelNameSet>
+MasterDeviceNames::channel_name_set_by_channel(const std::string& mode, uint8_t channel)
 {
        boost::shared_ptr<CustomDeviceMode> cdm = custom_device_mode_by_name(mode);
        boost::shared_ptr<ChannelNameSet> cns =  _channel_name_sets[cdm->channel_name_set_name_by_channel(channel)];
        return cns;
 }
 
-boost::shared_ptr<Patch> 
-MasterDeviceNames::find_patch(std::string mode, uint8_t channel, const PatchPrimaryKey& key) 
+boost::shared_ptr<Patch>
+MasterDeviceNames::find_patch(const std::string& mode, uint8_t channel, const PatchPrimaryKey& key)
 {
-       return channel_name_set_by_device_mode_and_channel(mode, channel)->find_patch(key);
+       boost::shared_ptr<ChannelNameSet> cns = channel_name_set_by_channel(mode, channel);
+       if (!cns) return boost::shared_ptr<Patch>();
+       return cns->find_patch(key);
 }
 
 boost::shared_ptr<ChannelNameSet>
@@ -506,6 +689,26 @@ MasterDeviceNames::channel_name_set(const std::string& name)
        return boost::shared_ptr<ChannelNameSet>();
 }
 
+boost::shared_ptr<ControlNameList>
+MasterDeviceNames::control_name_list(const std::string& name)
+{
+       ControlNameLists::const_iterator i = _control_name_lists.find(name);
+       if (i != _control_name_lists.end()) {
+               return i->second;
+       }
+       return boost::shared_ptr<ControlNameList>();
+}
+
+boost::shared_ptr<ValueNameList>
+MasterDeviceNames::value_name_list(const std::string& name)
+{
+       ValueNameLists::const_iterator i = _value_name_lists.find(name);
+       if (i != _value_name_lists.end()) {
+               return i->second;
+       }
+       return boost::shared_ptr<ValueNameList>();
+}
+
 boost::shared_ptr<NoteNameList>
 MasterDeviceNames::note_name_list(const std::string& name)
 {
@@ -527,14 +730,21 @@ MasterDeviceNames::note_name(const std::string& mode_name,
                return "";
        }
 
+       boost::shared_ptr<const NoteNameList> note_names;
        boost::shared_ptr<const Patch> patch(
                find_patch(mode_name, channel, PatchPrimaryKey(program, bank)));
-       if (!patch) {
-               return "";
+       if (patch) {
+               note_names = note_name_list(patch->note_list_name());
        }
 
-       boost::shared_ptr<const NoteNameList> note_names(
-               note_name_list(patch->note_list_name()));
+       if (!note_names) {
+               /* No note names specific to this patch, check the ChannelNameSet */
+               boost::shared_ptr<ChannelNameSet> chan_names = channel_name_set_by_channel(
+                       mode_name, channel);
+               if (chan_names) {
+                       note_names = note_name_list(chan_names->note_list_name());
+               }
+       }
        if (!note_names) {
                return "";
        }
@@ -593,7 +803,7 @@ MasterDeviceNames::set_state(const XMLTree& tree, const XMLNode&)
             ++i) {
                boost::shared_ptr<NoteNameList> note_name_list(new NoteNameList());
                note_name_list->set_state (tree, *(*i));
-               _note_name_lists[(*i)->property ("Name")->value()] = note_name_list;
+               _note_name_lists[note_name_list->name()] = note_name_list;
        }
 
        // ControlNameLists
@@ -603,7 +813,17 @@ MasterDeviceNames::set_state(const XMLTree& tree, const XMLNode&)
             ++i) {
                boost::shared_ptr<ControlNameList> control_name_list(new ControlNameList());
                control_name_list->set_state (tree, *(*i));
-               _control_name_lists.push_back(control_name_list);
+               _control_name_lists[control_name_list->name()] = control_name_list;
+       }
+
+       // ValueNameLists
+       boost::shared_ptr<XMLSharedNodeList> value_name_lists = tree.find("/child::MIDINameDocument/child::MasterDeviceNames/child::ValueNameList");
+       for (XMLSharedNodeList::iterator i = value_name_lists->begin();
+            i != value_name_lists->end();
+            ++i) {
+               boost::shared_ptr<ValueNameList> value_name_list(new ValueNameList());
+               value_name_list->set_state (tree, *(*i));
+               _value_name_lists[value_name_list->name()] = value_name_list;
        }
 
        // global/post-facto PatchNameLists
@@ -612,7 +832,7 @@ MasterDeviceNames::set_state(const XMLTree& tree, const XMLNode&)
             i != patch_name_lists->end();
             ++i) {
 
-               PatchBank::PatchNameList patch_name_list;
+               PatchNameList patch_name_list;
                const XMLNodeList patches = (*i)->children();
 
                for (XMLNodeList::const_iterator p = patches.begin(); p != patches.end(); ++p) {
@@ -635,7 +855,7 @@ MasterDeviceNames::set_state(const XMLTree& tree, const XMLNode&)
                PatchNameLists::iterator p;
 
                for (ChannelNameSet::PatchBanks::iterator pb = pbs.begin(); pb != pbs.end(); ++pb) {
-                       std::string pln = (*pb)->patch_list_name();
+                       const std::string& pln = (*pb)->patch_list_name();
                        if (!pln.empty()) {
                                if ((p = _patch_name_lists.find (pln)) != _patch_name_lists.end()) {
                                        if ((*pb)->set_patch_name_list (p->second)) {
@@ -661,14 +881,16 @@ MasterDeviceNames::get_state(void)
        return nothing;
 }
 
-MIDINameDocument::MIDINameDocument (const string& filename)
+MIDINameDocument::MIDINameDocument (const string& file_path)
+       : _file_path(file_path)
 {
-       if (!_document.read (filename)) {
+       XMLTree document;
+       if (!document.read (file_path)) {
                throw failed_constructor ();
        }
 
-       _document.set_filename (filename);
-       set_state (_document, *_document.root());
+       document.set_filename (file_path);
+       set_state (document, *document.root());
 }
 
 int
@@ -681,7 +903,7 @@ MIDINameDocument::set_state (const XMLTree& tree, const XMLNode&)
                error << "No author information in MIDNAM file" << endmsg;
                return -1;
        }
-       
+
        if (author->front()->children().size() > 0) {
                _author = author->front()->children().front()->content();
        }
@@ -705,7 +927,7 @@ MIDINameDocument::set_state (const XMLTree& tree, const XMLNode&)
                        _master_device_names_list.insert(
                                std::pair<std::string, boost::shared_ptr<MasterDeviceNames> >
                                (*model,      master_device_names));
-                       
+
                        _all_models.insert(*model);
                }
        }