Skip to content

Commit 61ba5e9

Browse files
committed
Move curves code to separate function
1 parent f317bc9 commit 61ba5e9

File tree

3 files changed

+82
-60
lines changed

3 files changed

+82
-60
lines changed

lib/main/generate-vex-objects.js

Lines changed: 3 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import Bar from '../models/bar';
1313
import Part from '../models/part';
1414
import Tune from '../models/tune';
1515

16-
import { getKeys, getVexDuration, addDecorations, getTabPosition, generateBeamsCompound } from '../utils';
16+
import { getKeys, getVexDuration, addDecorations, getTabPosition, generateBeamsCompound, getCurves } from '../utils';
1717
import { VEX_ACCIDENTAL_FROM_ABCJS } from '../constants';
1818

1919
/**
@@ -120,6 +120,8 @@ export default function generateVexObjects(partRegions, tuneAttrs, renderOptions
120120
if (accidental) { noteToAdd.addAccidental(i, new Vex.Flow.Accidental(accidental)); }
121121
});
122122

123+
currentPart.curves = getCurves(currentPart.curves, obj, noteToAdd);
124+
123125
// this is mostly duplicated with below. maybe can be put in a separate function
124126
if (obj.gracenotes) {
125127
const graceNotesArray = [];
@@ -162,65 +164,6 @@ export default function generateVexObjects(partRegions, tuneAttrs, renderOptions
162164
const tabGraceNoteGroup = new GraceNoteGroup(tabGraceNotesArray, false);
163165
tabNoteToAdd.addModifier(tabGraceNoteGroup);
164166
}
165-
166-
if (obj.startSlur) {
167-
obj.startSlur.forEach(() => {
168-
currentPart.curves.push({
169-
startNote: noteToAdd
170-
});
171-
});
172-
}
173-
if (obj.endSlur) {
174-
obj.endSlur.forEach(() => {
175-
let i = currentPart.curves.length - 1;
176-
while (i >= 0) {
177-
if (!currentPart.curves[i].endNote) {
178-
currentPart.curves[i].endNote = noteToAdd;
179-
break;
180-
}
181-
i -= 1;
182-
}
183-
});
184-
}
185-
186-
obj.pitches.forEach((pitch) => {
187-
if (pitch.startTie) {
188-
currentPart.curves.push({
189-
startNote: noteToAdd
190-
});
191-
}
192-
if (pitch.endTie) {
193-
currentPart.curves[currentPart.curves.length - 1].endNote = noteToAdd;
194-
let i = currentPart.curves.length - 1;
195-
while (i >= 0) {
196-
if (!currentPart.curves[i].endNote) {
197-
currentPart.curves[i].endNote = noteToAdd;
198-
break;
199-
}
200-
i -= 1;
201-
}
202-
}
203-
if (pitch.startSlur) {
204-
pitch.startSlur.forEach(() => {
205-
currentPart.curves.push({
206-
startNote: noteToAdd
207-
});
208-
});
209-
}
210-
if (pitch.endSlur) {
211-
pitch.endSlur.forEach(() => {
212-
let i = currentPart.curves.length - 1;
213-
while (i >= 0) {
214-
if (!currentPart.curves[i].endNote) {
215-
currentPart.curves[i].endNote = noteToAdd;
216-
break;
217-
}
218-
i -= 1;
219-
}
220-
});
221-
}
222-
});
223-
224167
currentBar.tabNotes.push(tabNoteToAdd);
225168
} else { // IS a rest
226169
noteToAdd = new StaveNote({

lib/utils/get-curves.js

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
/**
2+
* Takes an ABCJS parser output obj to extract the start/end curve (tie/slur)
3+
* markers that are attached to the obj or individual pitches in the obj. The .curves[] array
4+
* will be used later on to create the VexFlow Curve objects. Can't create them here because we
5+
* need to position the bars first before we can create the Curves, in order to be able to handle curves
6+
* that go across line breaks.
7+
*
8+
* @param {Array} curves the Part.curves[] array as it exists so far
9+
* @param {object} obj the obj element from the abcjs parser output array
10+
* @param {StaveNote} noteToAdd the VexFlow note that was created earlier in the GenerateVexObjects loop iteration
11+
*
12+
* @returns {Array} updated curves[] with the relevant curve markers added with a reference to noteToAdd
13+
*/
14+
export function getCurves(curves, obj, noteToAdd) {
15+
const newCurves = [...curves];
16+
17+
// handle curves on obj
18+
if (obj.startSlur) {
19+
obj.startSlur.forEach(() => {
20+
newCurves.push({
21+
startNote: noteToAdd
22+
});
23+
});
24+
}
25+
if (obj.endSlur) {
26+
obj.endSlur.forEach(() => {
27+
let i = newCurves.length - 1;
28+
while (i >= 0) {
29+
if (!newCurves[i].endNote) {
30+
newCurves[i].endNote = noteToAdd;
31+
break;
32+
}
33+
i -= 1;
34+
}
35+
});
36+
}
37+
38+
// handle curves on pitch
39+
obj.pitches.forEach((pitch) => {
40+
if (pitch.startTie) {
41+
newCurves.push({
42+
startNote: noteToAdd
43+
});
44+
}
45+
if (pitch.endTie) {
46+
newCurves[newCurves.length - 1].endNote = noteToAdd;
47+
let i = newCurves.length - 1;
48+
while (i >= 0) {
49+
if (!newCurves[i].endNote) {
50+
newCurves[i].endNote = noteToAdd;
51+
break;
52+
}
53+
i -= 1;
54+
}
55+
}
56+
if (pitch.startSlur) {
57+
pitch.startSlur.forEach(() => {
58+
newCurves.push({
59+
startNote: noteToAdd
60+
});
61+
});
62+
}
63+
if (pitch.endSlur) {
64+
pitch.endSlur.forEach(() => {
65+
let i = newCurves.length - 1;
66+
while (i >= 0) {
67+
if (!newCurves[i].endNote) {
68+
newCurves[i].endNote = noteToAdd;
69+
break;
70+
}
71+
i -= 1;
72+
}
73+
});
74+
}
75+
});
76+
77+
return newCurves;
78+
}

lib/utils/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,5 @@ export * from './generate-beams-compound';
22
export * from './get-tab-position';
33
export * from './get-keys';
44
export * from './add-decorations';
5+
export * from './get-curves';
56
export * from './utils';

0 commit comments

Comments
 (0)