-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathclean.R
458 lines (386 loc) · 13.6 KB
/
clean.R
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
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
library(magrittr)
KEYS <- list(
'B' = c('f#', 'c#', 'g#', 'd#', 'b#'), # C# and FX not implemented yet
'E' = c('f#', 'c#', 'g#', 'd#'),
'A' = c('f#', 'c#', 'g#'),
'D' = c('f#', 'c#'),
'G' = c('f#'),
'C' = character(0),
'F' = c('b-'),
'Bb'= c('b-', 'e-'),
'Eb'= c('b-', 'e-', 'a-'),
'Ab'= c('b-', 'e-', 'a-', 'd-') # Db, Gb, Cb not implemented yet
)
iif <- function(cond, trueval, falseval) {
if (cond) {
trueval
} else {
falseval
}
}
splitBASbasedOnInput <- function(lines) {
if (FALSE) {
lines <- bas[header$startLine[2]:header$endLine[2]]
}
code2 <- unlist(strsplit(lines, ":"))
hasPrint <- regexec("PRINT ", code2)>0
labels <- code2[hasPrint] %>%
gsub("PRINT ", "", .) %>%
gsub('"', "", .)
hasInput <- regexec("INPUT ", code2)>0
inputVar <- code2[hasInput] %>%
substr(., nchar(.), nchar(.))
hasIf <- regexec(paste0("IF ",inputVar, "="), code2)>0
cond <- code2[hasIf] %>%
gsub(paste0(".*IF ",inputVar, "="), "", .) %>%
gsub(" THEN ", "\t", .) %>%
gsub(" ELSE.*", "", .)
x <- strsplit(cond, '\t')
header <- as.data.frame(list(Nr=sapply(x, getElement, 1),
FirstLine=labels,
Goto=sapply(x,getElement,2)))
hasLineNr <- regexec("^[0-9]* ", code2) > 0
LineNrs <- gsub(" .*", "", code2)
LineNrs[!hasLineNr] <- ""
header$startLine <- match(header$Goto, LineNrs)
hasPlay <- (regexec("PLAY", code2)>0) * 1
countPlays <- rev(cumsum(rev(hasPlay)))
header$endLine <- header$startLine + countPlays[header$startLine] - countPlays[c(header$startLine[2:nrow(header)] - 1, length(code2))] - 1
header$playstatements <- sapply(1:nrow(header), combinePlaystatement, header, code2)
header
}
combinePlaystatement <- function(i, header, statement) { # i <- 19
all <- statement[header$startLine[i]:header$endLine[i]]
all <- gsub(":PRINT \"[^\"]*\"", "", all) %>%
gsub("LOCATE [0-9]*,[0-9]*", "", .) %>%
gsub("^REM.*", "", .) %>%
gsub(".*PLAY[ ]*", "", .) %>%
gsub('"', "", .) %>%
gsub(':GOTO [0-9]*', "", .) %>%
gsub('^GOTO [0-9]*', "", .) %>%
trimws() %>%
gsub("\\+", "#", .) # BASIC allows both a "+" or a "#" as a sharp indicator
# make sure we have space padding between all notes....
comb <- paste0(all, collapse=" ") %>%
gsub("([a-g])", " \\1", .) %>% # put a space before each note
gsub("(l[0-9])", " \\1", .) %>% # put a space before length instruction
gsub("(o[0-9])", " \\1", .) %>% # put a space before octave selector
gsub(" ", " ", .) %>% # remove double spaces
gsub(" ", " ", .) %>% # remove double spaces
gsub(" #", "#", .) %>% # in BASIC you can write "f #"
trimws()
comb
}
LINERANGES <- list(
"PSALMS.BAS" = 8:157,
"SKRIF.BAS" = 8:57
)
dofile <- function(fn, mainvar="A") {
if (FALSE) {
fn <- "SKRIF.BAS"
mainvar <- "A"
}
bas <- readLines(fn)
i <- simplify2array(regexec(" ", bas))
linenr <- substr(bas,1,i-1)
statement <- substr(bas,i+1, nchar(bas))
playstart <- which(regexec("PLAY ", statement)>0)[1]
processheader <- function(line, mainvar) {
# line <- statement[8]
line %>%
gsub(paste0(".*IF ", mainvar, "="), "", .) %>%
gsub(" .*PRINT \"", "\t",.) %>%
gsub(" .*PRINT CHR[^\"]*\"", "\t",.) %>%
gsub("\":GOTO ", "\t", .)
}
headerRange <- LINERANGES[[fn]]
x <- processheader(statement[headerRange], mainvar) %>%
strsplit("\t")
if (any(sapply(x, length)!=3)) stop("header detection problem....")
header <- as.data.frame(list(Nr=sapply(x, getElement, 1),
FirstLine=sapply(x, getElement, 2),
Goto=sapply(x,getElement,3)))
header$startLine <- match(header$Goto, linenr)
# find the final GOTO statement that ends each song
finalGoto <- gsub(".*GOTO ", "GOTO ", statement[header$startLine[2]-1])
lastSongStartline <- header$startLine[nrow(header)]
lastSongEndline <- lastSongStartline + which(regexpr(finalGoto, statement[lastSongStartline:length(statement)])>0)[1] - 1
header$endLine <- c(header$startLine[2:nrow(header)]- 1, lastSongEndline)
if (fn=="SKRIF.BAS") {
# same tune - just a GOTO statement
header[50, c("startLine", "endLine")] <- header[25, c("startLine", "endLine")]
}
header$playstatements <- sapply(1:nrow(header), combinePlaystatement, header, statement)
header$hasMultiple <- regexec("INPUT", header$playstatements) > 0
# sum(header$hasMultiple)
splitMultiple <- lapply(which(header$hasMultiple), function(i) {
code <- bas[header$startLine[i]:header$endLine[i]]
split <- splitBASbasedOnInput(code)
split$Nr <- paste0(header$Nr[i], letters[1:nrow(split)])
split$FirstLine <- paste0(header$FirstLine[i], " - ", split$FirstLine)
split
})
header <- header[!header$hasMultiple, ]
allSplit <- data.table::rbindlist(splitMultiple)
header$hasMultiple <- NULL
all <- rbind(header, allSplit)
save(all, file=gsub("BAS", "RData", fn))
PROPERNAME <- list(
`PSALMS.BAS` = "Psalm",
`SKRIF.BAS` = "Skrifberyming"
)
DoSong <- function(i) {
if (FALSE) {
fn <- "PSALMS.BAS"
load("PSALMS.RData")
i <- 19
}
BetterTitle <- paste0(PROPERNAME[[fn]], " ", gsub("a-z", "", all$Nr[i]))
message(i, " = ", BetterTitle)
j <- as.integer(gsub("[a-z]", "", all$Nr[i]))
padzero <- 2 - floor(log(j, base=10))
ABC <- Play2ABC(tolower(all$playstatements[i]), BetterTitle, all$FirstLine[i])
outfn <- file.path(gsub('\\.bas', '', tolower(fn)), paste0(PROPERNAME[[fn]], paste0(rep("0", padzero), collapse=""), all$Nr[i], '.abc'))
if (!dir.exists(dirn <- dirname(outfn))) dir.create(dirn)
writeLines(ABC, outfn)
TRUE
}
# saveFile
sapply(seq.int(nrow(all)), DoSong)
# DoSong(3)
}
Play2ABC <- function(play, title, subtitle=NULL) {
if (FALSE) {
i <- 19
play <- all$playstatements[i]
title <- "Psalm 1"
subtitle <- NULL
}
fixSharpFlat <- function(x) {
# transform f# to ^f, and f- to _f
sharp <- regexec("#", x)>0
flat <- regexec('-', x)>0
if (sharp) {
x <- paste0('^', gsub('#', '', x))
} else if (flat) {
x <- paste0('_', gsub('-', '', x))
} else {
# we are changing the colour of the note, so we need an explict normalize it
x <- paste0("=", x)
}
x
}
processNote <- function(x) {
if (O==4) {
fx <- tolower
post <- "'"
} else if (O==3) {
fx <- tolower
post <- ''
} else if (O==2) {
fx <- toupper
post <- ''
} else if (O==1) {
fx <- toupper
post <- ","
} else {
stop("Cannot handle Octave ", O, " for note ", x)
}
# local length multiplier
LL <- 1
if (regexpr("\\.", x)>0) {
# message("found . - L was ", L)
LL <- 1.5 # only applies to this note, only a local variable L.
# message("setting L to ", LL)
x <- gsub("\\.", "", x)
}
newnote <- paste0(fx(x), post)
# Basic music has "#" for sharp, and "-" for flat.
# We don't need this in ABC, for we have a key defined.
# ABC notation is "_" for flat, and "^" for sharp, with "=" for natural
# see if this note is affected by the Key or not
nx <- splitNoteColor(x)
if (noteColour[[nx$notes]] == nx$col) { # it has the same sharp or flat, so we can remove it...
newnote <- gsub('#|\\-', '', newnote) # remove the "#" or "-"
} else { # the default is 'n sharp, but we have a flat. [or the other way around]
changeColour(x)
newnote <- fixSharpFlat(newnote)
}
if (L==2) {
newnote <- paste0(newnote, 4*LL)
} else if (L==4) {
newnote <- paste0(newnote, 2*LL)
} else if (L==6) {
warning("assume L6 in Skrifberyming 19 = L8 :-) ")
} else if (L==8) {
# do nothing, the note is fine as is.
if (LL != 1) {
message("Need a 'dotted' regular note instruction for ABC, which is most probably a mistake: ", x, " - ", title, "; ", play)
newnote <- paste0(newnote, 3)
}
} else if (L==16) {
stopifnot(LL==1)
newnote <- paste0(newnote, "/")
} else {
stop("Cannot handle L=", L, "yet. Fix please. LL=", LL)
}
if (needToCloseSlur) { # close the slur
newnote <- paste0(newnote, ")")
needToCloseSlur <<- FALSE
}
newnote
}
executeInstruction <- function(x) { # x<- instructions[[1]]
if (regexec("o", x, ignore.case = TRUE)> 0) { # change octave
O <<- as.integer(gsub("o", "", x, ignore.case = TRUE))
NA
} else if (regexec("t", x, ignore.case = TRUE) > 0) { # Tempo
Q <<- as.integer(gsub("t", "", x, ignore.case = TRUE))
NA
} else if (regexec("m", x, ignore.case = TRUE) > 0) { # Change Mode - needs to happen before node Length, because we have a ML
prevMode <- M
M <<- tolower(gsub("m", "", x, ignore.case = TRUE))
if (M=="l") { # mode legato
"("
} else { # mode normal
needToCloseSlur <<- prevMode=="l"
NA
}
} else if (regexec("l", x, ignore.case = TRUE) > 0) { # Change Note Length
L <<- as.integer(gsub("l", "", x, ignore.case = TRUE))
if (is.na(L)) stop("Error setting note length: '",x,"'")
NA
} else if (regexec("p", x, ignore.case = TRUE) > 0) { # Change Mode
if (x=="p4") {
"z2\nyyyy"
} else if (x=="p8") {
"yyyy\nz"
} else if (x=="p16") {
"yyyy\nz/"
} else {
stop("Unknown stop instruction: ", x)
}
} else if (regexec("[a-g]", x, ignore.case = TRUE) > 0) {
# process this actual note
tryCatch(processNote(x),
error=function(e) stop("Error processing note '",x,"': ", e))
} else {
stop("doesnot understand '", x, "'")
}
}
playInstructions <- function(play) {
instructions <- strsplit(play, " ")[[1]]
n <- sapply(instructions, executeInstruction)
n <- n[!is.na(n)]
paste0(n, collapse=" ")
}
splitNoteColor <- function(x) {
res <- list(notes=sub("[^a-g]", "", x),
col=gsub("[a-g]", "", x))
res$col[res$col==""] <- "="
res
}
changeColour <- function(notes) { # notes <- KEYS$D
y <- splitNoteColor(notes)
noteColour[y$notes] <<- y$col
# print(noteColour)
}
findkey <- function(play) {
if (FALSE) {
play <- all$playstatements[137]
}
play <- gsub("\\.", "", play) %>%
gsub("\\+", "#", .)
f <- table(strsplit(play, " ")[[1]])
isNote <- regexpr("[a-g]", names(f), ignore.case = TRUE)>0
f <- f[isNote]
TYPES <- c("=", "#", "-")
NOTES <- letters[1:7]
allcomb <- outer(NOTES, TYPES, FUN = paste0)
rownames(allcomb) <- NOTES
colnames(allcomb) <- TYPES
hascomb <- (allcomb==TRUE)*0 # copy the structure
m <- match(tolower(names(f)), gsub("=", "", allcomb))
hascomb[m] <- unname(f)
# find the most prominent note
mostprominent <- apply(hascomb, 1, FUN = function(x) {TYPES[which(max(x)==x)[1]]})
# compare KEYS against hascomb
if (mostprominent['f']=="#") {
if (mostprominent['c']=="#") {
if (mostprominent['g']=="#") {
if (mostprominent['d']=="#") {
if (mostprominent['b']=="#") {
"B"
} else {
"E"
}
} else {
"A"
}
} else {
"D"
}
} else {
"G"
}
} else {
# molle
if (mostprominent['b']=="-") {
if (mostprominent['e']=="-") {
if (mostprominent['a']=="-") {
if (mostprominent['d']=="-") {
"Ab"
} else {
"Eb"
}
} else {
"Bb"
}
} else {
"F"
}
} else {
"C"
}
}
}
K <- findkey(play)
Q <- 100 # default tempo, if not specified....
O <- 3 # default octave for Basic
L <- 4 # default note length
M <- 'n' # music mode (normal vs. legato )
needToCloseSlur <- FALSE
noteColour <- setNames(rep('=',7), letters[1:7])
changeColour(KEYS[[K]])
notes <- playInstructions(play)
if (regexec("\n", notes)<0) { # no newlines in the notes
# try some fancy method to split the lines....
notes <- gsub("4", "4\n", notes)
}
lines <- strsplit(gsub("\n", "\n%w:words come here\n", notes), "\n")[[1]]
lines[1] <- paste0('yy ', lines[1])
lines[length(lines)] <- paste0(lines[length(lines)], ' yy |]')
lines <- c(lines, '%w:words come here')
return <- c(
'%%vocalfont Arial 14',
'X:1',
paste0('T:', title),
iif(is.null(subtitle), '%', paste0('C:', subtitle)),
'L:1/4',
'M:C|', # unsure when to display this.....
paste0('K:', K),
paste0('Q:1/2=', Q),
lines
)
#
return
}
# dofile("PSALMS.BAS")
dofile("SKRIF.BAS")
if (FALSE) {
play <- 't100 mn l8 o2 g g g l4 o3 c o2 g o3 c o2 g p4 l8 o3 c c l4 d d c p8 l8 c o2 l4 a l8 a a l4 f l8 b- a l4 g f p4 l8 o2 a a o3 l4 d o2 a o3 d o2 a p4 p8 l8 o3 d l4 e l8 e e d d d d l4 o2 b b l8 g g o3 c o2 b a g l4 a l8 a a l4 b g l8 o3 c o2 b a g l4 a a g g p4 g l8 a- a- a- a- g g g g l4 f f p4 f l8 e- e- e- e- d- d- d- d- c c l4 c p4 c l8 c c c c o1 l4 b b o2 c p4 l8 c c d- d- l4 e- l8 f f g g l4 a- b- o3 c p4 l8 o3 c c d d d d l4 e l8 e e l4 c l8 c c o2 a a a a l4 f l8 f f l4 b- l8 a b- l4 g f p4 l8 o2 f f l4 f l8 f f l4 b- l8 a b- l4 o3 c l8 c c l4 o2 b- b- a l8 a a l4 g f p8 l8 o2 f l4 b- l8 f f l4 b- l8 f f l4 b- p4 p4 l8 o2 a a o3 l4 d l8 d d c c c c o2 b- b- b- b- l4 a l8 o2 a a f f b- a l4 g g f l8 f f l4 b- l8 f f l4 b- b- p4 l4 o2 a o3 d l8 d d l4 c c o2 b b p4 l8 o2 a g l2 o3 e l4 d d l2 c c'
title <- "Geloofsbelydenis"
subtitle <- NULL
res <- Play2ABC(play, title, subtitle)
writeClipboard(res)
}