-
Notifications
You must be signed in to change notification settings - Fork 2
/
csv2ly.py
executable file
·406 lines (375 loc) · 11.8 KB
/
csv2ly.py
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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
#!/usr/bin/python
import csv, sys, io, re
#TODO
# Add key signature?
# add tie if note equals previous note. Global variables prev_sop, etc?
#
#Usage: ./csv2ly <file>
### Files and metadata
csv_file = sys.argv[1]
table_file = open(csv_file)
csv_table = csv.reader(table_file, delimiter='\t')
header = next(csv_table)
ly_filename = re.sub('csv$','csv.ly',csv_file)
ly_file = open(ly_filename,'w')
ly_template = io.open('/iomega/CP/NOH/csv/template.ly', 'r')
#ly_template = io.open('/home/ahinkley/Documents/gabctoly/template.ly', 'r')
print(header)
try:
ly_key = header[9]
except:
ly_key = ''
try:
ly_title = header[10]
except:
ly_title = ''
try:
ly_genre = header[11]
except:
ly_genre = ''
try:
ly_mode = header[12]
except:
ly_mode = ''
try:
noh_page = header[14]
except:
noh_page = ''
### Multiplier to notelength
# Assume note is a minim. If integer, then note*beats/2. If non integer, then beats/4??
def lilynotelength(mult):
if mult == "0":
return ''
if mult == "1":
length = "4"
elif mult == "2":
length = "2"
elif mult == "1.5":
length = "4."
elif mult == "3":
length = "2."
elif ".5" in mult:
multiplier = float(mult)
multiplier *= 2
mult = str(multiplier)
length = ("2*" + mult + "/4")
else:
length = ("2*" + mult + "/2")
if ".0" in length:
length = length.replace('.0','')
return length
##Soprano line
##TODO How to account for variable multiplier, esp. in row 2
##
lyrics = ''
syllable = ''
syllable_prev_row = ''
beats = '0'
beats_prev = '0'
soprano_score = '\\tieDown '
soprano_prev_note = ''
soprano_pitch = ''
soprano_prev_pitch = ''
alto_score = ''
alto_prev_note = ''
alto_pitch = ''
alto_prev_pitch = ''
alto_duration = 0
tenor_score = ''
tenor_prev_note = ''
tenor_pitch = ''
tenor_prev_pitch = ''
tenor_duration = 0
bass_score = ''
bass_prev_note = ''
bass_pitch = ''
bass_prev_pitch = ''
bass_duration = 0
voiceline = 0
voiceline_score = ''
voiceline_staffA = ''
voiceline_start = ''
voiceline_started = 0
voiceline_staffB = ''
voiceline_end = ''
voiceline_ended = 0
voiceline_duration = 0
voiceline_column = 0
slur = -1
i = 1
division = 0
divison_marker = ''
for row in csv_table:
syllable_prev_row = syllable
syllable = row[0]
gabc_note = row[1]
beats_prev = beats
beats = row[2]
duration = lilynotelength(beats)
##Tags
tags = row[8]
##Lyrics
#Split lyrics into smaller lines
#Divisions:: 1:Minima 2:Maior 3:Maxima 4:Finalis
if division > 1:
lyrics += '\n'
#Verses, asterisks, etc.
if "y" in tags:
syllable = '\n\set stanza = " ' + syllable + ' "'
#Add syllable
if syllable != '':
lyrics += syllable + ' '
#Continue syllable at start of slur (row[2] nonempty), if syllable blank (row[0] empty) EXCEPT
# and previous row not interrputed (prev S empty, prev syllable not)
if syllable == '':
if row[3] != '':
if beats_prev == '0' and syllable_prev_row != '':
print beats, beats_prev,i
lyrics += ''
else:
lyrics += '_ '
#if row[3] != '' and row[4] != soprano_prev_pitch and soprano_prev_pitch != '' and syllable_prev_row != '':
##Soprano
soprano_note = row[4]
soprano_pitch = row[4]
#x = row[3]
# \tiny \normalsize
if "tiny" and "normalsize" in soprano_note:
soprano_note = soprano_note.replace('\\normalsize','')
soprano_note = soprano_note.replace('\\tiny','\\once \\tweak #\'font-size #-4')
if slur == 0:
soprano_score += ')'
slur -= 1
if soprano_pitch == soprano_prev_pitch:
if row[0] == '' and row[3] != '':
soprano_score += ' ~'
if "\\" in soprano_note:
soprano_score += ' ' + soprano_note
elif "--" in soprano_note:
soprano_score += ' ' + soprano_note
else:
soprano_score += ' ' + soprano_note + duration
if row[3] != '':
slur = int(row[3])
soprano_score += ' ('
slur -= 1
if "x" in tags:
soprano_score += ' \\forceBreak\n'
if "divisio" in soprano_note:
soprano_score += '\n'
if "finalis" in soprano_note:
soprano_score += '\n'
soprano_prev_note = soprano_note
soprano_prev_pitch = row[4]
#Forced line breaks
##Alto
##TODO These should be separated out into a function
alto_note = row[5]
alto_pitch = row[5]
if alto_note != '':
alto_total_duration = lilynotelength(str(alto_duration))
alto_score += alto_prev_note + alto_total_duration + ' '
if alto_pitch == alto_prev_pitch and 'r' not in alto_note:
alto_score += '~ '
if "a" in tags:
alto_note = '\\shiftRight ' + alto_note
if "A" in tags:
alto_note = '\\shiftRightB ' + alto_note
if division != 0:
alto_score += divison_marker + '\n'
alto_duration = float(beats)
alto_prev_note = alto_note
alto_prev_pitch = alto_pitch
else:
alto_duration += float(beats)
if voiceline_started == 'a' and alto_note != '':
voiceline_start += alto_total_duration
voiceline_started = 0
if voiceline_ended == 'a' and alto_note != '':
voiceline_end += alto_total_duration
voiceline_ended = 0
voiceline_score += '\n\\voiceLine ' + voiceline_staffA + ' ' + voiceline_staffB + ' ' + voiceline_start + ' ' + voiceline_end
##Tenor
tenor_note = row[6]
tenor_pitch = row[6]
if tenor_note != '':
tenor_total_duration = lilynotelength(str(tenor_duration))
tenor_score += tenor_prev_note + tenor_total_duration + ' '
if tenor_pitch == tenor_prev_pitch and 'r' not in tenor_note:
tenor_score += '~ '
if "t" in tags:
tenor_note = '\\shiftRight ' + tenor_note
if "T" in tags:
tenor_note = '\\shiftRightB ' + tenor_note
if division != 0:
tenor_score += divison_marker + '\n'
tenor_duration = float(beats)
tenor_prev_note = tenor_note
tenor_prev_pitch = tenor_pitch
else:
tenor_duration += float(beats)
if voiceline_started == 't' and tenor_note != '':
voiceline_start += tenor_total_duration
voiceline_started = 0
if voiceline_ended == 't' and tenor_note != '':
voiceline_end += tenor_total_duration
voiceline_ended = 0
voiceline_score += '\n\\voiceLine ' + voiceline_staffA + ' ' + voiceline_staffB + ' ' + voiceline_start + ' ' + voiceline_end
##Bass
bass_note = row[7]
bass_pitch = row[7]
if bass_note != '':
bass_total_duration = lilynotelength(str(bass_duration))
bass_score += bass_prev_note + bass_total_duration + ' '
if bass_pitch == bass_prev_pitch and 'r' not in bass_note:
bass_score += '~ '
if "b" in tags:
bass_note = '\\shiftRight ' + bass_note
if "B" in tags:
bass_note = '\\shiftRightB ' + bass_note
if division != 0:
bass_score += divison_marker + '\n'
bass_duration = float(beats)
bass_prev_note = bass_note
bass_prev_pitch = bass_pitch
else:
bass_duration += float(beats)
if voiceline_started == 'b' and bass_note != '':
voiceline_start += bass_total_duration
voiceline_started = 0
if voiceline_ended == 'b' and bass_note != '':
voiceline_end += bass_total_duration
voiceline_ended = 0
voiceline_score += '\n\\voiceLine ' + voiceline_staffA + ' ' + voiceline_staffB + ' ' + voiceline_start + ' ' + voiceline_end
##Voicelines
#Start of voiceline
if row[9] != '' and voiceline == 0:
if "s" in row[9]:
voiceline_staffA = '"up"'
voiceline_start = row[4]
voiceline_started = 's'
if "t" in row[9]:
voiceline_staffA = '"up"'
voiceline_start = row[5]
voiceline_started = 't'
if "t" in row[9]:
voiceline_staffA = '"down"'
voiceline_start = row[6]
voiceline_started = 't'
if "b" in row[9]:
voiceline_staffA = '"down"'
voiceline_start = row[7]
voiceline_started = 'b'
voiceline = 1
voiceline_score += '\ns' + lilynotelength(str(voiceline_duration))
voiceline_duration = float(beats)
elif row[9] != '' and voiceline == 1:
if "s" in row[9]:
voiceline_staffB = '"up"'
voiceline_end = row[4]
voiceline_ended = 's'
if "a" in row[9]:
voiceline_staffB = '"up"'
voiceline_end = row[5]
voiceline_ended = 'a'
if "t" in row[9]:
voiceline_staffB = '"down"'
voiceline_end = row[6]
voiceline_ended = 't'
if "b" in row[9]:
voiceline_staffB = '"down"'
voiceline_end = row[7]
voiceline_ended = 'b'
voiceline = 0
voiceline_duration = float(beats)
else:
voiceline_duration += float(beats)
#
#
# voiceline = 1 voiceline_start = col(SATB)
i = i + 1
division = 0
if "divisioMinima" in soprano_note:
division = 1
divison_marker = soprano_note
if "divisioMaior" in soprano_note:
division = 2
divison_marker = soprano_note
if "divisioMaxima" in soprano_note:
division = 3
divison_marker = soprano_note
if "finalis" in soprano_note:
division = 4
divison_marker = soprano_note
#Write last notes of chords
alto_score += alto_prev_note + lilynotelength(str(alto_duration)) + ' ' + divison_marker
tenor_score += tenor_prev_note + lilynotelength(str(tenor_duration)) + ' ' + divison_marker
bass_score += bass_prev_note + lilynotelength(str(bass_duration)) + ' ' + divison_marker
#TODO Add to gabctk: "0" for mult if soprano contains bar
#Hacks...
#lyrics = lyrics.replace('*', '‍*')
lyrics = lyrics.replace('‍*‍*', '\n\set stanza = " ** " ')
lyrics = lyrics.replace('‍* ', '\n\set stanza = " * " ')
#soprano_score = soprano_score.replace('\\bar \"\"', '')
#soprano_score = soprano_score.replace('\\bar \"\'\"', '\\quarterBar')
#soprano_score = soprano_score.replace('\\bar \"\"', '\\halfBar')
#soprano_score = soprano_score.replace('\\bar \"|\"', '\\singleBar')
#soprano_score = soprano_score.replace('\\bar \"||\"', '\\doubleBar')
soprano_score = soprano_score.replace(') (', ' ')
soprano_score = soprano_score.replace(' \\normalsize)',') \\normalsize')
soprano_score = soprano_score.replace(' (\\tiny','\\tiny (')
lyrics = lyrics.replace('--', ' --')
#Remove all 2/2
alto_score = alto_score.replace('2*2/2','2')
tenor_score = tenor_score.replace('2*2/2','2')
bass_score = bass_score.replace('2*2/2','2')
#Replace 2*1/2 with 4
alto_score = alto_score.replace('2*1/2','4')
tenor_score = tenor_score.replace('2*1/2','4')
bass_score = bass_score.replace('2*1/2','4')
#Remove all 0/2
alto_score = re.sub('^2\*0\/2','',alto_score)
tenor_score = re.sub('^2\*0\/2','',tenor_score)
bass_score = re.sub('^2\*0\/2','',bass_score)
#Remove spaces at start
alto_score = re.sub('^\s','',alto_score)
tenor_score = re.sub('^\s','',tenor_score)
bass_score = re.sub('^\s','',bass_score)
#NOH uses minims for all "plural" lengths outside soprano line. (Instead of dotted quavers.)
alto_score = alto_score.replace('4.','2*3/4')
tenor_score = tenor_score.replace('4.','2*3/4')
bass_score = bass_score.replace('4.','2*3/4')
##Write file
#Fields: Name, mode,
with open("/iomega/CP/NOH/csv/template.ly", "rt") as ly_template:
#with open("/home/ahinkley/Documents/gabctoly/template.ly", "rt") as ly_template:
for line in ly_template:
if 'SOPRANO_PART' in line:
ly_file.write(soprano_score + '\n')
elif 'ALTO_PART' in line:
ly_file.write(alto_score + '\n')
elif 'TENOR_PART' in line:
ly_file.write(tenor_score + '\n')
elif 'BASS_PART' in line:
ly_file.write(bass_score + '\n')
elif 'VOICELINES' in line:
ly_file.write(voiceline_score + '\n')
elif 'LYRICS' in line:
ly_file.write(lyrics)
elif 'KEY_SIGNATURE' in line:
line = line.replace('KEY_SIGNATURE', str(ly_key))
ly_file.write(line)
elif 'GABC_TITLE' in line:
line = line.replace('GABC_TITLE', str(ly_title))
ly_file.write(line)
elif 'GABC_GENRE' in line:
line = line.replace('GABC_GENRE', str(ly_genre))
ly_file.write(line)
elif 'GABC_MODE' in line:
line = line.replace('GABC_MODE', str(ly_mode))
ly_file.write(line)
elif 'NOH_PAGE' in line:
line = line.replace('NOH_PAGE', str(noh_page))
ly_file.write(line)
else:
ly_file.write(line)