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