§
    ’©º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dS )é    )ÚAnyN)ÚTensor)Ú_check_same_shape)Ú_MULTIPROCESSING_AVAILABLEÚ_PESQ_AVAILABLE)Ú$perceptual_evaluation_speech_qualityÚpesqFé   ÚpredsÚtargetÚfsÚmodeÚkeep_same_deviceÚn_processesÚreturnc           	      óð  — 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š|                     || 
                    ¦   «                              ¦   «                              ¦   «         |  
                    ¦   «                              ¦   «                              ¦   «         |¦  «        }	t          j        |	¦  «        }
�n¸|                      d| j        d         ¦  «         
                    ¦   «                              ¦   «                              ¦   «         }|                     d| j        d         ¦  «         
                    ¦   «                              ¦   «                              ¦   «         }t"          r5|dk    r/|                     |||||¬¦  «        }	t          j        |	¦  «        }	nlt          j        |j        d         ¬¦  «        }	t+          |j        d         ¦  «        D ]1}|                     |||dd…f         ||dd…f         |¦  «        |	|<   Œ2t          j        |	 ||	¦  «                                      t
          j        ¦  «        ¦  «        }
|
                     t3          |
¦  «        ¦  «        }
|r|
                     | j        ¦  «        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                 óZ   — t          j        t          | ¦  «        t           j        ¦  «        S )N)ÚnpÚ
issubdtypeÚtypeÚnumber)r   s    úr/root/private/project/pallet_detection_v2/.venv/lib/python3.11/site-packages/torchmetrics/functional/audio/pesq.pyÚ_issubtype_numberz?perceptual_evaluation_speech_quality.<locals>._issubtype_numberY   s   € ÝŒ}�T !™WœW¥b¤iÑ0Ô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 r
   éÿÿÿÿ)Ú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      sÔ  € õp ð 
Ý!ðZñ
ô 
ð 	
ð  ÐÐÐð1�Sð 1¥Tð 1ð 1ð 1ð 1õ œÐ%6Ñ7Ô7Ðà	�ÐÐÝÐZÐVXÐZÐZÑ[Ô[Ð[Ø�<ÐÐÝÐ]ÐW[Ð]Ð]Ñ^Ô^Ð^Ý�e˜VÑ$Ô$Ð$à„z�Q‚€Ø"×'Ò'¨¨F¯MªM©O¬O×,?Ò,?Ñ,AÔ,A×,GÒ,GÑ,IÔ,IÈ5Ï<Ê<É>Ì>×K]ÒK]ÑK_ÔK_×KeÒKeÑKgÔKgÐimÑnÔnˆÝ”< Ñ,Ô,ˆ‰à—=’=  U¤[°¤_Ñ5Ô5×<Ò<Ñ>Ô>×BÒBÑDÔD×JÒJÑLÔLˆØ—N’N 2 u¤{°2¤Ñ7Ô7×>Ò>Ñ@Ô@×DÒDÑFÔF×LÒLÑNÔNˆ	å%ð 	^¨+¸Ò*:Ð*:Ø&×1Ò1°"°iÀÈ4Ð]hÐ1ÑiÔiˆKÝœ( ;Ñ/Ô/ˆKˆKåœ(¨(¬.¸Ô*;Ð=Ñ=Ô=ˆKÝ˜8œ>¨!Ô,Ñ-Ô-ð ^ð ^�Ø!-×!2Ò!2°2°yÀÀAÀAÀAÀ´ÈÐQRÐTUÐTUÐTUÐQUÌÐX\Ñ!]Ô!]�˜A‘�ÝÔ# KÐ0AÐ0AÀ+Ñ0NÔ0NÔ$O×$VÒ$VÕWYÔWaÑ$bÔ$bÑcÔcˆØ×#Ò#¥C¨¡M¤MÑ2Ô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Ð VÐ VÐ VÐ VÐ VÐ VÐ VàAÀFÀ8ÐLÐ ð #Øð^ð ^Øð^àð^ð 	ð^ð ð	^ð
 ð^ð ð^ð ð^ð ^ð ^ð ^ð ^ð ^r   