
    qi                         d Z ddlmZmZ ddlmZ ddlmZmZ ddl	m
Z
 ddlmZ  G d de      Z G d	 d
e
      Z G d de      Zy)zBase turn analyzer for determining end-of-turn in audio conversations.

This module provides the abstract base class and enumeration for analyzing
when a user has finished speaking in a conversation.
    )ABCabstractmethod)Enum)OptionalTuple)	BaseModel)MetricsDatac                       e Zd ZdZdZdZy)EndOfTurnStatezState enumeration for end-of-turn analysis results.

    Parameters:
        COMPLETE: The user has finished their turn and stopped speaking.
        INCOMPLETE: The user is still speaking or may continue speaking.
          N)__name__
__module____qualname____doc__COMPLETE
INCOMPLETE     W/opt/pipecat/venv/lib/python3.12/site-packages/pipecat/audio/turn/base_turn_analyzer.pyr   r      s     HJr   r   c                       e Zd ZdZy)BaseTurnParamsz(Base class for turn analyzer parameters.N)r   r   r   r   r   r   r   r   r   "   s    2r   r   c                       e Zd ZdZdddee   fdZedefd       ZdefdZ	ee
defd	              Zee
defd
              Ze
dededefd       Ze
deeee   f   fd       ZdefdZe
d        Zd Zy)BaseTurnAnalyzerzAbstract base class for analyzing user end of turn.

    This class inherits from BaseObject to leverage its event handling system
    while still defining an abstract interface through abstract methods.
    N)sample_rater   c                     || _         d| _        y)zInitialize the turn analyzer.

        Args:
            sample_rate: Optional initial sample rate for audio processing.
                If provided, this will be used as the fixed sample rate.
        r   N_init_sample_rate_sample_rateselfr   s     r   __init__zBaseTurnAnalyzer.__init__/   s     "-r   returnc                     | j                   S )z|Returns the current sample rate.

        Returns:
            int: The effective sample rate for audio processing.
        )r   r!   s    r   r   zBaseTurnAnalyzer.sample_rate9   s        r   c                 .    | j                   xs || _        y)zSets the sample rate for audio processing.

        If the initial sample rate was provided, it will use that; otherwise, it sets to
        the provided sample rate.

        Args:
            sample_rate (int): The sample rate to set.
        Nr   r    s     r   set_sample_ratez BaseTurnAnalyzer.set_sample_rateB   s     !22Akr   c                      y)zDetermines if speech has been detected.

        Returns:
            bool: True if speech is triggered, otherwise False.
        Nr   r%   s    r   speech_triggeredz!BaseTurnAnalyzer.speech_triggeredM        	r   c                      y)zGet the current turn analyzer parameters.

        Returns:
            Current turn analyzer configuration parameters.
        Nr   r%   s    r   paramszBaseTurnAnalyzer.paramsW   r*   r   buffer	is_speechc                      y)a   Appends audio data for analysis.

        Args:
            buffer (bytes): The audio data to append.
            is_speech (bool): Indicates whether the appended audio is speech or not.

        Returns:
            EndOfTurnState: The resulting state after appending the audio.
        Nr   )r!   r-   r.   s      r   append_audiozBaseTurnAnalyzer.append_audioa   s     	r   c                    K   yw)zAnalyzes if an end of turn has occurred based on the audio input.

        Returns:
            EndOfTurnState: The result of the end of turn analysis.
        Nr   r%   s    r   analyze_end_of_turnz$BaseTurnAnalyzer.analyze_end_of_turnn   s      	   vad_start_secsc                      y)a"  Update the VAD start trigger time.

        The turn analyzer may choose to change its buffer size depending
        on this value.

        Args:
            vad_start_secs (float): The number of seconds of voice activity
                before triggering the user speaking event.
        Nr   )r!   r4   s     r   update_vad_start_secsz&BaseTurnAnalyzer.update_vad_start_secsw   s     	r   c                      y)z-Reset the turn analyzer to its initial state.Nr   r%   s    r   clearzBaseTurnAnalyzer.clear   s     	r   c                    K   yw)zCleanup the turn analyzer.Nr   r%   s    r   cleanupzBaseTurnAnalyzer.cleanup   s	     r3   )r   r   r   r   r   intr"   propertyr   r'   r   boolr)   r   r,   bytesr   r0   r   r	   r2   floatr6   r8   r:   r   r   r   r   r   (   s    8< x}  !S ! !	B3 	B $        
5 
T 
n 
 
 5+AV1V+W  
E 
  r   r   N)r   abcr   r   enumr   typingr   r   pydanticr   pipecat.metrics.metricsr	   r   r   r   r   r   r   <module>rE      sA    $  "  /	T 		Y 	bs br   