
    Ph/                         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    bC:\Users\daisl\Desktop\realtime-object-detection\venv\Lib\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     LL"1v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&&ll1obq!,,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%@XY^YcYcYeXf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=$$!#"399-% 	% 	
 	#
 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    ++%%$++*:KK))!TXXdkkDKKRVRZRZDZ-[\KK488#K{{dhhmmA..!!#!!#  ++%%$++*: {{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     ZZ\F&/F
XXa[syy)!   
  C( 

KK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%@XY^YcYcYeXf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;;"KKEllGF{{g%,7=,Y x~~.EG(3}l^UF#(KeWdN_N_`C(+t{{E7DDUDUWgC $6#=KCU	'	?8>>3RT`a<<(M)9*FKc%? 0 6B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LX]]O\]]>>q O!!(K9K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>  ss     * ==ALX]]O\]]>>q O!!(K9KM]^ --/JW/! *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 HUXM ek : nr""-0"DG"]efi]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ekk \" \"r   