Skip to content

Commit

Permalink
use os.name instead of relying on user config
Browse files Browse the repository at this point in the history
  • Loading branch information
scjs committed Oct 30, 2013
1 parent 4f83e5d commit 8e655bb
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 33 deletions.
4 changes: 0 additions & 4 deletions FAVE-extract/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,6 @@ Second, two user environment variables need to be added. In Windows 7, this can

You also need to edit your PATH variable to include a directory with sox.exe (for example: C:\Program Files (x86)\sox-14-4-1 ), a directory with the R executables (for example: C:\Program Files\R\R-2.15.3\bin\x64 ), and a directory with praatcon.exe. Directories in the Windows PATH are separated by semi-colons.

Finally, when you run FAVE-extract, include windowsPC=T in your configuraton file.

## III. Usage ##

Currently, extractFormants is set up so that it must be run from the main directory of the extractFormants package. To run extractFormants, three arguments are required: the WAV file containing the speech data, the TextGrid file containing the alignments, and the name of an output file for the extracted formants. So, in the directory extractFormants/, type:
Expand Down Expand Up @@ -80,7 +78,6 @@ Parameter | =default (other possible values)
`remeasurement` | `=F` (`T`)
`candidates` | `=F` (`T`)
`vowelSystem` | `=NorthAmerican` (`Phila`)
`windowsPC` | `=F` (`T`)


For example, here are the contents of a possible configuration file:
Expand Down Expand Up @@ -114,7 +111,6 @@ Parameter | Description
`remeasurement` | Specifies whether a second pass is performed on the data, using the speaker's own system as the base of comparison for the Mahalanobis distance. Only used if `formantPredictionMethod=mahalanobis`.
`candidates` | Specifies whether the list of candidate formant values are included in the output. Only used if the `outputFormat=text`.
`vowelSystem` | If set to `Phila`, a number of vowels will be reclassified to reflect the phonemic distinctions of the Philadelphia vowel system (tense short-a etc.).
`windowsPC` | Must be set to `T` to use FAVE-extract on Windows systems.


References
Expand Down
59 changes: 30 additions & 29 deletions FAVE-extract/bin/extractFormants.py
Original file line number Diff line number Diff line change
Expand Up @@ -429,7 +429,7 @@ def checkConfigOption(f, option):

allowedOptions = ['case', 'outputFormat', 'outputHeader', 'formantPredictionMethod', 'measurementPointMethod', 'speechSoftware', 'nFormants', 'maxFormant',
'removeStopWords', 'measureUnstressed', 'minVowelDuration', 'windowSize', 'preEmphasis', 'multipleFiles', 'nSmoothing', 'remeasurement',
'candidates', 'vowelSystem', 'windowsPC']
'candidates', 'vowelSystem']
if option not in allowedOptions:
print "ERROR: unrecognized option '%s' in config file %s" % (option, f)
print "The following options are recognized: ", ", ".join(allowedOptions)
Expand All @@ -455,7 +455,7 @@ def checkConfigValue(f, option, value):
if option == 'speechSoftware':
allowedValues = ['praat', 'Praat', 'esps', 'ESPS']
checkAllowedValues(f, option, value, allowedValues)
if option in ['removeStopWords', 'measureUnstressed', 'outputHeader', 'multipleFiles', 'remeasurement', 'candidates', 'windowsPC']:
if option in ['removeStopWords', 'measureUnstressed', 'outputHeader', 'multipleFiles', 'remeasurement', 'candidates']:
allowedValues = ['T', 'F', 'True', 'False']
checkAllowedValues(f, option, value, allowedValues)
if option == 'vowelSystem':
Expand All @@ -475,7 +475,7 @@ def checkSpeechSoftware(speechSoftware):
"""checks that either Praat or ESPS is available as a speech analysis program"""

if speechSoftware in ['ESPS', 'esps']:
if windowsPC:
if os.name == 'nt':
print "ERROR: ESPS was specified as the speech analysis program, but this option is not yet compatible with Windows"
sys.exit()
if not programExists('formant'):
Expand All @@ -484,7 +484,7 @@ def checkSpeechSoftware(speechSoftware):
else:
return 'esps'
elif speechSoftware in ['praat', 'Praat']:
if not ((PRAATPATH and programExists('praat', PRAATPATH)) or programExists('praat') or programExists('praatcon.exe')):
if not ((PRAATPATH and programExists('praat', PRAATPATH)) or (os.name == 'posix' and programExists('praat')) or (os.name == 'nt' and programExists('praatcon.exe'))):
print "ERROR: Praat was specified as the speech analysis program, but the command 'praat' ('praatcon' for Windows) is not in your path"
sys.exit()
else:
Expand Down Expand Up @@ -582,10 +582,7 @@ def extractPortion(wavFile, vowelWavFile, beg, end, soundEditor):
## force output format because there have been issues with some sound files where Praat could not read the extracted portion
os.system(os.path.join(SOXPATH, 'sox') + ' ' + wavFile + ' -t wavpcm ' + os.path.join(SCRIPTS_HOME, vowelWavFile) + ' trim ' + str(beg) + ' ' + str(end - beg))
elif soundEditor == 'praat':
if not windowsPC:
os.system(os.path.join(PRAATPATH, 'praat') + ' ' + SCRIPTS_HOME + '/extractSegment.praat ' + os.path.join(os.path.pardir, wavFile) + ' ' + vowelWavFile + ' ' + str(beg) + ' ' + str(end))
else:
os.system(os.path.join(PRAATPATH, 'praatcon') + ' ' + SCRIPTS_HOME + '/extractSegment.praat ' + os.path.join(os.path.pardir, wavFile) + ' ' + vowelWavFile + ' ' + str(beg) + ' ' + str(end))
os.system(os.path.join(PRAATPATH, PRAATNAME) + ' ' + SCRIPTS_HOME + '/extractSegment.praat ' + os.path.join(os.path.pardir, wavFile) + ' ' + vowelWavFile + ' ' + str(beg) + ' ' + str(end))
else:
pass

Expand Down Expand Up @@ -753,9 +750,9 @@ def getSoundEditor():
"""checks whether SoX or Praat are available as sound editors"""

# use sox for manipulating the files if we have it, since it's faster
if (SOXPATH and programExists('sox', SOXPATH)) or programExists('sox') or programExists('sox.exe'):
if (SOXPATH and programExists('sox', SOXPATH)) or (os.name == 'posix' and programExists('sox')) or (os.name == 'nt' and programExists('sox.exe')):
soundEditor = 'sox'
elif (PRAATPATH and programExists('praat', PRAATPATH)) or programExists('praat') or programExists('praatcon.exe'):
elif (PRAATPATH and programExists('praat', PRAATPATH)) or (os.name == 'posix' and programExists('praat')) or (os.name == 'nt' and programExists('praatcon.exe')):
soundEditor = 'praat'
else:
print "ERROR: neither 'praat' ('praatcon' for Windows) nor 'sox' can be found in your path"
Expand Down Expand Up @@ -893,29 +890,20 @@ def getVowelMeasurement(vowelFileStem, p, w, speechSoftware, formantPredictionMe
LPCs = []
nFormants = 3
while nFormants <= 6:
if not windowsPC:
os.system(os.path.join(PRAATPATH, 'praat') + ' ' + os.path.join(SCRIPTS_HOME, 'extractFormants.praat') + ' ' + vowelWavFile + ' ' + str(nFormants) + ' ' + str(maxFormant) + ' ' ' ' + str(windowSize) + ' ' + str(preEmphasis) + ' burg')
else:
os.system(os.path.join(PRAATPATH, 'praatcon') + ' ' + os.path.join(SCRIPTS_HOME, 'extractFormants.praat') + ' ' + vowelWavFile + ' ' + str(nFormants) + ' ' + str(maxFormant) + ' ' ' ' + str(windowSize) + ' ' + str(preEmphasis) + ' burg')
os.system(os.path.join(PRAATPATH, PRAATNAME) + ' ' + os.path.join(SCRIPTS_HOME, 'extractFormants.praat') + ' ' + vowelWavFile + ' ' + str(nFormants) + ' ' + str(maxFormant) + ' ' ' ' + str(windowSize) + ' ' + str(preEmphasis) + ' burg')
lpc = praat.Formant()
lpc.read(os.path.join(SCRIPTS_HOME, vowelFileStem + '.Formant'))
LPCs.append(lpc)
nFormants +=1
else:
if not windowsPC:
os.system(os.path.join(PRAATPATH, 'praat') + ' ' + os.path.join(SCRIPTS_HOME, 'extractFormants.praat') + ' ' + vowelWavFile + ' ' + str(nFormants) + ' ' + str(maxFormant) + ' ' + str(windowSize) + ' ' + str(preEmphasis) + ' burg')
else:
os.system(os.path.join(PRAATPATH, 'praatcon') + ' ' + os.path.join(SCRIPTS_HOME, 'extractFormants.praat') + ' ' + vowelWavFile + ' ' + str(nFormants) + ' ' + str(maxFormant) + ' ' + str(windowSize) + ' ' + str(preEmphasis) + ' burg')
os.system(os.path.join(PRAATPATH, PRAATNAME) + ' ' + os.path.join(SCRIPTS_HOME, 'extractFormants.praat') + ' ' + vowelWavFile + ' ' + str(nFormants) + ' ' + str(maxFormant) + ' ' + str(windowSize) + ' ' + str(preEmphasis) + ' burg')
fmt = praat.Formant()
fmt.read(os.path.join(SCRIPTS_HOME, vowelFileStem + '.Formant'))
os.remove(os.path.join(SCRIPTS_HOME, vowelFileStem + '.Formant'))
## get Intensity object for intensity cutoff
## (only for those vowels where we need it)
if (p.label[:-1] in ["AY", "EY", "OW", "AW"]) or (p.label[:-1] == "UW" and p.cd == "73"):
if not windowsPC:
os.system(os.path.join(PRAATPATH, 'praat') + ' ' + os.path.join(SCRIPTS_HOME, 'getIntensity.praat') + ' ' + vowelWavFile)
else:
os.system(os.path.join(PRAATPATH, 'praatcon') + ' ' + os.path.join(SCRIPTS_HOME, 'getIntensity.praat') + ' ' + vowelWavFile)
os.system(os.path.join(PRAATPATH, PRAATNAME) + ' ' + os.path.join(SCRIPTS_HOME, 'getIntensity.praat') + ' ' + vowelWavFile)
intensity = praat.Intensity()
intensity.read(os.path.join(SCRIPTS_HOME, vowelFileStem + '.Intensity'))
os.remove(os.path.join(SCRIPTS_HOME, vowelFileStem + '.Intensity'))
Expand Down Expand Up @@ -949,7 +937,7 @@ def getWordsAndPhones(tg, phoneset, speaker, vowelSystem):
along with their associated phones, and Plotnik codes for the vowels"""

print ''
print 'Indentifying vowels in the TextGrid'
print 'Identifying vowels in the TextGrid'

n_words = len(tg[speaker.tiernum+1])
word_iter = 0
Expand Down Expand Up @@ -1607,10 +1595,17 @@ def programExists(program, path=''):
"""checks whether a given command line program exists (path can be specified optionally)"""

if not path:
if not windowsPC:
if os.name == 'posix':
pathDirs = os.environ['PATH'].split(':')
else:
elif os.name == 'nt':
pathDirs = os.environ['PATH'].split(';')
else:
# this can be bypassed by specifying paths to praat (and sox) on other OS types (still assumes PRAATNAME = 'praat')
print "WARNING: could not recognize OS as 'posix' or 'nt', guessing posix-like PATH"
pathDirs = os.environ['PATH'].split(':')
if not os.path.isdir(pathDirs[0]):
print "ERROR: could not parse PATH environment variable"
sys.exit()
for p in pathDirs:
if os.path.isfile(os.path.join(p, program)):
return True
Expand Down Expand Up @@ -1674,7 +1669,6 @@ def setDefaultOptions():
options['remeasurement'] = False
options['candidates'] = False
options['vowelSystem'] = 'NorthAmerican'
options['windowsPC'] = False

return options

Expand Down Expand Up @@ -1801,7 +1795,6 @@ def writeLog(filename, wavFile, maxTime, meansFile, covsFile, stopWords):
f.write("- covsFile:\t\t\t%s\n" % covsFile)
f.write("- remeasurement:\t\t%s\n" % remeasurement)
f.write("- vowelSystem:\t\t%s\n" % vowelSystem)
f.write("- windowsPC:\t\t%s\n" % windowsPC)
if removeStopWords:
f.write("- stopWords:\t\t\t%s\n" % stopWords)
f.write("\n\n")
Expand Down Expand Up @@ -1856,6 +1849,15 @@ def extractFormants(wavInput, tgInput, output, opts, SPATH='', PPATH=''):
SOXPATH = SPATH
global PRAATPATH
PRAATPATH = PPATH

# set OS-specific variables
if os.name == 'nt':
PRAATNAME = 'praatcon'
elif os.name == 'posix':
PRAATNAME = 'praat'
else:
print "WARNING: unknown OS type '%s' may not be supported" % os.name
PRAATNAME = 'praat'

# by default, assume that these files are located in the current directory
meansFile = 'means.txt'
Expand Down Expand Up @@ -1901,7 +1903,7 @@ def extractFormants(wavInput, tgInput, output, opts, SPATH='', PPATH=''):

# assign the options to individual variables and to type conversion if necessary
global case, outputHeader, formantPredictionMethod, measurementMethod, measurementPointMethod, speechSoftware, nFormants, maxFormant
global nSmoothing, removeStopWords, measureUnstressed, minVowelDuration, windowSize, preEmphasis, multipleFiles, remeasurement, candidates, vowelSystem, windowsPC
global nSmoothing, removeStopWords, measureUnstressed, minVowelDuration, windowSize, preEmphasis, multipleFiles, remeasurement, candidates, vowelSystem
case = options['case']
outputFormat = options['outputFormat']
outputHeader = options['outputHeader']
Expand All @@ -1920,7 +1922,6 @@ def extractFormants(wavInput, tgInput, output, opts, SPATH='', PPATH=''):
remeasurement = options['remeasurement']
candidates = options['candidates']
vowelSystem = options['vowelSystem']
windowsPC = options['windowsPC']
print "Processed options."

## read CMU phoneset ("cmu_phoneset.txt")
Expand Down

0 comments on commit 8e655bb

Please sign in to comment.