o
    h                     @  s   d dl mZ d dlZd dlZd dlZd dlZd dlmZ ddlm	Z	 ddl
mZmZ dZeddfdddZeG dd dZG dd dZdS )    )annotationsN)	dataclass   )	UserError   )npnpti]  buffer/npt.NDArray[np.int16 | np.float32 | np.float64]
frame_rateintsample_widthchannelsreturntuple[str, io.BytesIO, str]c                 C  s   | j tjkrt| dd} | d tj} n
| j tjkr tdt }t	
|d#}|| || || ||   |d W d    n1 sPw   Y  d|dfS )	N            ?  0Buffer must be a numpy array of int16 or float32wr   z	audio.wavz	audio/wav)dtyper   float32clipastypeint16r   ioBytesIOwaveopensetnchannelssetsampwidthsetframeratewriteframestobytesseek)r	   r   r   r   
audio_filewav_file r'   S/var/www/html/openai_agents/venv/lib/python3.10/site-packages/agents/voice/input.py_buffer_to_audio_file   s   



r)   c                   @  sZ   e Zd ZU dZded< 	 eZded< 	 dZded< 	 dZded	< 	 dddZ	dddZ
dS )
AudioInputz7Static audio to be used as input for the VoicePipeline.z"npt.NDArray[np.int16 | np.float32]r	   r   r   r   r   r   r   r   r   c                 C  s   t | j| j| j| jS )z2Returns a tuple of (filename, bytes, content_type))r)   r	   r   r   r   selfr'   r'   r(   to_audio_file:   s   zAudioInput.to_audio_filestrc                 C  sb   | j jtjkrt| j dd| _ | j d tj| _ n| j jtjkr&tdt	| j 
 dS )z2Returns the audio data as a base64 encoded string.r   r   r   r   zutf-8)r	   r   r   r   r   r   r   r   base64	b64encoder#   decoder+   r'   r'   r(   	to_base64>   s   zAudioInput.to_base64N)r   r   )r   r.   )__name__
__module____qualname____doc____annotations__DEFAULT_SAMPLE_RATEr   r   r   r-   r2   r'   r'   r'   r(   r*   (   s   
 
r*   c                   @  s"   e Zd ZdZdd Zd	ddZdS )
StreamedAudioInputzAudio input represented as a stream of audio data. You can pass this to the `VoicePipeline`
    and then push audio data into the queue using the `add_audio` method.
    c                 C  s   t  | _d S )N)asyncioQueuequeuer+   r'   r'   r(   __init__O   s   zStreamedAudioInput.__init__audio)npt.NDArray[np.int16 | np.float32] | Nonec                   s   | j |I dH  dS )zAdds more audio data to the stream.

        Args:
            audio: The audio data to add. Must be a numpy array of int16 or float32 or None.
              If None passed, it indicates the end of the stream.
        N)r<   put)r,   r>   r'   r'   r(   	add_audioR   s   zStreamedAudioInput.add_audioN)r>   r?   )r3   r4   r5   r6   r=   rA   r'   r'   r'   r(   r9   J   s    r9   )
r	   r
   r   r   r   r   r   r   r   r   )
__future__r   r:   r/   r   r   dataclassesr   
exceptionsr   importsr   r   r8   r)   r*   r9   r'   r'   r'   r(   <module>   s     !