
    qi                     v    d Z ddlmZ ddlmZ ddlmZ ddlmZ ddl	m
Z
mZmZmZ ddlmZmZ  G d d	e      Zy
)zVoice Activity Detection processor for detecting speech in audio streams.

This module provides a VADProcessor that wraps a VADController to process
audio frames and push VAD-related frames into the pipeline.
    )Type)logger)VADAnalyzer)VADController)FrameUserSpeakingFrameVADUserStartedSpeakingFrameVADUserStoppedSpeakingFrame)FrameDirectionFrameProcessorc                   F     e Zd ZdZdddedef fdZdedef fd	Z	 xZ
S )
VADProcessora  Processes audio frames through voice activity detection.

    This processor wraps a VADController to detect speech in audio streams
    and push VAD frames into the pipeline:

    - ``VADUserStartedSpeakingFrame``: Pushed when speech begins.
    - ``VADUserStoppedSpeakingFrame``: Pushed when speech ends.
    - ``UserSpeakingFrame``: Pushed periodically while speech is detected.

    Example::

        vad_processor = VADProcessor(vad_analyzer=SileroVADAnalyzer())
    g?speech_activity_periodvad_analyzerr   c                    t        	   di | t        ||       _         j                  j	                  d       fd       } j                  j	                  d       fd       } j                  j	                  d       fd       } j                  j	                  d      d	t
        d
t        f fd       } j                  j	                  d      dt        t
           f fd       }y)aD  Initialize the VAD processor.

        Args:
            vad_analyzer: The VADAnalyzer instance for processing audio.
            speech_activity_period: Minimum interval in seconds between
                UserSpeakingFrame pushes. Defaults to 0.2.
            **kwargs: Additional arguments passed to parent class.
        r   on_speech_startedc                    K   t        j                   d       j                  t        | j                  j
                  j                         d {    y 7 w)Nz: User started speaking)
start_secs)r   debugbroadcast_framer	   _vad_analyzerparamsr   _controllerselfs    X/opt/pipecat/venv/lib/python3.12/site-packages/pipecat/processors/audio/vad_processor.pyr   z0VADProcessor.__init__.<locals>.on_speech_started@   sP     LLD6!89:&&+&44;;FF '      AAAAon_speech_stoppedc                    K   t        j                   d       j                  t        | j                  j
                  j                         d {    y 7 w)Nz: User stopped speaking)	stop_secs)r   r   r   r
   r   r   r!   r   s    r   r   z0VADProcessor.__init__.<locals>.on_speech_stoppedH   sP     LLD6!89:&&+%33::DD '   r   on_speech_activityc                 L   K   j                  t               d {    y 7 wN)r   r   r   s    r   r"   z1VADProcessor.__init__.<locals>.on_speech_activityP   s     &&'8999s   $"$on_push_frameframe	directionc                 F   K   j                  ||       d {    y 7 wr$   )
push_frame)r   r&   r'   r   s      r   r%   z,VADProcessor.__init__.<locals>.on_push_frameU   s     //%333s   !!on_broadcast_frame	frame_clsc                 H   K    j                   |fi | d {    y 7 wr$   )r   )r   r+   kwargsr   s      r   r*   z1VADProcessor.__init__.<locals>.on_broadcast_frameY   s"     &$&&y;F;;;s   " "N )super__init__r   _vad_controllerevent_handlerr   r   r   )
r   r   r   r-   r   r   r"   r%   r*   	__class__s
   `        r   r0   zVADProcessor.__init__+   s    	"6",1G 

 
			+	+,?	@	 
A	 
			+	+,?	@	 
A	 
			+	+,@	A	: 
B	: 
			+	+O	<	4E 	4n 	4 
=	4 
			+	+,@	A	<T%[ 	< 
B	<    r&   r'   c                    K   t         |   ||       d{    | j                  ||       d{    | j                  j                  |       d{    y7 B7 *7 	w)zProcess a frame through VAD and forward it.

        Args:
            frame: The frame to process.
            direction: The direction of frame flow in the pipeline.
        N)r/   process_framer)   r1   )r   r&   r'   r3   s      r   r6   zVADProcessor.process_frame]   sa      g#E9555
 ooeY/// ""00777 	6
 	0 	8s1   A AA A"A AA A A )__name__
__module____qualname____doc__r   floatr0   r   r   r6   __classcell__)r3   s   @r   r   r      s?    $ ),	0< "0< !&	0<d8 8> 8 8r4   r   N)r:   typingr   logurur   pipecat.audio.vad.vad_analyzerr    pipecat.audio.vad.vad_controllerr   pipecat.frames.framesr   r   r	   r
   "pipecat.processors.frame_processorr   r   r   r.   r4   r   <module>rC      s6      6 :  NP8> P8r4   