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)
45 int initialize_primary_key_from_commands (PatchPrimaryKey& id, const XMLNode* node)
49 const XMLNodeList events = node->children();
50 for (XMLNodeList::const_iterator i = events.begin(); i != events.end(); ++i) {
53 if (node->name() == "ControlChange") {
54 string control = node->property("Control")->value();
55 assert(control != "");
56 string value = node->property("Value")->value();
60 id.bank_number |= (PBD::atoi (value)) << 7;
61 } else if (control == "32") {
62 id.bank_number |= PBD::atoi (value);
65 } else if (node->name() == "ProgramChange") {
66 string number = node->property("Number")->value();
68 id.program_number = PBD::atoi(number);
76 Patch::get_state (void)
78 XMLNode* node = new XMLNode("Patch");
80 /* XXX this is totally wrong */
82 node->add_property("Number", string_compose ("%1", _id.program_number));
83 node->add_property("Name", _name);
86 typedef std::list< boost::shared_ptr< Evoral::MIDIEvent<double> > > PatchMidiCommands;
87 XMLNode* commands = node->add_child("PatchMIDICommands");
88 for (PatchMidiCommands::const_iterator event = _patch_midi_commands.begin();
89 event != _patch_midi_commands.end();
91 commands->add_child_copy(*((((Evoral::MIDIEvent&)*event)).to_xml()));
99 Patch::set_state (const XMLTree&, const XMLNode& node)
101 if (node.name() != "Patch") {
102 cerr << "Incorrect node " << node.name() << " handed to Patch" << endl;
106 const XMLProperty* prop = node.property ("Number");
111 _id.program_number = PBD::atoi (prop->value());
113 prop = node.property ("Name");
118 _name = prop->value();
120 XMLNode* commands = node.child("PatchMIDICommands");
123 if (initialize_primary_key_from_commands(_id, commands)) {
127 string program_change = node.property("ProgramChange")->value();
128 assert(program_change.length());
129 _id.program_number = PBD::atoi(program_change);
132 XMLNode* use_note_name_list = node.child("UsesNoteNameList");
133 if (use_note_name_list) {
134 _note_list_name = use_note_name_list->property ("Name")->value();
141 Note::get_state (void)
143 XMLNode* node = new XMLNode("Note");
144 node->add_property("Number", _number + 1);
145 node->add_property("Name", _name);
151 Note::set_state (const XMLTree&, const XMLNode& node)
153 assert(node.name() == "Note");
155 /* If the note number is junk, this will pull a number from the start, or
156 return zero if there isn't one. The decrement will make that zero
157 illegal since note numbers in the file are one-based. Better error
158 detection would be a good idea, but the duplicate check in
159 NoteNameList::set_state() will probably catch most errors anyway. */
160 _number = atoi(node.property("Number")->value().c_str()) - 1;
161 _name = node.property("Name")->value();
167 NoteNameList::get_state (void)
169 XMLNode* node = new XMLNode("NoteNameList");
170 node->add_property("Name", _name);
176 NoteNameList::set_state (const XMLTree& tree, const XMLNode& node)
178 assert(node.name() == "NoteNameList");
179 _name = node.property("Name")->value();
183 for (XMLNodeList::const_iterator i = node.children().begin();
184 i != node.children().end(); ++i) {
185 if ((*i)->name() != "Note") {
188 boost::shared_ptr<Note> note(new Note());
189 note->set_state (tree, *(*i));
190 if (note->number() > 127) {
191 PBD::warning << string_compose("%1: Note number %2 (%3) out of range",
192 tree.filename(), (int)note->number(), note->name())
194 } else if (_notes[note->number()]) {
196 string_compose("%1: Duplicate note number %2 (%3) ignored",
197 tree.filename(), (int)note->number(), note->name())
200 _notes[note->number()] = note;
208 Control::get_state (void)
210 XMLNode* node = new XMLNode("Control");
211 node->add_property("Type", _type);
212 node->add_property("Number", _number);
213 node->add_property("Name", _name);
219 Control::set_state (const XMLTree&, const XMLNode& node)
221 assert(node.name() == "Control");
222 _type = node.property("Type")->value();
223 _number = node.property("Number")->value();
224 _name = node.property("Name")->value();
230 ControlNameList::get_state (void)
232 XMLNode* node = new XMLNode("ControlNameList");
233 node->add_property("Name", _name);
239 ControlNameList::set_state (const XMLTree& tree, const XMLNode& node)
241 assert(node.name() == "ControlNameList");
242 _name = node.property("Name")->value();
244 for (XMLNodeList::const_iterator i = node.children().begin();
245 i != node.children().end(); ++i) {
246 if ((*i)->name() != "comment") {
247 boost::shared_ptr<Control> control(new Control());
248 control->set_state (tree, *(*i));
249 _controls.push_back(control);
257 PatchBank::get_state (void)
259 XMLNode* node = new XMLNode("PatchBank");
260 node->add_property("Name", _name);
261 XMLNode* patch_name_list = node->add_child("PatchNameList");
262 for (PatchNameList::iterator patch = _patch_name_list.begin();
263 patch != _patch_name_list.end();
265 patch_name_list->add_child_nocopy((*patch)->get_state());
272 PatchBank::set_state (const XMLTree& tree, const XMLNode& node)
274 assert(node.name() == "PatchBank");
275 _name = node.property("Name")->value();
277 XMLNode* commands = node.child("MIDICommands");
279 PatchPrimaryKey id (0, 0);
280 if (initialize_primary_key_from_commands (id, commands)) {
283 _number = id.bank_number;
286 XMLNode* patch_name_list = node.child("PatchNameList");
288 if (patch_name_list) {
289 const XMLNodeList patches = patch_name_list->children();
290 for (XMLNodeList::const_iterator i = patches.begin(); i != patches.end(); ++i) {
291 boost::shared_ptr<Patch> patch (new Patch (string(), 0, _number));
292 patch->set_state(tree, *(*i));
293 _patch_name_list.push_back(patch);
296 XMLNode* use_patch_name_list = node.child ("UsesPatchNameList");
297 if (use_patch_name_list) {
298 _patch_list_name = use_patch_name_list->property ("Name")->value();
300 error << "Patch without patch name list - patchfile will be ignored" << endmsg;
309 PatchBank::set_patch_name_list (const PatchNameList& pnl)
311 _patch_name_list = pnl;
312 _patch_list_name = "";
314 for (PatchNameList::iterator p = _patch_name_list.begin(); p != _patch_name_list.end(); p++) {
315 (*p)->set_bank_number (_number);
322 operator<< (std::ostream& os, const ChannelNameSet& cns)
324 os << "Channel Name Set: name = " << cns._name << endl
325 << "Map size " << cns._patch_map.size () << endl
326 << "List size " << cns._patch_list.size() << endl
327 << "Patch list name = [" << cns._patch_list_name << ']' << endl
328 << "Available channels : ";
329 for (set<uint8_t>::iterator x = cns._available_for_channels.begin(); x != cns._available_for_channels.end(); ++x) {
330 os << (int) (*x) << ' ';
334 for (ChannelNameSet::PatchBanks::const_iterator pbi = cns._patch_banks.begin(); pbi != cns._patch_banks.end(); ++pbi) {
335 os << "\tPatch Bank " << (*pbi)->name() << " with " << (*pbi)->patch_name_list().size() << " patches\n";
336 for (PatchBank::PatchNameList::const_iterator pni = (*pbi)->patch_name_list().begin(); pni != (*pbi)->patch_name_list().end(); ++pni) {
337 os << "\t\tPatch name " << (*pni)->name() << " prog " << (int) (*pni)->program_number() << " bank " << (*pni)->bank_number() << endl;
345 ChannelNameSet::set_patch_banks (const ChannelNameSet::PatchBanks& pb)
350 _patch_list.clear ();
351 _patch_list_name = "";
352 _available_for_channels.clear ();
354 for (PatchBanks::const_iterator pbi = _patch_banks.begin(); pbi != _patch_banks.end(); ++pbi) {
355 for (PatchBank::PatchNameList::const_iterator pni = (*pbi)->patch_name_list().begin(); pni != (*pbi)->patch_name_list().end(); ++pni) {
356 _patch_map[(*pni)->patch_primary_key()] = (*pni);
357 _patch_list.push_back ((*pni)->patch_primary_key());
361 for (uint8_t n = 0; n < 16; ++n) {
362 _available_for_channels.insert (n);
367 ChannelNameSet::use_patch_name_list (const PatchBank::PatchNameList& pnl)
369 for (PatchBank::PatchNameList::const_iterator p = pnl.begin(); p != pnl.end(); ++p) {
370 _patch_map[(*p)->patch_primary_key()] = (*p);
371 _patch_list.push_back ((*p)->patch_primary_key());
376 ChannelNameSet::get_state (void)
378 XMLNode* node = new XMLNode("ChannelNameSet");
379 node->add_property("Name", _name);
381 XMLNode* available_for_channels = node->add_child("AvailableForChannels");
382 assert(available_for_channels);
384 for (uint8_t channel = 0; channel < 16; ++channel) {
385 XMLNode* available_channel = available_for_channels->add_child("AvailableChannel");
386 assert(available_channel);
388 available_channel->add_property("Channel", (long) channel);
390 if (_available_for_channels.find(channel) != _available_for_channels.end()) {
391 available_channel->add_property("Available", "true");
393 available_channel->add_property("Available", "false");
397 for (PatchBanks::iterator patch_bank = _patch_banks.begin();
398 patch_bank != _patch_banks.end();
400 node->add_child_nocopy((*patch_bank)->get_state());
407 ChannelNameSet::set_state (const XMLTree& tree, const XMLNode& node)
409 assert(node.name() == "ChannelNameSet");
410 _name = node.property("Name")->value();
412 const XMLNodeList children = node.children();
413 for (XMLNodeList::const_iterator i = children.begin(); i != children.end(); ++i) {
416 if (node->name() == "AvailableForChannels") {
417 boost::shared_ptr<XMLSharedNodeList> channels =
418 tree.find("//AvailableChannel[@Available = 'true']/@Channel", node);
419 for(XMLSharedNodeList::const_iterator i = channels->begin();
420 i != channels->end();
422 _available_for_channels.insert(atoi((*i)->attribute_value().c_str()));
426 if (node->name() == "PatchBank") {
427 boost::shared_ptr<PatchBank> bank (new PatchBank ());
428 bank->set_state(tree, *node);
429 _patch_banks.push_back(bank);
430 const PatchBank::PatchNameList& patches = bank->patch_name_list();
431 for (PatchBank::PatchNameList::const_iterator patch = patches.begin();
432 patch != patches.end();
434 _patch_map[(*patch)->patch_primary_key()] = *patch;
435 _patch_list.push_back((*patch)->patch_primary_key());
444 CustomDeviceMode::set_state(const XMLTree& tree, const XMLNode& a_node)
446 assert(a_node.name() == "CustomDeviceMode");
448 _name = a_node.property("Name")->value();
450 boost::shared_ptr<XMLSharedNodeList> channel_name_set_assignments =
451 tree.find("//ChannelNameSetAssign", const_cast<XMLNode *>(&a_node));
452 for(XMLSharedNodeList::const_iterator i = channel_name_set_assignments->begin();
453 i != channel_name_set_assignments->end();
455 int channel = atoi((*i)->property("Channel")->value().c_str());
456 string name_set = (*i)->property("NameSet")->value();
457 assert( 1 <= channel && channel <= 16 );
458 _channel_name_set_assignments[channel - 1] = name_set;
464 CustomDeviceMode::get_state(void)
466 XMLNode* custom_device_mode = new XMLNode("CustomDeviceMode");
467 custom_device_mode->add_property("Name", _name);
468 XMLNode* channel_name_set_assignments =
469 custom_device_mode->add_child("ChannelNameSetAssignments");
470 for (int i = 0; i < 15 && !_channel_name_set_assignments[i].empty(); i++) {
471 XMLNode* channel_name_set_assign =
472 channel_name_set_assignments->add_child("ChannelNameSetAssign");
473 channel_name_set_assign->add_property("Channel", i + 1);
474 channel_name_set_assign->add_property("NameSet", _channel_name_set_assignments[i]);
477 return *custom_device_mode;
480 boost::shared_ptr<CustomDeviceMode>
481 MasterDeviceNames::custom_device_mode_by_name(std::string mode_name)
483 return _custom_device_modes[mode_name];
486 boost::shared_ptr<ChannelNameSet>
487 MasterDeviceNames::channel_name_set_by_device_mode_and_channel(std::string mode, uint8_t channel)
489 boost::shared_ptr<CustomDeviceMode> cdm = custom_device_mode_by_name(mode);
490 boost::shared_ptr<ChannelNameSet> cns = _channel_name_sets[cdm->channel_name_set_name_by_channel(channel)];
494 boost::shared_ptr<Patch>
495 MasterDeviceNames::find_patch(std::string mode, uint8_t channel, const PatchPrimaryKey& key)
497 return channel_name_set_by_device_mode_and_channel(mode, channel)->find_patch(key);
500 boost::shared_ptr<ChannelNameSet>
501 MasterDeviceNames::channel_name_set(const std::string& name)
503 ChannelNameSets::const_iterator i = _channel_name_sets.find(name);
504 if (i != _channel_name_sets.end()) {
507 return boost::shared_ptr<ChannelNameSet>();
510 boost::shared_ptr<NoteNameList>
511 MasterDeviceNames::note_name_list(const std::string& name)
513 NoteNameLists::const_iterator i = _note_name_lists.find(name);
514 if (i != _note_name_lists.end()) {
517 return boost::shared_ptr<NoteNameList>();
521 MasterDeviceNames::note_name(const std::string& mode_name,
531 boost::shared_ptr<const Patch> patch(
532 find_patch(mode_name, channel, PatchPrimaryKey(program, bank)));
537 boost::shared_ptr<const NoteNameList> note_names(
538 note_name_list(patch->note_list_name()));
543 boost::shared_ptr<const Note> note(note_names->notes()[number]);
544 return note ? note->name() : "";
548 MasterDeviceNames::set_state(const XMLTree& tree, const XMLNode&)
551 boost::shared_ptr<XMLSharedNodeList> manufacturer = tree.find("//Manufacturer");
552 assert(manufacturer->size() == 1);
553 _manufacturer = manufacturer->front()->children().front()->content();
556 boost::shared_ptr<XMLSharedNodeList> models = tree.find("//Model");
557 assert(models->size() >= 1);
558 for (XMLSharedNodeList::iterator i = models->begin();
561 const XMLNodeList& contents = (*i)->children();
562 assert(contents.size() == 1);
563 XMLNode * content = *(contents.begin());
564 assert(content->is_content());
565 _models.insert(content->content());
569 boost::shared_ptr<XMLSharedNodeList> custom_device_modes = tree.find("//CustomDeviceMode");
570 for (XMLSharedNodeList::iterator i = custom_device_modes->begin();
571 i != custom_device_modes->end();
573 boost::shared_ptr<CustomDeviceMode> custom_device_mode(new CustomDeviceMode());
574 custom_device_mode->set_state(tree, *(*i));
576 _custom_device_modes[custom_device_mode->name()] = custom_device_mode;
577 _custom_device_mode_names.push_back(custom_device_mode->name());
581 boost::shared_ptr<XMLSharedNodeList> channel_name_sets = tree.find("//ChannelNameSet");
582 for (XMLSharedNodeList::iterator i = channel_name_sets->begin();
583 i != channel_name_sets->end();
585 boost::shared_ptr<ChannelNameSet> channel_name_set(new ChannelNameSet());
586 channel_name_set->set_state(tree, *(*i));
587 _channel_name_sets[channel_name_set->name()] = channel_name_set;
591 boost::shared_ptr<XMLSharedNodeList> note_name_lists = tree.find("//NoteNameList");
592 for (XMLSharedNodeList::iterator i = note_name_lists->begin();
593 i != note_name_lists->end();
595 boost::shared_ptr<NoteNameList> note_name_list(new NoteNameList());
596 note_name_list->set_state (tree, *(*i));
597 _note_name_lists[(*i)->property ("Name")->value()] = note_name_list;
601 boost::shared_ptr<XMLSharedNodeList> control_name_lists = tree.find("//ControlNameList");
602 for (XMLSharedNodeList::iterator i = control_name_lists->begin();
603 i != control_name_lists->end();
605 boost::shared_ptr<ControlNameList> control_name_list(new ControlNameList());
606 control_name_list->set_state (tree, *(*i));
607 _control_name_lists.push_back(control_name_list);
610 // global/post-facto PatchNameLists
611 boost::shared_ptr<XMLSharedNodeList> patch_name_lists = tree.find("/child::MIDINameDocument/child::MasterDeviceNames/child::PatchNameList");
612 for (XMLSharedNodeList::iterator i = patch_name_lists->begin();
613 i != patch_name_lists->end();
616 PatchBank::PatchNameList patch_name_list;
617 const XMLNodeList patches = (*i)->children();
619 for (XMLNodeList::const_iterator p = patches.begin(); p != patches.end(); ++p) {
620 boost::shared_ptr<Patch> patch (new Patch ());
621 patch->set_state(tree, *(*p));
622 patch_name_list.push_back(patch);
625 if (!patch_name_list.empty()) {
626 _patch_name_lists[(*i)->property ("Name")->value()] = patch_name_list;
630 /* now traverse patches and hook up anything that used UsePatchNameList
631 * to the right patch list
634 for (ChannelNameSets::iterator cns = _channel_name_sets.begin(); cns != _channel_name_sets.end(); ++cns) {
635 ChannelNameSet::PatchBanks pbs = cns->second->patch_banks();
636 PatchNameLists::iterator p;
638 for (ChannelNameSet::PatchBanks::iterator pb = pbs.begin(); pb != pbs.end(); ++pb) {
639 std::string pln = (*pb)->patch_list_name();
641 if ((p = _patch_name_lists.find (pln)) != _patch_name_lists.end()) {
642 if ((*pb)->set_patch_name_list (p->second)) {
645 cns->second->use_patch_name_list (p->second);
647 error << string_compose ("Patch list name %1 was not found - patch file ignored", pln) << endmsg;
659 MasterDeviceNames::get_state(void)
661 static XMLNode nothing("<nothing>");
665 MIDINameDocument::MIDINameDocument (const string& filename)
667 if (!_document.read (filename)) {
668 throw failed_constructor ();
671 _document.set_filename (filename);
672 set_state (_document, *_document.root());
676 MIDINameDocument::set_state (const XMLTree& tree, const XMLNode&)
680 boost::shared_ptr<XMLSharedNodeList> author = tree.find("//Author");
681 if (author->size() < 1) {
682 error << "No author information in MIDNAM file" << endmsg;
686 if (author->front()->children().size() > 0) {
687 _author = author->front()->children().front()->content();
692 boost::shared_ptr<XMLSharedNodeList> master_device_names_list = tree.find ("//MasterDeviceNames");
694 for (XMLSharedNodeList::iterator i = master_device_names_list->begin();
695 i != master_device_names_list->end();
697 boost::shared_ptr<MasterDeviceNames> master_device_names(new MasterDeviceNames());
699 if (master_device_names->set_state(tree, *(*i))) {
703 for (MasterDeviceNames::Models::const_iterator model = master_device_names->models().begin();
704 model != master_device_names->models().end();
706 _master_device_names_list.insert(
707 std::pair<std::string, boost::shared_ptr<MasterDeviceNames> >
708 (*model, master_device_names));
710 _all_models.insert(*model);
718 MIDINameDocument::get_state(void)
720 static XMLNode nothing("<nothing>");
724 boost::shared_ptr<MasterDeviceNames>
725 MIDINameDocument::master_device_names(const std::string& model)
727 MasterDeviceNamesList::const_iterator m = _master_device_names_list.find(model);
728 if (m != _master_device_names_list.end()) {
729 return boost::shared_ptr<MasterDeviceNames>(m->second);
731 return boost::shared_ptr<MasterDeviceNames>();
734 const char* general_midi_program_names[128] = {
735 "Acoustic Grand Piano",
736 "Bright Acoustic Piano",
737 "Electric Grand Piano",
759 "Acoustic Guitar (nylon)",
760 "Acoustic Guitar (steel)",
761 "Electric Guitar (jazz)",
762 "Electric Guitar (clean)",
763 "Electric Guitar (muted)",
768 "Electric Bass (finger)",
769 "Electric Bass (pick)",
817 "Lead 3 (calliope lead)",
818 "Lead 4 (chiff lead)",
822 "Lead 8 (bass + lead)",