o
    si*                     @   sL   d dl mZ d dlZG dd dejjZdd Zdd Zd	d
 Zdd Z	dS )    )groupbyNc                       s*   e Zd ZdZd	 fdd	Zdd Z  ZS )
Binarizea  This module transform a sequence of real numbers between 0 and 1 to a sequence of 0 or 1.
    The logic for transformation is based on thresholding and avoids jumping from 0 to 1 inadvertently.

    Example:

        >>> binarizer = Binarize(threshold=0.5, stability=3, sample_rate=1)
        >>> inputs=torch.Tensor([0.1, 0.6, 0.2, 0.6, 0.1, 0.1, 0.1, 0.7, 0.7, 0.7, 0.7, 0.7, 0.7, 0.1])
        >>>                    # |------------------|-------------|----------------------------|----|
        >>>                    #    unstable          stable             stable                 irregularity
        >>> result = binarizer(inputs.unsqueeze(0).unsqueeze(0))
        >>> print(result)
        tensor([[[0., 0., 0., 0., 0., 0., 0., 1., 1., 1., 1., 1., 1., 1.]]])

          ?皙?@  c                    s    t    || _|| _|| _dS )a  

        Args:
            threshold (float): if x > threshold 0 else 1
            stability (float): Minimum number of seconds of 0 (or 1) required to change from 1 (or 0) to 0 (or 1)
            sample_rate (int): The sample rate of the wave form
        N)super__init__	threshold	stabilitysample_rate)selfr	   r
   r   	__class__ E/home/ubuntu/.local/lib/python3.10/site-packages/asteroid/binarize.pyr      s   

zBinarize.__init__c                 C   s4   || j k}|d }t|}t|| j| j}|S )N   )r	   squeezetolistcount_same_pairtransform_to_binary_sequencer
   r   )r   xactivepairsr   r   r   forward"   s
   
zBinarize.forward)r   r   r   )__name__
__module____qualname____doc__r   r   __classcell__r   r   r   r   r      s    r   c                 C   s*   g }| D ]}| dd t|D  q|S )a~  Transform a list of 0 and 1 in a list of (value, num_consecutive_occurences).

    Args:
        nums (list): List of list containing the binary sequences.

    Returns:
        List of values and number consecutive occurences.

    Example:
        >>> nums = [[0,0,1,0]]
        >>> result = count_same_pair(nums)
        >>> print(result)
        [[[0, 2], [1, 1], [0, 1]]]

    c                 S   s&   g | ]\}}|t d d |D gqS )c                 s   s    | ]}d V  qdS )r   Nr   ).0_r   r   r   	<genexpr><   s    z-count_same_pair.<locals>.<listcomp>.<genexpr>)sum)r   igroupr   r   r   
<listcomp><   s   & z#count_same_pair.<locals>.<listcomp>)appendr   )numsresultnumr   r   r   r   *   s   r   c              	   C   s   g }| D ]e}g }t ||\}}|r|  S d}|t|k ra|| \}}	d}
d}|	t|| k r<t|||||
||\}}n|rK|t|| d  n|t|| d  |d7 }|t|k s|t| qt	|
d}|S )a  Transforms list of value and consecutive occurrences into a binary sequence with respect to stability

    Args:
        pairs (List): List of list of value and consecutive occurrences
        stability (Float): Minimal number of seconds to change from 0 to 1 or 1 to 0.
        sample_rate (int): The sample rate of the waveform.

    Returns:
        Torch.tensor : The binary sequences.
    r   r   )check_silence_or_voicelenintresolve_instabilityr&   torchoneszeroshstackvstack	unsqueeze)r   r
   r   batch_activepairr   checkr#   valuenum_consecutive_occurrencesactivednot_activedr   r   r   r   @   s.   
r   c                 C   sH   |d \}}d}t |dkr d}|rt|} | |fS t|} | |fS )zCheck if sequence is fully silence or fully voice.

    Args:
        active (List) : List containing the binary sequence
        pair: (List): list of value and consecutive occurrences

    r   Fr   T)r+   r.   r/   r0   )r   r5   r7   r8   r6   r   r   r   r*   o   s   

r*   c           	      C   s^  | t |k r=||  d t|| k r=||  \}}|r#||7 }| d7 } n||7 }| d7 } | t |k r=||  d t|| k s|| t|| k rqt |dkrq|d d dkrc|t||  || fS |t||  || fS || t|| k rt |dkr|t||  || fS ||kr|t||  || fS |t||  || fS )a  Resolve stability issue in input list of value and num_consecutive_occ

    Args:
        i (int): The index of the considered pair of value and num_consecutive_occ.
        pair (list) : Value and num_consecutive_occ.
        stability (float): Minimal number of seconds to change from 0 to 1 or 1 to 0.
        sample_rate (int): The sample rate of the waveform.
        actived (int) : Number of occurrences of the value 1.
        not_actived (int): Number of occurrences of the value 0.
        active (list) : The binary sequence.

    Returns:
        active (list) : The binary sequence.
         i (int): The index of the considered pair of value and num_consecutive_occ.
    r   r   )r+   r,   r&   r.   r/   r0   )	r#   r5   r
   r   r9   r:   r   r7   r8   r   r   r   r-      s,   $
$ 
 	r-   )
	itertoolsr   r.   nnModuler   r   r   r*   r-   r   r   r   r   <module>   s    %/