* Added MIDI::Name::Note
[ardour.git] / libs / midi++2 / midnam_patch.cc
1 #include "midi++/midnam_patch.h"
2 #include <algorithm>
3
4 namespace MIDI
5 {
6
7 namespace Name
8 {
9
10 XMLNode&
11 Patch::get_state (void)
12 {
13         XMLNode* node = new XMLNode("Patch");
14         node->add_property("Number", _number);
15         node->add_property("Name",   _name);
16         XMLNode* commands = node->add_child("PatchMIDICommands");
17         for (PatchMidiCommands::const_iterator event = _patch_midi_commands.begin();
18             event != _patch_midi_commands.end();
19             ++event) {
20                 commands->add_child_copy(*((((Evoral::MIDIEvent&)*event)).to_xml()));
21         }
22
23         return *node;
24 }
25
26 int
27 Patch::set_state (const XMLNode& node)
28 {
29         assert(node.name() == "Patch");
30         _number = node.property("Number")->value();
31         _name   = node.property("Name")->value();
32         XMLNode* commands = node.child("PatchMIDICommands");
33         assert(commands);
34         const XMLNodeList events = commands->children();
35         for (XMLNodeList::const_iterator i = events.begin(); i != events.end(); ++i) {
36                 _patch_midi_commands.push_back(*(new Evoral::MIDIEvent(*(*i))));
37         }
38
39         return 0;
40 }
41
42 XMLNode&
43 Note::get_state (void)
44 {
45         XMLNode* node = new XMLNode("Patch");
46         node->add_property("Number", _number);
47         node->add_property("Name",   _name);
48
49         return *node;
50 }
51
52 int
53 Note::set_state (const XMLNode& node)
54 {
55         assert(node.name() == "Patch");
56         _number = node.property("Number")->value();
57         _name   = node.property("Name")->value();
58
59         return 0;
60 }
61
62 XMLNode&
63 PatchBank::get_state (void)
64 {
65         XMLNode* node = new XMLNode("PatchBank");
66         node->add_property("Name",   _name);
67         XMLNode* patch_name_list = node->add_child("PatchNameList");
68         for (PatchNameList::iterator patch = _patch_name_list.begin();
69             patch != _patch_name_list.end();
70             ++patch) {
71                 patch_name_list->add_child_nocopy(patch->get_state());
72         }
73
74         return *node;
75 }
76
77 int
78 PatchBank::set_state (const XMLNode& node)
79 {
80         assert(node.name() == "PatchBank");
81         _name   = node.property("Name")->value();
82         XMLNode* patch_name_list = node.child("PatchNameList");
83         assert(patch_name_list);
84         const XMLNodeList patches = patch_name_list->children();
85         for (XMLNodeList::const_iterator i = patches.begin(); i != patches.end(); ++i) {
86                 Patch patch;
87                 patch.set_state(*(*i));
88                 _patch_name_list.push_back(patch);
89         }
90
91         return 0;
92 }
93
94 XMLNode&
95 ChannelNameSet::get_state (void)
96 {
97         XMLNode* node = new XMLNode("ChannelNameSet");
98         node->add_property("Name",   _name);
99
100         XMLNode* available_for_channels = node->add_child("AvailableForChannels");
101         assert(available_for_channels);
102
103         for (uint8_t channel = 0; channel < 16; ++channel) {
104                 XMLNode* available_channel = available_for_channels->add_child("AvailableChannel");
105                 assert(available_channel);
106
107                 available_channel->add_property("Channel", (long) channel);
108
109                 if (_available_for_channels.find(channel) != _available_for_channels.end()) {
110                         available_channel->add_property("Available", "true");
111                 } else {
112                         available_channel->add_property("Available", "false");
113                 }
114         }
115
116         for (PatchBanks::iterator patch_bank = _patch_banks.begin();
117             patch_bank != _patch_banks.end();
118             ++patch_bank) {
119                 node->add_child_nocopy(patch_bank->get_state());
120         }
121
122         return *node;
123 }
124
125 int
126 ChannelNameSet::set_state (const XMLNode& node)
127 {
128         assert(node.name() == "ChannelNameSet");
129         _name   = node.property("Name")->value();
130         const XMLNodeList children = node.children();
131         for (XMLNodeList::const_iterator i = children.begin(); i != children.end(); ++i) {
132                 XMLNode* node = *i;
133                 assert(node);
134                 if (node->name() == "AvailableForChannels") {
135                         boost::shared_ptr<XMLSharedNodeList> channels =
136                                 node->find("//AvailableChannel[@Available = 'true']/@Channel");
137                         for(XMLSharedNodeList::const_iterator i = channels->begin();
138                             i != channels->end();
139                             ++i) {
140                                 _available_for_channels.insert(atoi((*i)->attribute_value().c_str()));
141                         }
142                 }
143
144                 if (node->name() == "PatchBank") {
145                         PatchBank bank;
146                         bank.set_state(*node);
147                         _patch_banks.push_back(bank);
148                 }
149         }
150
151         return 0;
152 }
153
154 int
155 CustomDeviceMode::set_state(const XMLNode& a_node)
156 {
157         assert(a_node.name() == "CustomDeviceNode");
158         boost::shared_ptr<XMLSharedNodeList> channel_name_set_assignments =
159                 a_node.find("//ChannelNameSetAssign");
160         for(XMLSharedNodeList::const_iterator i = channel_name_set_assignments->begin();
161             i != channel_name_set_assignments->end();
162             ++i) {
163                 int channel = atoi((*i)->property("Channel")->value().c_str());
164                 string name_set = (*i)->property("NameSet")->value();
165                 assert( 1 <= channel && channel <= 16 );
166                 _channel_name_set_assignments[channel -1] = name_set;
167         }
168         return 0;
169 }
170
171 XMLNode&
172 CustomDeviceMode::get_state(void)
173 {
174         XMLNode* custom_device_mode = new XMLNode("CustomDeviceMode");
175         custom_device_mode->add_property("Name",   _name);
176         XMLNode* channel_name_set_assignments = 
177                 custom_device_mode->add_child("ChannelNameSetAssignments");
178         for (int i = 0; i < 15 && !_channel_name_set_assignments[i].empty(); i++) {
179                 XMLNode* channel_name_set_assign = 
180                         channel_name_set_assignments->add_child("ChannelNameSetAssign");
181                 channel_name_set_assign->add_property("Channel", i + 1);
182                 channel_name_set_assign->add_property("NameSet", _channel_name_set_assignments[i]);
183         }
184         
185         return *custom_device_mode;
186 }
187
188 int
189 MasterDeviceNames::set_state(const XMLNode& a_node)
190 {
191         return 0;
192 }
193
194 XMLNode&
195 MasterDeviceNames::get_state(void)
196 {
197         static XMLNode nothing("<nothing>");
198         return nothing;
199 }
200
201 int
202 MIDINameDocument::set_state(const XMLNode& a_node)
203 {
204         return 0;
205 }
206
207 XMLNode&
208 MIDINameDocument::get_state(void)
209 {
210         static XMLNode nothing("<nothing>");
211         return nothing;
212 }
213
214
215 } //namespace Name
216
217 } //namespace MIDI
218