o
    pi                     @   sH   d dl Z d dlZd dlZd dlmZ ddlmZ G dd de jjZ	dS )    N)is_multichannel   )&MultichannelAudioNotSupportedExceptionc                       sL   e Zd ZdZdZ	ddededef fddZdde	j
e fd
dZ  ZS )
RandomCropz2Crop the audio to predefined length in max_length.Tseconds
max_lengthsampling_ratemax_length_unitc                    sN   t t|   || _|dkrt| j| | _dS |dkr#t|| _dS td)a  
        :param max_length: length to which samples are to be cropped.
        :sampling_rate: sampling rate of input samples.
        :max_length_unit: defines the unit of max_length.
            "seconds": Number of seconds
            "samples": Number of audio samples
        r   samplesz.max_length_unit must be "samples" or "seconds"N)superr   __init__r   intnum_samples
ValueError)selfr   r   r	   	__class__ c/home/ubuntu/.local/lib/python3.10/site-packages/torch_audiomentations/augmentations/random_crop.pyr      s   
zRandomCrop.__init__Nc                 C   s>  |p| j }|d u rtdt|dkrtd| jj |S t|jdkr*tdt	|rI|jd |jd kr=td | j
sItd	| jj|jd | jk rXtd
 |S td|jd | j |jd f}tj|jd |jd | jf|jd}t|D ]\}}|dd d d d || || | j f ||< q|S )Nzsample_rate is requiredr   z(An empty samples tensor was passed to {}   ztorch-audiomentations expects input tensors to be three-dimensional, with dimension ordering like [batch_size, num_channels, num_samples]. If your audio is mono, you can use a shape like [batch_size, 1, num_samples].   r   zMultichannel audio must have channels first, not channels last. In other words, the shape must be (batch size, channels, samples), not (batch_size, samples, channels)z3{} only supports mono audio, not multichannel audioz&audio length less than cropping length)device)r   RuntimeErrorlenwarningswarnformatr   __name__shaper   supports_multichannelr   r   torchrandintemptyr   	enumerate	unsqueeze)r   r
   r   sample_ratestart_indicessamples_croppedisampler   r   r   forward!   sJ   

"
zRandomCrop.forward)r   )N)r   
__module____qualname____doc__r   floatr   strr   typingOptionalr*   __classcell__r   r   r   r   r      s    r   )
r    r0   r   (torch_audiomentations.utils.multichannelr   core.transforms_interfacer   nnModuler   r   r   r   r   <module>   s    