compmus_match_pitch_template: Match chroma vectors against templates

View source: R/norms.R

compmus_match_pitch_templateR Documentation

Match chroma vectors against templates

Description

Compares chroma vectors in a data frame against a list of templates, most likely key or chord profiles.

Usage

compmus_match_pitch_template(
  dat,
  templates,
  method = "cosine",
  norm = "euclidean"
)

Arguments

dat

A data frame containing chroma vectors in a pitches column.

templates

A data frame with a name column for each template and the templates themselves in a template column.

method

A character string indicating which distance metric to use (see compmus_long_distance). Default is cosine distance.

norm

An optional character string indicating the method for pre-normalising each vector with compmus_normalise. Default is Euclidean.

Value

A tibble with columns start, duration, name, and d.

Examples

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")

jaburgoyne/compmus documentation built on Feb. 26, 2023, 3:44 a.m.