
    v;3h,                     &   d dl Z d dlZd dlZddlmZmZmZmZmZm	Z	 ddl
mZ ddlmZmZ e j                  dk\  reZed        Zedd       Zedd	       Zedd
       Zedd       Zedd       Zed        Zed        Zed        Zedd       Zy)    N   )db_to_floatratio_to_dbregister_pydub_effectmake_chunksaudioopget_min_max_value)split_on_silence)TooManyMissingFramesInvalidDuration)   r   c                 2   | j                   }| j                         }|D cg c]
  } ||       }}| j                         }t        |      D ]3  \  }}t        |j                               D ]  \  }}||z  |z   }	|||	<    5 | j	                  |      S c c}w N)channelssplit_to_monoget_array_of_samples	enumerate_spawn)
seg	filter_fn
n_channelschannel_segschannel_segout_data	channel_isample_isampleindexs
             M/var/www/html/audio-gradio/venv/lib/python3.12/site-packages/pydub/effects.py!apply_mono_filter_to_each_channelr       s    J$$&L>JK{Ik*KLK'')H"+L"9 %	; )+*J*J*L M 	%Hf
*i7E$HUO	%%
 ::h Ls   Bc                     | j                   }|dk(  r| S | j                  t        |       z  }t        ||z        }| j	                  |      S )za
    headroom is how close to the maximum volume to boost the signal up to (specified in dB)
    r   )maxmax_possible_amplituder   r   
apply_gain)r   headroompeak_sample_valtarget_peakneeded_boosts        r   	normalizer)   #   sQ    
 ggO !
,,{H9/EEK{_<=L>>,''    c                    d|z  }|dk  rt        |d|z
  z  |z        }nt        |      }t        ||z  d|z
  z        }t        ||dz
        }t        | ||z         }t        |      dk  r&t	        dj                  ||| j                              ||z  }|d   }|d d D cg c]  }|d |  
 }}|d   }	|dd  D ]  }|	j                  ||      }	 |	|z  }	|	S c c}w )	N      ?       @r      zwCould not speed up AudioSegment, it was too short {2:0.2f}s for the current settings:
{0}ms chunks at {1:0.1f}x speedupr   	crossfade)intminr   len	Exceptionformatduration_secondsappend)
r   playback_speed
chunk_sizer1   atkms_to_remove_per_chunkchunks
last_chunkchunkouts
             r   speeduprA   4   s;    
C!$Z1s7%;c%A!B "%Zz)QW56
 I59:I
 j+AABF
6{Q  S  Z  Z(<(<> ? 	?
 i' J;A#2;G%e,,,-GFG
)C 5jj)j45 :CJ Hs   "Cc                     ||kD  rt        d      t        | |||      }|dz  }t        |      s| dd S |d   } |dd  D ]  }| j                  ||      }  | S )Nz)padding cannot be longer than silence_lenr.   r   r   r0   )r   r
   r4   r8   )r   silence_lensilence_threshpaddingr=   r1   r?   s          r   strip_silencerF   a   sx    IJJc;HF!Iv;1Qx
)C 5jj)j45 Jr*   c                      j                   t        |      z  t         j                  |             fd}fd}g }d} j                  |      }	 j                  |      }
t	        t         j                                     D ]  } ||      }dd|z  z
   ||      z  }||	z  }||
z  }|kD  r||k  r||z  }t        ||      }n||z  }t        |d      } j                  |      }|dk7  r+t        j                  | j                  t        |             }|j                  |         j                  dj                  |      	      S )
aL  
    Keyword Arguments:
        
        threshold - default: -20.0
            Threshold in dBFS. default of -20.0 means -20dB relative to the
            maximum possible volume. 0dBFS is the maximum possible value so
            all values for this argument sould be negative.

        ratio - default: 4.0
            Compression ratio. Audio louder than the threshold will be 
            reduced to 1/ratio the volume. A ratio of 4.0 is equivalent to
            a setting of 4:1 in a pro-audio compressor like the Waves C1.
        
        attack - default: 5.0
            Attack in milliseconds. How long it should take for the compressor
            to kick in once the audio has exceeded the threshold.

        release - default: 50.0
            Release in milliseconds. How long it should take for the compressor
            to stop compressing after the audio has falled below the threshold.

    
    For an overview of Dynamic Range Compression, and more detailed explanation
    of the related terminology, see: 

        http://en.wikipedia.org/wiki/Dynamic_range_compression
    )msc                 B    j                  | z
  |       j                  S r   )get_sample_slicerms)frame_ilook_framesr   s    r   rms_atz&compress_dynamic_range.<locals>.rms_at   s!    ##Gk$97CGGGr*   c                 D    | dk(  ryt        | z        }t        |d      S )Nr           )r   r"   )rK   db
thresh_rmss     r   db_over_thresholdz1compress_dynamic_range.<locals>.db_over_threshold   s'    !8Cz)*2qzr*   rP   r   r,   r   r*   data)r#   r   r2   frame_countxranger3   r"   	get_framer   mulsample_widthr8   r   join)r   	thresholdratioattackreleaserN   rS   outputattenuationattack_framesrelease_framesirms_nowmax_attenuationattenuation_incattenuation_decframerM   rR   s   `                @@r   compress_dynamic_rangerj   s   s^   < ++k).DDJcooo01KH
 F KOOvO.M___0NC)*+ ) e,0A'0JJ)M9)N:ZK?$B?*Kk?;K?*Kk1-Ka #KK # 0 0 +[L 9;E 	e/2 ::388F+:,,r*   c                    |dk(  r=t        j                  | j                  | j                  d      }| j	                  |      S | j
                  dk(  r| j                         \  }}n$t        dt        | j
                        z   dz         |dk(  r|j                         }n|j                         }| j                  ||      S )z
    channels- specifies which channel (left or right) to reverse the phase of.
    Note that mono AudioSegments will become stereo.
    r   r         rT   r.   z.Can't implicitly convert an AudioSegment with z channels to stereo.)r   r   )r   rY   _datarZ   r   r   r   r5   strinvert_phasefrom_mono_audiosegments)r   r   invertedleftrights        r   rp   rp      s     6;;syy#*:*:DAzzxz(( <<1++-KD%LsSVS_S_O``cyyzzv$$&D&&(E**477r*   c                 P   d|dz  t         j                  z  z  }d| j                  z  }|||z   z  }| j                         }t	        j                  | j
                  |      }t        | j                               }dg| j                  z  }t        | j                        D ]  }	||	   x||	<   ||	<    t        d|      D ]V  }	t        | j                        D ]<  }
|	| j                  z  |
z   }||
   |||   ||
   z
  z  z   ||
<   t        ||
         ||<   > X | j                  |      S )z
        cutoff - Frequency (in Hz) where higher frequency signal will begin to
            be reduced by 6dB per octave (doubling in frequency) above this point
    r,   r.   r   r   rT   )mathpi
frame_rater   array
array_typer2   rV   r   ranger   )r   cutoffRCdtalphaoriginalfilteredArrayrV   last_valrd   joffsets               r   low_pass_filterr      s6    

TWW$	%B	s~~	B"r'NE'')HKK9Mcoo'(KsS\\!H3<<  5)1!4mA&5 1k" 5s||$ 	5A#,,&!+F"1+(62BXa[2P)QRHQK$'$4M&!	55 ::=:))r*   c           
         d|dz  t         j                  z  z  }d| j                  z  }|||z   z  }t        | j                  dz        \  }}| j                         }t        j                  | j                  |      }t        | j                               }	dg| j                  z  }
t        | j                        D ]  }||   x|
|<   ||<    t        d|	      D ]  }t        | j                        D ]e  }|| j                  z  |z   }|dz
  | j                  z  |z   }||
|   ||   z   ||   z
  z  |
|<   t        t        t        |
|   |      |            ||<   g  | j                  |      S )z
        cutoff - Frequency (in Hz) where lower frequency signal will begin to
            be reduced by 6dB per octave (doubling in frequency) below this point
    r,   r.      r   r   rT   )rv   rw   rx   r	   rZ   r   ry   rz   r2   rV   r   r{   r3   r"   r   )r   r|   r}   r~   r   minvalmaxvalr   r   rV   r   rd   r   r   offset_minus_1s                  r   high_pass_filterr      sy    

TWW$	%B	s~~	B"r'NE&s'7'7!';<NFF'')HKK9Mcoo'(KsS\\!H3<<  5)1!4mA&5 1k" Os||$ 	OA#,,&!+F scll2a7N8A;&1A#AH^D\#\]HQK$'CV,Df(M$NM&!	OO ::=:))r*   c                    d|cxk  rdk  st        d       t        d      t        d      }t        |      |z  }t        |      }t        |      |z
  }t        |      }|dz  }|dk  r| j	                  ||      S | j	                  ||      S )as  
    pan_amount should be between -1.0 (100% left) and +1.0 (100% right)
    
    When pan_amount == 0.0 the left/right balance is not changed.
    
    Panning does not alter the *perceived* loundness, but since loudness
    is decreasing on one side, the other side needs to get louder to
    compensate. When panned hard left, the left channel will be 3dB louder.
    rm   r,   zCpan_amount should be between -1.0 (100% left) and +1.0 (100% right)r-   r   )
ValueErrorr   absr   apply_gain_stereo)r   
pan_amountmax_boost_dbboost_dbboost_factorreduce_factor	reduce_dbs          r   panr     s     :$$^__ %^__s#L:-Hx(L-<MM*I #~HA~$$Xy99$$Y99r*   c                 V   | j                   dk(  r| x}}n"| j                   dk(  r| j                         \  }}t        |      }t        |      }t        j                  j
                  |j                  |      }t        j                  ||j                  dd      }t        j                  j
                  |j                  |      }t        j                  ||j                  dd      }t        j                  ||| j                        }	| j                  |	dd| j                  z  d      S )z
    left_gain - amount of gain to apply to the left channel (in dB)
    right_gain - amount of gain to apply to the right channel (in dB)
    
    note: mono audio segments will be converted to stereo
    r   r.   r   )r   frame_width)rU   	overrides)
r   r   r   r   rY   rn   rZ   tostereoaddr   )
r   	left_gain
right_gainrs   rt   l_mult_factorr_mult_factor	left_data
right_datar`   s
             r   r   r   ;  s    ||qu		'')e	*M
+MDJJ(9(9=II  D,=,=q!DIU[[%*<*<mLJ!!*e.@.@!QGJ[[J0@0@AF::6'(*+c.>.>*>@  A Ar*   )g?)g      ?      )i  id   )g      4g      @g      @g      I@)rl   )rP   rP   )sysrv   ry   utilsr   r   r   r   r   r	   silencer
   
exceptionsr   r   version_infor{   rW   r    r)   rA   rF   rj   rp   r   r   r   r    r*   r   <module>r      s   
    & =vF     ( (  ) )X  " G- G-X 8 86 * *8 * *@ : :> A Ar*   