From 6adb14a7027c8d54827093c83fc80694d71fb6a7 Mon Sep 17 00:00:00 2001 From: Bent Bisballe Nyeng Date: Wed, 20 Jun 2018 19:21:48 +0200 Subject: Fix missing finalization of instruments on load. Fix relative instrument filenames according to the drumkit file. Make drumkit creator create version 2.0 drumkits. Reduce missing refs file to a warning. --- src/dgxmlparser.cc | 11 ++++++++--- src/domloader.cc | 7 +++++-- src/domloader.h | 3 ++- src/drumkitloader.cc | 29 ++++++++++++++++++++++++----- src/drumkitparser.cc | 2 +- src/path.cc | 21 ++++++++++++++++++++- src/path.h | 3 +++ test/domloadertest.cc | 10 ++++++---- test/drumkit_creator.cc | 15 +++++++-------- 9 files changed, 76 insertions(+), 25 deletions(-) diff --git a/src/dgxmlparser.cc b/src/dgxmlparser.cc index 3fdedc2..8fdcec4 100644 --- a/src/dgxmlparser.cc +++ b/src/dgxmlparser.cc @@ -105,9 +105,10 @@ bool parseDrumkitFile(const std::string& filename, DrumkitDOM& dom) pugi::xml_document doc; pugi::xml_parse_result result = doc.load_file(filename.c_str()); res &= !result.status; - - if(!res) { - printf("PugiXml error %d\n", (int) result.offset); + if(!res) + { + ERR(dgxmlparser, "XML parse error: '%s' %d", filename.data(), + (int) result.offset); return false; } @@ -156,6 +157,10 @@ bool parseInstrumentFile(const std::string& filename, InstrumentDOM& dom) pugi::xml_document doc; pugi::xml_parse_result result = doc.load_file(filename.data()); res &= !result.status; + if(!res) + { + ERR(dgxmlparser, "XML parse error: '%s'", filename.data()); + } //TODO: handle version pugi::xml_node instrument = doc.child("instrument"); diff --git a/src/domloader.cc b/src/domloader.cc index 1498917..e095acb 100644 --- a/src/domloader.cc +++ b/src/domloader.cc @@ -49,7 +49,8 @@ DOMLoader::DOMLoader(Settings& settings, Random& random) { } -bool DOMLoader::loadDom(const DrumkitDOM& dom, +bool DOMLoader::loadDom(const std::string& basepath, + const DrumkitDOM& dom, const std::vector& instrumentdoms, DrumKit& drumkit) { @@ -91,7 +92,7 @@ bool DOMLoader::loadDom(const DrumkitDOM& dom, instrument->version = instrumentdom.version; instrument->_description = instrumentdom.description; - auto path = getPath(instrumentref.file); + auto path = getPath(basepath + "/" + instrumentref.file); for(const auto& sampledom : instrumentdom.samples) { auto sample = new Sample(sampledom.name, sampledom.power); @@ -164,6 +165,8 @@ bool DOMLoader::loadDom(const DrumkitDOM& dom, } } + instrument->finalise(); + // Transfer ownership to the DrumKit object. drumkit.instruments.emplace_back(std::move(instrument)); diff --git a/src/domloader.h b/src/domloader.h index ea0aad2..2901560 100644 --- a/src/domloader.h +++ b/src/domloader.h @@ -42,7 +42,8 @@ class DOMLoader public: DOMLoader(Settings& settings, Random& random); - bool loadDom(const DrumkitDOM& dom, + bool loadDom(const std::string& basepath, + const DrumkitDOM& dom, const std::vector& instrumentdoms, DrumKit& drumkit); diff --git a/src/drumkitloader.cc b/src/drumkitloader.cc index 0cef31b..2ad6b0e 100644 --- a/src/drumkitloader.cc +++ b/src/drumkitloader.cc @@ -127,23 +127,42 @@ bool DrumKitLoader::loadkit(const std::string& file) } else { - ERR(drumkitparser, "Error reading refs.conf"); + WARN(drumkitparser, "Error reading refs.conf"); } DrumkitDOM drumkitdom; std::vector instrumentdoms; std::string path = getPath(edited_filename); - bool parseerror = parseDrumkitFile(edited_filename, drumkitdom); + bool parseerror = false; + bool ret = parseDrumkitFile(edited_filename, drumkitdom); + if(!ret) + { + WARN(drumkitloader, "Drumkit file parser error: '%s'", + edited_filename.data()); + } + + parseerror |= !ret; for(const auto& ref : drumkitdom.instruments) { instrumentdoms.emplace_back(); - parseerror &= parseInstrumentFile(path + "/" + ref.file, instrumentdoms.back()); + bool ret = parseInstrumentFile(path + "/" + ref.file, instrumentdoms.back()); + if(!ret) + { + WARN(drumkitloader, "Instrument file parser error: '%s'", + edited_filename.data()); + } + + parseerror |= !ret; } DOMLoader domloader(settings, rand); - parseerror &= domloader.loadDom(drumkitdom, instrumentdoms, kit); - + ret = domloader.loadDom(path, drumkitdom, instrumentdoms, kit); + if(!ret) + { + WARN(drumkitloader, "DOMLoader error"); + } + parseerror |= !ret; if(parseerror) { ERR(drumgizmo, "Drumkit parser failed: %s\n", file.c_str()); diff --git a/src/drumkitparser.cc b/src/drumkitparser.cc index d86957e..7966d06 100644 --- a/src/drumkitparser.cc +++ b/src/drumkitparser.cc @@ -58,7 +58,7 @@ int DrumKitParser::parseFile(const std::string& filename) } else { - ERR(drumkitparser, "Error reading refs.conf"); + WARN(drumkitparser, "Error reading refs.conf"); } path = getPath(edited_filename); diff --git a/src/path.cc b/src/path.cc index c2e7910..993f9a6 100644 --- a/src/path.cc +++ b/src/path.cc @@ -40,7 +40,7 @@ std::string getPath(const std::string& file) #ifdef __MINGW32__ char drive[_MAX_DRIVE]; char dir[_MAX_DIR]; - _splitpath(file.c_str(), drive, dir, NULL, NULL); + _splitpath(file.c_str(), drive, dir, nullptr, nullptr); path = std::string(drive) + dir; #else // POSIX @@ -51,3 +51,22 @@ std::string getPath(const std::string& file) return path; } + +std::string getFile(const std::string& file) +{ + std::string path; + +#ifdef __MINGW32__ + char fname[_MAX_FNAME]; + char ext[_MAX_EXT]; + _splitpath(file.c_str(), nullptr, nullptr, fname, ext); + path = std::string(fname) + "." + ext; +#else + // POSIX + char* buffer = strdup(file.c_str()); + path = basename(buffer); + free(buffer); +#endif + + return path; +} diff --git a/src/path.h b/src/path.h index 50ff842..270a58a 100644 --- a/src/path.h +++ b/src/path.h @@ -30,3 +30,6 @@ //! \returns path component of full filename with path. std::string getPath(const std::string& file); + +//! \returns path component of full filename with path. +std::string getFile(const std::string& file); diff --git a/test/domloadertest.cc b/test/domloadertest.cc index 69b9821..55d49a7 100644 --- a/test/domloadertest.cc +++ b/test/domloadertest.cc @@ -34,6 +34,7 @@ #include #include "scopedfile.h" +#include "path.h" class DOMLoaderTest : public DGUnit @@ -99,13 +100,13 @@ public: " \n" \ " \n" \ " \n" \ - " \n" \ + " \n" \ " \n" \ " \n" \ " \n" \ " \n" \ " \n" \ - " \n" \ + " \n" \ " \n" \ " \n" \ " \n" \ @@ -119,14 +120,15 @@ public: DrumkitDOM drumkitdom; std::vector instrumentdoms; DGUNIT_ASSERT(parseDrumkitFile(scoped_file.filename(), drumkitdom)); + auto basepath = getPath(scoped_file.filename()); for(const auto& ref: drumkitdom.instruments) { instrumentdoms.emplace_back(); - DGUNIT_ASSERT(parseInstrumentFile(ref.file, instrumentdoms.back())); + DGUNIT_ASSERT(parseInstrumentFile(basepath + "/" + ref.file, instrumentdoms.back())); } DOMLoader domloader(settings, random); - DGUNIT_ASSERT(domloader.loadDom(drumkitdom, instrumentdoms, drumkit)); + DGUNIT_ASSERT(domloader.loadDom(basepath, drumkitdom, instrumentdoms, drumkit)); // // Drumkit: diff --git a/test/drumkit_creator.cc b/test/drumkit_creator.cc index 7a0291c..d92f876 100644 --- a/test/drumkit_creator.cc +++ b/test/drumkit_creator.cc @@ -44,6 +44,7 @@ DrumkitCreator::~DrumkitCreator() { + return; for (const auto& file: created_files) { auto error = unlink(file.c_str()); @@ -304,18 +305,16 @@ void DrumkitCreator::createInstrument(const InstrumentData& data, std::size_t nu const std::string& dir) { std::string prefix = "\n" - "\n"; + "\n" + " \n"; // FIXME sampleref - std::string postfix = "\n" - "\n" - "\n" - "\n" - "\n" - "\n"; + std::string postfix = " \n\n"; std::string samples; + float power = 1.0f; for (const auto& sample: data.sample_data) { - samples += "\n"; + samples += "\n"; + power += 0.1f; for (std::size_t i = 0; i < sample.audiofiles.size(); ++i) { -- cgit v1.2.3