Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def getFormants(praatEXE, inputWavFN, outputTxtFN, maxFormant,
stepSize=0.01, window_length=0.025, preemphasis=50,
scriptFN=None, undefinedValue=None):
'''
Get F1, F2, and F3 for the audio file
maxFormant = 5500 for females, 5000 for males, <8000 for children
'''
if scriptFN is None:
scriptFN = join(utils.scriptsPath, "get_formants.praat")
argList = [inputWavFN, outputTxtFN, stepSize, maxFormant, window_length,
preemphasis, -1, -1]
utils.runPraatScript(praatEXE, scriptFN, argList)
# Load the output
path, fn = os.path.split(outputTxtFN)
dataList = utils.openCSV(path, fn)
# The new praat script includes a header
if dataList[0][0] == "time":
dataList = dataList[1:]
# Handle undefined values, convert values to float
returnList = []
for row in dataList:
keep = True
for i in range(1, 4):
if '--' in row[i]:
if undefinedValue is not None:
def getSpectralInfo(praatEXE, inputWavFN, inputTGFN, outputCSVFN, tierName,
spectralPower=2, spectralMoment=3, scriptFN=None):
'''
Extracts various spectral measures from an audio file
http://www.fon.hum.uva.nl/praat/manual/Spectrum.html
Measures include: center_of_gravity, standard_deviation
skewness, kertosis, central_movement
'''
if scriptFN is None:
scriptFN = join(utils.scriptsPath, "get_spectral_info.praat")
argList = [inputWavFN, inputTGFN, outputCSVFN, tierName,
spectralPower, spectralMoment]
utils.runPraatScript(praatEXE, scriptFN, argList)
# Load the output
with io.open(outputCSVFN, "r", encoding="utf-8") as fd:
data = fd.read()
dataList = data.rstrip().split("\n")
dataList = [row.split(",") for row in dataList]
titleRow, dataList = dataList[0], dataList[1:]
return titleRow, dataList
scriptFN=None):
'''
Extracts the klattgrid from a wav file
The default values are the same as the ones used in praat
'''
if subtractMean is True:
subtractMean = "yes"
else:
subtractMean = "no"
if scriptFN is None:
scriptFN = join(utils.scriptsPath, "sound_to_klattgrid.praat")
utils.runPraatScript(praatEXE, scriptFN,
[inputFullPath, outputFullPath, timeStep,
numFormants, maxFormantFreq, windowLength,
preEmphasis, pitchFloor, pitchCeiling,
minPitch, subtractMean])
# result
if os.path.exists(outputFN):
os.remove(outputFN)
if pitchQuadInterp is True:
doInterpolation = 1
else:
doInterpolation = 0
argList = [inputFN, outputFN, sampleStep,
minPitch, maxPitch, silenceThreshold, pitchUnit, -1, -1,
medianFilterWindowSize, doInterpolation]
scriptName = "get_pitch_and_intensity.praat"
scriptFN = join(utils.scriptsPath, scriptName)
utils.runPraatScript(praatEXE, scriptFN, argList)
piList = loadTimeSeriesData(outputFN, undefinedValue=undefinedValue)
return piList
assert(os.path.exists(inputFN))
firstTime = not os.path.exists(outputFN)
if firstTime or forceRegenerate is True:
# The praat script uses append mode, so we need to clear any prior
# result
if os.path.exists(outputFN):
os.remove(outputFN)
argList = [inputFN, outputFN, sampleStep,
minPitch, -1, -1]
scriptName = "get_intensity.praat"
scriptFN = join(utils.scriptsPath, scriptName)
utils.runPraatScript(praatEXE, scriptFN, argList)
iList = loadTimeSeriesData(outputFN, undefinedValue=undefinedValue)
return iList
The pitch track to use can optionally be passed in as pointList. If
so, it will be saved as pitchFN for praat to be able to use.
'''
if scriptFN is None:
scriptFN = join(utils.scriptsPath, "resynthesize_pitch.praat")
if pointList is not None:
dur = audioio.WavQueryObj(inputWavFN).getDuration()
pointObj = dataio.PointObject2D(pointList,
dataio.PITCH,
0,
dur)
pointObj.save(pitchFN)
utils.runPraatScript(praatEXE, scriptFN,
[inputWavFN, pitchFN, outputWavFN,
minPitch, maxPitch])
def resynthesize(praatEXE, wavFN, klattFN, outputWavFN, doCascade=True,
scriptFN=None):
if doCascade:
method = "Cascade"
else:
method = "Parallel"
if scriptFN is None:
scriptFN = join(utils.scriptsPath,
"resynthesize_from_klattgrid.praat")
# Praat crashes on exit after resynthesis with a klaatgrid
utils.runPraatScript(praatEXE, scriptFN,
[wavFN, klattFN, outputWavFN, method])
else:
doInterpolation = 0
assert(os.path.exists(wavFN))
firstTime = not os.path.exists(outputFN)
if firstTime or forceRegenerate is True:
if os.path.exists(outputFN):
os.remove(outputFN)
argList = [wavFN, outputFN, sampleStep,
minPitch, maxPitch, silenceThreshold,
medianFilterWindowSize, doInterpolation]
scriptName = "get_pitchtier.praat"
scriptFN = join(utils.scriptsPath, scriptName)
utils.runPraatScript(praatEXE, scriptFN, argList)
pitchTier = dataio.open2DPointObject(outputFN)
return pitchTier
def changeGender(praatEXE, wavFN, outputWavFN, pitchFloor, pitchCeiling,
formantShiftRatio, pitchMedian=0.0, pitchRange=1.0,
duration=1.0, scriptFN=None):
'''
Changes the speech formants in a file using praat's change gender function
PitchMedian = 0.0; no change in median pitch
PitchRange = 1.0; no change in pitch range
'''
if scriptFN is None:
scriptFN = join(utils.scriptsPath,
"change_gender.praat")
# Praat crashes on exit after resynthesis with a klaatgrid
utils.runPraatScript(praatEXE, scriptFN,
[wavFN, outputWavFN, pitchFloor, pitchCeiling,
formantShiftRatio, pitchMedian, pitchRange,
duration])
def changeIntensity(praatEXE, wavFN, outputWavFN, newIntensity, scriptFN=None):
'''
Changes the intensity of the wavFN (in db)
'''
if scriptFN is None:
scriptFN = join(utils.scriptsPath,
"change_intensity.praat")
# Praat crashes on exit after resynthesis with a klaatgrid
utils.runPraatScript(praatEXE, scriptFN,
[wavFN, outputWavFN, newIntensity])