vexflow
Version:
A JavaScript library for rendering music notation and guitar tablature
1,254 lines (796 loc) • 48.7 kB
HTML
<html>
<head>
<title>note.js</title>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<link rel="stylesheet" media="all" href="public/stylesheets/normalize.css" />
<link rel="stylesheet" media="all" href="docco.css" />
</head>
<body>
<div class="container">
<div class="page">
<div class="header">
<h1>note.js</h1>
<div class="toc">
<h3>Table of Contents</h3>
<ol>
<li>
<a class="source" href="accidental.html">
accidental.js
</a>
</li>
<li>
<a class="source" href="annotation.html">
annotation.js
</a>
</li>
<li>
<a class="source" href="articulation.html">
articulation.js
</a>
</li>
<li>
<a class="source" href="barnote.html">
barnote.js
</a>
</li>
<li>
<a class="source" href="beam.html">
beam.js
</a>
</li>
<li>
<a class="source" href="bend.html">
bend.js
</a>
</li>
<li>
<a class="source" href="boundingbox.html">
boundingbox.js
</a>
</li>
<li>
<a class="source" href="boundingboxcomputation.html">
boundingboxcomputation.js
</a>
</li>
<li>
<a class="source" href="canvascontext.html">
canvascontext.js
</a>
</li>
<li>
<a class="source" href="clef.html">
clef.js
</a>
</li>
<li>
<a class="source" href="clefnote.html">
clefnote.js
</a>
</li>
<li>
<a class="source" href="crescendo.html">
crescendo.js
</a>
</li>
<li>
<a class="source" href="curve.html">
curve.js
</a>
</li>
<li>
<a class="source" href="dot.html">
dot.js
</a>
</li>
<li>
<a class="source" href="easyscore.html">
easyscore.js
</a>
</li>
<li>
<a class="source" href="element.html">
element.js
</a>
</li>
<li>
<a class="source" href="factory.html">
factory.js
</a>
</li>
<li>
<a class="source" href="formatter.html">
formatter.js
</a>
</li>
<li>
<a class="source" href="fraction.html">
fraction.js
</a>
</li>
<li>
<a class="source" href="frethandfinger.html">
frethandfinger.js
</a>
</li>
<li>
<a class="source" href="ghostnote.html">
ghostnote.js
</a>
</li>
<li>
<a class="source" href="glyph.html">
glyph.js
</a>
</li>
<li>
<a class="source" href="glyphnote.html">
glyphnote.js
</a>
</li>
<li>
<a class="source" href="gracenote.html">
gracenote.js
</a>
</li>
<li>
<a class="source" href="gracenotegroup.html">
gracenotegroup.js
</a>
</li>
<li>
<a class="source" href="gracetabnote.html">
gracetabnote.js
</a>
</li>
<li>
<a class="source" href="index.html">
index.js
</a>
</li>
<li>
<a class="source" href="keymanager.html">
keymanager.js
</a>
</li>
<li>
<a class="source" href="keysignature.html">
keysignature.js
</a>
</li>
<li>
<a class="source" href="keysignote.html">
keysignote.js
</a>
</li>
<li>
<a class="source" href="modifier.html">
modifier.js
</a>
</li>
<li>
<a class="source" href="modifiercontext.html">
modifiercontext.js
</a>
</li>
<li>
<a class="source" href="multimeasurerest.html">
multimeasurerest.js
</a>
</li>
<li>
<a class="source" href="music.html">
music.js
</a>
</li>
<li>
<a class="source" href="note.html">
note.js
</a>
</li>
<li>
<a class="source" href="notehead.html">
notehead.js
</a>
</li>
<li>
<a class="source" href="notesubgroup.html">
notesubgroup.js
</a>
</li>
<li>
<a class="source" href="ornament.html">
ornament.js
</a>
</li>
<li>
<a class="source" href="parser.html">
parser.js
</a>
</li>
<li>
<a class="source" href="pedalmarking.html">
pedalmarking.js
</a>
</li>
<li>
<a class="source" href="raphaelcontext.html">
raphaelcontext.js
</a>
</li>
<li>
<a class="source" href="registry.html">
registry.js
</a>
</li>
<li>
<a class="source" href="renderer.html">
renderer.js
</a>
</li>
<li>
<a class="source" href="repeatnote.html">
repeatnote.js
</a>
</li>
<li>
<a class="source" href="smufl.html">
smufl.js
</a>
</li>
<li>
<a class="source" href="stave.html">
stave.js
</a>
</li>
<li>
<a class="source" href="stavebarline.html">
stavebarline.js
</a>
</li>
<li>
<a class="source" href="staveconnector.html">
staveconnector.js
</a>
</li>
<li>
<a class="source" href="stavehairpin.html">
stavehairpin.js
</a>
</li>
<li>
<a class="source" href="staveline.html">
staveline.js
</a>
</li>
<li>
<a class="source" href="stavemodifier.html">
stavemodifier.js
</a>
</li>
<li>
<a class="source" href="stavenote.html">
stavenote.js
</a>
</li>
<li>
<a class="source" href="staverepetition.html">
staverepetition.js
</a>
</li>
<li>
<a class="source" href="stavesection.html">
stavesection.js
</a>
</li>
<li>
<a class="source" href="stavetempo.html">
stavetempo.js
</a>
</li>
<li>
<a class="source" href="stavetext.html">
stavetext.js
</a>
</li>
<li>
<a class="source" href="stavetie.html">
stavetie.js
</a>
</li>
<li>
<a class="source" href="stavevolta.html">
stavevolta.js
</a>
</li>
<li>
<a class="source" href="stem.html">
stem.js
</a>
</li>
<li>
<a class="source" href="stemmablenote.html">
stemmablenote.js
</a>
</li>
<li>
<a class="source" href="stringnumber.html">
stringnumber.js
</a>
</li>
<li>
<a class="source" href="strokes.html">
strokes.js
</a>
</li>
<li>
<a class="source" href="svgcontext.html">
svgcontext.js
</a>
</li>
<li>
<a class="source" href="system.html">
system.js
</a>
</li>
<li>
<a class="source" href="tables.html">
tables.js
</a>
</li>
<li>
<a class="source" href="tabnote.html">
tabnote.js
</a>
</li>
<li>
<a class="source" href="tabslide.html">
tabslide.js
</a>
</li>
<li>
<a class="source" href="tabstave.html">
tabstave.js
</a>
</li>
<li>
<a class="source" href="tabtie.html">
tabtie.js
</a>
</li>
<li>
<a class="source" href="textbracket.html">
textbracket.js
</a>
</li>
<li>
<a class="source" href="textdynamics.html">
textdynamics.js
</a>
</li>
<li>
<a class="source" href="textnote.html">
textnote.js
</a>
</li>
<li>
<a class="source" href="tickable.html">
tickable.js
</a>
</li>
<li>
<a class="source" href="tickcontext.html">
tickcontext.js
</a>
</li>
<li>
<a class="source" href="timesignature.html">
timesignature.js
</a>
</li>
<li>
<a class="source" href="timesignote.html">
timesignote.js
</a>
</li>
<li>
<a class="source" href="tremolo.html">
tremolo.js
</a>
</li>
<li>
<a class="source" href="tuning.html">
tuning.js
</a>
</li>
<li>
<a class="source" href="tuplet.html">
tuplet.js
</a>
</li>
<li>
<a class="source" href="vex.html">
vex.js
</a>
</li>
<li>
<a class="source" href="vibrato.html">
vibrato.js
</a>
</li>
<li>
<a class="source" href="vibratobracket.html">
vibratobracket.js
</a>
</li>
<li>
<a class="source" href="voice.html">
voice.js
</a>
</li>
<li>
<a class="source" href="voicegroup.html">
voicegroup.js
</a>
</li>
</ol>
</div>
</div>
<p><a href="http://vexflow.com">VexFlow</a> - Copyright (c) Mohit Muthanna 2010.</p>
<h2 id="description">Description</h2>
<p>This file implements an abstract interface for notes and chords that
are rendered on a stave. Notes have some common properties: All of them
have a value (e.g., pitch, fret, etc.) and a duration (quarter, half, etc.)</p>
<p>Some notes have stems, heads, dots, etc. Most notational elements that
surround a note are called <em>modifiers</em>, and every note has an associated
array of them. All notes also have a rendering context and belong to a stave.</p>
<div class='highlight'><pre>
<span class="hljs-keyword">import</span> { Vex } <span class="hljs-keyword">from</span> <span class="hljs-string">'./vex'</span>;
<span class="hljs-keyword">import</span> { Flow } <span class="hljs-keyword">from</span> <span class="hljs-string">'./tables'</span>;
<span class="hljs-keyword">import</span> { Tickable } <span class="hljs-keyword">from</span> <span class="hljs-string">'./tickable'</span>;
<span class="hljs-keyword">export</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Note</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">Tickable</span> </span>{
<span class="hljs-keyword">static</span> get CATEGORY() { <span class="hljs-keyword">return</span> <span class="hljs-string">'note'</span>; }</pre></div>
<p>Debug helper. Displays various note metrics for the given
note.</p>
<div class='highlight'><pre> <span class="hljs-keyword">static</span> plotMetrics(ctx, note, yPos) {
<span class="hljs-keyword">const</span> metrics = note.getMetrics();
<span class="hljs-keyword">const</span> xStart = note.getAbsoluteX() - metrics.modLeftPx - metrics.leftDisplacedHeadPx;
<span class="hljs-keyword">const</span> xPre1 = note.getAbsoluteX() - metrics.leftDisplacedHeadPx;
<span class="hljs-keyword">const</span> xAbs = note.getAbsoluteX();
<span class="hljs-keyword">const</span> xPost1 = note.getAbsoluteX() + metrics.notePx;
<span class="hljs-keyword">const</span> xPost2 = note.getAbsoluteX() + metrics.notePx + metrics.rightDisplacedHeadPx;
<span class="hljs-keyword">const</span> xEnd = note.getAbsoluteX()
+ metrics.notePx
+ metrics.rightDisplacedHeadPx
+ metrics.modRightPx;
<span class="hljs-keyword">const</span> xFreedomRight = xEnd + (note.getFormatterMetrics().freedom.right || <span class="hljs-number">0</span>);
<span class="hljs-keyword">const</span> xWidth = xEnd - xStart;
ctx.save();
ctx.setFont(<span class="hljs-string">'Arial'</span>, <span class="hljs-number">8</span>, <span class="hljs-string">''</span>);
ctx.fillText(<span class="hljs-built_in">Math</span>.round(xWidth) + <span class="hljs-string">'px'</span>, xStart + note.getXShift(), yPos);
<span class="hljs-keyword">const</span> y = (yPos + <span class="hljs-number">7</span>);
<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">stroke</span>(<span class="hljs-params">x1, x2, color, yy = y</span>) </span>{
ctx.beginPath();
ctx.setStrokeStyle(color);
ctx.setFillStyle(color);
ctx.setLineWidth(<span class="hljs-number">3</span>);
ctx.moveTo(x1 + note.getXShift(), yy);
ctx.lineTo(x2 + note.getXShift(), yy);
ctx.stroke();
}
stroke(xStart, xPre1, <span class="hljs-string">'red'</span>);
stroke(xPre1, xAbs, <span class="hljs-string">'#999'</span>);
stroke(xAbs, xPost1, <span class="hljs-string">'green'</span>);
stroke(xPost1, xPost2, <span class="hljs-string">'#999'</span>);
stroke(xPost2, xEnd, <span class="hljs-string">'red'</span>);
stroke(xEnd, xFreedomRight, <span class="hljs-string">'#DD0'</span>);
stroke(xStart - note.getXShift(), xStart, <span class="hljs-string">'#BBB'</span>); <span class="hljs-comment">// Shift</span>
Vex.drawDot(ctx, xAbs + note.getXShift(), y, <span class="hljs-string">'blue'</span>);
<span class="hljs-keyword">const</span> formatterMetrics = note.getFormatterMetrics();
<span class="hljs-keyword">if</span> (formatterMetrics.iterations > <span class="hljs-number">0</span>) {
<span class="hljs-keyword">const</span> spaceDeviation = formatterMetrics.space.deviation;
<span class="hljs-keyword">const</span> prefix = spaceDeviation >= <span class="hljs-number">0</span> ? <span class="hljs-string">'+'</span> : <span class="hljs-string">''</span>;
ctx.setFillStyle(<span class="hljs-string">'red'</span>);
ctx.fillText(prefix + <span class="hljs-built_in">Math</span>.round(spaceDeviation),
xAbs + note.getXShift(), yPos - <span class="hljs-number">10</span>);
}
ctx.restore();
}
<span class="hljs-keyword">static</span> parseDuration(durationString) {
<span class="hljs-keyword">if</span> (<span class="hljs-keyword">typeof</span> (durationString) !== <span class="hljs-string">'string'</span>) { <span class="hljs-keyword">return</span> <span class="hljs-literal">null</span>; }
<span class="hljs-keyword">const</span> regexp = <span class="hljs-regexp">/(\d*\/?\d+|[a-z])(d*)([nrhms]|$)/</span>;
<span class="hljs-keyword">const</span> result = regexp.exec(durationString);
<span class="hljs-keyword">if</span> (!result) { <span class="hljs-keyword">return</span> <span class="hljs-literal">null</span>; }
<span class="hljs-keyword">const</span> duration = result[<span class="hljs-number">1</span>];
<span class="hljs-keyword">const</span> dots = result[<span class="hljs-number">2</span>].length;
<span class="hljs-keyword">const</span> type = result[<span class="hljs-number">3</span>] || <span class="hljs-string">'n'</span>;
<span class="hljs-keyword">return</span> { duration, dots, type };
}
<span class="hljs-keyword">static</span> parseNoteStruct(noteStruct) {
<span class="hljs-keyword">const</span> durationString = noteStruct.duration;
<span class="hljs-keyword">const</span> customTypes = [];</pre></div>
<p>Preserve backwards-compatibility</p>
<div class='highlight'><pre> <span class="hljs-keyword">const</span> durationProps = Note.parseDuration(durationString);
<span class="hljs-keyword">if</span> (!durationProps) { <span class="hljs-keyword">return</span> <span class="hljs-literal">null</span>; }</pre></div>
<p>If specified type is invalid, return null</p>
<div class='highlight'><pre> <span class="hljs-keyword">let</span> type = noteStruct.type;
<span class="hljs-keyword">if</span> (type && !Flow.getGlyphProps.validTypes[type]) { <span class="hljs-keyword">return</span> <span class="hljs-literal">null</span>; }</pre></div>
<p>If no type specified, check duration or custom types</p>
<div class='highlight'><pre> <span class="hljs-keyword">if</span> (!type) {
type = durationProps.type || <span class="hljs-string">'n'</span>;</pre></div>
<p>If we have keys, try and check if we’ve got a custom glyph</p>
<div class='highlight'><pre> <span class="hljs-keyword">if</span> (noteStruct.keys !== <span class="hljs-literal">undefined</span>) {
noteStruct.keys.forEach(<span class="hljs-function">(<span class="hljs-params">k, i</span>) =></span> {
<span class="hljs-keyword">const</span> result = k.split(<span class="hljs-string">'/'</span>);</pre></div>
<p>We have a custom glyph specified after the note eg. /X2</p>
<div class='highlight'><pre> customTypes[i] = (result && result.length === <span class="hljs-number">3</span>) ? result[<span class="hljs-number">2</span>] : type;
});
}
}</pre></div>
<p>Calculate the tick duration of the note</p>
<div class='highlight'><pre> <span class="hljs-keyword">let</span> ticks = Flow.durationToTicks(durationProps.duration);
<span class="hljs-keyword">if</span> (ticks == <span class="hljs-literal">null</span>) { <span class="hljs-keyword">return</span> <span class="hljs-literal">null</span>; }</pre></div>
<p>Are there any dots?</p>
<div class='highlight'><pre> <span class="hljs-keyword">const</span> dots = noteStruct.dots ? noteStruct.dots : durationProps.dots;
<span class="hljs-keyword">if</span> (<span class="hljs-keyword">typeof</span> (dots) !== <span class="hljs-string">'number'</span>) { <span class="hljs-keyword">return</span> <span class="hljs-literal">null</span>; }</pre></div>
<p>Add ticks as necessary depending on the numbr of dots</p>
<div class='highlight'><pre> <span class="hljs-keyword">let</span> currentTicks = ticks;
<span class="hljs-keyword">for</span> (<span class="hljs-keyword">let</span> i = <span class="hljs-number">0</span>; i < dots; i++) {
<span class="hljs-keyword">if</span> (currentTicks <= <span class="hljs-number">1</span>) <span class="hljs-keyword">return</span> <span class="hljs-literal">null</span>;
currentTicks = currentTicks / <span class="hljs-number">2</span>;
ticks += currentTicks;
}
<span class="hljs-keyword">return</span> {
<span class="hljs-attr">duration</span>: durationProps.duration,
type,
customTypes,
dots,
ticks,
};
}</pre></div>
<p>Every note is a tickable, i.e., it can be mutated by the <code>Formatter</code> class for
positioning and layout.
To create a new note you need to provide a <code>noteStruct</code>, which consists
of the following fields:</p>
<p><code>type</code>: The note type (e.g., <code>r</code> for rest, <code>s</code> for slash notes, etc.)
<code>dots</code>: The number of dots, which affects the duration.
<code>duration</code>: The time length (e.g., <code>q</code> for quarter, <code>h</code> for half, <code>8</code> for eighth etc.)</p>
<p>The range of values for these parameters are available in <code>src/tables.js</code>.</p>
<div class='highlight'><pre> <span class="hljs-keyword">constructor</span>(noteStruct) {
<span class="hljs-keyword">super</span>();
<span class="hljs-keyword">this</span>.setAttribute(<span class="hljs-string">'type'</span>, <span class="hljs-string">'Note'</span>);
<span class="hljs-keyword">if</span> (!noteStruct) {
<span class="hljs-keyword">throw</span> <span class="hljs-keyword">new</span> Vex.RuntimeError(
<span class="hljs-string">'BadArguments'</span>, <span class="hljs-string">'Note must have valid initialization data to identify duration and type.'</span>
);
}</pre></div>
<p>Parse <code>noteStruct</code> and get note properties.</p>
<div class='highlight'><pre> <span class="hljs-keyword">const</span> initStruct = Note.parseNoteStruct(noteStruct);
<span class="hljs-keyword">if</span> (!initStruct) {
<span class="hljs-keyword">throw</span> <span class="hljs-keyword">new</span> Vex.RuntimeError(
<span class="hljs-string">'BadArguments'</span>, <span class="hljs-string">`Invalid note initialization object: <span class="hljs-subst">${<span class="hljs-built_in">JSON</span>.stringify(noteStruct)}</span>`</span>
);
}</pre></div>
<p>Set note properties from parameters.</p>
<div class='highlight'><pre> <span class="hljs-keyword">this</span>.duration = initStruct.duration;
<span class="hljs-keyword">this</span>.dots = initStruct.dots;
<span class="hljs-keyword">this</span>.noteType = initStruct.type;
<span class="hljs-keyword">this</span>.customTypes = initStruct.customTypes;
<span class="hljs-keyword">if</span> (noteStruct.duration_override) {</pre></div>
<p>Custom duration</p>
<div class='highlight'><pre> <span class="hljs-keyword">this</span>.setDuration(noteStruct.duration_override);
} <span class="hljs-keyword">else</span> {</pre></div>
<p>Default duration</p>
<div class='highlight'><pre> <span class="hljs-keyword">this</span>.setIntrinsicTicks(initStruct.ticks);
}
<span class="hljs-keyword">this</span>.modifiers = [];</pre></div>
<p>Get the glyph code for this note from the font.</p>
<div class='highlight'><pre> <span class="hljs-keyword">this</span>.glyph = Flow.getGlyphProps(<span class="hljs-keyword">this</span>.duration, <span class="hljs-keyword">this</span>.noteType);
<span class="hljs-keyword">this</span>.customGlyphs = <span class="hljs-keyword">this</span>.customTypes.map(<span class="hljs-function"><span class="hljs-params">t</span> =></span> Flow.getGlyphProps(<span class="hljs-keyword">this</span>.duration, t));
<span class="hljs-keyword">if</span> (<span class="hljs-keyword">this</span>.positions && (<span class="hljs-keyword">typeof</span> (<span class="hljs-keyword">this</span>.positions) !== <span class="hljs-string">'object'</span> || !<span class="hljs-keyword">this</span>.positions.length)) {
<span class="hljs-keyword">throw</span> <span class="hljs-keyword">new</span> Vex.RuntimeError(<span class="hljs-string">'BadArguments'</span>, <span class="hljs-string">'Note keys must be array type.'</span>);
}</pre></div>
<p>Note to play for audio players.</p>
<div class='highlight'><pre> <span class="hljs-keyword">this</span>.playNote = <span class="hljs-literal">null</span>;</pre></div>
<p>Positioning contexts used by the Formatter.</p>
<div class='highlight'><pre> <span class="hljs-keyword">this</span>.tickContext = <span class="hljs-literal">null</span>; <span class="hljs-comment">// The current tick context.</span>
<span class="hljs-keyword">this</span>.modifierContext = <span class="hljs-literal">null</span>;
<span class="hljs-keyword">this</span>.ignore_ticks = <span class="hljs-literal">false</span>;</pre></div>
<p>Positioning variables</p>
<div class='highlight'><pre> <span class="hljs-keyword">this</span>.width = <span class="hljs-number">0</span>; <span class="hljs-comment">// Width in pixels calculated after preFormat</span>
<span class="hljs-keyword">this</span>.leftDisplacedHeadPx = <span class="hljs-number">0</span>; <span class="hljs-comment">// Extra room on left for displaced note head</span>
<span class="hljs-keyword">this</span>.rightDisplacedHeadPx = <span class="hljs-number">0</span>; <span class="hljs-comment">// Extra room on right for displaced note head</span>
<span class="hljs-keyword">this</span>.x_shift = <span class="hljs-number">0</span>; <span class="hljs-comment">// X shift from tick context X</span>
<span class="hljs-keyword">this</span>.voice = <span class="hljs-literal">null</span>; <span class="hljs-comment">// The voice that this note is in</span>
<span class="hljs-keyword">this</span>.preFormatted = <span class="hljs-literal">false</span>; <span class="hljs-comment">// Is this note preFormatted?</span>
<span class="hljs-keyword">this</span>.ys = []; <span class="hljs-comment">// list of y coordinates for each note</span></pre></div>
<p>we need to hold on to these for ties and beams.</p>
<div class='highlight'><pre>
<span class="hljs-keyword">if</span> (noteStruct.align_center) {
<span class="hljs-keyword">this</span>.setCenterAlignment(noteStruct.align_center);
}</pre></div>
<p>The render surface.</p>
<div class='highlight'><pre> <span class="hljs-keyword">this</span>.stave = <span class="hljs-literal">null</span>;
<span class="hljs-keyword">this</span>.render_options = {
<span class="hljs-attr">annotation_spacing</span>: <span class="hljs-number">5</span>,
};
}</pre></div>
<p>Get and set the play note, which is arbitrary data that can be used by an
audio player.</p>
<div class='highlight'><pre> getPlayNote() { <span class="hljs-keyword">return</span> <span class="hljs-keyword">this</span>.playNote; }
setPlayNote(note) { <span class="hljs-keyword">this</span>.playNote = note; <span class="hljs-keyword">return</span> <span class="hljs-keyword">this</span>; }</pre></div>
<p>Don’t play notes by default, call them rests. This is also used by things like
beams and dots for positioning.</p>
<div class='highlight'><pre> isRest() { <span class="hljs-keyword">return</span> <span class="hljs-literal">false</span>; }</pre></div>
<p>TODO(0xfe): Why is this method here?</p>
<div class='highlight'><pre> addStroke(index, stroke) {
stroke.setNote(<span class="hljs-keyword">this</span>);
stroke.setIndex(index);
<span class="hljs-keyword">this</span>.modifiers.push(stroke);
<span class="hljs-keyword">this</span>.setPreFormatted(<span class="hljs-literal">false</span>);
<span class="hljs-keyword">return</span> <span class="hljs-keyword">this</span>;
}</pre></div>
<p>Get and set the target stave.</p>
<div class='highlight'><pre> getStave() { <span class="hljs-keyword">return</span> <span class="hljs-keyword">this</span>.stave; }
setStave(stave) {
<span class="hljs-keyword">this</span>.stave = stave;
<span class="hljs-keyword">this</span>.setYs([stave.getYForLine(<span class="hljs-number">0</span>)]); <span class="hljs-comment">// Update Y values if the stave is changed.</span>
<span class="hljs-keyword">this</span>.context = <span class="hljs-keyword">this</span>.stave.context;
<span class="hljs-keyword">return</span> <span class="hljs-keyword">this</span>;
}</pre></div>
<p><code>Note</code> is not really a modifier, but is used in
a <code>ModifierContext</code>.</p>
<div class='highlight'><pre> getCategory() { <span class="hljs-keyword">return</span> Note.CATEGORY; }</pre></div>
<p>Set the rendering context for the note.</p>
<div class='highlight'><pre> setContext(context) { <span class="hljs-keyword">this</span>.context = context; <span class="hljs-keyword">return</span> <span class="hljs-keyword">this</span>; }</pre></div>
<p>Get and set spacing to the left and right of the notes.</p>
<div class='highlight'><pre> getLeftDisplacedHeadPx() { <span class="hljs-keyword">return</span> <span class="hljs-keyword">this</span>.leftDisplacedHeadPx; }
getRightDisplacedHeadPx() { <span class="hljs-keyword">return</span> <span class="hljs-keyword">this</span>.rightDisplacedHeadPx; }
setLeftDisplacedHeadPx(x) { <span class="hljs-keyword">this</span>.leftDisplacedHeadPx = x; <span class="hljs-keyword">return</span> <span class="hljs-keyword">this</span>; }
setRightDisplacedHeadPx(x) { <span class="hljs-keyword">this</span>.rightDisplacedHeadPx = x; <span class="hljs-keyword">return</span> <span class="hljs-keyword">this</span>; }</pre></div>
<p>Returns true if this note has no duration (e.g., bar notes, spacers, etc.)</p>
<div class='highlight'><pre> shouldIgnoreTicks() { <span class="hljs-keyword">return</span> <span class="hljs-keyword">this</span>.ignore_ticks; }</pre></div>
<p>Get the stave line number for the note.</p>
<div class='highlight'><pre> getLineNumber() { <span class="hljs-keyword">return</span> <span class="hljs-number">0</span>; }</pre></div>
<p>Get the stave line number for rest.</p>
<div class='highlight'><pre> getLineForRest() { <span class="hljs-keyword">return</span> <span class="hljs-number">0</span>; }</pre></div>
<p>Get the glyph associated with this note.</p>
<div class='highlight'><pre> getGlyph() { <span class="hljs-keyword">return</span> <span class="hljs-keyword">this</span>.glyph; }
getGlyphWidth() {</pre></div>
<p>TODO: FIXME (multiple potential values for this.glyph)</p>
<div class='highlight'><pre> <span class="hljs-keyword">if</span> (<span class="hljs-keyword">this</span>.glyph) {
<span class="hljs-keyword">if</span> (<span class="hljs-keyword">this</span>.glyph.getMetrics) {
<span class="hljs-keyword">return</span> <span class="hljs-keyword">this</span>.glyph.getMetrics().width;
} <span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> (<span class="hljs-keyword">this</span>.glyph.getWidth) {
<span class="hljs-keyword">return</span> <span class="hljs-keyword">this</span>.glyph.getWidth(<span class="hljs-keyword">this</span>.render_options.glyph_font_scale);
}
}
<span class="hljs-keyword">return</span> <span class="hljs-number">0</span>;
}</pre></div>
<p>Set and get Y positions for this note. Each Y value is associated with
an individual pitch/key within the note/chord.</p>
<div class='highlight'><pre> setYs(ys) { <span class="hljs-keyword">this</span>.ys = ys; <span class="hljs-keyword">return</span> <span class="hljs-keyword">this</span>; }
getYs() {
<span class="hljs-keyword">if</span> (<span class="hljs-keyword">this</span>.ys.length === <span class="hljs-number">0</span>) {
<span class="hljs-keyword">throw</span> <span class="hljs-keyword">new</span> Vex.RERR(<span class="hljs-string">'NoYValues'</span>, <span class="hljs-string">'No Y-values calculated for this note.'</span>);
}
<span class="hljs-keyword">return</span> <span class="hljs-keyword">this</span>.ys;
}</pre></div>
<p>Get the Y position of the space above the stave onto which text can
be rendered.</p>
<div class='highlight'><pre> getYForTopText(text_line) {
<span class="hljs-keyword">if</span> (!<span class="hljs-keyword">this</span>.stave) {
<span class="hljs-keyword">throw</span> <span class="hljs-keyword">new</span> Vex.RERR(<span class="hljs-string">'NoStave'</span>, <span class="hljs-string">'No stave attached to this note.'</span>);
}
<span class="hljs-keyword">return</span> <span class="hljs-keyword">this</span>.stave.getYForTopText(text_line);
}</pre></div>
<p>Get a <code>BoundingBox</code> for this note.</p>
<div class='highlight'><pre> getBoundingBox() { <span class="hljs-keyword">return</span> <span class="hljs-literal">null</span>; }</pre></div>
<p>Returns the voice that this note belongs in.</p>
<div class='highlight'><pre> getVoice() {
<span class="hljs-keyword">if</span> (!<span class="hljs-keyword">this</span>.voice) <span class="hljs-keyword">throw</span> <span class="hljs-keyword">new</span> Vex.RERR(<span class="hljs-string">'NoVoice'</span>, <span class="hljs-string">'Note has no voice.'</span>);
<span class="hljs-keyword">return</span> <span class="hljs-keyword">this</span>.voice;
}</pre></div>
<p>Attach this note to <code>voice</code>.</p>
<div class='highlight'><pre> setVoice(voice) {
<span class="hljs-keyword">this</span>.voice = voice;
<span class="hljs-keyword">this</span>.preFormatted = <span class="hljs-literal">false</span>;
<span class="hljs-keyword">return</span> <span class="hljs-keyword">this</span>;
}</pre></div>
<p>Get and set the <code>TickContext</code> for this note.</p>
<div class='highlight'><pre> getTickContext() { <span class="hljs-keyword">return</span> <span class="hljs-keyword">this</span>.tickContext; }
setTickContext(tc) {
<span class="hljs-keyword">this</span>.tickContext = tc;
<span class="hljs-keyword">this</span>.preFormatted = <span class="hljs-literal">false</span>;
<span class="hljs-keyword">return</span> <span class="hljs-keyword">this</span>;
}</pre></div>
<p>Accessors for the note type.</p>
<div class='highlight'><pre> getDuration() { <span class="hljs-keyword">return</span> <span class="hljs-keyword">this</span>.duration; }
isDotted() { <span class="hljs-keyword">return</span> (<span class="hljs-keyword">this</span>.dots > <span class="hljs-number">0</span>); }
hasStem() { <span class="hljs-keyword">return</span> <span class="hljs-literal">false</span>; }
getDots() { <span class="hljs-keyword">return</span> <span class="hljs-keyword">this</span>.dots; }
getNoteType() { <span class="hljs-keyword">return</span> <span class="hljs-keyword">this</span>.noteType; }
setBeam() { <span class="hljs-keyword">return</span> <span class="hljs-keyword">this</span>; } <span class="hljs-comment">// ignore parameters</span></pre></div>
<p>Attach this note to a modifier context.</p>
<div class='highlight'><pre> setModifierContext(mc) { <span class="hljs-keyword">this</span>.modifierContext = mc; <span class="hljs-keyword">return</span> <span class="hljs-keyword">this</span>; }</pre></div>
<p>Attach a modifier to this note.</p>
<div class='highlight'><pre> addModifier(modifier, index = <span class="hljs-number">0</span>) {
modifier.setNote(<span class="hljs-keyword">this</span>);
modifier.setIndex(index);
<span class="hljs-keyword">this</span>.modifiers.push(modifier);
<span class="hljs-keyword">this</span>.setPreFormatted(<span class="hljs-literal">false</span>);
<span class="hljs-keyword">return</span> <span class="hljs-keyword">this</span>;
}</pre></div>
<p>Get the coordinates for where modifiers begin.</p>
<div class='highlight'><pre> getModifierStartXY() {
<span class="hljs-keyword">if</span> (!<span class="hljs-keyword">this</span>.preFormatted) {
<span class="hljs-keyword">throw</span> <span class="hljs-keyword">new</span> Vex.RERR(<span class="hljs-string">'UnformattedNote'</span>, <span class="hljs-string">"Can't call GetModifierStartXY on an unformatted note"</span>);
}
<span class="hljs-keyword">return</span> {
<span class="hljs-attr">x</span>: <span class="hljs-keyword">this</span>.getAbsoluteX(),
<span class="hljs-attr">y</span>: <span class="hljs-keyword">this</span>.ys[<span class="hljs-number">0</span>],
};
}</pre></div>
<p>Get bounds and metrics for this note.</p>
<p>Returns a struct with fields:
<code>width</code>: The total width of the note (including modifiers.)
<code>notePx</code>: The width of the note head only.
<code>left_shift</code>: The horizontal displacement of the note.
<code>modLeftPx</code>: Start <code>X</code> for left modifiers.
<code>modRightPx</code>: Start <code>X</code> for right modifiers.
<code>leftDisplacedHeadPx</code>: Extra space on left of note.
<code>rightDisplacedHeadPx</code>: Extra space on right of note.</p>
<div class='highlight'><pre> getMetrics() {
<span class="hljs-keyword">if</span> (!<span class="hljs-keyword">this</span>.preFormatted) {
<span class="hljs-keyword">throw</span> <span class="hljs-keyword">new</span> Vex.RERR(<span class="hljs-string">'UnformattedNote'</span>, <span class="hljs-string">"Can't call getMetrics on an unformatted note."</span>);
}
<span class="hljs-keyword">const</span> modLeftPx = <span class="hljs-keyword">this</span>.modifierContext ? <span class="hljs-keyword">this</span>.modifierContext.state.left_shift : <span class="hljs-number">0</span>;
<span class="hljs-keyword">const</span> modRightPx = <span class="hljs-keyword">this</span>.modifierContext ? <span class="hljs-keyword">this</span>.modifierContext.state.right_shift : <span class="hljs-number">0</span>;
<span class="hljs-keyword">const</span> width = <span class="hljs-keyword">this</span>.getWidth();
<span class="hljs-keyword">const</span> glyphWidth = <span class="hljs-keyword">this</span>.getGlyphWidth();
<span class="hljs-keyword">const</span> notePx = width
- modLeftPx <span class="hljs-comment">// subtract left modifiers</span>
- modRightPx <span class="hljs-comment">// subtract right modifiers</span>
- <span class="hljs-keyword">this</span>.leftDisplacedHeadPx <span class="hljs-comment">// subtract left displaced head</span>
- <span class="hljs-keyword">this</span>.rightDisplacedHeadPx; <span class="hljs-comment">// subtract right displaced head</span>
<span class="hljs-keyword">return</span> {</pre></div>
<hr>
<h2 id="note-if-you-change-this-remember-to-update-mocktickable-in-the-tests-directory">NOTE: If you change this, remember to update MockTickable in the tests/ directory.</h2>
<div class='highlight'><pre> width,
glyphWidth,
notePx,</pre></div>
<p>Modifier spacing.</p>
<div class='highlight'><pre> modLeftPx,
modRightPx,</pre></div>
<p>Displaced note head on left or right.</p>
<div class='highlight'><pre> leftDisplacedHeadPx: <span class="hljs-keyword">this</span>.leftDisplacedHeadPx,
<span class="hljs-attr">rightDisplacedHeadPx</span>: <span class="hljs-keyword">this</span>.rightDisplacedHeadPx,
};
}</pre></div>
<p>Get the absolute <code>X</code> position of this note’s tick context. This
excludes x_shift, so you’ll need to factor it in if you’re
looking for the post-formatted x-position.</p>
<div class='highlight'><pre> getAbsoluteX() {
<span class="hljs-keyword">if</span> (!<span class="hljs-keyword">this</span>.tickContext) {
<span class="hljs-keyword">throw</span> <span class="hljs-keyword">new</span> Vex.RERR(<span class="hljs-string">'NoTickContext'</span>, <span class="hljs-string">'Note needs a TickContext assigned for an X-Value'</span>);
}</pre></div>
<p>Position note to left edge of tick context.</p>
<div class='highlight'><pre> <span class="hljs-keyword">let</span> x = <span class="hljs-keyword">this</span>.tickContext.getX();
<span class="hljs-keyword">if</span> (<span class="hljs-keyword">this</span>.stave) {
x += <span class="hljs-keyword">this</span>.stave.getNoteStartX() + <span class="hljs-keyword">this</span>.musicFont.lookupMetric(<span class="hljs-string">'stave.padding'</span>);
}
<span class="hljs-keyword">if</span> (<span class="hljs-keyword">this</span>.isCenterAligned()) {
x += <span class="hljs-keyword">this</span>.getCenterXShift();
}
<span class="hljs-keyword">return</span> x;
}
setPreFormatted(value) {
<span class="hljs-keyword">this</span>.preFormatted = value;
}
}</pre></div>
<div class="fleur">h</div>
</div>
</div>
</body>
</html>