-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.mjs
138 lines (113 loc) · 3.34 KB
/
index.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
const metaRegExpStr = '#(?<key>[a-z]+):(?<value>.+)';
const metaRowRegExp = new RegExp(metaRegExpStr, 'gi');
const metaRegExp = new RegExp(metaRegExpStr, 'i');
const ignoreKeys = /^(cover|video|encoding|background|start)$/i;
const numberKeys = /^(bpm|gap|year|end|videogap|previewstart)$/i;
const songRegExpStr = '(?<type>[:*F-])\\s(?<bpm_start>[0-9-]+)(\\s(?<bpm_length>[0-9]+)\\s(?<pitch>[0-9-]+)\\s(?<text>.+))?';
const songRowRegExp = new RegExp(songRegExpStr, 'g');
const songRegExp = new RegExp(songRegExpStr);
const metaDataReducer = (result, row) =>
{
let { key, value } = row.match(metaRegExp).groups;
if (ignoreKeys.test(key)) return result;
if (numberKeys.test(key))
{
value = parseFloat(value);
if (value === 0)
{
return result;
}
}
if (/language/i.test(key))
{
if (value in languageMap)
{
value = languageMap[value];
}
else
{
console.warn('Language ', value, 'is not mapped to a language code.');
}
}
return {
...result,
[key.toLowerCase()]: value,
};
};
const typeMapping = {
':': 1, // 'REGULAR',
'*': 2, // 'GOLDEN',
'F': 0, // 'FREESTYLE',
};
const languageMap = {
German: 'de',
Deutsch: 'de',
English: 'en',
Englisch: 'en',
Nederlands: 'nl',
Dutch: 'nl',
Spanish: 'es',
};
export const getMetaData = content => content.match(metaRowRegExp).reduce(metaDataReducer, {});
export function getSongData(meta, content)
{
if (/yes/i.test(meta.relative))
{
throw new Error('Parsing relative song files is not yet implemented!');
}
const bpmToMs = bpm => Math.round((250 / meta.bpm * 60) * parseInt(bpm, 10) * 100) / 100;
const song = [];
let lastLine;
content.match(songRowRegExp).forEach((row) =>
{
const { type, bpm_start, bpm_length, pitch, text } = row.match(songRegExp).groups;
const start = ((meta.videogap || 0) * 1000) + (meta.gap || 0) + bpmToMs(bpm_start);
const newLine = type === '-';
if (!lastLine || newLine)
{
if (lastLine)
{
const linePitches = lastLine.syllables.map(syllable => syllable.pitch);
lastLine.minPitch = Math.min(...linePitches);
lastLine.maxPitch = Math.max(...linePitches);
lastLine.end = start;
if (newLine && !lastLine.syllables.length) return;
}
lastLine = { start, end: null, minPitch: null, maxPitch: null, syllables: [] };
song.push(lastLine);
if (newLine) return;
}
if (!lastLine.syllables.length && start - lastLine.start > 1000) {
lastLine.start = start;
}
lastLine.syllables.push({
type: typeMapping[type],
start,
length: bpm_length && bpmToMs(bpm_length),
pitch: pitch && parseInt(pitch, 10),
text,
});
});
const lastSyllable = lastLine.syllables[lastLine.syllables.length - 1];
const lastPitches = lastLine.syllables.map(syllable => syllable.pitch);
lastLine.minPitch = Math.min(...lastPitches);
lastLine.maxPitch = Math.max(...lastPitches);
lastLine.end = lastSyllable.start + lastSyllable.length;
return song;
}
export default function(content, { transformFields })
{
const meta = getMetaData(content);
if (transformFields)
{
for (const [field, transform] of Object.entries(transformFields))
{
meta[field] = transform(meta[field]);
}
}
const song = getSongData(meta, content);
return {
...meta,
song,
};
}