commit df809f2185eaaca3dd34cb83e357904574672b38
parent c4457595db17525a9619212f32ba1bbcef0c7fc9
Author: Adam Malone <[email protected]>
Date: Tue, 8 Jan 2019 16:42:36 -0600
add Quantizer to the ever-growing dtpulse
Diffstat:
3 files changed, 76 insertions(+), 0 deletions(-)
diff --git a/src/dtpulse.cpp b/src/dtpulse.cpp
@@ -586,6 +586,9 @@ Parser::Parser(std::string expr) {
expression=expr;
inError = false;
}
+Parser::Parser() {
+ Parser("");
+}
void Parser::setForLaundry() {
//whitelists
std::vector<std::string> laundryInterleaveAny = {"Letter","Integer","ChanceOfInteger","Digit","LeftParen","RightParen"};
@@ -1138,3 +1141,55 @@ std::vector<Token> tokenizeString(std::string input) {
}
return stack;
}
+void whoKnowsQuantize(std::string input) {
+ Quantizer q = Quantizer("2212221",12,0);
+ float in = std::stof(input);
+ printf("%f\n",q.quantize(in));
+}
+Quantizer::Quantizer(std::string intervals, int divisions, int trans) {
+ scaleParser = Parser(intervals);
+ //printTokenVector(scaleParser.tokens);
+ numDivisions = divisions;
+ transpose=trans;
+ fTranspose = (float)transpose/(float)numDivisions;
+ mappedValues = generateMappedValues();
+ numSteps = (int) mappedValues.size();
+ printFloatVector(mappedValues);
+}
+std::vector<float> Quantizer::generateMappedValues() {
+ std::vector<float> output;
+ float sum = 0.f;
+ float fNumDivisions = (float)numDivisions;
+ float currentVal = 0.f;
+ std::vector<Token> stack = scaleParser.tokens;
+ output.push_back(0.f);
+ for(unsigned int i = 0; i < stack.size(); i++) {
+ if(stack[i].type=="Digit") {
+ sum += std::stof(stack[i].value);
+ currentVal = sum/fNumDivisions;
+ output.push_back(currentVal);
+ }
+ }
+ return output;
+}
+float Quantizer::findClosestValue(float input) {
+ float closestValue = 10.f;
+ float smallestDiff = 10.f;
+ float thisDiff = 0.f;
+ for(int i = 0; i < numSteps; i++) {
+ thisDiff = fabs(input - mappedValues[i]);
+ if(thisDiff < smallestDiff) {
+ closestValue = mappedValues[i];
+ smallestDiff = thisDiff;
+ }
+ }
+ return closestValue;
+}
+float Quantizer::quantize(float input) {
+ float octavePart = floor(input);
+ float fractionalPart = input-octavePart;
+ float quantizedFractional = findClosestValue(fractionalPart);
+ float quantizedPreTranspose = octavePart + quantizedFractional;
+ float quantizedVal = quantizedPreTranspose + fTranspose;
+ return quantizedVal;
+}
diff --git a/src/dtpulse.hpp b/src/dtpulse.hpp
@@ -7,6 +7,7 @@
#include <algorithm>
#include <typeinfo>
#include <stdexcept>
+#include <math.h>
#ifndef MY_GLOBALS_H
#define MY_GLOBALS_H
@@ -30,6 +31,7 @@ class Token {
};
class Parser {
public:
+ Parser();
Parser(std::string expr);
std::string expression;
std::vector<Token> tokens;
@@ -112,6 +114,21 @@ class LaundrySoupSequence {
void incrementAndCheck();
void randomizePulseValue(int index);
};
+class Quantizer {
+ public:
+ Quantizer(std::string intervals, int divisions, int trans);
+ float quantize(float val);
+ int numDivisions;
+ int transpose;
+ bool parseError;
+ int numSteps;
+ float fTranspose;
+ private:
+ Parser scaleParser;
+ float findClosestValue(float input);
+ std::vector<float> mappedValues;
+ std::vector<float> generateMappedValues();
+};
bool matchesAny(std::string val, std::vector<std::string> whitelist);
bool is_digits(const std::string &str);
void padTo(std::string &str, const size_t num, const char paddingChar );
@@ -138,5 +155,6 @@ bool matchParens(std::string value);
std::string evalToken(std::string input, std::string type,std::vector<Token> tStack);
void whoKnows(std::string input);
void whoKnowsLaundry(std::string input);
+void whoKnowsQuantize(std::string input);
std::vector<int> getIndicesFromTokenStack(std::vector<Token> tokens);
std::vector<int> duplicateIntVector(std::vector<int> input);
diff --git a/src/test.cpp b/src/test.cpp
@@ -39,6 +39,9 @@ int main(int argc, char** argv)
else if(type==6) {
whoKnowsLaundry(argv[1]);
}
+ else if(type==7) {
+ whoKnowsQuantize(argv[1]);
+ }
return 0;
}
void printVector(std::vector <int> intVector) {