# Analyses the structure of kappale.mp3 for the dance video. # # Writes # tempo_kappale.rds tempo and the first beat of the whole song # laulu_kayra.rds "centre voice" envelope at 25 fps (drives the mouth) # rakenne_piirteet.csv features of each 8-bar block (the basis for the labels) # # The section labels themselves are in rakenne.csv. The agent wrote them after looking at the # features below and at spectrograms; they are an interpretation, not a measurement, so fix them # by hand if they are wrong (columns: alku_s, loppu_s, osa, laulu = nainen/mies/ei). library(av) pcm <- read_audio_bin("kappale.mp3", channels = 2) m <- matrix(as.numeric(pcm) / 2^31, nrow = 2) dec <- 2; n <- dec * floor(ncol(m) / dec) L <- colMeans(matrix(m[1, 1:n], nrow = dec)); R <- colMeans(matrix(m[2, 1:n], nrow = dec)) sr <- attr(pcm, "sample_rate") / dec mid <- (L + R) / 2; side <- (L - R) / 2 # vocals are usually panned to the centre duration <- length(mid) / sr win <- 2048; hop <- 512 hann <- 0.5 - 0.5 * cos(2 * pi * (0:(win - 1)) / (win - 1)) nf <- floor((length(mid) - win) / hop) stft <- function(x) vapply(seq_len(nf), function(i) Mod(fft(x[((i - 1) * hop + 1):((i - 1) * hop + win)] * hann))[1:(win / 2)], numeric(win / 2)) SM <- stft(mid); SS <- stft(side) f <- (0:(win / 2 - 1)) * sr / win tt <- ((seq_len(nf) - 1) * hop + win / 2) / sr fps_env <- sr / hop # 1. Tempo and beat phase (same method as tempo.R) --------------------------------------- lm <- log1p(100 * SM) flux <- c(0, colSums(pmax(lm[, -1] - lm[, -nf], 0))) flux <- flux - stats::filter(flux, rep(1 / 16, 16), sides = 2); flux[is.na(flux)] <- 0 flux <- pmax(flux, 0) ac <- acf(flux, lag.max = ceiling(fps_env * 1.1), plot = FALSE)$acf[-1]; lags <- seq_along(ac) bpm_l <- 60 * fps_env / lags; ok <- bpm_l >= 60 & bpm_l <= 200 w <- exp(-0.5 * (log2(bpm_l / 120) / 0.9)^2); best <- lags[ok][which.max((ac * w)[ok])] y <- ac[(best - 1):(best + 1)]; period <- best + 0.5 * (y[1] - y[3]) / (y[1] - 2 * y[2] + y[3]) bpm <- 60 * fps_env / period phases <- seq(0, period, length.out = 200) score <- sapply(phases, function(p) sum(flux[round(seq(p, nf - 1, by = period)) + 1])) offset <- phases[which.max(score)] / fps_env cat(sprintf("duration %.1f s, BPM %.2f, first beat %.3f s, 8 bars = %.2f s\n", duration, bpm, offset, 32 * 60 / bpm)) saveRDS(list(bpm = bpm, offset = offset, duration = duration), "tempo_kappale.rds") # 2. Centre-voice envelope (mouth) -------------------------------------------------------- band <- function(S, lo, hi) colSums(S[f >= lo & f < hi, , drop = FALSE]^2) centre <- pmax(band(SM, 250, 3500) - 2 * band(SS, 250, 3500), 0) env <- log1p(centre / stats::median(centre[centre > 0])) t25 <- seq(0, duration, by = 1 / 25) env25 <- approx(tt, env, t25, rule = 2)$y env25 <- pmin(env25 / stats::quantile(env25, 0.95), 1) saveRDS(list(t = t25, env = env25), "laulu_kayra.rds") # 3. Features of each 8-bar block ----------------------------------------------------------- # Predominant pitch of the centre channel by harmonic summation (80-700 Hz). C <- pmax(SM - 1.5 * SS, 0); Lc <- log1p(200 * C) Wc <- pmax(Lc - apply(Lc, 2, function(v) as.numeric(stats::filter(v, rep(1 / 25, 25), sides = 2))), 0) Wc[is.na(Wc)] <- 0 cand <- exp(seq(log(90), log(700), length.out = 160)); df <- f[2] sal <- sapply(cand, function(c0) { h <- c0 * 1:8; h <- h[h < 4500] colSums(Wc[round(h / df) + 1, , drop = FALSE] * 0.85^(seq_along(h) - 1)) }) f0 <- cand[max.col(sal, ties.method = "first")]; smax <- apply(sal, 1, max) strong <- smax > stats::median(smax) blk_len <- 32 * 60 / bpm starts <- seq(offset, duration - 4, by = blk_len) piirteet <- do.call(rbind, lapply(starts, function(s) { i <- tt >= s & tt < s + blk_len data.frame( alku_s = round(s, 2), loppu_s = round(min(s + blk_len, duration), 2), voimakkuus_db = round(10 * log10(mean(colSums(SM[, i]^2))), 1), keski_sivu_db = round(10 * log10(sum(band(SM, 250, 3500)[i]) / sum(band(SS, 250, 3500)[i])), 1), basso_db = round(10 * log10(mean(band(SM, 30, 150)[i])), 1), savel_mediaani_hz = round(stats::median(f0[i & strong])), savel_yli_350hz = round(mean(f0[i & strong] >= 350), 2)) })) write.csv(piirteet, "rakenne_piirteet.csv", row.names = FALSE) print(piirteet)