def write_intervals(wav_dir, align_dir, intervals): for audio_name, entries in intervals.items(): audio_align_dir = align_dir / audio_name audio_align_dir.mkdir(parents=True, exist_ok=True) grid = tgio.Textgrid() tier = tgio.IntervalTier("sentences", entries) grid.addTier(tier) grid_path = audio_align_dir / f"{audio_name}.TextGrid" grid.save(str(grid_path)) logger.debug(f"Wrote {grid_path}") # Split audio wav_path = wav_dir / f"{audio_name}.wav" audio_wav_dir = audio_align_dir / "wav" logger.debug(f"Splitting {wav_path}") splitAudioOnTier(str(wav_path), str(grid_path), "sentences", str(audio_wav_dir)) # Write transcriptions text_align_dir = audio_align_dir / "text" text_align_dir.mkdir(parents=True, exist_ok=True) num_zeros = int(math.ceil(math.log10(len(entries)))) n_format = "{0:0" + str(num_zeros) + "d}" for i, interval in enumerate(entries): n = n_format.format(i) text_path = text_align_dir / f"{audio_name}_{n}.txt" text_path.write_text(interval.label.strip()) logger.debug(f"Wrote {text_path}")
def _runSpeechRateEstimateOnIntervals(wavPath, tgPath, tierName, wavTmpPath, syllableNucleiPath, matlabEXE, matlabScriptsPath, printCmd=True, outputTGFlag=False): utils.makeDir(wavTmpPath) # Split audio files into subsections based on textgrid intervals for name in utils.findFiles(wavPath, filterExt=".wav", stripExt=True): praatio_scripts.splitAudioOnTier(join(wavPath, name + ".wav"), join(tgPath, name + ".TextGrid"), tierName, wavTmpPath, outputTGFlag) uwe_sr.findSyllableNuclei(wavTmpPath, syllableNucleiPath, matlabEXE, matlabScriptsPath, printCmd)
def _extractPIPiecewise(inputFN, outputFN, praatEXE, minPitch, maxPitch, tgFN, tierName, tmpOutputPath, sampleStep=0.01, silenceThreshold=0.03, forceRegenerate=True, undefinedValue=None, medianFilterWindowSize=0, pitchQuadInterp=False): ''' Extracts pitch and int from each labeled interval in a textgrid This has the benefit of being faster than using _extractPIFile if only labeled regions need to have their pitch values sampled, particularly for longer files. Returns the result as a list. Will load the serialized result if this has already been called on the appropriate files before ''' outputPath = os.path.split(outputFN)[0] utils.makeDir(outputPath) windowSize = medianFilterWindowSize assert (os.path.exists(inputFN)) firstTime = not os.path.exists(outputFN) if firstTime or forceRegenerate is True: utils.makeDir(tmpOutputPath) splitAudioList = praatio_scripts.splitAudioOnTier( inputFN, tgFN, tierName, tmpOutputPath, False) allPIList = [] for start, _, fn in splitAudioList: tmpTrackName = os.path.splitext(fn)[0] + ".txt" piList = _extractPIFile(join(tmpOutputPath, fn), join(tmpOutputPath, tmpTrackName), praatEXE, minPitch, maxPitch, sampleStep, silenceThreshold, forceRegenerate=True, medianFilterWindowSize=windowSize, pitchQuadInterp=pitchQuadInterp) piList = [("%0.3f" % (float(time) + start), str(pV), str(iV)) for time, pV, iV in piList] allPIList.extend(piList) allPIList = [",".join(row) for row in allPIList] with open(outputFN, "w") as fd: fd.write("\n".join(allPIList) + "\n") piList = loadTimeSeriesData(outputFN, undefinedValue=undefinedValue) return piList
def _extractPIPiecewise(inputFN, outputFN, praatEXE, minPitch, maxPitch, tgFN, tierName, tmpOutputPath, sampleStep=0.01, silenceThreshold=0.03, pitchUnit="Hertz", forceRegenerate=True, undefinedValue=None, medianFilterWindowSize=0, pitchQuadInterp=False): ''' Extracts pitch and int from each labeled interval in a textgrid This has the benefit of being faster than using _extractPIFile if only labeled regions need to have their pitch values sampled, particularly for longer files. Returns the result as a list. Will load the serialized result if this has already been called on the appropriate files before ''' outputPath = os.path.split(outputFN)[0] utils.makeDir(outputPath) windowSize = medianFilterWindowSize assert(os.path.exists(inputFN)) firstTime = not os.path.exists(outputFN) if firstTime or forceRegenerate is True: utils.makeDir(tmpOutputPath) splitAudioList = praatio_scripts.splitAudioOnTier(inputFN, tgFN, tierName, tmpOutputPath, False) allPIList = [] for start, _, fn in splitAudioList: tmpTrackName = os.path.splitext(fn)[0] + ".txt" piList = _extractPIFile(join(tmpOutputPath, fn), join(tmpOutputPath, tmpTrackName), praatEXE, minPitch, maxPitch, sampleStep, silenceThreshold, pitchUnit, forceRegenerate=True, medianFilterWindowSize=windowSize, pitchQuadInterp=pitchQuadInterp) piList = [("%0.3f" % (float(time) + start), str(pV), str(iV)) for time, pV, iV in piList] allPIList.extend(piList) allPIList = [",".join(row) for row in allPIList] with open(outputFN, "w") as fd: fd.write("\n".join(allPIList) + "\n") piList = loadTimeSeriesData(outputFN, undefinedValue=undefinedValue) return piList
''' Created on Aug 31, 2014 @author: tmahrt Extracts a separate wav file for each tier in a textgrid ''' import os from os.path import join from praatio import praatio_scripts path = join(".", "files") outputPath = join(path, "sub_wavs") if not os.path.exists(outputPath): os.mkdir(outputPath) for wavFN, tgFN in [ #("bobby.wav", "bobby_words.TextGrid"), ("mary.wav", "mary.TextGrid") ]: praatio_scripts.splitAudioOnTier(join(path, wavFN), join(path, tgFN), "phone", outputPath, True)
def _audioToPIPiecewise(inputPath, inputFN, outputPath, outputFN, praatEXE, minPitch, maxPitch, tgPath, tgFN, tierName, tmpOutputPath, sampleStep=0.01, silenceThreshold=0.03, forceRegenerate=True, undefinedValue=None): ''' Extracts pitch and int from each labeled interval in a textgrid This has the benefit of being faster than using _audioToPIFile if only labeled regions need to have their pitch values sampled, particularly for longer files. Returns the result as a list. Will load the serialized result if this has already been called on the appropriate files before ''' inputFullFN = join(inputPath, inputFN) tgFullFN = join(tgPath, tgFN) outputFullFN = join(outputPath, outputFN) utils.makeDir(outputPath) assert (os.path.exists(inputFullFN)) firstTime = not os.path.exists(outputFullFN) if firstTime or forceRegenerate is True: utils.makeDir(tmpOutputPath) splitAudioList = praatio_scripts.splitAudioOnTier( inputFullFN, tgFullFN, tierName, tmpOutputPath, outputTGFlag=False, nameStyle='append_no_i') print(splitAudioList, 'splitAudioList') allPIList = [] for start, _, fn in splitAudioList: tmpTrackName = os.path.splitext( fn)[0] + ".wav" #ejg change ".txt" to ".wav" piList = _audioToPIFile(tmpOutputPath, fn, tmpOutputPath, tmpTrackName, praatEXE, minPitch, maxPitch, sampleStep, silenceThreshold, forceRegenerate=True) piList = [("%0.3f" % (float(time) + start), str(pV), str(iV)) for time, pV, iV in piList] allPIList.extend(piList) allPIList = [",".join(row) for row in allPIList] with open(outputFullFN, "w") as fd: fd.write("\n".join(allPIList) + "\n") piList = loadPIAndTime(outputPath, outputFN, undefinedValue=undefinedValue) print(piList) return piList
''' Praatio example of extracting a separate wav file for each labeled entry in a textgrid tier ''' import os from os.path import join from praatio import praatio_scripts path = join(".", "files") outputPath = join(path, "sub_wavs") if not os.path.exists(outputPath): os.mkdir(outputPath) for wavFN, tgFN in [#("bobby.wav", "bobby_words.TextGrid"), ("mary.wav", "mary.TextGrid")]: praatio_scripts.splitAudioOnTier(join(path, wavFN), join(path, tgFN), "phone", outputPath, True)