summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLars Bisballe Jensen <larsbisballe@gmail.com>2015-03-14 14:38:31 +0100
committerLars Bisballe Jensen <larsbisballe@gmail.com>2015-03-14 14:38:31 +0100
commit0936f0e39b1775c1a9df1f5dc3433ebe58b669ef (patch)
tree02a7889b8d19f6b2eea041e02c06725d8ac76763
parentb74eca322abf8d8f6c0eaa4f016f4f9d86c66193 (diff)
Imported new parser, still broken, a bit confused about id and name
-rw-r--r--src/drumkitparser.cc174
-rw-r--r--src/drumkitparser.h28
2 files changed, 199 insertions, 3 deletions
diff --git a/src/drumkitparser.cc b/src/drumkitparser.cc
index 2c21c52..58bbbcf 100644
--- a/src/drumkitparser.cc
+++ b/src/drumkitparser.cc
@@ -53,9 +53,84 @@ DrumKitParser::~DrumKitParser()
if(fd) fclose(fd);
}
+void DrumKitParser::characterData(std::string &data)
+{
+ this->data += data;
+}
+
void DrumKitParser::startTag(std::string name,
std::map<std::string, std::string> attr)
{
+ data = "";
+
+ if(name == "drumkit") {
+ if(attr.find("version") != attr.end()) {
+ try {
+ kit._version = VersionStr(attr["version"]);
+ } catch(const char *err) {
+ ERR(kitparser, "Error parsing version number: %s, using 1.0\n", err);
+ kit._version = VersionStr(1,0,0);
+ }
+ meta.version = attr["version"];
+ } else {
+ WARN(kitparser, "Missing version number, assuming 1.0\n");
+ kit._version = VersionStr(1,0,0);
+ meta.version = "Version not found";
+ }
+ }
+
+ if(name == "metadata") {
+ in_metadata = true;
+ }
+
+ if(name == "channels") {}
+
+ if(name == "channel") {
+ if(attr.find("name") == attr.end()) {
+ DEBUG(kitparser, "Missing channel name.\n");
+ return;
+ }
+ ch_name = attr["name"];
+ in_channel = true;
+ }
+
+ if(name == "instruments") {}
+
+ if(name == "instrument") {
+ if(attr.find("name") == attr.end()) {
+ DEBUG(kitparser, "Missing id in instrument tag.\n");
+ return;
+ }
+ if(attr.find("file") == attr.end()) {
+ DEBUG(kitparser, "Missing file in instrument tag.\n");
+ return;
+ }
+
+ instr_id = attr["name"];
+ instr_file = attr["file"];
+ if(attr.find("group") != attr.end()) instr_group = attr["group"];
+ else instr_group = "";
+
+ in_instrument = true;
+ }
+
+ if(in_instrument) {
+ if(name == "channelmap") {
+ if(attr.find("in") == attr.end()) {
+ DEBUG(kitparser, "Missing 'in' in channelmap tag.\n");
+ return;
+ }
+
+ if(attr.find("out") == attr.end()) {
+ DEBUG(kitparser, "Missing 'out' in channelmap tag.\n");
+ return;
+ }
+
+ channelmap[attr["in"]] = attr["out"];
+ }
+ }
+
+ /*
if(name == "drumkit") {
if(attr.find("name") != attr.end())
kit._name = attr["name"];
@@ -128,11 +203,68 @@ void DrumKitParser::startTag(std::string name,
channelmap[attr["in"]] = attr["out"];
}
+ */
+
}
void DrumKitParser::endTag(std::string name)
{
+ if(in_metadata) {
+ if(name == "title") {
+ meta.name = data;
+ }
+ if(name == "description") {
+ meta.description = data;
+ }
+ if(name == "notes") {
+ meta.notes = data;
+ }
+ if(name == "author") {
+ meta.author = data;
+ }
+ if(name == "email") {
+ meta.email = data;
+ }
+ if(name == "website") {
+ meta.website = data;
+ }
+ }
+
+ if(name == "metadata") {
+ in_metadata = false;
+ }
+
+ if(in_channel) {
+ if(name == "title") {
+ ch_name = data;
+ }
+ if(name == "microphone") {
+ ch_microphone = data;
+ }
+ }
+
+ if(name == "channel") {
+ Channel c(ch_name);
+ c.num = kit.channels.size();
+ kit.channels.push_back(c);
+
+ meta.channels.push_back(std::pair<std::string, std::string>(ch_name, ch_microphone));
+
+ in_channel = false;
+ }
+
+ if(in_instrument) {
+ if(name == "title") {
+ instr_name = data;
+ }
+
+ if(name == "description") {
+ instr_description = data;
+ }
+ }
+
if(name == "instrument") {
+
Instrument *i = new Instrument();
i->setGroup(instr_group);
// Instrument &i = kit.instruments[kit.instruments.size() - 1];
@@ -153,19 +285,57 @@ void DrumKitParser::endTag(std::string name)
}
if(c->num == NO_CHANNEL) {
DEBUG(kitparser, "Missing channel '%s' in instrument '%s'\n",
- c->name.c_str(), i->name().c_str());
+ c->name.c_str(), i->name().c_str());
} else {
/*
DEBUG(kitparser, "Assigned channel '%s' to number %d in instrument '%s'\n",
- c->name.c_str(), c->num, i.name().c_str());
+ c->name.c_str(), c->num, i.name().c_str());
*/
}
ic++;
}
+ meta.instruments.push_back(std::pair<std::string, std::string>(instr_name, instr_description));
+
+ channelmap.clear();
+ in_instrument = false;
+ }
+
+ // reset element data string
+ data = "";
+
+ /*
+ if(name == "instrument") {
+ Instrument *i = new Instrument();
+ i->setGroup(instr_group);
+ // Instrument &i = kit.instruments[kit.instruments.size() - 1];
+ InstrumentParser parser(path + "/" + instr_file, *i);
+ parser.parse();
+ kit.instruments.push_back(i);
+
+ // Assign kit channel numbers to instruments channels.
+ std::vector<InstrumentChannel*>::iterator ic = parser.channellist.begin();
+ while(ic != parser.channellist.end()) {
+ InstrumentChannel *c = *ic;
+
+ std::string cname = c->name;
+ if(channelmap.find(cname) != channelmap.end()) cname = channelmap[cname];
+
+ for(size_t cnt = 0; cnt < kit.channels.size(); cnt++) {
+ if(kit.channels[cnt].name == cname) c->num = kit.channels[cnt].num;
+ }
+ if(c->num == NO_CHANNEL) {
+ DEBUG(kitparser, "Missing channel '%s' in instrument '%s'\n",
+ c->name.c_str(), i->name().c_str());
+ } else {
+ }
+ ic++;
+ }
+
channelmap.clear();
}
+*/
}
int DrumKitParser::readData(char *data, size_t size)
diff --git a/src/drumkitparser.h b/src/drumkitparser.h
index 907b09d..8237cd5 100644
--- a/src/drumkitparser.h
+++ b/src/drumkitparser.h
@@ -30,28 +30,54 @@
#include "saxparser.h"
#include "drumkit.h"
+struct MetaData {
+ std::string version;
+ std::string title;
+ std::string description;
+ std::string notes;
+ std::string author;
+ std::string email;
+ std::string website;
+ std::vector<std::pair< std::string, std::string> > channels; // title, microphone
+ std::vector<std::pair< std::string, std::string> > instruments; // title, microphone
+};
+
class DrumKitParser : public SAXParser {
public:
DrumKitParser(const std::string &kitfile, DrumKit &kit);
~DrumKitParser();
+ void characterData(std::string &data);
void startTag(std::string name,
std::map< std::string, std::string> attributes);
void endTag(std::string name);
+ MetaData getMetaData();
protected:
int readData(char *data, size_t size);
private:
+ std::string data;
+
FILE *fd;
DrumKit &kit;
+
+ std::string ch_name;
+ std::string ch_title;
+ std::string ch_microphone;
+
// Instrument *instr;
std::string path;
std::map<std::string, std::string> channelmap;
- std::string instr_file;
std::string instr_name;
+ std::string instr_description;
+ std::string instr_file;
+ std::string instr_title;
std::string instr_group;
+ bool in_metadata;
+ bool in_channel;
+ bool in_instrument;
};
#endif/*__DRUMGIZMO_DRUMKITPARSER_H__*/