
    rh9                     H    S r SSKrSSKJr  S	S jrS	S jr  S
S jrSS jrg)zE
Various functions for finding/manipulating silence in AudioSegments
    N   )db_to_floatc                    [        U 5      nXA:  a  / $ [        U5      U R                  -  n/ nXA-
  n[        SUS-   U5      nXc-  (       a  [        R
                  " Xv/5      nU H,  nXX-    n	U	R                  U::  d  M  UR                  U5        M.     U(       d  / $ / n
UR                  S5      nUnU H8  nXU-   :H  nXU-   :  nU(       d  U(       a  U
R                  UX-   /5        UnUnM:     U
R                  UX-   /5        U
$ )a]  
Returns a list of all silent sections [start, end] in milliseconds of audio_segment.
Inverse of detect_nonsilent()

audio_segment - the segment to find silence in
min_silence_len - the minimum length for any silent section
silence_thresh - the upper bound for how quiet is silent in dFBS
seek_step - step size for interating over the segment in ms
r   r   )	lenr   max_possible_amplituderange	itertoolschainrmsappendpop)audio_segmentmin_silence_lensilence_thresh	seek_stepseg_lensilence_startslast_slice_startslice_startsiaudio_slicesilent_rangesprev_icurrent_range_startsilence_start_i
continuoussilence_has_gaps                   G/home/james-whalen/.local/lib/python3.13/site-packages/pydub/silence.pydetect_silencer   	   sA    - G  	 !0=3W3WWN N 0,q0)<L # |5GH#a&9:??n,!!!$  	 M"F )%));;

 *o-EFo  "5"(":"< ="1  * - 24 5     c                 .   [        XX#5      n[        U 5      nU(       d  SU//$ US   S   S:X  a  US   S   U:X  a  / $ Sn/ nU H  u  pUR                  Xh/5        U	nM     W	U:w  a  UR                  Xe/5        US   SS/:X  a  UR                  S5        U$ )a]  
Returns a list of all nonsilent sections [start, end] in milliseconds of audio_segment.
Inverse of detect_silent()

audio_segment - the segment to find silence in
min_silence_len - the minimum length for any silent section
silence_thresh - the upper bound for how quiet is silent in dFBS
seek_step - step size for interating over the segment in ms
r   r   )r   r   r   r   )
r   r   r   r   r   len_seg
prev_end_inonsilent_rangesstart_iend_is
             r   detect_nonsilentr'   L   s     #=>]M- G G~ QaM!$4Q$77$B	J' 56
 (  56q!f$Qr    c                    S n[        U[        5      (       a  U(       a  [        U 5      OSn[        XX$5       VVs/ s H  u  pgXc-
  Xs-   /PM     nnnU" U5       H(  u  pU	S   nU
S   nX:  d  M  X-   S-  U	S'   U	S   U
S'   M*     U VVs/ s H'  u  pgU [	        US5      [        U[        U 5      5       PM)     snn$ s  snnf s  snnf )a-  
Returns list of audio segments from splitting audio_segment on silent sections

audio_segment - original pydub.AudioSegment() object

min_silence_len - (in ms) minimum length of a silence to be used for
    a split. default: 1000ms

silence_thresh - (in dBFS) anything quieter than this will be
    considered silence. default: -16dBFS

keep_silence - (in ms or True/False) leave some silence at the beginning
    and end of the chunks. Keeps the sound from sounding like it
    is abruptly cut off.
    When the length of the silence is less than the keep_silence duration
    it is split evenly between the preceding and following non-silent
    segments.
    If True is specified, all the silence is kept, if False none is kept.
    default: 100ms

seek_step - step size for interating over the segment in ms
c                 `    [         R                  " U 5      u  p[        US5        [        X5      $ )z$s -> (s0,s1), (s1,s2), (s2, s3), ...N)r	   teenextzip)iterableabs      r   pairwise"split_on_silence.<locals>.pairwise   s%    }}X&Q1yr    r   r      )
isinstanceboolr   r'   maxmin)r   r   r   keep_silencer   r0   startendoutput_rangesrange_irange_iilast_end
next_starts                r   split_on_silencer?   p   s    4 ,%%-9s=)q
  Z [ U 
 24Z   &m41:a[
 "-1GAJ!!*HQK 5 '&IE 	s5|c#c-.@&AC& s   B>.Cc                     SnUS:  d   eXX2-    R                   U:  a8  U[        U 5      :  a)  X2-  nXX2-    R                   U:  a  U[        U 5      :  a  M)  [        U[        U 5      5      $ )z
Returns the millisecond/index that the leading silence ends.

audio_segment - the segment to find silence in
silence_threshold - the upper bound for how quiet is silent in dFBS
chunk_size - chunk size for interating over the segment in ms
r   )dBFSr   r6   )soundsilence_threshold
chunk_sizetrim_mss       r   detect_leading_silencerF      s|     G>>
*
+
0
03D
DSVW\S]I] *
+
0
03D
DSVW\S]I] wE
##r    )  r   )rG   rH   d   r   )g      I
   )__doc__r	   utilsr   r   r'   r?   rF    r    r   <module>rN      s4     @F!H \_ 3l$r    