2 Copyright (C) 2008 Hans Baier
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 #include "midi++/midnam_patch.h"
25 #include "pbd/compose.h"
26 #include "pbd/convert.h"
27 #include "pbd/error.h"
28 #include "pbd/failed_constructor.h"
39 Patch::Patch (std::string name, uint8_t p_number, uint16_t b_number)
41 , _id (p_number, b_number)
46 string_to_int(const XMLTree& tree, const std::string& str)
49 const int i = strtol(str.c_str(), &endptr, 10);
50 if (str.empty() || *endptr != '\0') {
51 PBD::error << string_compose("%1: Bad number `%2'", tree.filename(), str)
58 initialize_primary_key_from_commands (
59 const XMLTree& tree, PatchPrimaryKey& id, const XMLNode* node)
63 const XMLNodeList events = node->children();
64 for (XMLNodeList::const_iterator i = events.begin(); i != events.end(); ++i) {
67 if (node->name() == "ControlChange") {
68 const string& control = node->property("Control")->value();
69 const string& value = node->property("Value")->value();
72 id.bank_number |= string_to_int(tree, value) << 7;
73 } else if (control == "32") {
74 id.bank_number |= string_to_int(tree, value);
77 } else if (node->name() == "ProgramChange") {
78 const string& number = node->property("Number")->value();
80 id.program_number = string_to_int(tree, number);
88 Patch::get_state (void)
90 XMLNode* node = new XMLNode("Patch");
92 /* XXX this is totally wrong */
94 node->add_property("Number", string_compose ("%1", _id.program_number));
95 node->add_property("Name", _name);
98 typedef std::list< boost::shared_ptr< Evoral::MIDIEvent<double> > > PatchMidiCommands;
99 XMLNode* commands = node->add_child("PatchMIDICommands");
100 for (PatchMidiCommands::const_iterator event = _patch_midi_commands.begin();
101 event != _patch_midi_commands.end();
103 commands->add_child_copy(*((((Evoral::MIDIEvent&)*event)).to_xml()));
111 Patch::set_state (const XMLTree& tree, const XMLNode& node)
113 if (node.name() != "Patch") {
114 cerr << "Incorrect node " << node.name() << " handed to Patch" << endl;
118 /* Note there is a "Number" attribute, but it's really more like a label
119 and is often not numeric. We currently do not use it. */
121 const XMLProperty* program_change = node.property("ProgramChange");
122 if (program_change) {
123 _id.program_number = string_to_int(tree, program_change->value());
126 const XMLProperty* name = node.property("Name");
130 _name = name->value();
132 XMLNode* commands = node.child("PatchMIDICommands");
134 if (initialize_primary_key_from_commands(tree, _id, commands) &&
136 return -1; // Failed to find a program number anywhere
140 XMLNode* use_note_name_list = node.child("UsesNoteNameList");
141 if (use_note_name_list) {
142 _note_list_name = use_note_name_list->property ("Name")->value();
149 Note::get_state (void)
151 XMLNode* node = new XMLNode("Note");
152 node->add_property("Number", _number + 1);
153 node->add_property("Name", _name);
159 Note::set_state (const XMLTree& tree, const XMLNode& node)
161 assert(node.name() == "Note");
163 const int num = string_to_int(tree, node.property("Number")->value());
164 if (num < 1 || num > 128) {
165 PBD::warning << string_compose("%1: Note number %2 (%3) out of range",
166 tree.filename(), num, _name)
172 _name = node.property("Name")->value();
178 NoteNameList::get_state (void)
180 XMLNode* node = new XMLNode("NoteNameList");
181 node->add_property("Name", _name);
187 add_note_from_xml (NoteNameList::Notes& notes, const XMLTree& tree, const XMLNode& node)
189 boost::shared_ptr<Note> note(new Note());
190 if (!note->set_state (tree, node)) {
191 if (!notes[note->number()]) {
192 notes[note->number()] = note;
195 << string_compose("%1: Duplicate note number %2 (%3) ignored",
196 tree.filename(), (int)note->number(), note->name())
203 NoteNameList::set_state (const XMLTree& tree, const XMLNode& node)
205 assert(node.name() == "NoteNameList");
206 _name = node.property("Name")->value();
210 for (XMLNodeList::const_iterator i = node.children().begin();
211 i != node.children().end(); ++i) {
212 if ((*i)->name() == "Note") {
213 add_note_from_xml(_notes, tree, **i);
214 } else if ((*i)->name() == "NoteGroup") {
215 for (XMLNodeList::const_iterator j = (*i)->children().begin();
216 j != (*i)->children().end(); ++j) {
217 if ((*j)->name() == "Note") {
218 add_note_from_xml(_notes, tree, **j);
220 PBD::warning << string_compose("%1: Invalid NoteGroup child %2 ignored",
221 tree.filename(), (*j)->name())
232 Control::get_state (void)
234 XMLNode* node = new XMLNode("Control");
235 node->add_property("Type", _type);
236 node->add_property("Number", _number);
237 node->add_property("Name", _name);
243 Control::set_state (const XMLTree& tree, const XMLNode& node)
245 assert(node.name() == "Control");
246 if (node.property("Type")) {
247 _type = node.property("Type")->value();
251 _number = string_to_int(tree, node.property("Number")->value());
252 _name = node.property("Name")->value();
258 ControlNameList::get_state (void)
260 XMLNode* node = new XMLNode("ControlNameList");
261 node->add_property("Name", _name);
267 ControlNameList::set_state (const XMLTree& tree, const XMLNode& node)
269 assert(node.name() == "ControlNameList");
270 _name = node.property("Name")->value();
273 for (XMLNodeList::const_iterator i = node.children().begin();
274 i != node.children().end(); ++i) {
275 if ((*i)->name() == "Control") {
276 boost::shared_ptr<Control> control(new Control());
277 control->set_state (tree, *(*i));
278 if (_controls.find(control->number()) == _controls.end()) {
279 _controls.insert(make_pair(control->number(), control));
281 PBD::warning << string_compose("%1: Duplicate control %2 ignored",
282 tree.filename(), control->number())
291 boost::shared_ptr<const Control>
292 ControlNameList::control(uint16_t num) const
294 Controls::const_iterator i = _controls.find(num);
295 if (i != _controls.end()) {
298 return boost::shared_ptr<const Control>();
302 PatchBank::get_state (void)
304 XMLNode* node = new XMLNode("PatchBank");
305 node->add_property("Name", _name);
306 XMLNode* patch_name_list = node->add_child("PatchNameList");
307 for (PatchNameList::iterator patch = _patch_name_list.begin();
308 patch != _patch_name_list.end();
310 patch_name_list->add_child_nocopy((*patch)->get_state());
317 PatchBank::set_state (const XMLTree& tree, const XMLNode& node)
319 assert(node.name() == "PatchBank");
320 _name = node.property("Name")->value();
322 XMLNode* commands = node.child("MIDICommands");
324 PatchPrimaryKey id (0, 0);
325 if (initialize_primary_key_from_commands (tree, id, commands)) {
328 _number = id.bank_number;
331 XMLNode* patch_name_list = node.child("PatchNameList");
333 if (patch_name_list) {
334 const XMLNodeList patches = patch_name_list->children();
335 for (XMLNodeList::const_iterator i = patches.begin(); i != patches.end(); ++i) {
336 boost::shared_ptr<Patch> patch (new Patch (string(), 0, _number));
337 patch->set_state(tree, *(*i));
338 _patch_name_list.push_back(patch);
341 XMLNode* use_patch_name_list = node.child ("UsesPatchNameList");
342 if (use_patch_name_list) {
343 _patch_list_name = use_patch_name_list->property ("Name")->value();
345 error << "Patch without patch name list - patchfile will be ignored" << endmsg;
354 PatchBank::set_patch_name_list (const PatchNameList& pnl)
356 _patch_name_list = pnl;
357 _patch_list_name = "";
359 for (PatchNameList::iterator p = _patch_name_list.begin(); p != _patch_name_list.end(); p++) {
360 (*p)->set_bank_number (_number);
367 operator<< (std::ostream& os, const ChannelNameSet& cns)
369 os << "Channel Name Set: name = " << cns._name << endl
370 << "Map size " << cns._patch_map.size () << endl
371 << "List size " << cns._patch_list.size() << endl
372 << "Patch list name = [" << cns._patch_list_name << ']' << endl
373 << "Available channels : ";
374 for (set<uint8_t>::iterator x = cns._available_for_channels.begin(); x != cns._available_for_channels.end(); ++x) {
375 os << (int) (*x) << ' ';
379 for (ChannelNameSet::PatchBanks::const_iterator pbi = cns._patch_banks.begin(); pbi != cns._patch_banks.end(); ++pbi) {
380 os << "\tPatch Bank " << (*pbi)->name() << " with " << (*pbi)->patch_name_list().size() << " patches\n";
381 for (PatchNameList::const_iterator pni = (*pbi)->patch_name_list().begin(); pni != (*pbi)->patch_name_list().end(); ++pni) {
382 os << "\t\tPatch name " << (*pni)->name() << " prog " << (int) (*pni)->program_number() << " bank " << (*pni)->bank_number() << endl;
390 ChannelNameSet::set_patch_banks (const ChannelNameSet::PatchBanks& pb)
395 _patch_list.clear ();
396 _patch_list_name = "";
397 _available_for_channels.clear ();
399 for (PatchBanks::const_iterator pbi = _patch_banks.begin(); pbi != _patch_banks.end(); ++pbi) {
400 for (PatchNameList::const_iterator pni = (*pbi)->patch_name_list().begin(); pni != (*pbi)->patch_name_list().end(); ++pni) {
401 _patch_map[(*pni)->patch_primary_key()] = (*pni);
402 _patch_list.push_back ((*pni)->patch_primary_key());
406 for (uint8_t n = 0; n < 16; ++n) {
407 _available_for_channels.insert (n);
412 ChannelNameSet::use_patch_name_list (const PatchNameList& pnl)
414 for (PatchNameList::const_iterator p = pnl.begin(); p != pnl.end(); ++p) {
415 _patch_map[(*p)->patch_primary_key()] = (*p);
416 _patch_list.push_back ((*p)->patch_primary_key());
421 ChannelNameSet::get_state (void)
423 XMLNode* node = new XMLNode("ChannelNameSet");
424 node->add_property("Name", _name);
426 XMLNode* available_for_channels = node->add_child("AvailableForChannels");
427 assert(available_for_channels);
429 for (uint8_t channel = 0; channel < 16; ++channel) {
430 XMLNode* available_channel = available_for_channels->add_child("AvailableChannel");
431 assert(available_channel);
433 available_channel->add_property("Channel", (long) channel);
435 if (_available_for_channels.find(channel) != _available_for_channels.end()) {
436 available_channel->add_property("Available", "true");
438 available_channel->add_property("Available", "false");
442 for (PatchBanks::iterator patch_bank = _patch_banks.begin();
443 patch_bank != _patch_banks.end();
445 node->add_child_nocopy((*patch_bank)->get_state());
452 ChannelNameSet::set_state (const XMLTree& tree, const XMLNode& node)
454 assert(node.name() == "ChannelNameSet");
455 _name = node.property("Name")->value();
457 const XMLNodeList children = node.children();
458 for (XMLNodeList::const_iterator i = children.begin(); i != children.end(); ++i) {
461 if (node->name() == "AvailableForChannels") {
462 boost::shared_ptr<XMLSharedNodeList> channels =
463 tree.find("//AvailableChannel[@Available = 'true']/@Channel", node);
464 for (XMLSharedNodeList::const_iterator i = channels->begin();
465 i != channels->end();
467 _available_for_channels.insert(
468 string_to_int(tree, (*i)->attribute_value()));
470 } else if (node->name() == "PatchBank") {
471 boost::shared_ptr<PatchBank> bank (new PatchBank ());
472 bank->set_state(tree, *node);
473 _patch_banks.push_back(bank);
474 const PatchNameList& patches = bank->patch_name_list();
475 for (PatchNameList::const_iterator patch = patches.begin();
476 patch != patches.end();
478 _patch_map[(*patch)->patch_primary_key()] = *patch;
479 _patch_list.push_back((*patch)->patch_primary_key());
481 } else if (node->name() == "UsesNoteNameList") {
482 _note_list_name = node->property ("Name")->value();
483 } else if (node->name() == "UsesControlNameList") {
484 _control_list_name = node->property ("Name")->value();
492 CustomDeviceMode::set_state(const XMLTree& tree, const XMLNode& a_node)
494 assert(a_node.name() == "CustomDeviceMode");
496 _name = a_node.property("Name")->value();
498 boost::shared_ptr<XMLSharedNodeList> channel_name_set_assignments =
499 tree.find("//ChannelNameSetAssign", const_cast<XMLNode *>(&a_node));
500 for (XMLSharedNodeList::const_iterator i = channel_name_set_assignments->begin();
501 i != channel_name_set_assignments->end();
503 const int channel = string_to_int(tree, (*i)->property("Channel")->value());
504 const string& name_set = (*i)->property("NameSet")->value();
505 assert( 1 <= channel && channel <= 16 );
506 _channel_name_set_assignments[channel - 1] = name_set;
512 CustomDeviceMode::get_state(void)
514 XMLNode* custom_device_mode = new XMLNode("CustomDeviceMode");
515 custom_device_mode->add_property("Name", _name);
516 XMLNode* channel_name_set_assignments =
517 custom_device_mode->add_child("ChannelNameSetAssignments");
518 for (int i = 0; i < 15 && !_channel_name_set_assignments[i].empty(); i++) {
519 XMLNode* channel_name_set_assign =
520 channel_name_set_assignments->add_child("ChannelNameSetAssign");
521 channel_name_set_assign->add_property("Channel", i + 1);
522 channel_name_set_assign->add_property("NameSet", _channel_name_set_assignments[i]);
525 return *custom_device_mode;
528 boost::shared_ptr<CustomDeviceMode>
529 MasterDeviceNames::custom_device_mode_by_name(const std::string& mode_name)
531 return _custom_device_modes[mode_name];
534 boost::shared_ptr<ChannelNameSet>
535 MasterDeviceNames::channel_name_set_by_device_mode_and_channel(const std::string& mode, uint8_t channel)
537 boost::shared_ptr<CustomDeviceMode> cdm = custom_device_mode_by_name(mode);
538 boost::shared_ptr<ChannelNameSet> cns = _channel_name_sets[cdm->channel_name_set_name_by_channel(channel)];
542 boost::shared_ptr<Patch>
543 MasterDeviceNames::find_patch(const std::string& mode, uint8_t channel, const PatchPrimaryKey& key)
545 return channel_name_set_by_device_mode_and_channel(mode, channel)->find_patch(key);
548 boost::shared_ptr<ChannelNameSet>
549 MasterDeviceNames::channel_name_set(const std::string& name)
551 ChannelNameSets::const_iterator i = _channel_name_sets.find(name);
552 if (i != _channel_name_sets.end()) {
555 return boost::shared_ptr<ChannelNameSet>();
558 boost::shared_ptr<ControlNameList>
559 MasterDeviceNames::control_name_list(const std::string& name)
561 ControlNameLists::const_iterator i = _control_name_lists.find(name);
562 if (i != _control_name_lists.end()) {
565 return boost::shared_ptr<ControlNameList>();
568 boost::shared_ptr<NoteNameList>
569 MasterDeviceNames::note_name_list(const std::string& name)
571 NoteNameLists::const_iterator i = _note_name_lists.find(name);
572 if (i != _note_name_lists.end()) {
575 return boost::shared_ptr<NoteNameList>();
579 MasterDeviceNames::note_name(const std::string& mode_name,
589 boost::shared_ptr<const Patch> patch(
590 find_patch(mode_name, channel, PatchPrimaryKey(program, bank)));
595 boost::shared_ptr<const NoteNameList> note_names(
596 note_name_list(patch->note_list_name()));
598 /* No note names specific to this patch, check the ChannelNameSet */
599 boost::shared_ptr<ChannelNameSet> chan_names = channel_name_set_by_device_mode_and_channel(
602 note_names = note_name_list(chan_names->note_list_name());
609 boost::shared_ptr<const Note> note(note_names->notes()[number]);
610 return note ? note->name() : "";
614 MasterDeviceNames::set_state(const XMLTree& tree, const XMLNode&)
617 boost::shared_ptr<XMLSharedNodeList> manufacturer = tree.find("//Manufacturer");
618 assert(manufacturer->size() == 1);
619 _manufacturer = manufacturer->front()->children().front()->content();
622 boost::shared_ptr<XMLSharedNodeList> models = tree.find("//Model");
623 assert(models->size() >= 1);
624 for (XMLSharedNodeList::iterator i = models->begin();
627 const XMLNodeList& contents = (*i)->children();
628 assert(contents.size() == 1);
629 XMLNode * content = *(contents.begin());
630 assert(content->is_content());
631 _models.insert(content->content());
635 boost::shared_ptr<XMLSharedNodeList> custom_device_modes = tree.find("//CustomDeviceMode");
636 for (XMLSharedNodeList::iterator i = custom_device_modes->begin();
637 i != custom_device_modes->end();
639 boost::shared_ptr<CustomDeviceMode> custom_device_mode(new CustomDeviceMode());
640 custom_device_mode->set_state(tree, *(*i));
642 _custom_device_modes[custom_device_mode->name()] = custom_device_mode;
643 _custom_device_mode_names.push_back(custom_device_mode->name());
647 boost::shared_ptr<XMLSharedNodeList> channel_name_sets = tree.find("//ChannelNameSet");
648 for (XMLSharedNodeList::iterator i = channel_name_sets->begin();
649 i != channel_name_sets->end();
651 boost::shared_ptr<ChannelNameSet> channel_name_set(new ChannelNameSet());
652 channel_name_set->set_state(tree, *(*i));
653 _channel_name_sets[channel_name_set->name()] = channel_name_set;
657 boost::shared_ptr<XMLSharedNodeList> note_name_lists = tree.find("//NoteNameList");
658 for (XMLSharedNodeList::iterator i = note_name_lists->begin();
659 i != note_name_lists->end();
661 boost::shared_ptr<NoteNameList> note_name_list(new NoteNameList());
662 note_name_list->set_state (tree, *(*i));
663 _note_name_lists[note_name_list->name()] = note_name_list;
667 boost::shared_ptr<XMLSharedNodeList> control_name_lists = tree.find("//ControlNameList");
668 for (XMLSharedNodeList::iterator i = control_name_lists->begin();
669 i != control_name_lists->end();
671 boost::shared_ptr<ControlNameList> control_name_list(new ControlNameList());
672 control_name_list->set_state (tree, *(*i));
673 _control_name_lists[control_name_list->name()] = control_name_list;
676 // global/post-facto PatchNameLists
677 boost::shared_ptr<XMLSharedNodeList> patch_name_lists = tree.find("/child::MIDINameDocument/child::MasterDeviceNames/child::PatchNameList");
678 for (XMLSharedNodeList::iterator i = patch_name_lists->begin();
679 i != patch_name_lists->end();
682 PatchNameList patch_name_list;
683 const XMLNodeList patches = (*i)->children();
685 for (XMLNodeList::const_iterator p = patches.begin(); p != patches.end(); ++p) {
686 boost::shared_ptr<Patch> patch (new Patch ());
687 patch->set_state(tree, *(*p));
688 patch_name_list.push_back(patch);
691 if (!patch_name_list.empty()) {
692 _patch_name_lists[(*i)->property ("Name")->value()] = patch_name_list;
696 /* now traverse patches and hook up anything that used UsePatchNameList
697 * to the right patch list
700 for (ChannelNameSets::iterator cns = _channel_name_sets.begin(); cns != _channel_name_sets.end(); ++cns) {
701 ChannelNameSet::PatchBanks pbs = cns->second->patch_banks();
702 PatchNameLists::iterator p;
704 for (ChannelNameSet::PatchBanks::iterator pb = pbs.begin(); pb != pbs.end(); ++pb) {
705 const std::string& pln = (*pb)->patch_list_name();
707 if ((p = _patch_name_lists.find (pln)) != _patch_name_lists.end()) {
708 if ((*pb)->set_patch_name_list (p->second)) {
711 cns->second->use_patch_name_list (p->second);
713 error << string_compose ("Patch list name %1 was not found - patch file ignored", pln) << endmsg;
725 MasterDeviceNames::get_state(void)
727 static XMLNode nothing("<nothing>");
731 MIDINameDocument::MIDINameDocument (const string& filename)
733 if (!_document.read (filename)) {
734 throw failed_constructor ();
737 _document.set_filename (filename);
738 set_state (_document, *_document.root());
742 MIDINameDocument::set_state (const XMLTree& tree, const XMLNode&)
746 boost::shared_ptr<XMLSharedNodeList> author = tree.find("//Author");
747 if (author->size() < 1) {
748 error << "No author information in MIDNAM file" << endmsg;
752 if (author->front()->children().size() > 0) {
753 _author = author->front()->children().front()->content();
758 boost::shared_ptr<XMLSharedNodeList> master_device_names_list = tree.find ("//MasterDeviceNames");
760 for (XMLSharedNodeList::iterator i = master_device_names_list->begin();
761 i != master_device_names_list->end();
763 boost::shared_ptr<MasterDeviceNames> master_device_names(new MasterDeviceNames());
765 if (master_device_names->set_state(tree, *(*i))) {
769 for (MasterDeviceNames::Models::const_iterator model = master_device_names->models().begin();
770 model != master_device_names->models().end();
772 _master_device_names_list.insert(
773 std::pair<std::string, boost::shared_ptr<MasterDeviceNames> >
774 (*model, master_device_names));
776 _all_models.insert(*model);
784 MIDINameDocument::get_state(void)
786 static XMLNode nothing("<nothing>");
790 boost::shared_ptr<MasterDeviceNames>
791 MIDINameDocument::master_device_names(const std::string& model)
793 MasterDeviceNamesList::const_iterator m = _master_device_names_list.find(model);
794 if (m != _master_device_names_list.end()) {
795 return boost::shared_ptr<MasterDeviceNames>(m->second);
797 return boost::shared_ptr<MasterDeviceNames>();
800 const char* general_midi_program_names[128] = {
801 "Acoustic Grand Piano",
802 "Bright Acoustic Piano",
803 "Electric Grand Piano",
825 "Acoustic Guitar (nylon)",
826 "Acoustic Guitar (steel)",
827 "Electric Guitar (jazz)",
828 "Electric Guitar (clean)",
829 "Electric Guitar (muted)",
834 "Electric Bass (finger)",
835 "Electric Bass (pick)",
883 "Lead 3 (calliope lead)",
884 "Lead 4 (chiff lead)",
888 "Lead 8 (bass + lead)",