
    PhE                        d Z ddlZddlmZmZ ddlZddlmZ ddl	m
Z
 dZ ej                  d      r	 ddlZej                  Zd	Zn ej"                  d      Zi ddddddddddddddddddddddddddddddd dd!d"dddddddd#ddd$
Zd% Zd&d&d&d&d'd(d(ddd)d*
Zd+ed,efd-ZedGd.ed+ee   d/e
fd0       Zd1d2d3d4d5d6d7Ze	 	 	 	 	 dHd.ed8ed9ed:ed;ed+ee   d/eej6                  ef   fd<       Zd=ej:                  d>ed?efd@Zd>ed?efdAZd=ej:                  d+ed>ed?efdBZ e	 	 	 	 	 dId.edCej6                  dDed;edEee!   d+ee   d>ee   d?ee   fdF       Z"y# e$ r  ej"                  d
      ZY 'w xY w)JzCThe new soundfile backend which will become default in 0.8.0 onward    N)OptionalTuple)module_utils   )AudioMetaDataF	soundfileTz[requires soundfile, but we failed to import it. Please check the installation of soundfile.zFrequires soundfile, but it is not installed. Please install soundfile.PCM_S8   PCM_16   PCM_24   PCM_32    PCM_U8FLOATDOUBLE@   ULAWALAW	IMA_ADPCMMS_ADPCMGSM610	VOX_ADPCMG721_32G723_24G723_40DWVW_12      )
DWVW_16DWVW_24DWVW_NDPCM_8DPCM_16VORBISALAC_16ALAC_20ALAC_24ALAC_32c                 p    | t         vrt        j                  d|  d       t         j                  | d      S )NzThe z subtype is unknown to TorchAudio. As a result, the bits_per_sample attribute will be set to 0. If you are seeing this warning, please report by opening an issue on github (after checking for existing/closed ones). You may otherwise ignore this warning.r   )_SUBTYPE_TO_BITS_PER_SAMPLEwarningswarnget)subtypes    pC:\Users\daisl\Desktop\realtime-object-detection\venv\Lib\site-packages\torchaudio/_backend/soundfile_backend.py_get_bit_depthr2   E   s=    117) 5 5	
 '**7A66    PCM_SPCM_UPCM_Fr&   )
r	   r   r   r   r   r   r   r   r   r&   formatr0   c                 :    | dk(  ryt         j                  |d      S )NFLACUNKNOWN)_SUBTYPE_TO_ENCODINGr/   )r7   r0   s     r1   _get_encodingr<   ^   s     ##GY77r3   filepathreturnc           
          t        j                  |       }t        |j                  |j                  |j
                  t        |j                        t        |j                  |j                              S )ag  Get signal information of an audio file.

    Note:
        ``filepath`` argument is intentionally annotated as ``str`` only, even though it accepts
        ``pathlib.Path`` object as well. This is for the consistency with ``"sox_io"`` backend,
        which has a restriction on type annotation due to TorchScript compiler compatiblity.

    Args:
        filepath (path-like object or file-like object):
            Source of audio data.
        format (str or None, optional):
            Not used. PySoundFile does not accept format hint.

    Returns:
        AudioMetaData: meta data of the given audio.

    )bits_per_sampleencoding)
r   infor   
samplerateframeschannelsr2   r0   r<   r7   )r=   r7   sinfos      r1   rB   rB   d   sT    & NN8$E&u}}5u||U]]; r3   int8uint8int16int32float32float64)r	   r   r   r   r   r   frame_offset
num_frames	normalizechannels_firstc                    t        j                  | d      5 }|j                  dk7  s|rd}n=|j                  t        vrt        d|j                         t        |j                     }|j                  |d|      }|j                  ||d      }	|j                  }
ddd       t        j                  	      }	|r|	j                         }	|	
fS # 1 sw Y   4xY w)a  Load audio data from file.

    Note:
        The formats this function can handle depend on the soundfile installation.
        This function is tested on the following formats;

        * WAV

            * 32-bit floating-point
            * 32-bit signed integer
            * 16-bit signed integer
            * 8-bit unsigned integer

        * FLAC
        * OGG/VORBIS
        * SPHERE

    By default (``normalize=True``, ``channels_first=True``), this function returns Tensor with
    ``float32`` dtype, and the shape of `[channel, time]`.

    .. warning::

       ``normalize`` argument does not perform volume normalization.
       It only converts the sample type to `torch.float32` from the native sample
       type.

       When the input format is WAV with integer type, such as 32-bit signed integer, 16-bit
       signed integer, 24-bit signed integer, and 8-bit unsigned integer, by providing ``normalize=False``,
       this function can return integer Tensor, where the samples are expressed within the whole range
       of the corresponding dtype, that is, ``int32`` tensor for 32-bit signed PCM,
       ``int16`` for 16-bit signed PCM and ``uint8`` for 8-bit unsigned PCM. Since torch does not
       support ``int24`` dtype, 24-bit signed PCM are converted to ``int32`` tensors.

       ``normalize`` argument has no effect on 32-bit floating-point WAV and other formats, such as
       ``flac`` and ``mp3``.

       For these formats, this function always returns ``float32`` Tensor with values.

    Note:
        ``filepath`` argument is intentionally annotated as ``str`` only, even though it accepts
        ``pathlib.Path`` object as well. This is for the consistency with ``"sox_io"`` backend,
        which has a restriction on type annotation due to TorchScript compiler compatiblity.

    Args:
        filepath (path-like object or file-like object):
            Source of audio data.
        frame_offset (int, optional):
            Number of frames to skip before start reading data.
        num_frames (int, optional):
            Maximum number of frames to read. ``-1`` reads all the remaining samples,
            starting from ``frame_offset``.
            This function may return the less number of frames if there is not enough
            frames in the given file.
        normalize (bool, optional):
            When ``True``, this function converts the native sample type to ``float32``.
            Default: ``True``.

            If input file is integer WAV, giving ``False`` will change the resulting Tensor type to
            integer type.
            This argument has no effect for formats other than integer WAV type.

        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):
            Not used. PySoundFile does not accept format hint.

    Returns:
        (torch.Tensor, int): Resulting Tensor and sample rate.
            If the input file has integer wav format and normalization is off, then it has
            integer type, else ``float32`` type. If ``channels_first=True``, it has
            `[channel, time]` else `[time, channel]`.
    rWAVrK   zUnsupported subtype: NT)	always_2d)r   	SoundFiler7   r0   _SUBTYPE2DTYPE
ValueError_prepare_readreadrC   torch
from_numpyt)r=   rM   rN   rO   rP   r7   file_dtyperD   waveformsample_rates              r1   loadra      s    d 
		Xs	+u<<5 IE]].04U]]ODEE"5==1E$$\4D::fet:<&& 
, )H::<[   
,	+s   BCCr^   rA   r@   c           
         |s|sst         j                  dt         j                  dt         j                  dt         j                  dt         j
                  dij                  |       }|st        d|        |S |dk(  ryd| S |d	k(  r|sy|dk(  rt        d
      d| S |dk(  r|dv ryt        d      |dk(  r|dv ry|dk(  ryt        d      |dk(  r|dv ryt        d      |dk(  r|dv ryt        d      t        d| d      )Nr   r   r   r   r   zUnsupported dtype for wav: r
   PCM_r4   z/wav does not support 8-bit signed PCM encoding.r5   Nr
   z.wav only supports 8-bit unsigned PCM encoding.r6   )Nr   r   z/wav only supports 32/64-bit float PCM encoding.r   z(wav only supports 8-bit mu-law encoding.r   z'wav only supports 8-bit a-law encoding.zwav does not support .)rZ   rH   rI   rJ   rK   rL   r/   rW   )r^   rA   r@   r0   s       r1   _get_subtype_for_wavrf      sL   XXXwx c%j   #>ug!FGGNao&''7aNOOo&''7i'IJJ7j(b JKK6i'CDD6i'BCC
,XJa8
99r3   c                     | dv r	|rd| S dS | dv rt        d|  d      | dk(  r|dv ryt        d	      | d
k(  ry
t        d|  d      )N)Nr4   rc   r   )r5   r6   zsph does not support z
 encoding.r   rd   z,sph only supports 8-bit for mu-law encoding.r   re   )rW   )rA   r@   s     r1   _get_subtype_for_sphererh     s|    ?"+:o&'HH%%0
*EFF6i'GHH6
,XJa8
99r3   c                 6   |dk(  rt        | ||      S |dk(  r,|rt        d      |sy|dkD  rt        d      |dk(  rdS d	| S |d
v r)|rt        d      ||dk(  ry|dk(  ryt        d|       |dk(  ry|dk(  rt        ||      S |dv ryt        d|       )Nwavflaczflac does not support encoding.r   r   z+flac does not support bits_per_sample > 24.r
   r	   rc   )oggvorbisz,ogg/vorbis does not support bits_per_sample.rm   r&   opusOPUSzUnexpected encoding: mp3MPEG_LAYER_IIIsph)nisnistzUnsupported format: )rf   rW   rh   )r^   r7   rA   r@   s       r1   _get_subtyperu   (  s    #E8_EE>??RJKK*a/xMtO;L5MM""KLLx83v0
;<<&xAA 
+F84
55r3   srcr`   compressionc                    |j                   dk7  rt        d|j                    d      |t        j                  d       t	        | d      r|t        d      |j                         }n+t        |       j                  d      d	   j                         }|d
vrt        d      |dk(  rt        j                  d       t        |j                  |||      }	|dv r|d}|r|j                         }t        j                  | |||	|       y)a  Save audio data to file.

    Note:
        The formats this function can handle depend on the soundfile installation.
        This function is tested on the following formats;

        * WAV

            * 32-bit floating-point
            * 32-bit signed integer
            * 16-bit signed integer
            * 8-bit unsigned integer

        * FLAC
        * OGG/VORBIS
        * SPHERE

    Note:
        ``filepath`` argument is intentionally annotated as ``str`` only, even though it accepts
        ``pathlib.Path`` object as well. This is for the consistency with ``"sox_io"`` backend,
        which has a restriction on type annotation due to TorchScript compiler compatiblity.

    Args:
        filepath (str or pathlib.Path): Path to audio file.
        src (torch.Tensor): Audio data to save. must be 2D tensor.
        sample_rate (int): sampling rate
        channels_first (bool, optional): If ``True``, the given tensor is interpreted as `[channel, time]`,
            otherwise `[time, channel]`.
        compression (float of None, optional): Not used.
            It is here only for interface compatibility reson with "sox_io" backend.
        format (str or None, optional): Override the audio format.
            When ``filepath`` argument is path-like object, audio format is
            inferred from file extension. If the file extension is missing or
            different, you can specify the correct format with this argument.

            When ``filepath`` argument is file-like object,
            this argument is required.

            Valid values are ``"wav"``, ``"ogg"``, ``"vorbis"``,
            ``"flac"`` and ``"sph"``.
        encoding (str or None, optional): Changes the encoding for supported formats.
            This argument is effective only for supported formats, sush as
            ``"wav"``, ``""flac"`` and ``"sph"``. Valid values are;

                - ``"PCM_S"`` (signed integer Linear PCM)
                - ``"PCM_U"`` (unsigned integer Linear PCM)
                - ``"PCM_F"`` (floating point PCM)
                - ``"ULAW"`` (mu-law)
                - ``"ALAW"`` (a-law)

        bits_per_sample (int or None, optional): Changes the bit depth for the
            supported formats.
            When ``format`` is one of ``"wav"``, ``"flac"`` or ``"sph"``,
            you can change the bit depth.
            Valid values are ``8``, ``16``, ``24``, ``32`` and ``64``.

    Supported formats/encodings/bit depth/compression are:

    ``"wav"``
        - 32-bit floating-point PCM
        - 32-bit signed integer PCM
        - 24-bit signed integer PCM
        - 16-bit signed integer PCM
        - 8-bit unsigned integer PCM
        - 8-bit mu-law
        - 8-bit a-law

        Note:
            Default encoding/bit depth is determined by the dtype of
            the input Tensor.

    ``"flac"``
        - 8-bit
        - 16-bit (default)
        - 24-bit

    ``"ogg"``, ``"vorbis"``
        - Doesn't accept changing configuration.

    ``"sph"``
        - 8-bit signed integer PCM
        - 16-bit signed integer PCM
        - 24-bit signed integer PCM
        - 32-bit signed integer PCM (default)
        - 8-bit mu-law
        - 8-bit a-law
        - 16-bit a-law
        - 24-bit a-law
        - 32-bit a-law

       zExpected 2D Tensor, got zD.Nzr`save` function of "soundfile" backend does not support "compression" parameter. The argument is silently ignored.writez0`format` is required when saving to file object.re   )Nr
   r   r   r   r   zInvalid bits_per_sample.r   zvSaving audio with 24 bits per sample might warp samples near -1. Using 16 bits per sample might be able to avoid this.)rs   rt   rr   NIST)filedatarC   r0   r7   )ndimrW   r-   r.   hasattrRuntimeErrorlowerstrsplitru   r^   r\   r   rz   )
r=   rv   r`   rP   rw   r7   rA   r@   extr0   s
             r1   saver   D  s   L xx1}3CHH:R@AA0	
 x!>QRRlln(m!!#&r*00277344"D	
 399c8_EG $$eegOOW]cdr3   )N)r   r{   TTN)TNNNN)#__doc__r-   typingr   r   rZ   torchaudio._internalr   
_mod_utilscommonr   _IS_SOUNDFILE_AVAILABLEis_module_availabler   no_op_requires_soundfile	Exceptionfail_with_messager,   r2   r;   r   r<   rB   rV   intboolTensorra   r^   rf   rh   ru   floatr    r3   r1   <module>r      s|   I  "  ; !    ":!!+.
(.."& 7*66Pab b b	
 a R b A A   a  q q  q!" r#$ 7 >7  8# 8 8 3    :    `!`!`! `! 	`!
 `! SM`! 5<<`! `!F(: (:s (:S (:V:c :C :6 6S 6C 6RU 68 
  #' "%)DeDe	De De 	De
 %De SMDe smDe c]De Dec	  
:j::i

s   E& &F F