a lot of tweaking and fixing for the MIDNAM parsing code so that it correctly handles...
[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 (PatchBank* b)
40 {
41         use_bank_info (b);
42 }
43
44 Patch::Patch (std::string a_number, std::string a_name, PatchBank* a_bank)
45         : _number (a_number)
46         , _name (a_name)
47 {
48         use_bank_info (a_bank);
49 }
50
51 XMLNode&
52 Patch::get_state (void)
53 {
54         XMLNode* node = new XMLNode("Patch");
55         node->add_property("Number", _number);
56         node->add_property("Name",   _name);
57         /*
58         typedef std::list< boost::shared_ptr< Evoral::MIDIEvent<double> > > PatchMidiCommands;
59         XMLNode* commands = node->add_child("PatchMIDICommands");
60         for (PatchMidiCommands::const_iterator event = _patch_midi_commands.begin();
61             event != _patch_midi_commands.end();
62             ++event) {
63                 commands->add_child_copy(*((((Evoral::MIDIEvent&)*event)).to_xml()));
64         }
65         */
66
67         return *node;
68 }
69
70 int initialize_primary_key_from_commands (PatchPrimaryKey& id, const XMLNode* node)
71 {
72         const XMLNodeList events = node->children();
73         for (XMLNodeList::const_iterator i = events.begin(); i != events.end(); ++i) {
74                 XMLNode* node = *i;
75                 if (node->name() == "ControlChange") {
76                         string control = node->property("Control")->value();
77                         assert(control != "");
78                         string value = node->property("Value")->value();
79                         assert(value != "");
80
81                         if (control == "0") {
82                                 id.msb = PBD::atoi(value);
83                         } else if (control == "32") {
84                                 id.lsb = PBD::atoi(value);
85                         }
86                 } else if (node->name() == "ProgramChange") {
87                         string number = node->property("Number")->value();
88                         assert(number != "");
89                         id.program_number = PBD::atoi(number);
90                 }
91         }
92
93         return 0;
94 }
95
96 int
97 Patch::set_state (const XMLTree&, const XMLNode& node)
98 {
99         if (node.name() != "Patch") {
100                 cerr << "Incorrect node " << node.name() << " handed to Patch" << endl;
101                 return -1;
102         }
103
104         const XMLProperty* prop = node.property ("Number");
105
106         if (!prop) {
107                 return -1;
108         }
109         _number = prop->value();
110
111         prop = node.property ("Name");
112
113         if (!prop) {
114                 return -1;
115         }
116         _name   = prop->value();
117
118         XMLNode* commands = node.child("PatchMIDICommands");
119
120         if (commands) {
121                 if (initialize_primary_key_from_commands(_id, commands)) {
122                         return -1;
123                 }
124         } else {
125                 string program_change = node.property("ProgramChange")->value();
126                 assert(program_change.length());
127                 _id.program_number = PBD::atoi(program_change);
128         }
129
130         return 0;
131 }
132
133 int
134 Patch::use_bank_info (PatchBank* bank)
135 {
136         if (bank) {
137                 if (bank->patch_primary_key() ) {
138                         _id.msb = bank->patch_primary_key()->msb;
139                         _id.lsb = bank->patch_primary_key()->lsb;
140                 } else {
141                         return -1;
142                 }
143         }
144
145         if (!_id.is_sane()) {
146                 return -1;
147         }
148
149         return 0;
150 }
151
152 XMLNode&
153 Note::get_state (void)
154 {
155         XMLNode* node = new XMLNode("Note");
156         node->add_property("Number", _number);
157         node->add_property("Name",   _name);
158
159         return *node;
160 }
161
162 int
163 Note::set_state (const XMLTree&, const XMLNode& node)
164 {
165         assert(node.name() == "Note");
166         _number = node.property("Number")->value();
167         _name   = node.property("Name")->value();
168
169         return 0;
170 }
171
172 XMLNode&
173 NoteNameList::get_state (void)
174 {
175         XMLNode* node = new XMLNode("NoteNameList");
176         node->add_property("Name",   _name);
177
178         return *node;
179 }
180
181 int
182 NoteNameList::set_state (const XMLTree& tree, const XMLNode& node)
183 {
184         assert(node.name() == "NoteNameList");
185         _name   = node.property("Name")->value();
186
187         boost::shared_ptr<XMLSharedNodeList> notes = tree.find("//Note");
188         for (XMLSharedNodeList::const_iterator i = notes->begin(); i != notes->end(); ++i) {
189                 boost::shared_ptr<Note> note(new Note());
190                 note->set_state (tree, *(*i));
191                 _notes.push_back(note);
192         }
193
194         return 0;
195 }
196
197
198 XMLNode&
199 PatchBank::get_state (void)
200 {
201         XMLNode* node = new XMLNode("PatchBank");
202         node->add_property("Name",   _name);
203         XMLNode* patch_name_list = node->add_child("PatchNameList");
204         for (PatchNameList::iterator patch = _patch_name_list.begin();
205             patch != _patch_name_list.end();
206             ++patch) {
207                 patch_name_list->add_child_nocopy((*patch)->get_state());
208         }
209
210         return *node;
211 }
212
213 int
214 PatchBank::set_state (const XMLTree& tree, const XMLNode& node)
215 {
216         assert(node.name() == "PatchBank");
217         _name   = node.property("Name")->value();
218
219         XMLNode* commands = node.child("MIDICommands");
220         if (commands) {
221                 _id = new PatchPrimaryKey();
222                 if (initialize_primary_key_from_commands(*_id, commands)) {
223                         return -1;
224                 }
225         }
226
227         XMLNode* patch_name_list = node.child("PatchNameList");
228
229         if (patch_name_list) {
230                 const XMLNodeList patches = patch_name_list->children();
231                 for (XMLNodeList::const_iterator i = patches.begin(); i != patches.end(); ++i) {
232                         boost::shared_ptr<Patch> patch(new Patch(this));
233                         patch->set_state(tree, *(*i));
234                         _patch_name_list.push_back(patch);
235                 }
236         } else {
237                 XMLNode* use_patch_name_list = node.child ("UsesPatchNameList");
238                 if (use_patch_name_list) {
239                         _patch_list_name = node.property ("Name")->value();
240                 } else {
241                         error << "Patch without patch name list - patchfile will be ignored" << endmsg;
242                         return -1;
243                 }
244         }
245
246         return 0;
247 }
248
249 int
250 PatchBank::set_patch_name_list (const PatchNameList& pnl)
251 {
252         _patch_name_list = pnl;
253         _patch_list_name = "";
254         
255         for (PatchNameList::iterator p = _patch_name_list.begin(); p != _patch_name_list.end(); p++) {
256                 if ((*p)->use_bank_info (this)) {
257                         return -1;
258                 }
259         }
260
261         return 0;
262 }
263
264 XMLNode&
265 ChannelNameSet::get_state (void)
266 {
267         XMLNode* node = new XMLNode("ChannelNameSet");
268         node->add_property("Name",   _name);
269
270         XMLNode* available_for_channels = node->add_child("AvailableForChannels");
271         assert(available_for_channels);
272
273         for (uint8_t channel = 0; channel < 16; ++channel) {
274                 XMLNode* available_channel = available_for_channels->add_child("AvailableChannel");
275                 assert(available_channel);
276
277                 available_channel->add_property("Channel", (long) channel);
278
279                 if (_available_for_channels.find(channel) != _available_for_channels.end()) {
280                         available_channel->add_property("Available", "true");
281                 } else {
282                         available_channel->add_property("Available", "false");
283                 }
284         }
285
286         for (PatchBanks::iterator patch_bank = _patch_banks.begin();
287             patch_bank != _patch_banks.end();
288             ++patch_bank) {
289                 node->add_child_nocopy((*patch_bank)->get_state());
290         }
291
292         return *node;
293 }
294
295 int
296 ChannelNameSet::set_state (const XMLTree& tree, const XMLNode& node)
297 {
298         assert(node.name() == "ChannelNameSet");
299         _name   = node.property("Name")->value();
300         const XMLNodeList children = node.children();
301         for (XMLNodeList::const_iterator i = children.begin(); i != children.end(); ++i) {
302                 XMLNode* node = *i;
303                 assert(node);
304                 if (node->name() == "AvailableForChannels") {
305                         boost::shared_ptr<XMLSharedNodeList> channels =
306                                 tree.find("//AvailableChannel[@Available = 'true']/@Channel");
307                         for(XMLSharedNodeList::const_iterator i = channels->begin();
308                             i != channels->end();
309                             ++i) {
310                                 _available_for_channels.insert(atoi((*i)->attribute_value().c_str()));
311                         }
312                 }
313
314                 if (node->name() == "PatchBank") {
315                         boost::shared_ptr<PatchBank> bank(new PatchBank());
316                         bank->set_state(tree, *node);
317                         _patch_banks.push_back(bank);
318                         const PatchBank::PatchNameList& patches = bank->patch_name_list();
319                         for (PatchBank::PatchNameList::const_iterator patch = patches.begin();
320                              patch != patches.end();
321                              ++patch) {
322                                 _patch_map[(*patch)->patch_primary_key()] = *patch;
323                                 _patch_list.push_back((*patch)->patch_primary_key());
324                         }
325                 }
326         }
327
328         return 0;
329 }
330
331 int
332 CustomDeviceMode::set_state(const XMLTree& tree, const XMLNode& a_node)
333 {
334         assert(a_node.name() == "CustomDeviceMode");
335
336         _name = a_node.property("Name")->value();
337
338         boost::shared_ptr<XMLSharedNodeList> channel_name_set_assignments =
339                 tree.find("//ChannelNameSetAssign");
340         for(XMLSharedNodeList::const_iterator i = channel_name_set_assignments->begin();
341             i != channel_name_set_assignments->end();
342             ++i) {
343                 int channel = atoi((*i)->property("Channel")->value().c_str());
344                 string name_set = (*i)->property("NameSet")->value();
345                 assert( 1 <= channel && channel <= 16 );
346                 _channel_name_set_assignments[channel - 1] = name_set;
347         }
348         return 0;
349 }
350
351 XMLNode&
352 CustomDeviceMode::get_state(void)
353 {
354         XMLNode* custom_device_mode = new XMLNode("CustomDeviceMode");
355         custom_device_mode->add_property("Name",   _name);
356         XMLNode* channel_name_set_assignments =
357                 custom_device_mode->add_child("ChannelNameSetAssignments");
358         for (int i = 0; i < 15 && !_channel_name_set_assignments[i].empty(); i++) {
359                 XMLNode* channel_name_set_assign =
360                         channel_name_set_assignments->add_child("ChannelNameSetAssign");
361                 channel_name_set_assign->add_property("Channel", i + 1);
362                 channel_name_set_assign->add_property("NameSet", _channel_name_set_assignments[i]);
363         }
364
365         return *custom_device_mode;
366 }
367
368 int
369 MasterDeviceNames::set_state(const XMLTree& tree, const XMLNode& a_node)
370 {
371         // Manufacturer
372         boost::shared_ptr<XMLSharedNodeList> manufacturer = tree.find("//Manufacturer");
373         assert(manufacturer->size() == 1);
374         _manufacturer = manufacturer->front()->content();
375
376         // Models
377         boost::shared_ptr<XMLSharedNodeList> models = tree.find("//Model");
378         assert(models->size() >= 1);
379         for (XMLSharedNodeList::iterator i = models->begin();
380              i != models->end();
381              ++i) {
382                 const XMLNodeList& contents = (*i)->children();
383                 assert(contents.size() == 1);
384                 XMLNode * content = *(contents.begin());
385                 assert(content->is_content());
386                 _models.push_back(content->content());
387         }
388
389         // CustomDeviceModes
390         boost::shared_ptr<XMLSharedNodeList> custom_device_modes = tree.find("//CustomDeviceMode");
391         for (XMLSharedNodeList::iterator i = custom_device_modes->begin();
392              i != custom_device_modes->end();
393              ++i) {
394                 boost::shared_ptr<CustomDeviceMode> custom_device_mode(new CustomDeviceMode());
395                 custom_device_mode->set_state(tree, *(*i));
396
397                 _custom_device_modes[custom_device_mode->name()] = custom_device_mode;
398                 _custom_device_mode_names.push_back(custom_device_mode->name());
399         }
400
401         // ChannelNameSets
402         boost::shared_ptr<XMLSharedNodeList> channel_name_sets = tree.find("//ChannelNameSet");
403         for (XMLSharedNodeList::iterator i = channel_name_sets->begin();
404              i != channel_name_sets->end();
405              ++i) {
406                 boost::shared_ptr<ChannelNameSet> channel_name_set(new ChannelNameSet());
407                 channel_name_set->set_state(tree, *(*i));
408                 _channel_name_sets[channel_name_set->name()] = channel_name_set;
409         }
410
411         // NoteNameLists
412         boost::shared_ptr<XMLSharedNodeList> note_name_lists = tree.find("//NoteNameList");
413         for (XMLSharedNodeList::iterator i = note_name_lists->begin();
414              i != note_name_lists->end();
415              ++i) {
416                 boost::shared_ptr<NoteNameList> note_name_list(new NoteNameList());
417                 note_name_list->set_state (tree, *(*i));
418                 _note_name_lists.push_back(note_name_list);
419         }
420
421         // global/post-facto PatchNameLists
422         boost::shared_ptr<XMLSharedNodeList> patch_name_lists = tree.find("/child::MIDINameDocument/child::MasterDeviceNames/child::PatchNameList");
423         for (XMLSharedNodeList::iterator i = patch_name_lists->begin();
424              i != patch_name_lists->end();
425              ++i) {
426
427                 PatchBank::PatchNameList patch_name_list;
428                 const XMLNodeList patches = (*i)->children();
429
430                 for (XMLNodeList::const_iterator p = patches.begin(); p != patches.end(); ++p) {
431                         boost::shared_ptr<Patch> patch(new Patch());
432                         patch->set_state(tree, *(*p));
433                         patch_name_list.push_back(patch);
434                 }
435
436                 if (!patch_name_list.empty()) {
437                         _patch_name_lists[(*i)->property ("Name")->value()] = patch_name_list;
438                 }
439         }
440
441         /* now traverse patches and hook up anything that used UsePatchNameList
442          * to the right patch list
443          */
444
445         for (ChannelNameSets::iterator cns = _channel_name_sets.begin(); cns != _channel_name_sets.end(); ++cns) {
446                 ChannelNameSet::PatchBanks pbs = cns->second->patch_banks();
447                 for (ChannelNameSet::PatchBanks::iterator pb = pbs.begin(); pb != pbs.end(); ++pb) {
448                         std::string pln = (*pb)->patch_list_name();
449                         if (!pln.empty()) {
450                                 PatchNameLists::iterator p = _patch_name_lists.find (pln);
451                                 if (p != _patch_name_lists.end()) {
452                                         if ((*pb)->set_patch_name_list (p->second)) {
453                                                 return -1;
454                                         }
455                                 } else {
456                                         error << string_compose ("Patch list name %1 was not found - patch file ignored", pln) << endmsg;
457                                         return -1;
458                                 }
459                         }
460                 }
461         }
462
463         return 0;
464 }
465
466 XMLNode&
467 MasterDeviceNames::get_state(void)
468 {
469         static XMLNode nothing("<nothing>");
470         return nothing;
471 }
472
473 MIDINameDocument::MIDINameDocument (const string& filename)
474 {
475         if (!_document.read (filename)) {
476                 throw failed_constructor ();
477         }
478         
479         set_state (_document, *_document.root());
480 }
481
482 int
483 MIDINameDocument::set_state (const XMLTree& tree, const XMLNode& a_node)
484 {
485         // Author
486
487         boost::shared_ptr<XMLSharedNodeList> author = tree.find("//Author");
488         if (author->size() < 1) {
489                 error << "No author information in MIDNAM file" << endmsg;
490                 return -1;
491         }
492         _author = author->front()->content();
493
494         // MasterDeviceNames
495
496         boost::shared_ptr<XMLSharedNodeList> master_device_names_list = tree.find ("//MasterDeviceNames");
497
498         for (XMLSharedNodeList::iterator i = master_device_names_list->begin();
499              i != master_device_names_list->end();
500              ++i) {
501                 boost::shared_ptr<MasterDeviceNames> master_device_names(new MasterDeviceNames());
502
503                 if (master_device_names->set_state(tree, *(*i))) {
504                         return -1;
505                 }
506
507                 for (MasterDeviceNames::Models::const_iterator model = master_device_names->models().begin();
508                      model != master_device_names->models().end();
509                      ++model) {
510                         _master_device_names_list.insert(
511                                 std::pair<std::string, boost::shared_ptr<MasterDeviceNames> >
512                                 (*model,      master_device_names));
513                         
514                         _all_models.push_back(*model);
515                 }
516         }
517
518         return 0;
519 }
520
521 XMLNode&
522 MIDINameDocument::get_state(void)
523 {
524         static XMLNode nothing("<nothing>");
525         return nothing;
526 }
527
528
529 } //namespace Name
530
531 } //namespace MIDI
532