Skip to content

Instantly share code, notes, and snippets.

Created February 10, 2019 23:36
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/53acd493d29ef894bd0870b55d9874ce to your computer and use it in GitHub Desktop.
Save erzk/53acd493d29ef894bd0870b55d9874ce to your computer and use it in GitHub Desktop.
Extract pitch values, intensity, time, and confidence from all wav files in the working directory
# uncomment for a point-and-click version (then the next two lines should be commented out)
#inDir$ = chooseDirectory$: "Choose the folder containing your wav files"
wd$ = "./"
inDir$ = wd$
# create a list of all wav files in the chosen directory
inDirWild$ = inDir$ + "*.wav"
# get the names of the wav files
wavList = Create Strings as file list: "wavList", inDirWild$
# see how many wavs there are for the loop
numFiles = Get number of strings
# if there are no files, throw an error
if numFiles == 0
exitScript: "I didn't find any .wav files in that folder. Exiting"
# create the output directory - otherwise the script will crash
runSystem: "mkdir pitch_output"
# run the loop
for fileNum from 1 to numFiles
# select objects explicitly at the beginning of a loop, since
# they may not be selected by the end
selectObject: wavList
wavFile$ = Get string: fileNum
# load the wav
wav = Read from file: inDir$ + wavFile$
# create pitch object
# GUI: Analyse Periodicity > To Pitch
# Settings: Time step (0); Pitch floor (75 Hz); Pitch ceiling (default: 600 Hz)
textGrid = To Pitch: 0, 75, 400
# choose the output folder
outPathFolder$ = "pitch_output/"
# output file name
outPath$ = outPathFolder$ + wavFile$ + ".pitch"
# do not overwrite files
if fileReadable: outPath$
pauseScript: objName$ + ".pitch" + " exists! Overwrite?"
# write the text file
Save as text file: outPath$
# Remove newly opened objects for cleanup
selectObject: wav
plusObject: textGrid
# Remove the wav list
selectObject: wavList
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment