Merge branch 'master' into windows
[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         id.bank_number = 0;
62
63         const XMLNodeList events = node->children();
64         for (XMLNodeList::const_iterator i = events.begin(); i != events.end(); ++i) {
65
66                 XMLNode* node = *i;
67                 if (node->name() == "ControlChange") {
68                         const string& control = node->property("Control")->value();
69                         const string& value   = node->property("Value")->value();
70
71                         if (control == "0") {
72                                 id.bank_number |= string_to_int(tree, value) << 7;
73                         } else if (control == "32") {
74                                 id.bank_number |= string_to_int(tree, value);
75                         }
76
77                 } else if (node->name() == "ProgramChange") {
78                         const string& number = node->property("Number")->value();
79                         assert(number != "");
80                         id.program_number = string_to_int(tree, number);
81                 }
82         }
83
84         return 0;
85 }
86
87 XMLNode&
88 Patch::get_state (void)
89 {
90         XMLNode* node = new XMLNode("Patch");
91
92         /* XXX this is totally wrong */
93
94         node->add_property("Number", string_compose ("%1", _id.program_number));
95         node->add_property("Name",   _name);
96
97         /*
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();
102             ++event) {
103                 commands->add_child_copy(*((((Evoral::MIDIEvent&)*event)).to_xml()));
104         }
105         */
106
107         return *node;
108 }
109
110 int
111 Patch::set_state (const XMLTree& tree, const XMLNode& node)
112 {
113         if (node.name() != "Patch") {
114                 cerr << "Incorrect node " << node.name() << " handed to Patch" << endl;
115                 return -1;
116         }
117
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. */
120
121         const XMLProperty* program_change = node.property("ProgramChange");
122         if (program_change) {
123                 _id.program_number = string_to_int(tree, program_change->value());
124         }
125
126         const XMLProperty* name = node.property("Name");
127         if (!name) {
128                 return -1;
129         }
130         _name = name->value();
131
132         XMLNode* commands = node.child("PatchMIDICommands");
133         if (commands) {
134                 if (initialize_primary_key_from_commands(tree, _id, commands) &&
135                     !program_change) {
136                         return -1;  // Failed to find a program number anywhere
137                 }
138         }
139         
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();
143         }
144
145         return 0;
146 }
147
148 XMLNode&
149 Note::get_state (void)
150 {
151         XMLNode* node = new XMLNode("Note");
152         node->add_property("Number", _number + 1);
153         node->add_property("Name",   _name);
154
155         return *node;
156 }
157
158 int
159 Note::set_state (const XMLTree& tree, const XMLNode& node)
160 {
161         assert(node.name() == "Note");
162
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)
167                              << endmsg;
168                 return -1;
169         }
170
171         _number = num - 1;
172         _name   = node.property("Name")->value();
173
174         return 0;
175 }
176
177 XMLNode&
178 NoteNameList::get_state (void)
179 {
180         XMLNode* node = new XMLNode("NoteNameList");
181         node->add_property("Name", _name);
182
183         return *node;
184 }
185
186 static void
187 add_note_from_xml (NoteNameList::Notes& notes, const XMLTree& tree, const XMLNode& node)
188 {
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;
193                 } else {
194                         PBD::warning
195                                 << string_compose("%1: Duplicate note number %2 (%3) ignored",
196                                                   tree.filename(), (int)note->number(), note->name())
197                                 << endmsg;
198                 }
199         }
200 }
201
202 int
203 NoteNameList::set_state (const XMLTree& tree, const XMLNode& node)
204 {
205         assert(node.name() == "NoteNameList");
206         _name = node.property("Name")->value();
207         _notes.clear();
208         _notes.resize(128);
209
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);
219                                 } else {
220                                         PBD::warning << string_compose("%1: Invalid NoteGroup child %2 ignored",
221                                                                        tree.filename(), (*j)->name())
222                                                      << endmsg;
223                                 }
224                         }
225                 }
226         }
227
228         return 0;
229 }
230
231 XMLNode&
232 Control::get_state (void)
233 {
234         XMLNode* node = new XMLNode("Control");
235         node->add_property("Type",   _type);
236         node->add_property("Number", _number);
237         node->add_property("Name",   _name);
238
239         return *node;
240 }
241
242 int
243 Control::set_state (const XMLTree& tree, const XMLNode& node)
244 {
245         assert(node.name() == "Control");
246         if (node.property("Type")) {
247                 _type = node.property("Type")->value();
248         } else {
249                 _type = "7bit";
250         }
251         _number = string_to_int(tree, node.property("Number")->value());
252         _name   = node.property("Name")->value();
253
254         return 0;
255 }
256
257 XMLNode&
258 ControlNameList::get_state (void)
259 {
260         XMLNode* node = new XMLNode("ControlNameList");
261         node->add_property("Name", _name);
262
263         return *node;
264 }
265
266 int
267 ControlNameList::set_state (const XMLTree& tree, const XMLNode& node)
268 {
269         assert(node.name() == "ControlNameList");
270         _name = node.property("Name")->value();
271
272         _controls.clear();
273         for (XMLNodeList::const_iterator i = node.children().begin();
274              i != node.children().end(); ++i) {
275                 if ((*i)->name() == "Control") {
276                         boost::shared_ptr<Control> control(new Control());
277                         control->set_state (tree, *(*i));
278                         if (_controls.find(control->number()) == _controls.end()) {
279                                 _controls.insert(make_pair(control->number(), control));
280                         } else {
281                                 PBD::warning << string_compose("%1: Duplicate control %2 ignored",
282                                                                tree.filename(), control->number())
283                                              << endmsg;
284                         }
285                 }
286         }
287
288         return 0;
289 }
290
291 boost::shared_ptr<const Control>
292 ControlNameList::control(uint16_t num) const
293 {
294         Controls::const_iterator i = _controls.find(num);
295         if (i != _controls.end()) {
296                 return i->second;
297         }
298         return boost::shared_ptr<const Control>();
299 }
300
301 XMLNode&
302 PatchBank::get_state (void)
303 {
304         XMLNode* node = new XMLNode("PatchBank");
305         node->add_property("Name",   _name);
306         XMLNode* patch_name_list = node->add_child("PatchNameList");
307         for (PatchNameList::iterator patch = _patch_name_list.begin();
308             patch != _patch_name_list.end();
309             ++patch) {
310                 patch_name_list->add_child_nocopy((*patch)->get_state());
311         }
312
313         return *node;
314 }
315
316 int
317 PatchBank::set_state (const XMLTree& tree, const XMLNode& node)
318 {
319         assert(node.name() == "PatchBank");
320         _name   = node.property("Name")->value();
321
322         XMLNode* commands = node.child("MIDICommands");
323         if (commands) {
324                 PatchPrimaryKey id (0, 0);
325                 if (initialize_primary_key_from_commands (tree, id, commands)) {
326                         return -1;
327                 }
328                 _number = id.bank_number;
329         }
330
331         XMLNode* patch_name_list = node.child("PatchNameList");
332
333         if (patch_name_list) {
334                 const XMLNodeList patches = patch_name_list->children();
335                 for (XMLNodeList::const_iterator i = patches.begin(); i != patches.end(); ++i) {
336                         boost::shared_ptr<Patch> patch (new Patch (string(), 0, _number));
337                         patch->set_state(tree, *(*i));
338                         _patch_name_list.push_back(patch);
339                 }
340         } else {
341                 XMLNode* use_patch_name_list = node.child ("UsesPatchNameList");
342                 if (use_patch_name_list) {
343                         _patch_list_name = use_patch_name_list->property ("Name")->value();
344                 } else {
345                         error << "Patch without patch name list - patchfile will be ignored" << endmsg;
346                         return -1;
347                 }
348         }
349
350         return 0;
351 }
352
353 int
354 PatchBank::set_patch_name_list (const PatchNameList& pnl)
355 {
356         _patch_name_list = pnl;
357         _patch_list_name = "";
358
359         for (PatchNameList::iterator p = _patch_name_list.begin(); p != _patch_name_list.end(); p++) {
360                 (*p)->set_bank_number (_number);
361         }
362
363         return 0;
364 }
365
366 std::ostream&
367 operator<< (std::ostream& os, const ChannelNameSet& cns)
368 {
369         os << "Channel Name Set: name = " << cns._name << endl
370            << "Map size " << cns._patch_map.size () << endl
371            << "List size " << cns._patch_list.size() << endl
372            << "Patch list name = [" << cns._patch_list_name << ']' << endl
373            << "Available channels : ";
374         for (set<uint8_t>::const_iterator x = cns._available_for_channels.begin(); x != cns._available_for_channels.end(); ++x) {
375                 os << (int) (*x) << ' ';
376         }
377         os << endl;
378         
379         for (ChannelNameSet::PatchBanks::const_iterator pbi = cns._patch_banks.begin(); pbi != cns._patch_banks.end(); ++pbi) {
380                 os << "\tPatch Bank " << (*pbi)->name() << " with " << (*pbi)->patch_name_list().size() << " patches\n";
381                 for (PatchNameList::const_iterator pni = (*pbi)->patch_name_list().begin(); pni != (*pbi)->patch_name_list().end(); ++pni) {
382                         os << "\t\tPatch name " << (*pni)->name() << " prog " << (int) (*pni)->program_number() << " bank " << (*pni)->bank_number() << endl;
383                 }
384         }
385
386         return os;
387 }
388
389 void
390 ChannelNameSet::set_patch_banks (const ChannelNameSet::PatchBanks& pb)
391 {
392         _patch_banks = pb;
393         
394         _patch_map.clear ();
395         _patch_list.clear ();
396         _patch_list_name = "";
397         _available_for_channels.clear ();
398         
399         for (PatchBanks::const_iterator pbi = _patch_banks.begin(); pbi != _patch_banks.end(); ++pbi) {
400                 for (PatchNameList::const_iterator pni = (*pbi)->patch_name_list().begin(); pni != (*pbi)->patch_name_list().end(); ++pni) {
401                         _patch_map[(*pni)->patch_primary_key()] = (*pni);
402                         _patch_list.push_back ((*pni)->patch_primary_key());
403                 }
404         }
405
406         for (uint8_t n = 0; n < 16; ++n) {
407                 _available_for_channels.insert (n);
408         }
409 }
410
411 void
412 ChannelNameSet::use_patch_name_list (const PatchNameList& pnl)
413 {
414         for (PatchNameList::const_iterator p = pnl.begin(); p != pnl.end(); ++p) {
415                 _patch_map[(*p)->patch_primary_key()] = (*p);
416                 _patch_list.push_back ((*p)->patch_primary_key());
417         }
418 }
419
420 XMLNode&
421 ChannelNameSet::get_state (void)
422 {
423         XMLNode* node = new XMLNode("ChannelNameSet");
424         node->add_property("Name",   _name);
425
426         XMLNode* available_for_channels = node->add_child("AvailableForChannels");
427         assert(available_for_channels);
428
429         for (uint8_t channel = 0; channel < 16; ++channel) {
430                 XMLNode* available_channel = available_for_channels->add_child("AvailableChannel");
431                 assert(available_channel);
432
433                 available_channel->add_property("Channel", (long) channel);
434
435                 if (_available_for_channels.find(channel) != _available_for_channels.end()) {
436                         available_channel->add_property("Available", "true");
437                 } else {
438                         available_channel->add_property("Available", "false");
439                 }
440         }
441
442         for (PatchBanks::iterator patch_bank = _patch_banks.begin();
443             patch_bank != _patch_banks.end();
444             ++patch_bank) {
445                 node->add_child_nocopy((*patch_bank)->get_state());
446         }
447
448         return *node;
449 }
450
451 int
452 ChannelNameSet::set_state (const XMLTree& tree, const XMLNode& node)
453 {
454         assert(node.name() == "ChannelNameSet");
455         _name = node.property("Name")->value();
456
457         const XMLNodeList children = node.children();
458         for (XMLNodeList::const_iterator i = children.begin(); i != children.end(); ++i) {
459                 XMLNode* node = *i;
460                 assert(node);
461                 if (node->name() == "AvailableForChannels") {
462                         boost::shared_ptr<XMLSharedNodeList> channels =
463                                 tree.find("//AvailableChannel[@Available = 'true']/@Channel", node);
464                         for (XMLSharedNodeList::const_iterator i = channels->begin();
465                             i != channels->end();
466                             ++i) {
467                                 _available_for_channels.insert(
468                                         string_to_int(tree, (*i)->attribute_value()));
469                         }
470                 } else if (node->name() == "PatchBank") {
471                         boost::shared_ptr<PatchBank> bank (new PatchBank ());
472                         bank->set_state(tree, *node);
473                         _patch_banks.push_back(bank);
474                         const PatchNameList& patches = bank->patch_name_list();
475                         for (PatchNameList::const_iterator patch = patches.begin();
476                              patch != patches.end();
477                              ++patch) {
478                                 _patch_map[(*patch)->patch_primary_key()] = *patch;
479                                 _patch_list.push_back((*patch)->patch_primary_key());
480                         }
481                 } else if (node->name() == "UsesNoteNameList") {
482                         _note_list_name = node->property ("Name")->value();
483                 } else if (node->name() == "UsesControlNameList") {
484                         _control_list_name = node->property ("Name")->value();
485                 }
486         }
487
488         return 0;
489 }
490
491 int
492 CustomDeviceMode::set_state(const XMLTree& tree, const XMLNode& a_node)
493 {
494         assert(a_node.name() == "CustomDeviceMode");
495
496         _name = a_node.property("Name")->value();
497
498         boost::shared_ptr<XMLSharedNodeList> channel_name_set_assignments =
499                 tree.find("//ChannelNameSetAssign", const_cast<XMLNode *>(&a_node));
500         for (XMLSharedNodeList::const_iterator i = channel_name_set_assignments->begin();
501             i != channel_name_set_assignments->end();
502             ++i) {
503                 const int     channel  = string_to_int(tree, (*i)->property("Channel")->value());
504                 const string& name_set = (*i)->property("NameSet")->value();
505                 assert( 1 <= channel && channel <= 16 );
506                 _channel_name_set_assignments[channel - 1] = name_set;
507         }
508         return 0;
509 }
510
511 XMLNode&
512 CustomDeviceMode::get_state(void)
513 {
514         XMLNode* custom_device_mode = new XMLNode("CustomDeviceMode");
515         custom_device_mode->add_property("Name",   _name);
516         XMLNode* channel_name_set_assignments =
517                 custom_device_mode->add_child("ChannelNameSetAssignments");
518         for (int i = 0; i < 15 && !_channel_name_set_assignments[i].empty(); i++) {
519                 XMLNode* channel_name_set_assign =
520                         channel_name_set_assignments->add_child("ChannelNameSetAssign");
521                 channel_name_set_assign->add_property("Channel", i + 1);
522                 channel_name_set_assign->add_property("NameSet", _channel_name_set_assignments[i]);
523         }
524
525         return *custom_device_mode;
526 }
527
528 boost::shared_ptr<CustomDeviceMode> 
529 MasterDeviceNames::custom_device_mode_by_name(const std::string& mode_name)
530 {
531         return _custom_device_modes[mode_name];
532 }
533
534 boost::shared_ptr<ChannelNameSet> 
535 MasterDeviceNames::channel_name_set_by_device_mode_and_channel(const std::string& mode, uint8_t channel)
536 {
537         boost::shared_ptr<CustomDeviceMode> cdm = custom_device_mode_by_name(mode);
538         boost::shared_ptr<ChannelNameSet> cns =  _channel_name_sets[cdm->channel_name_set_name_by_channel(channel)];
539         return cns;
540 }
541
542 boost::shared_ptr<Patch> 
543 MasterDeviceNames::find_patch(const std::string& mode, uint8_t channel, const PatchPrimaryKey& key) 
544 {
545         return channel_name_set_by_device_mode_and_channel(mode, channel)->find_patch(key);
546 }
547
548 boost::shared_ptr<ChannelNameSet>
549 MasterDeviceNames::channel_name_set(const std::string& name)
550 {
551         ChannelNameSets::const_iterator i = _channel_name_sets.find(name);
552         if (i != _channel_name_sets.end()) {
553                 return i->second;
554         }
555         return boost::shared_ptr<ChannelNameSet>();
556 }
557
558 boost::shared_ptr<ControlNameList>
559 MasterDeviceNames::control_name_list(const std::string& name)
560 {
561         ControlNameLists::const_iterator i = _control_name_lists.find(name);
562         if (i != _control_name_lists.end()) {
563                 return i->second;
564         }
565         return boost::shared_ptr<ControlNameList>();
566 }
567
568 boost::shared_ptr<NoteNameList>
569 MasterDeviceNames::note_name_list(const std::string& name)
570 {
571         NoteNameLists::const_iterator i = _note_name_lists.find(name);
572         if (i != _note_name_lists.end()) {
573                 return i->second;
574         }
575         return boost::shared_ptr<NoteNameList>();
576 }
577
578 std::string
579 MasterDeviceNames::note_name(const std::string& mode_name,
580                              uint8_t            channel,
581                              uint16_t           bank,
582                              uint8_t            program,
583                              uint8_t            number)
584 {
585         if (number > 127) {
586                 return "";
587         }
588
589         boost::shared_ptr<const Patch> patch(
590                 find_patch(mode_name, channel, PatchPrimaryKey(program, bank)));
591         if (!patch) {
592                 return "";
593         }
594
595         boost::shared_ptr<const NoteNameList> note_names(
596                 note_name_list(patch->note_list_name()));
597         if (!note_names) {
598                 /* No note names specific to this patch, check the ChannelNameSet */
599                 boost::shared_ptr<ChannelNameSet> chan_names = channel_name_set_by_device_mode_and_channel(
600                         mode_name, channel);
601                 if (chan_names) {
602                         note_names = note_name_list(chan_names->note_list_name());
603                 }
604         }
605         if (!note_names) {
606                 return "";
607         }
608
609         boost::shared_ptr<const Note> note(note_names->notes()[number]);
610         return note ? note->name() : "";
611 }
612
613 int
614 MasterDeviceNames::set_state(const XMLTree& tree, const XMLNode&)
615 {
616         // Manufacturer
617         boost::shared_ptr<XMLSharedNodeList> manufacturer = tree.find("//Manufacturer");
618         assert(manufacturer->size() == 1);
619         _manufacturer = manufacturer->front()->children().front()->content();
620
621         // Models
622         boost::shared_ptr<XMLSharedNodeList> models = tree.find("//Model");
623         assert(models->size() >= 1);
624         for (XMLSharedNodeList::iterator i = models->begin();
625              i != models->end();
626              ++i) {
627                 const XMLNodeList& contents = (*i)->children();
628                 assert(contents.size() == 1);
629                 XMLNode * content = *(contents.begin());
630                 assert(content->is_content());
631                 _models.insert(content->content());
632         }
633
634         // CustomDeviceModes
635         boost::shared_ptr<XMLSharedNodeList> custom_device_modes = tree.find("//CustomDeviceMode");
636         for (XMLSharedNodeList::iterator i = custom_device_modes->begin();
637              i != custom_device_modes->end();
638              ++i) {
639                 boost::shared_ptr<CustomDeviceMode> custom_device_mode(new CustomDeviceMode());
640                 custom_device_mode->set_state(tree, *(*i));
641
642                 _custom_device_modes[custom_device_mode->name()] = custom_device_mode;
643                 _custom_device_mode_names.push_back(custom_device_mode->name());
644         }
645
646         // ChannelNameSets
647         boost::shared_ptr<XMLSharedNodeList> channel_name_sets = tree.find("//ChannelNameSet");
648         for (XMLSharedNodeList::iterator i = channel_name_sets->begin();
649              i != channel_name_sets->end();
650              ++i) {
651                 boost::shared_ptr<ChannelNameSet> channel_name_set(new ChannelNameSet());
652                 channel_name_set->set_state(tree, *(*i));
653                 _channel_name_sets[channel_name_set->name()] = channel_name_set;
654         }
655
656         // NoteNameLists
657         boost::shared_ptr<XMLSharedNodeList> note_name_lists = tree.find("//NoteNameList");
658         for (XMLSharedNodeList::iterator i = note_name_lists->begin();
659              i != note_name_lists->end();
660              ++i) {
661                 boost::shared_ptr<NoteNameList> note_name_list(new NoteNameList());
662                 note_name_list->set_state (tree, *(*i));
663                 _note_name_lists[note_name_list->name()] = note_name_list;
664         }
665
666         // ControlNameLists
667         boost::shared_ptr<XMLSharedNodeList> control_name_lists = tree.find("//ControlNameList");
668         for (XMLSharedNodeList::iterator i = control_name_lists->begin();
669              i != control_name_lists->end();
670              ++i) {
671                 boost::shared_ptr<ControlNameList> control_name_list(new ControlNameList());
672                 control_name_list->set_state (tree, *(*i));
673                 _control_name_lists[control_name_list->name()] = control_name_list;
674         }
675
676         // global/post-facto PatchNameLists
677         boost::shared_ptr<XMLSharedNodeList> patch_name_lists = tree.find("/child::MIDINameDocument/child::MasterDeviceNames/child::PatchNameList");
678         for (XMLSharedNodeList::iterator i = patch_name_lists->begin();
679              i != patch_name_lists->end();
680              ++i) {
681
682                 PatchNameList patch_name_list;
683                 const XMLNodeList patches = (*i)->children();
684
685                 for (XMLNodeList::const_iterator p = patches.begin(); p != patches.end(); ++p) {
686                         boost::shared_ptr<Patch> patch (new Patch ());
687                         patch->set_state(tree, *(*p));
688                         patch_name_list.push_back(patch);
689                 }
690
691                 if (!patch_name_list.empty()) {
692                         _patch_name_lists[(*i)->property ("Name")->value()] = patch_name_list;
693                 }
694         }
695
696         /* now traverse patches and hook up anything that used UsePatchNameList
697          * to the right patch list
698          */
699
700         for (ChannelNameSets::iterator cns = _channel_name_sets.begin(); cns != _channel_name_sets.end(); ++cns) {
701                 ChannelNameSet::PatchBanks pbs = cns->second->patch_banks();
702                 PatchNameLists::iterator p;
703
704                 for (ChannelNameSet::PatchBanks::iterator pb = pbs.begin(); pb != pbs.end(); ++pb) {
705                         const std::string& pln = (*pb)->patch_list_name();
706                         if (!pln.empty()) {
707                                 if ((p = _patch_name_lists.find (pln)) != _patch_name_lists.end()) {
708                                         if ((*pb)->set_patch_name_list (p->second)) {
709                                                 return -1;
710                                         }
711                                         cns->second->use_patch_name_list (p->second);
712                                 } else {
713                                         error << string_compose ("Patch list name %1 was not found - patch file ignored", pln) << endmsg;
714                                         return -1;
715                                 }
716                         }
717                 }
718
719         }
720
721         return 0;
722 }
723
724 XMLNode&
725 MasterDeviceNames::get_state(void)
726 {
727         static XMLNode nothing("<nothing>");
728         return nothing;
729 }
730
731 MIDINameDocument::MIDINameDocument (const string& filename)
732 {
733         if (!_document.read (filename)) {
734                 throw failed_constructor ();
735         }
736
737         _document.set_filename (filename);
738         set_state (_document, *_document.root());
739 }
740
741 int
742 MIDINameDocument::set_state (const XMLTree& tree, const XMLNode&)
743 {
744         // Author
745
746         boost::shared_ptr<XMLSharedNodeList> author = tree.find("//Author");
747         if (author->size() < 1) {
748                 error << "No author information in MIDNAM file" << endmsg;
749                 return -1;
750         }
751         
752         if (author->front()->children().size() > 0) {
753                 _author = author->front()->children().front()->content();
754         }
755
756         // MasterDeviceNames
757
758         boost::shared_ptr<XMLSharedNodeList> master_device_names_list = tree.find ("//MasterDeviceNames");
759
760         for (XMLSharedNodeList::iterator i = master_device_names_list->begin();
761              i != master_device_names_list->end();
762              ++i) {
763                 boost::shared_ptr<MasterDeviceNames> master_device_names(new MasterDeviceNames());
764
765                 if (master_device_names->set_state(tree, *(*i))) {
766                         return -1;
767                 }
768
769                 for (MasterDeviceNames::Models::const_iterator model = master_device_names->models().begin();
770                      model != master_device_names->models().end();
771                      ++model) {
772                         _master_device_names_list.insert(
773                                 std::pair<std::string, boost::shared_ptr<MasterDeviceNames> >
774                                 (*model,      master_device_names));
775                         
776                         _all_models.insert(*model);
777                 }
778         }
779
780         return 0;
781 }
782
783 XMLNode&
784 MIDINameDocument::get_state(void)
785 {
786         static XMLNode nothing("<nothing>");
787         return nothing;
788 }
789
790 boost::shared_ptr<MasterDeviceNames>
791 MIDINameDocument::master_device_names(const std::string& model)
792 {
793         MasterDeviceNamesList::const_iterator m = _master_device_names_list.find(model);
794         if (m != _master_device_names_list.end()) {
795                 return boost::shared_ptr<MasterDeviceNames>(m->second);
796         }
797         return boost::shared_ptr<MasterDeviceNames>();
798 }
799
800 const char* general_midi_program_names[128] = {
801         "Acoustic Grand Piano",
802         "Bright Acoustic Piano",
803         "Electric Grand Piano",
804         "Honky-tonk Piano",
805         "Rhodes Piano",
806         "Chorused Piano",
807         "Harpsichord",
808         "Clavinet",
809         "Celesta",
810         "Glockenspiel",
811         "Music Box",
812         "Vibraphone",
813         "Marimba",
814         "Xylophone",
815         "Tubular Bells",
816         "Dulcimer",
817         "Hammond Organ",
818         "Percussive Organ",
819         "Rock Organ",
820         "Church Organ",
821         "Reed Organ",
822         "Accordion",
823         "Harmonica",
824         "Tango Accordion",
825         "Acoustic Guitar (nylon)",
826         "Acoustic Guitar (steel)",
827         "Electric Guitar (jazz)",
828         "Electric Guitar (clean)",
829         "Electric Guitar (muted)",
830         "Overdriven Guitar",
831         "Distortion Guitar",
832         "Guitar Harmonics",
833         "Acoustic Bass",
834         "Electric Bass (finger)",
835         "Electric Bass (pick)",
836         "Fretless Bass",
837         "Slap Bass 1",
838         "Slap Bass 2",
839         "Synth Bass 1",
840         "Synth Bass 2",
841         "Violin",
842         "Viola",
843         "Cello",
844         "Contrabass",
845         "Tremolo Strings",
846         "Pizzicato Strings",
847         "Orchestral Harp",
848         "Timpani",
849         "String Ensemble 1",
850         "String Ensemble 2",
851         "SynthStrings 1",
852         "SynthStrings 2",
853         "Choir Aahs",
854         "Voice Oohs",
855         "Synth Voice",
856         "Orchestra Hit",
857         "Trumpet",
858         "Trombone",
859         "Tuba",
860         "Muted Trumpet",
861         "French Horn",
862         "Brass Section",
863         "Synth Brass 1",
864         "Synth Brass 2",
865         "Soprano Sax",
866         "Alto Sax",
867         "Tenor Sax",
868         "Baritone Sax",
869         "Oboe",
870         "English Horn",
871         "Bassoon",
872         "Clarinet",
873         "Piccolo",
874         "Flute",
875         "Recorder",
876         "Pan Flute",
877         "Bottle Blow",
878         "Shakuhachi",
879         "Whistle",
880         "Ocarina",
881         "Lead 1 (square)",
882         "Lead 2 (sawtooth)",
883         "Lead 3 (calliope lead)",
884         "Lead 4 (chiff lead)",
885         "Lead 5 (charang)",
886         "Lead 6 (voice)",
887         "Lead 7 (fifths)",
888         "Lead 8 (bass + lead)",
889         "Pad 1 (new age)",
890         "Pad 2 (warm)",
891         "Pad 3 (polysynth)",
892         "Pad 4 (choir)",
893         "Pad 5 (bowed)",
894         "Pad 6 (metallic)",
895         "Pad 7 (halo)",
896         "Pad 8 (sweep)",
897         "FX 1 (rain)",
898         "FX 2 (soundtrack)",
899         "FX 3 (crystal)",
900         "FX 4 (atmosphere)",
901         "FX 5 (brightness)",
902         "FX 6 (goblins)",
903         "FX 7 (echoes)",
904         "FX 8 (sci-fi)",
905         "Sitar",
906         "Banjo",
907         "Shamisen",
908         "Koto",
909         "Kalimba",
910         "Bagpipe",
911         "Fiddle",
912         "Shanai",
913         "Tinkle Bell",
914         "Agogo",
915         "Steel Drums",
916         "Woodblock",
917         "Taiko Drum",
918         "Melodic Tom",
919         "Synth Drum",
920         "Reverse Cymbal",
921         "Guitar Fret Noise",
922         "Breath Noise",
923         "Seashore",
924         "Bird Tweet",
925         "Telephone Ring",
926         "Helicopter",
927         "Applause",
928         "Gunshot",
929 };
930
931 } //namespace Name
932
933 } //namespace MIDI
934