o
    *iW                     @   s   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dZeG dd dZddeded
edee fddZdS )    )	dataclass)ListOptionalTupleN)Tensor)fail_if_no_align	log_probstargetsinput_lengthstarget_lengthsblankreturnc           	      C   s   ||v rt d| dt|| jd krt d|du r5| d| d}}tj|f|tj| jd}|du rP|d|d}}tj|f|tj|jd}|dusVJ |dus\J tjj	
| ||||\}}||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      )Zdtypedevice)
ValueErrortorchmaxshapesizefullZint64r   opsZ
torchaudioforced_align)	r   r	   r
   r   r   Z
batch_sizelengthpathsscores r   o/var/www/html/eduruby.in/lip-sync/lip-sync-env/lib/python3.10/site-packages/torchaudio/functional/_alignment.pyr      s   -r   c                   @   sH   e Zd ZU dZeed< 	 eed< 	 eed< 	 eed< 	 defddZd	S )
	TokenSpanz[TokenSpan()
    Token with time stamps and score. Returned by :py:func:`merge_tokens`.
    tokenstartendscorer   c                 C   s   | j | j S )zReturns the time span)r"   r!   )selfr   r   r   __len__[   s   zTokenSpan.__len__N)__name__
__module____qualname____doc__int__annotations__floatr%   r   r   r   r   r   L   s   
 r   tokensr   c                    s   j dks
j dkrtdttkrtdtjtjdgjdtjdgjdd}t|dk 	 }	  fd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              	      s@   g | ]\}}|   krt ||||   d qS ))r    r!   r"   r#   )r   meanitem).0r!   r"   r   r   r    r-   r   r   
<listcomp>{   s
    z merge_tokens.<locals>.<listcomp>N)ndimr   lenr   diffZtensorr   ZnonzeroZsqueezetolistzip)r-   r   r   r7   Zchanges_wo_blankspansr   r3   r   merge_tokens`   s   "r;   )NNr   )r   )dataclassesr   typingr   r   r   r   r   Ztorchaudio._extensionr   __all__r*   r   r   r;   r   r   r   r   <module>   s4    
@$