o
    7viC                     @   sL  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dr?zddlZejZd	ZW n ey>   ed
ZY nw e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%d& Zd'd'd'd'd(d)d)ddd*d+
Zd,ed-efd.d/ZedPd0ed,ee d1e
fd2d3Zd4d5d6d7d8d9d:Ze		;					dQd0ed<ed=ed>ed?ed,ee d1eejef fd@dAZdBejdCedDefdEdFZdCedDefdGdHZdBejd,edCedDefdIdJZ e						dRd0edKejdLed?edMee! d,ee dCee dDee fdNdOZ"dS )SzCThe 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                 C   s&   | t vrtd|  d t | 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)subtype r/   b/home/ubuntu/sommelier/.venv/lib/python3.10/site-packages/torchaudio/_backend/soundfile_backend.py_get_bit_depthE   s
   
r1   PCM_SPCM_UPCM_Fr%   )
r   r
   r   r   r   r   r   r   r   r%   formatr.   c                 C   s   | dkrdS t |dS )NFLACUNKNOWN)_SUBTYPE_TO_ENCODINGr-   )r5   r.   r/   r/   r0   _get_encoding^   s   r9   filepathreturnc                 C   s2   t | }t|j|j|jt|jt|j	|jd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channelsr1   r.   r9   r5   )r:   r5   sinfor/   r/   r0   r>   d   s   
r>   int8uint8int16int32float32float64)r   r   r
   r   r   r   frame_offset
num_frames	normalizechannels_firstc                 C   s   t | d6}|jdks|rd}n|jtvrtd|j t|j }||d|}|j||dd}	|j}
W d   n1 s?w   Y  t	
|	}	|rO|	 }	|	|
fS )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WAVrG   zUnsupported subtype: NT)	always_2d)r   	SoundFiler5   r.   _SUBTYPE2DTYPE
ValueError_prepare_readreadr?   torch
from_numpyt)r:   rJ   rK   rL   rM   r5   file_dtyper@   waveformsample_rater/   r/   r0   load   s   R


r]   rZ   r=   r<   c              
   C   s  |s.|s#t jdt jdt jdt jdt jdi| }|s!td|  |S |dkr)dS d| S |d	krC|s6dS |dkr>td
d| S |dkrQ|dv rMdS td|dkre|dv r[dS |dkradS td|dkrs|dv rodS td|dkr|dv r}dS tdtd| d)Nr   r
   r   r   r   zUnsupported dtype for wav: r	   PCM_r2   z/wav does not support 8-bit signed PCM encoding.r3   Nr	   z.wav only supports 8-bit unsigned PCM encoding.r4   )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 .)rV   rD   rE   rF   rG   rH   r-   rS   )rZ   r=   r<   r.   r/   r/   r0   _get_subtype_for_wav   sR   

ra   c                 C   sj   | dv r|rd| S dS | dv rt d|  d| dkr'|dv r#dS t d	| d
kr-d
S t d|  d)N)Nr2   r^   r   )r3   r4   zsph does not support z
 encoding.r   r_   z,sph only supports 8-bit for mu-law encoding.r   r`   )rS   )r=   r<   r/   r/   r0   _get_subtype_for_sphere  s   rb   c                 C   s   |dkr
t | ||S |dkr+|rtd|sdS |dkr td|dkr&dS d	| S |d
v rL|r5td|d u s=|dkr?dS |dkrEdS td| |dkrRdS |dkr[t||S |dv radS td| )Nwavflaczflac does not support encoding.r
   r   z+flac does not support bits_per_sample > 24.r	   r   r^   )oggvorbisz,ogg/vorbis does not support bits_per_sample.rf   r%   opusOPUSzUnexpected encoding: mp3MPEG_LAYER_IIIsph)nisnistzUnsupported format: )ra   rS   rb   )rZ   r5   r=   r<   r/   r/   r0   _get_subtype(  s2   
rn   srcr\   compressionc           
      C   s   |j dkrtd|j  d|durtd t| dr)|du r$td| }nt| dd	  }|d
vr<td|dkrEtd t	|j
|||}	|dv rW|du rWd}|r]| }tj| |||	|d dS )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.r`   rI   )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.)rl   rm   rk   NIST)filedatar?   r.   r5   )ndimrS   r+   r,   hasattrRuntimeErrorlowerstrsplitrn   rZ   rX   r   rr   )
r:   ro   r\   rM   rp   r5   r=   r<   extr.   r/   r/   r0   saveD  s.   
f

r}   )N)r   rI   TTN)TNNNN)#__doc__r+   typingr   r   rV   torchaudio._internalr   
_mod_utilscommonr   _IS_SOUNDFILE_AVAILABLEis_module_availabler   no_op_requires_soundfile	Exceptionfail_with_messager*   r1   r8   rz   r9   r>   rR   intboolTensorr]   rZ   ra   rb   rn   floatr}   r/   r/   r/   r0   <module>   s   
	

c+