Skip to content

Instantly share code, notes, and snippets.

@erzk
Last active January 9, 2016 18:59
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/09a96480622dfdff0df0 to your computer and use it in GitHub Desktop.
Save erzk/09a96480622dfdff0df0 to your computer and use it in GitHub Desktop.
append txt with a short text format to enable reading txt file in praat
txt2praat <- function(x, filename) {
# Appends the Praat header to enable txt vector to be read in Praat.
#
# Args:
# x: A path to the txt file (vector) that the header attached will be attached to.
# Example:
# >x <- file.choose()
#
# filename: Optional argument specifying the output file name
#
# Returns:
# Text file that can be loaded into Praat.
# assign the output file name
if(missing(filename)) {
# use the original file name
filename <- basename(file.path(x))
} else {
filename <- paste(deparse(substitute(filename)),
".txt",
sep = ""
)
}
# read as a vector
x <- as.character(scan(x))
# header needed to read txt into Praat
praat_header <- c('File type = "ooTextFile"',
'Object class = "Sound 2"',
'',
'0',
'0.41',
'6536',
'0.0000610221205187',
'0.0000305110602593',
'1',
'1',
'1',
'1',
'1')
# add the header to loaded file
pf <- append(praat_header, x)
# save the output
write.table(pf,
file=filename,
quote=FALSE,
row.names=FALSE,
col.names=FALSE)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment