class_docstr: Align text to audio using CTC segmentation.
  
  Usage:
      Initialize with given ASR model and parameters.
      If needed, parameters for CTC segmentation can be set with ``set_config(·)``.
      Then call the instance as function to align text within an audio file.
  
  Example:
      >>> # example file included in the ESPnet repository
      >>> import soundfile
      >>> speech, fs = soundfile.read("test_utils/ctc_align_test.wav")
      >>> # load an ASR model
      >>> from espnet_model_zoo.downloader import ModelDownloader
      >>> d = ModelDownloader()
      >>> wsjmodel = d.download_and_unpack( "kamo-naoyuki/wsj" )
      >>> # Apply CTC segmentation
      >>> aligner = CTCSegmentation( **wsjmodel )
      >>> text=["utt1 THE SALE OF THE HOTELS", "utt2 ON PROPERTY MANAGEMENT"]
      >>> aligner.set_config( gratis_blank=True )
      >>> segments = aligner( speech, text, fs=fs )
      >>> print( segments )
      utt1 utt 0.27 1.72 -0.1663 THE SALE OF THE HOTELS
      utt2 utt 4.54 6.10 -4.9646 ON PROPERTY MANAGEMENT
  
  On multiprocessing:
      To parallelize the computation with multiprocessing, these three steps
      can be separated:
      (1) ``get_lpz``: obtain the lpz,
      (2) ``prepare_segmentation_task``: prepare the task, and
      (3) ``get_segments``: perform CTC segmentation.
      Note that the function `get_segments` is a staticmethod and therefore
      independent of an already initialized CTCSegmentation object.
  
  References:
      CTC-Segmentation of Large Corpora for German End-to-end Speech Recognition
      2020, Kürzinger, Winkelbauer, Li, Watzel, Rigoll
      https://arxiv.org/abs/2007.09127
  
  More parameters are described in https://github.com/lumaku/ctc-segmentation
docstr: Set CTC segmentation parameters.
  
  Parameters for timing:
      time_stamps: Select method how CTC index duration is estimated, and
          thus how the time stamps are calculated.
      fs: Sample rate.
      samples_to_frames_ratio: If you want to directly determine the
          ratio of samples to CTC frames, set this parameter, and
          set ``time_stamps`` to "fixed".
          Note: If you want to calculate the time stamps as in
          ESPnet 1, set this parameter to:
          ``subsampling_factor * frame_duration / 1000``.
  
  Parameters for text preparation:
      set_blank: Index of blank in token list. Default: 0.
      replace_spaces_with_blanks: Inserts blanks between words, which is
          useful for handling long pauses between words. Only used in
          ``text_converter="classic"`` preprocessing mode. Default: False.
      kaldi_style_text: Determines whether the utterance name is expected
          as fist word of the utterance. Set at module initialization.
      text_converter: How CTC segmentation handles text.
          Set at module initialization.
  
  Parameters for alignment:
      min_window_size: Minimum number of frames considered for a single
          utterance. The current default value of 8000 corresponds to
          roughly 4 minutes (depending on ASR model) and should be OK in
          most cases. If your utterances are further apart, increase
          this value, or decrease it for smaller audio files.
      max_window_size: Maximum window size. It should not be necessary
          to change this value.
      gratis_blank: If True, the transition cost of blank is set to zero.
          Useful for long preambles or if there are large unrelated segments
          between utterances. Default: False.
  
  Parameters for calculation of confidence score:
      scoring_length: Block length to calculate confidence score. The
          default value of 30 should be OK in most cases.
docstr_tok: ["Set","CTC","segmentation","parameters",".","Parameters","for","timing",":","time_stamps",":","Select","method","how","CTC","index","duration","is","estimated",",","and","thus","how","the","time","stamps","are","calculated",".","fs",":","Sample","rate",".","samples_to_frames_ratio",":","If","you","want","to","directly","determine","the","ratio","of","samples","to","CTC","frames",",","set","this","parameter",",","and","set","``","time_stamps","``","to","``","fixed","''",".","Note",":","If","you","want","to","calculate","the","time","stamps","as","in","ESPnet","1",",","set","this","parameter","to",":","``","subsampling_factor","*","frame_duration","\/","1000","``",".","Parameters","for","text","preparation",":","set_blank",":","Index","of","blank","in","token","list",".","Default",":","0.","replace_spaces_with_blanks",":","Inserts","blanks","between","words",",","which","is","useful","for","handling","long","pauses","between","words",".","Only","used","in","``","text_converter=","''","classic","''","``","preprocessing","mode",".","Default",":","False",".","kaldi_style_text",":","Determines","whether","the","utterance","name","is","expected","as","fist","word","of","the","utterance",".","Set","at","module","initialization",".","text_converter",":","How","CTC","segmentation","handles","text",".","Set","at","module","initialization",".","Parameters","for","alignment",":","min_window_size",":","Minimum","number","of","frames","considered","for","a","single","utterance",".","The","current","default","value","of","8000","corresponds","to","roughly","4","minutes","(","depending","on","ASR","model",")","and","should","be","OK","in","most","cases",".","If","your","utterances","are","further","apart",",","increase","this","value",",","or","decrease","it","for","smaller","audio","files",".","max_window_size",":","Maximum","window","size",".","It","should","not","be","necessary","to","change","this","value",".","gratis_blank",":","If","True",",","the","transition","cost","of","blank","is","set","to","zero",".","Useful","for","long","preambles","or","if","there","are","large","unrelated","segments","between","utterances",".","Default",":","False",".","Parameters","for","calculation","of","confidence","score",":","scoring_length",":","Block","length","to","calculate","confidence","score",".","The","default","value","of","30","should","be","OK","in","most","cases","."]
code: def set_config(self, **kwargs):
      """Set CTC segmentation parameters.
  
      Parameters for timing:
          time_stamps: Select method how CTC index duration is estimated, and
              thus how the time stamps are calculated.
          fs: Sample rate.
          samples_to_frames_ratio: If you want to directly determine the
              ratio of samples to CTC frames, set this parameter, and
              set ``time_stamps`` to "fixed".
              Note: If you want to calculate the time stamps as in
              ESPnet 1, set this parameter to:
              ``subsampling_factor * frame_duration / 1000``.
  
      Parameters for text preparation:
          set_blank: Index of blank in token list. Default: 0.
          replace_spaces_with_blanks: Inserts blanks between words, which is
              useful for handling long pauses between words. Only used in
              ``text_converter="classic"`` preprocessing mode. Default: False.
          kaldi_style_text: Determines whether the utterance name is expected
              as fist word of the utterance. Set at module initialization.
          text_converter: How CTC segmentation handles text.
              Set at module initialization.
  
      Parameters for alignment:
          min_window_size: Minimum number of frames considered for a single
              utterance. The current default value of 8000 corresponds to
              roughly 4 minutes (depending on ASR model) and should be OK in
              most cases. If your utterances are further apart, increase
              this value, or decrease it for smaller audio files.
          max_window_size: Maximum window size. It should not be necessary
              to change this value.
          gratis_blank: If True, the transition cost of blank is set to zero.
              Useful for long preambles or if there are large unrelated segments
              between utterances. Default: False.
  
      Parameters for calculation of confidence score:
          scoring_length: Block length to calculate confidence score. The
              default value of 30 should be OK in most cases.
      """
      # Parameters for timing
      if "time_stamps" in kwargs:
          if kwargs["time_stamps"] not in self.choices_time_stamps:
              raise NotImplementedError(
                  f"Parameter ´time_stamps´ has to be one of "
                  f"{list(self.choices_time_stamps)}",
              )
          self.time_stamps = kwargs["time_stamps"]
      if "fs" in kwargs:
          self.fs = float(kwargs["fs"])
      if "samples_to_frames_ratio" in kwargs:
          self.samples_to_frames_ratio = float(
              kwargs["samples_to_frames_ratio"]
          )
      # Parameters for text preparation
      if "set_blank" in kwargs:
          assert isinstance(kwargs["set_blank"], int)
          self.config.blank = kwargs["set_blank"]
      if "replace_spaces_with_blanks" in kwargs:
          self.config.replace_spaces_with_blanks = bool(
              kwargs["replace_spaces_with_blanks"]
          )
      if "kaldi_style_text" in kwargs:
          assert isinstance(kwargs["kaldi_style_text"], bool)
          self.kaldi_style_text = kwargs["kaldi_style_text"]
      if "text_converter" in kwargs:
          if (
              kwargs["text_converter"]
              not in self.choices_text_converter
          ):
              raise NotImplementedError(
                  f"Parameter ´text_converter´ has to be one of "
                  f"{list(self.choices_text_converter)}",
              )
          self.text_converter = kwargs["text_converter"]
      # Parameters for alignment
      if "min_window_size" in kwargs:
          assert isinstance(kwargs["min_window_size"], int)
          self.config.min_window_size = kwargs["min_window_size"]
      if "max_window_size" in kwargs:
          assert isinstance(kwargs["max_window_size"], int)
          self.config.max_window_size = kwargs["max_window_size"]
      if "gratis_blank" in kwargs:
          self.config.blank_transition_cost_zero = bool(
              kwargs["gratis_blank"]
          )
      if (
          self.config.blank_transition_cost_zero
          and self.config.replace_spaces_with_blanks
          and not self.warned_about_misconfiguration
      ):
          logging.error(
              "Blanks are inserted between words, and also the transition cost of"
              " blank is zero. This configuration may lead to misalignments!"
          )
          self.warned_about_misconfiguration = True
      # Parameter for calculation of confidence score
      if "scoring_length" in kwargs:
          assert isinstance(kwargs["scoring_length"], int)
          self.config.score_min_mean_over_L = kwargs["scoring_length"]
  
imports: import argparse
  import logging
  import sys
  from pathlib import Path
  from typing import List, Optional, TextIO, Union
  import numpy
  import soundfile
  import torch
  from ctc_segmentation import CtcSegmentationParameters, ctc_segmentation, determine_utterance_segments, prepare_text, prepare_token_list
  from typeguard import check_argument_types, check_return_type
  from espnet2.tasks.asr import ASRTask
  from espnet2.torch_utils.device_funcs import to_device
  from espnet2.utils import config_argparse
  from espnet2.utils.types import str2bool, str_or_none
  from espnet.utils.cli_utils import get_commandline_args
label_desc: Use image node_id 2 for calling the CTCSegmentation obj's underlying member method code with example usage: obj.set_config() without return types