o
    yi                      @   s   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 er#d dl	Z
ndZ
ddgiZ		dd	ed
edededededefddZdS )    N)Tensor)_check_same_shape)_MULTIPROCESSING_AVAILABLE_PESQ_AVAILABLE)$perceptual_evaluation_speech_qualitypesqF   predstargetfsmodekeep_same_devicen_processesreturnc              	   C   sl  t std|dvrtd| |dvrtd| t| | | jdkr@t||  	 |   	 |}t
|}nl| d| jd   	 }|d| jd   	 }	trt|dkrttj||	|||d}t|}n)tj|jd	 d
}t|jd	 D ]}
t||	|
ddf ||
ddf |||
< qt
|}|| jdd }|r|| j}|S )a  Calculates `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 ect. 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.

    .. note:: using 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 specifiying 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 torchmetrics.functional.audio.pesq import perceptual_evaluation_speech_quality
        >>> import torch
        >>> g = torch.manual_seed(1)
        >>> preds = torch.randn(8000)
        >>> target = torch.randn(8000)
        >>> perceptual_evaluation_speech_quality(preds, target, 8000, 'nb')
        tensor(2.2076)
        >>> perceptual_evaluation_speech_quality(preds, target, 16000, 'wb')
        tensor(1.7359)
    zwPESQ metric requires that pesq is installed. Either install as `pip install torchmetrics[audio]` or `pip install pesq`.)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_processorr   )shapeN)r   ModuleNotFoundError
ValueErrorr   ndimpesq_backendr   detachcpunumpytorchtensorreshaper   r   
pesq_batchnparrayemptyrange
from_numpytodevice)r	   r
   r   r   r   r   pesq_val_nppesq_valpreds_np	target_npb r,   V/home/ubuntu/.local/lib/python3.10/site-packages/torchmetrics/functional/audio/pesq.pyr      s2   6

(.
r   )Fr   )r   r    r   r   torchmetrics.utilities.checksr   torchmetrics.utilities.importsr   r   r   r   __doctest_requires__intstrboolr   r,   r,   r,   r-   <module>   s4   

