X-Git-Url: https://main.carlh.net/gitweb/?a=blobdiff_plain;f=libs%2Fmidi%2B%2B2%2Fmidnam_patch.cc;h=cb1e28508b93eecb397b011c8ad6219579551e85;hb=c8c6bca6587450ff64303dbc994a4cd28d6ce7aa;hp=9dd5bc33db4ca3c3a77308d39e11c8c53175e8df;hpb=658bb3ccd43658de18fbd43cd91a8e66650e27a7;p=ardour.git diff --git a/libs/midi++2/midnam_patch.cc b/libs/midi++2/midnam_patch.cc index 9dd5bc33db..cb1e28508b 100644 --- a/libs/midi++2/midnam_patch.cc +++ b/libs/midi++2/midnam_patch.cc @@ -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,11 +93,11 @@ Patch::get_state (void) /* XXX this is totally wrong */ - node->add_property("Number", string_compose ("%1", _id.program_number)); + node->add_property("Number", string_compose ("%1", _id.program())); node->add_property("Name", _name); /* - typedef std::list< boost::shared_ptr< Evoral::MIDIEvent > > PatchMidiCommands; + typedef std::list< boost::shared_ptr< Evoral::MIDIEvent > > PatchMidiCommands; XMLNode* commands = node->add_child("PatchMIDICommands"); for (PatchMidiCommands::const_iterator event = _patch_midi_commands.begin(); event != _patch_midi_commands.end(); @@ -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,7 +151,7 @@ XMLNode& Note::get_state (void) { XMLNode* node = new XMLNode("Note"); - node->add_property("Number", _number + 1); + node->add_property("Number", _number); node->add_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; @@ -251,6 +253,22 @@ Control::set_state (const XMLTree& tree, const XMLNode& node) _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") { + // 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(new ValueNameList()); + _value_name_list->set_state(tree, **j); + } else if ((*j)->name() == "UsesValueNameList") { + _value_name_list_name = (*j)->property("Name")->value(); + } + } + } + } + return 0; } @@ -298,6 +316,91 @@ ControlNameList::control(uint16_t num) const return boost::shared_ptr(); } +XMLNode& +Value::get_state (void) +{ + XMLNode* node = new XMLNode("Value"); + node->add_property("Number", _number); + node->add_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->add_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 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(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 +ValueNameList::value(uint16_t num) const +{ + Values::const_iterator i = _values.find(num); + if (i != _values.end()) { + return i->second; + } + return boost::shared_ptr(); +} + +boost::shared_ptr +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(); + } else { + // Found the smallest element >= num, so the previous one is our result + --i; + return i->second; + } +} + XMLNode& PatchBank::get_state (void) { @@ -325,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"); @@ -375,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) { @@ -390,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); @@ -525,24 +628,49 @@ CustomDeviceMode::get_state(void) return *custom_device_mode; } -boost::shared_ptr +boost::shared_ptr +MasterDeviceNames::value_name_list_by_control(const std::string& mode, uint8_t channel, uint8_t number) +{ + boost::shared_ptr chan_names = channel_name_set_by_channel(mode, channel); + if (!chan_names) { + return boost::shared_ptr(); + } + + boost::shared_ptr control_names = control_name_list(chan_names->control_list_name()); + if (!control_names) { + return boost::shared_ptr(); + } + + boost::shared_ptr control = control_names->control(number); + if (!control) { + return boost::shared_ptr(); + } + + 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 MasterDeviceNames::custom_device_mode_by_name(const std::string& mode_name) { return _custom_device_modes[mode_name]; } -boost::shared_ptr -MasterDeviceNames::channel_name_set_by_device_mode_and_channel(const std::string& mode, uint8_t channel) +boost::shared_ptr +MasterDeviceNames::channel_name_set_by_channel(const std::string& mode, uint8_t channel) { boost::shared_ptr cdm = custom_device_mode_by_name(mode); boost::shared_ptr cns = _channel_name_sets[cdm->channel_name_set_name_by_channel(channel)]; return cns; } -boost::shared_ptr -MasterDeviceNames::find_patch(const std::string& mode, uint8_t channel, const PatchPrimaryKey& key) +boost::shared_ptr +MasterDeviceNames::find_patch(const std::string& mode, uint8_t channel, const PatchPrimaryKey& key) { - boost::shared_ptr cns = channel_name_set_by_device_mode_and_channel(mode, channel); + boost::shared_ptr cns = channel_name_set_by_channel(mode, channel); if (!cns) return boost::shared_ptr(); return cns->find_patch(key); } @@ -567,6 +695,16 @@ MasterDeviceNames::control_name_list(const std::string& name) return boost::shared_ptr(); } +boost::shared_ptr +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(); +} + boost::shared_ptr MasterDeviceNames::note_name_list(const std::string& name) { @@ -588,17 +726,16 @@ MasterDeviceNames::note_name(const std::string& mode_name, return ""; } + boost::shared_ptr note_names; boost::shared_ptr 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 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 chan_names = channel_name_set_by_device_mode_and_channel( + boost::shared_ptr chan_names = channel_name_set_by_channel( mode_name, channel); if (chan_names) { note_names = note_name_list(chan_names->note_list_name()); @@ -675,6 +812,16 @@ MasterDeviceNames::set_state(const XMLTree& tree, const XMLNode&) _control_name_lists[control_name_list->name()] = control_name_list; } + // ValueNameLists + boost::shared_ptr 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 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 boost::shared_ptr patch_name_lists = tree.find("/child::MIDINameDocument/child::MasterDeviceNames/child::PatchNameList"); for (XMLSharedNodeList::iterator i = patch_name_lists->begin(); @@ -730,14 +877,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 @@ -750,7 +899,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(); } @@ -774,7 +923,7 @@ MIDINameDocument::set_state (const XMLTree& tree, const XMLNode&) _master_device_names_list.insert( std::pair > (*model, master_device_names)); - + _all_models.insert(*model); } }