
    Vji                     \    d dl mZ d dlmZmZ d dlZd dlmZ d dlmZ  G d d          Z	dS )    )Path)OptionalUnionN)Tensor)_corec                      e Zd ZdZdedefdZdddddeee	f         de
e         d	e
e         de
e         d
df
dZdddddede
e         d	e
e         de
e         d
ef
dZdddddede
e         d	e
e         de
e         d
df
dZdS )AudioEncodera  An audio encoder.

    Args:
        samples (``torch.Tensor``): The samples to encode. This must be a 2D
            tensor of shape ``(num_channels, num_samples)``, or a 1D tensor in
            which case ``num_channels = 1`` is assumed. Values must be float
            values in ``[-1, 1]``.
        sample_rate (int): The sample rate of the **input** ``samples``. The
            sample rate of the encoded output can be specified using the
            encoding methods (``to_file``, etc.).
    samplessample_ratec                   t           j                            d           t          |t                    s t          dt          |          d          |j        dk    rt          j        |d          }|j        dk    rt          d|j	        d          |j
        t           j        k    rt          d|j
        d          |dk    rt          d	|d
          || _        || _        d S )Nz torchcodec.encoders.AudioEncoderz5Expected samples to be a Tensor, got type(samples) = .   r      z/Expected 1D or 2D samples, got samples.shape = z.Expected float32 samples, got samples.dtype = zsample_rate = z must be > 0.)torch_C_log_api_usage_once
isinstancer   
ValueErrortypendim	unsqueezeshapedtypefloat32_samples_sample_rate)selfr
   r   s      \/root/voice-cloning/.venv/lib/python3.11/site-packages/torchcodec/encoders/_audio_encoder.py__init__zAudioEncoder.__init__   s   $$%GHHH '6** 	KWKKK   <1ogq11G<1Rw}RRRSSS=EM))QgmQQQRRR!====>>>'    N)bit_ratenum_channelsr   destr!   r"   returnc                h    t          j        | j        | j        t	          |          |||           dS )ab  Encode samples into a file.

        Args:
            dest (str or ``pathlib.Path``): The path to the output file, e.g.
                ``audio.mp3``. The extension of the file determines the audio
                format and container.
            bit_rate (int, optional): The output bit rate. Encoders typically
                support a finite set of bit rate values, so ``bit_rate`` will be
                matched to one of those supported values. The default is chosen
                by FFmpeg.
            num_channels (int, optional): The number of channels of the encoded
                output samples. By default, the number of channels of the input
                ``samples`` is used.
            sample_rate (int, optional): The sample rate of the encoded output.
                By default, the sample rate of the input ``samples`` is used.
        )r
   r   filenamer!   r"   desired_sample_rateN)r   encode_audio_to_filer   r   str)r   r#   r!   r"   r   s        r   to_filezAudioEncoder.to_file-   sE    0 	"M)YY% +	
 	
 	
 	
 	
 	
r    formatc                J    t          j        | j        | j        ||||          S )as  Encode samples into raw bytes, as a 1D uint8 Tensor.

        Args:
            format (str): The format of the encoded samples, e.g. "mp3", "wav"
                or "flac".
            bit_rate (int, optional): The output bit rate. Encoders typically
                support a finite set of bit rate values, so ``bit_rate`` will be
                matched to one of those supported values. The default is chosen
                by FFmpeg.
            num_channels (int, optional): The number of channels of the encoded
                output samples. By default, the number of channels of the input
                ``samples`` is used.
            sample_rate (int, optional): The sample rate of the encoded output.
                By default, the sample rate of the input ``samples`` is used.

        Returns:
            Tensor: The raw encoded bytes as 1D uint8 Tensor.
        )r
   r   r+   r!   r"   r'   )r   encode_audio_to_tensorr   r   )r   r+   r!   r"   r   s        r   	to_tensorzAudioEncoder.to_tensorN   s5    4 +M)% +
 
 
 	
r    c          	      P    t          j        | j        | j        |||||           dS )al  Encode samples into a file-like object.

        Args:
            file_like: A file-like object that supports ``write()`` and
                ``seek()`` methods, such as io.BytesIO(), an open file in binary
                write mode, etc. Methods must have the following signature:
                ``write(data: bytes) -> int`` and ``seek(offset: int, whence:
                int = 0) -> int``.
            format (str): The format of the encoded samples, e.g. "mp3", "wav"
                or "flac".
            bit_rate (int, optional): The output bit rate. Encoders typically
                support a finite set of bit rate values, so ``bit_rate`` will be
                matched to one of those supported values. The default is chosen
                by FFmpeg.
            num_channels (int, optional): The number of channels of the encoded
                output samples. By default, the number of channels of the input
                ``samples`` is used.
            sample_rate (int, optional): The sample rate of the encoded output.
                By default, the sample rate of the input ``samples`` is used.
        )r
   r   r+   	file_liker!   r"   r'   N)r   encode_audio_to_file_liker   r   )r   r0   r+   r!   r"   r   s         r   to_file_likezAudioEncoder.to_file_likeq   sB    : 	'M)% +	
 	
 	
 	
 	
 	
r    )__name__
__module____qualname____doc__r   intr   r   r)   r   r   r*   r.   r2    r    r   r	   r	   
   s|       
 
( ( ( ( ( (4 #'&*%)
 
 
CI
 3-	

 sm
 c]
 

 
 
 
J #'&*%)!
 !
 !
!
 3-	!

 sm!
 c]!
 
!
 !
 !
 !
P #'&*%)%
 %
 %
 %

 3-%
 sm%
 c]%
 
%
 %
 %
 %
 %
 %
r    r	   )
pathlibr   typingr   r   r   r   
torchcodecr   r	   r8   r    r   <module>r<      s          " " " " " " " "             L
 L
 L
 L
 L
 L
 L
 L
 L
 L
r    