-
Notifications
You must be signed in to change notification settings - Fork 1
/
read-corpus.js
176 lines (165 loc) · 4.9 KB
/
read-corpus.js
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
const fs = require('fs')
const path = require('path')
// read **kern format files from corpus
// https://figshare.com/articles/dataset/iRealPro_Corpus_of_Jazz_Standards/10326725
// argument should be dir with the *.jazz files
const cleanBassNote = x => x.replace('-', 'b')
const convertChord = x => {
const alternates = x.split('(')
if (alternates.length > 1) {
return `${convertChord(alternates[0])}(${convertChord(alternates[1])}`
}
const parts = x.split('/')
if (parts.length > 1) {
return `${convertChord(parts[0])}/${cleanBassNote(parts[1])}`
}
return parts[0]
.replace('^9', 'maj9')
.replace('^7', 'maj7')
.replace('^', 'maj')
.replace('h7', 'm7b5')
.replace('-:', 'b')
.replace('-', 'b')
.replace(':', '')
.replace('min', 'm')
.replace('m:maj', 'mmaj')
.replace(/o/g, 'dim')
.replace('r', '')
}
let DEBUG = false
const log = (...x) => (DEBUG ? console.log(...x) : {})
const getDebug = () => false
//const getDebug = s => s.id === 'halfnelson'
const files = fs.readdirSync(process.argv[2]).filter(x => x.endsWith('.jazz'))
const songs = files.map(filename => {
const data = fs
.readFileSync(path.resolve(process.argv[2], filename))
.toString()
.split('\n')
const song = { sections: {}, id: filename.replace('.jazz', '') }
let currentSection = null
let currentBar = []
let tsRead = false
DEBUG = getDebug(song)
data.forEach(line => {
if (line.startsWith('!') && line.indexOf('!OTL') > -1) {
song.title = line
.split(':')
.slice(1)
.join('')
.trim()
} else if (line.startsWith('*>[')) {
song.structure = line
.substring(3)
.replace(']', '')
.split(',')
.filter(x => x !== 'Sign') // TODO handle D.S. etc
} else if (line.startsWith('*>')) {
const sect = line.substring(2)
// TODO handle D.S. etc
if (sect !== 'Sign') {
currentSection = sect
}
} else if (line.startsWith('*M') && !tsRead) {
// song might have time signature changes
song.timeSignature = line.substring(2)
tsRead = true
} else if (line.startsWith('*') && line.endsWith(':')) {
song.key = line.substring(1, line.length - 1)
} else if (line.startsWith('=')) {
if (currentSection === null) {
currentSection = 'A'
}
if (!song.sections[currentSection]) {
song.sections[currentSection] = []
}
song.sections[currentSection].push(currentBar)
currentBar = []
} else if (line[0] >= '1' && line[0] <= '9') {
const dotted = line[1] === '.'
const duration = dotted ? line.substring(0, 2) : line[0]
const chord = convertChord(line.substring(dotted ? 2 : 1))
currentBar.push([chord, duration])
}
})
// check empty sections
if (song.structure) {
song.structure.forEach(sect => {
if (!song.sections[sect]) {
song.sections[sect] = []
}
})
}
log(song)
return song
})
/*console.log(
songs
.filter(x => x.timeSignature !== '4/4' && x.timeSignature !== '3/4')
.map(x => [x.title, x.timeSignature])
)*/
const addendum = [
{
id: 'dolphindance2',
name: 'Dolphin Dance v2',
string:
'[B2] Bbm7/Eb;Ebmaj7;Abmaj7#5/Eb;Dm7b5 G7b9 [A] Cm7;Abmaj7;Cm7;Am7 D7;Gmaj7;Abm7 Db7;Fm7;Bb7;Cm7;Cm7/Bb;Am7;D7 [B1] Gmaj7;Fmaj7/G;A/G;Cmmaj7/G;F9sus;Cmmaj7/F;F9sus;Em7 A7;Eb7;Am7 D7;Bm7;E7 Dm7;C#m7;F#7;Bm7/E;Am7/E;Bm7/E;Am7/E',
swing: true,
tempo: 118
}
]
const sorted = arr => {
arr.sort((s1, s2) => {
const a = s1.name.toUpperCase()
const b = s2.name.toUpperCase()
if (a < b) return -1
if (a > b) return 1
return 0
})
return arr
}
const output = sorted(
songs
.map(song => {
//console.log(song.title, song.structure, song.sections)
let outputted = {}
const data = (!song.structure ? ['A'] : song.structure)
.map(sect => {
if (outputted[sect]) {
return `[${sect}]`
}
const out = `[${sect}] ${song.sections[sect]
.map(bar => {
const durations = new Set(bar.map(x => x[1]))
const multiLen = durations.size > 1
return bar
.map(chord => {
return multiLen && chord[1] !== '4'
? `${chord[0]} .`
: chord[0]
})
.join(' ')
})
.join(';')}`
outputted[sect] = true
return out
})
.join(' ')
DEBUG = getDebug(song)
log(data)
const out = {
id: song.id,
name: song.title,
string: data,
tempo: 140,
swing: true
}
if (song.timeSignature !== '4/4') {
// one song in corpus has 3/2. time signature, correct to 3/4
out.ts = song.timeSignature.replace('2.', '4')
}
return out
})
.concat(addendum)
)
fs.writeFileSync('assets/jazzStandards.json', JSON.stringify(output))