Last active
January 9, 2016 18:59
-
-
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
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
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