summaryrefslogtreecommitdiff
path: root/libs/midi++2/midnam_patch.cc
blob: dbde6e240c41911958c71729c1cd0c59625d797d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
#include "midi++/midnam_patch.h"
#include <algorithm>

namespace MIDI
{

namespace Name
{

XMLNode&
Patch::get_state (void)
{
	XMLNode* node = new XMLNode("Patch");
	node->add_property("Number", _number);
	node->add_property("Name",   _name);
	XMLNode* commands = node->add_child("PatchMIDICommands");
	for (PatchMidiCommands::const_iterator event = _patch_midi_commands.begin();
	    event != _patch_midi_commands.end();
	    ++event) {
		commands->add_child_copy(*((((Evoral::MIDIEvent&)*event)).to_xml()));
	}

	return *node;
}

int
Patch::set_state (const XMLNode& node)
{
	assert(node.name() == "Patch");
	_number = node.property("Number")->value();
	_name   = node.property("Name")->value();
	XMLNode* commands = node.child("PatchMIDICommands");
	assert(commands);
	const XMLNodeList events = commands->children();
	for (XMLNodeList::const_iterator i = events.begin(); i != events.end(); ++i) {
		_patch_midi_commands.push_back(*(new Evoral::MIDIEvent(*(*i))));
	}

	return 0;
}

XMLNode&
Note::get_state (void)
{
	XMLNode* node = new XMLNode("Patch");
	node->add_property("Number", _number);
	node->add_property("Name",   _name);

	return *node;
}

int
Note::set_state (const XMLNode& node)
{
	assert(node.name() == "Patch");
	_number = node.property("Number")->value();
	_name   = node.property("Name")->value();

	return 0;
}

XMLNode&
PatchBank::get_state (void)
{
	XMLNode* node = new XMLNode("PatchBank");
	node->add_property("Name",   _name);
	XMLNode* patch_name_list = node->add_child("PatchNameList");
	for (PatchNameList::iterator patch = _patch_name_list.begin();
	    patch != _patch_name_list.end();
	    ++patch) {
		patch_name_list->add_child_nocopy(patch->get_state());
	}

	return *node;
}

int
PatchBank::set_state (const XMLNode& node)
{
	assert(node.name() == "PatchBank");
	_name   = node.property("Name")->value();
	XMLNode* patch_name_list = node.child("PatchNameList");
	assert(patch_name_list);
	const XMLNodeList patches = patch_name_list->children();
	for (XMLNodeList::const_iterator i = patches.begin(); i != patches.end(); ++i) {
		Patch patch;
		patch.set_state(*(*i));
		_patch_name_list.push_back(patch);
	}

	return 0;
}

XMLNode&
ChannelNameSet::get_state (void)
{
	XMLNode* node = new XMLNode("ChannelNameSet");
	node->add_property("Name",   _name);

	XMLNode* available_for_channels = node->add_child("AvailableForChannels");
	assert(available_for_channels);

	for (uint8_t channel = 0; channel < 16; ++channel) {
		XMLNode* available_channel = available_for_channels->add_child("AvailableChannel");
		assert(available_channel);

		available_channel->add_property("Channel", (long) channel);

		if (_available_for_channels.find(channel) != _available_for_channels.end()) {
			available_channel->add_property("Available", "true");
		} else {
			available_channel->add_property("Available", "false");
		}
	}

	for (PatchBanks::iterator patch_bank = _patch_banks.begin();
	    patch_bank != _patch_banks.end();
	    ++patch_bank) {
		node->add_child_nocopy(patch_bank->get_state());
	}

	return *node;
}

int
ChannelNameSet::set_state (const XMLNode& node)
{
	assert(node.name() == "ChannelNameSet");
	_name   = node.property("Name")->value();
	const XMLNodeList children = node.children();
	for (XMLNodeList::const_iterator i = children.begin(); i != children.end(); ++i) {
		XMLNode* node = *i;
		assert(node);
		if (node->name() == "AvailableForChannels") {
			boost::shared_ptr<XMLSharedNodeList> channels =
				node->find("//AvailableChannel[@Available = 'true']/@Channel");
			for(XMLSharedNodeList::const_iterator i = channels->begin();
			    i != channels->end();
			    ++i) {
				_available_for_channels.insert(atoi((*i)->attribute_value().c_str()));
			}
		}

		if (node->name() == "PatchBank") {
			PatchBank bank;
			bank.set_state(*node);
			_patch_banks.push_back(bank);
		}
	}

	return 0;
}

int
CustomDeviceMode::set_state(const XMLNode& a_node)
{
	assert(a_node.name() == "CustomDeviceNode");
	boost::shared_ptr<XMLSharedNodeList> channel_name_set_assignments =
		a_node.find("//ChannelNameSetAssign");
	for(XMLSharedNodeList::const_iterator i = channel_name_set_assignments->begin();
	    i != channel_name_set_assignments->end();
	    ++i) {
		int channel = atoi((*i)->property("Channel")->value().c_str());
		string name_set = (*i)->property("NameSet")->value();
		assert( 1 <= channel && channel <= 16 );
		_channel_name_set_assignments[channel -1] = name_set;
	}
	return 0;
}

XMLNode&
CustomDeviceMode::get_state(void)
{
	XMLNode* custom_device_mode = new XMLNode("CustomDeviceMode");
	custom_device_mode->add_property("Name",   _name);
	XMLNode* channel_name_set_assignments = 
		custom_device_mode->add_child("ChannelNameSetAssignments");
	for (int i = 0; i < 15 && !_channel_name_set_assignments[i].empty(); i++) {
		XMLNode* channel_name_set_assign = 
			channel_name_set_assignments->add_child("ChannelNameSetAssign");
		channel_name_set_assign->add_property("Channel", i + 1);
		channel_name_set_assign->add_property("NameSet", _channel_name_set_assignments[i]);
	}
	
	return *custom_device_mode;
}

int
MasterDeviceNames::set_state(const XMLNode& a_node)
{
	return 0;
}

XMLNode&
MasterDeviceNames::get_state(void)
{
	static XMLNode nothing("<nothing>");
	return nothing;
}

int
MIDINameDocument::set_state(const XMLNode& a_node)
{
	return 0;
}

XMLNode&
MIDINameDocument::get_state(void)
{
	static XMLNode nothing("<nothing>");
	return nothing;
}


} //namespace Name

} //namespace MIDI