compmus_match_pitch_template.Rd
Compares chroma vectors in a data frame against a list of templates, most likely key or chord profiles.
compmus_match_pitch_template(
dat,
templates,
method = "cosine",
norm = "euclidean"
)
A data frame containing chroma vectors in a pitches
column.
A data frame with a name
column for each template and
the templates themselves in a template
column.
A character string indicating which distance metric to use (see
compmus_long_distance
). Default is cosine distance.
An optional character string indicating the method for
pre-normalising each vector with compmus_normalise
. Default
is Euclidean.
A tibble with columns start
, duration
, name
, and
d
.
library(tidyverse)
circshift <- function(v, n) {
if (n == 0) v else c(tail(v, n), head(v, -n))
}
major_chord <-
c(1, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0)
minor_chord <-
c(1, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0)
chord_templates <-
tribble(
~name, ~template,
"D:min", circshift(minor_chord, 2),
"F:maj", circshift(major_chord, 5),
"A:min", circshift(minor_chord, 9),
"C:maj", circshift(major_chord, 0),
"E:min", circshift(minor_chord, 4),
"G:maj", circshift(major_chord, 7),
"B:min", circshift(minor_chord, 11)
)
get_tidy_audio_analysis("5UVsbUV0Kh033cqsZ5sLQi") %>%
compmus_align(sections, segments) %>%
select(sections) %>%
unnest(sections) %>%
mutate(
pitches =
map(segments,
compmus_summarise, pitches,
method = "mean", norm = "manhattan"
)
) %>%
compmus_match_pitch_template(chord_templates, "euclidean", "manhattan")
#> # A tibble: 63 × 4
#> start duration name d
#> <dbl> <dbl> <fct> <dbl>
#> 1 0 24.8 D:min 0.545
#> 2 0 24.8 F:maj 0.556
#> 3 0 24.8 A:min 0.537
#> 4 0 24.8 C:maj 0.531
#> 5 0 24.8 E:min 0.499
#> 6 0 24.8 G:maj 0.530
#> 7 0 24.8 B:min 0.532
#> 8 24.8 28.6 D:min 0.544
#> 9 24.8 28.6 F:maj 0.542
#> 10 24.8 28.6 A:min 0.522
#> # … with 53 more rows