
    /Ui                        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	Ze G d
 d             Z G d d      Zy)    )annotationsN)	dataclass   )	UserError   )npnpti]  c                N   | j                   t        j                  k(  r:t        j                  | dd      } | dz  j	                  t        j
                        } n(| j                   t        j
                  k7  rt        d      t        j                         }t        j                  |d      5 }|j                  |       |j                  |       |j                  |       |j                  | j                                |j!                  d       d d d        d|dfS # 1 sw Y   xY w)	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)buffer
frame_ratesample_widthchannels
audio_filewav_files         n/home/jay/workspace/tools/ai-image-gen/jaaz-app/server/venv/lib/python3.12/site-packages/agents/voice/input.py_buffer_to_audio_filer&      s     ||rzz!s+5.((2		!JKKJ	:s	# xh'l+j)V^^-. [11 s   *A$DD$c                  ^    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
Z	ddZ
y)
AudioInputz7Static audio to be used as input for the VoicePipeline.z"npt.NDArray[np.int16 | np.float32]r   intr    r   r!   r   r"   c                n    t        | j                  | j                  | j                  | j                        S )z2Returns a tuple of (filename, bytes, content_type))r&   r   r    r!   r"   selfs    r%   to_audio_filezAudioInput.to_audio_file:   s(    $T[[$//4CTCTVZVcVcdd    c                   | j                   j                  t        j                  k(  rXt        j                  | j                   dd      | _         | j                   dz  j                  t        j                        | _         n2| j                   j                  t        j                  k7  rt        d      t        j                  | j                   j                               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+   s    r%   	to_base64zAudioInput.to_base64>   s    ;;

*''$++tS9DK;;.66rxx@DK[["((*NOO 3 3 56==gFFr.   N)returntuple[str, io.BytesIO, str])r4   str)__name__
__module____qualname____doc____annotations__DEFAULT_SAMPLE_RATEr    r!   r"   r-   r3    r.   r%   r(   r(   (   sE    A.. *J)?L#<HcBe	Gr.   r(   c                      e Zd ZdZd ZddZy)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                6    t        j                         | _        y )N)asyncioQueuequeuer+   s    r%   __init__zStreamedAudioInput.__init__O   s    OV}}
r.   c                V   K   | j                   j                  |       d{    y7 w)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)rC   put)r,   audios     r%   	add_audiozStreamedAudioInput.add_audioR   s      jjnnU###s   )')N)rG   z)npt.NDArray[np.int16 | np.float32] | None)r7   r8   r9   r:   rD   rH   r=   r.   r%   r?   r?   J   s    _$r.   r?   )
r   z/npt.NDArray[np.int16 | np.float32 | np.float64]r    r)   r!   r)   r"   r)   r4   r5   )
__future__r   rA   r0   r   r   dataclassesr   
exceptionsr   importsr   r	   r<   r&   r(   r?   r=   r.   r%   <module>rM      s    "   	  ! "  
 *	2;22 2 	2
 !22 G G GB$ $r.   