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