-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathencoding.js
More file actions
171 lines (119 loc) · 4.35 KB
/
Copy pathencoding.js
File metadata and controls
171 lines (119 loc) · 4.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
import {StaffGroupType} from "./staffLayout";
const tabs = (n) => n > 0 ? "\t".repeat(n) : "";
const indentParagraph = (text, indent) => text.split("\n").map(line => tabs(indent) + line).join("\n");
const lilyStaffGroup = (staffGroup, nameDict, indent = 0) => {
let result = "";
const headers = [];
const instrument = nameDict[staffGroup.key];
if (instrument)
headers.push(`instrumentName = "${instrument}"`);
const headerStatement = () => headers.length > 0 ? `\\with { ${headers.join(" ")} } ` : "";
if (staffGroup.type !== StaffGroupType.Default || staffGroup.subs) {
switch (staffGroup.type) {
case StaffGroupType.Default:
result += tabs(indent) + "<<\n";
break;
case StaffGroupType.Brace:
result += tabs(indent) + `\\new GrandStaff ${headerStatement()}<<\n`;
break;
case StaffGroupType.Bracket:
result += tabs(indent) + `\\new StaffGroup ${headerStatement()}<<\n`;
break;
case StaffGroupType.Square:
headers.push("systemStartDelimiter = #'SystemStartSquare");
result += tabs(indent) + `\\new StaffGroup ${headerStatement()}<<\n`;
break;
}
++indent;
}
if (staffGroup.staff) {
//console.assert(statementDict[staffGroup.staff], "unknown staff id:", staffGroup.staff, Object.keys(statementDict), staffGroup);
const statement = `\\new Staff "${staffGroup.staff}" ${headerStatement()}{}`;
result += indentParagraph(statement, indent);
result += "\n";
}
else if (staffGroup.subs) {
for (const sub of staffGroup.subs)
result += lilyStaffGroup(sub, nameDict, indent);
}
if (staffGroup.type !== StaffGroupType.Default || staffGroup.subs) {
--indent;
result += tabs(indent) + ">>\n";
}
return result;
};
const encodeLilypond = (layout, nameDict) => {
return lilyStaffGroup(layout.group, nameDict);
};
const GROUP_SYMBOLS = [
null,
"brace",
"bracket",
"square",
];
const stateMusicxmlGroup = (statements, group, keys, nameDict, indent = 1) => {
const indentTabs = tabs(indent);
const number = keys.indexOf(group.key) + 1;
const name = nameDict[group.key];
if (group.grand) {
statements.push(indentTabs + `<score-part id="${group.key}">`);
if (name)
statements.push(indentTabs + ` <part-name>${name}</part-name>`);
statements.push(indentTabs + "</score-part>");
return;
}
if (group.type > 0 || name) {
statements.push(indentTabs + `<part-group number="${number}" type="start">`);
statements.push(indentTabs + ` <group-symbol>${GROUP_SYMBOLS[group.type]}</group-symbol>`);
if (name)
statements.push(indentTabs + ` <group-name>${name}</group-name>`);
statements.push(indentTabs + ` <group-barline>${group.bar > 1 ? "yes" : "no"}</group-barline>`);
statements.push(indentTabs + "</part-group>");
}
if (group.subs)
group.subs.forEach(group => stateMusicxmlGroup(statements, group, keys, nameDict, indent));
if (group.staff)
statements.push(indentTabs + `<score-part id="${group.key}">`);
if (group.type > 0 || name)
statements.push(indentTabs + `<part-group number="${number}" type="stop" />`);
};
const encodeMusicxml = (layout, nameDict) => {
const statements = [];
const keys = layout.groups.map(g => g.key);
stateMusicxmlGroup(statements, layout.group, keys, nameDict);
return `<part-list>\n${statements.join("\n")}\n</part-list>`;
};
const bool = x => x ? "true" : "false";
const stateMEIGroup = (statements, group, nameDict, ids, indent = 0) => {
const indentTabs = tabs(indent);
const name = nameDict[group.key];
if (group.subs) {
const symbol = GROUP_SYMBOLS[group.type] ? ` symbol="${GROUP_SYMBOLS[group.type]}"` : "";
statements.push(indentTabs + `<staffGrp bar.thru="${bool(group.bar > 1)}"${symbol}>`);
if (name)
statements.push(indentTabs + ` <label>${name}</label>`);
group.subs.forEach(group => stateMEIGroup(statements, group, nameDict, ids, indent + 1));
statements.push(indentTabs + "</staffGrp>");
}
if (group.staff)
statements.push(indentTabs + `<staffDef n="${ids.indexOf(group.staff) + 1}">`);
};
const encodeMEI = (layout, nameDict) => {
const statements = [];
stateMEIGroup(statements, layout.group, nameDict, layout.staffIds);
return statements.join("\n");
};
const encode = (lang, layout, nameDict) => {
switch (lang) {
case "Lilypond":
return encodeLilypond(layout, nameDict);
case "MusicXML":
return encodeMusicxml(layout, nameDict);
case "MEI":
return encodeMEI(layout, nameDict);
}
};
export {
encodeLilypond,
encode,
};