Only show user-presets in favorite sidebar
[ardour.git] / libs / midi++2 / midnam_patch.cc
1 /*
2     Copyright (C) 2008 Hans Baier
3
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.
8
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.
13
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.
17 */
18
19 #include <stdlib.h>
20
21 #include <algorithm>
22 #include <iostream>
23
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"
29
30 using namespace std;
31 using PBD::error;
32
33 namespace MIDI
34 {
35
36 namespace Name
37 {
38
39 Patch::Patch (std::string name, uint8_t p_number, uint16_t b_number)
40         : _name (name)
41         , _id (p_number, b_number)
42 {
43 }
44
45 static int
46 string_to_int(const XMLTree& tree, const std::string& str)
47 {
48         char*     endptr = NULL;
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)
52                            << endmsg;
53         }
54         return i;
55 }
56
57 static int
58 initialize_primary_key_from_commands (
59         const XMLTree& tree, PatchPrimaryKey& id, const XMLNode* node)
60 {
61         uint16_t bank    = 0;
62         uint8_t  program = 0;
63
64         const XMLNodeList events = node->children();
65         for (XMLNodeList::const_iterator i = events.begin(); i != events.end(); ++i) {
66
67                 XMLNode* node = *i;
68                 if (node->name() == "ControlChange") {
69                         const string& control = node->property("Control")->value();
70                         const string& value   = node->property("Value")->value();
71
72                         if (control == "0") {
73                                 bank |= string_to_int(tree, value) << 7;
74                         } else if (control == "32") {
75                                 bank |= string_to_int(tree, value);
76                         }
77
78                 } else if (node->name() == "ProgramChange") {
79                         const string& number = node->property("Number")->value();
80                         assert(number != "");
81                         program = string_to_int(tree, number);
82                 }
83         }
84
85         id = PatchPrimaryKey(program, bank);
86         return 0;
87 }
88
89 XMLNode&
90 Patch::get_state (void)
91 {
92         XMLNode* node = new XMLNode("Patch");
93
94         /* XXX this is totally wrong */
95
96         node->set_property("Number", _id.program());
97         node->set_property("Name",   _name);
98
99         /*
100         typedef std::list< boost::shared_ptr< Evoral::Event<Temporal::Beats> > > PatchMidiCommands;
101         XMLNode* commands = node->add_child("PatchMIDICommands");
102         for (PatchMidiCommands::const_iterator event = _patch_midi_commands.begin();
103             event != _patch_midi_commands.end();
104             ++event) {
105                 commands->add_child_copy(Evoral::MIDIXML::midi_to_xml(*event));
106         }
107         */
108
109         return *node;
110 }
111
112 int
113 Patch::set_state (const XMLTree& tree, const XMLNode& node)
114 {
115         if (node.name() != "Patch") {
116                 cerr << "Incorrect node type '" << node.name() << "' handed to Patch" << endl;
117                 return -1;
118         }
119
120         /* Note there is a "Number" attribute, but it's really more like a label
121            and is often not numeric.  We currently do not use it. */
122
123         const XMLProperty* program_change = node.property("ProgramChange");
124         if (program_change) {
125                 _id = PatchPrimaryKey(string_to_int(tree, program_change->value()), _id.bank());
126         }
127
128         const XMLProperty* name = node.property("Name");
129         if (!name) {
130                 return -1;
131         }
132         _name = name->value();
133
134         XMLNode* commands = node.child("PatchMIDICommands");
135         if (commands) {
136                 if (initialize_primary_key_from_commands(tree, _id, commands) &&
137                     !program_change) {
138                         return -1;  // Failed to find a program number anywhere
139                 }
140         }
141
142         XMLNode* use_note_name_list = node.child("UsesNoteNameList");
143         if (use_note_name_list) {
144                 _note_list_name = use_note_name_list->property ("Name")->value();
145         }
146
147         return 0;
148 }
149
150 XMLNode&
151 Note::get_state (void)
152 {
153         XMLNode* node = new XMLNode("Note");
154         node->set_property("Number", _number);
155         node->set_property("Name",   _name);
156
157         return *node;
158 }
159
160 int
161 Note::set_state (const XMLTree& tree, const XMLNode& node)
162 {
163         assert(node.name() == "Note");
164
165         const int num = string_to_int(tree, node.property("Number")->value());
166         if (num > 127) {
167                 PBD::warning << string_compose("%1: Note number %2 (%3) out of range",
168                                                tree.filename(), num, _name)
169                              << endmsg;
170                 return -1;
171         }
172
173         _number = num;
174         _name   = node.property("Name")->value();
175
176         return 0;
177 }
178
179 XMLNode&
180 NoteNameList::get_state (void)
181 {
182         XMLNode* node = new XMLNode("NoteNameList");
183         node->set_property("Name", _name);
184
185         return *node;
186 }
187
188 static void
189 add_note_from_xml (NoteNameList::Notes& notes, const XMLTree& tree, const XMLNode& node)
190 {
191         boost::shared_ptr<Note> note(new Note());
192         if (!note->set_state (tree, node)) {
193                 if (!notes[note->number()]) {
194                         notes[note->number()] = note;
195                 } else {
196                         PBD::warning
197                                 << string_compose("%1: Duplicate note number %2 (%3) ignored",
198                                                   tree.filename(), (int)note->number(), note->name())
199                                 << endmsg;
200                 }
201         }
202 }
203
204 int
205 NoteNameList::set_state (const XMLTree& tree, const XMLNode& node)
206 {
207         assert(node.name() == "NoteNameList");
208         _name = node.property("Name")->value();
209         _notes.clear();
210         _notes.resize(128);
211
212         for (XMLNodeList::const_iterator i = node.children().begin();
213              i != node.children().end(); ++i) {
214                 if ((*i)->name() == "Note") {
215                         add_note_from_xml(_notes, tree, **i);
216                 } else if ((*i)->name() == "NoteGroup") {
217                         for (XMLNodeList::const_iterator j = (*i)->children().begin();
218                              j != (*i)->children().end(); ++j) {
219                                 if ((*j)->name() == "Note") {
220                                         add_note_from_xml(_notes, tree, **j);
221                                 } else {
222                                         PBD::warning << string_compose("%1: Invalid NoteGroup child %2 ignored",
223                                                                        tree.filename(), (*j)->name())
224                                                      << endmsg;
225                                 }
226                         }
227                 }
228         }
229
230         return 0;
231 }
232
233 XMLNode&
234 Control::get_state (void)
235 {
236         XMLNode* node = new XMLNode("Control");
237         node->set_property("Type",   _type);
238         node->set_property("Number", _number);
239         node->set_property("Name",   _name);
240
241         return *node;
242 }
243
244 int
245 Control::set_state (const XMLTree& tree, const XMLNode& node)
246 {
247         assert(node.name() == "Control");
248         if (node.property("Type")) {
249                 _type = node.property("Type")->value();
250         } else {
251                 _type = "7bit";
252         }
253         _number = string_to_int(tree, node.property("Number")->value());
254         _name   = node.property("Name")->value();
255
256         for (XMLNodeList::const_iterator i = node.children().begin();
257              i != node.children().end(); ++i) {
258                 if ((*i)->name() == "Values") {
259                         // <Values> has Min and Max properties, but we don't care
260                         for (XMLNodeList::const_iterator j = (*i)->children().begin();
261                              j != (*i)->children().end(); ++j) {
262                                 if ((*j)->name() == "ValueNameList") {
263                                         _value_name_list = boost::shared_ptr<ValueNameList>(new ValueNameList());
264                                         _value_name_list->set_state(tree, **j);
265                                 } else if ((*j)->name() == "UsesValueNameList") {
266                                         _value_name_list_name = (*j)->property("Name")->value();
267                                 }
268                         }
269                 }
270         }
271
272         return 0;
273 }
274
275 XMLNode&
276 ControlNameList::get_state (void)
277 {
278         XMLNode* node = new XMLNode("ControlNameList");
279         node->set_property("Name", _name);
280
281         return *node;
282 }
283
284 int
285 ControlNameList::set_state (const XMLTree& tree, const XMLNode& node)
286 {
287         assert(node.name() == "ControlNameList");
288         _name = node.property("Name")->value();
289
290         _controls.clear();
291         for (XMLNodeList::const_iterator i = node.children().begin();
292              i != node.children().end(); ++i) {
293                 if ((*i)->name() == "Control") {
294                         boost::shared_ptr<Control> control(new Control());
295                         control->set_state (tree, *(*i));
296                         if (_controls.find(control->number()) == _controls.end()) {
297                                 _controls.insert(make_pair(control->number(), control));
298                         } else {
299                                 PBD::warning << string_compose("%1: Duplicate control %2 ignored",
300                                                                tree.filename(), control->number())
301                                              << endmsg;
302                         }
303                 }
304         }
305
306         return 0;
307 }
308
309 boost::shared_ptr<const Control>
310 ControlNameList::control(uint16_t num) const
311 {
312         Controls::const_iterator i = _controls.find(num);
313         if (i != _controls.end()) {
314                 return i->second;
315         }
316         return boost::shared_ptr<const Control>();
317 }
318
319 XMLNode&
320 Value::get_state (void)
321 {
322         XMLNode* node = new XMLNode("Value");
323         node->set_property("Number", _number);
324         node->set_property("Name",   _name);
325
326         return *node;
327 }
328
329 int
330 Value::set_state (const XMLTree& tree, const XMLNode& node)
331 {
332         assert(node.name() == "Value");
333         _number = string_to_int(tree, node.property("Number")->value());
334         _name   = node.property("Name")->value();
335
336         return 0;
337 }
338
339 XMLNode&
340 ValueNameList::get_state (void)
341 {
342         XMLNode* node = new XMLNode("ValueNameList");
343         node->set_property("Name", _name);
344
345         return *node;
346 }
347
348 int
349 ValueNameList::set_state (const XMLTree& tree, const XMLNode& node)
350 {
351         assert(node.name() == "ValueNameList");
352         const XMLProperty* name_prop = node.property("Name");
353         if (name_prop) {
354                 // May be anonymous if written inline within a single <Control> tag
355                 _name = name_prop->value();
356         }
357
358         _values.clear();
359         for (XMLNodeList::const_iterator i = node.children().begin();
360              i != node.children().end(); ++i) {
361                 if ((*i)->name() == "Value") {
362                         boost::shared_ptr<Value> value(new Value());
363                         value->set_state (tree, *(*i));
364                         if (_values.find(value->number()) == _values.end()) {
365                                 _values.insert(make_pair(value->number(), value));
366                         } else {
367                                 PBD::warning << string_compose("%1: Duplicate value %2 ignored",
368                                                                tree.filename(), value->number())
369                                              << endmsg;
370                         }
371                 }
372         }
373
374         return 0;
375 }
376
377 boost::shared_ptr<const Value>
378 ValueNameList::value(uint16_t num) const
379 {
380         Values::const_iterator i = _values.find(num);
381         if (i != _values.end()) {
382                 return i->second;
383         }
384         return boost::shared_ptr<const Value>();
385 }
386
387 boost::shared_ptr<const Value>
388 ValueNameList::max_value_below(uint16_t num) const
389 {
390         Values::const_iterator i = _values.lower_bound(num);
391         if (i->first == num) {
392                 // Exact match
393                 return i->second;
394         } else if (i == _values.begin()) {
395                 // No value is < num
396                 return boost::shared_ptr<const Value>();
397         } else {
398                 // Found the smallest element >= num, so the previous one is our result
399                 --i;
400                 return i->second;
401         }
402 }
403
404 XMLNode&
405 PatchBank::get_state (void)
406 {
407         XMLNode* node = new XMLNode("PatchBank");
408         node->set_property("Name",   _name);
409         XMLNode* patch_name_list = node->add_child("PatchNameList");
410         for (PatchNameList::iterator patch = _patch_name_list.begin();
411             patch != _patch_name_list.end();
412             ++patch) {
413                 patch_name_list->add_child_nocopy((*patch)->get_state());
414         }
415
416         return *node;
417 }
418
419 int
420 PatchBank::set_state (const XMLTree& tree, const XMLNode& node)
421 {
422         assert(node.name() == "PatchBank");
423         _name   = node.property("Name")->value();
424
425         XMLNode* commands = node.child("MIDICommands");
426         if (commands) {
427                 PatchPrimaryKey id (0, 0);
428                 if (initialize_primary_key_from_commands (tree, id, commands)) {
429                         return -1;
430                 }
431                 _number = id.bank();
432         }
433
434         XMLNode* patch_name_list = node.child("PatchNameList");
435
436         if (patch_name_list) {
437                 const XMLNodeList patches = patch_name_list->children();
438                 for (XMLNodeList::const_iterator i = patches.begin(); i != patches.end(); ++i) {
439                         boost::shared_ptr<Patch> patch (new Patch (string(), 0, _number));
440                         if (0 == patch->set_state(tree, *(*i))) {
441                                 _patch_name_list.push_back(patch);
442                         }
443                 }
444         } else {
445                 XMLNode* use_patch_name_list = node.child ("UsesPatchNameList");
446                 if (use_patch_name_list) {
447                         _patch_list_name = use_patch_name_list->property ("Name")->value();
448                 } else {
449                         error << "Patch without patch name list - patchfile will be ignored" << endmsg;
450                         return -1;
451                 }
452         }
453
454         return 0;
455 }
456
457 int
458 PatchBank::set_patch_name_list (const PatchNameList& pnl)
459 {
460         _patch_name_list = pnl;
461         _patch_list_name = "";
462
463         for (PatchNameList::iterator p = _patch_name_list.begin(); p != _patch_name_list.end(); p++) {
464                 (*p)->set_bank_number (_number);
465         }
466
467         return 0;
468 }
469
470 std::ostream&
471 operator<< (std::ostream& os, const ChannelNameSet& cns)
472 {
473         os << "Channel Name Set: name = " << cns._name << endl
474            << "Map size " << cns._patch_map.size () << endl
475            << "List size " << cns._patch_list.size() << endl
476            << "Patch list name = [" << cns._patch_list_name << ']' << endl
477            << "Available channels : ";
478         for (set<uint8_t>::const_iterator x = cns._available_for_channels.begin(); x != cns._available_for_channels.end(); ++x) {
479                 os << (int) (*x) << ' ';
480         }
481         os << endl;
482
483         for (ChannelNameSet::PatchBanks::const_iterator pbi = cns._patch_banks.begin(); pbi != cns._patch_banks.end(); ++pbi) {
484                 os << "\tPatch Bank " << (*pbi)->name() << " with " << (*pbi)->patch_name_list().size() << " patches\n";
485                 for (PatchNameList::const_iterator pni = (*pbi)->patch_name_list().begin(); pni != (*pbi)->patch_name_list().end(); ++pni) {
486                         os << "\t\tPatch name " << (*pni)->name() << " prog " << (int) (*pni)->program_number() << " bank " << (*pni)->bank_number() << endl;
487                 }
488         }
489
490         return os;
491 }
492
493 void
494 ChannelNameSet::set_patch_banks (const ChannelNameSet::PatchBanks& pb)
495 {
496         _patch_banks = pb;
497
498         _patch_map.clear ();
499         _patch_list.clear ();
500         _patch_list_name = "";
501         _available_for_channels.clear ();
502
503         for (PatchBanks::const_iterator pbi = _patch_banks.begin(); pbi != _patch_banks.end(); ++pbi) {
504                 for (PatchNameList::const_iterator pni = (*pbi)->patch_name_list().begin(); pni != (*pbi)->patch_name_list().end(); ++pni) {
505                         _patch_map[(*pni)->patch_primary_key()] = (*pni);
506                         _patch_list.push_back ((*pni)->patch_primary_key());
507                 }
508         }
509
510         for (uint8_t n = 0; n < 16; ++n) {
511                 _available_for_channels.insert (n);
512         }
513 }
514
515 void
516 ChannelNameSet::use_patch_name_list (const PatchNameList& pnl)
517 {
518         for (PatchNameList::const_iterator p = pnl.begin(); p != pnl.end(); ++p) {
519                 _patch_map[(*p)->patch_primary_key()] = (*p);
520                 _patch_list.push_back ((*p)->patch_primary_key());
521         }
522 }
523
524 XMLNode&
525 ChannelNameSet::get_state (void)
526 {
527         XMLNode* node = new XMLNode("ChannelNameSet");
528         node->set_property("Name",   _name);
529
530         XMLNode* available_for_channels = node->add_child("AvailableForChannels");
531         assert(available_for_channels);
532
533         for (uint8_t channel = 0; channel < 16; ++channel) {
534                 XMLNode* available_channel = available_for_channels->add_child("AvailableChannel");
535                 assert(available_channel);
536
537                 available_channel->set_property("Channel", channel);
538
539                 if (_available_for_channels.find(channel) != _available_for_channels.end()) {
540                         available_channel->set_property("Available", "true");
541                 } else {
542                         available_channel->set_property("Available", "false");
543                 }
544         }
545
546         for (PatchBanks::iterator patch_bank = _patch_banks.begin();
547             patch_bank != _patch_banks.end();
548             ++patch_bank) {
549                 node->add_child_nocopy((*patch_bank)->get_state());
550         }
551
552         return *node;
553 }
554
555 int
556 ChannelNameSet::set_state (const XMLTree& tree, const XMLNode& node)
557 {
558         assert(node.name() == "ChannelNameSet");
559         _name = node.property("Name")->value();
560
561         const XMLNodeList children = node.children();
562         for (XMLNodeList::const_iterator i = children.begin(); i != children.end(); ++i) {
563                 XMLNode* node = *i;
564                 assert(node);
565                 if (node->name() == "AvailableForChannels") {
566                         boost::shared_ptr<XMLSharedNodeList> channels =
567                                 tree.find("//AvailableChannel[@Available = 'true']/@Channel", node);
568                         for (XMLSharedNodeList::const_iterator i = channels->begin();
569                             i != channels->end();
570                             ++i) {
571                                 try {
572                                         _available_for_channels.insert(
573                                                         string_to_int(tree, (*i)->attribute_value()));
574                                 } catch (XMLException &e) {
575                                         cerr << "ChannelNameSet::set_state: " << e.what () << endl;
576                                 }
577                         }
578                 } else if (node->name() == "PatchBank") {
579                         boost::shared_ptr<PatchBank> bank (new PatchBank ());
580                         bank->set_state(tree, *node);
581                         _patch_banks.push_back(bank);
582                         const PatchNameList& patches = bank->patch_name_list();
583                         for (PatchNameList::const_iterator patch = patches.begin();
584                              patch != patches.end();
585                              ++patch) {
586                                 _patch_map[(*patch)->patch_primary_key()] = *patch;
587                                 _patch_list.push_back((*patch)->patch_primary_key());
588                         }
589                 } else if (node->name() == "UsesNoteNameList") {
590                         _note_list_name = node->property ("Name")->value();
591                 } else if (node->name() == "UsesControlNameList") {
592                         _control_list_name = node->property ("Name")->value();
593                 }
594         }
595
596         return 0;
597 }
598
599 int
600 CustomDeviceMode::set_state(const XMLTree& tree, const XMLNode& a_node)
601 {
602         assert(a_node.name() == "CustomDeviceMode");
603
604         _name = a_node.property("Name")->value();
605
606         boost::shared_ptr<XMLSharedNodeList> channel_name_set_assignments =
607                 tree.find("//ChannelNameSetAssign", const_cast<XMLNode *>(&a_node));
608         for (XMLSharedNodeList::const_iterator i = channel_name_set_assignments->begin();
609             i != channel_name_set_assignments->end();
610             ++i) {
611                 const int     channel  = string_to_int(tree, (*i)->property("Channel")->value());
612                 const string& name_set = (*i)->property("NameSet")->value();
613                 assert( 1 <= channel && channel <= 16 );
614                 _channel_name_set_assignments[channel - 1] = name_set;
615         }
616         return 0;
617 }
618
619 XMLNode&
620 CustomDeviceMode::get_state(void)
621 {
622         XMLNode* custom_device_mode = new XMLNode("CustomDeviceMode");
623         custom_device_mode->set_property("Name",   _name);
624         XMLNode* channel_name_set_assignments =
625                 custom_device_mode->add_child("ChannelNameSetAssignments");
626         for (int i = 0; i < 15 && !_channel_name_set_assignments[i].empty(); i++) {
627                 XMLNode* channel_name_set_assign =
628                         channel_name_set_assignments->add_child("ChannelNameSetAssign");
629                 channel_name_set_assign->set_property("Channel", i + 1);
630                 channel_name_set_assign->set_property("NameSet", _channel_name_set_assignments[i]);
631         }
632
633         return *custom_device_mode;
634 }
635
636 boost::shared_ptr<const ValueNameList>
637 MasterDeviceNames::value_name_list_by_control(const std::string& mode, uint8_t channel, uint8_t number)
638 {
639         boost::shared_ptr<ChannelNameSet> chan_names = channel_name_set_by_channel(mode, channel);
640         if (!chan_names) {
641                 return boost::shared_ptr<const ValueNameList>();
642         }
643
644         boost::shared_ptr<ControlNameList> control_names = control_name_list(chan_names->control_list_name());
645         if (!control_names) {
646                 return boost::shared_ptr<const ValueNameList>();
647         }
648
649         boost::shared_ptr<const Control> control = control_names->control(number);
650         if (!control) {
651                 return boost::shared_ptr<const ValueNameList>();
652         }
653
654         if (!control->value_name_list_name().empty()) {
655                 return value_name_list(control->value_name_list_name());
656         } else {
657                 return control->value_name_list();
658         }
659 }
660
661 boost::shared_ptr<CustomDeviceMode>
662 MasterDeviceNames::custom_device_mode_by_name(const std::string& mode_name)
663 {
664         return _custom_device_modes[mode_name];
665 }
666
667 boost::shared_ptr<ChannelNameSet>
668 MasterDeviceNames::channel_name_set_by_channel(const std::string& mode, uint8_t channel)
669 {
670         boost::shared_ptr<CustomDeviceMode> cdm = custom_device_mode_by_name(mode);
671         boost::shared_ptr<ChannelNameSet> cns =  _channel_name_sets[cdm->channel_name_set_name_by_channel(channel)];
672         return cns;
673 }
674
675 boost::shared_ptr<Patch>
676 MasterDeviceNames::find_patch(const std::string& mode, uint8_t channel, const PatchPrimaryKey& key)
677 {
678         boost::shared_ptr<ChannelNameSet> cns = channel_name_set_by_channel(mode, channel);
679         if (!cns) return boost::shared_ptr<Patch>();
680         return cns->find_patch(key);
681 }
682
683 boost::shared_ptr<ChannelNameSet>
684 MasterDeviceNames::channel_name_set(const std::string& name)
685 {
686         ChannelNameSets::const_iterator i = _channel_name_sets.find(name);
687         if (i != _channel_name_sets.end()) {
688                 return i->second;
689         }
690         return boost::shared_ptr<ChannelNameSet>();
691 }
692
693 boost::shared_ptr<ControlNameList>
694 MasterDeviceNames::control_name_list(const std::string& name)
695 {
696         ControlNameLists::const_iterator i = _control_name_lists.find(name);
697         if (i != _control_name_lists.end()) {
698                 return i->second;
699         }
700         return boost::shared_ptr<ControlNameList>();
701 }
702
703 boost::shared_ptr<ValueNameList>
704 MasterDeviceNames::value_name_list(const std::string& name)
705 {
706         ValueNameLists::const_iterator i = _value_name_lists.find(name);
707         if (i != _value_name_lists.end()) {
708                 return i->second;
709         }
710         return boost::shared_ptr<ValueNameList>();
711 }
712
713 boost::shared_ptr<NoteNameList>
714 MasterDeviceNames::note_name_list(const std::string& name)
715 {
716         NoteNameLists::const_iterator i = _note_name_lists.find(name);
717         if (i != _note_name_lists.end()) {
718                 return i->second;
719         }
720         return boost::shared_ptr<NoteNameList>();
721 }
722
723 std::string
724 MasterDeviceNames::note_name(const std::string& mode_name,
725                              uint8_t            channel,
726                              uint16_t           bank,
727                              uint8_t            program,
728                              uint8_t            number)
729 {
730         if (number > 127) {
731                 return "";
732         }
733
734         boost::shared_ptr<const NoteNameList> note_names;
735         boost::shared_ptr<const Patch> patch(
736                 find_patch(mode_name, channel, PatchPrimaryKey(program, bank)));
737         if (patch) {
738                 note_names = note_name_list(patch->note_list_name());
739         }
740
741         if (!note_names) {
742                 /* No note names specific to this patch, check the ChannelNameSet */
743                 boost::shared_ptr<ChannelNameSet> chan_names = channel_name_set_by_channel(
744                         mode_name, channel);
745                 if (chan_names) {
746                         note_names = note_name_list(chan_names->note_list_name());
747                 }
748         }
749         if (!note_names) {
750                 return "";
751         }
752
753         boost::shared_ptr<const Note> note(note_names->notes()[number]);
754         return note ? note->name() : "";
755 }
756
757 int
758 MasterDeviceNames::set_state(const XMLTree& tree, const XMLNode&)
759 {
760         // Manufacturer
761         boost::shared_ptr<XMLSharedNodeList> manufacturer = tree.find("//Manufacturer");
762         assert(manufacturer->size() == 1);
763         _manufacturer = manufacturer->front()->children().front()->content();
764
765         // Models
766         boost::shared_ptr<XMLSharedNodeList> models = tree.find("//Model");
767         assert(models->size() >= 1);
768         for (XMLSharedNodeList::iterator i = models->begin();
769              i != models->end();
770              ++i) {
771                 const XMLNodeList& contents = (*i)->children();
772                 assert(contents.size() == 1);
773                 XMLNode * content = *(contents.begin());
774                 assert(content->is_content());
775                 _models.insert(content->content());
776         }
777
778         // CustomDeviceModes
779         boost::shared_ptr<XMLSharedNodeList> custom_device_modes = tree.find("//CustomDeviceMode");
780         for (XMLSharedNodeList::iterator i = custom_device_modes->begin();
781              i != custom_device_modes->end();
782              ++i) {
783                 boost::shared_ptr<CustomDeviceMode> custom_device_mode(new CustomDeviceMode());
784                 custom_device_mode->set_state(tree, *(*i));
785
786                 _custom_device_modes[custom_device_mode->name()] = custom_device_mode;
787                 _custom_device_mode_names.push_back(custom_device_mode->name());
788         }
789
790         // ChannelNameSets
791         boost::shared_ptr<XMLSharedNodeList> channel_name_sets = tree.find("//ChannelNameSet");
792         for (XMLSharedNodeList::iterator i = channel_name_sets->begin();
793              i != channel_name_sets->end();
794              ++i) {
795                 boost::shared_ptr<ChannelNameSet> channel_name_set(new ChannelNameSet());
796                 channel_name_set->set_state(tree, *(*i));
797                 _channel_name_sets[channel_name_set->name()] = channel_name_set;
798         }
799
800         // NoteNameLists
801         boost::shared_ptr<XMLSharedNodeList> note_name_lists = tree.find("//NoteNameList");
802         for (XMLSharedNodeList::iterator i = note_name_lists->begin();
803              i != note_name_lists->end();
804              ++i) {
805                 boost::shared_ptr<NoteNameList> note_name_list(new NoteNameList());
806                 note_name_list->set_state (tree, *(*i));
807                 _note_name_lists[note_name_list->name()] = note_name_list;
808         }
809
810         // ControlNameLists
811         boost::shared_ptr<XMLSharedNodeList> control_name_lists = tree.find("//ControlNameList");
812         for (XMLSharedNodeList::iterator i = control_name_lists->begin();
813              i != control_name_lists->end();
814              ++i) {
815                 boost::shared_ptr<ControlNameList> control_name_list(new ControlNameList());
816                 control_name_list->set_state (tree, *(*i));
817                 _control_name_lists[control_name_list->name()] = control_name_list;
818         }
819
820         // ValueNameLists
821         boost::shared_ptr<XMLSharedNodeList> value_name_lists = tree.find("/child::MIDINameDocument/child::MasterDeviceNames/child::ValueNameList");
822         for (XMLSharedNodeList::iterator i = value_name_lists->begin();
823              i != value_name_lists->end();
824              ++i) {
825                 boost::shared_ptr<ValueNameList> value_name_list(new ValueNameList());
826                 value_name_list->set_state (tree, *(*i));
827                 _value_name_lists[value_name_list->name()] = value_name_list;
828         }
829
830         // global/post-facto PatchNameLists
831         boost::shared_ptr<XMLSharedNodeList> patch_name_lists = tree.find("/child::MIDINameDocument/child::MasterDeviceNames/child::PatchNameList");
832         for (XMLSharedNodeList::iterator i = patch_name_lists->begin();
833              i != patch_name_lists->end();
834              ++i) {
835
836                 PatchNameList patch_name_list;
837                 const XMLNodeList patches = (*i)->children();
838
839                 for (XMLNodeList::const_iterator p = patches.begin(); p != patches.end(); ++p) {
840                         boost::shared_ptr<Patch> patch (new Patch ());
841                         patch->set_state(tree, *(*p));
842                         patch_name_list.push_back(patch);
843                 }
844
845                 if (!patch_name_list.empty()) {
846                         _patch_name_lists[(*i)->property ("Name")->value()] = patch_name_list;
847                 }
848         }
849
850         /* now traverse patches and hook up anything that used UsePatchNameList
851          * to the right patch list
852          */
853
854         for (ChannelNameSets::iterator cns = _channel_name_sets.begin(); cns != _channel_name_sets.end(); ++cns) {
855                 ChannelNameSet::PatchBanks pbs = cns->second->patch_banks();
856                 PatchNameLists::iterator p;
857
858                 for (ChannelNameSet::PatchBanks::iterator pb = pbs.begin(); pb != pbs.end(); ++pb) {
859                         const std::string& pln = (*pb)->patch_list_name();
860                         if (!pln.empty()) {
861                                 if ((p = _patch_name_lists.find (pln)) != _patch_name_lists.end()) {
862                                         if ((*pb)->set_patch_name_list (p->second)) {
863                                                 return -1;
864                                         }
865                                         cns->second->use_patch_name_list (p->second);
866                                 } else {
867                                         error << string_compose ("Patch list name %1 was not found - patch file ignored", pln) << endmsg;
868                                         return -1;
869                                 }
870                         }
871                 }
872
873         }
874
875         return 0;
876 }
877
878 XMLNode&
879 MasterDeviceNames::get_state(void)
880 {
881         static XMLNode nothing("<nothing>");
882         return nothing;
883 }
884
885 MIDINameDocument::MIDINameDocument (const string& file_path)
886         : _file_path(file_path)
887 {
888         XMLTree document;
889         if (!document.read (file_path)) {
890                 throw failed_constructor ();
891         }
892
893         document.set_filename (file_path);
894         set_state (document, *document.root());
895 }
896
897 int
898 MIDINameDocument::set_state (const XMLTree& tree, const XMLNode&)
899 {
900         // Author
901
902         boost::shared_ptr<XMLSharedNodeList> author = tree.find("//Author");
903         if (author->size() < 1) {
904                 error << "No author information in MIDNAM file" << endmsg;
905                 return -1;
906         }
907
908         if (author->front()->children().size() > 0) {
909                 _author = author->front()->children().front()->content();
910         }
911
912         // MasterDeviceNames
913
914         boost::shared_ptr<XMLSharedNodeList> master_device_names_list = tree.find ("//MasterDeviceNames");
915
916         for (XMLSharedNodeList::iterator i = master_device_names_list->begin();
917              i != master_device_names_list->end();
918              ++i) {
919                 boost::shared_ptr<MasterDeviceNames> master_device_names(new MasterDeviceNames());
920
921                 if (master_device_names->set_state(tree, *(*i))) {
922                         return -1;
923                 }
924
925                 for (MasterDeviceNames::Models::const_iterator model = master_device_names->models().begin();
926                      model != master_device_names->models().end();
927                      ++model) {
928                         _master_device_names_list.insert(
929                                 std::pair<std::string, boost::shared_ptr<MasterDeviceNames> >
930                                 (*model,      master_device_names));
931
932                         _all_models.insert(*model);
933                 }
934         }
935
936         return 0;
937 }
938
939 XMLNode&
940 MIDINameDocument::get_state(void)
941 {
942         static XMLNode nothing("<nothing>");
943         return nothing;
944 }
945
946 boost::shared_ptr<MasterDeviceNames>
947 MIDINameDocument::master_device_names(const std::string& model)
948 {
949         MasterDeviceNamesList::const_iterator m = _master_device_names_list.find(model);
950         if (m != _master_device_names_list.end()) {
951                 return boost::shared_ptr<MasterDeviceNames>(m->second);
952         }
953         return boost::shared_ptr<MasterDeviceNames>();
954 }
955
956 const char* general_midi_program_names[128] = {
957         "Acoustic Grand Piano",
958         "Bright Acoustic Piano",
959         "Electric Grand Piano",
960         "Honky-tonk Piano",
961         "Rhodes Piano",
962         "Chorused Piano",
963         "Harpsichord",
964         "Clavinet",
965         "Celesta",
966         "Glockenspiel",
967         "Music Box",
968         "Vibraphone",
969         "Marimba",
970         "Xylophone",
971         "Tubular Bells",
972         "Dulcimer",
973         "Hammond Organ",
974         "Percussive Organ",
975         "Rock Organ",
976         "Church Organ",
977         "Reed Organ",
978         "Accordion",
979         "Harmonica",
980         "Tango Accordion",
981         "Acoustic Guitar (nylon)",
982         "Acoustic Guitar (steel)",
983         "Electric Guitar (jazz)",
984         "Electric Guitar (clean)",
985         "Electric Guitar (muted)",
986         "Overdriven Guitar",
987         "Distortion Guitar",
988         "Guitar Harmonics",
989         "Acoustic Bass",
990         "Electric Bass (finger)",
991         "Electric Bass (pick)",
992         "Fretless Bass",
993         "Slap Bass 1",
994         "Slap Bass 2",
995         "Synth Bass 1",
996         "Synth Bass 2",
997         "Violin",
998         "Viola",
999         "Cello",
1000         "Contrabass",
1001         "Tremolo Strings",
1002         "Pizzicato Strings",
1003         "Orchestral Harp",
1004         "Timpani",
1005         "String Ensemble 1",
1006         "String Ensemble 2",
1007         "SynthStrings 1",
1008         "SynthStrings 2",
1009         "Choir Aahs",
1010         "Voice Oohs",
1011         "Synth Voice",
1012         "Orchestra Hit",
1013         "Trumpet",
1014         "Trombone",
1015         "Tuba",
1016         "Muted Trumpet",
1017         "French Horn",
1018         "Brass Section",
1019         "Synth Brass 1",
1020         "Synth Brass 2",
1021         "Soprano Sax",
1022         "Alto Sax",
1023         "Tenor Sax",
1024         "Baritone Sax",
1025         "Oboe",
1026         "English Horn",
1027         "Bassoon",
1028         "Clarinet",
1029         "Piccolo",
1030         "Flute",
1031         "Recorder",
1032         "Pan Flute",
1033         "Bottle Blow",
1034         "Shakuhachi",
1035         "Whistle",
1036         "Ocarina",
1037         "Lead 1 (square)",
1038         "Lead 2 (sawtooth)",
1039         "Lead 3 (calliope lead)",
1040         "Lead 4 (chiff lead)",
1041         "Lead 5 (charang)",
1042         "Lead 6 (voice)",
1043         "Lead 7 (fifths)",
1044         "Lead 8 (bass + lead)",
1045         "Pad 1 (new age)",
1046         "Pad 2 (warm)",
1047         "Pad 3 (polysynth)",
1048         "Pad 4 (choir)",
1049         "Pad 5 (bowed)",
1050         "Pad 6 (metallic)",
1051         "Pad 7 (halo)",
1052         "Pad 8 (sweep)",
1053         "FX 1 (rain)",
1054         "FX 2 (soundtrack)",
1055         "FX 3 (crystal)",
1056         "FX 4 (atmosphere)",
1057         "FX 5 (brightness)",
1058         "FX 6 (goblins)",
1059         "FX 7 (echoes)",
1060         "FX 8 (sci-fi)",
1061         "Sitar",
1062         "Banjo",
1063         "Shamisen",
1064         "Koto",
1065         "Kalimba",
1066         "Bagpipe",
1067         "Fiddle",
1068         "Shanai",
1069         "Tinkle Bell",
1070         "Agogo",
1071         "Steel Drums",
1072         "Woodblock",
1073         "Taiko Drum",
1074         "Melodic Tom",
1075         "Synth Drum",
1076         "Reverse Cymbal",
1077         "Guitar Fret Noise",
1078         "Breath Noise",
1079         "Seashore",
1080         "Bird Tweet",
1081         "Telephone Ring",
1082         "Helicopter",
1083         "Applause",
1084         "Gunshot",
1085 };
1086
1087 } //namespace Name
1088
1089 } //namespace MIDI
1090