diff options
| -rw-r--r-- | src/drumkit.cc | 24 | ||||
| -rw-r--r-- | src/drumkit.h | 1 | ||||
| -rw-r--r-- | src/drumkitparser.cc | 30 | ||||
| -rw-r--r-- | src/instrumentparser.cc | 56 | ||||
| -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 | 18 | ||||
| -rw-r--r-- | src/path.h | 1 | ||||
| -rw-r--r-- | src/sample.cc | 21 | ||||
| -rw-r--r-- | src/sample.h | 11 | ||||
| -rw-r--r-- | src/saxparser.cc | 66 | ||||
| -rw-r--r-- | src/saxparser.h | 19 | 
13 files changed, 113 insertions, 141 deletions
| diff --git a/src/drumkit.cc b/src/drumkit.cc index 1b95c87..f25a6ea 100644 --- a/src/drumkit.cc +++ b/src/drumkit.cc @@ -39,12 +39,11 @@ DrumKit::~DrumKit()  void DrumKit::clear()  { -	Instruments::iterator i = instruments.begin(); -	while(i != instruments.end()) +	for(auto& instrument : instruments)  	{ -		delete *i; -		i++; +		delete instrument;  	} +  	instruments.clear();  	channels.clear(); @@ -78,20 +77,3 @@ size_t DrumKit::getSamplerate() const  {  	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 1ceef3e..a6913c0 100644 --- a/src/drumkit.h +++ b/src/drumkit.h @@ -33,7 +33,6 @@  #include "instrument.h"  #include "versionstr.h" -class DrumKitParser;  class DrumKit  {  	friend class DrumKitParser; diff --git a/src/drumkitparser.cc b/src/drumkitparser.cc index a538996..c727590 100644 --- a/src/drumkitparser.cc +++ b/src/drumkitparser.cc @@ -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;  	} @@ -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->getName().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 8bcb7a0..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,10 +64,11 @@ 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); @@ -82,7 +83,6 @@ void InstrumentParser::startTag(const std::string& name, const attr_t& attr)  	if(name == "samples")  	{ -  	}  	if(name == "sample") @@ -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 43c64f0..c2e7910 100644 --- a/src/path.cc +++ b/src/path.cc @@ -35,17 +35,19 @@  std::string getPath(const std::string& file)  { -	std::string p; -#ifndef __MINGW32__ -	char *b = strdup(file.c_str()); -	p = dirname(b); -	free(b); -#else +	std::string path; + +#ifdef __MINGW32__  	char drive[_MAX_DRIVE];  	char dir[_MAX_DIR];  	_splitpath(file.c_str(), drive, dir, NULL, NULL); -	p = std::string(drive) + dir; +	path = std::string(drive) + dir; +#else +	// POSIX +	char* buffer = strdup(file.c_str()); +	path = dirname(buffer); +	free(buffer);  #endif -	return p; +	return path;  } @@ -28,4 +28,5 @@  #include <string> +//! \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 66fe3c5..ced8a47 100644 --- a/src/sample.cc +++ b/src/sample.cc @@ -65,24 +65,3 @@ AudioFile* Sample::getAudioFile(Channel* c)  	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), nullptr, "?"); - -TEST_END; - -#endif /*TEST_SAMPLE*/ diff --git a/src/sample.h b/src/sample.h index 5b4f3b5..a3400a4 100644 --- a/src/sample.h +++ b/src/sample.h @@ -34,7 +34,6 @@  typedef std::map<Channel*, AudioFile*> AudioFiles; -class InstrumentParser;  class Sample  {  	friend class InstrumentParser; @@ -53,13 +52,3 @@ private:  	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> - */ 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 92197d1..4b1a273 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::unordered_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);  }; | 
