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);
136 Note::get_state (void)
138 XMLNode* node = new XMLNode("Note");
139 node->add_property("Number", _number);
140 node->add_property("Name", _name);
146 Note::set_state (const XMLTree&, const XMLNode& node)
148 assert(node.name() == "Note");
149 _number = node.property("Number")->value();
150 _name = node.property("Name")->value();
156 NoteNameList::get_state (void)
158 XMLNode* node = new XMLNode("NoteNameList");
159 node->add_property("Name", _name);
165 NoteNameList::set_state (const XMLTree& tree, const XMLNode& node)
167 assert(node.name() == "NoteNameList");
168 _name = node.property("Name")->value();
170 boost::shared_ptr<XMLSharedNodeList> notes = tree.find("//Note");
171 for (XMLSharedNodeList::const_iterator i = notes->begin(); i != notes->end(); ++i) {
172 boost::shared_ptr<Note> note(new Note());
173 note->set_state (tree, *(*i));
174 _notes.push_back(note);
181 Control::get_state (void)
183 XMLNode* node = new XMLNode("Control");
184 node->add_property("Type", _type);
185 node->add_property("Number", _number);
186 node->add_property("Name", _name);
192 Control::set_state (const XMLTree&, const XMLNode& node)
194 assert(node.name() == "Control");
195 _type = node.property("Type")->value();
196 _number = node.property("Number")->value();
197 _name = node.property("Name")->value();
203 ControlNameList::get_state (void)
205 XMLNode* node = new XMLNode("ControlNameList");
206 node->add_property("Name", _name);
212 ControlNameList::set_state (const XMLTree& tree, const XMLNode& node)
214 assert(node.name() == "ControlNameList");
215 _name = node.property("Name")->value();
217 for (XMLNodeList::const_iterator i = node.children().begin();
218 i != node.children().end(); ++i) {
219 if ((*i)->name() != "comment") {
220 boost::shared_ptr<Control> control(new Control());
221 control->set_state (tree, *(*i));
222 _controls.push_back(control);
230 PatchBank::get_state (void)
232 XMLNode* node = new XMLNode("PatchBank");
233 node->add_property("Name", _name);
234 XMLNode* patch_name_list = node->add_child("PatchNameList");
235 for (PatchNameList::iterator patch = _patch_name_list.begin();
236 patch != _patch_name_list.end();
238 patch_name_list->add_child_nocopy((*patch)->get_state());
245 PatchBank::set_state (const XMLTree& tree, const XMLNode& node)
247 assert(node.name() == "PatchBank");
248 _name = node.property("Name")->value();
250 XMLNode* commands = node.child("MIDICommands");
252 PatchPrimaryKey id (0, 0);
253 if (initialize_primary_key_from_commands (id, commands)) {
256 _number = id.bank_number;
259 XMLNode* patch_name_list = node.child("PatchNameList");
261 if (patch_name_list) {
262 const XMLNodeList patches = patch_name_list->children();
263 for (XMLNodeList::const_iterator i = patches.begin(); i != patches.end(); ++i) {
264 boost::shared_ptr<Patch> patch (new Patch (string(), 0, _number));
265 patch->set_state(tree, *(*i));
266 _patch_name_list.push_back(patch);
269 XMLNode* use_patch_name_list = node.child ("UsesPatchNameList");
270 if (use_patch_name_list) {
271 _patch_list_name = use_patch_name_list->property ("Name")->value();
273 error << "Patch without patch name list - patchfile will be ignored" << endmsg;
282 PatchBank::set_patch_name_list (const PatchNameList& pnl)
284 _patch_name_list = pnl;
285 _patch_list_name = "";
287 for (PatchNameList::iterator p = _patch_name_list.begin(); p != _patch_name_list.end(); p++) {
288 (*p)->set_bank_number (_number);
295 operator<< (std::ostream& os, const ChannelNameSet& cns)
297 os << "Channel Name Set: name = " << cns._name << endl
298 << "Map size " << cns._patch_map.size () << endl
299 << "List size " << cns._patch_list.size() << endl
300 << "Patch list name = [" << cns._patch_list_name << ']' << endl
301 << "Available channels : ";
302 for (set<uint8_t>::iterator x = cns._available_for_channels.begin(); x != cns._available_for_channels.end(); ++x) {
303 os << (int) (*x) << ' ';
307 for (ChannelNameSet::PatchBanks::const_iterator pbi = cns._patch_banks.begin(); pbi != cns._patch_banks.end(); ++pbi) {
308 os << "\tPatch Bank " << (*pbi)->name() << " with " << (*pbi)->patch_name_list().size() << " patches\n";
309 for (PatchBank::PatchNameList::const_iterator pni = (*pbi)->patch_name_list().begin(); pni != (*pbi)->patch_name_list().end(); ++pni) {
310 os << "\t\tPatch name " << (*pni)->name() << " prog " << (int) (*pni)->program_number() << " bank " << (*pni)->bank_number() << endl;
318 ChannelNameSet::set_patch_banks (const ChannelNameSet::PatchBanks& pb)
323 _patch_list.clear ();
324 _patch_list_name = "";
325 _available_for_channels.clear ();
327 for (PatchBanks::const_iterator pbi = _patch_banks.begin(); pbi != _patch_banks.end(); ++pbi) {
328 for (PatchBank::PatchNameList::const_iterator pni = (*pbi)->patch_name_list().begin(); pni != (*pbi)->patch_name_list().end(); ++pni) {
329 _patch_map[(*pni)->patch_primary_key()] = (*pni);
330 _patch_list.push_back ((*pni)->patch_primary_key());
334 for (uint8_t n = 0; n < 16; ++n) {
335 _available_for_channels.insert (n);
340 ChannelNameSet::use_patch_name_list (const PatchBank::PatchNameList& pnl)
342 for (PatchBank::PatchNameList::const_iterator p = pnl.begin(); p != pnl.end(); ++p) {
343 _patch_map[(*p)->patch_primary_key()] = (*p);
344 _patch_list.push_back ((*p)->patch_primary_key());
349 ChannelNameSet::get_state (void)
351 XMLNode* node = new XMLNode("ChannelNameSet");
352 node->add_property("Name", _name);
354 XMLNode* available_for_channels = node->add_child("AvailableForChannels");
355 assert(available_for_channels);
357 for (uint8_t channel = 0; channel < 16; ++channel) {
358 XMLNode* available_channel = available_for_channels->add_child("AvailableChannel");
359 assert(available_channel);
361 available_channel->add_property("Channel", (long) channel);
363 if (_available_for_channels.find(channel) != _available_for_channels.end()) {
364 available_channel->add_property("Available", "true");
366 available_channel->add_property("Available", "false");
370 for (PatchBanks::iterator patch_bank = _patch_banks.begin();
371 patch_bank != _patch_banks.end();
373 node->add_child_nocopy((*patch_bank)->get_state());
380 ChannelNameSet::set_state (const XMLTree& tree, const XMLNode& node)
382 assert(node.name() == "ChannelNameSet");
383 _name = node.property("Name")->value();
385 const XMLNodeList children = node.children();
386 for (XMLNodeList::const_iterator i = children.begin(); i != children.end(); ++i) {
389 if (node->name() == "AvailableForChannels") {
390 boost::shared_ptr<XMLSharedNodeList> channels =
391 tree.find("//AvailableChannel[@Available = 'true']/@Channel", node);
392 for(XMLSharedNodeList::const_iterator i = channels->begin();
393 i != channels->end();
395 _available_for_channels.insert(atoi((*i)->attribute_value().c_str()));
399 if (node->name() == "PatchBank") {
400 boost::shared_ptr<PatchBank> bank (new PatchBank ());
401 bank->set_state(tree, *node);
402 _patch_banks.push_back(bank);
403 const PatchBank::PatchNameList& patches = bank->patch_name_list();
404 for (PatchBank::PatchNameList::const_iterator patch = patches.begin();
405 patch != patches.end();
407 _patch_map[(*patch)->patch_primary_key()] = *patch;
408 _patch_list.push_back((*patch)->patch_primary_key());
417 CustomDeviceMode::set_state(const XMLTree& tree, const XMLNode& a_node)
419 assert(a_node.name() == "CustomDeviceMode");
421 _name = a_node.property("Name")->value();
423 boost::shared_ptr<XMLSharedNodeList> channel_name_set_assignments =
424 tree.find("//ChannelNameSetAssign", const_cast<XMLNode *>(&a_node));
425 for(XMLSharedNodeList::const_iterator i = channel_name_set_assignments->begin();
426 i != channel_name_set_assignments->end();
428 int channel = atoi((*i)->property("Channel")->value().c_str());
429 string name_set = (*i)->property("NameSet")->value();
430 assert( 1 <= channel && channel <= 16 );
431 _channel_name_set_assignments[channel - 1] = name_set;
437 CustomDeviceMode::get_state(void)
439 XMLNode* custom_device_mode = new XMLNode("CustomDeviceMode");
440 custom_device_mode->add_property("Name", _name);
441 XMLNode* channel_name_set_assignments =
442 custom_device_mode->add_child("ChannelNameSetAssignments");
443 for (int i = 0; i < 15 && !_channel_name_set_assignments[i].empty(); i++) {
444 XMLNode* channel_name_set_assign =
445 channel_name_set_assignments->add_child("ChannelNameSetAssign");
446 channel_name_set_assign->add_property("Channel", i + 1);
447 channel_name_set_assign->add_property("NameSet", _channel_name_set_assignments[i]);
450 return *custom_device_mode;
453 boost::shared_ptr<CustomDeviceMode>
454 MasterDeviceNames::custom_device_mode_by_name(std::string mode_name)
456 // can't assert this, since in many of the patch files the mode name is empty
457 //assert(mode_name != "");
458 return _custom_device_modes[mode_name];
461 boost::shared_ptr<ChannelNameSet>
462 MasterDeviceNames::channel_name_set_by_device_mode_and_channel(std::string mode, uint8_t channel)
464 boost::shared_ptr<CustomDeviceMode> cdm = custom_device_mode_by_name(mode);
465 boost::shared_ptr<ChannelNameSet> cns = _channel_name_sets[cdm->channel_name_set_name_by_channel(channel)];
469 boost::shared_ptr<Patch>
470 MasterDeviceNames::find_patch(std::string mode, uint8_t channel, PatchPrimaryKey& key)
472 return channel_name_set_by_device_mode_and_channel(mode, channel)->find_patch(key);
476 MasterDeviceNames::set_state(const XMLTree& tree, const XMLNode&)
479 boost::shared_ptr<XMLSharedNodeList> manufacturer = tree.find("//Manufacturer");
480 assert(manufacturer->size() == 1);
481 _manufacturer = manufacturer->front()->children().front()->content();
484 boost::shared_ptr<XMLSharedNodeList> models = tree.find("//Model");
485 assert(models->size() >= 1);
486 for (XMLSharedNodeList::iterator i = models->begin();
489 const XMLNodeList& contents = (*i)->children();
490 assert(contents.size() == 1);
491 XMLNode * content = *(contents.begin());
492 assert(content->is_content());
493 _models.push_back(content->content());
497 boost::shared_ptr<XMLSharedNodeList> custom_device_modes = tree.find("//CustomDeviceMode");
498 for (XMLSharedNodeList::iterator i = custom_device_modes->begin();
499 i != custom_device_modes->end();
501 boost::shared_ptr<CustomDeviceMode> custom_device_mode(new CustomDeviceMode());
502 custom_device_mode->set_state(tree, *(*i));
504 _custom_device_modes[custom_device_mode->name()] = custom_device_mode;
505 _custom_device_mode_names.push_back(custom_device_mode->name());
509 boost::shared_ptr<XMLSharedNodeList> channel_name_sets = tree.find("//ChannelNameSet");
510 for (XMLSharedNodeList::iterator i = channel_name_sets->begin();
511 i != channel_name_sets->end();
513 boost::shared_ptr<ChannelNameSet> channel_name_set(new ChannelNameSet());
514 channel_name_set->set_state(tree, *(*i));
515 _channel_name_sets[channel_name_set->name()] = channel_name_set;
519 boost::shared_ptr<XMLSharedNodeList> note_name_lists = tree.find("//NoteNameList");
520 for (XMLSharedNodeList::iterator i = note_name_lists->begin();
521 i != note_name_lists->end();
523 boost::shared_ptr<NoteNameList> note_name_list(new NoteNameList());
524 note_name_list->set_state (tree, *(*i));
525 _note_name_lists.push_back(note_name_list);
529 boost::shared_ptr<XMLSharedNodeList> control_name_lists = tree.find("//ControlNameList");
530 for (XMLSharedNodeList::iterator i = control_name_lists->begin();
531 i != control_name_lists->end();
533 boost::shared_ptr<ControlNameList> control_name_list(new ControlNameList());
534 control_name_list->set_state (tree, *(*i));
535 _control_name_lists.push_back(control_name_list);
538 // global/post-facto PatchNameLists
539 boost::shared_ptr<XMLSharedNodeList> patch_name_lists = tree.find("/child::MIDINameDocument/child::MasterDeviceNames/child::PatchNameList");
540 for (XMLSharedNodeList::iterator i = patch_name_lists->begin();
541 i != patch_name_lists->end();
544 PatchBank::PatchNameList patch_name_list;
545 const XMLNodeList patches = (*i)->children();
547 for (XMLNodeList::const_iterator p = patches.begin(); p != patches.end(); ++p) {
548 boost::shared_ptr<Patch> patch (new Patch ());
549 patch->set_state(tree, *(*p));
550 patch_name_list.push_back(patch);
553 if (!patch_name_list.empty()) {
554 _patch_name_lists[(*i)->property ("Name")->value()] = patch_name_list;
558 /* now traverse patches and hook up anything that used UsePatchNameList
559 * to the right patch list
562 for (ChannelNameSets::iterator cns = _channel_name_sets.begin(); cns != _channel_name_sets.end(); ++cns) {
563 ChannelNameSet::PatchBanks pbs = cns->second->patch_banks();
564 PatchNameLists::iterator p;
566 for (ChannelNameSet::PatchBanks::iterator pb = pbs.begin(); pb != pbs.end(); ++pb) {
567 std::string pln = (*pb)->patch_list_name();
569 if ((p = _patch_name_lists.find (pln)) != _patch_name_lists.end()) {
570 if ((*pb)->set_patch_name_list (p->second)) {
573 cns->second->use_patch_name_list (p->second);
575 error << string_compose ("Patch list name %1 was not found - patch file ignored", pln) << endmsg;
587 MasterDeviceNames::get_state(void)
589 static XMLNode nothing("<nothing>");
593 MIDINameDocument::MIDINameDocument (const string& filename)
595 if (!_document.read (filename)) {
596 throw failed_constructor ();
599 set_state (_document, *_document.root());
603 MIDINameDocument::set_state (const XMLTree& tree, const XMLNode&)
607 boost::shared_ptr<XMLSharedNodeList> author = tree.find("//Author");
608 if (author->size() < 1) {
609 error << "No author information in MIDNAM file" << endmsg;
613 if (author->front()->children().size() > 0) {
614 _author = author->front()->children().front()->content();
619 boost::shared_ptr<XMLSharedNodeList> master_device_names_list = tree.find ("//MasterDeviceNames");
621 for (XMLSharedNodeList::iterator i = master_device_names_list->begin();
622 i != master_device_names_list->end();
624 boost::shared_ptr<MasterDeviceNames> master_device_names(new MasterDeviceNames());
626 if (master_device_names->set_state(tree, *(*i))) {
630 for (MasterDeviceNames::Models::const_iterator model = master_device_names->models().begin();
631 model != master_device_names->models().end();
633 _master_device_names_list.insert(
634 std::pair<std::string, boost::shared_ptr<MasterDeviceNames> >
635 (*model, master_device_names));
637 _all_models.push_back(*model);
645 MIDINameDocument::get_state(void)
647 static XMLNode nothing("<nothing>");
651 boost::shared_ptr<MasterDeviceNames>
652 MIDINameDocument::master_device_names(const std::string& model)
654 MasterDeviceNamesList::const_iterator m = _master_device_names_list.find(model);
655 if (m != _master_device_names_list.end()) {
656 return boost::shared_ptr<MasterDeviceNames>(m->second);
658 return boost::shared_ptr<MasterDeviceNames>();
661 const char* general_midi_program_names[128] = {
662 "Acoustic Grand Piano",
663 "Bright Acoustic Piano",
664 "Electric Grand Piano",
686 "Acoustic Guitar (nylon)",
687 "Acoustic Guitar (steel)",
688 "Electric Guitar (jazz)",
689 "Electric Guitar (clean)",
690 "Electric Guitar (muted)",
695 "Electric Bass (finger)",
696 "Electric Bass (pick)",
744 "Lead 3 (calliope lead)",
745 "Lead 4 (chiff lead)",
749 "Lead 8 (bass + lead)",