Extract pitch values, intensity, time, and confidence from Praat pitch files
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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