latex_format <- FALSE # set TRUE to compile the LaTeX paper
save_figures <- FALSE # set TRUE to compile the LaTeX paper

Introduction

These analyses are the ones reported in paper titled "onsetsync: An R Package for Onset Synchrony Analysis" (submitted) by Tuomas Eerola and Martin Clayton. The sections and figures correspond to the manuscript.

Load libraries

library(dplyr)
library(ggplot2)
devtools::install_github("tuomaseerola/onsetsync")
library(onsetsync)
packageVersion("onsetsync")
library(dplyr)
library(ggplot2)
library(onsetsync)
packageVersion("onsetsync")

Read data

data("CSS_IEMP", package = "onsetsync")
CSS_Song2 <- CSS_IEMP[[2]]
CSS_Song2 <- dplyr::select(CSS_Song2,Piece,Section,Clave,Bass,Guitar,Tres,SD,Cycle,Isochronous.SD.Time) # delete previous
print(knitr::kable(head(CSS_Song2),digits = 2,format = if(latex_format==TRUE){'latex'} else {'markdown'}))

Summarise onsets

tab1 <- summarise_onsets(df = CSS_Song2, 
                         instr = c('Clave','Bass','Guitar','Tres','Isochronous.SD.Time'))
print(knitr::kable(tab1, digits = 1,format = if(latex_format==TRUE){'latex'} else {'markdown'}))

Figure 1

print(plot_by_beat(df = CSS_Song2, 
                   instr = c('Bass','Clave','Guitar','Tres'), 
                   beat = 'SD', 
                   virtual='Isochronous.SD.Time',
                   pcols=2))
if(save_figures==TRUE){
fig1<-plot_by_beat(df = CSS_Song2, 
                   instr = c('Bass','Clave','Guitar','Tres'), 
                   beat = 'SD', 
                   virtual='Isochronous.SD.Time',
                   pcols=2)
ggsave(filename = 'figure1.pdf',fig1,height = 4,width = 7.5)
}

Analysis of Synchrony

Begin with a diagnostic plot.

Figure 2

print(plot_by_beat(df = CSS_Song2,
                     instr = c('Guitar','Tres'),
                     beat = 'SD',
                     virtual = 'Isochronous.SD.Time', 
                     griddeviations = TRUE))
if(save_figures==TRUE){
fig2 <- plot_by_beat(df = CSS_Song2,
                     instr = c('Guitar','Tres'),
                     beat = 'SD',
                     virtual = 'Isochronous.SD.Time', 
                     griddeviations = TRUE)
print(fig2)
ggsave(filename = 'figure2.pdf',fig2,height = 6,width = 10)
}

Synchrony between the instruments

d1 <- sync_sample_paired(CSS_Song2, instr1 = "Guitar", instr2 = "Tres", beat = "SD")
dplyr::summarise(data.frame(d1), N = n(), Mean.ms = mean(asynch*1000), Sd.ms = sd(asynch*1000))

Figure 3

inst <- c('Clave','Bass','Guitar','Tres') # Define instruments 
dn <- sync_execute_pairs(CSS_Song2,inst,beat = "SD")
print(plot_by_pair(dn))
if(save_figures==TRUE){
  fig3 <- plot_by_pair(dn)  # plot
  print(fig3)  
  ggsave(filename = 'figure3.pdf',fig5,height = 4,width = 6)
}

Plain summaries of asynchrony

Reporting of several measures of absolute asyncronies.

d <- sync_sample_paired(CSS_Song2,'Clave','Bass',beat = 'SD')
print(summarise_sync(d))

Relative measure for two instruments vs other other instruments.

x<-sync_sample_paired_relative(df = CSS_Song2,instr = 'Bass',instr_ref = c('Guitar','Bass','Tres','Clave'),beat = 'SD')
print(x$`Mean pairwise asynchrony`)

x <- sync_sample_paired_relative(df = CSS_Song2,instr = 'Clave',instr_ref = c('Guitar','Bass','Tres','Clave'),beat = 'SD')
print(x$`Mean pairwise asynchrony`)

Asynchrony across beat subdivisions

dn <- sync_execute_pairs(df = CSS_Song2, instruments = c("Guitar", "Tres"),beat = "SD")
print(summarise_sync_by_pair(dn, bybeat = TRUE))

Synchrony across several performances

Using the built-in corpus of Cuban Salsa and Son preformances (CSS_IEMP).

corpus <- onsetsync::CSS_IEMP
D <- sync_sample_paired(corpus,'Guitar','Tres',n=0,beat='SD')
D <- D$asynch
D$asynch_abs <- abs(D$asynch)*1000
print(paste('Mean asynchrony across all performances is', round(mean(D$asynch)*1000,1),'ms'))

Figure 4

print(plot_by_dataset(D,'asynch_abs','name', box = TRUE) + ylab('Absolute Asynchrony (ms)'))
if(save_figures==TRUE){
  fig4 <- plot_by_dataset(D,'asynch_abs','name', box = TRUE)
  fig4 + ylab('Absolute Asynchrony (ms)')
  ggsave(filename = 'figure4.pdf',fig7,height = 3.5,width = 6)
}

Synchrony with other variables

CSS_Song2 <- CSS_Song2 %>%
  group_by(Cycle) %>%
  mutate(Tempo = 240/(max(Isochronous.SD.Time) - min(Isochronous.SD.Time))) %>%
  ungroup()

d2 <- sync_sample_paired(CSS_Song2,"Tres","Guitar", beat = 'Tempo')
d3 <- sync_sample_paired(CSS_Song2,"Tres","Guitar", beat = 'Isochronous.SD.Time')
tmp <- data.frame(asynch = d2$asynch*1000, Tempo = d2$beatL, Time = d3$beatL)
fig<-plot_by_var_time(tmp,var1 = "Time", var2="asynch",var3="Tempo",ylabel='Synchrony (ms)')
print(fig)


tuomaseerola/onsetsynch documentation built on Jan. 17, 2024, 10:58 a.m.