
    Vji$                     x    d dl Z 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
mZ d dlmZmZ  G d d          ZdS )	    N)Path)OptionalUnion)Tensor)_coreAudioSamples)create_decoderERROR_REPORTING_INSTRUCTIONSc            
           e Zd ZdZdddddeeeej        ej	        e
ef         dee         dee         dee         fdZd	efd
Z	 ddedee         d	efdZdS )AudioDecodera!  A single-stream audio decoder.

    This can be used to decode audio from pure audio files (e.g. mp3, wav,
    etc.), or from videos that contain audio streams (e.g. mp4 videos).

    Returned samples are float samples normalized in [-1, 1]

    Args:
        source (str, ``Pathlib.path``, bytes, ``torch.Tensor`` or file-like
            object): The source of the video or audio:

            - If ``str``: a local path or a URL to a video or audio file.
            - If ``Pathlib.path``: a path to a local video or audio file.
            - If ``bytes`` object or ``torch.Tensor``: the raw encoded audio data.
            - If file-like object: we read video data from the object on demand. The object must
              expose the methods `read(self, size: int) -> bytes` and
              `seek(self, offset: int, whence: int) -> int`. Read more in:
              :ref:`sphx_glr_generated_examples_decoding_file_like.py`.
        stream_index (int, optional): Specifies which stream in the file to decode samples from.
            Note that this index is absolute across all media types. If left unspecified, then
            the :term:`best stream` is used.
        sample_rate (int, optional): The desired output sample rate of the decoded samples.
            By default, the sample rate of the source is used.
        num_channels (int, optional): The desired number of channels of the decoded samples.
            By default, the number of channels of the source is used.

    Attributes:
        metadata (AudioStreamMetadata): Metadata of the audio stream.
        stream_index (int): The stream index that this decoder is retrieving samples from. If a
            stream index was provided at initialization, this is the same value. If it was left
            unspecified, this is the :term:`best stream`.
    Nstream_indexsample_ratenum_channelssourcer   r   r   c                ^   t           j                            d           t          |d          | _        t          j        | j                  }||j        n|| _        | j        t          dt          z             | j        t          |j                  k    rt          d| d          |j        | j                 | _        t          | j        t
          j        j                  st          d| d          ||n| j        j        | _        t          j        | j        |||           d S )	Nz torchcodec.decoders.AudioDecoderapproximate)r   	seek_modezCThe best audio stream is unknown and there is no specified stream. zThe stream at index z is not a valid stream.z is not an audio stream. r   )torch_C_log_api_usage_oncer	   _decodercoreget_container_metadatabest_audio_stream_indexr   
ValueErrorr
   lenstreamsmetadata
isinstance	_metadataAudioStreamMetadatar   _desired_sample_rateadd_audio_stream)selfr   r   r   r   container_metadatas         \/root/voice-cloning/.venv/lib/python3.11/site-packages/torchcodec/decoders/_audio_decoder.py__init__zAudioDecoder.__init__7   s`    	$$%GHHH&fNNN!8GG # 66 	
 $U./   $6$> ? ???L|LLL   +243DE$-)KLL 	N|NNN  
 '2KK8Q 	! 	M%#%		
 	
 	
 	
 	
 	
    returnc                 *    |                                  S )a  Returns all the audio samples from the source.

        To decode samples in a specific range, use
        :meth:`~torchcodec.decoders.AudioDecoder.get_samples_played_in_range`.

        Returns:
            AudioSamples: The samples within the file.
        )get_samples_played_in_range)r%   s    r'   get_all_sampleszAudioDecoder.get_all_samplesc   s     //111r)           start_secondsstop_secondsc                    |||k    st          d| d| d          t          j        | j        ||          \  }}|                                }| j        }|J ||k     rt          ||z
  |z            }|}nd}|}|j        d         }|||z  z   }	|||	k     r|t          |	|z
  |z            z
  }
n|}
|dd||
f         }t          |||j        d         |z  |          S )	a>  Returns audio samples in the given range.

        Samples are in the half open range [start_seconds, stop_seconds).

        To decode all the samples from beginning to end, you can call this
        method while leaving ``start_seconds`` and ``stop_seconds`` to their
        default values, or use
        :meth:`~torchcodec.decoders.AudioDecoder.get_all_samples` as a more
        convenient alias.

        Args:
            start_seconds (float): Time, in seconds, of the start of the
                range. Default: 0.
            stop_seconds (float or None): Time, in seconds, of the end of the
                range. As a half open range, the end is excluded. Default: None,
                which decodes samples until the end.

        Returns:
            AudioSamples: The samples within the specified range.
        NzInvalid start seconds: z1. It must be less than or equal to stop seconds (z).)r/   r0   r      )datapts_secondsduration_secondsr   )	r   r    get_frames_by_pts_in_range_audior   itemr#   roundshaper   )r%   r/   r0   frames	first_ptsr   offset_beginningoutput_pts_secondsnum_sampleslast_pts
offset_endr3   s               r'   r,   z(AudioDecoder.get_samples_played_in_rangen   s[   . #M\,I,Iz-zzjvzzz   !AM'%
 
 
	
 NN$$	  /&&&}$$$mi&?;%NOO!.
  !!*l1o{[88#x(?(?$uh.E-T'U'UUJJ$Jaaa)*445*!Z][8#	
 
 
 	
r)   )r.   N)__name__
__module____qualname____doc__r   strr   io	RawIOBaseBufferedReaderbytesr   r   intr(   r   r-   floatr,    r)   r'   r   r      s         J '+%)&**
 *
 *
c4r/@%OP*
 sm	*

 c]*
 sm*
 *
 *
 *
X	2 	2 	2 	2 	2 KOK
 K
"K
8@K
	K
 K
 K
 K
 K
 K
r)   r   )rF   pathlibr   typingr   r   r   r   
torchcodecr   r   r   "torchcodec.decoders._decoder_utilsr	   r
   r   rL   r)   r'   <module>rQ      s    
			       " " " " " " " "        2 2 2 2 2 2 2 2       d
 d
 d
 d
 d
 d
 d
 d
 d
 d
r)   