Skip to content

Commit

Permalink
Added tremelo support.
Browse files Browse the repository at this point in the history
Signed-off-by: Mike Corrigan <corrigan@gmail.com>
  • Loading branch information
CaveMike authored and 0xfe committed Mar 6, 2012
1 parent f745565 commit 7b26818
Show file tree
Hide file tree
Showing 6 changed files with 115 additions and 2 deletions.
1 change: 1 addition & 0 deletions src/SConstruct
Expand Up @@ -52,6 +52,7 @@ vexflow_sources = [
"stavevolta.js",
"staverepetition.js",
"stavesection.js",
"tremelo.js",
]

Import('dbg opt')
Expand Down
2 changes: 1 addition & 1 deletion src/fonts/vexflow_font.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/transform.html
Expand Up @@ -63,7 +63,7 @@
"v44": true, "v45": true, "v46": true, "v47": true, "v49": true,
"v4a": true, "v4d": true, "v4e": true, "v52": true,
"v54": true, "v55": true, "v58": true, "v59": true, "v5b": true, "v5c": true,
"v62": true, "v70": true, "v72": true, "v75": true,
"v62": true, "v70": true, "v72": true, "v74": true, "v75": true,
"v79": true, "v7c": true, "v7d": true, "v7f": true, "v80": true,
"v81": true, "v83": true, "v84": true, "v8b": true,
"v8c": true, "v8f": true, "v92": true, "v93": true, "v94": true,
Expand Down
59 changes: 59 additions & 0 deletions src/tremelo.js
@@ -0,0 +1,59 @@
// VexFlow - Music Engraving for HTML5
// Copyright Mike Corrigan 2012 <corrigan@gmail.com>
//
// This class implements tremelo notation.

/**
* @constructor
*/
Vex.Flow.Tremelo = function(num) {
if (arguments.length > 0) this.init(num);
}
Vex.Flow.Tremelo.prototype = new Vex.Flow.Modifier();
Vex.Flow.Tremelo.prototype.constructor = Vex.Flow.Tremelo;
Vex.Flow.Tremelo.superclass = Vex.Flow.Modifier.prototype;

Vex.Flow.Tremelo.prototype.init = function(num) {
var superclass = Vex.Flow.Tremelo.superclass;
superclass.init.call(this);

this.num = num;
this.note = null;
this.index = null;
this.position = Vex.Flow.Modifier.Position.CENTER;
this.code = "v74";
this.shift_right = -2;
this.y_spacing = 4;

this.render_options = {
font_scale: 35,
stroke_px: 3,
stroke_spacing: 10
};

this.font = {
family: "Arial",
size: 16,
weight: ""
};
}

Vex.Flow.Tremelo.prototype.getCategory = function() { return "tremelo"; }

Vex.Flow.Tremelo.prototype.draw = function() {
if (!this.context) throw new Vex.RERR("NoContext",
"Can't draw Tremelo without a context.");
if (!(this.note && (this.index != null))) throw new Vex.RERR("NoAttachedNote",
"Can't draw Tremelo without a note and index.");

var start = this.note.getModifierStartXY(this.position, this.index);
var x = start.x;
var y = start.y;

x += this.shift_right;
for (var i = 0; i < this.num; ++i) {
Vex.Flow.renderGlyph(this.context, x, y,
this.render_options.font_scale, this.code);
y += this.y_spacing;
}
}
1 change: 1 addition & 0 deletions tests/flow.html
Expand Up @@ -100,6 +100,7 @@
<script src="../src/staverepetition.js"></script>
<script src="../src/stavesection.js"></script>
<script src="../src/articulation.js"></script>
<script src="../src/tremelo.js"></script>
<script src="../src/raphaelcontext.js"></script>

<!-- Compiled Source -->
Expand Down
52 changes: 52 additions & 0 deletions tests/percussion_tests.js
Expand Up @@ -24,6 +24,8 @@ Vex.Flow.Test.Percussion.Start = function() {
Vex.Flow.Test.Percussion.drawSnare0);
Vex.Flow.Test.Percussion.runBoth("Percussion Snare1",
Vex.Flow.Test.Percussion.drawSnare1);
Vex.Flow.Test.Percussion.runBoth("Percussion Snare2",
Vex.Flow.Test.Percussion.drawSnare2);
}

Vex.Flow.Test.Percussion.runBoth = function(title, func) {
Expand All @@ -40,6 +42,10 @@ Vex.Flow.Test.Percussion.newArticulation = function(s) {
return new Vex.Flow.Articulation(s).setPosition(Vex.Flow.Modifier.Position.ABOVE);
}

Vex.Flow.Test.Percussion.newTremelo = function(s) {
return new Vex.Flow.Tremelo(s);
}

Vex.Flow.Test.Percussion.draw = function(options, contextBuilder) {
var ctx = new contextBuilder(options.canvas_sel, 400, 120);

Expand Down Expand Up @@ -386,3 +392,49 @@ Vex.Flow.Test.Percussion.drawSnare1 = function(options, contextBuilder) {

ok(true, "");
}

Vex.Flow.Test.Percussion.drawSnare2 = function(options, contextBuilder) {
var ctx = contextBuilder(options.canvas_sel, 600, 120);
ctx.scale(0.9, 0.9); ctx.fillStyle = "#221"; ctx.strokeStyle = "#221";
ctx.setFont("Arial", 15, "");

x = 10;
y = 10;
w = 280;

{
var stave = new Vex.Flow.Stave(x, y, w);
stave.setBegBarType(Vex.Flow.Barline.type.REPEAT_BEGIN);
stave.setEndBarType(Vex.Flow.Barline.type.SINGLE);
stave.addClef("percussion");
stave.setContext(ctx);
stave.draw();

notesDown = [
new Vex.Flow.StaveNote({ keys: ["c/5"], duration: "q",
stem_direction: -1 }).
addArticulation(0, Vex.Flow.Test.Percussion.newTremelo(0)),
new Vex.Flow.StaveNote({ keys: ["c/5"], duration: "q",
stem_direction: -1 }).
addArticulation(0, Vex.Flow.Test.Percussion.newTremelo(1)),
new Vex.Flow.StaveNote({ keys: ["c/5"], duration: "q",
stem_direction: -1 }).
addArticulation(0, Vex.Flow.Test.Percussion.newTremelo(3)),
new Vex.Flow.StaveNote({ keys: ["c/5"], duration: "q",
stem_direction: -1 }).
addArticulation(0, Vex.Flow.Test.Percussion.newTremelo(5)),
];
var voiceDown = new Vex.Flow.Voice({ num_beats: 4, beat_value: 4,
resolution: Vex.Flow.RESOLUTION });
voiceDown.addTickables(notesDown);

var formatter = new Vex.Flow.Formatter().
joinVoices([voiceDown]).formatToStave([voiceDown], stave);

voiceDown.draw(ctx, stave);

x += stave.width;
}

ok(true, "");
}

0 comments on commit 7b26818

Please sign in to comment.