diff --git a/Magical8bitPlug2.jucer b/Magical8bitPlug2.jucer index a4db363..a8b4e52 100644 --- a/Magical8bitPlug2.jucer +++ b/Magical8bitPlug2.jucer @@ -10,6 +10,10 @@ pluginDesc="8bit sound generator 2nd ver. by YMCK" displaySplashScreen="1" jucerFormatVersion="1" version="1.0.1"> + + + result14 = parser.parseSlope(input14, 0, 15, &error); + expect(result14.size() == 4); + expect(result14[0] == 3); + expect(result14[1] == 2); + expect(result14[2] == 1); + expect(result14[3] == 0); // Should include the last value + + beginTest ("Slow decrement"); + String input15 = "2to0in8"; + std::vector result15 = parser.parseSlope(input15, 0, 15, &error); + expect(result15.size() == 8); + expect(result15[0] == 2); + expect(result15[1] == 2); + expect(result15[2] == 1); + expect(result15[3] == 1); + expect(result15[4] == 1); + expect(result15[5] == 1); + expect(result15[6] == 0); + expect(result15[7] == 0); + + beginTest ("Fast decrement"); + String input16 = "15to0in5"; + std::vector result16 = parser.parseSlope(input16, 0, 15, &error); + expect(result16.size() == 5); + expect(result16[0] == 15); + expect(result16[1] == 11); + expect(result16[2] == 8); + expect(result16[3] == 4); + expect(result16[4] == 0); + + beginTest ("Up slope"); + String input17 = "0to3in4"; + std::vector result17 = parser.parseSlope(input17, 0, 15, &error); + expect(result17.size() == 4); + expect(result17[0] == 0); + expect(result17[1] == 1); + expect(result17[2] == 2); + expect(result17[3] == 3); // Should include the last value + + beginTest ("[Error] Missing 'in'"); + String input18 = "0to3"; + std::vector result18 = parser.parseSlope(input18, 0, 15, &error); + expect(error == kParseErrorMissingSlopeLengthDelimiter); + + beginTest ("[Error] Missing initial value"); + String input19 = "to5in5"; + std::vector result19 = parser.parseSlope(input19, 0, 15, &error); + expect(error == kParseErrorMissingSlopeInitialValue); + + beginTest ("[Error] Missing final value"); + String input20 = "4toin5"; + std::vector result20 = parser.parseSlope(input20, 0, 15, &error); + expect(error == kParseErrorMissingSlopeFinalValue); + + beginTest ("[Error] Missing frame count"); + String input21 = "0to3in"; + std::vector result21 = parser.parseSlope(input21, 0, 15, &error); + expect(error == kParseErrorMissingSlopeFrameCount); + + //------------------------------------------------------- + // + // Hold + // + //------------------------------------------------------- + beginTest ("Hold"); + String input22 = "15x3"; + std::vector result22 = parser.parseHold(input22, 0, 15, &error); + expect(result22.size() == 3); + expect(result22[0] == 15); + expect(result22[1] == 15); + expect(result22[2] == 15); + + beginTest ("[Error] Missing value"); + String input23 = "x3"; + std::vector result23 = parser.parseHold(input23, 0, 15, &error); + expect(error == kParseErrorMissingHoldValue); + + beginTest ("[Error] Missing value"); + String input24 = "3x"; + std::vector result24 = parser.parseHold(input24, 0, 15, &error); + expect(error == kParseErrorMissingHoldFrameCount); + + //------------------------------------------------------- + // + // Total + // + //------------------------------------------------------- + beginTest ("Overall test"); + error = kParseErrorNone; + String input25 = "1, 2x2, 3to4in2 [5, 6]|7, 8"; + FrameSequence result25 = parser.parse(input25, 0, 15, &error); + expect(result25.valueAt(0) == 1); + expect(result25.valueAt(1) == 2); + expect(result25.valueAt(2) == 2); + expect(result25.valueAt(3) == 3); + expect(result25.valueAt(4) == 4); + expect(result25.valueAt(5) == 5); + expect(result25.valueAt(6) == 6); + expect(result25.valueAt(7) == 7); + expect(result25.valueAt(8) == 8); + expect(result25.loopStartIndex = 5); + expect(result25.releaseSequenceStartIndex = 7); + + beginTest ("Value error"); + error = kParseErrorNone; + String input26 = "1, 2, 3"; + FrameSequence result26 = parser.parse(input26, 0, 2, &error); + expect(error = kParseErrorValueOutOfRange); + + beginTest ("Value error - under"); + error = kParseErrorNone; + String input27 = "1, 2, 3"; + FrameSequence result27 = parser.parse(input27, 2, 3, &error); + expect(error = kParseErrorValueOutOfRange); + + beginTest ("Value error2"); + error = kParseErrorNone; + String input28 = "1, 2, 3x2"; + FrameSequence result28 = parser.parse(input28, 0, 2, &error); + expect(error = kParseErrorValueOutOfRange); + + beginTest ("Value error3"); + error = kParseErrorNone; + String input29 = "1, 2, 0to3in2"; + FrameSequence result29 = parser.parse(input29, 0, 2, &error); + expect(error = kParseErrorValueOutOfRange); + + } +}; diff --git a/Source/FrameSequenceParseErrors.cpp b/Source/FrameSequenceParseErrors.cpp index 49e4553..8e05e4f 100644 --- a/Source/FrameSequenceParseErrors.cpp +++ b/Source/FrameSequenceParseErrors.cpp @@ -58,6 +58,10 @@ String getParseErrorString (ParseError err, int minValue, int maxValue) return TRANS ("Missing destination value before \"in\""); break; + case kParseErrorMissingSlopeFrameCount: + return TRANS ("Frame count should be specified after \"in\""); + break; + case kParseErrorNotANumber: return TRANS ("Number parse failed."); break; @@ -70,11 +74,11 @@ String getParseErrorString (ParseError err, int minValue, int maxValue) return TRANS ("Frame count should be more than 2"); break; - case kParseErrorMissingValueForRepeatDelimiter: + case kParseErrorMissingHoldValue: return TRANS ("Operator x should be followed by a number."); break; - case kParseErrorMissingFrameCountForRepeatDelimiter: + case kParseErrorMissingHoldFrameCount: return TRANS ("A number should be specified after operator x."); break; diff --git a/Source/FrameSequenceParseErrors.h b/Source/FrameSequenceParseErrors.h index 68881bc..3afa5fe 100644 --- a/Source/FrameSequenceParseErrors.h +++ b/Source/FrameSequenceParseErrors.h @@ -27,11 +27,12 @@ enum ParseError kParseErrorMissingSlopeLengthDelimiter, kParseErrorMissingSlopeInitialValue, kParseErrorMissingSlopeFinalValue, + kParseErrorMissingSlopeFrameCount, kParseErrorNotANumber, kParseErrorValueOutOfRange, kParseErrorFrameLengthTooShort, - kParseErrorMissingValueForRepeatDelimiter, - kParseErrorMissingFrameCountForRepeatDelimiter, + kParseErrorMissingHoldValue, + kParseErrorMissingHoldFrameCount, }; String getParseErrorString (ParseError err, int minValue = 0, int maxValue = 0); diff --git a/Source/FrameSequenceParser.cpp b/Source/FrameSequenceParser.cpp index ff3696c..e6ba71d 100644 --- a/Source/FrameSequenceParser.cpp +++ b/Source/FrameSequenceParser.cpp @@ -10,11 +10,7 @@ #include "FrameSequenceParser.h" -// -// Fileprivate -// - -std::vector parseSlope (const String& input, +std::vector FrameSequenceParser::parseSlope (const String& input, int minValue, int maxValue, ParseError* error) @@ -40,18 +36,23 @@ std::vector parseSlope (const String& input, // find index of "in" int inIndex = input.indexOf ("in"); - if (inIndex > input.length() - 3) - { - *error = kParseErrorMissingSlopeFinalValue; - return retval; - } - if (inIndex < 0) { *error = kParseErrorMissingSlopeLengthDelimiter; return retval; } + if (inIndex > input.length() - 3) + { + *error = kParseErrorMissingSlopeFrameCount; + return retval; + } + if (inIndex - toIndex < 3) + { + *error = kParseErrorMissingSlopeFinalValue; + return retval; + } + // get 3 substrings separated by "to" and "in" and put them into `from`, `to`, `cntStr` String fromStr = input.substring (0, toIndex); String toStr = input.substring (toIndex + 2, inIndex); @@ -115,7 +116,7 @@ std::vector parseSlope (const String& input, return retval; } -std::vector parseRepeat (const String& input, +std::vector FrameSequenceParser::parseHold (const String& input, int minValue, int maxValue, ParseError* error) @@ -126,13 +127,13 @@ std::vector parseRepeat (const String& input, if (xIndex < 1) { - *error = kParseErrorMissingValueForRepeatDelimiter; + *error = kParseErrorMissingHoldValue; return retval; } - if (xIndex > input.length() - 1) + if (xIndex >= input.length() - 1) { - *error = kParseErrorMissingFrameCountForRepeatDelimiter; + *error = kParseErrorMissingHoldFrameCount; return retval; } @@ -172,7 +173,7 @@ std::vector parseRepeat (const String& input, return retval; } -std::vector parseSegment (const String& input, +std::vector FrameSequenceParser::parseSegment (const String& input, int minValue, int maxValue, ParseError* error) @@ -194,7 +195,7 @@ std::vector parseSegment (const String& input, else if (aToken.contains ("x")) { // parse as repeat-fixed-value - parsed = parseRepeat (aToken, minValue, maxValue, error); + parsed = parseHold (aToken, minValue, maxValue, error); } else { @@ -228,6 +229,161 @@ std::vector parseSegment (const String& input, return retval; } +SegmentIndexes FrameSequenceParser::findSegment(const String& input) { + int releaseBlockIndex = -1; + int repeatStartIndex = -1; + int repeatEndIndex = -1; + int openBracketCount = 0; + int closeBracketCount = 0; + + SegmentIndexes retval = SegmentIndexes(); + + // loop by character + for (int i = 0; i < input.length(); i++) + { + if (input[i] == '|') // found "|": + { + if (releaseBlockIndex >= 0) + { + // if releaseBlockIndex is already determined: Duplication Error + retval.error = kParseErrorDuplicatedReleaseDelimiter; + return retval; + } + + // if(repeatStartIndex >= 0) { + // // if appeard before "[" or "]": Repetition After Release Error + // throw new FrameSequenceParseException(TRANS("You cannot repeat in release phase"), true); + // } + // set releaseBlockIndex + releaseBlockIndex = i + 1; + + if (repeatEndIndex < 0) + { + // if "]" is omitted: Also set repeatEndIndex + repeatEndIndex = i; + } + } + + if (input[i] == '[') /// found "[": + { + openBracketCount++; + + if (openBracketCount > 1) + { + // Duplication Error + retval.error = kParseErrorDuplicatedOpenBracket; + return retval; + } + + if (releaseBlockIndex >= 0) + { + // if repeat end is already defined: Repetition After Release Error + retval.error = kParseErrorRepeatingInReleaseBlock; + return retval; + } + + if (repeatEndIndex >= 0) + { + // if repeat end is already defined: Repetition After Release Error + retval.error = kParseErrorDuplicatedOpenBracket; + return retval; + } + + // set repeatStartIndex + repeatStartIndex = i + 1; + } + + if (input[i] == ']') // found "]": + { + closeBracketCount++; + + if (closeBracketCount > 1) + { + // Duplication Error + retval.error = kParseErrorDuplicatedCloseBracket; + return retval; + } + + if (repeatStartIndex < 0) + { + // if repeatStartIndex hasn't set: Syntax Error + retval.error = kParseErrorUnmatchingCloseBracket; + return retval; + } + + if (releaseBlockIndex >= 0) + { + // if repeat end is already defined: Repetition After Release Error + retval.error = kParseErrorRepeatingInReleaseBlock; + return retval; + } + + repeatEndIndex = i; + } + } + + // if (releaseBlockIndex < 0) { // "|" didn't explicitly specified + // releaseBlockIndex = trimmed.length(); + // } + + if (openBracketCount != closeBracketCount) + { + retval.error = kParseErrorUnmatchingBracketNumber; + return retval; + } + + if (releaseBlockIndex - repeatEndIndex > 1) + { + // throw new FrameSequenceParseException(TRANS("Elements between repeat block and release block will be ignored"), false); + // FiXME: non-fatal exceptionをどう扱うか + } + + retval.releaseBlockIndex = releaseBlockIndex; + retval.repeatStartIndex = repeatStartIndex; + retval.repeatEndIndex = repeatEndIndex; + return retval; +} + +void FrameSequenceParser::splitSegment (const String& input, + SegmentIndexes indexes, + String& beforeRepeat, + String& insideRepeat, + String& afterRelease) { + int releaseBlockIndex = indexes.releaseBlockIndex; + int repeatStartIndex = indexes.repeatStartIndex; + int repeatEndIndex = indexes.repeatEndIndex; + + // Just for convenience + bool hasRelease = (releaseBlockIndex >= 0); + bool shouldRepeat = (repeatStartIndex >= 0); + + if (shouldRepeat) + { + if (hasRelease) + { + beforeRepeat = input.substring(0, repeatStartIndex - 1); + insideRepeat = input.substring (repeatStartIndex, repeatEndIndex); + afterRelease = input.substring(releaseBlockIndex, input.length()); + } + else + { + beforeRepeat = input.substring(0, repeatStartIndex - 1); + insideRepeat = input.substring (repeatStartIndex, repeatEndIndex); + } + } + else + { + if (hasRelease) { + beforeRepeat = input.substring (0, releaseBlockIndex - 1); + afterRelease = input.substring (releaseBlockIndex, input.length()); + } + else + { + beforeRepeat = input.substring (0, input.length()); + } + } +} + FrameSequence FrameSequenceParser::parse (const String& input, int minValue, int maxValue, @@ -248,116 +404,11 @@ FrameSequence FrameSequenceParser::parse (const String& input, // // Overall structure // - - int releaseBlockIndex = -1; - int repeatStartIndex = -1; - int repeatEndIndex = -1; - int openBracketCount = 0; - int closeBracketCount = 0; - - // loop by character - for (int i = 0; i < trimmed.length(); i++) - { - if (trimmed[i] == '|') // found "|": - { - if (releaseBlockIndex >= 0) - { - // if releaseBlockIndex is already determined: Duplication Error - *error = kParseErrorDuplicatedReleaseDelimiter; - return fs; - } - - // if(repeatStartIndex >= 0) { - // // if appeard before "[" or "]": Repetition After Release Error - // throw new FrameSequenceParseException(TRANS("You cannot repeat in release phase"), true); - // } - // set releaseBlockIndex - releaseBlockIndex = i + 1; - - if (repeatEndIndex < 0) - { - // if "]" is omitted: Also set repeatEndIndex - repeatEndIndex = i; - } - } - - if (trimmed[i] == '[') /// found "[": - { - openBracketCount++; - - if (openBracketCount > 1) - { - // Duplication Error - *error = kParseErrorDuplicatedOpenBracket; - return fs; - } - - if (releaseBlockIndex >= 0) - { - // if repeat end is already defined: Repetition After Release Error - *error = kParseErrorRepeatingInReleaseBlock; - return fs; - } - - if (repeatEndIndex >= 0) - { - // if repeat end is already defined: Repetition After Release Error - *error = kParseErrorDuplicatedOpenBracket; - return fs; - } - - // set repeatStartIndex - repeatStartIndex = i + 1; - } - - if (trimmed[i] == ']') // found "]": - { - closeBracketCount++; - - if (closeBracketCount > 1) - { - // Duplication Error - *error = kParseErrorDuplicatedCloseBracket; - return fs; - } - - if (repeatStartIndex < 0) - { - // if repeatStartIndex hasn't set: Syntax Error - *error = kParseErrorUnmatchingCloseBracket; - return fs; - } - - if (releaseBlockIndex >= 0) - { - // if repeat end is already defined: Repetition After Release Error - *error = kParseErrorRepeatingInReleaseBlock; - return fs; - } - - repeatEndIndex = i; - } - } - - // if (releaseBlockIndex < 0) { // "|" didn't explicitly specified - // releaseBlockIndex = trimmed.length(); - // } - - if (openBracketCount != closeBracketCount) - { - *error = kParseErrorUnmatchingBracketNumber; - return fs; - } - - if (releaseBlockIndex - repeatEndIndex > 1) - { - // throw new FrameSequenceParseException(TRANS("Elements between repeat block and release block will be ignored"), false); - // FiXME: non-fatal exceptionをどう扱うか - } + SegmentIndexes si = findSegment(trimmed); // Just for convenience - bool hasRelease = (releaseBlockIndex >= 0); - bool shouldRepeat = (repeatStartIndex >= 0); + bool hasRelease = (si.releaseBlockIndex >= 0); + bool shouldRepeat = (si.repeatStartIndex >= 0); //----------------------------------- // @@ -369,32 +420,7 @@ FrameSequence FrameSequenceParser::parse (const String& input, String str_insideRepeat; String str_release; - if (shouldRepeat) - { - if (hasRelease) - { - str_beforeRepeat = trimmed.substring (0, repeatStartIndex - 1); - } - - str_insideRepeat = trimmed.substring (repeatStartIndex, repeatEndIndex); - } - else - { - if (hasRelease) - { - str_beforeRepeat = trimmed.substring (0, releaseBlockIndex - 1); - str_release = trimmed.substring (releaseBlockIndex, trimmed.length()); - } - else - { - str_beforeRepeat = trimmed.substring (0, trimmed.length()); - } - } - - std::cout << "before repeat : " + str_beforeRepeat + "\n"; - std::cout << "inside repeat : " + str_insideRepeat + "\n"; - std::cout << "after release : " + str_release + "\n"; - + splitSegment(trimmed, si, str_beforeRepeat, str_insideRepeat, str_release); //----------------------------------- // diff --git a/Source/FrameSequenceParser.h b/Source/FrameSequenceParser.h index 67c8cc0..420e6de 100644 --- a/Source/FrameSequenceParser.h +++ b/Source/FrameSequenceParser.h @@ -13,7 +13,41 @@ #include "FrameSequence.h" #include "FrameSequenceParseErrors.h" +struct SegmentIndexes { + static const int NONE = -1; + + int releaseBlockIndex = NONE; + int repeatStartIndex = NONE; + int repeatEndIndex = NONE; + ParseError error; +}; + struct FrameSequenceParser { + /* + Public + */ FrameSequence parse (const String& input, int minValue, int maxValue, ParseError* error); + + /* + Semantically private (leave them open for unit testing) + */ + std::vector parseSlope (const String& input, + int minValue, + int maxValue, + ParseError* error); + std::vector parseHold (const String& input, + int minValue, + int maxValue, + ParseError* error); + std::vector parseSegment (const String& input, + int minValue, + int maxValue, + ParseError* error); + void splitSegment (const String& input, + SegmentIndexes indexes, + String& beforeRepeat, + String& insideRepeat, + String& afterRelease); + SegmentIndexes findSegment(const String& input); }; diff --git a/Source/PluginProcessor.cpp b/Source/PluginProcessor.cpp index 270ea3b..27b41f7 100644 --- a/Source/PluginProcessor.cpp +++ b/Source/PluginProcessor.cpp @@ -14,6 +14,7 @@ #include "TriangleVoice.h" #include "NoiseVoice.h" #include "FrameSequenceParseErrors.h" +#include "EnvelopeParserTest.h" //============================================================================== Magical8bitPlug2AudioProcessor::Magical8bitPlug2AudioProcessor() @@ -114,7 +115,7 @@ Magical8bitPlug2AudioProcessor::Magical8bitPlug2AudioProcessor() std::make_unique ("isPitchSequenceEnabled_raw", "Enabled", false), std::make_unique ("isDutySequenceEnabled_raw", "Enabled", false), std::make_unique ("pitchSequenceMode_raw", "Mode", StringArray ({"Coarse", "Fine"}), 0) -} + } ) , settingRefs (¶meters) #ifndef JucePlugin_PreferredChannelConfigurations @@ -132,6 +133,12 @@ Magical8bitPlug2AudioProcessor::Magical8bitPlug2AudioProcessor() setupVoice(); synth.addSound (new GenericSound()); + +#if JUCE_DEBUG + EnvelopeParserTest test; + UnitTestRunner runner; + runner.runAllTests(); +#endif } Magical8bitPlug2AudioProcessor::~Magical8bitPlug2AudioProcessor()