o
    piQ                     @   s   d dl Z d dlmZmZ d dl mZ ddlmZ ddlmZ dde jd	e jd
e	fddZ
	dde jde jd
e	fddZG dd deZdS )    N)OptionalUnion)Tensor   )BaseWaveformTransform)
ObjectDictFtensorrrolloverc           
      C   s   | j \}}}tj|| jd}|ddddf }|| |||g}t| d|| }|r.|S |d d}	d|	|	|k< d||	dk< |S )z Shift or roll a batch of tensors)deviceNr      r   )shapetorcharanger   expandgatherclamp)
r   r	   r
   bctxidxsret
cut_points r   ]/home/ubuntu/.local/lib/python3.10/site-packages/torch_audiomentations/augmentations/shift.py	shift_gpu	   s   r   selected_samplesshift_samplesc                 C   sz   |  d}t|D ]1}||  }tj| | |dd| |< |s:|dkr-d| |dd|f< q	|dk r:d| |d|df< q	| S )zMShift or roll a batch of tensors with the help of a for loop and torch.roll()r   )shiftsdimsg        .N)sizerangeitemr   roll)r   r   r
   selected_batch_sizeinum_samples_to_shiftr   r   r   	shift_cpu   s   


r)   c                       s
  e Zd ZdZh dZdZdZdZdZ														dd
e	e
ef de	e
ef dededede
dee dee dee dee f fddZ								d dedee dee dee fddZ								d dedee dee dee def
ddZdefddZ  ZS )!ShiftzI
    Shift the audio forwards or backwards, with or without rollover
    >   	per_batchper_channelper_exampleTF            ?fractionr-   N	min_shift	max_shift
shift_unitr
   modepp_modesample_ratetarget_rateoutput_typec                    sZ   t  j|||||	|
d || _|| _|| _|| _| j| jkr"td| jdvr+tddS )aW  

        :param min_shift: minimum amount of shifting in time. See also shift_unit.
        :param max_shift: maximum amount of shifting in time. See also shift_unit.
        :param shift_unit: Defines the unit of the value of min_shift and max_shift.
            "fraction": Fraction of the total sound length
            "samples": Number of audio samples
            "seconds": Number of seconds
        :param rollover: When set to True, samples that roll beyond the first or last position
            are re-introduced at the last or first. When set to False, samples that roll beyond
            the first or last position are discarded. In other words, rollover=False results in
            an empty space (with zeroes).
        :param mode:
        :param p:
        :param p_mode:
        :param sample_rate:
        :param target_rate:
        )r4   r5   r6   r7   r8   r9   z,min_shift must not be greater than max_shift)r0   samplessecondsz5shift_unit must be "samples", "fraction" or "seconds"N)super__init__r1   r2   r3   r
   
ValueError)selfr1   r2   r3   r
   r4   r5   r6   r7   r8   r9   	__class__r   r   r=   @   s"   
zShift.__init__r:   targetsc                 C   sH  | j dkr| j}| j}n:| j dkr*tt| j|jd  }tt| j|jd  }n| j dkrBtt| j| }tt| j| }ntdttj	j
|  kr[ttj	jks^J  J ttj	j
|  krsttj	jksvJ  J |d}||krtj|f|tj	|jd| jd< d S tj||d	 |ftj	|jd
| jd< d S )Nr:   r0   r   r;   zInvalid shift_unitr   )r"   
fill_valuedtyper   r(   r   )lowhighr"   rD   r   )r3   r1   r2   introundr   r>   r   iinfoint32minmaxr"   fullr   transform_parametersrandint)r?   r:   r7   rB   r8   min_shift_in_samplesmax_shift_in_samplesr&   r   r   r   randomize_parametersp   sF   



zShift.randomize_parametersreturnc           
      C   s   | j d }|jjdkrtnt}|||| j}|d u s|dkr!|}ntt|| | }	||dd|	| jdd}t	||||dS )Nr(   cudar   r   )r:   r7   rB   r8   )
rN   r   typer   r)   r
   rG   rH   	transposer   )
r?   r:   r7   rB   r8   r(   shiftshifted_samplesshifted_targetsnum_frames_to_shiftr   r   r   apply_transform   s&   
zShift.apply_transformc                 C   s
   | j dkS )Nr;   )r3   )r?   r   r   r   is_sample_rate_required   s   
zShift.is_sample_rate_required)
r.   r/   r0   Tr-   r/   NNNN)NNNN)__name__
__module____qualname____doc__supported_modessupports_multichannelrequires_sample_ratesupports_targetrequires_targetr   floatrG   strboolr   r=   r   rR   r   r\   r]   __classcell__r   r   r@   r   r*   3   s    

	
2
4
r*   )F)r   typingr   r   r   core.transforms_interfacer   utils.object_dictr   ri   r   r)   r*   r   r   r   r   <module>   s    
