Use conf.fatal for fatal configuration errors
[ardour.git] / libs / midi++2 / midnam_patch.cc
1 /*
2  * Copyright (C) 2008-2012 Hans Baier <hansfbaier@googlemail.com>
3  * Copyright (C) 2008-2016 David Robillard <d@drobilla.net>
4  * Copyright (C) 2008-2017 Paul Davis <paul@linuxaudiosystems.com>
5  * Copyright (C) 2009-2012 Carl Hetherington <carl@carlh.net>
6  * Copyright (C) 2013-2019 Robin Gareus <robin@gareus.org>
7  * Copyright (C) 2015-2016 Tim Mayberry <mojofunk@gmail.com>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License along
20  * with this program; if not, write to the Free Software Foundation, Inc.,
21  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22  */
23
24 #include <stdlib.h>
25
26 #include <algorithm>
27 #include <iostream>
28
29 #include "midi++/midnam_patch.h"
30 #include "pbd/compose.h"
31 #include "pbd/convert.h"
32 #include "pbd/error.h"
33 #include "pbd/failed_constructor.h"
34
35 using namespace std;
36 using PBD::error;
37
38 namespace MIDI
39 {
40
41 namespace Name
42 {
43
44 Patch::Patch (std::string name, uint8_t p_number, uint16_t b_number)
45         : _name (name)
46         , _id (p_number, b_number)
47 {
48 }
49
50 static int
51 string_to_int(const XMLTree& tree, const std::string& str)
52 {
53         char*     endptr = NULL;
54         const int i      = strtol(str.c_str(), &endptr, 10);
55         if (str.empty() || *endptr != '\0') {
56                 PBD::error << string_compose("%1: Bad number `%2'", tree.filename(), str)
57                            << endmsg;
58         }
59         return i;
60 }
61
62 static int
63 initialize_primary_key_from_commands (
64         const XMLTree& tree, PatchPrimaryKey& id, const XMLNode* node)
65 {
66         uint16_t bank    = 0;
67         uint8_t  program = 0;
68
69         const XMLNodeList events = node->children();
70         for (XMLNodeList::const_iterator i = events.begin(); i != events.end(); ++i) {
71
72                 XMLNode* node = *i;
73                 if (node->name() == "ControlChange") {
74                         const string& control = node->property("Control")->value();
75                         const string& value   = node->property("Value")->value();
76
77                         if (control == "0") {
78                                 bank |= string_to_int(tree, value) << 7;
79                         } else if (control == "32") {
80                                 bank |= string_to_int(tree, value);
81                         }
82
83                 } else if (node->name() == "ProgramChange") {
84                         const string& number = node->property("Number")->value();
85                         assert(number != "");
86                         program = string_to_int(tree, number);
87                 }
88         }
89
90         id = PatchPrimaryKey(program, bank);
91         return 0;
92 }
93
94 XMLNode&
95 Patch::get_state (void)
96 {
97         XMLNode* node = new XMLNode("Patch");
98
99         /* XXX this is totally wrong */
100
101         node->set_property("Number", _id.program());
102         node->set_property("Name",   _name);
103
104         /*
105         typedef std::list< boost::shared_ptr< Evoral::Event<Temporal::Beats> > > PatchMidiCommands;
106         XMLNode* commands = node->add_child("PatchMIDICommands");
107         for (PatchMidiCommands::const_iterator event = _patch_midi_commands.begin();
108             event != _patch_midi_commands.end();
109             ++event) {
110                 commands->add_child_copy(Evoral::MIDIXML::midi_to_xml(*event));
111         }
112         */
113
114         return *node;
115 }
116
117 int
118 Patch::set_state (const XMLTree& tree, const XMLNode& node)
119 {
120         if (node.name() != "Patch") {
121                 cerr << "Incorrect node type '" << node.name() << "' handed to Patch" << endl;
122                 return -1;
123         }
124
125         /* Note there is a "Number" attribute, but it's really more like a label
126            and is often not numeric.  We currently do not use it. */
127
128         const XMLProperty* program_change = node.property("ProgramChange");
129         if (program_change) {
130                 _id = PatchPrimaryKey(string_to_int(tree, program_change->value()), _id.bank());
131         }
132
133         const XMLProperty* name = node.property("Name");
134         if (!name) {
135                 return -1;
136         }
137         _name = name->value();
138
139         XMLNode* commands = node.child("PatchMIDICommands");
140         if (commands) {
141                 if (initialize_primary_key_from_commands(tree, _id, commands) &&
142                     !program_change) {
143                         return -1;  // Failed to find a program number anywhere
144                 }
145         }
146
147         XMLNode* use_note_name_list = node.child("UsesNoteNameList");
148         if (use_note_name_list) {
149                 _note_list_name = use_note_name_list->property ("Name")->value();
150         }
151
152         return 0;
153 }
154
155 XMLNode&
156 Note::get_state (void)
157 {
158         XMLNode* node = new XMLNode("Note");
159         node->set_property("Number", _number);
160         node->set_property("Name",   _name);
161
162         return *node;
163 }
164
165 int
166 Note::set_state (const XMLTree& tree, const XMLNode& node)
167 {
168         assert(node.name() == "Note");
169
170         const int num = string_to_int(tree, node.property("Number")->value());
171         if (num > 127) {
172                 PBD::warning << string_compose("%1: Note number %2 (%3) out of range",
173                                                tree.filename(), num, _name)
174                              << endmsg;
175                 return -1;
176         }
177
178         _number = num;
179         _name   = node.property("Name")->value();
180
181         return 0;
182 }
183
184 XMLNode&
185 NoteNameList::get_state (void)
186 {
187         XMLNode* node = new XMLNode("NoteNameList");
188         node->set_property("Name", _name);
189
190         return *node;
191 }
192
193 static void
194 add_note_from_xml (NoteNameList::Notes& notes, const XMLTree& tree, const XMLNode& node)
195 {
196         boost::shared_ptr<Note> note(new Note());
197         if (!note->set_state (tree, node)) {
198                 if (!notes[note->number()]) {
199                         notes[note->number()] = note;
200                 } else {
201                         PBD::warning
202                                 << string_compose("%1: Duplicate note number %2 (%3) ignored",
203                                                   tree.filename(), (int)note->number(), note->name())
204                                 << endmsg;
205                 }
206         }
207 }
208
209 int
210 NoteNameList::set_state (const XMLTree& tree, const XMLNode& node)
211 {
212         assert(node.name() == "NoteNameList");
213         _name = node.property("Name")->value();
214         _notes.clear();
215         _notes.resize(128);
216
217         for (XMLNodeList::const_iterator i = node.children().begin();
218              i != node.children().end(); ++i) {
219                 if ((*i)->name() == "Note") {
220                         add_note_from_xml(_notes, tree, **i);
221                 } else if ((*i)->name() == "NoteGroup") {
222                         for (XMLNodeList::const_iterator j = (*i)->children().begin();
223                              j != (*i)->children().end(); ++j) {
224                                 if ((*j)->name() == "Note") {
225                                         add_note_from_xml(_notes, tree, **j);
226                                 } else {
227                                         PBD::warning << string_compose("%1: Invalid NoteGroup child %2 ignored",
228                                                                        tree.filename(), (*j)->name())
229                                                      << endmsg;
230                                 }
231                         }
232                 }
233         }
234
235         return 0;
236 }
237
238 XMLNode&
239 Control::get_state (void)
240 {
241         XMLNode* node = new XMLNode("Control");
242         node->set_property("Type",   _type);
243         node->set_property("Number", _number);
244         node->set_property("Name",   _name);
245
246         return *node;
247 }
248
249 int
250 Control::set_state (const XMLTree& tree, const XMLNode& node)
251 {
252         assert(node.name() == "Control");
253         if (node.property("Type")) {
254                 _type = node.property("Type")->value();
255         } else {
256                 _type = "7bit";
257         }
258         if (_type == "NRPN") {
259                 return -1;
260         }
261         _number = string_to_int(tree, node.property("Number")->value());
262         _name   = node.property("Name")->value();
263
264         for (XMLNodeList::const_iterator i = node.children().begin();
265              i != node.children().end(); ++i) {
266                 if ((*i)->name() == "Values") {
267                         // <Values> has Min and Max properties, but we don't care
268                         for (XMLNodeList::const_iterator j = (*i)->children().begin();
269                              j != (*i)->children().end(); ++j) {
270                                 if ((*j)->name() == "ValueNameList") {
271                                         _value_name_list = boost::shared_ptr<ValueNameList>(new ValueNameList());
272                                         _value_name_list->set_state(tree, **j);
273                                 } else if ((*j)->name() == "UsesValueNameList") {
274                                         _value_name_list_name = (*j)->property("Name")->value();
275                                 }
276                         }
277                 }
278         }
279
280         return 0;
281 }
282
283 XMLNode&
284 ControlNameList::get_state (void)
285 {
286         XMLNode* node = new XMLNode("ControlNameList");
287         node->set_property("Name", _name);
288
289         return *node;
290 }
291
292 int
293 ControlNameList::set_state (const XMLTree& tree, const XMLNode& node)
294 {
295         assert(node.name() == "ControlNameList");
296         _name = node.property("Name")->value();
297
298         _controls.clear();
299         for (XMLNodeList::const_iterator i = node.children().begin();
300              i != node.children().end(); ++i) {
301                 if ((*i)->name() == "Control") {
302                         boost::shared_ptr<Control> control(new Control());
303                         if (control->set_state (tree, *(*i))) {
304                                 continue;
305                         }
306                         if (_controls.find(control->number()) == _controls.end()) {
307                                 _controls.insert(make_pair(control->number(), control));
308                         } else {
309                                 PBD::warning << string_compose("%1: Duplicate control %2 ignored",
310                                                                tree.filename(), control->number())
311                                              << endmsg;
312                         }
313                 }
314         }
315
316         return 0;
317 }
318
319 boost::shared_ptr<const Control>
320 ControlNameList::control(uint16_t num) const
321 {
322         Controls::const_iterator i = _controls.find(num);
323         if (i != _controls.end()) {
324                 return i->second;
325         }
326         return boost::shared_ptr<const Control>();
327 }
328
329 XMLNode&
330 Value::get_state (void)
331 {
332         XMLNode* node = new XMLNode("Value");
333         node->set_property("Number", _number);
334         node->set_property("Name",   _name);
335
336         return *node;
337 }
338
339 int
340 Value::set_state (const XMLTree& tree, const XMLNode& node)
341 {
342         assert(node.name() == "Value");
343         _number = string_to_int(tree, node.property("Number")->value());
344         _name   = node.property("Name")->value();
345
346         return 0;
347 }
348
349 XMLNode&
350 ValueNameList::get_state (void)
351 {
352         XMLNode* node = new XMLNode("ValueNameList");
353         node->set_property("Name", _name);
354
355         return *node;
356 }
357
358 int
359 ValueNameList::set_state (const XMLTree& tree, const XMLNode& node)
360 {
361         assert(node.name() == "ValueNameList");
362         const XMLProperty* name_prop = node.property("Name");
363         if (name_prop) {
364                 // May be anonymous if written inline within a single <Control> tag
365                 _name = name_prop->value();
366         }
367
368         _values.clear();
369         for (XMLNodeList::const_iterator i = node.children().begin();
370              i != node.children().end(); ++i) {
371                 if ((*i)->name() == "Value") {
372                         boost::shared_ptr<Value> value(new Value());
373                         value->set_state (tree, *(*i));
374                         if (_values.find(value->number()) == _values.end()) {
375                                 _values.insert(make_pair(value->number(), value));
376                         } else {
377                                 PBD::warning << string_compose("%1: Duplicate value %2 ignored",
378                                                                tree.filename(), value->number())
379                                              << endmsg;
380                         }
381                 }
382         }
383
384         return 0;
385 }
386
387 boost::shared_ptr<const Value>
388 ValueNameList::value(uint16_t num) const
389 {
390         Values::const_iterator i = _values.find(num);
391         if (i != _values.end()) {
392                 return i->second;
393         }
394         return boost::shared_ptr<const Value>();
395 }
396
397 boost::shared_ptr<const Value>
398 ValueNameList::max_value_below(uint16_t num) const
399 {
400         Values::const_iterator i = _values.lower_bound(num);
401         if (i->first == num) {
402                 // Exact match
403                 return i->second;
404         } else if (i == _values.begin()) {
405                 // No value is < num
406                 return boost::shared_ptr<const Value>();
407         } else {
408                 // Found the smallest element >= num, so the previous one is our result
409                 --i;
410                 return i->second;
411         }
412 }
413
414 XMLNode&
415 PatchBank::get_state (void)
416 {
417         XMLNode* node = new XMLNode("PatchBank");
418         node->set_property("Name",   _name);
419         XMLNode* patch_name_list = node->add_child("PatchNameList");
420         for (PatchNameList::iterator patch = _patch_name_list.begin();
421             patch != _patch_name_list.end();
422             ++patch) {
423                 patch_name_list->add_child_nocopy((*patch)->get_state());
424         }
425
426         return *node;
427 }
428
429 int
430 PatchBank::set_state (const XMLTree& tree, const XMLNode& node)
431 {
432         assert(node.name() == "PatchBank");
433         _name   = node.property("Name")->value();
434
435         XMLNode* commands = node.child("MIDICommands");
436         if (commands) {
437                 PatchPrimaryKey id (0, 0);
438                 if (initialize_primary_key_from_commands (tree, id, commands)) {
439                         return -1;
440                 }
441                 _number = id.bank();
442         }
443
444         XMLNode* patch_name_list = node.child("PatchNameList");
445
446         if (patch_name_list) {
447                 const XMLNodeList patches = patch_name_list->children();
448                 for (XMLNodeList::const_iterator i = patches.begin(); i != patches.end(); ++i) {
449                         boost::shared_ptr<Patch> patch (new Patch (string(), 0, _number));
450                         if (0 == patch->set_state(tree, *(*i))) {
451                                 _patch_name_list.push_back(patch);
452                         }
453                 }
454         } else {
455                 XMLNode* use_patch_name_list = node.child ("UsesPatchNameList");
456                 if (use_patch_name_list) {
457                         _patch_list_name = use_patch_name_list->property ("Name")->value();
458                 } else {
459                         error << "Patch without patch name list - patchfile will be ignored" << endmsg;
460                         return -1;
461                 }
462         }
463
464         return 0;
465 }
466
467 int
468 PatchBank::set_patch_name_list (const PatchNameList& pnl)
469 {
470         _patch_name_list = pnl;
471         _patch_list_name = "";
472
473         for (PatchNameList::iterator p = _patch_name_list.begin(); p != _patch_name_list.end(); p++) {
474                 (*p)->set_bank_number (_number);
475         }
476
477         return 0;
478 }
479
480 std::ostream&
481 operator<< (std::ostream& os, const ChannelNameSet& cns)
482 {
483         os << "Channel Name Set: name = " << cns._name << endl
484            << "Map size " << cns._patch_map.size () << endl
485            << "List size " << cns._patch_list.size() << endl
486            << "Patch list name = [" << cns._patch_list_name << ']' << endl
487            << "Available channels : ";
488         for (set<uint8_t>::const_iterator x = cns._available_for_channels.begin(); x != cns._available_for_channels.end(); ++x) {
489                 os << (int) (*x) << ' ';
490         }
491         os << endl;
492
493         for (ChannelNameSet::PatchBanks::const_iterator pbi = cns._patch_banks.begin(); pbi != cns._patch_banks.end(); ++pbi) {
494                 os << "\tPatch Bank " << (*pbi)->name() << " with " << (*pbi)->patch_name_list().size() << " patches\n";
495                 for (PatchNameList::const_iterator pni = (*pbi)->patch_name_list().begin(); pni != (*pbi)->patch_name_list().end(); ++pni) {
496                         os << "\t\tPatch name " << (*pni)->name() << " prog " << (int) (*pni)->program_number() << " bank " << (*pni)->bank_number() << endl;
497                 }
498         }
499
500         return os;
501 }
502
503 void
504 ChannelNameSet::set_patch_banks (const ChannelNameSet::PatchBanks& pb)
505 {
506         _patch_banks = pb;
507
508         _patch_map.clear ();
509         _patch_list.clear ();
510         _patch_list_name = "";
511         _available_for_channels.clear ();
512
513         for (PatchBanks::const_iterator pbi = _patch_banks.begin(); pbi != _patch_banks.end(); ++pbi) {
514                 for (PatchNameList::const_iterator pni = (*pbi)->patch_name_list().begin(); pni != (*pbi)->patch_name_list().end(); ++pni) {
515                         _patch_map[(*pni)->patch_primary_key()] = (*pni);
516                         _patch_list.push_back ((*pni)->patch_primary_key());
517                 }
518         }
519
520         for (uint8_t n = 0; n < 16; ++n) {
521                 _available_for_channels.insert (n);
522         }
523 }
524
525 void
526 ChannelNameSet::use_patch_name_list (const PatchNameList& pnl)
527 {
528         for (PatchNameList::const_iterator p = pnl.begin(); p != pnl.end(); ++p) {
529                 _patch_map[(*p)->patch_primary_key()] = (*p);
530                 _patch_list.push_back ((*p)->patch_primary_key());
531         }
532 }
533
534 XMLNode&
535 ChannelNameSet::get_state (void)
536 {
537         XMLNode* node = new XMLNode("ChannelNameSet");
538         node->set_property("Name",   _name);
539
540         XMLNode* available_for_channels = node->add_child("AvailableForChannels");
541         assert(available_for_channels);
542
543         for (uint8_t channel = 0; channel < 16; ++channel) {
544                 XMLNode* available_channel = available_for_channels->add_child("AvailableChannel");
545                 assert(available_channel);
546
547                 available_channel->set_property("Channel", channel);
548
549                 if (_available_for_channels.find(channel) != _available_for_channels.end()) {
550                         available_channel->set_property("Available", "true");
551                 } else {
552                         available_channel->set_property("Available", "false");
553                 }
554         }
555
556         for (PatchBanks::iterator patch_bank = _patch_banks.begin();
557             patch_bank != _patch_banks.end();
558             ++patch_bank) {
559                 node->add_child_nocopy((*patch_bank)->get_state());
560         }
561
562         return *node;
563 }
564
565 int
566 ChannelNameSet::set_state (const XMLTree& tree, const XMLNode& node)
567 {
568         assert(node.name() == "ChannelNameSet");
569         _name = node.property("Name")->value();
570
571         const XMLNodeList children = node.children();
572         for (XMLNodeList::const_iterator i = children.begin(); i != children.end(); ++i) {
573                 XMLNode* node = *i;
574                 assert(node);
575                 if (node->name() == "AvailableForChannels") {
576                         boost::shared_ptr<XMLSharedNodeList> channels =
577                                 tree.find("//AvailableChannel[@Available = 'true']/@Channel", node);
578                         for (XMLSharedNodeList::const_iterator i = channels->begin();
579                             i != channels->end();
580                             ++i) {
581                                 try {
582                                         _available_for_channels.insert(
583                                                         string_to_int(tree, (*i)->attribute_value()));
584                                 } catch (XMLException &e) {
585                                         cerr << "ChannelNameSet::set_state: " << e.what () << endl;
586                                 }
587                         }
588                 } else if (node->name() == "PatchBank") {
589                         boost::shared_ptr<PatchBank> bank (new PatchBank ());
590                         bank->set_state(tree, *node);
591                         _patch_banks.push_back(bank);
592                         const PatchNameList& patches = bank->patch_name_list();
593                         for (PatchNameList::const_iterator patch = patches.begin();
594                              patch != patches.end();
595                              ++patch) {
596                                 _patch_map[(*patch)->patch_primary_key()] = *patch;
597                                 _patch_list.push_back((*patch)->patch_primary_key());
598                         }
599                 } else if (node->name() == "UsesNoteNameList") {
600                         _note_list_name = node->property ("Name")->value();
601                 } else if (node->name() == "UsesControlNameList") {
602                         _control_list_name = node->property ("Name")->value();
603                 }
604         }
605
606         return 0;
607 }
608
609 int
610 CustomDeviceMode::set_state(const XMLTree& tree, const XMLNode& a_node)
611 {
612         assert(a_node.name() == "CustomDeviceMode");
613
614         _name = a_node.property("Name")->value();
615
616         boost::shared_ptr<XMLSharedNodeList> channel_name_set_assignments =
617                 tree.find("//ChannelNameSetAssign", const_cast<XMLNode *>(&a_node));
618         for (XMLSharedNodeList::const_iterator i = channel_name_set_assignments->begin();
619             i != channel_name_set_assignments->end();
620             ++i) {
621                 const int     channel  = string_to_int(tree, (*i)->property("Channel")->value());
622                 const string& name_set = (*i)->property("NameSet")->value();
623                 assert( 1 <= channel && channel <= 16 );
624                 _channel_name_set_assignments[channel - 1] = name_set;
625         }
626         return 0;
627 }
628
629 XMLNode&
630 CustomDeviceMode::get_state(void)
631 {
632         XMLNode* custom_device_mode = new XMLNode("CustomDeviceMode");
633         custom_device_mode->set_property("Name",   _name);
634         XMLNode* channel_name_set_assignments =
635                 custom_device_mode->add_child("ChannelNameSetAssignments");
636         for (int i = 0; i < 15 && !_channel_name_set_assignments[i].empty(); i++) {
637                 XMLNode* channel_name_set_assign =
638                         channel_name_set_assignments->add_child("ChannelNameSetAssign");
639                 channel_name_set_assign->set_property("Channel", i + 1);
640                 channel_name_set_assign->set_property("NameSet", _channel_name_set_assignments[i]);
641         }
642
643         return *custom_device_mode;
644 }
645
646 boost::shared_ptr<const ValueNameList>
647 MasterDeviceNames::value_name_list_by_control(const std::string& mode, uint8_t channel, uint8_t number)
648 {
649         boost::shared_ptr<ChannelNameSet> chan_names = channel_name_set_by_channel(mode, channel);
650         if (!chan_names) {
651                 return boost::shared_ptr<const ValueNameList>();
652         }
653
654         boost::shared_ptr<ControlNameList> control_names = control_name_list(chan_names->control_list_name());
655         if (!control_names) {
656                 return boost::shared_ptr<const ValueNameList>();
657         }
658
659         boost::shared_ptr<const Control> control = control_names->control(number);
660         if (!control) {
661                 return boost::shared_ptr<const ValueNameList>();
662         }
663
664         if (!control->value_name_list_name().empty()) {
665                 return value_name_list(control->value_name_list_name());
666         } else {
667                 return control->value_name_list();
668         }
669 }
670
671 boost::shared_ptr<CustomDeviceMode>
672 MasterDeviceNames::custom_device_mode_by_name(const std::string& mode_name)
673 {
674         return _custom_device_modes[mode_name];
675 }
676
677 boost::shared_ptr<ChannelNameSet>
678 MasterDeviceNames::channel_name_set_by_channel(const std::string& mode, uint8_t channel)
679 {
680         boost::shared_ptr<CustomDeviceMode> cdm = custom_device_mode_by_name(mode);
681         boost::shared_ptr<ChannelNameSet> cns =  _channel_name_sets[cdm->channel_name_set_name_by_channel(channel)];
682         return cns;
683 }
684
685 boost::shared_ptr<Patch>
686 MasterDeviceNames::find_patch(const std::string& mode, uint8_t channel, const PatchPrimaryKey& key)
687 {
688         boost::shared_ptr<ChannelNameSet> cns = channel_name_set_by_channel(mode, channel);
689         if (!cns) return boost::shared_ptr<Patch>();
690         return cns->find_patch(key);
691 }
692
693 boost::shared_ptr<ChannelNameSet>
694 MasterDeviceNames::channel_name_set(const std::string& name)
695 {
696         ChannelNameSets::const_iterator i = _channel_name_sets.find(name);
697         if (i != _channel_name_sets.end()) {
698                 return i->second;
699         }
700         return boost::shared_ptr<ChannelNameSet>();
701 }
702
703 boost::shared_ptr<ControlNameList>
704 MasterDeviceNames::control_name_list(const std::string& name)
705 {
706         ControlNameLists::const_iterator i = _control_name_lists.find(name);
707         if (i != _control_name_lists.end()) {
708                 return i->second;
709         }
710         return boost::shared_ptr<ControlNameList>();
711 }
712
713 boost::shared_ptr<ValueNameList>
714 MasterDeviceNames::value_name_list(const std::string& name)
715 {
716         ValueNameLists::const_iterator i = _value_name_lists.find(name);
717         if (i != _value_name_lists.end()) {
718                 return i->second;
719         }
720         return boost::shared_ptr<ValueNameList>();
721 }
722
723 boost::shared_ptr<NoteNameList>
724 MasterDeviceNames::note_name_list(const std::string& name)
725 {
726         NoteNameLists::const_iterator i = _note_name_lists.find(name);
727         if (i != _note_name_lists.end()) {
728                 return i->second;
729         }
730         return boost::shared_ptr<NoteNameList>();
731 }
732
733 std::string
734 MasterDeviceNames::note_name(const std::string& mode_name,
735                              uint8_t            channel,
736                              uint16_t           bank,
737                              uint8_t            program,
738                              uint8_t            number)
739 {
740         if (number > 127) {
741                 return "";
742         }
743
744         boost::shared_ptr<const NoteNameList> note_names;
745         boost::shared_ptr<const Patch> patch(
746                 find_patch(mode_name, channel, PatchPrimaryKey(program, bank)));
747         if (patch) {
748                 note_names = note_name_list(patch->note_list_name());
749         }
750
751         if (!note_names) {
752                 /* No note names specific to this patch, check the ChannelNameSet */
753                 boost::shared_ptr<ChannelNameSet> chan_names = channel_name_set_by_channel(
754                         mode_name, channel);
755                 if (chan_names) {
756                         note_names = note_name_list(chan_names->note_list_name());
757                 }
758         }
759         if (!note_names) {
760                 return "";
761         }
762
763         boost::shared_ptr<const Note> note(note_names->notes()[number]);
764         return note ? note->name() : "";
765 }
766
767 int
768 MasterDeviceNames::set_state(const XMLTree& tree, const XMLNode&)
769 {
770         // Manufacturer
771         boost::shared_ptr<XMLSharedNodeList> manufacturer = tree.find("//Manufacturer");
772         assert(manufacturer->size() == 1);
773         _manufacturer = manufacturer->front()->children().front()->content();
774
775         // Models
776         boost::shared_ptr<XMLSharedNodeList> models = tree.find("//Model");
777         assert(models->size() >= 1);
778         for (XMLSharedNodeList::iterator i = models->begin();
779              i != models->end();
780              ++i) {
781                 const XMLNodeList& contents = (*i)->children();
782                 assert(contents.size() == 1);
783                 XMLNode * content = *(contents.begin());
784                 assert(content->is_content());
785                 _models.insert(content->content());
786         }
787
788         // CustomDeviceModes
789         boost::shared_ptr<XMLSharedNodeList> custom_device_modes = tree.find("//CustomDeviceMode");
790         for (XMLSharedNodeList::iterator i = custom_device_modes->begin();
791              i != custom_device_modes->end();
792              ++i) {
793                 boost::shared_ptr<CustomDeviceMode> custom_device_mode(new CustomDeviceMode());
794                 custom_device_mode->set_state(tree, *(*i));
795
796                 _custom_device_modes[custom_device_mode->name()] = custom_device_mode;
797                 _custom_device_mode_names.push_back(custom_device_mode->name());
798         }
799
800         // ChannelNameSets
801         boost::shared_ptr<XMLSharedNodeList> channel_name_sets = tree.find("//ChannelNameSet");
802         for (XMLSharedNodeList::iterator i = channel_name_sets->begin();
803              i != channel_name_sets->end();
804              ++i) {
805                 boost::shared_ptr<ChannelNameSet> channel_name_set(new ChannelNameSet());
806                 channel_name_set->set_state(tree, *(*i));
807                 _channel_name_sets[channel_name_set->name()] = channel_name_set;
808         }
809
810         // NoteNameLists
811         boost::shared_ptr<XMLSharedNodeList> note_name_lists = tree.find("//NoteNameList");
812         for (XMLSharedNodeList::iterator i = note_name_lists->begin();
813              i != note_name_lists->end();
814              ++i) {
815                 boost::shared_ptr<NoteNameList> note_name_list(new NoteNameList());
816                 note_name_list->set_state (tree, *(*i));
817                 _note_name_lists[note_name_list->name()] = note_name_list;
818         }
819
820         // ControlNameLists
821         boost::shared_ptr<XMLSharedNodeList> control_name_lists = tree.find("//ControlNameList");
822         for (XMLSharedNodeList::iterator i = control_name_lists->begin();
823              i != control_name_lists->end();
824              ++i) {
825                 boost::shared_ptr<ControlNameList> control_name_list(new ControlNameList());
826                 control_name_list->set_state (tree, *(*i));
827                 _control_name_lists[control_name_list->name()] = control_name_list;
828         }
829
830         // ValueNameLists
831         boost::shared_ptr<XMLSharedNodeList> value_name_lists = tree.find("/child::MIDINameDocument/child::MasterDeviceNames/child::ValueNameList");
832         for (XMLSharedNodeList::iterator i = value_name_lists->begin();
833              i != value_name_lists->end();
834              ++i) {
835                 boost::shared_ptr<ValueNameList> value_name_list(new ValueNameList());
836                 value_name_list->set_state (tree, *(*i));
837                 _value_name_lists[value_name_list->name()] = value_name_list;
838         }
839
840         // global/post-facto PatchNameLists
841         boost::shared_ptr<XMLSharedNodeList> patch_name_lists = tree.find("/child::MIDINameDocument/child::MasterDeviceNames/child::PatchNameList");
842         for (XMLSharedNodeList::iterator i = patch_name_lists->begin();
843              i != patch_name_lists->end();
844              ++i) {
845
846                 PatchNameList patch_name_list;
847                 const XMLNodeList patches = (*i)->children();
848
849                 for (XMLNodeList::const_iterator p = patches.begin(); p != patches.end(); ++p) {
850                         boost::shared_ptr<Patch> patch (new Patch ());
851                         patch->set_state(tree, *(*p));
852                         patch_name_list.push_back(patch);
853                 }
854
855                 if (!patch_name_list.empty()) {
856                         _patch_name_lists[(*i)->property ("Name")->value()] = patch_name_list;
857                 }
858         }
859
860         /* now traverse patches and hook up anything that used UsePatchNameList
861          * to the right patch list
862          */
863
864         for (ChannelNameSets::iterator cns = _channel_name_sets.begin(); cns != _channel_name_sets.end(); ++cns) {
865                 ChannelNameSet::PatchBanks pbs = cns->second->patch_banks();
866                 PatchNameLists::iterator p;
867
868                 for (ChannelNameSet::PatchBanks::iterator pb = pbs.begin(); pb != pbs.end(); ++pb) {
869                         const std::string& pln = (*pb)->patch_list_name();
870                         if (!pln.empty()) {
871                                 if ((p = _patch_name_lists.find (pln)) != _patch_name_lists.end()) {
872                                         if ((*pb)->set_patch_name_list (p->second)) {
873                                                 return -1;
874                                         }
875                                         cns->second->use_patch_name_list (p->second);
876                                 } else {
877                                         error << string_compose ("Patch list name %1 was not found - patch file ignored", pln) << endmsg;
878                                         return -1;
879                                 }
880                         }
881                 }
882
883         }
884
885         return 0;
886 }
887
888 XMLNode&
889 MasterDeviceNames::get_state(void)
890 {
891         static XMLNode nothing("<nothing>");
892         return nothing;
893 }
894
895 MIDINameDocument::MIDINameDocument (const string& file_path)
896         : _file_path(file_path)
897 {
898         XMLTree document;
899         if (!document.read (file_path)) {
900                 throw failed_constructor ();
901         }
902
903         document.set_filename (file_path);
904         set_state (document, *document.root());
905 }
906
907 int
908 MIDINameDocument::set_state (const XMLTree& tree, const XMLNode&)
909 {
910         // Author
911
912         boost::shared_ptr<XMLSharedNodeList> author = tree.find("//Author");
913         if (author->size() < 1) {
914                 error << "No author information in MIDNAM file" << endmsg;
915                 return -1;
916         }
917
918         if (author->front()->children().size() > 0) {
919                 _author = author->front()->children().front()->content();
920         }
921
922         // MasterDeviceNames
923
924         boost::shared_ptr<XMLSharedNodeList> master_device_names_list = tree.find ("//MasterDeviceNames");
925
926         for (XMLSharedNodeList::iterator i = master_device_names_list->begin();
927              i != master_device_names_list->end();
928              ++i) {
929                 boost::shared_ptr<MasterDeviceNames> master_device_names(new MasterDeviceNames());
930
931                 if (master_device_names->set_state(tree, *(*i))) {
932                         return -1;
933                 }
934
935                 for (MasterDeviceNames::Models::const_iterator model = master_device_names->models().begin();
936                      model != master_device_names->models().end();
937                      ++model) {
938                         _master_device_names_list.insert(
939                                 std::pair<std::string, boost::shared_ptr<MasterDeviceNames> >
940                                 (*model,      master_device_names));
941
942                         _all_models.insert(*model);
943                 }
944         }
945
946         return 0;
947 }
948
949 XMLNode&
950 MIDINameDocument::get_state(void)
951 {
952         static XMLNode nothing("<nothing>");
953         return nothing;
954 }
955
956 boost::shared_ptr<MasterDeviceNames>
957 MIDINameDocument::master_device_names(const std::string& model)
958 {
959         MasterDeviceNamesList::const_iterator m = _master_device_names_list.find(model);
960         if (m != _master_device_names_list.end()) {
961                 return boost::shared_ptr<MasterDeviceNames>(m->second);
962         }
963         return boost::shared_ptr<MasterDeviceNames>();
964 }
965
966 const char* general_midi_program_names[128] = {
967         "Acoustic Grand Piano",
968         "Bright Acoustic Piano",
969         "Electric Grand Piano",
970         "Honky-tonk Piano",
971         "Rhodes Piano",
972         "Chorused Piano",
973         "Harpsichord",
974         "Clavinet",
975         "Celesta",
976         "Glockenspiel",
977         "Music Box",
978         "Vibraphone",
979         "Marimba",
980         "Xylophone",
981         "Tubular Bells",
982         "Dulcimer",
983         "Hammond Organ",
984         "Percussive Organ",
985         "Rock Organ",
986         "Church Organ",
987         "Reed Organ",
988         "Accordion",
989         "Harmonica",
990         "Tango Accordion",
991         "Acoustic Guitar (nylon)",
992         "Acoustic Guitar (steel)",
993         "Electric Guitar (jazz)",
994         "Electric Guitar (clean)",
995         "Electric Guitar (muted)",
996         "Overdriven Guitar",
997         "Distortion Guitar",
998         "Guitar Harmonics",
999         "Acoustic Bass",
1000         "Electric Bass (finger)",
1001         "Electric Bass (pick)",
1002         "Fretless Bass",
1003         "Slap Bass 1",
1004         "Slap Bass 2",
1005         "Synth Bass 1",
1006         "Synth Bass 2",
1007         "Violin",
1008         "Viola",
1009         "Cello",
1010         "Contrabass",
1011         "Tremolo Strings",
1012         "Pizzicato Strings",
1013         "Orchestral Harp",
1014         "Timpani",
1015         "String Ensemble 1",
1016         "String Ensemble 2",
1017         "SynthStrings 1",
1018         "SynthStrings 2",
1019         "Choir Aahs",
1020         "Voice Oohs",
1021         "Synth Voice",
1022         "Orchestra Hit",
1023         "Trumpet",
1024         "Trombone",
1025         "Tuba",
1026         "Muted Trumpet",
1027         "French Horn",
1028         "Brass Section",
1029         "Synth Brass 1",
1030         "Synth Brass 2",
1031         "Soprano Sax",
1032         "Alto Sax",
1033         "Tenor Sax",
1034         "Baritone Sax",
1035         "Oboe",
1036         "English Horn",
1037         "Bassoon",
1038         "Clarinet",
1039         "Piccolo",
1040         "Flute",
1041         "Recorder",
1042         "Pan Flute",
1043         "Bottle Blow",
1044         "Shakuhachi",
1045         "Whistle",
1046         "Ocarina",
1047         "Lead 1 (square)",
1048         "Lead 2 (sawtooth)",
1049         "Lead 3 (calliope lead)",
1050         "Lead 4 (chiff lead)",
1051         "Lead 5 (charang)",
1052         "Lead 6 (voice)",
1053         "Lead 7 (fifths)",
1054         "Lead 8 (bass + lead)",
1055         "Pad 1 (new age)",
1056         "Pad 2 (warm)",
1057         "Pad 3 (polysynth)",
1058         "Pad 4 (choir)",
1059         "Pad 5 (bowed)",
1060         "Pad 6 (metallic)",
1061         "Pad 7 (halo)",
1062         "Pad 8 (sweep)",
1063         "FX 1 (rain)",
1064         "FX 2 (soundtrack)",
1065         "FX 3 (crystal)",
1066         "FX 4 (atmosphere)",
1067         "FX 5 (brightness)",
1068         "FX 6 (goblins)",
1069         "FX 7 (echoes)",
1070         "FX 8 (sci-fi)",
1071         "Sitar",
1072         "Banjo",
1073         "Shamisen",
1074         "Koto",
1075         "Kalimba",
1076         "Bagpipe",
1077         "Fiddle",
1078         "Shanai",
1079         "Tinkle Bell",
1080         "Agogo",
1081         "Steel Drums",
1082         "Woodblock",
1083         "Taiko Drum",
1084         "Melodic Tom",
1085         "Synth Drum",
1086         "Reverse Cymbal",
1087         "Guitar Fret Noise",
1088         "Breath Noise",
1089         "Seashore",
1090         "Bird Tweet",
1091         "Telephone Ring",
1092         "Helicopter",
1093         "Applause",
1094         "Gunshot",
1095 };
1096
1097 } //namespace Name
1098
1099 } //namespace MIDI
1100