
    v;3h9                     @    d Z ddlZddlmZ d	dZd	dZ	 	 d
dZddZy)zE
Various functions for finding/manipulating silence in AudioSegments
    N   )db_to_floatc                    t        |       }||k  rg S t        |      | j                  z  }g }||z
  }t        d|dz   |      }||z  rt	        j
                  ||g      }|D ]+  }| |||z    }	|	j                  |k  s|j                  |       - |sg S g }
|j                  d      }|}|D ]0  }|||z   k(  }|||z   kD  }|s|r|
j                  |||z   g       |}|}2 |
j                  |||z   g       |
S )ay  
    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                   M/var/www/html/audio-gradio/venv/lib/python3.12/site-packages/pydub/silence.pydetect_silencer   	   s`    - G  	 !0=3W3WWN N 0,q0)<L )# |6F5GH %#Aa/&9:??n,!!!$% 	 M"F ) !%));;

 *Vo-EFo  "5"(?":"< ="1 ! - ?24 5     c                 "   t        | |||      }t        |       }|sd|ggS |d   d   dk(  r|d   d   |k(  rg S d}g }|D ]  \  }}	|j                  ||g       |	} 	|k7  r|j                  ||g       |d   ddgk(  r|j                  d       |S )ay  
    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     #=/>S\]M- G G~ QaM!$4Q$77$B	J' W 56
 W 56q!f$Qr    c                 ~   d }t        |t              r|rt        |       nd}t        | |||      D cg c]  \  }}||z
  ||z   g }}} ||      D ](  \  }	}
|	d   }|
d   }||k  s||z   dz  |	d<   |	d   |
d<   * |D cg c]'  \  }}| t	        |d      t        |t        |              ) c}}S c c}}w c c}}w )am  
    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                 b    t        j                  |       \  }}t        |d       t        ||      S )z$s -> (s0,s1), (s1,s2), (s2, s3), ...N)r	   teenextzip)iterableabs      r   pairwisez"split_on_silence.<locals>.pairwise   s)    }}X&1Q1ay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
  PYZU3 ,
l 24M  &m4 %1:a[
 ":-1GAJ!!*HQK% 'E# 	s5|c#c-.@&AC s   B3,B9c                     d}|dkD  sJ | |||z    j                   |k  r7|t        |       k  r)||z  }| |||z    j                   |k  r|t        |       k  r)t        |t        |             S )a  
    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   r5   )soundsilence_threshold
chunk_sizetrim_mss       r   detect_leading_silencerE      s     G>>

*
+
0
03D
DSVW\S]I]: 
*
+
0
03D
DSVW\S]I] wE
##r    )  r   )rF   rG   d   r   )g      I
   )__doc__r	   utilsr   r   r'   r>   rE    r    r   <module>rM      s4     @F!H \_ 3l$r    