o
    soi(                     @   s<   d dl Z d dlZddlmZ ddlmZ G dd deZdS )    N   )util)	IIRfilterc                   @   sL   e Zd ZdZdddZdd Zd	d
 Zdd Zedd Z	e	j
dd Z	dS )Metera   Meter object which defines how the meter operates

    Defaults to the algorithm defined in ITU-R BS.1770-4.

    Parameters
    ----------
    rate : float
        Sampling rate in Hz.
    filter_class : str
        Class of weighting filter used.
        - 'K-weighting'
        - 'Fenton/Lee 1'
        - 'Fenton/Lee 2'
        - 'Dash et al.'
        - 'DeMan'
    block_size : float
        Gating block size in seconds.
    K-weighting皙?      ?c                 C   s"   || _ || _|| _|| _g | _d S N)ratefilter_class
block_sizeoverlapblockwise_loudness)selfr
   r   r   r    r   D/home/ubuntu/.local/lib/python3.10/site-packages/pyloudnorm/meter.py__init__   s
   
zMeter.__init__c              
      s,  |  }t|| j| j |jdkrt||jd df}|jd |jd }| j	
 D ]\}}tD ]}||dd|f |dd|f< q4q,g d | j}d| j}d| }	|| j }
tt|
| ||	  d }td|}tj|fdtD ]8}|D ]3}t|||	  | j }t|||	 d  | j }d|| j  tt||||f  ||f< qqt  tjdtd	  fd
d|D }W d   n1 sw   Y  || _fddt|D t  tjdtd	 fddtD W d   n	1 sw   Y  ddtt fddtD   d fddt|D t ! tjdtd	 ttfddtD W d   n	1 saw   Y  tjdd ddtt fddtD   }W d   |S 1 sw   Y  |S )a   Measure the integrated gated loudness of a signal.

        Uses the weighting filters and block size defined by the meter
        the integrated loudness is measured based upon the gating algorithm
        defined in the ITU-R BS.1770-4 specification.

        Input data must have shape (samples, ch) or (samples,) for mono audio.
        Supports up to 5 channels and follows the channel ordering:
        [Left, Right, Center, Left surround, Right surround]

        Params
        -------
        data : ndarray
            Input multichannel audio data.

        Returns
        -------
        LUFS : float
            Integrated gated loudness of the input measured in dB LUFS.
        r   r   N)      ?r   r   (\?r   g     Qr   )shapeignore)categoryc                    s:   g | ] d dt t  fddtD   qS )&1      $@c                    s    g | ]} | |f  qS r   r   .0i)Gjzr   r   
<listcomp>Y   s     8Meter.integrated_loudness.<locals>.<listcomp>.<listcomp>)nplog10sumranger   )r   numChannelsr   )r   r   r    Y   s   : z-Meter.integrated_loudness.<locals>.<listcomp>c                    s   g | ]
\}}| kr|qS r   r   r   r   l_j)Gamma_ar   r   r    ]       c                    &   g | ] t  fd dD qS )c                       g | ]} |f qS r   r   r   r   r   r   r   r   r    b       r!   r"   meanr&   J_gr   r   r   r    b      & r   r   c                       g | ]
} | |  qS r   r   r   r   z_avg_gatedr   r   r    d   r+   c                    s$   g | ]\}}|kr| kr|qS r   r   r(   )r*   Gamma_rr   r   r    g   s   $ c                    r,   )c                    r-   r   r   r.   r/   r   r   r    k   r0   r!   r1   r&   r3   r5   r   r    k   r6   )dividec                    r7   r   r   r   r8   r   r   r    o   r+   )copyr   valid_audior
   r   ndimr"   reshaper   _filtersitemsr%   apply_filterr   introundarangezerosr$   squarewarningscatch_warningssimplefilterRuntimeWarningr   	enumerater#   
nan_to_numarrayerrstate)r   data
input_data
numSamplesr   filter_stagechT_gr   stepT	numBlocksj_ranger   r   luLUFSr   )r   r*   r:   r4   r'   r   r9   r   integrated_loudness!   s^   


$
4

0
&.
zMeter.integrated_loudnessc              	      s,  | j }| j}zd| _ d| _| j|dd}| | | js tdd dd}d	} fd
d| jD }t|dkrBtjW || _ || _S t|}t	t
dt|d| }dt| fdd|D }	t|	dkrvtjW || _ || _S t|	|}
t|	|}||
 }|W || _ || _S || _ || _w )a   Measure the loudness range of a signal.

        An implementation based on the MATLAB example of TECH 3342 -
        LOUDNESS RANGE: A MEASURE TO SUPPLEMENT EBU R 128 LOUDNESS NORMALIZATION

        Input data must have shape (samples, ch) or (samples,) for mono audio.
        Supports up to 5 channels and follows the channel ordering:
        [Left, Right, Center, Left surround, Right surround]

        Params
        -------
        data : ndarray
            Input multichannel audio data.

        Returns
        -------
        LRA : float
            Loudness Range measure in LU.
            Returns NaN if the signal is too quiet to compute LRA.
        g      @g
ףp=
?g      ?)silence_duration_seczNo blockwise loudness foundii
   _   c                    s   g | ]}| kr|qS r   r   r   x)	ABS_THRESr   r   r       r0   z(Meter.loudness_range.<locals>.<listcomp>r   c                    s   g | ]
}|  kr|qS r   r   ra   )	REL_THRESstl_integratedr   r   r       r+   )r   r   _append_silencer]   r   
ValueErrorlenr"   nanr$   powerr;   r#   
percentile)r   rP   original_block_sizeoriginal_overlapPRC_LOWPRC_HIGHstl_absgated_vecn	stl_powerstl_relgated_vecstl_perc_lowstl_perc_highLRAr   )rc   rd   re   r   loudness_ranges   sD   
	zMeter.loudness_rangec                 C   s|   t || j }t|}t|jdkrt||f}|S t|jdkr:|jd }t||f}tj||fdd}|S td)Nr      r   )axiszInvalid shape for audio data)rC   r
   r"   rF   rh   r   concatenaterg   )r   rP   r^   num_silence_samplessilencenew_audio_datanum_channelsr   r   r   rf      s   

zMeter._append_silencec                 C   s   | j S r	   )_filter_class)r   r   r   r   r      s   zMeter.filter_classc                 C   s  i | _ || _| jdkr*tddtd d| jd| j d< tddd	| jd
| j d
< d S | jdkr_tddtd d| jd| j d< tddd| jd
| j d
< tddtd d| jd| j d< d S | jdkrtddtd d| jd| j d< tddd	| jd
| j d
< d S | jdkrtddd| jd
| j d
< tddd| jd| j d< d S | jdkrtddd| jd| j d< tddd| jd| j d< d S | jd krd S td!| j)"Nr   g      @r   rx   g     p@
high_shelfg        g      ?g      C@	high_passzFenton/Lee 1g      @g     @`@g     @@peakingzFenton/Lee 2	high_selfzDash et al.g      ?g     b@gݡsgrˢE?g     @@DeMangCo"@giwh-?gCxG@high_shelf_DeMangح?g^JWC@high_pass_DeMancustomzInvalid filter class:)r@   r   r   r"   sqrtr
   rg   )r   valuer   r   r   r      s*   
"
"&
"


N)r   r   r   )__name__
__module____qualname____doc__r   r]   rw   rf   propertyr   setterr   r   r   r   r      s    
RD
r   )	rH   numpyr"    r   	iirfilterr   objectr   r   r   r   r   <module>   s
    