Skip to content

Commit

Permalink
Merge pull request #2 from cgamache/duration-fix
Browse files Browse the repository at this point in the history
Updated duration generator to handle any duration value
  • Loading branch information
incompleteopus committed Nov 5, 2013
2 parents 6ee0548 + 2cc9d1b commit 64d48f3
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 57 deletions.
78 changes: 22 additions & 56 deletions src/vexabc-common.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,66 +57,32 @@ VexAbc.Util.findEqualDataNotePitchIndices = function(dataNotes1, dataNotes2, fro
return result;
}

VexAbc.Def.fractionDurationToVexFlowDuration = {};

// TODO: This kind of array definition is really inflexible, so find a better way to deduce duration!
VexAbc.Def.fractionDurationToVexFlowDuration[VexAbc.Def.DURATION_RESOLUTION / 1] =
{ value: "1", dots: 0 };
VexAbc.Def.fractionDurationToVexFlowDuration[VexAbc.Def.DURATION_RESOLUTION / 2 + VexAbc.Def.DURATION_RESOLUTION / 4 + VexAbc.Def.DURATION_RESOLUTION / 8 + VexAbc.Def.DURATION_RESOLUTION / 16] =
{ value: "2", dots: 3 };
VexAbc.Def.fractionDurationToVexFlowDuration[VexAbc.Def.DURATION_RESOLUTION / 2 + VexAbc.Def.DURATION_RESOLUTION / 4 + VexAbc.Def.DURATION_RESOLUTION / 8] =
{ value: "2", dots: 2 };
VexAbc.Def.fractionDurationToVexFlowDuration[VexAbc.Def.DURATION_RESOLUTION / 2 + VexAbc.Def.DURATION_RESOLUTION / 4] =
{ value: "2", dots: 1 };
VexAbc.Def.fractionDurationToVexFlowDuration[VexAbc.Def.DURATION_RESOLUTION / 2] =
{ value: "2", dots: 0 };
VexAbc.Def.fractionDurationToVexFlowDuration[VexAbc.Def.DURATION_RESOLUTION / 4 + VexAbc.Def.DURATION_RESOLUTION / 8 + VexAbc.Def.DURATION_RESOLUTION / 16 + VexAbc.Def.DURATION_RESOLUTION / 32] =
{ value: "4", dots: 3 };
VexAbc.Def.fractionDurationToVexFlowDuration[VexAbc.Def.DURATION_RESOLUTION / 4 + VexAbc.Def.DURATION_RESOLUTION / 8 + VexAbc.Def.DURATION_RESOLUTION / 16] =
{ value: "4", dots: 2 };
VexAbc.Def.fractionDurationToVexFlowDuration[VexAbc.Def.DURATION_RESOLUTION / 4 + VexAbc.Def.DURATION_RESOLUTION / 8] =
{ value: "4", dots: 1 };
VexAbc.Def.fractionDurationToVexFlowDuration[VexAbc.Def.DURATION_RESOLUTION / 4] =
{ value: "4", dots: 0 };
VexAbc.Def.fractionDurationToVexFlowDuration[VexAbc.Def.DURATION_RESOLUTION / 8 + VexAbc.Def.DURATION_RESOLUTION / 16 + VexAbc.Def.DURATION_RESOLUTION / 32 + VexAbc.Def.DURATION_RESOLUTION / 64] =
{ value: "8", dots: 3 };
VexAbc.Def.fractionDurationToVexFlowDuration[VexAbc.Def.DURATION_RESOLUTION / 8 + VexAbc.Def.DURATION_RESOLUTION / 16 + VexAbc.Def.DURATION_RESOLUTION / 32] =
{ value: "8", dots: 2 };
VexAbc.Def.fractionDurationToVexFlowDuration[VexAbc.Def.DURATION_RESOLUTION / 8 + VexAbc.Def.DURATION_RESOLUTION / 16] =
{ value: "8", dots: 1 };
VexAbc.Def.fractionDurationToVexFlowDuration[VexAbc.Def.DURATION_RESOLUTION / 8] =
{ value: "8", dots: 0 };
VexAbc.Def.fractionDurationToVexFlowDuration[VexAbc.Def.DURATION_RESOLUTION / 16 + VexAbc.Def.DURATION_RESOLUTION / 32 + VexAbc.Def.DURATION_RESOLUTION / 64 + VexAbc.Def.DURATION_RESOLUTION / 128] =
{ value: "16", dots: 3 };
VexAbc.Def.fractionDurationToVexFlowDuration[VexAbc.Def.DURATION_RESOLUTION / 16 + VexAbc.Def.DURATION_RESOLUTION / 32 + VexAbc.Def.DURATION_RESOLUTION / 64] =
{ value: "16", dots: 2 };
VexAbc.Def.fractionDurationToVexFlowDuration[VexAbc.Def.DURATION_RESOLUTION / 16 + VexAbc.Def.DURATION_RESOLUTION / 32] =
{ value: "16", dots: 1 };
VexAbc.Def.fractionDurationToVexFlowDuration[VexAbc.Def.DURATION_RESOLUTION / 16] =
{ value: "16", dots: 0 };
VexAbc.Def.fractionDurationToVexFlowDuration[VexAbc.Def.DURATION_RESOLUTION / 32 + VexAbc.Def.DURATION_RESOLUTION / 64 + VexAbc.Def.DURATION_RESOLUTION / 128 + VexAbc.Def.DURATION_RESOLUTION / 256] =
{ value: "32", dots: 3 };
VexAbc.Def.fractionDurationToVexFlowDuration[VexAbc.Def.DURATION_RESOLUTION / 32 + VexAbc.Def.DURATION_RESOLUTION / 64 + VexAbc.Def.DURATION_RESOLUTION / 128] =
{ value: "32", dots: 2 };
VexAbc.Def.fractionDurationToVexFlowDuration[VexAbc.Def.DURATION_RESOLUTION / 32 + VexAbc.Def.DURATION_RESOLUTION / 64] =
{ value: "32", dots: 1 };
VexAbc.Def.fractionDurationToVexFlowDuration[VexAbc.Def.DURATION_RESOLUTION / 32] =
{ value: "32", dots: 0 };
VexAbc.Def.fractionDurationToVexFlowDuration[VexAbc.Def.DURATION_RESOLUTION / 64 + VexAbc.Def.DURATION_RESOLUTION / 128 + VexAbc.Def.DURATION_RESOLUTION / 256 + VexAbc.Def.DURATION_RESOLUTION / 512] =
{ value: "64", dots: 3 };
VexAbc.Def.fractionDurationToVexFlowDuration[VexAbc.Def.DURATION_RESOLUTION / 64 + VexAbc.Def.DURATION_RESOLUTION / 128 + VexAbc.Def.DURATION_RESOLUTION / 256] =
{ value: "64", dots: 2 };
VexAbc.Def.fractionDurationToVexFlowDuration[VexAbc.Def.DURATION_RESOLUTION / 64 + VexAbc.Def.DURATION_RESOLUTION / 128] =
{ value: "64", dots: 1 };
VexAbc.Def.fractionDurationToVexFlowDuration[VexAbc.Def.DURATION_RESOLUTION / 64] =
{ value: "64", dots: 0 };

// TODO: How to handle unknown durations?
VexAbc.Util.fractionDurationToVexFlowDuration = function(noteValue) {

var factor = Math.log(VexAbc.Def.DURATION_RESOLUTION) / Math.LN2;

var pow = Math.floor(Math.log(noteValue) / Math.LN2);

var value = Math.pow(2,factor - pow), remainder = noteValue - Math.pow(2,pow), dots = 0;

while (remainder > 0) {
remainder = remainder - Math.pow(2, pow - 1 - dots);
dots = dots + 1;
}

if (value < 1) {
return null;
} else {
return {value: value.toString(), dots: dots};
}

};

VexAbc.Util.convertFractionDurationToVexFlowDuration = function(multiplier, noteValue) {
var factor = VexAbc.Def.DURATION_RESOLUTION / noteValue;
var durationTicks = multiplier * factor;

var result = VexAbc.Def.fractionDurationToVexFlowDuration[durationTicks];
var result = VexAbc.Util.fractionDurationToVexFlowDuration(durationTicks);
if (!result) {
return null;
}
Expand Down
50 changes: 50 additions & 0 deletions test/test-data.js
Original file line number Diff line number Diff line change
Expand Up @@ -448,3 +448,53 @@ moduleDefinitions.push({
]
});

testDefinitions.push({name: "Durations",
tests: [
{
name: "Standard Durations",
test: function() {
deepEqual(VexAbc.Util.fractionDurationToVexFlowDuration(VexAbc.Def.DURATION_RESOLUTION / 1),{ value: "1", dots: 0 },"Whole");
deepEqual(VexAbc.Util.fractionDurationToVexFlowDuration(VexAbc.Def.DURATION_RESOLUTION / 2 + VexAbc.Def.DURATION_RESOLUTION / 4 + VexAbc.Def.DURATION_RESOLUTION / 8 + VexAbc.Def.DURATION_RESOLUTION / 16),{ value: "2", dots: 3 },"Triple-Dotted Half");
deepEqual(VexAbc.Util.fractionDurationToVexFlowDuration(VexAbc.Def.DURATION_RESOLUTION / 2 + VexAbc.Def.DURATION_RESOLUTION / 4 + VexAbc.Def.DURATION_RESOLUTION / 8),{ value: "2", dots: 2 },"Double-Dotted Half");
deepEqual(VexAbc.Util.fractionDurationToVexFlowDuration(VexAbc.Def.DURATION_RESOLUTION / 2 + VexAbc.Def.DURATION_RESOLUTION / 4),{ value: "2", dots: 1 },"Dotted Half");
deepEqual(VexAbc.Util.fractionDurationToVexFlowDuration(VexAbc.Def.DURATION_RESOLUTION / 2),{ value: "2", dots: 0 },"Half");
deepEqual(VexAbc.Util.fractionDurationToVexFlowDuration(VexAbc.Def.DURATION_RESOLUTION / 4 + VexAbc.Def.DURATION_RESOLUTION / 8 + VexAbc.Def.DURATION_RESOLUTION / 16 + VexAbc.Def.DURATION_RESOLUTION / 32),{ value: "4", dots: 3 },"Triple-Dotted Quarter");
deepEqual(VexAbc.Util.fractionDurationToVexFlowDuration(VexAbc.Def.DURATION_RESOLUTION / 4 + VexAbc.Def.DURATION_RESOLUTION / 8 + VexAbc.Def.DURATION_RESOLUTION / 16),{ value: "4", dots: 2 },"Double-Dotted Quarter");
deepEqual(VexAbc.Util.fractionDurationToVexFlowDuration(VexAbc.Def.DURATION_RESOLUTION / 4 + VexAbc.Def.DURATION_RESOLUTION / 8),{ value: "4", dots: 1 },"Dotted Quarter");
deepEqual(VexAbc.Util.fractionDurationToVexFlowDuration(VexAbc.Def.DURATION_RESOLUTION / 4),{ value: "4", dots: 0 },"Quarter");
deepEqual(VexAbc.Util.fractionDurationToVexFlowDuration(VexAbc.Def.DURATION_RESOLUTION / 8 + VexAbc.Def.DURATION_RESOLUTION / 16 + VexAbc.Def.DURATION_RESOLUTION / 32 + VexAbc.Def.DURATION_RESOLUTION / 64),{ value: "8", dots: 3 },"Triple-Dotted Eighth");
deepEqual(VexAbc.Util.fractionDurationToVexFlowDuration(VexAbc.Def.DURATION_RESOLUTION / 8 + VexAbc.Def.DURATION_RESOLUTION / 16 + VexAbc.Def.DURATION_RESOLUTION / 32),{ value: "8", dots: 2 },"Double-Dotted Eighth");
deepEqual(VexAbc.Util.fractionDurationToVexFlowDuration(VexAbc.Def.DURATION_RESOLUTION / 8 + VexAbc.Def.DURATION_RESOLUTION / 16 ),{ value: "8", dots: 1 },"Dotted Eighth");
deepEqual(VexAbc.Util.fractionDurationToVexFlowDuration(VexAbc.Def.DURATION_RESOLUTION / 8),{ value: "8", dots: 0 },"Eighth");
deepEqual(VexAbc.Util.fractionDurationToVexFlowDuration(VexAbc.Def.DURATION_RESOLUTION / 16 + VexAbc.Def.DURATION_RESOLUTION / 32 + VexAbc.Def.DURATION_RESOLUTION / 64 + VexAbc.Def.DURATION_RESOLUTION / 128),{ value: "16", dots: 3 },"Triple-Dotted Sixteenth");
deepEqual(VexAbc.Util.fractionDurationToVexFlowDuration(VexAbc.Def.DURATION_RESOLUTION / 16 + VexAbc.Def.DURATION_RESOLUTION / 32 + VexAbc.Def.DURATION_RESOLUTION / 64),{ value: "16", dots: 2 },"Double-Dotted Sixteenth");
deepEqual(VexAbc.Util.fractionDurationToVexFlowDuration(VexAbc.Def.DURATION_RESOLUTION / 16 + VexAbc.Def.DURATION_RESOLUTION / 32),{ value: "16", dots: 1 },"Dotted Sixteenth");
deepEqual(VexAbc.Util.fractionDurationToVexFlowDuration(VexAbc.Def.DURATION_RESOLUTION / 16),{ value: "16", dots: 0 },"Sixteenth");
deepEqual(VexAbc.Util.fractionDurationToVexFlowDuration(VexAbc.Def.DURATION_RESOLUTION / 32 + VexAbc.Def.DURATION_RESOLUTION / 64 + VexAbc.Def.DURATION_RESOLUTION / 128 + VexAbc.Def.DURATION_RESOLUTION / 256),{ value: "32", dots: 3 },"Triple-Dotted Thirtysecond");
deepEqual(VexAbc.Util.fractionDurationToVexFlowDuration(VexAbc.Def.DURATION_RESOLUTION / 32 + VexAbc.Def.DURATION_RESOLUTION / 64 + VexAbc.Def.DURATION_RESOLUTION / 128),{ value: "32", dots: 2 },"Double-Dotted Thirtysecond");
deepEqual(VexAbc.Util.fractionDurationToVexFlowDuration(VexAbc.Def.DURATION_RESOLUTION / 32 + VexAbc.Def.DURATION_RESOLUTION / 64),{ value: "32", dots: 1 },"Dotted Thirtysecond");
deepEqual(VexAbc.Util.fractionDurationToVexFlowDuration(VexAbc.Def.DURATION_RESOLUTION / 32),{ value: "32", dots: 0 },"Thirtysecond");
deepEqual(VexAbc.Util.fractionDurationToVexFlowDuration(VexAbc.Def.DURATION_RESOLUTION / 64 + VexAbc.Def.DURATION_RESOLUTION / 128 + VexAbc.Def.DURATION_RESOLUTION / 256 + VexAbc.Def.DURATION_RESOLUTION / 512),{ value: "64", dots: 3 },"Triple-Dotted Sixtyfourth");
deepEqual(VexAbc.Util.fractionDurationToVexFlowDuration(VexAbc.Def.DURATION_RESOLUTION / 64 + VexAbc.Def.DURATION_RESOLUTION / 128 + VexAbc.Def.DURATION_RESOLUTION / 256),{ value: "64", dots: 2 },"Double-Dotted Sixtyfourth");
deepEqual(VexAbc.Util.fractionDurationToVexFlowDuration(VexAbc.Def.DURATION_RESOLUTION / 64 + VexAbc.Def.DURATION_RESOLUTION / 128),{ value: "64", dots: 1 },"Dotted Sixtyfourth");
deepEqual(VexAbc.Util.fractionDurationToVexFlowDuration(VexAbc.Def.DURATION_RESOLUTION / 64),{ value: "64", dots: 0 },"Sixtyfourth");
}
},
{
name: "Irregular durations",
test: function() {
deepEqual(VexAbc.Util.fractionDurationToVexFlowDuration(VexAbc.Def.DURATION_RESOLUTION / 2 + VexAbc.Def.DURATION_RESOLUTION / 4 + VexAbc.Def.DURATION_RESOLUTION / 8 + VexAbc.Def.DURATION_RESOLUTION / 16 +
VexAbc.Def.DURATION_RESOLUTION / 32 + VexAbc.Def.DURATION_RESOLUTION / 64 + VexAbc.Def.DURATION_RESOLUTION / 128 + VexAbc.Def.DURATION_RESOLUTION / 256 +
VexAbc.Def.DURATION_RESOLUTION / 512 + VexAbc.Def.DURATION_RESOLUTION / 1024 + VexAbc.Def.DURATION_RESOLUTION / 2048 + VexAbc.Def.DURATION_RESOLUTION / 4096
),{ value: "2", dots: 11 },"Eleven dots");
deepEqual(VexAbc.Util.fractionDurationToVexFlowDuration(VexAbc.Def.DURATION_RESOLUTION - 1),{ value: "2", dots: 17},"Seventeen dots");
}
},
{
name: "Invalid durations",
test: function() {
deepEqual(VexAbc.Util.fractionDurationToVexFlowDuration(VexAbc.Def.DURATION_RESOLUTION * -1),{ value: "NaN", dots: 0 },"Negative duration");
deepEqual(VexAbc.Util.fractionDurationToVexFlowDuration(0),{ value: "Infinity", dots: 0 },"Zero duration");
}
}]
});
4 changes: 4 additions & 0 deletions test/test.html
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@

<script>
var moduleDefinitions = [];
var testDefinitions = [];
</script>

<script src="test-data.js"></script>
Expand All @@ -45,6 +46,9 @@
return;
}

for (i = 0; i < testDefinitions.length; i++) {
testSuite.registerTest(testDefinitions[i]);
}
for (i = 0; i < moduleDefinitions.length; i++) {
testSuite.registerModule(moduleDefinitions[i]);
}
Expand Down
19 changes: 18 additions & 1 deletion test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,32 @@ VexAbc.TestSuite = function(parserDefinitionText, testOutputSelector) {
this.pegParser = PEG.buildParser(parserDefinitionText);
this.testOutputSelector = testOutputSelector;
this.modules = [];
this.tests = [];
this.testId = 0;
}

VexAbc.TestSuite.prototype.registerModule = function(moduleDefinition) {
this.modules.push(moduleDefinition);
}

VexAbc.TestSuite.prototype.registerTest = function(tests) {
this.tests.push(tests);
}

VexAbc.TestSuite.prototype.run = function() {
var i;
var i,j;

for (i = 0; i < this.tests.length; i++) {
var testDefinition = this.tests[i];

this.testId++;
VexAbc.Util.fractionDurationToVexFlowDuration
var subTests = testDefinition.tests;
for (j = 0; j < subTests.length; j++) {
test(testDefinition.name + ": " + subTests[j].name, subTests[j].test);
}
}

for (i = 0; i < this.modules.length; i++) {
var moduleDefinition = this.modules[i];
this.runModule(moduleDefinition);
Expand Down

0 comments on commit 64d48f3

Please sign in to comment.