Catch new exception in other places.
[ardour.git] / libs / midi++2 / midnam_patch.cc
index 252804e4669857bf0b0e90c2d1d7bc7a02851b3f..eb7274e69fec435e78f70646c75cf41766a8f87b 100644 (file)
@@ -35,7 +35,7 @@ namespace MIDI
 
 namespace Name
 {
-       
+
 Patch::Patch (std::string name, uint8_t p_number, uint16_t b_number)
        : _name (name)
        , _id (p_number, b_number)
@@ -58,7 +58,8 @@ 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) {
@@ -69,18 +70,19 @@ initialize_primary_key_from_commands (
                        const string& value   = node->property("Value")->value();
 
                        if (control == "0") {
-                               id.bank_number |= string_to_int(tree, value) << 7;
+                               bank |= string_to_int(tree, value) << 7;
                        } else if (control == "32") {
-                               id.bank_number |= string_to_int(tree, value);
+                               bank |= string_to_int(tree, value);
                        }
 
                } else if (node->name() == "ProgramChange") {
                        const string& number = node->property("Number")->value();
                        assert(number != "");
-                       id.program_number = string_to_int(tree, number);
+                       program = string_to_int(tree, number);
                }
        }
 
+       id = PatchPrimaryKey(program, bank);
        return 0;
 }
 
@@ -91,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));
        }
        */
 
@@ -120,7 +122,7 @@ Patch::set_state (const XMLTree& tree, const XMLNode& node)
 
        const XMLProperty* program_change = node.property("ProgramChange");
        if (program_change) {
-               _id.program_number = string_to_int(tree, program_change->value());
+               _id = PatchPrimaryKey(string_to_int(tree, program_change->value()), _id.bank());
        }
 
        const XMLProperty* name = node.property("Name");
@@ -136,7 +138,7 @@ Patch::set_state (const XMLTree& tree, const XMLNode& node)
                        return -1;  // Failed to find a program number anywhere
                }
        }
-       
+
        XMLNode* use_note_name_list = node.child("UsesNoteNameList");
        if (use_note_name_list) {
                _note_list_name = use_note_name_list->property ("Name")->value();
@@ -149,8 +151,8 @@ XMLNode&
 Note::get_state (void)
 {
        XMLNode* node = new XMLNode("Note");
-       node->add_property("Number", _number + 1);
-       node->add_property("Name",   _name);
+       node->set_property("Number", _number);
+       node->set_property("Name",   _name);
 
        return *node;
 }
@@ -161,14 +163,14 @@ Note::set_state (const XMLTree& tree, const XMLNode& node)
        assert(node.name() == "Note");
 
        const int num = string_to_int(tree, node.property("Number")->value());
-       if (num < 1 || num > 128) {
+       if (num > 127) {
                PBD::warning << string_compose("%1: Note number %2 (%3) out of range",
                                               tree.filename(), num, _name)
                             << endmsg;
                return -1;
        }
 
-       _number = num - 1;
+       _number = num;
        _name   = node.property("Name")->value();
 
        return 0;
@@ -178,7 +180,7 @@ XMLNode&
 NoteNameList::get_state (void)
 {
        XMLNode* node = new XMLNode("NoteNameList");
-       node->add_property("Name", _name);
+       node->set_property("Name", _name);
 
        return *node;
 }
@@ -232,9 +234,9 @@ 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;
 }
@@ -274,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;
 }
@@ -318,8 +320,8 @@ XMLNode&
 Value::get_state (void)
 {
        XMLNode* node = new XMLNode("Value");
-       node->add_property("Number", _number);
-       node->add_property("Name",   _name);
+       node->set_property("Number", _number);
+       node->set_property("Name",   _name);
 
        return *node;
 }
@@ -338,7 +340,7 @@ XMLNode&
 ValueNameList::get_state (void)
 {
        XMLNode* node = new XMLNode("ValueNameList");
-       node->add_property("Name", _name);
+       node->set_property("Name", _name);
 
        return *node;
 }
@@ -403,7 +405,7 @@ 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();
@@ -426,7 +428,7 @@ PatchBank::set_state (const XMLTree& tree, const XMLNode& node)
                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");
@@ -476,7 +478,7 @@ operator<< (std::ostream& os, const ChannelNameSet& cns)
                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 (PatchNameList::const_iterator pni = (*pbi)->patch_name_list().begin(); pni != (*pbi)->patch_name_list().end(); ++pni) {
@@ -491,12 +493,12 @@ 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 (PatchNameList::const_iterator pni = (*pbi)->patch_name_list().begin(); pni != (*pbi)->patch_name_list().end(); ++pni) {
                        _patch_map[(*pni)->patch_primary_key()] = (*pni);
@@ -522,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);
@@ -531,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");
                }
        }
 
@@ -565,8 +567,12 @@ ChannelNameSet::set_state (const XMLTree& tree, const XMLNode& node)
                        for (XMLSharedNodeList::const_iterator i = channels->begin();
                            i != channels->end();
                            ++i) {
-                               _available_for_channels.insert(
-                                       string_to_int(tree, (*i)->attribute_value()));
+                               try {
+                                       _available_for_channels.insert(
+                                                       string_to_int(tree, (*i)->attribute_value()));
+                               } catch (XMLException &e) {
+                                       cerr << "ChannelNameSet::set_state: " << e.what () << endl;
+                               }
                        }
                } else if (node->name() == "PatchBank") {
                        boost::shared_ptr<PatchBank> bank (new PatchBank ());
@@ -613,14 +619,14 @@ 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;
@@ -651,13 +657,13 @@ MasterDeviceNames::value_name_list_by_control(const std::string& mode, uint8_t c
        }
 }
 
-boost::shared_ptr<CustomDeviceMode> 
+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> 
+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);
@@ -665,8 +671,8 @@ MasterDeviceNames::channel_name_set_by_channel(const std::string& mode, uint8_t
        return cns;
 }
 
-boost::shared_ptr<Patch> 
-MasterDeviceNames::find_patch(const 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)
 {
        boost::shared_ptr<ChannelNameSet> cns = channel_name_set_by_channel(mode, channel);
        if (!cns) return boost::shared_ptr<Patch>();
@@ -724,14 +730,13 @@ 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(
@@ -876,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
@@ -896,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();
        }
@@ -920,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);
                }
        }