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&, const XMLNode& node)
245 assert(node.name() == "Control");
246 _type = node.property("Type")->value();
247 _number = node.property("Number")->value();
248 _name = node.property("Name")->value();
254 ControlNameList::get_state (void)
256 XMLNode* node = new XMLNode("ControlNameList");
257 node->add_property("Name", _name);
263 ControlNameList::set_state (const XMLTree& tree, const XMLNode& node)
265 assert(node.name() == "ControlNameList");
266 _name = node.property("Name")->value();
268 for (XMLNodeList::const_iterator i = node.children().begin();
269 i != node.children().end(); ++i) {
270 if ((*i)->name() != "comment") {
271 boost::shared_ptr<Control> control(new Control());
272 control->set_state (tree, *(*i));
273 _controls.push_back(control);
281 PatchBank::get_state (void)
283 XMLNode* node = new XMLNode("PatchBank");
284 node->add_property("Name", _name);
285 XMLNode* patch_name_list = node->add_child("PatchNameList");
286 for (PatchNameList::iterator patch = _patch_name_list.begin();
287 patch != _patch_name_list.end();
289 patch_name_list->add_child_nocopy((*patch)->get_state());
296 PatchBank::set_state (const XMLTree& tree, const XMLNode& node)
298 assert(node.name() == "PatchBank");
299 _name = node.property("Name")->value();
301 XMLNode* commands = node.child("MIDICommands");
303 PatchPrimaryKey id (0, 0);
304 if (initialize_primary_key_from_commands (tree, id, commands)) {
307 _number = id.bank_number;
310 XMLNode* patch_name_list = node.child("PatchNameList");
312 if (patch_name_list) {
313 const XMLNodeList patches = patch_name_list->children();
314 for (XMLNodeList::const_iterator i = patches.begin(); i != patches.end(); ++i) {
315 boost::shared_ptr<Patch> patch (new Patch (string(), 0, _number));
316 patch->set_state(tree, *(*i));
317 _patch_name_list.push_back(patch);
320 XMLNode* use_patch_name_list = node.child ("UsesPatchNameList");
321 if (use_patch_name_list) {
322 _patch_list_name = use_patch_name_list->property ("Name")->value();
324 error << "Patch without patch name list - patchfile will be ignored" << endmsg;
333 PatchBank::set_patch_name_list (const PatchNameList& pnl)
335 _patch_name_list = pnl;
336 _patch_list_name = "";
338 for (PatchNameList::iterator p = _patch_name_list.begin(); p != _patch_name_list.end(); p++) {
339 (*p)->set_bank_number (_number);
346 operator<< (std::ostream& os, const ChannelNameSet& cns)
348 os << "Channel Name Set: name = " << cns._name << endl
349 << "Map size " << cns._patch_map.size () << endl
350 << "List size " << cns._patch_list.size() << endl
351 << "Patch list name = [" << cns._patch_list_name << ']' << endl
352 << "Available channels : ";
353 for (set<uint8_t>::iterator x = cns._available_for_channels.begin(); x != cns._available_for_channels.end(); ++x) {
354 os << (int) (*x) << ' ';
358 for (ChannelNameSet::PatchBanks::const_iterator pbi = cns._patch_banks.begin(); pbi != cns._patch_banks.end(); ++pbi) {
359 os << "\tPatch Bank " << (*pbi)->name() << " with " << (*pbi)->patch_name_list().size() << " patches\n";
360 for (PatchBank::PatchNameList::const_iterator pni = (*pbi)->patch_name_list().begin(); pni != (*pbi)->patch_name_list().end(); ++pni) {
361 os << "\t\tPatch name " << (*pni)->name() << " prog " << (int) (*pni)->program_number() << " bank " << (*pni)->bank_number() << endl;
369 ChannelNameSet::set_patch_banks (const ChannelNameSet::PatchBanks& pb)
374 _patch_list.clear ();
375 _patch_list_name = "";
376 _available_for_channels.clear ();
378 for (PatchBanks::const_iterator pbi = _patch_banks.begin(); pbi != _patch_banks.end(); ++pbi) {
379 for (PatchBank::PatchNameList::const_iterator pni = (*pbi)->patch_name_list().begin(); pni != (*pbi)->patch_name_list().end(); ++pni) {
380 _patch_map[(*pni)->patch_primary_key()] = (*pni);
381 _patch_list.push_back ((*pni)->patch_primary_key());
385 for (uint8_t n = 0; n < 16; ++n) {
386 _available_for_channels.insert (n);
391 ChannelNameSet::use_patch_name_list (const PatchBank::PatchNameList& pnl)
393 for (PatchBank::PatchNameList::const_iterator p = pnl.begin(); p != pnl.end(); ++p) {
394 _patch_map[(*p)->patch_primary_key()] = (*p);
395 _patch_list.push_back ((*p)->patch_primary_key());
400 ChannelNameSet::get_state (void)
402 XMLNode* node = new XMLNode("ChannelNameSet");
403 node->add_property("Name", _name);
405 XMLNode* available_for_channels = node->add_child("AvailableForChannels");
406 assert(available_for_channels);
408 for (uint8_t channel = 0; channel < 16; ++channel) {
409 XMLNode* available_channel = available_for_channels->add_child("AvailableChannel");
410 assert(available_channel);
412 available_channel->add_property("Channel", (long) channel);
414 if (_available_for_channels.find(channel) != _available_for_channels.end()) {
415 available_channel->add_property("Available", "true");
417 available_channel->add_property("Available", "false");
421 for (PatchBanks::iterator patch_bank = _patch_banks.begin();
422 patch_bank != _patch_banks.end();
424 node->add_child_nocopy((*patch_bank)->get_state());
431 ChannelNameSet::set_state (const XMLTree& tree, const XMLNode& node)
433 assert(node.name() == "ChannelNameSet");
434 _name = node.property("Name")->value();
436 const XMLNodeList children = node.children();
437 for (XMLNodeList::const_iterator i = children.begin(); i != children.end(); ++i) {
440 if (node->name() == "AvailableForChannels") {
441 boost::shared_ptr<XMLSharedNodeList> channels =
442 tree.find("//AvailableChannel[@Available = 'true']/@Channel", node);
443 for(XMLSharedNodeList::const_iterator i = channels->begin();
444 i != channels->end();
446 _available_for_channels.insert(
447 string_to_int(tree, (*i)->attribute_value()));
451 if (node->name() == "PatchBank") {
452 boost::shared_ptr<PatchBank> bank (new PatchBank ());
453 bank->set_state(tree, *node);
454 _patch_banks.push_back(bank);
455 const PatchBank::PatchNameList& patches = bank->patch_name_list();
456 for (PatchBank::PatchNameList::const_iterator patch = patches.begin();
457 patch != patches.end();
459 _patch_map[(*patch)->patch_primary_key()] = *patch;
460 _patch_list.push_back((*patch)->patch_primary_key());
464 if (node->name() == "UsesNoteNameList") {
465 _note_list_name = node->property ("Name")->value();
473 CustomDeviceMode::set_state(const XMLTree& tree, const XMLNode& a_node)
475 assert(a_node.name() == "CustomDeviceMode");
477 _name = a_node.property("Name")->value();
479 boost::shared_ptr<XMLSharedNodeList> channel_name_set_assignments =
480 tree.find("//ChannelNameSetAssign", const_cast<XMLNode *>(&a_node));
481 for(XMLSharedNodeList::const_iterator i = channel_name_set_assignments->begin();
482 i != channel_name_set_assignments->end();
484 const int channel = string_to_int(tree, (*i)->property("Channel")->value());
485 const string& name_set = (*i)->property("NameSet")->value();
486 assert( 1 <= channel && channel <= 16 );
487 _channel_name_set_assignments[channel - 1] = name_set;
493 CustomDeviceMode::get_state(void)
495 XMLNode* custom_device_mode = new XMLNode("CustomDeviceMode");
496 custom_device_mode->add_property("Name", _name);
497 XMLNode* channel_name_set_assignments =
498 custom_device_mode->add_child("ChannelNameSetAssignments");
499 for (int i = 0; i < 15 && !_channel_name_set_assignments[i].empty(); i++) {
500 XMLNode* channel_name_set_assign =
501 channel_name_set_assignments->add_child("ChannelNameSetAssign");
502 channel_name_set_assign->add_property("Channel", i + 1);
503 channel_name_set_assign->add_property("NameSet", _channel_name_set_assignments[i]);
506 return *custom_device_mode;
509 boost::shared_ptr<CustomDeviceMode>
510 MasterDeviceNames::custom_device_mode_by_name(const std::string& mode_name)
512 return _custom_device_modes[mode_name];
515 boost::shared_ptr<ChannelNameSet>
516 MasterDeviceNames::channel_name_set_by_device_mode_and_channel(const std::string& mode, uint8_t channel)
518 boost::shared_ptr<CustomDeviceMode> cdm = custom_device_mode_by_name(mode);
519 boost::shared_ptr<ChannelNameSet> cns = _channel_name_sets[cdm->channel_name_set_name_by_channel(channel)];
523 boost::shared_ptr<Patch>
524 MasterDeviceNames::find_patch(const std::string& mode, uint8_t channel, const PatchPrimaryKey& key)
526 return channel_name_set_by_device_mode_and_channel(mode, channel)->find_patch(key);
529 boost::shared_ptr<ChannelNameSet>
530 MasterDeviceNames::channel_name_set(const std::string& name)
532 ChannelNameSets::const_iterator i = _channel_name_sets.find(name);
533 if (i != _channel_name_sets.end()) {
536 return boost::shared_ptr<ChannelNameSet>();
539 boost::shared_ptr<NoteNameList>
540 MasterDeviceNames::note_name_list(const std::string& name)
542 NoteNameLists::const_iterator i = _note_name_lists.find(name);
543 if (i != _note_name_lists.end()) {
546 return boost::shared_ptr<NoteNameList>();
550 MasterDeviceNames::note_name(const std::string& mode_name,
560 boost::shared_ptr<const Patch> patch(
561 find_patch(mode_name, channel, PatchPrimaryKey(program, bank)));
566 boost::shared_ptr<const NoteNameList> note_names(
567 note_name_list(patch->note_list_name()));
569 /* No note names specific to this patch, check the ChannelNameSet */
570 boost::shared_ptr<ChannelNameSet> chan_names = channel_name_set_by_device_mode_and_channel(
573 note_names = note_name_list(chan_names->note_list_name());
580 boost::shared_ptr<const Note> note(note_names->notes()[number]);
581 return note ? note->name() : "";
585 MasterDeviceNames::set_state(const XMLTree& tree, const XMLNode&)
588 boost::shared_ptr<XMLSharedNodeList> manufacturer = tree.find("//Manufacturer");
589 assert(manufacturer->size() == 1);
590 _manufacturer = manufacturer->front()->children().front()->content();
593 boost::shared_ptr<XMLSharedNodeList> models = tree.find("//Model");
594 assert(models->size() >= 1);
595 for (XMLSharedNodeList::iterator i = models->begin();
598 const XMLNodeList& contents = (*i)->children();
599 assert(contents.size() == 1);
600 XMLNode * content = *(contents.begin());
601 assert(content->is_content());
602 _models.insert(content->content());
606 boost::shared_ptr<XMLSharedNodeList> custom_device_modes = tree.find("//CustomDeviceMode");
607 for (XMLSharedNodeList::iterator i = custom_device_modes->begin();
608 i != custom_device_modes->end();
610 boost::shared_ptr<CustomDeviceMode> custom_device_mode(new CustomDeviceMode());
611 custom_device_mode->set_state(tree, *(*i));
613 _custom_device_modes[custom_device_mode->name()] = custom_device_mode;
614 _custom_device_mode_names.push_back(custom_device_mode->name());
618 boost::shared_ptr<XMLSharedNodeList> channel_name_sets = tree.find("//ChannelNameSet");
619 for (XMLSharedNodeList::iterator i = channel_name_sets->begin();
620 i != channel_name_sets->end();
622 boost::shared_ptr<ChannelNameSet> channel_name_set(new ChannelNameSet());
623 channel_name_set->set_state(tree, *(*i));
624 _channel_name_sets[channel_name_set->name()] = channel_name_set;
628 boost::shared_ptr<XMLSharedNodeList> note_name_lists = tree.find("//NoteNameList");
629 for (XMLSharedNodeList::iterator i = note_name_lists->begin();
630 i != note_name_lists->end();
632 boost::shared_ptr<NoteNameList> note_name_list(new NoteNameList());
633 note_name_list->set_state (tree, *(*i));
634 _note_name_lists[(*i)->property ("Name")->value()] = note_name_list;
638 boost::shared_ptr<XMLSharedNodeList> control_name_lists = tree.find("//ControlNameList");
639 for (XMLSharedNodeList::iterator i = control_name_lists->begin();
640 i != control_name_lists->end();
642 boost::shared_ptr<ControlNameList> control_name_list(new ControlNameList());
643 control_name_list->set_state (tree, *(*i));
644 _control_name_lists.push_back(control_name_list);
647 // global/post-facto PatchNameLists
648 boost::shared_ptr<XMLSharedNodeList> patch_name_lists = tree.find("/child::MIDINameDocument/child::MasterDeviceNames/child::PatchNameList");
649 for (XMLSharedNodeList::iterator i = patch_name_lists->begin();
650 i != patch_name_lists->end();
653 PatchBank::PatchNameList patch_name_list;
654 const XMLNodeList patches = (*i)->children();
656 for (XMLNodeList::const_iterator p = patches.begin(); p != patches.end(); ++p) {
657 boost::shared_ptr<Patch> patch (new Patch ());
658 patch->set_state(tree, *(*p));
659 patch_name_list.push_back(patch);
662 if (!patch_name_list.empty()) {
663 _patch_name_lists[(*i)->property ("Name")->value()] = patch_name_list;
667 /* now traverse patches and hook up anything that used UsePatchNameList
668 * to the right patch list
671 for (ChannelNameSets::iterator cns = _channel_name_sets.begin(); cns != _channel_name_sets.end(); ++cns) {
672 ChannelNameSet::PatchBanks pbs = cns->second->patch_banks();
673 PatchNameLists::iterator p;
675 for (ChannelNameSet::PatchBanks::iterator pb = pbs.begin(); pb != pbs.end(); ++pb) {
676 const std::string& pln = (*pb)->patch_list_name();
678 if ((p = _patch_name_lists.find (pln)) != _patch_name_lists.end()) {
679 if ((*pb)->set_patch_name_list (p->second)) {
682 cns->second->use_patch_name_list (p->second);
684 error << string_compose ("Patch list name %1 was not found - patch file ignored", pln) << endmsg;
696 MasterDeviceNames::get_state(void)
698 static XMLNode nothing("<nothing>");
702 MIDINameDocument::MIDINameDocument (const string& filename)
704 if (!_document.read (filename)) {
705 throw failed_constructor ();
708 _document.set_filename (filename);
709 set_state (_document, *_document.root());
713 MIDINameDocument::set_state (const XMLTree& tree, const XMLNode&)
717 boost::shared_ptr<XMLSharedNodeList> author = tree.find("//Author");
718 if (author->size() < 1) {
719 error << "No author information in MIDNAM file" << endmsg;
723 if (author->front()->children().size() > 0) {
724 _author = author->front()->children().front()->content();
729 boost::shared_ptr<XMLSharedNodeList> master_device_names_list = tree.find ("//MasterDeviceNames");
731 for (XMLSharedNodeList::iterator i = master_device_names_list->begin();
732 i != master_device_names_list->end();
734 boost::shared_ptr<MasterDeviceNames> master_device_names(new MasterDeviceNames());
736 if (master_device_names->set_state(tree, *(*i))) {
740 for (MasterDeviceNames::Models::const_iterator model = master_device_names->models().begin();
741 model != master_device_names->models().end();
743 _master_device_names_list.insert(
744 std::pair<std::string, boost::shared_ptr<MasterDeviceNames> >
745 (*model, master_device_names));
747 _all_models.insert(*model);
755 MIDINameDocument::get_state(void)
757 static XMLNode nothing("<nothing>");
761 boost::shared_ptr<MasterDeviceNames>
762 MIDINameDocument::master_device_names(const std::string& model)
764 MasterDeviceNamesList::const_iterator m = _master_device_names_list.find(model);
765 if (m != _master_device_names_list.end()) {
766 return boost::shared_ptr<MasterDeviceNames>(m->second);
768 return boost::shared_ptr<MasterDeviceNames>();
771 const char* general_midi_program_names[128] = {
772 "Acoustic Grand Piano",
773 "Bright Acoustic Piano",
774 "Electric Grand Piano",
796 "Acoustic Guitar (nylon)",
797 "Acoustic Guitar (steel)",
798 "Electric Guitar (jazz)",
799 "Electric Guitar (clean)",
800 "Electric Guitar (muted)",
805 "Electric Bass (finger)",
806 "Electric Bass (pick)",
854 "Lead 3 (calliope lead)",
855 "Lead 4 (chiff lead)",
859 "Lead 8 (bass + lead)",