
    h^.                        S SK r S SKJrJrJr  S SKrS SKJr  S SKJrJ	r
  S SKJrJr   " S S5      rS\R                   4S	 jr " S
 S5      rS\S\S\S\S\\   S\\   4S jrS\R                   4S jr " S S5      rg)    N)IteratorListOptional)Tensor)_get_afilter_descStreamingMediaDecoder)CodecConfigStreamingMediaEncoderc                   2    \ rS rSrSrS rS\4S jrS rSr	g)	_StreamingIOBuffer   z6Streaming Bytes IO buffer. Data are dropped when read.c                     / U l         g N_buffer)selfs    O/var/www/auris/envauris/lib/python3.13/site-packages/torchaudio/io/_effector.py__init___StreamingIOBuffer.__init__   s	    $&    bc                 \    U(       a  U R                   R                  U5        [        U5      $ r   )r   appendlen)r   r   s     r   write_StreamingIOBuffer.write   s     LL"1vr   c                     U R                   (       d  g[        U R                   S   5      U::  a  U R                   R                  S5      $ U R                   S   SU nU R                   S   US U R                   S'   U$ )zMPop the oldest byte string. It does not necessary return the requested amountr   r   N)r   r   pop)r   nrets      r   r   _StreamingIOBuffer.pop   sk    ||t||A1$<<##A&&ll1obq!,,q/!"-Q
r   r   N)
__name__
__module____qualname____firstlineno____doc__r   bytesr   r   __static_attributes__ r   r   r   r      s    @'u 
r   r   dtypec           
          [         R                  S[         R                  S[         R                  S[         R                  S[         R
                  S0nX;  a  [        SU  SUR                  5        35      eX   $ )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_fmtr>   !   sk    TUUuuE 9%@XY^YcYcYeXfghh<r   c                   P    \ rS rSrSrS\S\S\S\S\\   S\\	   S	\4S
 jr
S rSrg)_AudioStreamingEncoder.   z3Given a waveform, encode on-demand and return bytessrcsample_rateeffectmuxerencodercodec_configframes_per_chunkc           	      .   Xl         [        5       U l        [        U R                  US9U l        U R                  R                  UR                  S5      U[        UR                  5      UUUS9  U R                  R                  5         Xpl
        SU l        g N)format   )num_channelsrC   rK   rF   filter_descrG   r   )rB   r   bufferStreamWriterwriteradd_audio_streamsizer>   r*   openfpci_iter)r   rB   rC   rD   rE   rF   rG   rH   s           r   r   _AudioStreamingEncoder.__init__1   s     (*"4;;u=$$!#"399-% 	% 	
 	#
 r   c                    U R                   R                  (       Gd
  U R                  S:  a  U R                  R	                  SU R
                  U R                  U R                  U R                  -    5        U =R                  U R                  -  sl        U R                  U R
                  R                  S5      :  a;  U R                  R                  5         U R                  R                  5         SU l        U R                   R                  (       d  U R                  S:  a  M  U R                   R                  U5      $ )Nr   )rO   r   rV   rQ   write_audio_chunkrB   rU   rS   flushcloser   )r   r   s     r   read_AudioStreamingEncoder.readN   s    ++%%%$++*:KK))!TXXdkkDKKRVRZRZDZ-[\KK488#K{{dhhmmA..!!#!!#  ++%%$++*: {{q!!r   )rO   rU   rV   rB   rQ   N)r"   r#   r$   r%   r&   r   intstrr   r	   r   r]   r(   r)   r   r   r@   r@   .   s\    =  	
  # {+ :"r   r@   rB   rC   rD   rE   rF   rG   c           	      H   [         R                  " 5       n[        XcS9nUR                  U R	                  S5      U[        U R                  5      UUUS9  UR                  5          UR                  SU 5        S S S 5        UR                  S5        U$ ! , (       d  f       N!= frJ   )
ioBytesIOrP   rR   rS   r>   r*   rT   rZ   seek)rB   rC   rD   rE   rF   rG   rO   rQ   s           r   _encodere   Y   s     ZZ\F&/F
XXa[syy)!   
  C( 

KKNM 
s   %B
B!c           
          [         R                  S[         R                  S[         R                  S[         R                  S[         R
                  S0nX;  a  [        SU  SUR                  5        35      eX   $ )Nr,   s16les32lef32lef64ler1   r2   r3   r<   s     r   
_get_muxerrk   q   sm     	TWWwwE 9%@XY^YcYcYeXfghh<r   c                       \ rS rSrSr  SSSSS.S\\   S\\   S\\   S	\\   S
\4
S jjjr	SS jr
SS\S\S\\   S\4S jjr SS\S\S\S\\   S\\   4
S jjrSrg)AudioEffector   a  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)rF   rG   pad_endrD   rK   rF   rG   ro   c                h    Uc  Uc  Ub  [        S5      eXl        X l        X0l        X@l        XPl        g )NzM`encoder` and/or `condec_config` opions are provided without `format` option.)r:   rD   rK   rF   rG   ro   )r   rD   rK   rF   rG   ro   s         r   r   AudioEffector.__init__   s:     >"l&> !pqq(r   c           	      4   UR                   u  pVU R                  b2  U R                  nU R                  n0 n	U R                  S:X  a  U U S.n	O[        UR                  5      nS nU U S.n	Uc#  [        XU R                  XxU R                  5      n
O#[        XU R                  XxU R                  U5      n
Uc  UOUn[        U[        UR                  5      U5      nU R                  (       a  U SU 3n[        XU	S9nUR                  U=(       d    SSUS9  U$ )Nmulaw)rC   channelsz,apad=whole_len=)rK   optionrY   )rN   )shaperK   rF   rk   r*   re   rD   rG   r@   r   r>   ro   StreamReaderrR   )r   waveformrC   output_sample_raterH   
num_framesrM   rE   rF   ru   rB   	output_srrN   readers                 r   _get_readerAudioEffector._get_reader  s   #+>> 
;;"KKEllGF{{g%,7=,Y x~~.EG(3}l^UF#(edN_N_`C(t{{EDDUDUWgC $6#=KCU	'	?8>>3RT`a<<(M)9*FKc? 0 6BTr   rx   rC   ry   returnc                     UR                   S:w  a  [        SUR                    35      eUR                  5       S:X  a  U$ U R                  XU5      nUR	                  5         UR                  5       u  n[        U5      $ )aL  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   )ndimr:   numelr}   process_all_packets
pop_chunksr   )r   rx   rC   ry   r|   applieds         r   applyAudioEffector.apply"  st    " ==ALX]]O\]]>>q O!!(9KL""$&&(
gr   rH   c              #      #    UR                   S:w  a  [        SUR                    35      eUR                  5       S:X  a  U$ U R                  XXC5      nUR	                  5        H  u  n[        U5      v   M     g7f)a  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.
r   r   r   N)r   r:   r   r}   streamr   )r   rx   rC   rH   ry   r|   r   s          r   r   AudioEffector.stream>  sm     * ==ALX]]O\]]>>q O!!(9K^ --/JW/! *s   A8A:)rG   rD   rF   rK   ro   )NNr   )r"   r#   r$   r%   r&   r   r`   r	   boolr   r}   r   r_   r   r   r   r(   r)   r   r   rm   rm      s    nd !% $
 "&.2 
 # {+ $@f 3 HUXM ek : nr""-0"DG"]efi]j"	&	" "r   rm   )rb   typingr   r   r   r4   r   !torio.io._streaming_media_decoderr   r   rw   !torio.io._streaming_media_encoderr	   r
   rP   r   r*   r>   r@   r_   r`   re   rk   rm   r)   r   r   <module>r      s    	 + +   f ` ,
5;; 
(" ("V	  	
 c] ;'0ekk \" \"r   