§
    
@ÄjÄ5  ã                   óp  — d Z ddlZddlmZmZmZmZ ddlZ	 	 	 	 	 	 	 ddeeeej	        f         de
d	e
d
ededee         de
dee         deej        e
f         fd„Z	 	 	 	 	 	 	 ddeeej	        f         dej        de
dedee         dee         dee
         de
dee         deeee
f                  ddfd„ZdS )z&TorchCodec integration for TorchAudio.é    N)ÚBinaryIOÚOptionalÚTupleÚUnionéÿÿÿÿTé   ÚuriÚframe_offsetÚ
num_framesÚ	normalizeÚchannels_firstÚformatÚbuffer_sizeÚbackendÚreturnc                 ó\  — 	 ddl m} n"# t          $ r}	t          d¦  «        |	‚d}	~	ww xY w|s!ddl}
|
                     dt
          d¬¦  «         |dk    r!ddl}
|
                     d	t
          d¬¦  «         |�!ddl}
|
                     d
t
          d¬¦  «         |�!ddl}
|
                     dt
          d¬¦  «         	  || ¦  «        }n(# t          $ r}	t          d| › d|	› �¦  «        |	‚d}	~	ww xY w|j        j	        }|€t          d¦  «        ‚	 | 
                    ¦   «         }n%# t          $ r}	t          d|	› �¦  «        |	‚d}	~	ww xY w|j        }|dk    ra||j        d         k    rB|r|j        d         dfnd|j        d         f}t          j        |t          j        ¬¦  «        |fS |dd…|d…f         }|dk    rB|r|j        d         dfnd|j        d         f}t          j        |t          j        ¬¦  «        |fS |dk    r|dd…d|…f         }|s|                     dd¦  «        }||fS )aŽ  Load audio data from source using TorchCodec's AudioDecoder.

    .. note::

        This function supports the same API as :func:`~torchaudio.load`, and
        relies on TorchCodec's decoding capabilities under the hood. It is
        provided for convenience, but we do recommend that you port your code to
        natively use ``torchcodec``'s ``AudioDecoder`` class for better
        performance:
        https://docs.pytorch.org/torchcodec/stable/generated/torchcodec.decoders.AudioDecoder.
        As of TorchAudio 2.9, :func:`~torchaudio.load` relies on
        :func:`~torchaudio.load_with_torchcodec`. Note that some parameters of
        :func:`~torchaudio.load`, like ``normalize``, ``buffer_size``, and
        ``backend``, are ignored by :func:`~torchaudio.load_with_torchcodec`.
        To install torchcodec, follow the instructions at https://github.com/pytorch/torchcodec#installing-torchcodec.


    Args:
        uri (path-like object or file-like object):
            Source of audio data. The following types are accepted:

            * ``path-like``: File path or URL.
            * ``file-like``: Object with ``read(size: int) -> bytes`` method.

        frame_offset (int, optional):
            Number of samples to skip before start reading data.
        num_frames (int, optional):
            Maximum number of samples to read. ``-1`` reads all the remaining samples,
            starting from ``frame_offset``.
        normalize (bool, optional):
            TorchCodec always returns normalized float32 samples. This parameter
            is ignored and a warning is issued if set to False.
            Default: ``True``.
        channels_first (bool, optional):
            When True, the returned Tensor has dimension `[channel, time]`.
            Otherwise, the returned Tensor's dimension is `[time, channel]`.
        format (str or None, optional):
            Format hint for the decoder. May not be supported by all TorchCodec
            decoders. (Default: ``None``)
        buffer_size (int, optional):
            Not used by TorchCodec AudioDecoder. Provided for API compatibility.
        backend (str or None, optional):
            Not used by TorchCodec AudioDecoder. Provided for API compatibility.

    Returns:
        (torch.Tensor, int): Resulting Tensor and sample rate.
        Always returns float32 tensors. If ``channels_first=True``, shape is
        `[channel, time]`, otherwise `[time, channel]`.

    Raises:
        ImportError: If torchcodec is not available.
        ValueError: If unsupported parameters are used.
        RuntimeError: If TorchCodec fails to decode the audio.

    Note:
        - TorchCodec always returns normalized float32 samples, so the ``normalize``
          parameter has no effect.
        - The ``buffer_size`` and ``backend`` parameters are ignored.
        - Not all audio formats supported by torchaudio backends may be supported
          by TorchCodec.
    r   )ÚAudioDecoderz`TorchCodec is required for load_with_torchcodec. Please install torchcodec to use this function.NznTorchCodec AudioDecoder always returns normalized float32 samples. The 'normalize=False' parameter is ignored.é   ©Ú
stacklevelr   zCThe 'buffer_size' parameter is not used by TorchCodec AudioDecoder.z?The 'backend' parameter is not used by TorchCodec AudioDecoder.zCThe 'format' parameter is not supported by TorchCodec AudioDecoder.z"Failed to create AudioDecoder for ú: z3Unable to determine sample rate from audio metadataz Failed to decode audio samples: é   )Údtype)Útorchcodec.decodersr   ÚImportErrorÚwarningsÚwarnÚUserWarningÚ	ExceptionÚRuntimeErrorÚmetadataÚsample_rateÚget_all_samplesÚdataÚshapeÚtorchÚzerosÚfloat32Ú	transpose)r	   r
   r   r   r   r   r   r   r   Úer   Údecoderr"   Úaudio_samplesr$   Úempty_shapes                   úGC:\Projects\Historical\venv\Lib\site-packages\torchaudio/_torchcodec.pyÚload_with_torchcodecr/   	   s÷  € ðPØ4Ð4Ð4Ð4Ð4Ð4Ð4øÝð ð ð ÝØqñ
ô 
àð	øøøøðøøøð ð 
Øˆˆˆà�Šð:åØð	 	ñ 	
ô 	
ð 	
ð �dÒÐØˆˆˆà�ŠÐ[Õ]hÐuvˆÑwÔwÐwàÐØˆˆˆà�ŠÐWÕYdÐqrˆÑsÔsÐsàÐØˆˆˆà�ŠÐ[Õ]hÐuvˆÑwÔwÐwðSØ�,˜sÑ#Ô#ˆˆøÝð Sð Sð SÝÐJÀÐJÐJÀqÐJÐJÑKÔKÐQRÐRøøøøðSøøøð Ô"Ô.€KØÐÝÐPÑQÔQÐQðJØ×/Ò/Ñ1Ô1ˆˆøÝð Jð Jð JÝÐA¸aÐAÐAÑBÔBÈÐIøøøøðJøøøð Ô€Dð �aÒÐØ˜4œ: aœ=Ò(Ð(à0>ÐV˜4œ: aœ=¨!Ð,Ð,ÀQÈÌ
ÐSTÌÐDVˆKÝ”;˜{µ%´-Ð@Ñ@Ô@À+ÐMÐMØ�A�A�A�|�}�}Ð$Ô%ˆà�Q‚€à,:ÐR�t”z !”} aÐ(Ð(ÀÀDÄJÈqÄMÐ@RˆÝŒ{˜;­e¬mÐ<Ñ<Ô<¸kÐIÐIØ	�aŠˆØ�A�A�A�{˜
�{�NÔ#ˆð ð $Ø�~Š~˜a Ñ#Ô#ˆà�ÐÐs@   ‚	 ‰
(“#£(Â<C Ã
C-ÃC(Ã(C-ÄD# Ä#
EÄ-E Å EÚsrcr"   ÚencodingÚbits_per_sampleÚcompressionc
                 óx  — 	 ddl m}
 n"# t          $ r}t          d¦  «        |‚d}~ww xY w|�!ddl}|                     dt
          d¬¦  «         |�!ddl}|                     dt
          d¬¦  «         |�!ddl}|                     d	t
          d¬¦  «         |d
k    r!ddl}|                     dt
          d¬¦  «         |�!ddl}|                     dt
          d¬¦  «         t          |t          j        ¦  «        st          dt          |¦  «        › �¦  «        ‚|j        t          j        k    r|                     ¦   «         }|dk    rt          d|› �¦  «        ‚|j        dk    rB|r|                     d¦  «        }ni|                     d¦  «                             dd¦  «        }n?|j        dk    r|r|}n/|                     dd¦  «        }nt          d|j        › d�¦  «        ‚	  |
||¬¦  «        }n%# t"          $ r}t%          d|› �¦  «        |‚d}~ww xY wd}|	�^t          |	t&          t          f¦  «        rt'          |	¦  «        }n2ddl}|                     dt          |	¦  «        › d�t
          d¬¦  «         	 |                     | |¬¦  «         dS # t"          $ r}t%          d| › d|› �¦  «        |‚d}~ww xY w)aÚ  Save audio data to file using TorchCodec's AudioEncoder.

    .. note::

        This function supports the same API as :func:`~torchaudio.save`, and
        relies on TorchCodec's encoding capabilities under the hood. It is
        provided for convenience, but we do recommend that you port your code to
        natively use ``torchcodec``'s ``AudioEncoder`` class for better
        performance:
        https://docs.pytorch.org/torchcodec/stable/generated/torchcodec.encoders.AudioEncoder.
        As of TorchAudio 2.9, :func:`~torchaudio.save` relies on
        :func:`~torchaudio.save_with_torchcodec`. Note that some parameters of
        :func:`~torchaudio.save`, like ``format``, ``encoding``,
        ``bits_per_sample``, ``buffer_size``, and ``backend``, are ignored by
        are ignored by :func:`~torchaudio.save_with_torchcodec`.
        To install torchcodec, follow the instructions at https://github.com/pytorch/torchcodec#installing-torchcodec.

    This function provides a TorchCodec-based alternative to torchaudio.save
    with the same API. TorchCodec's AudioEncoder provides efficient encoding
    with FFmpeg under the hood.

    Args:
        uri (path-like object):
            Path to save the audio file. The file extension determines the format.

        src (torch.Tensor):
            Audio data to save. Must be a 1D or 2D tensor with float32 values
            in the range [-1, 1]. If 2D, shape should be [channel, time] when
            channels_first=True, or [time, channel] when channels_first=False.

        sample_rate (int):
            Sample rate of the audio data.

        channels_first (bool, optional):
            Indicates whether the input tensor has channels as the first dimension.
            If True, expects [channel, time]. If False, expects [time, channel].
            Default: True.

        format (str or None, optional):
            Audio format hint. Not used by TorchCodec (format is determined by
            file extension). A warning is issued if provided.
            Default: None.

        encoding (str or None, optional):
            Audio encoding. Not fully supported by TorchCodec AudioEncoder.
            A warning is issued if provided. Default: None.

        bits_per_sample (int or None, optional):
            Bits per sample. Not directly supported by TorchCodec AudioEncoder.
            A warning is issued if provided. Default: None.

        buffer_size (int, optional):
            Not used by TorchCodec AudioEncoder. Provided for API compatibility.
            A warning is issued if not default value. Default: 4096.

        backend (str or None, optional):
            Not used by TorchCodec AudioEncoder. Provided for API compatibility.
            A warning is issued if provided. Default: None.

        compression (float, int or None, optional):
            Compression level or bit rate. Maps to bit_rate parameter in
            TorchCodec AudioEncoder. Default: None.

    Raises:
        ImportError: If torchcodec is not available.
        ValueError: If input parameters are invalid.
        RuntimeError: If TorchCodec fails to encode the audio.

    Note:
        - TorchCodec AudioEncoder expects float32 samples in [-1, 1] range.
        - Some parameters (format, encoding, bits_per_sample, buffer_size, backend)
          are not used by TorchCodec but are provided for API compatibility.
        - The output format is determined by the file extension in the uri.
        - TorchCodec uses FFmpeg under the hood for encoding.
    r   )ÚAudioEncoderz`TorchCodec is required for save_with_torchcodec. Please install torchcodec to use this function.NzjThe 'format' parameter is not used by TorchCodec AudioEncoder. Format is determined by the file extension.r   r   zKThe 'encoding' parameter is not fully supported by TorchCodec AudioEncoder.zUThe 'bits_per_sample' parameter is not directly supported by TorchCodec AudioEncoder.r   zCThe 'buffer_size' parameter is not used by TorchCodec AudioEncoder.z?The 'backend' parameter is not used by TorchCodec AudioEncoder.z'Expected src to be a torch.Tensor, got z"sample_rate must be positive, got r   zExpected 1D or 2D tensor, got zD tensor)r"   zFailed to create AudioEncoder: zUnsupported compression type z<. TorchCodec AudioEncoder expects int or float for bit_rate.)Úbit_ratezFailed to save audio to r   )Útorchcodec.encodersr5   r   r   r   r   Ú
isinstancer&   ÚTensorÚ
ValueErrorÚtyper   r(   ÚfloatÚndimÚ	unsqueezer)   r   r    ÚintÚto_file)r	   r0   r"   r   r   r1   r2   r   r   r3   r5   r*   r   r$   Úencoderr6   s                   r.   Úsave_with_torchcodecrB   �   s–  € ðpØ4Ð4Ð4Ð4Ð4Ð4Ð4øÝð ð ð ÝØqñ
ô 
àð	øøøøðøøøð ÐØˆˆˆà�Šð:åØð	 	ñ 	
ô 	
ð 	
ð ÐØˆˆˆà�ŠØYÕ[fÐstð 	ñ 	
ô 	
ð 	
ð Ð"Øˆˆˆà�ŠØcÝØð 	ñ 	
ô 	
ð 	
ð �dÒÐØˆˆˆà�ŠÐ[Õ]hÐuvˆÑwÔwÐwàÐØˆˆˆà�ŠÐWÕYdÐqrˆÑsÔsÐsõ �c�5œ<Ñ(Ô(ð PÝÐNÅ4ÈÁ9Ä9ÐNÐNÑOÔOÐOà
„y•E”MÒ!Ð!Ø�iŠi‰kŒkˆà�aÒÐÝÐK¸kÐKÐKÑLÔLÐLð „x�1‚}€}àð 	4Ø—=’= Ñ#Ô#ˆDˆDð —=’= Ñ#Ô#×-Ò-¨a°Ñ3Ô3ˆDˆDØ	Œ�QŠˆØð 	'ØˆDˆDà—=’=  AÑ&Ô&ˆDˆDåÐL¸#¼(ÐLÐLÐLÑMÔMÐMðIØ�,˜t°Ð=Ñ=Ô=ˆˆøÝð Ið Ið IÝÐ@¸QÐ@Ð@ÑAÔAÀqÐHøøøøðIøøøð €HØÐÝ�k¥C­ <Ñ0Ô0ð 
	Ý˜;Ñ'Ô'ˆHˆHàˆOˆOˆOà�MŠMðMµ°[Ñ0AÔ0Að Mð Mð MåØð	 ñ ô ð ðIØ�Š˜ hˆÑ/Ô/Ð/Ð/Ð/øÝð Ið Ið IÝÐ@°cÐ@Ð@¸QÐ@Ð@ÑAÔAÀqÐHøøøøðIøøøs@   ‚	 ‰
(“#£(Ç%G3 Ç3
HÇ=HÈHÉ;J Ê
J9ÊJ4Ê4J9)r   r   TTNr   N)TNNNr   NN)Ú__doc__ÚosÚtypingr   r   r   r   r&   ÚstrÚPathLiker?   Úboolr9   r/   r<   rB   © ó    r.   ú<module>rK      sì  ðØ ,Ð ,à 	€	€	€	Ø 3Ð 3Ð 3Ð 3Ð 3Ð 3Ð 3Ð 3Ð 3Ð 3Ð 3Ð 3à €€€ð
 ØØØØ ØØ!ðQð QØ	ˆx˜˜bœkÐ)Ô	*ðQàðQð ðQð ð	Qð
 ðQð �SŒMðQð ðQð �cŒ]ðQð ˆ5Œ<˜ÐÔðQð Qð Qð Qðp  Ø Ø"Ø%)ØØ!Ø/3ðwIð wIØ	ˆs�B”KÐÔ	 ðwIà	ŒðwIð ðwIð ð	wIð
 �SŒMðwIð �sŒmðwIð ˜c”]ðwIð ðwIð �cŒ]ðwIð ˜%  s 
Ô+Ô,ðwIð 
ðwIð wIð wIð wIð wIð wIrJ   