# Path: praatio/textgrid.py
# def openTextgrid(
#     fnFullPath: str,
#     includeEmptyIntervals: bool,
#     reportingMode: Literal["silence", "warning", "error"] = "warning",
#     duplicateNamesMode: Literal["error", "rename"] = "error",
# ) -> Textgrid:
# def _dictionaryToTg(
#     tgAsDict: dict, reportingMode: Literal["silence", "warning", "error"]
# ) -> Textgrid:
# 
# Path: praatio/praat_scripts.py
# SILENCE_LABEL = "silent"
# SOUND_LABEL = "sound"
# def changeGender(
#     praatEXE: str,
#     wavFN: str,
#     outputWavFN: str,
#     pitchFloor: float,
#     pitchCeiling: float,
#     formantShiftRatio: float,
#     pitchMedian: float = 0.0,
#     pitchRange: float = 1.0,
#     duration: float = 1.0,
#     scriptFN: Optional[str] = None,
# ) -> None:
# def changeIntensity(
#     praatEXE: str,
#     wavFN: str,
#     outputWavFN: str,
#     newIntensity: float,
#     scriptFN: Optional[str] = None,
# ) -> None:
# def getFormants(
#     praatEXE: str,
#     inputWavFN: str,
#     outputTxtFN: str,
#     maxFormant: float,
#     stepSize: float = 0.01,
#     window_length: float = 0.025,
#     preemphasis: float = 50,
#     scriptFN: Optional[str] = None,
#     undefinedValue: Optional[str] = None,
# ) -> List:
# def getPulses(
#     praatEXE: str,
#     inputWavFN: str,
#     outputPointTierFN: str,
#     minPitch: float,
#     maxPitch: float,
#     scriptFN: Optional[str] = None,
# ) -> data_points.PointObject1D:
# def getSpectralInfo(
#     praatEXE: str,
#     inputWavFN: str,
#     inputTGFN: str,
#     outputCSVFN: str,
#     tierName: str,
#     spectralPower: int = 2,
#     spectralMoment: int = 3,
#     scriptFN: Optional[str] = None,
# ) -> Tuple[List, List]:
# def resynthesizePitch(
#     praatEXE: str,
#     inputWavFN: str,
#     pitchFN: str,
#     outputWavFN: str,
#     minPitch: float,
#     maxPitch: float,
#     scriptFN: Optional[str] = None,
#     pointList: Optional[List] = None,
# ) -> None:
# def resynthesizeDuration(
#     praatEXE: str,
#     inputWavFN: str,
#     durationTierFN: str,
#     outputWavFN: str,
#     minPitch: float,
#     maxPitch: float,
#     scriptFN: Optional[str] = None,
# ) -> None:
# def annotateSilences(
#     praatEXE: str,
#     inputWavPath: str,
#     outputTGPath: str,
#     minPitch: float = 100,
#     timeStep: float = 0.0,
#     silenceThreshold: float = -25.0,
#     minSilDur: float = 0.1,
#     minSoundDur: float = 0.1,
#     silentLabel: str = SILENCE_LABEL,
#     soundLabel: str = SOUND_LABEL,
#     scriptFN: Optional[str] = None,
# ) -> None:
# 
# Path: praatio/utilities/utils.py
# def reportNoop(_exception: Type[BaseException], _text: str) -> None:
# def reportException(exception: Type[BaseException], text: str) -> NoReturn:
# def reportWarning(_exception: Type[BaseException], text: str) -> None:
# def getErrorReporter(reportingMode: Literal["silence", "warning", "error"]):
# def checkIsUndershoot(time: float, referenceTime: float, errorReporter) -> bool:
# def checkIsOvershoot(time: float, referenceTime: float, errorReporter) -> bool:
# def validateOption(variableName, value, optionClass):
# def intervalOverlapCheck(
#     interval: Interval,
#     cmprInterval: Interval,
#     percentThreshold: float = 0,
#     timeThreshold: float = 0,
#     boundaryInclusive: bool = False,
# ) -> bool:
# def getIntervalsInInterval(
#     start: float,
#     end: float,
#     intervals: List[Interval],
#     mode: Literal["strict", "lax", "truncated"],
# ) -> List[Interval]:
# def escapeQuotes(text: str) -> str:
# def strToIntOrFloat(inputStr: str) -> float:
# def getValueAtTime(
#     timestamp: float,
#     sortedDataTupleList: List[Tuple[Any, ...]],
#     fuzzyMatching: bool = False,
#     startI: int = 0,
# ) -> Tuple[Tuple[Any, ...], int]:
# def getValuesInInterval(dataTupleList: List, start: float, end: float) -> List:
# def sign(x: float) -> int:
# def invertIntervalList(
#     inputList: List[Tuple[float, float]], minValue: float = None, maxValue: float = None
# ) -> List[Tuple[float, float]]:
# def makeDir(path: str) -> None:
# def findAll(txt: str, subStr: str) -> List[int]:
# def runPraatScript(
#     praatEXE: str, scriptFN: str, argList: List[Any], cwd: str = None
# ) -> None:
# def safeZip(listOfLists: List[list], enforceLength: bool) -> Iterator[Any]:
# def getWavDuration(wavFN: str) -> float:


import os
import math
from os.path import join
from praatio import textgrid
from praatio import praat_scripts
from praatio.utilities import utils
"""
Automatically segments an audio file based on silence.

Marks an utterance for speech and silence.  Then, via
markTranscriptForAnnotations, chunks those segments into small,
manageable chunks.  The code assumes that the speaker is
talking most of the time.

Probably only useful in limited circumstances.
"""




def markTranscriptForAnnotations(tgFN, tierName, outputTGFN, proportion=1 / 5.0):
    """
    Prep a noisy silence annotation for an annotation task

    Voice activity detectors are liable to segment speech into very small
    chunks (fragments of speech and silence).  The point of this code is
    to segment a recording into larger units that could be used in a
    speech transcription task.

    Assumes the speaker is speaking for most of the recording.
    """

# Path: praatio/textgrid.py
# def openTextgrid(
#     fnFullPath: str,
#     includeEmptyIntervals: bool,
#     reportingMode: Literal["silence", "warning", "error"] = "warning",
#     duplicateNamesMode: Literal["error", "rename"] = "error",
# ) -> Textgrid:
# def _dictionaryToTg(
#     tgAsDict: dict, reportingMode: Literal["silence", "warning", "error"]
# ) -> Textgrid:
# 
# Path: praatio/praat_scripts.py
# SILENCE_LABEL = "silent"
# SOUND_LABEL = "sound"
# def changeGender(
#     praatEXE: str,
#     wavFN: str,
#     outputWavFN: str,
#     pitchFloor: float,
#     pitchCeiling: float,
#     formantShiftRatio: float,
#     pitchMedian: float = 0.0,
#     pitchRange: float = 1.0,
#     duration: float = 1.0,
#     scriptFN: Optional[str] = None,
# ) -> None:
# def changeIntensity(
#     praatEXE: str,
#     wavFN: str,
#     outputWavFN: str,
#     newIntensity: float,
#     scriptFN: Optional[str] = None,
# ) -> None:
# def getFormants(
#     praatEXE: str,
#     inputWavFN: str,
#     outputTxtFN: str,
#     maxFormant: float,
#     stepSize: float = 0.01,
#     window_length: float = 0.025,
#     preemphasis: float = 50,
#     scriptFN: Optional[str] = None,
#     undefinedValue: Optional[str] = None,
# ) -> List:
# def getPulses(
#     praatEXE: str,
#     inputWavFN: str,
#     outputPointTierFN: str,
#     minPitch: float,
#     maxPitch: float,
#     scriptFN: Optional[str] = None,
# ) -> data_points.PointObject1D:
# def getSpectralInfo(
#     praatEXE: str,
#     inputWavFN: str,
#     inputTGFN: str,
#     outputCSVFN: str,
#     tierName: str,
#     spectralPower: int = 2,
#     spectralMoment: int = 3,
#     scriptFN: Optional[str] = None,
# ) -> Tuple[List, List]:
# def resynthesizePitch(
#     praatEXE: str,
#     inputWavFN: str,
#     pitchFN: str,
#     outputWavFN: str,
#     minPitch: float,
#     maxPitch: float,
#     scriptFN: Optional[str] = None,
#     pointList: Optional[List] = None,
# ) -> None:
# def resynthesizeDuration(
#     praatEXE: str,
#     inputWavFN: str,
#     durationTierFN: str,
#     outputWavFN: str,
#     minPitch: float,
#     maxPitch: float,
#     scriptFN: Optional[str] = None,
# ) -> None:
# def annotateSilences(
#     praatEXE: str,
#     inputWavPath: str,
#     outputTGPath: str,
#     minPitch: float = 100,
#     timeStep: float = 0.0,
#     silenceThreshold: float = -25.0,
#     minSilDur: float = 0.1,
#     minSoundDur: float = 0.1,
#     silentLabel: str = SILENCE_LABEL,
#     soundLabel: str = SOUND_LABEL,
#     scriptFN: Optional[str] = None,
# ) -> None:
# 
# Path: praatio/utilities/utils.py
# def reportNoop(_exception: Type[BaseException], _text: str) -> None:
# def reportException(exception: Type[BaseException], text: str) -> NoReturn:
# def reportWarning(_exception: Type[BaseException], text: str) -> None:
# def getErrorReporter(reportingMode: Literal["silence", "warning", "error"]):
# def checkIsUndershoot(time: float, referenceTime: float, errorReporter) -> bool:
# def checkIsOvershoot(time: float, referenceTime: float, errorReporter) -> bool:
# def validateOption(variableName, value, optionClass):
# def intervalOverlapCheck(
#     interval: Interval,
#     cmprInterval: Interval,
#     percentThreshold: float = 0,
#     timeThreshold: float = 0,
#     boundaryInclusive: bool = False,
# ) -> bool:
# def getIntervalsInInterval(
#     start: float,
#     end: float,
#     intervals: List[Interval],
#     mode: Literal["strict", "lax", "truncated"],
# ) -> List[Interval]:
# def escapeQuotes(text: str) -> str:
# def strToIntOrFloat(inputStr: str) -> float:
# def getValueAtTime(
#     timestamp: float,
#     sortedDataTupleList: List[Tuple[Any, ...]],
#     fuzzyMatching: bool = False,
#     startI: int = 0,
# ) -> Tuple[Tuple[Any, ...], int]:
# def getValuesInInterval(dataTupleList: List, start: float, end: float) -> List:
# def sign(x: float) -> int:
# def invertIntervalList(
#     inputList: List[Tuple[float, float]], minValue: float = None, maxValue: float = None
# ) -> List[Tuple[float, float]]:
# def makeDir(path: str) -> None:
# def findAll(txt: str, subStr: str) -> List[int]:
# def runPraatScript(
#     praatEXE: str, scriptFN: str, argList: List[Any], cwd: str = None
# ) -> None:
# def safeZip(listOfLists: List[list], enforceLength: bool) -> Iterator[Any]:
# def getWavDuration(wavFN: str) -> float:


# Path: examples/auto_segment_speech.py
import os
import math
from os.path import join
from praatio import textgrid
from praatio import praat_scripts
from praatio.utilities import utils

"""
Automatically segments an audio file based on silence.

Marks an utterance for speech and silence.  Then, via
markTranscriptForAnnotations, chunks those segments into small,
manageable chunks.  The code assumes that the speaker is
talking most of the time.

Probably only useful in limited circumstances.
"""




def markTranscriptForAnnotations(tgFN, tierName, outputTGFN, proportion=1 / 5.0):
    """
    Prep a noisy silence annotation for an annotation task

    Voice activity detectors are liable to segment speech into very small
    chunks (fragments of speech and silence).  The point of this code is
    to segment a recording into larger units that could be used in a
    speech transcription task.

    Assumes the speaker is speaking for most of the recording.
    """

    tg = textgrid.openTextgrid(tgFN, False)