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