Previous topic

music21.variant

Next topic

music21.vexflow.exporter

Table Of Contents

Table Of Contents

This Page

music21.vexflow

Objects for transcribing music21 objects as VexFlow code

Here’s the hierarchy:

A VexflowContext can be used to display multiple VexflowParts. Each VexflowPart contains multiple VexflowStaves (one for each measure) Each VexflowStave might contain multiple VexflowVoices

Functions

music21.vexflow.fromChord(thisChord, mode='txt')

Parses a music21 chord into VexFlow code:

>>> a = chord.Chord(['C3', 'E-3', 'G3', 'C4'])
>>> print(vexflow.fromChord(a, mode='txt'))
new Vex.Flow.StaveNote({keys: ["Cn/3", "Eb/3", "Gn/3", "Cn/4"], duration: "q"})
>>> print(vexflow.fromChord(a, mode='jsbody'))
var canvas = $('#music21canvas')[0];
var renderer = new Vex.Flow.Renderer(canvas, Vex.Flow.Renderer.Backends.CANVAS);
var ctx = renderer.getContext();
var stave = new Vex.Flow.Stave(10,0,500);
stave.addClef('treble').setContext(ctx).draw();
var notes = [new Vex.Flow.StaveNote({keys: ["Cn/3", "Eb/3", "Gn/3", "Cn/4"], duration: "q"})];
var voice = new Vex.Flow.Voice({
    num_beats: 1.0,
    beat_value: 4,
    resolution: Vex.Flow.RESOLUTION
});
voice.addTickables(notes);
var formatter = new Vex.Flow.Formatter()
formatter.joinVoices([voice])
formatter.format([voice], 500);
voice.draw(ctx, stave);
>>> print(vexflow.fromChord(a, mode='html'))
<!DOCTYPE HTML>
<html>
<head>
    <meta name='author' content='Music21' />
    <script src='http://code.jquery.com/jquery-latest.js'></script>
    <script src='http://www.vexflow.com/support/vexflow-min.js'></script>
</head>
<body>
    <canvas width="525" height="120" id='music21canvas'></canvas>
    <script>
        $(document).ready(function(){
            var canvas = $('#music21canvas')[0];
            var renderer = new Vex.Flow.Renderer(canvas, Vex.Flow.Renderer.Backends.CANVAS);
            var ctx = renderer.getContext();
            var stave = new Vex.Flow.Stave(10,0,500);
            stave.addClef('treble').setContext(ctx).draw();
            var notes = [new Vex.Flow.StaveNote({keys: ["Cn/3", "Eb/3", "Gn/3", "Cn/4"], duration: "q"})];
            var voice = new Vex.Flow.Voice({
                num_beats: 1.0,
                beat_value: 4,
                resolution: Vex.Flow.RESOLUTION
            });
            voice.addTickables(notes);
            var formatter = new Vex.Flow.Formatter()
            formatter.joinVoices([voice])
            formatter.format([voice], 500);
            voice.draw(ctx, stave);
        });
    </script>
</body>
</html>
music21.vexflow.fromMeasure(thisMeasure, mode='txt')

Parses a music21 measure into VexFlow code:

>>> b = corpus.parse('bwv1.6.mxl')
>>> m = b.parts[0].measures(0,1)[2]
>>> d = vexflow.fromMeasure(m)
>>> print(d)
var music21Voice0 = new Vex.Flow.Voice({num_beats: 4.0, beat_value: 4, resolution: Vex.Flow.RESOLUTION});
var music21Voice0Notes = [new Vex.Flow.StaveNote({keys: ["Gn/4"], duration: "8", stem_direction: Vex.Flow.StaveNote.STEM_UP}), new Vex.Flow.StaveNote({keys: ["Cn/4"], duration: "8", stem_direction: Vex.Flow.StaveNote.STEM_UP}), new Vex.Flow.StaveNote({keys: ["Fn/4"], duration: "8", stem_direction: Vex.Flow.StaveNote.STEM_UP}), new Vex.Flow.StaveNote({keys: ["Fn/3"], duration: "8", stem_direction: Vex.Flow.StaveNote.STEM_UP}), new Vex.Flow.StaveNote({keys: ["An/3"], duration: "8", stem_direction: Vex.Flow.StaveNote.STEM_UP}), new Vex.Flow.StaveNote({keys: ["Fn/3"], duration: "8", stem_direction: Vex.Flow.StaveNote.STEM_UP}), new Vex.Flow.StaveNote({keys: ["An/3"], duration: "8", stem_direction: Vex.Flow.StaveNote.STEM_UP}), new Vex.Flow.StaveNote({keys: ["Cn/4"], duration: "8", stem_direction: Vex.Flow.StaveNote.STEM_UP})];
music21Voice0.addTickables(music21Voice0Notes);
>>> c = vexflow.fromMeasure(m, 'html')
music21.vexflow.fromNote(thisNote, mode='txt')

Parses a music21 note into VexFlow string code:

>>> print(vexflow.fromNote(note.Note('C4'), mode='txt'))
new Vex.Flow.StaveNote({keys: ["Cn/4"], duration: "q"})

See VexFlowNote.generateCode() for an example of mode=’html’.

music21.vexflow.fromObject(thisObject, mode='txt')

Attempts to translate an arbitrary Music21Object into vexflow

Able to translate anything in vexflow.supportedMusic21Classes

TODO: Unit Tests (one for each supportedMusic21Class)

>>> print(vexflow.fromObject(note.Note('C4')))
new Vex.Flow.StaveNote({keys: ["Cn/4"], duration: "q"})
>>> print(vexflow.fromObject(pitch.Pitch('C4')))
new Vex.Flow.StaveNote({keys: ["Cn/4"], duration: "q"})
>>> print(vexflow.fromObject(note.Rest()))
new Vex.Flow.StaveNote({keys: ["b/4"], duration: "qr"})
>>> print(vexflow.fromObject(chord.Chord(['C4', 'E-4', 'G4'])))
new Vex.Flow.StaveNote({keys: ["Cn/4", "Eb/4", "Gn/4"], duration: "q"})
>>> bwv666 = corpus.parse('bwv66.6')
>>> soprano = bwv666.parts[0]
>>> measure1 = soprano.getElementsByClass('Measure')[0]
>>> trebleVoice = bwv666.partsToVoices()[1][1][0]
>>> bwv666
<music21.stream.Score ...>
>>> soprano
<music21.stream.Part Soprano>
>>> measure1
<music21.stream.Measure 0 offset=0.0>
>>> trebleVoice
<music21.stream.Voice 0>
>>> print(vexflow.fromObject(measure1))
var music21Voice0 = new Vex.Flow.Voice({num_beats: 1.0, beat_value: 4, resolution: Vex.Flow.RESOLUTION});
var music21Voice0Notes = [new Vex.Flow.StaveNote({keys: ["C#/5"], duration: "8", stem_direction: Vex.Flow.StaveNote.STEM_DOWN}), new Vex.Flow.StaveNote({keys: ["Bn/4"], duration: "8", stem_direction: Vex.Flow.StaveNote.STEM_DOWN})];
music21Voice0.addTickables(music21Voice0Notes);
>>> print(vexflow.fromObject(trebleVoice))
var music21Voice0 = new Vex.Flow.Voice({num_beats: 4.0, beat_value: 4, resolution: Vex.Flow.RESOLUTION});
var music21Voice0Notes = [new Vex.Flow.StaveNote({keys: ["An/4"], duration: "q", stem_direction: Vex.Flow.StaveNote.STEM_UP}), new Vex.Flow.StaveNote({keys: ["Bn/4"], duration: "q", stem_direction: Vex.Flow.StaveNote.STEM_DOWN}), new Vex.Flow.StaveNote({keys: ["C#/5"], duration: "q", stem_direction: Vex.Flow.StaveNote.STEM_DOWN}).addArticulation(0, new Vex.Flow.Articulation("a@a").setPosition(3)), new Vex.Flow.StaveNote({keys: ["En/5"], duration: "q", stem_direction: Vex.Flow.StaveNote.STEM_DOWN})];
music21Voice0.addTickables(music21Voice0Notes);
>>> #print vexflow.fromObject(soprano)
>>> #print vexflow.fromObject(bwv666)
>>> #print vexflow.fromObject(tinyNotation.TinyNotationStream("3/4 E4 r f# g=lastG b-8 a g c4~ c"), mode='txt') 
music21.vexflow.fromPart(thisPart, mode='txt')

Parses a music21 part into VexFlow code:

>>> a = corpus.parse('bwv66.6').parts[1]
>>> textOut = vexflow.fromPart(a, mode='txt')
music21.vexflow.fromRest(thisRest, mode='txt')

Parses a music21 rest into VexFlow code:

>>> a = note.Rest()
>>> print(vexflow.fromRest(a, mode='txt'))
new Vex.Flow.StaveNote({keys: ["b/4"], duration: "qr"})
>>> print(vexflow.fromRest(a, mode='html'))
<!DOCTYPE HTML>
<html>
<head>
    <meta name='author' content='Music21' />
    <script src='http://code.jquery.com/jquery-latest.js'></script>
    <script src='http://www.vexflow.com/support/vexflow-min.js'></script>
</head>
<body>
    <canvas width="525" height="120" id='music21canvas'></canvas>
    <script>
        $(document).ready(function(){
            var canvas = $('#music21canvas')[0];
            var renderer = new Vex.Flow.Renderer(canvas, Vex.Flow.Renderer.Backends.CANVAS);
            var ctx = renderer.getContext();
            var stave = new Vex.Flow.Stave(10,0,500);
            stave.addClef('treble').setContext(ctx).draw();
            var notes = [new Vex.Flow.StaveNote({keys: ["b/4"], duration: "qr"})];
            var voice = new Vex.Flow.Voice({
                num_beats: 1.0,
                beat_value: 4,
                resolution: Vex.Flow.RESOLUTION
            });
            voice.addTickables(notes);
            var formatter = new Vex.Flow.Formatter()
            formatter.joinVoices([voice])
            formatter.format([voice], 500);
            voice.draw(ctx, stave);
        });
</script>
</body>
</html>
music21.vexflow.fromScore(thisScore, mode='txt')

Parses a music21 score into VexFlow code

>>> a = corpus.parse('bwv66.6')
>>> #print vexflow.fromScore(a, mode='txt') 
music21.vexflow.fromStream(thisStream, mode='txt')

Parses a music21 stream into VexFlow code

Checks if it has parts. If so, parses like a Score. Otherwise, just flattens it and parses it like a Part

>>> #print vexflow.fromStream(tinyNotation.TinyNotationStream('c8 d8 e-4 dd4 cc2'), mode='txt')
>>> #print vexflow.fromStream(tinyNotation.TinyNotationStream('C8 D8 E-4 d4 c2'), mode='txt')
music21.vexflow.staffString(xPosStr='10', yPosStr='0', staffWidth='500', staveName='stave')

Returns a string formated new VexFlow Stave.

Arguments are strings representing the x and y position and the width.

They are strings because a Javascript function can be used in lieu of the number.

>>> vexflow.staffString()
'var stave = new Vex.Flow.Stave(10,0,500);'
>>> vexflow.staffString('(0 * (((($(window).width()-10) - (2*10))) / 4) + 10)', '((0 * ((4 * (90 + 20)) + 60)) + 10 + (0*(90+20)))', '(((($(window).width()-10) - (2*10))) / 4)')
'var stave = new Vex.Flow.Stave((0 * (((($(window).width()-10) - (2*10))) / 4) + 10),((0 * ((4 * (90 + 20)) + 60)) + 10 + (0*(90+20))),(((($(window).width()-10) - (2*10))) / 4));'
music21.vexflow.vexflowClefFromClef(music21clef)

Given a music21 clef object, returns the vexflow clef:

>>> vexflow.vexflowClefFromClef(clef.TrebleClef())
'treble'
>>> vexflow.vexflowClefFromClef(clef.BassClef())
'bass'
>>> vexflow.vexflowClefFromClef(clef.TenorClef())
'tenor'
>>> vexflow.vexflowClefFromClef(clef.AltoClef())
'alto'
>>> vexflow.vexflowClefFromClef(clef.Treble8vbClef())
'bass'
>>> vexflow.vexflowClefFromClef(clef.PercussionClef())
'percussion'
>>> vexflow.vexflowClefFromClef(clef.GClef())
'treble'

UIDCounter

class music21.vexflow.UIDCounter(UIDStart=0)

A generic counter object for keeping track of the number of objects used:

>>> uidc = vexflow.UIDCounter(UIDStart = 20)
>>> uidc.UID
20
>>> uidc.readAndIncrement()
20
>>> uidc.readAndIncrement()
21
>>> uidc.UID
22

UIDCounter methods

UIDCounter.readAndIncrement()

VexflowChord

class music21.vexflow.VexflowChord(music21Chord=None, clef=None)

A simultaneous grouping of notes.

TODO: __str__ should just call the str() method on the original notes TODO: Also store original notes as Chord object TODO: write unit tests

VexflowChord bases

VexflowChord methods

VexflowChord.vexflowCode()

Returns a string showing the vexflow code needed to display this chord in a browser.

Methods inherited from VexflowObject:

VexflowContext

class music21.vexflow.VexflowContext(params={}, canvasName=None)

Contains information about the canvas, formatter, and renderer.

VexflowContext methods

VexflowContext.generateHTML(applyAttributes=False)

Generates the HTML for the canvas and stores it in self.canvasHTML.

Note

End users should use the getCanvasHTML() method.

If applyAttributes is True, then apply the attributes in the
HTML code instead of in the Javascript

Note

There is no checking that the values aren’t also set in Javascript.

VexflowContext.generateJS(applyAttributes=True)

Generates the Javascript to set up the canvas for VexFlow and stores it in self.canvasJSCode, self.rendererCode, and self.contextCode.

Note

End users should use the get methods.

If applyAttributes is True, then apply the attributes in the Javascript instead of the HTML.

Note

Applying the attributes in Javascript will overwrite any attributes set in the HTML.

VexflowContext.getCanvasCode(indentation=1, cache=True, applyAttributes=True)
VexflowContext.getCanvasHTML(cache=True, applyAttributes=False)
VexflowContext.getHeight()
VexflowContext.getJSCode(indentation=1, cache=True, applyAttributes=True)
VexflowContext.getRenderContextCode(indentation=1, cache=True, applyAttributes=True)
VexflowContext.getWidth()
VexflowContext.setHeight(height)
VexflowContext.setWidth(width)

VexflowNote

class music21.vexflow.VexflowNote(music21note=None, clef=None)

A VexflowNote object has a .generateCode() method which produces VexFlow code to represent a music21.note.Note object.

TODO: Verify that I’m not overwriting any base attribute names of music21

TODO: add setters/getters

TODO: __str__

>>> n = note.Note('C-')
>>> v = vexflow.VexflowNote(n)
>>> v.vexflowKey()
'Cb/4'
>>> v.vexflowDuration()
'q'
>>> v.vexflowCode()
'new Vex.Flow.StaveNote({keys: ["Cb/4"], duration: "q"})'
>>> n = tinyNotation.TinyNotationNote('c##2.').note
>>> n.stemDirection = 'up'
>>> v = vexflow.VexflowNote(n)
>>> v.vexflowKey()
'C##/4'
>>> v.vexflowDuration()
'hd'
>>> v.stemDirectionCode()
'stem_direction: Vex.Flow.StaveNote.STEM_UP'
>>> v.vexflowCode()
'new Vex.Flow.StaveNote({keys: ["C##/4"], duration: "hd", stem_direction: Vex.Flow.StaveNote.STEM_UP}).addDotToAll()'

VexflowNote bases

VexflowNote methods

VexflowNote.vexflowCode()

returns a string representing the vexflow code for this note

Methods inherited from VexflowObject:

VexflowObject

class music21.vexflow.VexflowObject(music21Object=None, clef=None)

A general class for all VexflowObjects to inherit from.

See specific objects such as VexflowNote, VexflowChord, and VexflowRest for more details.

VexflowObject methods

VexflowObject.accidentalCode(pitch=None, index=0)

Returns code to add an accidental to a Vex.Flow.Note or key.

Index refers to the pitch within a chord to be altered (0 for notes).

VexflowObject.dotCode()

Generates VexFlow code for the number of dots in the object.

Currently Vexflow’s layout engine only supports single dotted notes however!

>>> n = note.Note()
>>> n.duration.dots = 1
>>> vn = vexflow.VexflowNote(n)
>>> vn.dotCode()
'.addDotToAll()'
VexflowObject.fermataCode()

Returns a string of Vexflow code if there is a fermata and ‘’ if not.

>>> n = note.Note()
>>> n.expressions.append(expressions.Fermata())
>>> vn = vexflow.VexflowNote(n)
>>> vn.fermataCode()
'.addArticulation(0, new Vex.Flow.Articulation("a@a").setPosition(3))'
VexflowObject.generateCode(mode='txt')

Returns the VexFlow code for a single note in the desired display mode.

Currently supported modes are txt (returns the VexFlow code which can be used in conjunction with other VexFlow code) and html (returns standalone HTML code for displaying just this note.)

>>> n = note.Note('C-')
>>> v = vexflow.VexflowNote(n)
>>> v.generateCode('txt')
'new Vex.Flow.StaveNote({keys: ["Cb/4"], duration: "q"})'
>>> print(v.generateCode('jsbody'))
var canvas = $('#music21canvas')[0];
var renderer = new Vex.Flow.Renderer(canvas, Vex.Flow.Renderer.Backends.CANVAS);
var ctx = renderer.getContext();
var stave = new Vex.Flow.Stave(10,0,500);
stave.addClef('treble').setContext(ctx).draw();
var notes = [new Vex.Flow.StaveNote({keys: ["Cb/4"], duration: "q"})];
var voice = new Vex.Flow.Voice({
    num_beats: 1.0,
    beat_value: 4,
    resolution: Vex.Flow.RESOLUTION
});
voice.addTickables(notes);
var formatter = new Vex.Flow.Formatter()
formatter.joinVoices([voice])
formatter.format([voice], 500);
voice.draw(ctx, stave);
>>> print(v.generateCode('html'))
<!DOCTYPE HTML>
<html>
<head>
    <meta name='author' content='Music21' />
    <script src='http://code.jquery.com/jquery-latest.js'></script>
    <script src='http://www.vexflow.com/support/vexflow-min.js'></script>
</head>
<body>
    <canvas width="525" height="120" id='music21canvas'></canvas>
    <script>
        $(document).ready(function(){
            var canvas = $('#music21canvas')[0];
            var renderer = new Vex.Flow.Renderer(canvas, Vex.Flow.Renderer.Backends.CANVAS);
            var ctx = renderer.getContext();
            var stave = new Vex.Flow.Stave(10,0,500);
            stave.addClef('treble').setContext(ctx).draw();
            var notes = [new Vex.Flow.StaveNote({keys: ["Cb/4"], duration: "q"})];
            var voice = new Vex.Flow.Voice({
                num_beats: 1.0,
                beat_value: 4,
                resolution: Vex.Flow.RESOLUTION
            });
            voice.addTickables(notes);
            var formatter = new Vex.Flow.Formatter()
            formatter.joinVoices([voice])
            formatter.format([voice], 500);
            voice.draw(ctx, stave);
        });
    </script>
</body>
</html>
VexflowObject.getVoiceString(numBeats, voiceName='voice', currentIndentLevel=3)

Returns a string creating a new Vex.Flow.Voice with the number of beats at the current indentation level:

>>> vo = vexflow.VexflowObject()
>>> print(vo.getVoiceString(2.0).rstrip())
        var voice = new Vex.Flow.Voice({
            num_beats: 2.0,
            beat_value: 4,
            resolution: Vex.Flow.RESOLUTION
        });
>>> print(vo.getVoiceString(3.0, voiceName='myVoice', currentIndentLevel = 0).rstrip())
var myVoice = new Vex.Flow.Voice({
    num_beats: 3.0,
    beat_value: 4,
    resolution: Vex.Flow.RESOLUTION
});
VexflowObject.jsBodyCode()
VexflowObject.setBeamStatus()

Set the beamStatus for a note by setting beamStart to True, beamStop to True, or neither.

VexflowObject.setTieStatus()

Set the tieStatus for a note by setting tieStart to True, tieStop to True, or neither.

VexflowObject.staveDefaultClefAddString()
VexflowObject.stemDirectionCode()

Gets the Vexflow StemDirection String:

>>> n = note.Note()
>>> vfn = vexflow.VexflowNote(n)
>>> vfn.stemDirectionCode()
''
>>> n.stemDirection = 'up'
>>> vfn.stemDirectionCode()
'stem_direction: Vex.Flow.StaveNote.STEM_UP'
VexflowObject.vexflowDuration()

Given a music21 Note (or Pitch) object, returns the vexflow duration.

>>> n = note.Note()
>>> vfn = vexflow.VexflowNote(n)
>>> vfn.vexflowDuration()
'q'
>>> n.quarterLength = 0.75
>>> vfn.vexflowDuration()
'8d'
VexflowObject.vexflowKey(clef=None, usePitch=None)

Generate a VexFlow Key from self.originalObject.

Since a VexFlow key is the position on a staff, we need to know the current clef in order to give a key. The key is a position as if the clef were treble clef.

Why the accidental is included is beyond me, since it is rendered separately, it might store information actual pitch, but that’s weird since we’re storing a position as if it’s treble clef.

If usePitch is given then we use this pitch instead of self.originalObject, which is needed for iterating through chord pitches

If clef is None then we look at self.clefContext for more information, otherwise treble is assumed. Clef is a Vexflow clef name #TODO: CHANGE THIS!

::
>>> vfn1 = vexflow.VexflowNote(note.Note('C4'))
>>> vfn1
<music21.vexflow.VexflowNote object at 0x...>
>>> vfn1.vexflowKey() #'treble')
'Cn/4'
>>> vfn2 = vexflow.VexflowNote(note.Note('C4'))
>>> vfn2.vexflowKey('bass')
'An/5'
>>> c = chord.Chord(['C4','G#4','E-5'])
>>> vfc = vexflow.VexflowChord(c)
>>> vfc.vexflowKey('treble', c.pitches[0])
'Cn/4'
>>> vfc.vexflowKey('treble', c.pitches[1])
'G#/4'
>>> vfc.vexflowKey('treble', c.pitches[2])
'Eb/5'

VexflowPart

class music21.vexflow.VexflowPart(music21part, params={})

A part is a wrapper for the vexflow code representing multiple measures of music that should go in the same musical staff (as opposed to a vexflow Stave).

VexflowPart methods

VexflowPart.beamCode(contextName, indentation=3)

Generates the code for beaming all of the staves in this part.

Returns as an array containing the preamble and postamble.

VexflowPart.generateCode(mode='txt')

Generates the vexflow code necessary to display this part in a browser.

VexflowPart.jsBodyCode()

VexflowRest

class music21.vexflow.VexflowRest(music21rest=None, clef=None)

Class for representing rests in VexFlow.

VexflowRest bases

VexflowRest methods

VexflowRest.vexflowCode()

Returns a string which is the generated the vexflow code needed to render this rest object.

>>> r = note.Rest()
>>> vr = vexflow.VexflowRest(r)
>>> vr.vexflowCode()
'new Vex.Flow.StaveNote({keys: ["b/4"], duration: "qr"})'

Methods inherited from VexflowObject:

VexflowScore

class music21.vexflow.VexflowScore(music21score, params={})

Represents the code for multiple VexflowPart objects.

VexflowScore methods

VexflowScore.generateCode(mode='txt')

Returns the vexflow code needed to render this object in a browser.

VexflowScore.jsBodyCode()
VexflowScore.vexflowCode()

Generates the code necessary to display this score.

VexflowStave

class music21.vexflow.VexflowStave(params={})

A “Stave”[sic] in VexFlow is the object for the graphic staff to be displayed. It usually represents a Measure that might have one or more Voices on it.

TODO: generateCode should take a VexflowContext object as a param

VexflowStave methods

VexflowStave.beamCode(contextName, indentation=3)

Generates the code for beaming all of the voices on this stave.

Returns an array containing the preamble and postamble.

VexflowStave.formatterCode()

Code for setting up a formatter to join voices.

VexflowStave.generateCode(mode='txt')

Generates the vexflow code to display this staff in a browser.

VexflowStave.getLineNum()

Tries to get the line number of this stave.

VexflowStave.getWidth()
VexflowStave.jsBodyCode()
VexflowStave.setVoices(theseVexflowVoices)

Replaces any existing voices attached to this Stave with theseVexflowVoices (a list of instances of VexflowVoice).

VexflowStave.staveCode()

JavaScript/VexFlow code for putting clefs, timeSignatures, etc. on the staff.

VexflowStave.vexflowCode()

VexflowVoice

class music21.vexflow.VexflowVoice(music21measure=None, params={})

A Voice in VexFlow is a “lateral” grouping of notes in one measure It’s the equivalent to a Measure.

Requires either a Measure object or a Voice object.

If those objects aren’t already flat, flattens them in the process.

params is a dict containing various parameters to be passed to the voice object. Most important is the UIDCounter parameter which keeps track of the number of objects created.

VexflowVoice methods

VexflowVoice.createBeamCode(contextName='ctx', indentation=3)

Returns the code to create beams for this staff.

VexflowVoice.drawBeamCode(contextName='ctx', indentation=3)

Returns the code to draw beams on this staff.

VexflowVoice.generateCode(mode='txt')

Returns the vexflow code necessary to display this Voice in a browser as a string.

>>> m = stream.Measure()
>>> m.append(note.Note('c4', type='half'))
>>> vfv = vexflow.VexflowVoice(m)
>>> print(vfv.generateCode(mode="jsbody"))
var canvas = $(‘#music21canvas’)[0]; var renderer = new Vex.Flow.Renderer(canvas, Vex.Flow.Renderer.Backends.CANVAS); var ctx = renderer.getContext(); var stave = new Vex.Flow.Stave(10,0,500); stave.addClef(‘treble’).setContext(ctx).draw(); var music21Voice0 = new Vex.Flow.Voice({num_beats: 2.0, beat_value: 4, resolution: Vex.Flow.RESOLUTION}); var music21Voice0Notes = [new Vex.Flow.StaveNote({keys: [“Cn/4”], duration: “h”})]; music21Voice0.addTickables(music21Voice0Notes);
<BLANKLINE>
var formatter = new Vex.Flow.Formatter().joinVoices([music21Voice0]).format([music21Voice0], 500); music21Voice0.draw(ctx, stave);

<BLANKLINE>

VexflowVoice.getBeaming()

Beaming is a boolean value determining if the voice should be beamed

Note

So far only VexFlow’s automatic beaming is supported. We cannot manually modify beams.

VexflowVoice.jsBodyCode()
VexflowVoice.notesCode()

Note the plural. Generates an String that is a Javascript array of all the vexflow notes in a measure:

>>> s = stream.Measure()
>>> s.append(note.Note('c4'))
>>> s.append(note.Note('d4'))
>>> vfv = vexflow.VexflowVoice(s)
>>> vfv.voiceName = 'myVoice'
>>> vfv.notesCode()
'var myVoiceNotes = [new Vex.Flow.StaveNote({keys: ["Cn/4"], duration: "q"}), new Vex.Flow.StaveNote({keys: ["Dn/4"], duration: "q"})];'
VexflowVoice.setBeaming(beaming)

Beaming is a boolean value determining if the voice should be beamed

Note

So far, only VexFlow’s automatic beaming is supported. We cannot manually modify beams.

VexflowVoice.tieCode(contextName='ctx', indentation=3)

Returns the code for the ties for this voice.

Returns it as a two-element tuple containing [0] a list of code for the completed ties within this voice, and [1] a two-element array for partial ties that go across the bar line consisting of (0) a two-element tuple of the start index, and end index (None), and (1) the string name of the Note group to which element [0][0] belongs.

N.B. Bug: Only the first index of a chord is tied. Returns a string that generates the code necessary to display this voice.

>>> s = stream.Measure()
>>> n1 = note.Note('c4')
>>> n1.tie = tie.Tie("start")
>>> n2 = note.Note('c4')
>>> n2.tie = tie.Tie("stop")
>>> s.append(n1)
>>> s.append(n2)
>>> vfv = vexflow.VexflowVoice(s)
>>> vfv.voiceName = 'myVoice'
>>> print(vfv.tieCode()[0][0])
    var myVoiceTie0 = new Vex.Flow.StaveTie({
        first_note: myVoiceNotes[0],
        last_note: myVoiceNotes[1],
        first_indices: [0],
        last_indices: [0]
    });
    myVoiceTie0.setContext(ctx).draw();
VexflowVoice.vexflowCode()

Returns a string that generates the code necessary to display this voice.

>>> s = stream.Measure()
>>> s.append(note.Note('c4'))
>>> s.append(note.Note('d4'))
>>> vfv = vexflow.VexflowVoice(s)
>>> vfv.voiceName = 'myVoice'
>>> print(vfv.vexflowCode())
var myVoice = new Vex.Flow.Voice({num_beats: 2.0, beat_value: 4, resolution: Vex.Flow.RESOLUTION});
var myVoiceNotes = [new Vex.Flow.StaveNote({keys: ["Cn/4"], duration: "q"}), new Vex.Flow.StaveNote({keys: ["Dn/4"], duration: "q"})];
myVoice.addTickables(myVoiceNotes);
VexflowVoice.vexflowObjects()

Returns a list of all the notesAndRests in the originalMeasure represented as VexflowObjects.

>>> s = stream.Measure()
>>> s.append(note.Note('c4'))
>>> s.append(note.Note('d4'))
>>> vfv = vexflow.VexflowVoice(s)
>>> vfv.vexflowObjects()
[<music21.vexflow.VexflowNote object at 0x...>, <music21.vexflow.VexflowNote object at 0x...>]
VexflowVoice.voiceCode()

Creates the code to create a new voice object with the name stored in self.voiceName.

>>> s = stream.Measure()
>>> s.append(note.Note('c4'))
>>> s.append(note.Note('d4'))
>>> vfv = vexflow.VexflowVoice(s)
>>> vfv.voiceName = 'myVoice'
>>> vfv.voiceCode()
'var myVoice = new Vex.Flow.Voice({num_beats: 2.0, beat_value: 4, resolution: Vex.Flow.RESOLUTION});'