Skip to content

Instantly share code, notes, and snippets.

@fauxneticien
Last active October 25, 2017 05:43
Show Gist options
  • Save fauxneticien/4c96a553c9d95a2a466943848b0f0ab4 to your computer and use it in GitHub Desktop.
Save fauxneticien/4c96a553c9d95a2a466943848b0f0ab4 to your computer and use it in GitHub Desktop.
Read in pitch tracks from .wav files as an R data frame
# This is a tidyverse-friendly wrapper function for wrassp::mhsF0
#
# Usage:
#
# dir(path = ".", pattern = "*.wav", full.names = TRUE) %>% mhsF0_df
# dir(path = ".", pattern = "*.wav", full.names = TRUE) %>% mhsF0_df(beginTime = 1, gender = "f")
#
# For list of arguments to wrassp::mhsF0, see https://www.rdocumentation.org/packages/wrassp/versions/0.1.4/topics/mhsF0
mhsF0_df <- function(fileList, ...) {
mhsF0_func <- partial(
...f = wrassp::mhsF0,
toFile = FALSE,
...
)
map(
.x = fileList,
.f = function(filePath) {
res <- mhsF0_func(filePath)
attr(res, "filePath") <- filePath
res
}
) %>%
map_df(
.f = function(mhsF0_result) {
times <- seq(
from = attr(mhsF0_result, "startTime"),
length.out = length(mhsF0_result$pitch),
by = 1/attr(mhsF0_result, "sampleRate")
)
data.frame(
stringsAsFactors = FALSE,
file = attr(mhsF0_result, "filePath"),
time = times,
pitch = mhsF0_result$pitch
)
}
)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment