Skip to content

Instantly share code, notes, and snippets.

@erzk
Created February 11, 2019 00:57
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 erzk/ff2aabea6ecbde47bde095a1c89f05a6 to your computer and use it in GitHub Desktop.
Save erzk/ff2aabea6ecbde47bde095a1c89f05a6 to your computer and use it in GitHub Desktop.
Extract pitch values, intensity, time, and confidence from Praat pitch files
#!/usr/bin/env r
## load pitch files extracted from Praat and pull key information
# take input from the command line
f <- argv
# load packages and hide messages
library(dplyr)
library(rPraat)
# load the file
pitch_path <- f
p <- pitch.read(pitch_path)
# extract the filename - will be used to save the output
pitch_filename <- basename(pitch_path)
# extract pitch
pitch <- p$frame
# run through all frame examples and extract max strength freq
# extract strength
strength_list <- lapply(pitch, `[[`, "strength")
max_strength <- lapply(strength_list, max) %>% unlist()
max_strength_index <- lapply(strength_list, which.max) %>% unlist()
# extract frequency
frequency_list <- lapply(pitch, `[[`, "frequency")
max_strength_frequency <- c()
for (i in 1:length(frequency_list)) {
## extract pitch where strength has the max value
## that's the best candidate in Praat (i.e. pink circle in the Pitch Track)
max_strength_frequency[i] <- frequency_list[[i]][[max_strength_index[i]]]
}
# time
time <- p$t
# extract intensity
intensity <- lapply(pitch, `[[`, "intensity") %>% unlist()
# make a df with the key values
key_values <- data.frame(Time = time,
Intensity = intensity,
Frequency = max_strength_frequency,
MaxStrength = max_strength,
File = pitch_filename,
stringsAsFactors = FALSE)
write.csv(key_values,
paste0(tools::file_path_sans_ext(pitch_filename), ".csv"),
row.names = FALSE)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment