File size: 1,174 Bytes
af6912c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
function germanNote(note) {
	switch (note) {
		case "B#": return "H#";
		case "B♯": return "H♯";
		case "B": return "H";
		case "Bb": return "B";
		case "B♭": return "B";
	}
	return note;
}

function translateChord(chordString, jazzchords, germanAlphabet) {
	var lines = chordString.split("\n");
	for (let i = 0; i < lines.length; i++) {
		let chord = lines[i];
		// If the chord isn't in a recognizable format then just skip it.
		let reg = chord.match(/^([ABCDEFG][♯♭]?)?([^\/]+)?(\/([ABCDEFG][#b♯♭]?))?/);
		if (!reg) {
			continue;
		}
		let baseChord = reg[1] || "";
		let modifier = reg[2] || "";
		let bassNote = reg[4] || "";
		if (germanAlphabet) {
			baseChord = germanNote(baseChord);
			bassNote = germanNote(bassNote);
		}
		// This puts markers in the pieces of the chord that are read by the svg creator.
		// After the main part of the chord (the letter, a sharp or flat, and "m") a marker is added. Before a slash a marker is added.
		const marker = jazzchords ? "\x03" : "";
		const bass = bassNote ? "/" + bassNote : "";
		lines[i] = [baseChord, modifier, bass].join(marker);
	}
	return lines.join("\n");
}

module.exports = translateChord;