
    ~Vji                         d dl mZ d dlmZmZmZ d dlZd dlmZ d dlm	Z	 g Z
e		 	 	 ddededee         d	ee         d
edeeef         fd            Ze G d d                      Zddeded
edee         fdZdS )    )	dataclass)ListOptionalTupleN)Tensor)fail_if_no_align	log_probstargetsinput_lengthstarget_lengthsblankreturnc                    ||v rt          d| d          t          j        |          | j        d         k    rt          d          |R|                     d          |                     d          }}t          j        |f|t          j        | j                  }|R|                    d          |                    d          }}t          j        |f|t          j        |j                  }|J |J t          j        j	        
                    | ||||          \  }}||ddt          j        |j        d                   |d         f         fS )	a  Align a CTC label sequence to an emission.

    .. devices:: CPU CUDA

    .. properties:: TorchScript

    Args:
        log_probs (Tensor): log probability of CTC emission output.
            Tensor of shape `(B, T, C)`. where `B` is the batch size, `T` is the input length,
            `C` is the number of characters in alphabet including blank.
        targets (Tensor): Target sequence. Tensor of shape `(B, L)`,
            where `L` is the target length.
        input_lengths (Tensor or None, optional):
            Lengths of the inputs (max value must each be <= `T`). 1-D Tensor of shape `(B,)`.
        target_lengths (Tensor or None, optional):
            Lengths of the targets. 1-D Tensor of shape `(B,)`.
        blank_id (int, optional): The index of blank symbol in CTC emission. (Default: 0)

    Returns:
        Tuple(Tensor, Tensor):
            Tensor: Label for each time step in the alignment path computed using forced alignment.

            Tensor: Log probability scores of the labels for each time step.

    Note:
        The sequence length of `log_probs` must satisfy:


        .. math::
            L_{\text{log\_probs}} \ge L_{\text{label}} + N_{\text{repeat}}

        where :math:`N_{\text{repeat}}` is the number of consecutively repeated tokens.
        For example, in str `"aabbc"`, the number of repeats are `2`.

    Note:
        The current version only supports ``batch_size==1``.
    z4targets Tensor shouldn't contain blank index. Found .z2targets values must be less than the CTC dimensionNr      )dtypedevice)
ValueErrortorchmaxshapesizefullint64r   ops
torchaudioforced_alignarange)	r	   r
   r   r   r   
batch_sizelengthpathsscoress	            Z/root/voice-cloning/.venv/lib/python3.11/site-packages/torchaudio/functional/_alignment.pyr   r      sO   Z ZPWZZZ[[[yY_R000MNNN&^^A..	q0A0AF

J=&T]Tdeee$\\!__gll1ooF
ZM6U\Ucddd $$$%%%I(55i-YginooME6&ELa9958CDDD    c                   P    e Zd ZU dZeed<   	 eed<   	 eed<   	 eed<   	 defdZdS )		TokenSpanz[TokenSpan()
    Token with time stamps and score. Returned by :py:func:`merge_tokens`.
    tokenstartendscorer   c                      | j         | j        z
  S )zReturns the time span)r*   r)   )selfs    r$   __len__zTokenSpan.__len__[   s    x$*$$r%   N)__name__
__module____qualname____doc__int__annotations__floatr.    r%   r$   r'   r'   L   sm           JJJJJJ;	HHH9LLL&% % % % % % %r%   r'   tokensr#   c                 J     j         dk    sj         dk    rt          d          t                     t                    k    rt          d          t          j         t          j        dg j                  t          j        dg j                            }t          j        |dk                                              	                                } 	                                  fdt          |d	d         |dd	                   D             }|S )
a  Removes repeated tokens and blank tokens from the given CTC token sequence.

    Args:
        tokens (Tensor): Alignment tokens (unbatched) returned from :py:func:`forced_align`.
            Shape: `(time, )`.
        scores (Tensor): Alignment scores (unbatched) returned from :py:func:`forced_align`.
            Shape: `(time, )`. When computing the token-size score, the given score is averaged
            across the corresponding time span.

    Returns:
        list of TokenSpan

    Example:
        >>> aligned_tokens, scores = forced_align(emission, targets, input_lengths, target_lengths)
        >>> token_spans = merge_tokens(aligned_tokens[0], scores[0])
    r   z(`tokens` and `scores` must be 1D Tensor.z.`tokens` and `scores` must be the same length.r   )r   )prependappendr   c           
          g | ]R\  }}|         xk    t          ||||                                                                                    SS ))r(   r)   r*   r+   )r'   meanitem).0r)   r*   r   r#   r(   r7   s      r$   
<listcomp>z merge_tokens.<locals>.<listcomp>{   sm       E3E]"Eu,, 	U6%);L;Q;Q;S;S;X;X;Z;Z[[[,,,r%   N)ndimr   lenr   difftensorr   nonzerosqueezetolistzip)r7   r#   r   rB   changes_wo_blankspansr(   s   ```   @r$   merge_tokensrJ   `   s4   " {a6;!++CDDD
6{{c&kk!!IJJJ:bT&-@@@WYVZcicpIqIqIq  D }dai1199;;BBDD]]__F      .ss35Eabb5IJJ  E
 Lr%   )NNr   )r   )dataclassesr   typingr   r   r   r   r   torchaudio._extensionr   __all__r3   r   r'   rJ   r6   r%   r$   <module>rO      sf   ! ! ! ! ! ! ( ( ( ( ( ( ( ( ( (        2 2 2 2 2 2
  '+'+=E =E=E=E F#=E V$	=E
 =E 66>=E =E =E =E@ % % % % % % % %&         DO            r%   