Merge branch 'master' into cairocanvas
[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>::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         boost::shared_ptr<ChannelNameSet> cns = channel_name_set_by_device_mode_and_channel(mode, channel);
546         if (!cns) return boost::shared_ptr<Patch>();
547         return cns->find_patch(key);
548 }
549
550 boost::shared_ptr<ChannelNameSet>
551 MasterDeviceNames::channel_name_set(const std::string& name)
552 {
553         ChannelNameSets::const_iterator i = _channel_name_sets.find(name);
554         if (i != _channel_name_sets.end()) {
555                 return i->second;
556         }
557         return boost::shared_ptr<ChannelNameSet>();
558 }
559
560 boost::shared_ptr<ControlNameList>
561 MasterDeviceNames::control_name_list(const std::string& name)
562 {
563         ControlNameLists::const_iterator i = _control_name_lists.find(name);
564         if (i != _control_name_lists.end()) {
565                 return i->second;
566         }
567         return boost::shared_ptr<ControlNameList>();
568 }
569
570 boost::shared_ptr<NoteNameList>
571 MasterDeviceNames::note_name_list(const std::string& name)
572 {
573         NoteNameLists::const_iterator i = _note_name_lists.find(name);
574         if (i != _note_name_lists.end()) {
575                 return i->second;
576         }
577         return boost::shared_ptr<NoteNameList>();
578 }
579
580 std::string
581 MasterDeviceNames::note_name(const std::string& mode_name,
582                              uint8_t            channel,
583                              uint16_t           bank,
584                              uint8_t            program,
585                              uint8_t            number)
586 {
587         if (number > 127) {
588                 return "";
589         }
590
591         boost::shared_ptr<const Patch> patch(
592                 find_patch(mode_name, channel, PatchPrimaryKey(program, bank)));
593         if (!patch) {
594                 return "";
595         }
596
597         boost::shared_ptr<const NoteNameList> note_names(
598                 note_name_list(patch->note_list_name()));
599         if (!note_names) {
600                 /* No note names specific to this patch, check the ChannelNameSet */
601                 boost::shared_ptr<ChannelNameSet> chan_names = channel_name_set_by_device_mode_and_channel(
602                         mode_name, channel);
603                 if (chan_names) {
604                         note_names = note_name_list(chan_names->note_list_name());
605                 }
606         }
607         if (!note_names) {
608                 return "";
609         }
610
611         boost::shared_ptr<const Note> note(note_names->notes()[number]);
612         return note ? note->name() : "";
613 }
614
615 int
616 MasterDeviceNames::set_state(const XMLTree& tree, const XMLNode&)
617 {
618         // Manufacturer
619         boost::shared_ptr<XMLSharedNodeList> manufacturer = tree.find("//Manufacturer");
620         assert(manufacturer->size() == 1);
621         _manufacturer = manufacturer->front()->children().front()->content();
622
623         // Models
624         boost::shared_ptr<XMLSharedNodeList> models = tree.find("//Model");
625         assert(models->size() >= 1);
626         for (XMLSharedNodeList::iterator i = models->begin();
627              i != models->end();
628              ++i) {
629                 const XMLNodeList& contents = (*i)->children();
630                 assert(contents.size() == 1);
631                 XMLNode * content = *(contents.begin());
632                 assert(content->is_content());
633                 _models.insert(content->content());
634         }
635
636         // CustomDeviceModes
637         boost::shared_ptr<XMLSharedNodeList> custom_device_modes = tree.find("//CustomDeviceMode");
638         for (XMLSharedNodeList::iterator i = custom_device_modes->begin();
639              i != custom_device_modes->end();
640              ++i) {
641                 boost::shared_ptr<CustomDeviceMode> custom_device_mode(new CustomDeviceMode());
642                 custom_device_mode->set_state(tree, *(*i));
643
644                 _custom_device_modes[custom_device_mode->name()] = custom_device_mode;
645                 _custom_device_mode_names.push_back(custom_device_mode->name());
646         }
647
648         // ChannelNameSets
649         boost::shared_ptr<XMLSharedNodeList> channel_name_sets = tree.find("//ChannelNameSet");
650         for (XMLSharedNodeList::iterator i = channel_name_sets->begin();
651              i != channel_name_sets->end();
652              ++i) {
653                 boost::shared_ptr<ChannelNameSet> channel_name_set(new ChannelNameSet());
654                 channel_name_set->set_state(tree, *(*i));
655                 _channel_name_sets[channel_name_set->name()] = channel_name_set;
656         }
657
658         // NoteNameLists
659         boost::shared_ptr<XMLSharedNodeList> note_name_lists = tree.find("//NoteNameList");
660         for (XMLSharedNodeList::iterator i = note_name_lists->begin();
661              i != note_name_lists->end();
662              ++i) {
663                 boost::shared_ptr<NoteNameList> note_name_list(new NoteNameList());
664                 note_name_list->set_state (tree, *(*i));
665                 _note_name_lists[note_name_list->name()] = note_name_list;
666         }
667
668         // ControlNameLists
669         boost::shared_ptr<XMLSharedNodeList> control_name_lists = tree.find("//ControlNameList");
670         for (XMLSharedNodeList::iterator i = control_name_lists->begin();
671              i != control_name_lists->end();
672              ++i) {
673                 boost::shared_ptr<ControlNameList> control_name_list(new ControlNameList());
674                 control_name_list->set_state (tree, *(*i));
675                 _control_name_lists[control_name_list->name()] = control_name_list;
676         }
677
678         // global/post-facto PatchNameLists
679         boost::shared_ptr<XMLSharedNodeList> patch_name_lists = tree.find("/child::MIDINameDocument/child::MasterDeviceNames/child::PatchNameList");
680         for (XMLSharedNodeList::iterator i = patch_name_lists->begin();
681              i != patch_name_lists->end();
682              ++i) {
683
684                 PatchNameList patch_name_list;
685                 const XMLNodeList patches = (*i)->children();
686
687                 for (XMLNodeList::const_iterator p = patches.begin(); p != patches.end(); ++p) {
688                         boost::shared_ptr<Patch> patch (new Patch ());
689                         patch->set_state(tree, *(*p));
690                         patch_name_list.push_back(patch);
691                 }
692
693                 if (!patch_name_list.empty()) {
694                         _patch_name_lists[(*i)->property ("Name")->value()] = patch_name_list;
695                 }
696         }
697
698         /* now traverse patches and hook up anything that used UsePatchNameList
699          * to the right patch list
700          */
701
702         for (ChannelNameSets::iterator cns = _channel_name_sets.begin(); cns != _channel_name_sets.end(); ++cns) {
703                 ChannelNameSet::PatchBanks pbs = cns->second->patch_banks();
704                 PatchNameLists::iterator p;
705
706                 for (ChannelNameSet::PatchBanks::iterator pb = pbs.begin(); pb != pbs.end(); ++pb) {
707                         const std::string& pln = (*pb)->patch_list_name();
708                         if (!pln.empty()) {
709                                 if ((p = _patch_name_lists.find (pln)) != _patch_name_lists.end()) {
710                                         if ((*pb)->set_patch_name_list (p->second)) {
711                                                 return -1;
712                                         }
713                                         cns->second->use_patch_name_list (p->second);
714                                 } else {
715                                         error << string_compose ("Patch list name %1 was not found - patch file ignored", pln) << endmsg;
716                                         return -1;
717                                 }
718                         }
719                 }
720
721         }
722
723         return 0;
724 }
725
726 XMLNode&
727 MasterDeviceNames::get_state(void)
728 {
729         static XMLNode nothing("<nothing>");
730         return nothing;
731 }
732
733 MIDINameDocument::MIDINameDocument (const string& filename)
734 {
735         if (!_document.read (filename)) {
736                 throw failed_constructor ();
737         }
738
739         _document.set_filename (filename);
740         set_state (_document, *_document.root());
741 }
742
743 int
744 MIDINameDocument::set_state (const XMLTree& tree, const XMLNode&)
745 {
746         // Author
747
748         boost::shared_ptr<XMLSharedNodeList> author = tree.find("//Author");
749         if (author->size() < 1) {
750                 error << "No author information in MIDNAM file" << endmsg;
751                 return -1;
752         }
753         
754         if (author->front()->children().size() > 0) {
755                 _author = author->front()->children().front()->content();
756         }
757
758         // MasterDeviceNames
759
760         boost::shared_ptr<XMLSharedNodeList> master_device_names_list = tree.find ("//MasterDeviceNames");
761
762         for (XMLSharedNodeList::iterator i = master_device_names_list->begin();
763              i != master_device_names_list->end();
764              ++i) {
765                 boost::shared_ptr<MasterDeviceNames> master_device_names(new MasterDeviceNames());
766
767                 if (master_device_names->set_state(tree, *(*i))) {
768                         return -1;
769                 }
770
771                 for (MasterDeviceNames::Models::const_iterator model = master_device_names->models().begin();
772                      model != master_device_names->models().end();
773                      ++model) {
774                         _master_device_names_list.insert(
775                                 std::pair<std::string, boost::shared_ptr<MasterDeviceNames> >
776                                 (*model,      master_device_names));
777                         
778                         _all_models.insert(*model);
779                 }
780         }
781
782         return 0;
783 }
784
785 XMLNode&
786 MIDINameDocument::get_state(void)
787 {
788         static XMLNode nothing("<nothing>");
789         return nothing;
790 }
791
792 boost::shared_ptr<MasterDeviceNames>
793 MIDINameDocument::master_device_names(const std::string& model)
794 {
795         MasterDeviceNamesList::const_iterator m = _master_device_names_list.find(model);
796         if (m != _master_device_names_list.end()) {
797                 return boost::shared_ptr<MasterDeviceNames>(m->second);
798         }
799         return boost::shared_ptr<MasterDeviceNames>();
800 }
801
802 const char* general_midi_program_names[128] = {
803         "Acoustic Grand Piano",
804         "Bright Acoustic Piano",
805         "Electric Grand Piano",
806         "Honky-tonk Piano",
807         "Rhodes Piano",
808         "Chorused Piano",
809         "Harpsichord",
810         "Clavinet",
811         "Celesta",
812         "Glockenspiel",
813         "Music Box",
814         "Vibraphone",
815         "Marimba",
816         "Xylophone",
817         "Tubular Bells",
818         "Dulcimer",
819         "Hammond Organ",
820         "Percussive Organ",
821         "Rock Organ",
822         "Church Organ",
823         "Reed Organ",
824         "Accordion",
825         "Harmonica",
826         "Tango Accordion",
827         "Acoustic Guitar (nylon)",
828         "Acoustic Guitar (steel)",
829         "Electric Guitar (jazz)",
830         "Electric Guitar (clean)",
831         "Electric Guitar (muted)",
832         "Overdriven Guitar",
833         "Distortion Guitar",
834         "Guitar Harmonics",
835         "Acoustic Bass",
836         "Electric Bass (finger)",
837         "Electric Bass (pick)",
838         "Fretless Bass",
839         "Slap Bass 1",
840         "Slap Bass 2",
841         "Synth Bass 1",
842         "Synth Bass 2",
843         "Violin",
844         "Viola",
845         "Cello",
846         "Contrabass",
847         "Tremolo Strings",
848         "Pizzicato Strings",
849         "Orchestral Harp",
850         "Timpani",
851         "String Ensemble 1",
852         "String Ensemble 2",
853         "SynthStrings 1",
854         "SynthStrings 2",
855         "Choir Aahs",
856         "Voice Oohs",
857         "Synth Voice",
858         "Orchestra Hit",
859         "Trumpet",
860         "Trombone",
861         "Tuba",
862         "Muted Trumpet",
863         "French Horn",
864         "Brass Section",
865         "Synth Brass 1",
866         "Synth Brass 2",
867         "Soprano Sax",
868         "Alto Sax",
869         "Tenor Sax",
870         "Baritone Sax",
871         "Oboe",
872         "English Horn",
873         "Bassoon",
874         "Clarinet",
875         "Piccolo",
876         "Flute",
877         "Recorder",
878         "Pan Flute",
879         "Bottle Blow",
880         "Shakuhachi",
881         "Whistle",
882         "Ocarina",
883         "Lead 1 (square)",
884         "Lead 2 (sawtooth)",
885         "Lead 3 (calliope lead)",
886         "Lead 4 (chiff lead)",
887         "Lead 5 (charang)",
888         "Lead 6 (voice)",
889         "Lead 7 (fifths)",
890         "Lead 8 (bass + lead)",
891         "Pad 1 (new age)",
892         "Pad 2 (warm)",
893         "Pad 3 (polysynth)",
894         "Pad 4 (choir)",
895         "Pad 5 (bowed)",
896         "Pad 6 (metallic)",
897         "Pad 7 (halo)",
898         "Pad 8 (sweep)",
899         "FX 1 (rain)",
900         "FX 2 (soundtrack)",
901         "FX 3 (crystal)",
902         "FX 4 (atmosphere)",
903         "FX 5 (brightness)",
904         "FX 6 (goblins)",
905         "FX 7 (echoes)",
906         "FX 8 (sci-fi)",
907         "Sitar",
908         "Banjo",
909         "Shamisen",
910         "Koto",
911         "Kalimba",
912         "Bagpipe",
913         "Fiddle",
914         "Shanai",
915         "Tinkle Bell",
916         "Agogo",
917         "Steel Drums",
918         "Woodblock",
919         "Taiko Drum",
920         "Melodic Tom",
921         "Synth Drum",
922         "Reverse Cymbal",
923         "Guitar Fret Noise",
924         "Breath Noise",
925         "Seashore",
926         "Bird Tweet",
927         "Telephone Ring",
928         "Helicopter",
929         "Applause",
930         "Gunshot",
931 };
932
933 } //namespace Name
934
935 } //namespace MIDI
936