Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vlandham/3936045df66b31cd8a0f8f8c7f41826f to your computer and use it in GitHub Desktop.
Save vlandham/3936045df66b31cd8a0f8f8c7f41826f to your computer and use it in GitHub Desktop.
An example of seewave::acoustat amplitude density calculation
# Install dependencies
#install.packages(c("fftw","tuneR","rgl","rpanel", "seewave"), repos="http://cran.at.r-project.org/")
# for Fast Fourier transform (fftw) to work, install
# the fftw lib (e.g. brew install fftw)
# Load libraries
library(data.table)
library(tuneR)
library(seewave)
# Path to a folder with MP3s in question
setwd("/path/to/folder/with/MP3s")
# Read in the MP3s
fifeteen_step <- tuneR::readMP3("15 Step — In Rainbows.mp3")
true_love_waits <- readMP3("True Love Waits — A Moon Shaped Pool.mp3")
# Bind the top- and bottom-scoring tracks on the gloom index into one track
target_track <- bind(fifeteen_step, true_love_waits)
# Get the length of the first track
fifeteen_step_length <- round(length(fifeteen_step@left) / fifeteen_step@samp.rate, 2)
# Aggregated short-term Fourier transform
signalstat <- seewave::acoustat(target_track, wl = 2^15, ovlp = 50, plot = F)
# Extract time profile of amplitude probability density
timedata <- as.data.table(signalstat$time.contour)
# Assign proper names to the two tracks by time
timedata[, track := ifelse(time <= fifeteen_step_length, "15 Step - In Rainbows", "True Love Waits - A Moon Shaped Pool")]
timedata[, time := ifelse(track == "True Love Waits - A Moon Shaped Pool", time - fifeteen_step_length, time)]
# Plotting
par(mfrow=c(1,2))
plot(contour ~ time, data = timedata[track == "15 Step - In Rainbows"], type = "l", ylim = c(0, max(timedata$contour)), main = "15 Step - In Rainbows")
plot(contour ~ time, data = timedata[track == "True Love Waits - A Moon Shaped Pool"], type = "l", ylim = c(0, max(timedata$contour)), main = "True Love Waits - A Moon Shaped Pool")
# CSV save point
write.csv(timedata, "true_love_15_step_energy_profile.csv", row.names = F)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment