o
    i                  
   @   s
  d Z ddl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 ddlmZ ddlmZ dd	lmZ dd
lmZmZ zddlZW n  eyb Z zede  ed ede dZ[ww G dd deZG dd deZG dd deZG dd deZdS )zLocal audio transport implementation for Pipecat.

This module provides a local audio transport that uses PyAudio for real-time
audio input and output through the system's default audio devices.
    N)ThreadPoolExecutor)Optional)logger)InputAudioRawFrameOutputAudioRawFrame
StartFrame)FrameProcessor)BaseInputTransport)BaseOutputTransport)BaseTransportTransportParamszException: z~In order to use local audio, you need to `pip install pipecat-ai[local]`. On MacOS, you also need to `brew install portaudio`.zMissing module: c                   @   s2   e Zd ZU dZdZee ed< dZee ed< dS )LocalAudioTransportParamszConfiguration parameters for local audio transport.

    Parameters:
        input_device_index: PyAudio device index for audio input. If None, uses default.
        output_device_index: PyAudio device index for audio output. If None, uses default.
    Ninput_device_indexoutput_device_index)	__name__
__module____qualname____doc__r   r   int__annotations__r    r   r   R/home/ubuntu/.local/lib/python3.10/site-packages/pipecat/transports/local/audio.pyr   #   s   
 r   c                       s\   e Zd ZU dZeed< dejdef fddZde	f fdd	Z
 fd
dZdd Z  ZS )LocalAudioInputTransportzLocal audio input transport using PyAudio.

    Captures audio from the system's audio input device and converts it to
    InputAudioRawFrame objects for processing in the pipeline.
    _paramspy_audioparamsc                    s"   t  | || _d| _d| _dS )zInitialize the local audio input transport.

        Args:
            py_audio: PyAudio instance for audio device management.
            params: Transport configuration parameters.
        Nr   )super__init__	_py_audio
_in_stream_sample_rateselfr   r   	__class__r   r   r   8   s   
z!LocalAudioInputTransport.__init__framec              	      s   t  |I dH  | jrdS | jjp|j| _t| jd d }| jj| j	d| jj
| j|| jd| jjd| _| j  | |I dH  dS )z~Start the audio input stream.

        Args:
            frame: The start frame containing initialization parameters.
        Nd      T)formatchannelsrateframes_per_bufferstream_callbackinputr   )r   startr   r   audio_in_sample_rater    r   r   openget_format_from_widthaudio_in_channels_audio_in_callbackr   start_streamset_transport_ready)r"   r%   
num_framesr#   r   r   r.   E   s"   

	zLocalAudioInputTransport.startc                    :   t   I dH  | jr| j  | j  d| _dS dS )z(Stop and cleanup the audio input stream.N)r   cleanupr   stop_streamcloser"   r#   r   r   r8   `      


z LocalAudioInputTransport.cleanupc                 C   s4   t || j| jjd}t| ||   dtj	fS )z+Callback function for PyAudio input stream.)audiosample_ratenum_channelsN)
r   r    r   r2   asynciorun_coroutine_threadsafepush_audio_frameget_event_looppyaudio
paContinue)r"   in_dataframe_count	time_infostatusr%   r   r   r   r3   h   s   
z+LocalAudioInputTransport._audio_in_callback)r   r   r   r   r   r   rD   PyAudior   r   r.   r8   r3   __classcell__r   r   r#   r   r   /   s   
 r   c                       sf   e Zd ZU dZeed< dejdef fddZde	f fdd	Z
 fd
dZdedefddZ  ZS )LocalAudioOutputTransportzLocal audio output transport using PyAudio.

    Plays audio frames through the system's audio output device by converting
    OutputAudioRawFrame objects to playable audio data.
    r   r   r   c                    s.   t  | || _d| _d| _tdd| _dS )zInitialize the local audio output transport.

        Args:
            py_audio: PyAudio instance for audio device management.
            params: Transport configuration parameters.
        Nr      )max_workers)r   r   r   _out_streamr    r   	_executorr!   r#   r   r   r   ~   s
   z"LocalAudioOutputTransport.__init__r%   c                    sv   t  |I dH  | jrdS | jjp|j| _| jj| jd| jj	| jd| jj
d| _| j  | |I dH  dS )zStart the audio output stream.

        Args:
            frame: The start frame containing initialization parameters.
        Nr'   T)r(   r)   r*   outputr   )r   r.   rO   r   audio_out_sample_rater    r   r0   r1   audio_out_channelsr   r4   r5   r"   r%   r#   r   r   r.      s   

zLocalAudioOutputTransport.startc                    r7   )z)Stop and cleanup the audio output stream.N)r   r8   rO   r9   r:   r;   r#   r   r   r8      r<   z!LocalAudioOutputTransport.cleanupreturnc                    s0   | j r|  | j| j j|jI dH  dS dS )zWrite an audio frame to the output stream.

        Args:
            frame: The audio frame to write to the output device.

        Returns:
            True if the audio frame was written successfully, False otherwise.
        NTF)rO   rC   run_in_executorrP   writer=   rT   r   r   r   write_audio_frame   s   	
z+LocalAudioOutputTransport.write_audio_frame)r   r   r   r   r   r   rD   rJ   r   r   r.   r8   r   boolrX   rK   r   r   r#   r   rL   u   s   
 rL   c                       sB   e Zd ZdZdef fddZdefddZdefdd	Z  Z	S )
LocalAudioTransportzComplete local audio transport with input and output capabilities.

    Provides a unified interface for local audio I/O using PyAudio, supporting
    both audio capture and playback through the system's audio devices.
    r   c                    s*   t    || _t | _d| _d| _dS )zuInitialize the local audio transport.

        Args:
            params: Transport configuration parameters.
        N)r   r   r   rD   rJ   _pyaudio_input_output)r"   r   r#   r   r   r      s
   


zLocalAudioTransport.__init__rU   c                 C      | j st| j| j| _ | j S )z}Get the input frame processor for this transport.

        Returns:
            The audio input transport processor.
        )r\   r   r[   r   r;   r   r   r   r-         zLocalAudioTransport.inputc                 C   r^   )zGet the output frame processor for this transport.

        Returns:
            The audio output transport processor.
        )r]   rL   r[   r   r;   r   r   r   rQ      r_   zLocalAudioTransport.output)
r   r   r   r   r   r   r   r-   rQ   rK   r   r   r#   r   rZ      s
    
rZ   )r   r@   concurrent.futuresr   typingr   logurur   pipecat.frames.framesr   r   r   "pipecat.processors.frame_processorr   pipecat.transports.base_inputr	   pipecat.transports.base_outputr
   !pipecat.transports.base_transportr   r   rD   ModuleNotFoundErroreerror	Exceptionr   r   rL   rZ   r   r   r   r   <module>   s0   FK