+
    ~j\                    L   ^ RI Ht ^ RIt^ RIt^ RIt^ RIHt ^ RIHt ^ RI	H
t
 ^ RIt^ RIt^ RIHtHt ^ RIHt ^ RIHtHt ^ R	IHt ^R
IHt ^RIHt Rt] ! R R4      4       t ! R R]P<                  P>                  4      t ! R R]P<                  P@                  4      t R# )    )annotationsN)	dataclass)Path)Literal)agentsrtc)utils)	NOT_GIVEN
NotGivenOr)is_given)
onnx_modelloggerg?c                  ^    ] tR t^)t$ R]R&   R]R&   R]R&   R]R&   R]R&   R]R&   R]R	&   R
tR# )_VADOptionsfloatmin_speech_durationmin_silence_durationprefix_padding_durationmax_buffered_speechactivation_thresholddeactivation_thresholdintsample_rate N)__name__
__module____qualname____firstlineno____annotations____static_attributes__r       o/Users/mitch_tango/dev/rabbit-r1-livekit/agent/.venv/lib/python3.14/site-packages/livekit/plugins/silero/vad.pyr   r   )   s,    ""!!r"   r   c                     a  ] tR t^4tRt]RRRRRRRR	R
RRRRRR]R]R]/
R R ll4       tR V 3R llt]	R R l4       t
]	R R l4       tR R ltR]R]R]R]R
]R]/R R lltRtV ;t# )VADz
Silero Voice Activity Detection (VAD) class.

This class provides functionality to detect speech segments within audio data using the Silero VAD model.
r   g?r   g?r   g      ?r   g      N@r   r   i>  	force_cpuTonnx_file_pathr   padding_durationc               D    V ^8  d   QhRRRRRRRRRRRRR	R
RRRRRRRR/# )   r   r   r   r   r   r   r   zLiteral[8000, 16000]r&   boolr'   zNotGivenOr[Path | str]r   NotGivenOr[float]r(   returnr%   r   )formats   "r#   __annotate__VAD.__annotate__<   s     Q/ Q/ #Q/ $	Q/
 "'Q/ #Q/ $Q/ *Q/ Q/ /Q/ !2Q/ ,Q/ 
Q/r"   c       
           V\         P                  9  d   \        R4      h\        V
4      '       d   \        P
                  ! R4       T
p\        V	4      '       d   V	^ 8:  d   \        R4      h\         P                  ! Yx;'       g    RR7      p\        TTTTTT	;'       g    \        VR,
          R4      VR7      pV ! WR	7      # )
a  
Load and initialize the Silero VAD model.

This method loads the ONNX model and prepares it for inference. When options are not provided,
sane defaults are used.

**Note:**
    This method is blocking and may take time to load the model into memory.
    It is recommended to call this method inside your prewarm mechanism.

**Example:**

    ```python
    def prewarm(proc: JobProcess):
        proc.userdata["vad"] = silero.VAD.load()


    async def entrypoint(ctx: JobContext):
        vad = (ctx.proc.userdata["vad"],)
        # your agent logic...


    if __name__ == "__main__":
        cli.run_app(WorkerOptions(entrypoint_fnc=entrypoint, prewarm_fnc=prewarm))
    ```

Args:
    min_speech_duration (float): Minimum duration of speech to start a new speech chunk.
    min_silence_duration (float): At the end of each speech, wait this duration before ending the speech.
    prefix_padding_duration (float): Duration of padding to add to the beginning of each speech chunk.
    max_buffered_speech (float): Maximum duration of speech to keep in the buffer (in seconds).
    activation_threshold (float): Threshold to consider a frame as speech.
    sample_rate (Literal[8000, 16000]): Sample rate for the inference (only 8KHz and 16KHz are supported).
    onnx_file_path (Path | str | None): Path to the ONNX model file. If not provided, the default model will be loaded. This can be helpful if you want to use a previous version of the silero model.
    force_cpu (bool): Force the use of CPU for inference.
    deactivation_threshold (float): Negative threshold (noise or exit threshold). If model's current state is SPEECH, values BELOW this value are considered as NON-SPEECH. Default is max(activation_threshold - 0.15, 0.01).
    padding_duration (float | None): **Deprecated**. Use `prefix_padding_duration` instead.

Returns:
    VAD: An instance of the VAD class ready for streaming.

Raises:
    ValueError: If an unsupported sample rate is provided.
z4Silero VAD only supports 8KHz and 16KHz sample ratesz`padding_duration is deprecated and will be removed in 1.5.0, use prefix_padding_duration insteadz-deactivation_threshold must be greater than 0N)r'   g333333?g{Gz?)r   r   r   r   r   r   r   )sessionopts)	r   SUPPORTED_SAMPLE_RATES
ValueErrorr   r   warningnew_inference_sessionr   max)clsr   r   r   r   r   r   r&   r'   r   r(   r2   r3   s   &$$$$$$$$$$  r#   loadVAD.load;   s    x j???STT$%%NNr '7#*++0F!0KLMM229McMc_cd 3!5$; 3!5#9#c#cSAUX\A\^b=c#
 7..r"   c               $    V ^8  d   QhRRRRRR/# )r*   r2   zonnxruntime.InferenceSessionr3   r   r-   Noner   )r.   s   "r#   r/   r0      s*     	5 	5 .	5 		5
 
	5r"   c               	   < \         SV `  \        P                  P	                  R R7      R7       Wn        W n        \        P                  \        ,          ! 4       V n
        R# )gMb?)update_interval)capabilitiesN)super__init__r   vadVADCapabilities_onnx_session_optsweakrefWeakSet	VADStream_streams)selfr2   r3   	__class__s   &$$r#   rB   VAD.__init__   sF     	fjj&@&@QV&@&WX$
	24r"   c                   V ^8  d   QhRR/# r*   r-   strr   )r.   s   "r#   r/   r0      s      s r"   c                	    R # )sileror   rK   s   &r#   model	VAD.model   s    r"   c                   V ^8  d   QhRR/# rO   r   )r.   s   "r#   r/   r0      s      # r"   c                	    R # )ONNXr   rS   s   &r#   providerVAD.provider   s    r"   c                   V ^8  d   QhRR/# )r*   r-   rI   r   )r.   s   "r#   r/   r0      s      	 r"   c           	         \        V V P                  \        P                  ! V P                  V P                  P
                  R7      4      pV P                  P                  V4       V# )z
Create a new VADStream for processing audio data.

Returns:
    VADStream: A stream object for processing audio input and detecting speech.
)onnx_sessionr   )rI   rF   r   	OnnxModelrE   r   rJ   add)rK   streams   & r#   r`   
VAD.stream   sT     JJ  !//TZZ=S=S
 	&!r"   c               4    V ^8  d   QhRRRRRRRRRRRRRR	/# 
r*   r   r,   r   r   r   r   r   r-   r=   r   )r.   s   "r#   r/   r0      sR     + + /+ 0	+
 "3+ /+ 0+ !2+ 
+r"   c          
        \        V4      '       d   WP                  n        \        V4      '       d   W P                  n        \        V4      '       d   W0P                  n        \        V4      '       d   W@P                  n        \        V4      '       d   WPP                  n        \        V4      '       d   W`P                  n        V P                   F  pVP                  VVVVVVR7       K  	  R# )aN  
Update the VAD options.

This method allows you to update the VAD options after the VAD object has been created.

Args:
    min_speech_duration (float): Minimum duration of speech to start a new speech chunk.
    min_silence_duration (float): At the end of each speech, wait this duration before ending the speech.
    prefix_padding_duration (float): Duration of padding to add to the beginning of each speech chunk.
    max_buffered_speech (float): Maximum duration of speech to keep in the buffer (in seconds).
    activation_threshold (float): Threshold to consider a frame as speech.
)r   r   r   r   r   r   N)
r   rF   r   r   r   r   r   r   rJ   update_options)rK   r   r   r   r   r   r   r`   s   &$$$$$$ r#   re   VAD.update_options   s    , '((-@JJ*()).BJJ++,,1HJJ.'((-@JJ*()).BJJ+*++0FJJ-mmF!!$7%9(?$7%9'= "  $r"   )rE   rF   rJ   )r   r   r   r   __doc__classmethodr
   r:   rB   propertyrT   rY   r`   re   r!   __classcell__rL   s   @r#   r%   r%   4   s
    Q/ &*Q/ '+	Q/
 *-Q/ &*Q/ '*Q/ -2Q/ Q/ 2;Q/ 5>Q/ /8Q/ Q/f	5 	5    "+ 2;+ 3<	+
 6?+ 2;+ 3<+ 5>+ +r"   r%   c                     a  ] tR t^tR V 3R lltR]R]R]R]R]R]/R	 R
 llt]P                  P                  ]
R7      R R l4       tRtV ;t# )rI   c               (    V ^8  d   QhRRRRRRRR/# )	r*   rC   r%   r3   r   rT   zonnx_model.OnnxModelr-   r=   r   )r.   s   "r#   r/   VADStream.__annotate__   s*     	) 	)C 	){ 	);O 	)TX 	)r"   c                	   < \         SV `  V4       W#uV n        V n        \        P
                  ! 4       V n        \        P                  ! R R7      V n	        ^ V n
        RV n        RV n        ^ V n        R# )gffffff?)alphaNF)rA   rB   rF   _modelasyncioget_event_loop_loopr	   	ExpFilter_exp_filter_input_sample_rate_speech_buffer_speech_buffer_max_reached_prefix_padding_samples)rK   rC   r3   rT   rL   s   &&&&r#   rB   VADStream.__init__   s_    "&
DK++-
 ??6"#15*/''($r"   r   r   r   r   r   r   c               4    V ^8  d   QhRRRRRRRRRRRRRR	/# rc   r   )r.   s   "r#   r/   rn      sR     38 38 /38 0	38
 "338 /38 038 !238 
38r"   c               l   V P                   P                  p\        V4      '       d   WP                   n        \        V4      '       d   W P                   n        \        V4      '       d   W0P                   n        \        V4      '       d   W@P                   n        \        V4      '       d   WPP                   n        \        V4      '       d   W`P                   n        V P                  '       d   V P                  f   Q h\        V P                   P
                  V P                  ,          4      V n        V P                  P                  \        V P                   P                  V P                  ,          4      V P                  ,           4       V P                   P                  V8  d
   RV n        R# R# R# )a  
Update the VAD options.

This method allows you to update the VAD options after the VAD object has been created.

Args:
    min_speech_duration (float): Minimum duration of speech to start a new speech chunk.
    min_silence_duration (float): At the end of each speech, wait this duration before ending the speech.
    prefix_padding_duration (float): Duration of padding to add to the beginning of each speech chunk.
    max_buffered_speech (float): Maximum duration of speech to keep in the buffer (in seconds).
    activation_threshold (float): Threshold to consider a frame as speech.
    deactivation_threshold (float): Negative threshold (noise or exit threshold). If model's current state is SPEECH, values BELOW this value are considered as NON-SPEECH.
NF)rF   r   r   r   r   r   r   r   rw   rx   r   rz   resizery   )rK   r   r   r   r   r   r   old_max_buffered_speechs   &$$$$$$ r#   re   VADStream.update_options   sD   . #'**"@"@'((-@JJ*()).BJJ++,,1HJJ.'((-@JJ*()).BJJ+*++0FJJ-"""&&222+.

22T5L5LL,D( &&DJJ22T5L5LLM../
 zz--0GG27/ H #r"   r   c                   V ^8  d   QhRR/# r*   r-   r=   r   )r.   s   "r#   r/   rn   #  s     F F$ Fr"   c                	  a a"   \         P                  ! S P                  P                  \         P                  R 7      p^ oRpRpRp^ pRpRpRp. p	. p
RpRpRpS P
                    Rj  xL
  p\        V\        P                  4      '       g   K,  S P                  '       Eg&   VP                  S n	        \        S P                  P                  S P                  ,          4      S n        \         P                  ! \        S P                  P                  S P                  ,          4      S P                  ,           \         P                   R 7      S n        S P                  S P                  P                  8w  dP   \        P$                  ! S P                  S P                  P                  \        P&                  P(                  R7      pM4S P                  VP                  8w  d   \*        P,                  ! R4       EK  S P"                  f   Q hV	P/                  V4       Ve"   V
P1                  VP3                  V4      4       MV
P/                  V4        \4        P6                  ! 4       p\9        V
 Uu. uF  pVP:                  NK  	  up4      pVS P                  P                  8  d   EKH  \<        P>                  ! V	4      p\<        P>                  ! V
4      p\         P@                  ! VPB                  RS P                  P                   \         PD                  ! \         P                   4      PF                  V\         P                  R7       S PH                  PK                  RS P                  V4      G Rj  xL
 pS PL                  PO                  RVR	7      pS P                  P                  S P                  P                  ,          pVS P                  P                  ,          pVV,          pS P                  S P                  P                  ,          pS P                  P                  V,          V,           p\        V4      pVV,
          p\Q        S P"                  4      S,
          p\S        VV4      pV^ 8  d.   VPB                  RV S P"                  SSV,           % SV,          oM/S PT                  '       g   RS n*        \*        PV                  ! R
4       \4        P6                  ! 4       V,
          p\G        RVV,           V,
          4      pV\X        8  d   \*        PV                  ! RRV/R7       R V V3R llpR V V3R llpV'       d   VV,          pM	VV,          pS PZ                  P]                  \^        P`                  Pc                  \^        P`                  Pd                  Pf                  VVVVVV\        P                  ! VPB                  RV Pi                  4       S P                  ^VR7      .VVVR7      4       VS P                  Pj                  8  g$   V'       d   VS P                  Pl                  8  d   VV,          pRpV'       g   VS P                  Pn                  8  dn   RpRpTpS PZ                  P]                  \^        P`                  Pc                  \^        P`                  Pd                  Pp                  VVVVV! 4       .RR7      4       MVV,          pRpV'       g   V! 4        V'       d   VS P                  Pr                  8  d   RpTpS PZ                  P]                  \^        P`                  Pc                  \^        P`                  Pd                  Pt                  VVV\G        RW8,
          4      V! 4       .RR7      4       RpV! 4        . p	. p
\Q        VPB                  4      V,
          ^ 8  da   VPB                  VR Pi                  4       pV	P/                  \        P                  ! VS P                  ^\Q        V4      ^,          R7      4       \Q        VPB                  4      S P                  P                  ,
          ^ 8  g   EK  VPB                  S P                  P                  R Pi                  4       pV
P/                  \        P                  ! VS P                  P                  ^\Q        V4      ^,          R7      4       EK   ELxu upi  ELqEDR# 5i))dtypeFg        N)
input_rateoutput_ratequalityz3a frame with another sample rate was already pushedT)outr   g      ?)expsamplezOmax_buffered_speech reached, ignoring further data for the current speech inputz!inference is slower than realtimedelay)extrac                   V ^8  d   QhRR/# r   r   )r.   s   "r#   r/   *VADStream._main_task.<locals>.__annotate__  s     G GT Gr"   c                    < SP                   f   Q hSSP                  8:  d   R # SP                   SSP                  ,
          S p RSn        V SP                   R SP                  % SP                  oR # )NF)rx   rz   ry   )padding_datarK   speech_buffer_indexs    r#   _reset_write_cursor1VADStream._main_task.<locals>._reset_write_cursor  sv    ..:::*d.J.JJ#'#6#6+d.J.JJM`$L 7<D3JVD''(F$*F*FG*.*F*F'r"   c                   V ^8  d   QhRR/# )r*   r-   zrtc.AudioFramer   )r.   s   "r#   r/   r     s     
 
^ 
r"   c                    < SP                   f   Q hSP                   R S P                  4       p \        P                  ! SP                  ^SV R7      # )N)r   num_channelssamples_per_channeldata)rx   tobytesr   
AudioFramerw   )speech_datarK   r   s    r#   _copy_speech_buffer1VADStream._main_task.<locals>._copy_speech_buffer  sU    ..:::"&"5"56J7J"K"S"S"UK>>$($;$;%&,?(	 r"   )r   r   r   r   )typesamples_index	timestampsilence_durationspeech_durationprobabilityinference_durationframesspeakingraw_accumulated_silenceraw_accumulated_speech)r   r   r   r   r   r   r   );npemptyrq   window_size_samplesfloat32	_input_ch
isinstancer   r   rw   r   r   rF   r   rz   r   int16rx   AudioResamplerAudioResamplerQualityQUICKr   errorappendextendpushtimeperf_countersumr   r	   combine_framesdivider   iinfor8   rt   run_in_executorrv   applylenminry   r6   SLOW_INFERENCE_THRESHOLD	_event_chsend_nowaitr   rC   VADEventVADEventTypeINFERENCE_DONEr   r   r   r   START_OF_SPEECHr   END_OF_SPEECH)rK   inference_f32_datapub_speakingpub_speech_durationpub_silence_durationpub_current_samplepub_timestampspeech_threshold_durationsilence_threshold_durationinput_framesinference_frames	resamplerinput_copy_remaining_fractextra_inference_timeinput_frame
start_timeframeavailable_inference_samplesinference_framepwindow_durationresampling_ratioto_copyto_copy_intavailable_spaceto_copy_bufferr   r   r   r   r   s   f                             @r#   
_main_taskVADStream._main_task"  s    XXdkk&E&ERZZX#$ !"$'!%("-/13/3	 &)""!% o	 o	+k3>>::****5*A*A' 03JJ669P9PP0, ')hh

669P9PPQ223(('# **djj.D.DD !$ 2 2#'#:#:$(JJ$:$: # 9 9 ? ?!I ((K,C,CCRS&&222,$ !''	{(CD ''4!..0
.1<LM<L5U..<LM/+ /1P1PP#22<@"'"6"67G"H 		#(()J4;;+J+JKHHRXX&*****	 **44T4;;HZ[[$$**s1*="&++"A"ADJJDZDZ"Z"dkk&E&EE"0#'#:#:T[[=T=T#T KK336FFIcc  "'l-4{-B* #&d&9&9":=P"P!$[/!B!A% $((.9 ''+.AN.R (>9'8886:D3NNi &*%6%6%8:%E"'*(+==O($ &(@@NN;&(<=
G G
 
  '?:'(O;(**JJ''#ZZ44CC&8"/)=(;$%+=NN%0%5%5l{%C%K%K%M,0,C,C-.4?	  ".0J/H% ( . 

777 Q)J)J%J-@-14.'4

8V8VV+/L3602K/ NN66 &

 3 3)/)@)@)P)P2D.;5I4G,?,A+B-1 !4 !"
 //A.03-'+- %6$**:Y:YY',/I,22"JJ//%+ZZ%<%<%J%J.@*71E03$')<)Y1" )<(='>). 0 
 /2++-  "#%  {''(;6:&++KL9AACD ''!%(,(?(?)*03D	Q	 ++,t{{/N/NNQRR*//0O0O0QRZZ\D$++!%(,

(>(>)*03D	Q	Qo	Z N" \} "0s   A!c0%c,)c!*c,-6c0$Gc03c$
C0c08c)9Ec0>Bc0Cc0+-c0Bc05c0Ec0 Bc0!c,$c0,c0)rv   rw   rt   rq   rF   rz   rx   ry   )r   r   r   r   rB   r
   re   r   r	   log_exceptionsr   r   r!   rj   rk   s   @r#   rI   rI      sw    	) 	)38 2;38 3<	38
 6?38 2;38 3<38 5>38j \\   /F 0Fr"   rI   )!
__future__r   rr   r   rG   dataclassesr   pathlibr   typingr   numpyr   onnxruntimelivekitr   r   livekit.agentsr	   livekit.agents.typesr
   r   livekit.agents.utilsr    r   logr   r   r   rC   r%   rI   r   r"   r#   <module>r      s    #    !        *      j&**.. jZH

$$ Hr"   