-
Notifications
You must be signed in to change notification settings - Fork 0
/
convert-ripchord-scaler.mjs
267 lines (240 loc) · 5.71 KB
/
convert-ripchord-scaler.mjs
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
import { XMLBuilder, XMLParser, XMLValidator } from 'fast-xml-parser';
import { v4 as uuidv4 } from 'uuid';
import { Chord, Midi } from 'tonal';
// {
// "info": {
// "empty": false,
// "name": "E ",
// "setNum": 2386,
// "chroma": "100101010010",
// "normalized": "100101001010",
// "intervals": [
// "1P",
// "3m",
// "5P",
// "7m",
// "11P"
// ],
// "quality": "Minor",
// "aliases": [
// "m7add11",
// "m7add4"
// ],
// "symbol": "Em7add11",
// "type": "",
// "root": "",
// "rootDegree": 0,
// "tonic": "E",
// "notes": [
// "E",
// "G",
// "B",
// "D",
// "A"
// ]
// },
// "name": "Em7add11 / G6/9/E / A11/E / A9sus4/E",
// "notes": [
// {
// "value": "40",
// "note": "E2"
// },
// {
// "value": "55",
// "note": "G3"
// },
// {
// "value": "59",
// "note": "B3"
// },
// {
// "value": "62",
// "note": "D4"
// },
// {
// "value": "64",
// "note": "E4"
// },
// {
// "value": "69",
// "note": "A4"
// }
// ],
// "trigger": "48"
// }
export const builder = new XMLBuilder({
processEntities: false,
format: true,
ignoreAttributes: false,
});
export const parser = new XMLParser({
ignoreAttributes: false,
allowBooleanAttributes: true,
});
/**
* @param data
* @param root0
* @param root0.sharps
*/
export function parseText(data, { sharps = true } = {}) {
// Validate
const result = XMLValidator.validate(data);
if (result?.err) {
console.log(`XML is invalid becuause of - ${result.err.msg}`, result);
return;
}
const json = parser.parse(data);
if (json.ripchord) {
return parseRipchord(json, { sharps });
}
if (json.CHORDSET) {
return parseScaler(json, { sharps });
}
}
/**
* @param json
* @param root0
* @param root0.sharps
*/
export function parseRipchord(json, { sharps = true } = {}) {
// console.log('parseRipchord:', JSON.stringify(json, null, 2));
const data = {
chords: [],
type: 'ripchord',
uuid: '',
version: '',
};
// Missing the container node.
if (!json?.ripchord?.preset?.input) {
console.log('Not a Ripchord file.');
return data;
}
// Metadata
data.uuid = uuidv4();
// Read the keys from the data and detect the chords
for (const { chord, '@_note': trigger } of json.ripchord.preset.input) {
const notes = chord['@_notes'].split(';').map((value) => ({ value, note: Midi.midiToNoteName(value, { sharps }) }));
// console.log('notes', notes);
const name = Chord.detect(notes.map(({ note }) => note));
// console.log('name', name);
const info = Chord.get(name[0]);
// console.log('info', info);
data.chords.push({
info,
name: chord['@_name'] || name.join(' / ') || notes.map(({ note }) => note).join(' + '),
notes,
trigger,
});
}
// console.log('parseRipchord:', data);
return data;
}
/**
* @param json
* @param root0
* @param root0.sharps
*/
export function parseScaler(json, { sharps = true } = {}) {
// console.log('parseScaler:', json);
const data = {
chords: [],
type: 'scaler',
uuid: '',
version: '',
};
// Missing the container node.
if (!json.CHORDSET?.CHORD) {
console.log('Not a Scaler file.');
return data;
}
// Set Metadata
data.uuid = json.CHORDSET['@_uuid'];
data.version = json.CHORDSET['@_version'];
// Read the keys from the data and detect the chords
for (const chord of json.CHORDSET.CHORD) {
const notes = chord.NOTE.map(({ '@_MIDI': value }) => ({ value, note: Midi.midiToNoteName(value, { sharps }) }));
// console.log('notes', notes.map(({ value }) => value));
const name = Chord.detect(notes.map(({ note }) => note));
// console.log('name', name);
const info = Chord.get(name[0]);
// console.log('info', info);
data.chords.push({
info,
name: name.join(' / ') || notes.map(({ note }) => note).join(' + '),
notes,
trigger: '',
});
}
// console.log('parseScaler:', data);
return data;
}
/**
* @param data
*/
export function buildXML(data) {
let output = builder.build(data);
// Make Ripchord chord nodes self closing.
output = output.replaceAll('"></NOTE>', '"/>');
output = output.replaceAll('"></chord>', '"/>');
return output;
}
/**
* @param data
* @param root0
* @param root0.generateTrigger
* @param root0.startValue
*/
export function buildRipchord(data, { generateTrigger = false, startValue = 48 } = {}) {
const output = {
'?xml': {
'@_version': '1.0',
'@_encoding': 'UTF-8',
},
ripchord: {
preset: {
input: [],
},
},
};
// Start from C3 / 48
let generatedTrigger;
if (generateTrigger) {
generatedTrigger = startValue;
}
for (const { name, notes, trigger } of data.chords) {
output.ripchord.preset.input.push({
chord: {
'@_name': name,
'@_notes': notes.map(({ value }) => value).join(';'),
},
'@_note': trigger && !generateTrigger ? String(trigger) : String(generatedTrigger),
});
if (generateTrigger) {
generatedTrigger += 1;
}
}
return output;
}
/**
* @param data
*/
export function buildScaler(data) {
const output = {
'?xml': {
'@_version': '1.0',
'@_encoding': 'UTF-8',
},
CHORDSET: {
CHORD: [],
'@_version': data.version || '2',
'@_uuid': data.uuid,
},
};
// Scaler only accepts notes, information is parsed in app and there is no trigger.
for (const { name, notes, trigger } of data.chords) {
output.CHORDSET.CHORD.push({
NOTE: notes.map(({ value }) => ({ '@_MIDI': String(value) })),
});
}
return output;
}