diff options
author | Bent Bisballe Nyeng <deva@aasimon.org> | 2016-03-23 21:57:41 +0100 |
---|---|---|
committer | Bent Bisballe Nyeng <deva@aasimon.org> | 2016-03-29 22:43:32 +0200 |
commit | 6ac5946767ba41d7eff9eb8521519007fdc58750 (patch) | |
tree | 3ea70169dfe7568fa97c54c19a6f2de5189ef249 /src | |
parent | ef1d7e4478649296ccb17900acc949a604097d66 (diff) |
More cleanup.
Diffstat (limited to 'src')
-rw-r--r-- | src/drumkit.cc | 53 | ||||
-rw-r--r-- | src/drumkit.h | 43 | ||||
-rw-r--r-- | src/drumkitparser.cc | 38 | ||||
-rw-r--r-- | src/instrumentparser.cc | 62 | ||||
-rw-r--r-- | src/instrumentparser.h | 2 | ||||
-rw-r--r-- | src/midimapparser.cc | 3 | ||||
-rw-r--r-- | src/midimapparser.h | 2 | ||||
-rw-r--r-- | src/path.cc | 24 | ||||
-rw-r--r-- | src/path.h | 8 | ||||
-rw-r--r-- | src/sample.cc | 67 | ||||
-rw-r--r-- | src/sample.h | 36 | ||||
-rw-r--r-- | src/saxparser.cc | 66 | ||||
-rw-r--r-- | src/saxparser.h | 19 |
13 files changed, 194 insertions, 229 deletions
diff --git a/src/drumkit.cc b/src/drumkit.cc index d8596c7..e41bd49 100644 --- a/src/drumkit.cc +++ b/src/drumkit.cc @@ -28,69 +28,52 @@ DrumKit::DrumKit() { - magic = this; + magic = this; } DrumKit::~DrumKit() { - magic = NULL; - clear(); + magic = NULL; + clear(); } void DrumKit::clear() { - Instruments::iterator i = instruments.begin(); - while(i != instruments.end()) { - delete *i; - i++; - } - instruments.clear(); + for(auto& instrument : instruments) + { + delete instrument; + } - channels.clear(); + instruments.clear(); - _name = ""; - _description = ""; - _samplerate = 44100; + channels.clear(); + + _name = ""; + _description = ""; + _samplerate = 44100; } bool DrumKit::isValid() { - return this == magic; + return this == magic; } std::string DrumKit::file() { - return _file; + return _file; } std::string DrumKit::name() { - return _name; + return _name; } std::string DrumKit::description() { - return _description; + return _description; } size_t DrumKit::samplerate() { - return _samplerate; + return _samplerate; } - -#ifdef TEST_DRUMKIT -//Additional dependency files -//deps: -//Required cflags (autoconf vars may be used) -//cflags: -//Required link options (autoconf vars may be used) -//libs: -#include "test.h" - -TEST_BEGIN; - -// TODO: Put some testcode here (see test.h for usable macros). - -TEST_END; - -#endif/*TEST_DRUMKIT*/ diff --git a/src/drumkit.h b/src/drumkit.h index 24fce99..26f2945 100644 --- a/src/drumkit.h +++ b/src/drumkit.h @@ -24,8 +24,7 @@ * along with DrumGizmo; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. */ -#ifndef __DRUMGIZMO_DRUMKIT_H__ -#define __DRUMGIZMO_DRUMKIT_H__ +#pragma once #include <map> #include <string> @@ -34,37 +33,35 @@ #include "instrument.h" #include "versionstr.h" -class DrumKitParser; -class DrumKit { - friend class DrumKitParser; +class DrumKit +{ + friend class DrumKitParser; public: - DrumKit(); - ~DrumKit(); + DrumKit(); + ~DrumKit(); - std::string file(); + std::string file(); - std::string name(); - std::string description(); - - Instruments instruments; - Channels channels; - - void clear(); + std::string name(); + std::string description(); - bool isValid(); + Instruments instruments; + Channels channels; - size_t samplerate(); + void clear(); + + bool isValid(); + + size_t samplerate(); private: void *magic{nullptr}; - std::string _file; + std::string _file; - std::string _name; - std::string _description; + std::string _name; + std::string _description; size_t _samplerate{0}; - VersionStr _version; + VersionStr _version; }; - -#endif/*__DRUMGIZMO_DRUMKIT_H__*/ diff --git a/src/drumkitparser.cc b/src/drumkitparser.cc index 595cd41..bb51a75 100644 --- a/src/drumkitparser.cc +++ b/src/drumkitparser.cc @@ -36,7 +36,7 @@ DrumKitParser::DrumKitParser(DrumKit& kit) : kit(kit) - , refs(REFSFILE) + , refs(REFSFILE) { } @@ -46,9 +46,9 @@ int DrumKitParser::parseFile(const std::string& filename) if(refs.load()) { - if(filename.size() > 1 && filename[0] == '@') + if((filename.size() > 1) && (filename[0] == '@')) { - edited_filename = refs.getValue(filename.substr(1)); + edited_filename = refs.getValue(filename.substr(1)); } } else @@ -59,7 +59,8 @@ int DrumKitParser::parseFile(const std::string& filename) path = getPath(edited_filename); auto result = SAXParser::parseFile(filename); - if (result == 0) { + if(result == 0) + { kit._file = edited_filename; } @@ -101,7 +102,7 @@ void DrumKitParser::startTag(const std::string& name, const attr_t& attr) { ERR(kitparser, "Error parsing version number: %s, using 1.0\n", err); kit._version = VersionStr(1,0,0); - } + } } else { @@ -112,7 +113,7 @@ void DrumKitParser::startTag(const std::string& name, const attr_t& attr) if(name == "channels") { - + } if(name == "channel") @@ -140,6 +141,7 @@ void DrumKitParser::startTag(const std::string& name, const attr_t& attr) ERR(kitparser, "Missing name in instrument tag.\n"); return; } + if(attr.find("file") == attr.end()) { ERR(kitparser, "Missing file in instrument tag.\n"); @@ -180,12 +182,14 @@ void DrumKitParser::endTag(const std::string& name) { if(name == "instrument") { - Instrument* i = new Instrument(); - i->setGroup(instr_group); - // Instrument &i = kit.instruments[kit.instruments.size() - 1]; - InstrumentParser parser(*i); + Instrument* instrument = new Instrument(); + instrument->setGroup(instr_group); + + InstrumentParser parser(*instrument); parser.parseFile(path + "/" + instr_file); - kit.instruments.push_back(i); + + // Transfer ownership to the DrumKit object. + kit.instruments.push_back(instrument); // Assign kit channel numbers to instruments channels. std::vector<InstrumentChannel*>::iterator ic = parser.channellist.begin(); @@ -206,16 +210,18 @@ void DrumKitParser::endTag(const std::string& name) c->num = kit.channels[cnt].num; } } + if(c->num == NO_CHANNEL) { ERR(kitparser, "Missing channel '%s' in instrument '%s'\n", - c->name.c_str(), i->name().c_str()); + c->name.c_str(), instrument->name().c_str()); } - else { + else + { /* - DEBUG(kitparser, "Assigned channel '%s' to number %d in instrument '%s'\n", - c->name.c_str(), c->num, i.name().c_str()); - */ + DEBUG(kitparser, "Assigned channel '%s' to number %d in instrument '%s'\n", + c->name.c_str(), c->num, i.name().c_str()); + */ } ic++; } diff --git a/src/instrumentparser.cc b/src/instrumentparser.cc index 268f8f3..1e42cc3 100644 --- a/src/instrumentparser.cc +++ b/src/instrumentparser.cc @@ -35,8 +35,8 @@ #include "nolocale.h" -InstrumentParser::InstrumentParser(Instrument& i) - : instrument(i) +InstrumentParser::InstrumentParser(Instrument& instrument) + : instrument(instrument) { } @@ -64,14 +64,15 @@ void InstrumentParser::startTag(const std::string& name, const attr_t& attr) if(attr.find("version") != attr.end()) { - try { + try + { instrument.version = VersionStr(attr.at("version")); } - catch(const char *err) + catch(const char* err) { ERR(instrparser, "Error parsing version number: %s, using 1.0\n", err); instrument.version = VersionStr(1,0,0); - } + } } else { @@ -82,7 +83,6 @@ void InstrumentParser::startTag(const std::string& name, const attr_t& attr) if(name == "samples") { - } if(name == "sample") @@ -93,10 +93,10 @@ void InstrumentParser::startTag(const std::string& name, const attr_t& attr) return; } - float power; + float power; if(attr.find("power") == attr.end()) { - power = -1; + power = -1; } else { @@ -105,12 +105,12 @@ void InstrumentParser::startTag(const std::string& name, const attr_t& attr) } // TODO get rid of new or delete it properly - s = new Sample(attr.at("name"), power); + sample = new Sample(attr.at("name"), power); } if(name == "audiofile") { - if(s == nullptr) + if(sample == nullptr) { ERR(instrparser,"Missing Sample!\n"); return; @@ -139,18 +139,24 @@ void InstrumentParser::startTag(const std::string& name, const attr_t& attr) } } - filechannel = filechannel - 1; // 1-based in file, but zero-based internally - // TODO do those next two lines correspond with proper deletes? If not fix it. - AudioFile *af = new AudioFile(path + "/" + attr.at("file"), filechannel); - InstrumentChannel *ch = new InstrumentChannel(attr.at("channel")); - channellist.push_back(ch); - s->addAudioFile(ch, af); - instrument.audiofiles.push_back(af); + filechannel = filechannel - 1; // 1-based in file but zero-based internally. + + AudioFile *audio_file = + new AudioFile(path + "/" + attr.at("file"), filechannel); + + // TODO: This is not deleted anywhere... + InstrumentChannel *instrument_channel = + new InstrumentChannel(attr.at("channel")); + + channellist.push_back(instrument_channel); + sample->addAudioFile(instrument_channel, audio_file); + + // Transfer audio_file ownership to the instrument. + instrument.audiofiles.push_back(audio_file); } if(name == "velocities") { - } if(name == "velocity") @@ -179,19 +185,17 @@ void InstrumentParser::startTag(const std::string& name, const attr_t& attr) return; } - Sample* sample = nullptr; - std::vector<Sample *>::iterator i = instrument.samplelist.begin(); - while(i != instrument.samplelist.end()) + Sample* sample_ref = nullptr; + for(auto& sample : instrument.samplelist) { - if((*i)->name == attr.at("name")) + if(sample->name == attr.at("name")) { - sample = *i; + sample_ref = sample; break; } - i++; } - if(sample == nullptr) + if(sample_ref == nullptr) { ERR(instrparser,"Samplref pointed at non-existing sample.\n"); return; @@ -200,7 +204,7 @@ void InstrumentParser::startTag(const std::string& name, const attr_t& attr) if(instrument.version == VersionStr("1.0")) { // Old "velocity group" algorithm needs this - instrument.addSample(lower, upper, sample); + instrument.addSample(lower, upper, sample_ref); } } } @@ -209,15 +213,15 @@ void InstrumentParser::endTag(const std::string& name) { if(name == "sample") { - if(s == nullptr) + if(sample == nullptr) { ERR(instrparser,"Missing Sample.\n"); return; } - instrument.samplelist.push_back(s); + instrument.samplelist.push_back(sample); - s = nullptr; + sample = nullptr; } if(name == "instrument") { diff --git a/src/instrumentparser.h b/src/instrumentparser.h index 965694a..6cbaf8a 100644 --- a/src/instrumentparser.h +++ b/src/instrumentparser.h @@ -47,7 +47,7 @@ protected: private: Instrument& instrument; - Sample* s{nullptr}; + Sample* sample{nullptr}; std::string path; diff --git a/src/midimapparser.cc b/src/midimapparser.cc index ec4c10d..cc97280 100644 --- a/src/midimapparser.cc +++ b/src/midimapparser.cc @@ -30,7 +30,8 @@ void MidiMapParser::startTag(const std::string& name, const attr_t& attr) { if(name == "map") { - if(attr.find("note") != attr.end() && attr.find("instr") != attr.end()) + if((attr.find("note") != attr.end()) && + (attr.find("instr") != attr.end())) { midimap[std::stoi(attr.at("note"))] = attr.at("instr"); } diff --git a/src/midimapparser.h b/src/midimapparser.h index 740cb60..8ec76c0 100644 --- a/src/midimapparser.h +++ b/src/midimapparser.h @@ -26,8 +26,6 @@ */ #pragma once -#include <stdio.h> - #include "saxparser.h" #include "midimapper.h" diff --git a/src/path.cc b/src/path.cc index 5c899f2..c2e7910 100644 --- a/src/path.cc +++ b/src/path.cc @@ -33,19 +33,21 @@ #include <string.h> #include <stdlib.h> -std::string getPath(std::string file) +std::string getPath(const std::string& file) { - std::string p; -#ifndef __MINGW32__ - char *b = strdup(file.c_str()); - p = dirname(b); - free(b); + std::string path; + +#ifdef __MINGW32__ + char drive[_MAX_DRIVE]; + char dir[_MAX_DIR]; + _splitpath(file.c_str(), drive, dir, NULL, NULL); + path = std::string(drive) + dir; #else - char drive[_MAX_DRIVE]; - char dir[_MAX_DIR]; - _splitpath(file.c_str(), drive, dir, NULL, NULL); - p = std::string(drive) + dir; + // POSIX + char* buffer = strdup(file.c_str()); + path = dirname(buffer); + free(buffer); #endif - return p; + return path; } @@ -24,11 +24,9 @@ * along with DrumGizmo; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. */ -#ifndef __DRUMGIZMO_PATH_H__ -#define __DRUMGIZMO_PATH_H__ +#pragma once #include <string> -std::string getPath(std::string file); - -#endif/*__DRUMGIZMO_PATH_H__*/ +//! \returns path component of full filename with path. +std::string getPath(const std::string& file); diff --git a/src/sample.cc b/src/sample.cc index 27382af..22bee14 100644 --- a/src/sample.cc +++ b/src/sample.cc @@ -26,60 +26,37 @@ */ #include "sample.h" -#include <stdlib.h> -#include <unistd.h> - -#include <sndfile.h> - -Sample::Sample(std::string name, float power) +Sample::Sample(const std::string& name, float power) + : name(name) + , power(power) { - this->name = name; - this->power = power; } Sample::~Sample() { } -void Sample::addAudioFile(Channel *c, AudioFile *a) +void Sample::addAudioFile(InstrumentChannel* instrument_channel, + AudioFile* audio_file) { - audiofiles[c] = a; + audiofiles[instrument_channel] = audio_file; } -AudioFile *Sample::getAudioFile(Channel *c) +AudioFile *Sample::getAudioFile(InstrumentChannel* instrument_channel) { - /* - if(audiofiles.find(c) == audiofiles.end()) return NULL; - return audiofiles[c]; - */ - - AudioFiles::iterator i = audiofiles.begin(); - while(i != audiofiles.end()) { - Channel *ch = i->first; - if(c->num == ch->num) return i->second; - i++; - } - - return NULL; + /* + if(audiofiles.find(c) == audiofiles.end()) return NULL; + return audiofiles[c]; + */ + + for(auto& audio_file : audiofiles) + { + InstrumentChannel *ch = audio_file.first; + if(instrument_channel->num == ch->num) + { + return audio_file.second; + } + } + + return nullptr; } - -#ifdef TEST_SAMPLE -//deps: channel.cc audiofile.cc -//cflags: $(SNDFILE_CFLAGS) -//libs: $(SNDFILE_LIBS) -#include "test.h" - -TEST_BEGIN; - -Sample s; -InstrumentChannel c; -InstrumentChannel c2; -AudioFile a("test"); - -s.addAudioFile(&c, &a); -TEST_EQUAL(s.getAudioFile(&c), &a, "?"); -TEST_EQUAL(s.getAudioFile(&c2), NULL, "?"); - -TEST_END; - -#endif/*TEST_SAMPLE*/ diff --git a/src/sample.h b/src/sample.h index 26c7be2..f00db13 100644 --- a/src/sample.h +++ b/src/sample.h @@ -24,8 +24,7 @@ * along with DrumGizmo; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. */ -#ifndef __DRUMGIZMO_SAMPLE_H__ -#define __DRUMGIZMO_SAMPLE_H__ +#pragma once #include <map> #include <string> @@ -33,33 +32,22 @@ #include "channel.h" #include "audiofile.h" -typedef std::map< Channel*, AudioFile* > AudioFiles; +using AudioFiles = std::map<Channel*, AudioFile*>; -class InstrumentParser; class Sample { - friend class InstrumentParser; - friend class PowerList; + friend class InstrumentParser; + friend class PowerList; public: - Sample(std::string name, float power); - ~Sample(); + Sample(const std::string& name, float power); + ~Sample(); - AudioFile *getAudioFile(InstrumentChannel *c); + AudioFile* getAudioFile(InstrumentChannel *instrument_channel); private: - void addAudioFile(InstrumentChannel *c, AudioFile *a); + void addAudioFile(InstrumentChannel* instrument_channel, + AudioFile* audio_file); - std::string name; - float power; - AudioFiles audiofiles; + std::string name; + float power; + AudioFiles audiofiles; }; - -/* - * <sample name="kick-r-1"> - * <audiofile channel="Alesis-3" file="samples/1-kick-r-Alesis-3.wav"/> - * <audiofile channel="Amb L-3" file="samples/1-kick-r-Amb L-3.wav"/> - * <audiofile channel="Amb R-3" file="samples/1-kick-r-Amb R-3.wav"/> - * <audiofile channel="Kick L-3" file="samples/1-kick-r-Kick L-3.wav"/> - * <audiofile channel="Kick R-3" file="samples/1-kick-r-Kick R-3.wav"/> - * </sample> - */ -#endif/*__DRUMGIZMO_SAMPLE_H__*/ diff --git a/src/saxparser.cc b/src/saxparser.cc index e32143d..280e608 100644 --- a/src/saxparser.cc +++ b/src/saxparser.cc @@ -26,29 +26,30 @@ */ #include "saxparser.h" -#include <string.h> -#include <hugin.hpp> #include <sstream> #include <iostream> +#include <hugin.hpp> + SAXParser::SAXParser() { - p = XML_ParserCreate(nullptr); - if(!p) { + parser = XML_ParserCreate(nullptr); + if(!parser) + { ERR(sax, "Couldn't allocate memory for parser\n"); // throw Exception(...); TODO return; } - XML_SetUserData(p, this); - XML_UseParserAsHandlerArg(p); - XML_SetElementHandler(p, start_hndl, end_hndl); - XML_SetCharacterDataHandler(p, character_hndl); + XML_SetUserData(parser, this); + XML_UseParserAsHandlerArg(parser); + XML_SetElementHandler(parser, SAXParser::startHandler, SAXParser::endHandler); + XML_SetCharacterDataHandler(parser, SAXParser::characterHandler); } SAXParser::~SAXParser() { - XML_ParserFree(p); + XML_ParserFree(parser); } int SAXParser::parseFile(const std::string& filename) @@ -60,7 +61,8 @@ int SAXParser::parseFile(const std::string& filename) std::ifstream file(filename, std::ifstream::in); - if(!file.is_open()) { + if(!file.is_open()) + { return 1; } @@ -68,59 +70,63 @@ int SAXParser::parseFile(const std::string& filename) ss << file.rdbuf(); std::string str = ss.str(); - parseString(str, filename); - - return 0; + return parseString(str, filename); } -int SAXParser::parseString(const std::string& str, const std::string& xml_source_name) +int SAXParser::parseString(const std::string& str, + const std::string& xml_source_name) { DEBUG(sax, "parse(buffer %d bytes)\n", (int)str.length()); - if(!XML_Parse(p, str.c_str(), str.length(), true)) { - parseError(str, XML_ErrorString(XML_GetErrorCode(p)), - xml_source_name, (int)XML_GetCurrentLineNumber(p)); + if(!XML_Parse(parser, str.c_str(), str.length(), true)) + { + parseError(str, XML_ErrorString(XML_GetErrorCode(parser)), + xml_source_name, (int)XML_GetCurrentLineNumber(parser)); return 1; } return 0; } -void SAXParser::parseError(const std::string& buf, const std::string& error, const std::string& xml_source_name, std::size_t lineno) +void SAXParser::parseError(const std::string& buf, const std::string& error, + const std::string& xml_source_name, + std::size_t lineno) { - std::cerr << "SAXParser error trying to parse from source: " << xml_source_name << "\n"; + std::cerr << "SAXParser error trying to parse from source: " << + xml_source_name << "\n"; std::cerr << "At line " << lineno << ": " << error << "\n"; std::cerr << "Buffer " << buf.size() << " bytes: \n[\n"; std::cerr << buf; std::cerr << "\n]" << std::endl; } -void SAXParser::character_hndl(void* p, const XML_Char* s, int len) +void SAXParser::characterHandler(void* parser, const XML_Char* cData, int len) { - SAXParser* parser = (SAXParser*)XML_GetUserData(p); - std::string chars(s, len); - parser->characterData(chars); + SAXParser* sax_parser = (SAXParser*)XML_GetUserData(parser); + std::string chars(cData, len); + sax_parser->characterData(chars); } -void SAXParser::start_hndl(void* p, const char* el, const char** attr) +void SAXParser::startHandler(void* parser, const char* el, const char** attr) { - SAXParser* parser = (SAXParser*)XML_GetUserData(p); + SAXParser* sax_parser = (SAXParser*)XML_GetUserData(parser); // Convert to comfy C++ values... attr_t attributes; - while(*attr) { + while(*attr) + { std::string at_name = *attr++; std::string at_value = *attr++; attributes.emplace(at_name, at_value); } - parser->startTag(std::string(el), attributes); + sax_parser->startTag(std::string(el), attributes); } -void SAXParser::end_hndl(void* p, const char* el) +void SAXParser::endHandler(void* parser, const char* el) { - SAXParser* parser = (SAXParser*)XML_GetUserData(p); - parser->endTag(std::string(el)); + SAXParser* sax_parser = (SAXParser*)XML_GetUserData(parser); + sax_parser->endTag(std::string(el)); } diff --git a/src/saxparser.h b/src/saxparser.h index b4d9823..8cfbdda 100644 --- a/src/saxparser.h +++ b/src/saxparser.h @@ -31,7 +31,8 @@ #include <expat.h> #include <fstream> -class SAXParser { +class SAXParser +{ public: SAXParser(); virtual ~SAXParser(); @@ -40,7 +41,8 @@ public: virtual int parseFile(const std::string& filename); //! Parses all the data in the buffer. - virtual int parseString(const std::string& str, const std::string& xml_source_name = ""); + virtual int parseString(const std::string& str, + const std::string& xml_source_name = ""); protected: using attr_t = std::map<std::string, std::string>; @@ -48,13 +50,16 @@ protected: virtual void characterData(const std::string& data) {} virtual void startTag(const std::string& name, const attr_t& attr) {} virtual void endTag(const std::string& name) {} - virtual void parseError(const std::string& buf, const std::string& error, const std::string& xml_source_name, std::size_t lineno); + virtual void parseError(const std::string& buf, + const std::string& error, + const std::string& xml_source_name, + std::size_t lineno); private: - XML_Parser p; + XML_Parser parser; std::string filename; - static void character_hndl(void* p, const XML_Char* s, int len); - static void start_hndl(void* p, const char* el, const char** attr); - static void end_hndl(void* p, const char* el); + static void characterHandler(void* parser, const XML_Char* cData, int len); + static void startHandler(void* parser, const char* el, const char** attr); + static void endHandler(void* parser, const char* el); }; |