Ë
    þÍ:j¥  ã                   ó|   — d dl mZ d dlZd dlZd dlmZ d dlmZ d dlm	Z	m
Z
 ddgiZ	 	 dded	ed
ededededefd„Zy)é    )ÚAnyN)ÚTensor)Ú_check_same_shape)Ú_MULTIPROCESSING_AVAILABLEÚ_PESQ_AVAILABLE)Ú$perceptual_evaluation_speech_qualityÚpesqÚpredsÚtargetÚfsÚmodeÚkeep_same_deviceÚn_processesÚreturnc           	      ó6  — t         st        d«      ‚ddl}dt        dt        fd„}t        j                  |«      }|dvrt        d|› �«      ‚|d	vrt        d
|› �«      ‚t        | |«       | j                  dk(  r|j                  ||j                  «       j                  «       j                  «       | j                  «       j                  «       j                  «       |«      }	t        j                  |	«      }
�nƒ| j                  d| j                   d   «      j                  «       j                  «       j                  «       }|j                  d| j                   d   «      j                  «       j                  «       j                  «       }t"        r1|dk7  r,|j%                  |||||¬«      }	t        j&                  |	«      }	net        j(                  |j                   d   ¬«      }	t+        |j                   d   «      D ]'  }|j                  |||dd…f   ||dd…f   |«      |	|<   Œ) t        j,                  |	 ||	«         j/                  t
        j0                  «      «      }
|
j                  t3        |
«      «      }
|r|
j5                  | j6                  «      S |
S )aº  Calculate `Perceptual Evaluation of Speech Quality`_ (PESQ).

    It's a recognized industry standard for audio quality that takes into considerations characteristics such as: audio
    sharpness, call volume, background noise, clipping, audio interference etc. PESQ returns a score between -0.5 and
    4.5 with the higher scores indicating a better quality.

    This metric is a wrapper for the `pesq package`_. Note that input will be moved to `cpu` to perform the metric
    calculation.

    .. hint::
        Usingsing this metrics requires you to have ``pesq`` install. Either install as ``pip install
        torchmetrics[audio]`` or ``pip install pesq``. Note that ``pesq`` will compile with your currently
        installed version of numpy, meaning that if you upgrade numpy at some point in the future you will
        most likely have to reinstall ``pesq``.

    Args:
        preds: float tensor with shape ``(...,time)``
        target: float tensor with shape ``(...,time)``
        fs: sampling frequency, should be 16000 or 8000 (Hz)
        mode: ``'wb'`` (wide-band) or ``'nb'`` (narrow-band)
        keep_same_device: whether to move the pesq value to the device of preds
        n_processes: integer specifying the number of processes to run in parallel for the metric calculation.
            Only applies to batches of data and if ``multiprocessing`` package is installed.

    Returns:
        Float tensor with shape ``(...,)`` of PESQ values per sample

    Raises:
        ModuleNotFoundError:
            If ``pesq`` package is not installed
        ValueError:
            If ``fs`` is not either  ``8000`` or ``16000``
        ValueError:
            If ``mode`` is not either ``"wb"`` or ``"nb"``
        RuntimeError:
            If ``preds`` and ``target`` do not have the same shape

    Example:
        >>> from torch import randn
        >>> from torchmetrics.functional.audio.pesq import perceptual_evaluation_speech_quality
        >>> preds = randn(8000)
        >>> target = randn(8000)
        >>> perceptual_evaluation_speech_quality(preds, target, 8000, 'nb')
        tensor(2.2885)
        >>> perceptual_evaluation_speech_quality(preds, target, 16000, 'wb')
        tensor(1.6805)

    zwPESQ metric requires that pesq is installed. Either install as `pip install torchmetrics[audio]` or `pip install pesq`.r   NÚxr   c                 ó\   — t        j                  t        | «      t         j                  «      S )N)ÚnpÚ
issubdtypeÚtypeÚnumber)r   s    úw/home/mcse/projects/srt_converter/srt-converter-venv/lib/python3.12/site-packages/torchmetrics/functional/audio/pesq.pyÚ_issubtype_numberz?perceptual_evaluation_speech_quality.<locals>._issubtype_numberY   s   € Ü�}‰}œT !›W¤b§i¡iÓ0Ð0ó    )i@  i€>  z:Expected argument `fs` to either be 8000 or 16000 but got )ÚwbÚnbz;Expected argument `mode` to either be 'wb' or 'nb' but got é   éÿÿÿÿ)Ún_processor)Úshape)r   ÚModuleNotFoundErrorr	   r   Úboolr   Ú	vectorizeÚ
ValueErrorr   ÚndimÚdetachÚcpuÚnumpyÚtorchÚtensorÚreshaper    r   Ú
pesq_batchÚarrayÚemptyÚrangeÚ
from_numpyÚastypeÚfloat32ÚlenÚtoÚdevice)r
   r   r   r   r   r   Úpesq_backendr   Ú_filter_error_msgÚpesq_val_npÚpesq_valÚpreds_npÚ	target_npÚbs                 r   r   r      sC  € õp Ü!ðZó
ð 	
ó  ð1œSð 1¤Tó 1ô Ÿ™Ð%6Ó7Ðà	�ÑÜÐUÐVXÐUYÐZÓ[Ð[Ø�<ÑÜÐVÐW[ÐV\Ð]Ó^Ð^Ü�e˜VÔ$à‡z�z�Q‚Ø"×'Ñ'¨¨F¯M©M«O×,?Ñ,?Ó,A×,GÑ,GÓ,IÈ5Ï<É<Ë>×K]ÑK]ÓK_×KeÑKeÓKgÐimÓnˆÜ—<‘< Ó,Šà—=‘=  U§[¡[°¡_Ó5×<Ñ<Ó>×BÑBÓD×JÑJÓLˆØ—N‘N 2 u§{¡{°2¡Ó7×>Ñ>Ó@×DÑDÓF×LÑLÓNˆ	å%¨+¸Ò*:Ø&×1Ñ1°"°iÀÈ4Ð]hÐ1ÓiˆKÜŸ(™( ;Ó/‰KäŸ(™(¨(¯.©.¸Ñ*;Ô=ˆKÜ˜8Ÿ>™>¨!Ñ,Ó-ò ^�Ø!-×!2Ñ!2°2°yÀÂAÀ±ÈÐQRÒTUÐQUÉÐX\Ó!]�˜A’ð^ä×#Ñ# KÑ0AÀ+Ó0NÑ$O×$VÑ$VÔWY×WaÑWaÓ$bÓcˆØ×#Ñ#¤C¨£MÓ2ˆáØ�{‰{˜5Ÿ<™<Ó(Ð(à€Or   )Fr   )Útypingr   r(   r   r)   r   Útorchmetrics.utilities.checksr   Útorchmetrics.utilities.importsr   r   Ú__doctest_requires__ÚintÚstrr"   r   © r   r   ú<module>rD      s}   ðõ ã Û Ý å ;ß VàAÀFÀ8ÐLÐ ð #Øñ^Øð^àð^ð 	ð^ð ð	^ð
 ð^ð ð^ð ô^r   