2 Copyright (C) 2008 Hans Baier
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.
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.
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.
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"
39 Patch::Patch (PatchBank* b)
44 Patch::Patch (std::string a_number, std::string a_name, PatchBank* a_bank)
48 use_bank_info (a_bank);
52 Patch::get_state (void)
54 XMLNode* node = new XMLNode("Patch");
55 node->add_property("Number", _number);
56 node->add_property("Name", _name);
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();
63 commands->add_child_copy(*((((Evoral::MIDIEvent&)*event)).to_xml()));
70 int initialize_primary_key_from_commands (PatchPrimaryKey& id, const XMLNode* node)
72 const XMLNodeList events = node->children();
73 for (XMLNodeList::const_iterator i = events.begin(); i != events.end(); ++i) {
75 if (node->name() == "ControlChange") {
76 string control = node->property("Control")->value();
77 assert(control != "");
78 string value = node->property("Value")->value();
82 id.msb = PBD::atoi(value);
83 } else if (control == "32") {
84 id.lsb = PBD::atoi(value);
86 } else if (node->name() == "ProgramChange") {
87 string number = node->property("Number")->value();
89 id.program_number = PBD::atoi(number);
97 Patch::set_state (const XMLTree&, const XMLNode& node)
99 if (node.name() != "Patch") {
100 cerr << "Incorrect node " << node.name() << " handed to Patch" << endl;
104 const XMLProperty* prop = node.property ("Number");
109 _number = prop->value();
111 prop = node.property ("Name");
116 _name = prop->value();
118 XMLNode* commands = node.child("PatchMIDICommands");
121 if (initialize_primary_key_from_commands(_id, commands)) {
125 string program_change = node.property("ProgramChange")->value();
126 assert(program_change.length());
127 _id.program_number = PBD::atoi(program_change);
134 Patch::use_bank_info (PatchBank* bank)
137 if (bank->patch_primary_key() ) {
138 _id.msb = bank->patch_primary_key()->msb;
139 _id.lsb = bank->patch_primary_key()->lsb;
145 if (!_id.is_sane()) {
153 Note::get_state (void)
155 XMLNode* node = new XMLNode("Note");
156 node->add_property("Number", _number);
157 node->add_property("Name", _name);
163 Note::set_state (const XMLTree&, const XMLNode& node)
165 assert(node.name() == "Note");
166 _number = node.property("Number")->value();
167 _name = node.property("Name")->value();
173 NoteNameList::get_state (void)
175 XMLNode* node = new XMLNode("NoteNameList");
176 node->add_property("Name", _name);
182 NoteNameList::set_state (const XMLTree& tree, const XMLNode& node)
184 assert(node.name() == "NoteNameList");
185 _name = node.property("Name")->value();
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);
199 PatchBank::get_state (void)
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();
207 patch_name_list->add_child_nocopy((*patch)->get_state());
214 PatchBank::set_state (const XMLTree& tree, const XMLNode& node)
216 assert(node.name() == "PatchBank");
217 _name = node.property("Name")->value();
219 XMLNode* commands = node.child("MIDICommands");
221 _id = new PatchPrimaryKey();
222 if (initialize_primary_key_from_commands(*_id, commands)) {
227 XMLNode* patch_name_list = node.child("PatchNameList");
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);
237 XMLNode* use_patch_name_list = node.child ("UsesPatchNameList");
238 if (use_patch_name_list) {
239 _patch_list_name = node.property ("Name")->value();
241 error << "Patch without patch name list - patchfile will be ignored" << endmsg;
250 PatchBank::set_patch_name_list (const PatchNameList& pnl)
252 _patch_name_list = pnl;
253 _patch_list_name = "";
255 for (PatchNameList::iterator p = _patch_name_list.begin(); p != _patch_name_list.end(); p++) {
256 if ((*p)->use_bank_info (this)) {
265 ChannelNameSet::get_state (void)
267 XMLNode* node = new XMLNode("ChannelNameSet");
268 node->add_property("Name", _name);
270 XMLNode* available_for_channels = node->add_child("AvailableForChannels");
271 assert(available_for_channels);
273 for (uint8_t channel = 0; channel < 16; ++channel) {
274 XMLNode* available_channel = available_for_channels->add_child("AvailableChannel");
275 assert(available_channel);
277 available_channel->add_property("Channel", (long) channel);
279 if (_available_for_channels.find(channel) != _available_for_channels.end()) {
280 available_channel->add_property("Available", "true");
282 available_channel->add_property("Available", "false");
286 for (PatchBanks::iterator patch_bank = _patch_banks.begin();
287 patch_bank != _patch_banks.end();
289 node->add_child_nocopy((*patch_bank)->get_state());
296 ChannelNameSet::set_state (const XMLTree& tree, const XMLNode& node)
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) {
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();
310 _available_for_channels.insert(atoi((*i)->attribute_value().c_str()));
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();
322 _patch_map[(*patch)->patch_primary_key()] = *patch;
323 _patch_list.push_back((*patch)->patch_primary_key());
332 CustomDeviceMode::set_state(const XMLTree& tree, const XMLNode& a_node)
334 assert(a_node.name() == "CustomDeviceMode");
336 _name = a_node.property("Name")->value();
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();
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;
352 CustomDeviceMode::get_state(void)
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]);
365 return *custom_device_mode;
369 MasterDeviceNames::set_state(const XMLTree& tree, const XMLNode& a_node)
372 boost::shared_ptr<XMLSharedNodeList> manufacturer = tree.find("//Manufacturer");
373 assert(manufacturer->size() == 1);
374 _manufacturer = manufacturer->front()->content();
377 boost::shared_ptr<XMLSharedNodeList> models = tree.find("//Model");
378 assert(models->size() >= 1);
379 for (XMLSharedNodeList::iterator i = models->begin();
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());
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();
394 boost::shared_ptr<CustomDeviceMode> custom_device_mode(new CustomDeviceMode());
395 custom_device_mode->set_state(tree, *(*i));
397 _custom_device_modes[custom_device_mode->name()] = custom_device_mode;
398 _custom_device_mode_names.push_back(custom_device_mode->name());
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();
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;
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();
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);
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();
427 PatchBank::PatchNameList patch_name_list;
428 const XMLNodeList patches = (*i)->children();
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);
436 if (!patch_name_list.empty()) {
437 _patch_name_lists[(*i)->property ("Name")->value()] = patch_name_list;
441 /* now traverse patches and hook up anything that used UsePatchNameList
442 * to the right patch list
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();
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)) {
456 error << string_compose ("Patch list name %1 was not found - patch file ignored", pln) << endmsg;
467 MasterDeviceNames::get_state(void)
469 static XMLNode nothing("<nothing>");
473 MIDINameDocument::MIDINameDocument (const string& filename)
475 if (!_document.read (filename)) {
476 throw failed_constructor ();
479 set_state (_document, *_document.root());
483 MIDINameDocument::set_state (const XMLTree& tree, const XMLNode& a_node)
487 boost::shared_ptr<XMLSharedNodeList> author = tree.find("//Author");
488 if (author->size() < 1) {
489 error << "No author information in MIDNAM file" << endmsg;
492 _author = author->front()->content();
496 boost::shared_ptr<XMLSharedNodeList> master_device_names_list = tree.find ("//MasterDeviceNames");
498 for (XMLSharedNodeList::iterator i = master_device_names_list->begin();
499 i != master_device_names_list->end();
501 boost::shared_ptr<MasterDeviceNames> master_device_names(new MasterDeviceNames());
503 if (master_device_names->set_state(tree, *(*i))) {
507 for (MasterDeviceNames::Models::const_iterator model = master_device_names->models().begin();
508 model != master_device_names->models().end();
510 _master_device_names_list.insert(
511 std::pair<std::string, boost::shared_ptr<MasterDeviceNames> >
512 (*model, master_device_names));
514 _all_models.push_back(*model);
522 MIDINameDocument::get_state(void)
524 static XMLNode nothing("<nothing>");