Ë
    P/æj^.  ã                   óü   — d dl Z d dlmZ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ej                   fd	„Z G d
„ d«      Zdededededee   dee   fd„Zdej                   fd„Z G d„ d«      Zy)é    N)ÚIteratorÚListÚOptional)ÚTensor)Ú_get_afilter_descÚStreamingMediaDecoder)ÚCodecConfigÚStreamingMediaEncoderc                   ó(   — e Zd ZdZd„ Zdefd„Zd„ Zy)Ú_StreamingIOBufferz6Streaming Bytes IO buffer. Data are dropped when read.c                 ó   — g | _         y ©N)Ú_buffer)Úselfs    úZ/root/repos/pocs/video-evals/.venv/lib/python3.12/site-packages/torchaudio/io/_effector.pyÚ__init__z_StreamingIOBuffer.__init__   s	   € Ø$&ˆ�ó    Úbc                 óR   — |r| j                   j                  |«       t        |«      S r   )r   ÚappendÚlen)r   r   s     r   Úwritez_StreamingIOBuffer.write   s    € ÙØ�L‰L×Ñ Ô"Ü�1‹vˆr   c                 óî   — | j                   syt        | j                   d   «      |k  r| j                   j                  d«      S | j                   d   d| }| j                   d   |d | j                   d<   |S )zMPop the oldest byte string. It does not necessary return the requested amountr   r   N)r   r   Úpop)r   ÚnÚrets      r   r   z_StreamingIOBuffer.pop   sk   € à�|Š|ØÜˆt�|‰|˜A‰Ó 1Ò$Ø—<‘<×#Ñ# AÓ&Ð&Ø�l‰l˜1‰o˜b˜qÐ!ˆØŸ,™, q™/¨!¨"Ð-ˆ�‰�Q‰Øˆ
r   N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   Úbytesr   r   © r   r   r   r      s   „ Ù@ò'ð�uó ó
r   r   Údtypec           
      óö   — t         j                  dt         j                  dt         j                  dt         j                  dt         j
                  di}| |vrt        d| › d|j                  «       › �«      ‚||    S )NÚu8Ús16Ús32ÚfltÚdblúUnsupported dtype is provided ú. Supported dtypes are: ©ÚtorchÚuint8Úint16Úint32Úfloat32Úfloat64Ú
ValueErrorÚkeys©r#   Útypess     r   Ú_get_sample_fmtr7   !   so   € ä�‰�TÜ�‰�UÜ�‰�UÜ�‰�uÜ�‰�uð€Eð �EÑÜÐ9¸%¸Ð@XÐY^×YcÑYcÓYeÐXfÐgÓhÐhØ�‰<Ðr   c                   óF   — e Zd ZdZdededededee   dee   defd	„Z	d
„ Z
y)Ú_AudioStreamingEncoderz3Given a waveform, encode on-demand and return bytesÚsrcÚsample_rateÚeffectÚmuxerÚencoderÚcodec_configÚframes_per_chunkc                 ó>  — || _         t        «       | _        t        | j                  |¬«      | _        | j                  j                  |j                  d«      |t        |j                  «      |||¬«       | j                  j                  «        || _
        d| _        y ©N)Úformaté   )Únum_channelsr;   rC   r>   Úfilter_descr?   r   )r:   r   ÚbufferÚStreamWriterÚwriterÚadd_audio_streamÚsizer7   r#   ÚopenÚfpcÚi_iter)r   r:   r;   r<   r=   r>   r?   r@   s           r   r   z_AudioStreamingEncoder.__init__1   sƒ   € ð ˆŒÜ(Ó*ˆŒÜ" 4§;¡;°uÔ=ˆŒØ�‰×$Ñ$ØŸ™ !›Ø#Ü" 3§9¡9Ó-ØØØ%ð 	%ô 	
ð 	�‰×ÑÔØ#ˆŒð
 ˆ�r   c                 óh  — | j                   j                  �s| j                  dk\  rò| j                  j	                  d| j
                  | j                  | j                  | j                  z    «       | xj                  | j                  z  c_        | j                  | j
                  j                  d«      k\  r;| j                  j                  «        | j                  j                  «        d| _        | j                   j                  s| j                  dk\  rŒò| j                   j                  |«      S )Nr   éÿÿÿÿ)rG   r   rN   rI   Úwrite_audio_chunkr:   rM   rK   ÚflushÚcloser   )r   r   s     r   Úreadz_AudioStreamingEncoder.readN   sÎ   € Ø—+‘+×%Ó%¨$¯+©+¸Ò*:Ø�K‰K×)Ñ)¨!¨T¯X©X°d·k±kÀDÇKÁKÐRV×RZÑRZÑDZÐ-[Ô\Ø�KŠK˜4Ÿ8™8Ñ#�KØ�{‰{˜dŸh™hŸm™m¨AÓ.Ò.Ø—‘×!Ñ!Ô#Ø—‘×!Ñ!Ô#Ø �”ð —+‘+×%Ò%¨$¯+©+¸Ó*:ð �{‰{�‰˜qÓ!Ð!r   N)r   r   r   r    r   ÚintÚstrr   r	   r   rT   r"   r   r   r9   r9   .   s\   „ Ù=ðàðð ðð ð	ð
 ðð ˜#‘ðð ˜{Ñ+ðð óó:"r   r9   r:   r;   r<   r=   r>   r?   c                 óJ  — t        j                  «       }t        ||¬«      }|j                  | j	                  d«      |t        | j                  «      |||¬«       |j                  «       5  |j                  d| «       d d d «       |j                  d«       |S # 1 sw Y   ŒxY wrB   )
ÚioÚBytesIOrH   rJ   rK   r7   r#   rL   rQ   Úseek)r:   r;   r<   r=   r>   r?   rG   rI   s           r   Ú_encoder[   Y   s‘   € ô �Z‰Z‹\€FÜ˜&¨Ô/€FØ
×ÑØ—X‘X˜a“[ØÜ˜sŸy™yÓ)ØØØ!ð ô ð 
�‰‹ñ )Ø× Ñ   CÔ(÷)à
‡K�K�„NØ€M÷)ð )ús   Á+BÂB"c           
      óö   — t         j                  dt         j                  dt         j                  dt         j                  dt         j
                  di}| |vrt        d| › d|j                  «       › �«      ‚||    S )Nr%   Ús16leÚs32leÚf32leÚf64ler*   r+   r,   r5   s     r   Ú
_get_muxerra   q   sq   € ô 	�‰�TÜ�‰�WÜ�‰�WÜ�‰�wÜ�‰�wð€Eð �EÑÜÐ9¸%¸Ð@XÐY^×YcÑYcÓYeÐXfÐgÓhÐhØ�‰<Ðr   c                   ó¨   — e Zd ZdZ	 	 dddddœdee   dee   dee   dee   d	ef
d
„Zdd„Z	dde
dedee   de
fd„Z	 dde
dededee   dee
   f
d„Zy)ÚAudioEffectora  Apply various filters and/or codecs to waveforms.

    .. versionadded:: 2.1

    Args:
        effect (str or None, optional): Filter expressions or ``None`` to apply no filter.
            See https://ffmpeg.org/ffmpeg-filters.html#Audio-Filters for the
            details of filter syntax.

        format (str or None, optional): When provided, encode the audio into the
            corresponding format. Default: ``None``.

        encoder (str or None, optional): When provided, override the encoder used
            by the ``format``. Default: ``None``.

        codec_config (CodecConfig or None, optional): When provided, configure the encoding codec.
            Should be provided in conjunction with ``format`` option.

        pad_end (bool, optional): When enabled, and if the waveform becomes shorter after applying
            effects/codec, then pad the end with silence.

    Example - Basic usage
        To use ``AudioEffector``, first instantiate it with a set of
        ``effect`` and ``format``.

        >>> # instantiate the effector
        >>> effector = AudioEffector(effect=..., format=...)

        Then, use :py:meth:`~AudioEffector.apply` or :py:meth:`~AudioEffector.stream`
        method to apply them.

        >>> # Apply the effect to the whole waveform
        >>> applied = effector.apply(waveform, sample_rate)

        >>> # Apply the effect chunk-by-chunk
        >>> for chunk in effector.stream(waveform, sample_rate):
        >>>    ...

    Example - Applying effects
        Please refer to
        https://ffmpeg.org/ffmpeg-filters.html#Filtergraph-description
        for the overview of filter description, and
        https://ffmpeg.org/ffmpeg-filters.html#toc-Audio-Filters
        for the list of available filters.

        Tempo - https://ffmpeg.org/ffmpeg-filters.html#atempo

        >>> AudioEffector(effect="atempo=1.5")

        Echo - https://ffmpeg.org/ffmpeg-filters.html#aecho

        >>> AudioEffector(effect="aecho=0.8:0.88:60:0.4")

        Flanger - https://ffmpeg.org/ffmpeg-filters.html#flanger

        >>> AudioEffector(effect="aflanger")

        Vibrato - https://ffmpeg.org/ffmpeg-filters.html#vibrato

        >>> AudioEffector(effect="vibrato")

        Tremolo - https://ffmpeg.org/ffmpeg-filters.html#tremolo

        >>> AudioEffector(effect="vibrato")

        You can also apply multiple effects at once.

        >>> AudioEffector(effect="")

    Example - Applying codec
        One can apply codec using ``format`` argument. ``format`` can be
        audio format or container format. If the container format supports
        multiple encoders, you can specify it with ``encoder`` argument.

        Wav format
        (no compression is applied but samples are converted to
        16-bit signed integer)

        >>> AudioEffector(format="wav")

        Ogg format with default encoder

        >>> AudioEffector(format="ogg")

        Ogg format with vorbis

        >>> AudioEffector(format="ogg", encoder="vorbis")

        Ogg format with opus

        >>> AudioEffector(format="ogg", encoder="opus")

        Webm format with opus

        >>> AudioEffector(format="webm", encoder="opus")

    Example - Applying codec with configuration
        Reference: https://trac.ffmpeg.org/wiki/Encode/MP3

        MP3 with default config

        >>> AudioEffector(format="mp3")

        MP3 with variable bitrate

        >>> AudioEffector(format="mp3", codec_config=CodecConfig(qscale=5))

        MP3 with constant bitrate

        >>> AudioEffector(format="mp3", codec_config=CodecConfig(bit_rate=32_000))
    NT)r>   r?   Úpad_endr<   rC   r>   r?   rd   c                ól   — |€|€|�t        d«      ‚|| _        || _        || _        || _        || _        y )NzM`encoder` and/or `condec_config` opions are provided without `format` option.)r3   r<   rC   r>   r?   rd   )r   r<   rC   r>   r?   rd   s         r   r   zAudioEffector.__init__ð   sD   € ð ˆ>ØÐ" lÐ&>Ü Ð!pÓqÐqØˆŒØˆŒØˆŒØ(ˆÔØˆ�r   c           	      ó0  — |j                   \  }}| j                  �1| j                  }| j                  }i }	| j                  dk(  r&|› |› dœ}	nt        |j                  «      }d }|› |› dœ}	|€%t        ||| j                  ||| j                  «      }
n%t        ||| j                  ||| j                  |«      }
|€|n|}t        |t        |j                  «      |«      }| j                  r|› d|› �}t        |
||	¬«      }|j                  |xs dd|¬«       |S )NÚmulaw)r;   Úchannelsz,apad=whole_len=)rC   ÚoptionrP   )rF   )ÚshaperC   r>   ra   r#   r[   r<   r?   r9   r   r7   rd   ÚStreamReaderrJ   )r   Úwaveformr;   Úoutput_sample_rater@   Ú
num_framesrE   r=   r>   ri   r:   Ú	output_srrF   Úreaders                 r   Ú_get_readerzAudioEffector._get_reader  s#  € Ø#+§>¡>Ñ ˆ
�Là�;‰;Ð"Ø—K‘KˆEØ—l‘lˆGØˆFà�{‰{˜gÒ%Ø,7¨=È,ÈÑY‘ô ˜xŸ~™~Ó.ˆEØˆGØ(3 }ÀlÀ^ÑUˆFàÐ#Ü˜( K°·±¸eÀWÈd×N_ÑN_Ó`‰Cä(Ø˜+ t§{¡{°E¸7ÀD×DUÑDUÐWgóˆCð $6Ð#=‘KÐCUˆ	Ü'¨	´?À8Ç>Á>Ó3RÐT`ÓaˆØ�<Š<Ø(˜MÐ)9¸*¸ÐFˆKä˜c¨%¸Ô?ˆØ×ÑÐ 0Ò 6°B¸ÈÐÔTØˆr   rl   r;   rm   Úreturnc                 óú   — |j                   dk7  rt        d|j                   › �«      ‚|j                  «       dk(  r|S | j                  |||«      }|j	                  «        |j                  «       \  }t        |«      S )a´  Apply the effect and/or codecs to the whole tensor.

        Args:
            waveform (Tensor): The input waveform. Shape: ``(time, channel)``
            sample_rate (int): Sample rate of the input waveform.
            output_sample_rate (int or None, optional): Output sample rate.
                If provided, override the output sample rate.
                Otherwise, the resulting tensor is resampled to have
                the same sample rate as the input.
                Default: ``None``.

        Returns:
            Tensor:
                Resulting Tensor. Shape: ``(time, channel)``. The number of frames
                could be different from that of the input.
        é   ú-Expected the input waveform to be 2D. Found: r   )Úndimr3   Únumelrq   Úprocess_all_packetsÚ
pop_chunksr   )r   rl   r;   rm   rp   Úapplieds         r   ÚapplyzAudioEffector.apply"  sv   € ð" �=‰=˜AÒÜÐLÈXÏ]É]ÈOÐ\Ó]Ð]à�>‰>Ó˜qÒ ØˆOà×!Ñ! (¨KÐ9KÓLˆØ×"Ñ"Ô$Ø×&Ñ&Ó(‰
ˆÜ�g‹Ðr   r@   c              #   óô   K  — |j                   dk7  rt        d|j                   › �«      ‚|j                  «       dk(  r|S | j                  ||||«      }|j	                  «       D ]  \  }t        |«      –— Œ y­w)aZ  Apply the effect and/or codecs to the given tensor chunk by chunk.

        Args:
            waveform (Tensor): The input waveform. Shape: ``(time, channel)``
            sample_rate (int): Sample rate of the waveform.
            frames_per_chunk (int): The number of frames to return at a time.
            output_sample_rate (int or None, optional): Output sample rate.
                If provided, override the output sample rate.
                Otherwise, the resulting tensor is resampled to have
                the same sample rate as the input.
                Default: ``None``.

        Returns:
            Iterator[Tensor]:
                Series of processed chunks. Shape: ``(time, channel)``, where the
                the number of frames matches ``frames_per_chunk`` except the
                last chunk, which could be shorter.
        rt   ru   r   N)rv   r3   rw   rq   Ústreamr   )r   rl   r;   r@   rm   rp   rz   s          r   r}   zAudioEffector.stream>  sx   è ø€ ð* �=‰=˜AÒÜÐLÈXÏ]É]ÈOÐ\Ó]Ð]à�>‰>Ó˜qÒ ØˆOà×!Ñ! (¨KÐ9KÐM]Ó^ˆØ Ÿ-™-›/ò 	"‰JˆWÜ˜“/Ó!ñ	"ùs   ‚A6A8)NNr   )r   r   r   r    r   rV   r	   Úboolr   rq   r   rU   r{   r   r}   r"   r   r   rc   rc      sÓ   „ ñnðd !%Ø $ðð
 "&Ø.2Øòà˜‘ðð ˜‘ðð
 ˜#‘ðð ˜{Ñ+ðð óó$ñ@˜fð °3ð ÈHÐUXÉMð Ðekó ð: nrñ"Øð"Ø-0ð"ØDGð"Ø]eÐfiÑ]jð"à	�&Ñ	ô"r   rc   )rX   Útypingr   r   r   r-   r   Ú!torio.io._streaming_media_decoderr   r   rk   Ú!torio.io._streaming_media_encoderr	   r
   rH   r   r#   r7   r9   rU   rV   r[   ra   rc   r"   r   r   ú<module>r‚      s¡   ðÛ 	ß +Ñ +ã Ý ç fß `÷ñ ð,
˜5Ÿ;™;ó 
÷("ñ ("ðVØ	ðàðð ðð ð	ð
 �c‰]ðð ˜;Ñ'óð0�e—k‘kó ÷\"ò \"r   