From 9efd8cf9af0207c5cfc096295b5aaa7bc74e0251 Mon Sep 17 00:00:00 2001 From: zawz Date: Fri, 22 May 2020 16:15:48 +0200 Subject: [PATCH] Output to mim format + bugfixes --- example.mim | 28 ++++++++------------- example.zfd | 26 +++++++------------ include/format.hpp | 2 ++ src/format.cpp | 23 +++++++++++++++++ src/main.cpp | 63 ++++++++++++++++++++++++++-------------------- src/system.cpp | 2 -- 6 files changed, 81 insertions(+), 63 deletions(-) diff --git a/example.mim b/example.mim index 467b9b4..84ffc95 100644 --- a/example.mim +++ b/example.mim @@ -1,49 +1,43 @@ - - Device "Launch Control" //display value 0:127 for knobs 21-28 Command controller id=21:28 - shell=echo "Knob #$id ch$channel:$value" + shell='echo "Knob #$id ch$channel:$value"' - //display value 0:127 for knob 41 from any channel + //display value -100:100 for knob 41 on channel 0 Command controller id=41 - channel=* - shell=echo "Knob #$id ch$channel:$value" - - //display value -100:100 for knob 42 on channel 0 - Command controller - id=42 channel=0 remap=-100:100 - shell=echo "Knob #$id ch0:$value r:$rawvalue" + shell='echo "Knob #$id ch0:$value r:$rawvalue"' - //display value 0:1:0 for knob 42 on channel 1 (first half) + //display value 0:1:0 for knob 41 on channel 1 (first half) Command controller id=42 channel=1 range=0:63 remap=0:1 float=true - shell=echo "Knob #$id ch1:$value r:$rawvalue" + shell='echo "Knob #$id ch1:$value r:$rawvalue"' - //display value 0:1:0 for knob 42 on channel 1 (second half) + //display value 0:1:0 for knob 41 on channel 1 (second half) Command controller id=42 channel=1 range=64:127 remap=1:0 float=true - shell=echo "Knob #$id ch1:$value r:$rawvalue" + shell='echo "Knob #$id ch1:$value r:$rawvalue"' Device "Launchpad S" + //display any note pressed and its velocity Command note - shell=echo "Note $id on velocity:$velocity" + shell=echo Note $id on velocity:$velocity + //display any note off Command note trigger=0 - shell=echo "Note $id off" + shell=echo Note $id off diff --git a/example.zfd b/example.zfd index 1e04d13..e952f15 100644 --- a/example.zfd +++ b/example.zfd @@ -10,46 +10,38 @@ //displays value 0:127 for knobs 21-28 type=controller id=21:28 - shell=echo "Knob #$id ch$channel:$value" - }, - // KNOBS LOW - { - //KNOB L1 - //displays value 0:127 for knob 41 from any channel - type=controller - id=41 - shell=echo "Knob #$id ch$channel:$value" + shell='echo "Knob #$id ch$channel:$value"' }, { //KNOB L2 - //displays value -100:100 for knob 42 on channel 0 + //displays value -100:100 for knob 41 on channel 0 type=controller id=42 channel=0 remap=-100:100 - shell=echo "Knob #$id ch0:$value r:$rawvalue" + shell='echo "Knob #$id ch0:$value r:$rawvalue"' }, { //KNOB L3 H1 - //displays value 0:1:0 for knob 42 on channel 1 (first half) + //displays value 0:1:0 for knob 41 on channel 1 (first half) type=controller id=42 channel=1 range=0:63 remap=0:1 float=true - shell=echo "Knob #$id ch1:$value r:$rawvalue" + shell='echo "Knob #$id ch1:$value r:$rawvalue"' }, { //KNOB L3 H2 - //displays value 0:1:0 for knob 42 on channel 1 (second half) + //displays value 0:1:0 for knob 41 on channel 1 (second half) type=controller id=42 channel=1 range=64:127 remap=1:0 float=true - shell=echo "Knob #$id ch1:$value r:$rawvalue" + shell='echo "Knob #$id ch1:$value r:$rawvalue"' } ] } @@ -61,13 +53,13 @@ { // ANY NOTE ON type=note - shell=echo "Note $id on velocity:$velocity" + shell=echo Note $id on velocity:$velocity }, { // ANY NOTE OFF type=note trigger=0 - shell=echo "Note $id off" + shell=echo Note $id off } ] } diff --git a/include/format.hpp b/include/format.hpp index 1039a51..ada53c5 100644 --- a/include/format.hpp +++ b/include/format.hpp @@ -5,6 +5,8 @@ ztd::chunkdat mimtochk(std::string mim); +std::string chktomim(ztd::chunkdat const& chk, std::string const& aligner); + std::string file_strimport(const std::string& path); bool is_mim(const std::string& str); diff --git a/src/format.cpp b/src/format.cpp index 871eb6f..042d272 100644 --- a/src/format.cpp +++ b/src/format.cpp @@ -98,6 +98,29 @@ ztd::chunkdat mimtochk(std::string mim) return chk; } +std::string chktomim(ztd::chunkdat const& chk, std::string const& aligner) +{ + std::string ret; + for(int i=0 ; iactivated) + if(options["zfd"]) { file.data() = str; } - else if(options.find("mim")->activated) + else if(options["mim"]) { file.data() = mimtochk(str); } @@ -100,11 +100,11 @@ void load_filedat(ztd::filedat& file, bool from_stdin, std::string const& path) { file.setFilePath(path); log("Loading map file '" + path + "'\n"); - if(options.find("zfd")->activated) + if(options["zfd"]) { file.import_file(); } - else if(options.find("mim")->activated) + else if(options["mim"]) { file.data() = mimtochk(file_strimport(path)); } @@ -161,7 +161,8 @@ int main(int argc, char* argv[]) options.add(ztd::option("\r [Map file]")); options.add(ztd::option('m',"mim", false, "Read file in mim format")); options.add(ztd::option('z',"zfd", false, "Read file in zfd format")); - options.add(ztd::option('o',"output", true, "Output the resulting zfd map to file. - for stdout", "file")); + options.add(ztd::option('o',"output", true, "Output the resulting map to file", "file")); + options.add(ztd::option("out-zfd", false, "Output in zfd format")); options.add(ztd::option("aligner", true, "String to use for aligning output map format. Default \\t", "string")); options.add(ztd::option("no-reload", false, "Disable auto reloading when file changes are detected")); options.add(ztd::option("\rIf no file format is specified, the program will try to guess the format")); @@ -180,74 +181,72 @@ int main(int argc, char* argv[]) stop(1); } - ztd::option* op=nullptr; //exit options - if( options.find('h')->activated ) + if( options['h'] ) { help(); stop(0); } - if( options.find('v')->activated ) + if( options['v'] ) { version(); stop(0); } - if( options.find("mim-format")->activated ) + if( options["mim-format"] ) { printf("%s\n", MIM_FORMAT); stop(0); } - if( options.find("zfd-format")->activated ) + if( options["zfd-format"] ) { printf("%s\n", ZFD_FORMAT); stop(0); } - if( options.find("command-tags")->activated ) + if( options["command-tags"] ) { printf("%s\n", COMMAND_TAGS); stop(0); } - if( options.find("shell-format")->activated ) + if( options["shell-format"] ) { printf("%s\n", SHELL_FORMAT); stop(0); } - if( options.find('L')->activated ) + if( options['L'] ) { ztd::shr("aseqdump -l"); stop(0); } - if( options.find('l')->activated ) + if( options['l'] ) { ztd::shr(LIST_COMMAND); stop(0); } - op = options.find('p'); - if( op->activated ) + if( options['p'] ) { - option_p(op->argument); + option_p(options['p']); stop(0); } - if (options.find('o')->activated) + if (options['o']) { log_on=false; } //behavioral options - if( options.find("no-reload")->activated ) + if( options["no-reload"] ) { autoreload=false; } - if( options.find("no-log")->activated ) + if( options["no-log"] ) { log_on=false; } std::string aligner="\t"; - if(options.find("aligner")->activated) + if(options["aligner"]) { - aligner=options.find("aligner")->argument; + aligner=options["aligner"].argument; } //no argument: display help @@ -274,14 +273,24 @@ int main(int argc, char* argv[]) //load load_filedat(file, from_stdin, filepath); //output - if(options.find('o')->activated) + if(options['o']) { - if(options.find('o')->argument == "-") { - std::cout << file.strval(aligner) << std::endl; + std::string ret; + if(options["out-zfd"]) + ret=file.data().str(0, aligner); + else + ret=chktomim(file.data(), aligner); + if(options['o'].argument == "-") { + std::cout << ret << std::endl; } else { - file.setFilePath(options.find('o')->argument); - file.export_file(); + std::ofstream output(options['o']); + if(!output) + { + std::cerr << "Cannot write to file '" + options['o'].argument + "'\n"; + return 1; + } + output << ret << std::endl ; } return 0; } diff --git a/src/system.cpp b/src/system.cpp index 86c1d5c..fe891e0 100644 --- a/src/system.cpp +++ b/src/system.cpp @@ -97,8 +97,6 @@ void announce_loop() } int ret = WEXITSTATUS(ztd::pclose2(stream, announce_thread_pid)); - if( ret != 0 ) - throw std::runtime_error("ALSA error"); if(buff != NULL) free(buff);