o
    pi                     @   sd   d dl mZ d dlmZ d dlmZ d dlmZmZm	Z	 ddl
mZ ddlmZ G dd	 d	eZd
S )    )choices)Tensor)Optional)pitch_shiftget_fast_shiftssemitones_to_ratio   )BaseWaveformTransform)
ObjectDictc                       s   e Zd ZdZh dZdZdZdZdZ												dd
e	de	de
de	de
ded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  ZS )
PitchShiftzC
    Pitch-shift sounds up or down without changing the tempo.
    >   	per_batchper_channelper_exampleTF            @r         ?Nmin_transpose_semitonesmax_transpose_semitonesmodepp_modesample_ratetarget_rateoutput_typec	           	         sl   t  j||||||d  krtd|std|| _t| fdd| _t| js1td|| _dS )a  
        :param sample_rate:
        :param min_transpose_semitones: Minimum pitch shift transposition in semitones (default -4.0)
        :param max_transpose_semitones: Maximum pitch shift transposition in semitones (default +4.0)
        :param mode: ``per_example``, ``per_channel``, or ``per_batch``. Default ``per_example``.
        :param p:
        :param p_mode:
        :param target_rate:
        )r   r   r   r   r   r   z9max_transpose_semitones must be > min_transpose_semitoneszsample_rate is invalid.c                    s    | t ko| t  ko| dkS )N   )r   )xr   r    c/home/ubuntu/.local/lib/python3.10/site-packages/torch_audiomentations/augmentations/pitch_shift.py<lambda><   s    
z%PitchShift.__init__.<locals>.<lambda>z[No fast pitch-shift ratios could be computed for the given sample rate and transpose range.N)super__init__
ValueError_sample_rater   _fast_shiftslen_mode)	selfr   r   r   r   r   r   r   r   	__class__r   r   r!      s,   	

zPitchShift.__init__samplestargetsc                    s   |j \ }}jdkrtj djd< d	S jdkr1tt fddt|D  jd< d	S jdkrBtjddjd< d	S d	S )
e
        :param samples: (batch_size, num_channels, num_samples)
        :param sample_rate:
        r   ktranspositionsr   c                    s   g | ]	}t j d qS )r-   )r   r$   ).0i
batch_sizer'   r   r   
<listcomp>Z   s    z3PitchShift.randomize_parameters.<locals>.<listcomp>r   r   N)shaper&   r   r$   transform_parameterslistziprange)r'   r*   r   r+   r   num_channelsnum_samplesr   r2   r   randomize_parametersF   s    


zPitchShift.randomize_parametersreturnc           
      C   s  |j \}}}|dur|| jkrtd| j}| jdkr8t|D ]}t|| d | jd | |d ||df< qnB| jdkrjt|D ]'}t|D ] }	t|| |	 d d | jd | |	 |d d |||	df< qGqAn| jdkrzt|| jd d |}t||||d	S )
r,   NzVsample_rate must match the value of sample_rate passed into the PitchShift constructorr   r/   r   .r   r   )r*   r   r+   r   )	r5   r#   r"   r   r&   r9   r   r6   r
   )
r'   r*   r   r+   r   r3   r:   r;   r1   jr   r   r   apply_transformc   sP   



zPitchShift.apply_transform)r   r   r   r   NNNN)NNNN)__name__
__module____qualname____doc__supported_modessupports_multichannelrequires_sample_ratesupports_targetrequires_targetfloatstrintr   r!   r   r<   r
   r?   __classcell__r   r   r(   r   r      sx    	0
r   N)randomr   torchr   typingr   torch_pitch_shiftr   r   r   core.transforms_interfacer	   utils.object_dictr
   r   r   r   r   r   <module>   s    