summaryrefslogtreecommitdiff
path: root/src/projectserialiser.cc
blob: ac872c72eeffbf85034d50db6fc35bbe45da6958 (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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/***************************************************************************
 *            projectserialiser.cc
 *
 *  Sat May 12 10:11:22 CEST 2018
 *  Copyright 2018 Bent Bisballe Nyeng
 *  deva@aasimon.org
 ****************************************************************************/

/*
 *  This file is part of DrumGizmo.
 *
 *  DrumGizmo is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  DrumGizmo is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with DrumGizmo; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA.
 */
#include "projectserialiser.h"

#include <vector>
#include <iostream>

#include <QDomDocument>

/*
<?xml version="1.0" encoding="UTF-8"?>
<dgedit version="1.0">
	<project_name>MyProject</project_name>
	<raw_file_root>/some/path</raw_file_root>
</dgedit>
*/

extern Q_CORE_EXPORT QBasicAtomicInt qt_qhash_seed;

class DomHelper
{
public:
	DomHelper(const QDomDocument& doc) : elem(doc.documentElement()) {}
	DomHelper(const QDomElement& elem) : elem(elem) {}

	DomHelper operator()(const QString& name)
	{
		auto node = elem.namedItem(name);
		if(!node.isElement())
		{
			std::cout << "No such child-element: '" << name.toStdString() << "'\n";
			QDomElement e;
			return DomHelper(e);
		}

		return DomHelper(node.toElement());
	}

	QString operator[](const QString& name)
	{
		auto attrs = elem.attributes();
		auto node = attrs.namedItem(name);
		if(!node.isAttr())
		{
			std::cout << "No such attribute: '" << name.toStdString() << "'\n";
			return "";
		}
		return node.toAttr().value();
	}

	QString text()
	{
		return elem.text();
	}

	// Get child nodes with tag_name or all child nodes if tag_name == "".
	std::vector<DomHelper> children(const QString& tag_name = "")
	{
		std::vector<DomHelper> children;
		auto child_nodes = elem.childNodes();
		for(int i = 0; i < child_nodes.count(); ++i)
		{
			auto node = child_nodes.at(i);
			if(!node.isElement())
			{
				continue;
			}

			auto elem = node.toElement();
			if(elem.tagName() == tag_name || tag_name == "")
			{
				children.emplace_back(DomHelper(elem));
			}
		}

		return children;
	}

private:
	QDomElement elem;
};

QString ProjectSerialiser::serialise(const Project& project)
{
	// Ugly hack to ensure the xml attribute order is the same each time a save
	// or export is performed.
	qt_qhash_seed.store(0);

	QDomDocument doc;
	auto header =
		doc.createProcessingInstruction("xml", "version='1.0' encoding='UTF-8'");
	doc.appendChild(header);

	auto dgedit = doc.createElement("dgedit");
	doc.appendChild(dgedit);

	dgedit.setAttribute("version", "1.0");

	auto project_name = doc.createElement("project_name");
	project_name.appendChild(doc.createTextNode(project.project_name));
	dgedit.appendChild(project_name);

	auto raw_file_root = doc.createElement("raw_file_root");
	raw_file_root.appendChild(doc.createTextNode(project.raw_file_root));
	dgedit.appendChild(raw_file_root);

	auto export_path = doc.createElement("export_path");
	export_path.appendChild(doc.createTextNode(project.export_path));
	dgedit.appendChild(export_path);

	dgedit.setAttribute("next_id", project.next_id);

	auto channels = doc.createElement("channels");
	dgedit.appendChild(channels);

	for(const auto& c : project.channels)
	{
		auto channel = doc.createElement("channel");
		channel.setAttribute("id", c.getId());
		channel.setAttribute("name", c.getChannelName());
		channels.appendChild(channel);
	}

	auto instruments = doc.createElement("instruments");
	dgedit.appendChild(instruments);

	for(const auto& i : project.instruments)
	{
		auto instrument = doc.createElement("instrument");

		instrument.setAttribute("id", (int)i.getId());

		instrument.setAttribute("attack_length", (int)i.getAttackLength());
		instrument.setAttribute("power_spread", (int)i.getPowerSpread());
		instrument.setAttribute("minimum_size", (int)i.getMinimumSize());
		instrument.setAttribute("falloff", (int)i.getFalloff());
		instrument.setAttribute("fade_length", (int)i.getFadeLength());

		instrument.setAttribute("threshold", i.getThreshold());

		auto prefix = doc.createElement("prefix");
		prefix.appendChild(doc.createTextNode(i.getPrefix()));
		instrument.appendChild(prefix);

		instruments.appendChild(instrument);

		auto instrument_name = doc.createElement("instrument_name");
		instrument_name.appendChild(doc.createTextNode(i.instrument_name));
		instrument.appendChild(instrument_name);

		auto file_list = doc.createElement("file_list");
		instrument.appendChild(file_list);

		for(const auto& audiofile : i.audio_files)
		{
			auto file = doc.createElement("file");
			file.appendChild(doc.createTextNode(audiofile.getFile()));
			file.setAttribute("id", audiofile.getId());
			file.setAttribute("name", audiofile.getName());
			file.setAttribute("channel_map_id", audiofile.getChannelMapId());
			file.setAttribute("master", i.master_file == audiofile.getAbsoluteFile());
			file.setAttribute("main", audiofile.getMainChannel() ? "true" : "false");
			file_list.appendChild(file);
		}

		auto regions = doc.createElement("regions");
		regions.setAttribute("nextid", i.selections.nextid);
		regions.setAttribute("act", i.selections.act);
		instrument.appendChild(regions);

		for(auto r = i.selections.sels.begin(); r != i.selections.sels.end(); ++r)
		{
			auto region = doc.createElement("region");
			region.setAttribute("id", (int)r.key());
			region.setAttribute("from", (int)r.value().from);
			region.setAttribute("to", (int)r.value().to);
			region.setAttribute("fadein", (int)r.value().fadein);
			region.setAttribute("fadeout", (int)r.value().fadeout);
			region.setAttribute("energy", r.value().energy);
			region.setAttribute("name", r.value().name);
			regions.appendChild(region);
		}
	}

	return doc.toString();
}

bool ProjectSerialiser::deserialise(const QString& data, Project& project)
{
	// Ugly hack to ensure the xml attribute order is the same each time a save
	// or export is performed.
	qt_qhash_seed.store(0);

	QDomDocument doc;
	if(!doc.setContent(data))
	{
		std::cout << "Could not parse XML\n";
		return false;
	}

	project.reset();
	DomHelper dom(doc);

	QString version = dom["version"];
	if(version != "1.0")
	{
		std::cout << "Bad version: '" << version.toStdString() << "'\n";
		return false;
	}

	project.next_id = dom["next_id"].toInt();
	project.project_name = dom("project_name").text();
	project.raw_file_root = dom("raw_file_root").text();
	project.export_path = dom("export_path").text();

	auto channels = dom("channels").children("channel");
	for(auto& channel : channels)
	{
		auto id = channel["id"].toInt();
		project.channels.emplace_back(Channel(project, id));

		auto& ch = project.channels.back();
		ch.channel_name = channel["name"];
	}

	auto instruments = dom("instruments").children("instrument");
	for(auto& instrument : instruments)
	{
		auto id = instrument["id"].toInt();
		project.instruments.emplace_back(Instrument(project, id));

		auto& instr = project.instruments.back();
		instr.instrument_name = instrument("instrument_name").text();

		QString master_file;
		auto files = instrument("file_list").children("file");
		for(auto& file : files)
		{
			auto id = file["id"].toInt();
			instr.audio_files.emplace_back(AudioFile(instr, id));
			auto& audiofile = instr.audio_files.back();
			audiofile.file = file.text();
			audiofile.name = file["name"];
			audiofile.channel_map_id = file["channel_map_id"].toInt();
			audiofile.main_channel = file["main"] == "true";

			if(file["master"] == "1")
			{
				master_file = audiofile.getAbsoluteFile();
			}
		}

		instr.master_file = master_file;

		instr.attack_length = instrument["attack_length"].toInt();
		instr.power_spread = instrument["power_spread"].toInt();;
		instr.minimum_size = instrument["minimum_size"].toInt();
		instr.falloff = instrument["falloff"].toInt();
		instr.fade_length = instrument["fade_length"].toInt();

		instr.threshold = instrument["threshold"].toFloat();

		instr.prefix = instrument("prefix").text();

		auto selections = instrument("regions");
		instr.selections.nextid = selections["nextid"].toInt();
		instr.selections.act = selections["act"].toInt();

		for(auto& selection : selections.children())
		{
			Selection s;
			s.from = selection["from"].toInt();
			s.to = selection["to"].toInt();
			s.fadein = selection["fadein"].toInt();
			s.fadeout = selection["fadeout"].toInt();
			s.energy = selection["energy"].toFloat();
			s.name = selection["name"];
			instr.selections.sels[selection["id"].toInt()] = s;
		}
	}

	return true;
}