o
    i                     @   s|   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 deZG d	d
 d
e
ZG dd deZdS )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                   @   s   e Zd ZdZdZdZdS )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 r   r   Y/home/ubuntu/.local/lib/python3.10/site-packages/pipecat/audio/turn/base_turn_analyzer.pyr	      s    r	   c                   @   s   e Zd ZdZdS )BaseTurnParamsz(Base class for turn analyzer parameters.N)r   r   r   r   r   r   r   r   r   "   s    r   c                   @   s   e Zd ZdZdddee fddZedefdd	Zdefd
dZ	ee
defddZee
defddZe
dededefddZe
deeee f fddZdefddZe
dd Zdd ZdS )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                C   s   || _ d| _dS )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   r   r   r   __init__/   s   
zBaseTurnAnalyzer.__init__returnc                 C   s   | j S )z|Returns the current sample rate.

        Returns:
            int: The effective sample rate for audio processing.
        )r   r   r   r   r   r   9   s   zBaseTurnAnalyzer.sample_ratec                 C   s   | j p|| _dS )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   r   r   r   set_sample_rateB   s   	z BaseTurnAnalyzer.set_sample_ratec                 C      dS )zDetermines if speech has been detected.

        Returns:
            bool: True if speech is triggered, otherwise False.
        Nr   r   r   r   r   speech_triggeredM      z!BaseTurnAnalyzer.speech_triggeredc                 C   r    )zGet the current turn analyzer parameters.

        Returns:
            Current turn analyzer configuration parameters.
        Nr   r   r   r   r   paramsW   r"   zBaseTurnAnalyzer.paramsbuffer	is_speechc                 C   r    )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%   r   r   r   append_audioa   s   zBaseTurnAnalyzer.append_audioc                       dS )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   r   r   r   analyze_end_of_turnn   s   z$BaseTurnAnalyzer.analyze_end_of_turnvad_start_secsc                 C   r    )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   r)   r   r   r   update_vad_start_secsw   s   
z&BaseTurnAnalyzer.update_vad_start_secsc                 C   r    )z-Reset the turn analyzer to its initial state.Nr   r   r   r   r   clear   s   zBaseTurnAnalyzer.clearc                    r'   )zCleanup the turn analyzer.Nr   r   r   r   r   cleanup   s   zBaseTurnAnalyzer.cleanup)r   r   r   r   r   intr   propertyr   r   r   boolr!   r   r#   bytesr	   r&   r   r   r(   floatr*   r+   r,   r   r   r   r   r   (   s(    

r   N)r   abcr   r   enumr   typingr   r   pydanticr   pipecat.metrics.metricsr   r	   r   r   r   r   r   r   <module>   s   