-
Notifications
You must be signed in to change notification settings - Fork 0
/
scoring_simile.R
424 lines (347 loc) · 12.7 KB
/
scoring_simile.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
#' ngrukkon as in Müllensiefen & Frieler (2004)
#'
#' @param x
#' @param y
#' @param N
#'
#' @return
#' @export
#'
#' @examples
ngrukkon <- function(x, y, N = 3){
x <- itembankr::get_all_ngrams(x, N = N) %>% dplyr::pull(value)
y <- itembankr::get_all_ngrams(y, N = N) %>% dplyr::pull(value)
joint <- c(x, y) %>% table()
tx <- factor(x, levels = names(joint)) %>% table()
ty <- factor(y, levels = names(joint)) %>% table()
1 - sum(abs(tx - ty))/(length(x) + length(y))
}
#' An ngrukkon wrapper to produce warnings and return NAs rather than stop if one entry is too short
#'
#' @param x
#' @param y
#' @param N
#'
#' @return
#' @export
#'
#' @examples
ngrukkon_safe <- function(x, y, N = 3) {
ngrukkon_warning(x)
ngrukkon_warning(y)
if( length(x) < N | length(y) < N ) {
res <- NA
} else {
res <- ngrukkon(x, y, N)
}
res
}
ngrukkon_warning <- function(v) {
# ngrukkon must be used on intervals not pitches, so warn based on a guess that the input might be pitch rather than interval values
if(mean(v, na.rm = TRUE) > 20) warning("Are you definitely using intervals for ngrukkon?")
}
#' get harmonies via the Krumhansl-Schmuckler algorithm
#'
#' @param pitch_vec
#' @param segmentation
#' @param only_winner
#'
#' @return
#' @export
#'
#' @examples
get_implicit_harmonies <- function(pitch_vec, segmentation = NULL, only_winner = TRUE){
#warning('Segmentation format must be as segment ID')
# Krumhansl-Schmuckler algorithm
ks_weights_major <- c(6.33, 2.23, 3.48, 2.33, 4.38, 4.09, 2.52, 5.19, 2.39, 3.66, 2.29, 2.88)
ks_weights_minor <- c(6.33, 2.68, 3.52, 5.38, 2.60, 3.53, 2.54, 4.75, 3.98, 2.69, 3.34, 3.17)
if(!is.null(segmentation)){
if(length(segmentation) != length(pitch_vec)){
stop("Segmentation must be of same length as pitch")
}
s <- unique(segmentation)
return(
purrr::map_dfr(s, function(x){
pv <- pitch_vec[segmentation == x]
tidyr::tibble(segment = x, key = get_implicit_harmonies(pv, NULL, only_winner = only_winner) %>% dplyr::pull(key))
})
)
}
pitch_freq <- table(factor(pitch_vec %% 12, levels = 0:11))
correlations <- purrr::map_dfr(0:11, function(t){
w_major <- cor.test(pitch_freq, ks_weights_major[((0:11 - t) %% 12) + 1]) %>% broom::tidy() %>% dplyr::pull(estimate)
w_minor <- cor.test(pitch_freq, ks_weights_minor[((0:11 - t) %% 12) + 1]) %>% broom::tidy() %>% dplyr::pull(estimate)
dplyr::bind_rows(tidyr::tibble(transposition = t, match = w_major, type = "major", key = sprintf("%s-maj", itembankr::pc_labels_flat[t+1])),
tidyr::tibble(transposition = t, match = w_minor, type = "minor", key = sprintf("%s-min", itembankr::pc_labels_flat[t+1])))
}) %>% dplyr::arrange(desc(match))
if(only_winner){
return(correlations[1,])
}
correlations
}
bootstrap_implicit_harmonies <- function(pitch_vec, segmentation = NULL, sample_frac = .8, size = 10){
if(!is.null(segmentation)){
segments <- unique(segmentation)
ret <-
purrr::map_dfr(segments, function(seg){
bootstrap_implicit_harmonies(pitch_vec[segmentation == seg],
NULL,
sample_frac = sample_frac,
size = size) %>%
dplyr::mutate(segment = seg)
})
return(ret)
}
l <-length(pitch_vec)
sample_size <- max(1, round(sample_frac * l))
bs <-
purrr::map_dfr(1:size, function(x){
pv <- sample(pitch_vec, replace = T, sample_size)
get_implicit_harmonies(pitch_vec = pv, only_winner = TRUE)
})
best_key <- bs %>% dplyr::count(key) %>% dplyr::arrange(dplyr::desc(n)) %>% dplyr::pull(key)
bs %>% dplyr::filter(key == best_key[1]) %>% head(1)
}
#' Compute the rhythfuzz measure
#'
#' @param dur_vec1
#' @param dur_vec2
#'
#' @return
#' @export
#'
#' @examples
rhythfuzz <- function(dur_vec1, dur_vec2){
edit_sim(intToUtf8(dur_vec1 + 128), intToUtf8(dur_vec2 + 128))
}
#' Compute harmcore
#'
#' @param pitch_vec1
#' @param pitch_vec2
#' @param segmentation1
#' @param segmentation2
#' @param segmentation_type
#'
#' @return
#' @export
#'
#' @examples
harmcore <- function(pitch_vec1, pitch_vec2, segmentation1 = NULL, segmentation2 = NULL, segmentation_type = c("phrase_boundary_marker",
"segment_id")){
segmentation_type <- match.arg(segmentation_type)
# phrase_boundary_marker e.g., c(0, 0, 0, 1, 0 0, 1)
# segment_id e.g., c(1, 1, 1, 2, 2, 2, 3)
if(!is.null(segmentation1) & !is.null(segmentation2)) {
if(segmentation_type == "phrase_boundary_marker") {
segmentation1 <- cumsum(segmentation1)
segmentation2 <- cumsum(segmentation2)
}
}
implicit_harm1 <- get_implicit_harmonies(pitch_vec1, segmentation1) %>% dplyr::pull(key)
implicit_harm2 <- get_implicit_harmonies(pitch_vec2, segmentation2) %>% dplyr::pull(key)
common_keys <- levels(factor(union(implicit_harm1, implicit_harm2)))
implicit_harm1 <- factor(implicit_harm1, levels = common_keys) %>% as.integer()
implicit_harm2 <- factor(implicit_harm2, levels = common_keys) %>% as.integer()
edit_sim(intToUtf8(implicit_harm1), intToUtf8(implicit_harm2))
}
harmcore2 <- function(pitch_vec1, pitch_vec2, segmentation1 = NULL, segmentation2 = NULL, segmentation_type = c("phrase_boundary_marker",
"segment_id")) {
# phrase_boundary_marker e.g., c(0, 0, 0, 1, 0 0, 1)
# segment_id e.g., c(1, 1, 1, 2, 2, 2, 3)
if(segmentation_type == "phrase_boundary_marker") {
segmentation1 <- cumsum(segmentation1)
segmentation2 <- cumsum(segmentation2)
}
implicit_harm1 <- bootstrap_implicit_harmonies(pitch_vec1, segmentation1) %>% dplyr::pull(key)
implicit_harm2 <- bootstrap_implicit_harmonies(pitch_vec2, segmentation2) %>% dplyr::pull(key)
common_keys <- levels(factor(union(implicit_harm1, implicit_harm2)))
implicit_harm1 <- factor(implicit_harm1, levels = common_keys) %>% as.integer()
implicit_harm2 <- factor(implicit_harm2, levels = common_keys) %>% as.integer()
harmcore2 <- edit_sim(intToUtf8(implicit_harm1), intToUtf8(implicit_harm2))
}
#little helper to calculate modus of simple vector
modus <- function(x){
t <- table(x)
as(names(t[t == max(t)]), class(x))
}
#find a list of candidates for best transpositions for two pitch vectors, based on basic stats
get_transposition_hints <- function(pitch_vec1, pitch_vec2){
ih1 <- get_implicit_harmonies(pitch_vec1, only_winner = TRUE)
key1 <- ih1 %>% dplyr::pull(key)
pc1 <- ih1 %>% dplyr::pull(transposition)
ih2 <- get_implicit_harmonies(pitch_vec2, only_winner = TRUE)
pc2 <- ih2 %>% dplyr::pull(transposition)
key_diff <- (pc2 - pc1) %% 12
#messagef("Best key 1 = %s, best key 2 = %s, key diff = %d", key1, ih2 %>% head(1) %>% dplyr::pull(key), key_diff )
modus1 <- modus(pitch_vec1)
modus2 <- modus(pitch_vec2)
ret <- c(modus1 - modus2,
round(mean(pitch_vec1)) - round(mean(pitch_vec2)),
round(median(pitch_vec1)) - round(median(pitch_vec2)))
octave_offset <- modus(round(ret/12))
#messagef("Octave offset = %d", octave_offset)
ret <- c(0, ret, octave_offset*12 + key_diff, octave_offset * 12 + 12 - key_diff)
unique(ret) %>% sort()
}
#finds transposition that maximize raw edit distance of two pitch vectors
#transposision in semitone of the *second* melody
find_best_transposition <- function(pitch_vec1, pitch_vec2){
trans_hints <- get_transposition_hints(pitch_vec1, pitch_vec2)
sims <- purrr:map_dfr(trans_hints, function(x){
tidyr::tibble(transposition = x, sim = edit_dist(intToUtf8(pitch_vec1), intToUtf8(pitch_vec2 + x)))
})
sims %>% dplyr::arrange(sim) %>% head(1) %>% dplyr::pull(transposition)
}
#' Score using the opti3 measure of similarity
#'
#' @param pitch_vec1
#' @param onset_vec1
#' @param pitch_vec2
#' @param onset_vec2
#' @param N
#' @param use_bootstrap
#' @param return_components
#' @param segmentation1
#' @param segmentation2
#'
#' @return
#' @export
#'
#' @examples
opti3 <- function(pitch_vec1, onset_vec1,
pitch_vec2, onset_vec2,
N = 3,
use_bootstrap = TRUE,
return_components = FALSE,
segmentation1 = NULL, segmentation2 = NULL) {
warning('It is recommended to use the more comprehensive opti3_df version of opti3')
stopifnot(all(c(pitch_vec1, pitch_vec2) > 0L))
pitch_vec1 <- round(pitch_vec1)
pitch_vec2 <- round(pitch_vec2)
v_ngrukkon <- ngrukkon_safe(diff(pitch_vec1), diff(pitch_vec2), N = N)
ioi1 <- c(NA, diff(onset_vec1))
ioi2 <- c(NA, diff(onset_vec2))
ioi_class1 <- classify_duration(ioi1)
ioi_class2 <- classify_duration(ioi2)
v_rhythfuzz <- rhythfuzz(ioi_class1, ioi_class2)
if(use_bootstrap) {
v_harmcore <- harmcore2(pitch_vec1, pitch_vec2)
}
else{
v_harmcore <- harmcore(pitch_vec1, pitch_vec2)
}
opti3 <- 0.505 * v_ngrukkon + 0.417 * v_rhythfuzz + 0.24 * v_harmcore - 0.146
#messagef("ngrukkon = %.3f, rhythfuzz = %.3f, harmcor = %.3f, opti3 = %.3f",
# v_ngrukkon, v_rhythfuzz, v_harmcore, opti3)
opti3 <- max(min(opti3, 1), 0)
if(return_components) {
list("opti3" = opti3,
"ngrukkon" = v_ngrukkon,
"rhythfuzz" = v_rhythfuzz,
"harmcore" = v_harmcore)
} else {
opti3
}
}
#' read a pYIN note track (outputted from Sonic Annotator or Tony) and make it nice
#'
#' @param fname
#' @param style
#'
#' @return
#' @export
#'
#' @examples
read_melody <- function(fname, style = c("sonic_annotator", "tony")) {
warning("Have you specified whether it is a Sonic Annotator vs. Tony pitch track correctly?")
melody <-
read.csv(fname, header = FALSE) %>%
tidyr::as_tibble() %>%
{if(style == "sonic_annotator") dplyr::rename(., onset = V1, freq = V3, dur = V2) else dplyr::rename(.,onset = V1, freq = V2, dur = V3)} %>%
itembankr::produce_extra_melodic_features() ## NB! sonic annotator and tony output different column orders, hence the above
if(any(is.na(melody$note)) || any(is.infinite(melody$note))){
stop("Warning: Melody (%s) contains invalid pitches", fname)
}
if(any(melody$ioi[!is.na(melody$ioi)] < .01)){
stop("Warnings: Melody (%s) contains IOIs less than 1 ms, possibly no note track", fname)
}
melody
}
#' opti3 for melodies read by read_melody
#' returns sorted tibble of transpositions of melody2 and opti3 similarity
#'
#' @param melody1
#' @param melody2
#' @param N
#' @param use_bootstrap
#' @param return_winner
#'
#' @return
#' @export
#'
#' @examples
opti3_df <- function(melody1, melody2, N = 3, use_bootstrap = FALSE, return_winner = TRUE){
trans_hints <- get_transposition_hints(melody1$note, melody2$note)
v_rhythfuzz <- rhythfuzz(melody1$ioi_class, melody2$ioi_class)
v_ngrukkon <- ngrukkon_safe(diff(melody1$note), diff(melody2$note), N = N)
sims <- purrr::map_dfr(trans_hints, function(th){
if(use_bootstrap) {
v_harmcore <- harmcore2(melody1$note, melody2$note + th, segmentation1 = melody1$phrasbeg, segmentation2 = melody2$phrasbeg)
}
else{
v_harmcore <- harmcore(melody1$note, melody2$note + th, segmentation1 = melody1$phrasbeg, segmentation2 = melody2$phrasbeg)
}
opti3 <- 0.505 * v_ngrukkon + 0.417 * v_rhythfuzz + 0.24 * v_harmcore - 0.146
opti3 <- max(min(opti3, 1), 0)
tibble::tibble(transposition = th,
ngrukkon = v_ngrukkon,
rhythfuzz = v_rhythfuzz,
harmcore = v_harmcore,
opti3 = opti3)
})
res <- sims %>% dplyr::arrange(dplyr::desc(opti3))
if(return_winner) {
res %>% dplyr::slice(1)
} else {
res
}
}
best_subsequence_similarity <- function(melody1, melody2){
#windowed version, shifts shorter melody along longer
#returns tibble of shift offsets and highest opti3 similarity
shorter <- melody1
longer <- melody2
swap <- "1 along 2"
l1 <- nrow(melody1)
l2 <- nrow(melody2)
if(l2 < l1){
shorter <- melody2
longer <- melody1
swap <- "2 along 1"
}
l1 <- nrow(shorter)
l2 <- nrow(longer)
d_l <- l2 - l1
purrr::map_dfr(1:(d_l + 1), function(offset){
#messagef("Offset %d", offset)
longer_snip <- longer[seq(offset, offset + l1 - 1),]
tidyr::tibble(offset = offset - 1, sim = opti3_df(shorter, longer_snip) %>% head(1) %>% dplyr::pull(opti3))
}) %>% dplyr::mutate(process = swap) %>% dplyr::arrange(desc(sim))
}
messagef <- function(...) message(sprintf(...))
asc <- function(x, n = 1){
raw <- charToRaw(x)
if(n < 0){
n <- length(raw) + n + 1
}
if(n == 0){
return(strtoi(raw, 16))
}
strtoi(raw, 16)[n]
}
edit_dist <- function(s, t){
adist(s,t)[1,1]
}
edit_sim <- function(s, t){
1 - edit_dist(s, t)/max(nchar(s), nchar(t))
}