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 _type = node.property("Type")->value();
247 _number = string_to_int(tree, 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();
269 for (XMLNodeList::const_iterator i = node.children().begin();
270 i != node.children().end(); ++i) {
271 if ((*i)->name() == "Control") {
272 boost::shared_ptr<Control> control(new Control());
273 control->set_state (tree, *(*i));
274 if (_controls.find(control->number()) == _controls.end()) {
275 _controls.insert(make_pair(control->number(), control));
277 PBD::warning << string_compose("%1: Duplicate control %2 ignored",
278 tree.filename(), control->number())
287 boost::shared_ptr<const Control>
288 ControlNameList::control(uint16_t num) const
290 Controls::const_iterator i = _controls.find(num);
291 if (i != _controls.end()) {
294 return boost::shared_ptr<const Control>();
298 PatchBank::get_state (void)
300 XMLNode* node = new XMLNode("PatchBank");
301 node->add_property("Name", _name);
302 XMLNode* patch_name_list = node->add_child("PatchNameList");
303 for (PatchNameList::iterator patch = _patch_name_list.begin();
304 patch != _patch_name_list.end();
306 patch_name_list->add_child_nocopy((*patch)->get_state());
313 PatchBank::set_state (const XMLTree& tree, const XMLNode& node)
315 assert(node.name() == "PatchBank");
316 _name = node.property("Name")->value();
318 XMLNode* commands = node.child("MIDICommands");
320 PatchPrimaryKey id (0, 0);
321 if (initialize_primary_key_from_commands (tree, id, commands)) {
324 _number = id.bank_number;
327 XMLNode* patch_name_list = node.child("PatchNameList");
329 if (patch_name_list) {
330 const XMLNodeList patches = patch_name_list->children();
331 for (XMLNodeList::const_iterator i = patches.begin(); i != patches.end(); ++i) {
332 boost::shared_ptr<Patch> patch (new Patch (string(), 0, _number));
333 patch->set_state(tree, *(*i));
334 _patch_name_list.push_back(patch);
337 XMLNode* use_patch_name_list = node.child ("UsesPatchNameList");
338 if (use_patch_name_list) {
339 _patch_list_name = use_patch_name_list->property ("Name")->value();
341 error << "Patch without patch name list - patchfile will be ignored" << endmsg;
350 PatchBank::set_patch_name_list (const PatchNameList& pnl)
352 _patch_name_list = pnl;
353 _patch_list_name = "";
355 for (PatchNameList::iterator p = _patch_name_list.begin(); p != _patch_name_list.end(); p++) {
356 (*p)->set_bank_number (_number);
363 operator<< (std::ostream& os, const ChannelNameSet& cns)
365 os << "Channel Name Set: name = " << cns._name << endl
366 << "Map size " << cns._patch_map.size () << endl
367 << "List size " << cns._patch_list.size() << endl
368 << "Patch list name = [" << cns._patch_list_name << ']' << endl
369 << "Available channels : ";
370 for (set<uint8_t>::iterator x = cns._available_for_channels.begin(); x != cns._available_for_channels.end(); ++x) {
371 os << (int) (*x) << ' ';
375 for (ChannelNameSet::PatchBanks::const_iterator pbi = cns._patch_banks.begin(); pbi != cns._patch_banks.end(); ++pbi) {
376 os << "\tPatch Bank " << (*pbi)->name() << " with " << (*pbi)->patch_name_list().size() << " patches\n";
377 for (PatchNameList::const_iterator pni = (*pbi)->patch_name_list().begin(); pni != (*pbi)->patch_name_list().end(); ++pni) {
378 os << "\t\tPatch name " << (*pni)->name() << " prog " << (int) (*pni)->program_number() << " bank " << (*pni)->bank_number() << endl;
386 ChannelNameSet::set_patch_banks (const ChannelNameSet::PatchBanks& pb)
391 _patch_list.clear ();
392 _patch_list_name = "";
393 _available_for_channels.clear ();
395 for (PatchBanks::const_iterator pbi = _patch_banks.begin(); pbi != _patch_banks.end(); ++pbi) {
396 for (PatchNameList::const_iterator pni = (*pbi)->patch_name_list().begin(); pni != (*pbi)->patch_name_list().end(); ++pni) {
397 _patch_map[(*pni)->patch_primary_key()] = (*pni);
398 _patch_list.push_back ((*pni)->patch_primary_key());
402 for (uint8_t n = 0; n < 16; ++n) {
403 _available_for_channels.insert (n);
408 ChannelNameSet::use_patch_name_list (const PatchNameList& pnl)
410 for (PatchNameList::const_iterator p = pnl.begin(); p != pnl.end(); ++p) {
411 _patch_map[(*p)->patch_primary_key()] = (*p);
412 _patch_list.push_back ((*p)->patch_primary_key());
417 ChannelNameSet::get_state (void)
419 XMLNode* node = new XMLNode("ChannelNameSet");
420 node->add_property("Name", _name);
422 XMLNode* available_for_channels = node->add_child("AvailableForChannels");
423 assert(available_for_channels);
425 for (uint8_t channel = 0; channel < 16; ++channel) {
426 XMLNode* available_channel = available_for_channels->add_child("AvailableChannel");
427 assert(available_channel);
429 available_channel->add_property("Channel", (long) channel);
431 if (_available_for_channels.find(channel) != _available_for_channels.end()) {
432 available_channel->add_property("Available", "true");
434 available_channel->add_property("Available", "false");
438 for (PatchBanks::iterator patch_bank = _patch_banks.begin();
439 patch_bank != _patch_banks.end();
441 node->add_child_nocopy((*patch_bank)->get_state());
448 ChannelNameSet::set_state (const XMLTree& tree, const XMLNode& node)
450 assert(node.name() == "ChannelNameSet");
451 _name = node.property("Name")->value();
453 const XMLNodeList children = node.children();
454 for (XMLNodeList::const_iterator i = children.begin(); i != children.end(); ++i) {
457 if (node->name() == "AvailableForChannels") {
458 boost::shared_ptr<XMLSharedNodeList> channels =
459 tree.find("//AvailableChannel[@Available = 'true']/@Channel", node);
460 for (XMLSharedNodeList::const_iterator i = channels->begin();
461 i != channels->end();
463 _available_for_channels.insert(
464 string_to_int(tree, (*i)->attribute_value()));
466 } else if (node->name() == "PatchBank") {
467 boost::shared_ptr<PatchBank> bank (new PatchBank ());
468 bank->set_state(tree, *node);
469 _patch_banks.push_back(bank);
470 const PatchNameList& patches = bank->patch_name_list();
471 for (PatchNameList::const_iterator patch = patches.begin();
472 patch != patches.end();
474 _patch_map[(*patch)->patch_primary_key()] = *patch;
475 _patch_list.push_back((*patch)->patch_primary_key());
477 } else if (node->name() == "UsesNoteNameList") {
478 _note_list_name = node->property ("Name")->value();
479 } else if (node->name() == "UsesControlNameList") {
480 _control_list_name = node->property ("Name")->value();
488 CustomDeviceMode::set_state(const XMLTree& tree, const XMLNode& a_node)
490 assert(a_node.name() == "CustomDeviceMode");
492 _name = a_node.property("Name")->value();
494 boost::shared_ptr<XMLSharedNodeList> channel_name_set_assignments =
495 tree.find("//ChannelNameSetAssign", const_cast<XMLNode *>(&a_node));
496 for (XMLSharedNodeList::const_iterator i = channel_name_set_assignments->begin();
497 i != channel_name_set_assignments->end();
499 const int channel = string_to_int(tree, (*i)->property("Channel")->value());
500 const string& name_set = (*i)->property("NameSet")->value();
501 assert( 1 <= channel && channel <= 16 );
502 _channel_name_set_assignments[channel - 1] = name_set;
508 CustomDeviceMode::get_state(void)
510 XMLNode* custom_device_mode = new XMLNode("CustomDeviceMode");
511 custom_device_mode->add_property("Name", _name);
512 XMLNode* channel_name_set_assignments =
513 custom_device_mode->add_child("ChannelNameSetAssignments");
514 for (int i = 0; i < 15 && !_channel_name_set_assignments[i].empty(); i++) {
515 XMLNode* channel_name_set_assign =
516 channel_name_set_assignments->add_child("ChannelNameSetAssign");
517 channel_name_set_assign->add_property("Channel", i + 1);
518 channel_name_set_assign->add_property("NameSet", _channel_name_set_assignments[i]);
521 return *custom_device_mode;
524 boost::shared_ptr<CustomDeviceMode>
525 MasterDeviceNames::custom_device_mode_by_name(const std::string& mode_name)
527 return _custom_device_modes[mode_name];
530 boost::shared_ptr<ChannelNameSet>
531 MasterDeviceNames::channel_name_set_by_device_mode_and_channel(const std::string& mode, uint8_t channel)
533 boost::shared_ptr<CustomDeviceMode> cdm = custom_device_mode_by_name(mode);
534 boost::shared_ptr<ChannelNameSet> cns = _channel_name_sets[cdm->channel_name_set_name_by_channel(channel)];
538 boost::shared_ptr<Patch>
539 MasterDeviceNames::find_patch(const std::string& mode, uint8_t channel, const PatchPrimaryKey& key)
541 return channel_name_set_by_device_mode_and_channel(mode, channel)->find_patch(key);
544 boost::shared_ptr<ChannelNameSet>
545 MasterDeviceNames::channel_name_set(const std::string& name)
547 ChannelNameSets::const_iterator i = _channel_name_sets.find(name);
548 if (i != _channel_name_sets.end()) {
551 return boost::shared_ptr<ChannelNameSet>();
554 boost::shared_ptr<ControlNameList>
555 MasterDeviceNames::control_name_list(const std::string& name)
557 ControlNameLists::const_iterator i = _control_name_lists.find(name);
558 if (i != _control_name_lists.end()) {
561 return boost::shared_ptr<ControlNameList>();
564 boost::shared_ptr<NoteNameList>
565 MasterDeviceNames::note_name_list(const std::string& name)
567 NoteNameLists::const_iterator i = _note_name_lists.find(name);
568 if (i != _note_name_lists.end()) {
571 return boost::shared_ptr<NoteNameList>();
575 MasterDeviceNames::note_name(const std::string& mode_name,
585 boost::shared_ptr<const Patch> patch(
586 find_patch(mode_name, channel, PatchPrimaryKey(program, bank)));
591 boost::shared_ptr<const NoteNameList> note_names(
592 note_name_list(patch->note_list_name()));
594 /* No note names specific to this patch, check the ChannelNameSet */
595 boost::shared_ptr<ChannelNameSet> chan_names = channel_name_set_by_device_mode_and_channel(
598 note_names = note_name_list(chan_names->note_list_name());
605 boost::shared_ptr<const Note> note(note_names->notes()[number]);
606 return note ? note->name() : "";
610 MasterDeviceNames::set_state(const XMLTree& tree, const XMLNode&)
613 boost::shared_ptr<XMLSharedNodeList> manufacturer = tree.find("//Manufacturer");
614 assert(manufacturer->size() == 1);
615 _manufacturer = manufacturer->front()->children().front()->content();
618 boost::shared_ptr<XMLSharedNodeList> models = tree.find("//Model");
619 assert(models->size() >= 1);
620 for (XMLSharedNodeList::iterator i = models->begin();
623 const XMLNodeList& contents = (*i)->children();
624 assert(contents.size() == 1);
625 XMLNode * content = *(contents.begin());
626 assert(content->is_content());
627 _models.insert(content->content());
631 boost::shared_ptr<XMLSharedNodeList> custom_device_modes = tree.find("//CustomDeviceMode");
632 for (XMLSharedNodeList::iterator i = custom_device_modes->begin();
633 i != custom_device_modes->end();
635 boost::shared_ptr<CustomDeviceMode> custom_device_mode(new CustomDeviceMode());
636 custom_device_mode->set_state(tree, *(*i));
638 _custom_device_modes[custom_device_mode->name()] = custom_device_mode;
639 _custom_device_mode_names.push_back(custom_device_mode->name());
643 boost::shared_ptr<XMLSharedNodeList> channel_name_sets = tree.find("//ChannelNameSet");
644 for (XMLSharedNodeList::iterator i = channel_name_sets->begin();
645 i != channel_name_sets->end();
647 boost::shared_ptr<ChannelNameSet> channel_name_set(new ChannelNameSet());
648 channel_name_set->set_state(tree, *(*i));
649 _channel_name_sets[channel_name_set->name()] = channel_name_set;
653 boost::shared_ptr<XMLSharedNodeList> note_name_lists = tree.find("//NoteNameList");
654 for (XMLSharedNodeList::iterator i = note_name_lists->begin();
655 i != note_name_lists->end();
657 boost::shared_ptr<NoteNameList> note_name_list(new NoteNameList());
658 note_name_list->set_state (tree, *(*i));
659 _note_name_lists[note_name_list->name()] = note_name_list;
663 boost::shared_ptr<XMLSharedNodeList> control_name_lists = tree.find("//ControlNameList");
664 for (XMLSharedNodeList::iterator i = control_name_lists->begin();
665 i != control_name_lists->end();
667 boost::shared_ptr<ControlNameList> control_name_list(new ControlNameList());
668 control_name_list->set_state (tree, *(*i));
669 _control_name_lists[control_name_list->name()] = control_name_list;
672 // global/post-facto PatchNameLists
673 boost::shared_ptr<XMLSharedNodeList> patch_name_lists = tree.find("/child::MIDINameDocument/child::MasterDeviceNames/child::PatchNameList");
674 for (XMLSharedNodeList::iterator i = patch_name_lists->begin();
675 i != patch_name_lists->end();
678 PatchNameList patch_name_list;
679 const XMLNodeList patches = (*i)->children();
681 for (XMLNodeList::const_iterator p = patches.begin(); p != patches.end(); ++p) {
682 boost::shared_ptr<Patch> patch (new Patch ());
683 patch->set_state(tree, *(*p));
684 patch_name_list.push_back(patch);
687 if (!patch_name_list.empty()) {
688 _patch_name_lists[(*i)->property ("Name")->value()] = patch_name_list;
692 /* now traverse patches and hook up anything that used UsePatchNameList
693 * to the right patch list
696 for (ChannelNameSets::iterator cns = _channel_name_sets.begin(); cns != _channel_name_sets.end(); ++cns) {
697 ChannelNameSet::PatchBanks pbs = cns->second->patch_banks();
698 PatchNameLists::iterator p;
700 for (ChannelNameSet::PatchBanks::iterator pb = pbs.begin(); pb != pbs.end(); ++pb) {
701 const std::string& pln = (*pb)->patch_list_name();
703 if ((p = _patch_name_lists.find (pln)) != _patch_name_lists.end()) {
704 if ((*pb)->set_patch_name_list (p->second)) {
707 cns->second->use_patch_name_list (p->second);
709 error << string_compose ("Patch list name %1 was not found - patch file ignored", pln) << endmsg;
721 MasterDeviceNames::get_state(void)
723 static XMLNode nothing("<nothing>");
727 MIDINameDocument::MIDINameDocument (const string& filename)
729 if (!_document.read (filename)) {
730 throw failed_constructor ();
733 _document.set_filename (filename);
734 set_state (_document, *_document.root());
738 MIDINameDocument::set_state (const XMLTree& tree, const XMLNode&)
742 boost::shared_ptr<XMLSharedNodeList> author = tree.find("//Author");
743 if (author->size() < 1) {
744 error << "No author information in MIDNAM file" << endmsg;
748 if (author->front()->children().size() > 0) {
749 _author = author->front()->children().front()->content();
754 boost::shared_ptr<XMLSharedNodeList> master_device_names_list = tree.find ("//MasterDeviceNames");
756 for (XMLSharedNodeList::iterator i = master_device_names_list->begin();
757 i != master_device_names_list->end();
759 boost::shared_ptr<MasterDeviceNames> master_device_names(new MasterDeviceNames());
761 if (master_device_names->set_state(tree, *(*i))) {
765 for (MasterDeviceNames::Models::const_iterator model = master_device_names->models().begin();
766 model != master_device_names->models().end();
768 _master_device_names_list.insert(
769 std::pair<std::string, boost::shared_ptr<MasterDeviceNames> >
770 (*model, master_device_names));
772 _all_models.insert(*model);
780 MIDINameDocument::get_state(void)
782 static XMLNode nothing("<nothing>");
786 boost::shared_ptr<MasterDeviceNames>
787 MIDINameDocument::master_device_names(const std::string& model)
789 MasterDeviceNamesList::const_iterator m = _master_device_names_list.find(model);
790 if (m != _master_device_names_list.end()) {
791 return boost::shared_ptr<MasterDeviceNames>(m->second);
793 return boost::shared_ptr<MasterDeviceNames>();
796 const char* general_midi_program_names[128] = {
797 "Acoustic Grand Piano",
798 "Bright Acoustic Piano",
799 "Electric Grand Piano",
821 "Acoustic Guitar (nylon)",
822 "Acoustic Guitar (steel)",
823 "Electric Guitar (jazz)",
824 "Electric Guitar (clean)",
825 "Electric Guitar (muted)",
830 "Electric Bass (finger)",
831 "Electric Bass (pick)",
879 "Lead 3 (calliope lead)",
880 "Lead 4 (chiff lead)",
884 "Lead 8 (bass + lead)",