o
    ͩZh                     @  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mZm	Z	m
Z
mZmZmZ d dlmZmZ ddlmZmZ ddlmZmZ erHd dlmZ dZed	ejd
ZG dd de
e ZdS )    )annotationsN)AnyTypeUnionGenericTypeVarCallableoverload)TYPE_CHECKINGLiteral   )	FileTypesFileContent)numpysounddevicei]  DType)boundc                   @  sd   e Zd Zdejddfd ddZd!ddZed"ddZed#ddZed$d%ddZd&d'ddZdS )(
Microphone   NchannelsintdtypeType[DType]should_recordUnion[Callable[[], bool], None]timeoutUnion[float, None]c                 C  s,   || _ || _|| _g | _|| _t|| _d S N)r   r   r   buffer_chunksr   callableZhas_record_function)selfr   r   r   r    r!   P/var/www/html/lang_env/lib/python3.10/site-packages/openai/helpers/microphone.py__init__   s   zMicrophone.__init__
audio_datanpt.NDArray[DType]returnr   c                 C  s   t  }t|d$}|| j |t| jj	 |
t ||  W d    n1 s1w   Y  |d d|dfS )Nwr   z	audio.wavz	audio/wav)ioBytesIOwaveopenZsetnchannelsr   Zsetsampwidthnpr   itemsizeZsetframerateSAMPLE_RATEZwriteframestobytesseek)r    r$   bufferZwav_filer!   r!   r"   _ndarray_to_wav%   s   


zMicrophone._ndarray_to_wavreturn_ndarrayLiteral[True]c                      d S r   r!   r    r3   r!   r!   r"   record/      zMicrophone.recordLiteral[False]c                   r5   r   r!   r6   r!   r!   r"   r7   2   r8   .Nonec                   r5   r   r!   r6   r!   r!   r"   r7   5   r8   FUnion[bool, None]$Union[npt.NDArray[DType], FileTypes]c                   s   t  t   g _t d fdd	}tj|jt	j
d
}|   I d H  W d    n1 s9w   Y  tjdkrMtjjddntjg jd}|rY|S |S )Nindatar%   _frame_countr   
_time_infor   _statusc                   sz   t   }jd ur|jknd}|r j tjtjr& nd}|s3 j tjj	
|   d S )NFT)timeperf_counterr   call_soon_threadsafesetsdZCallbackStopr   r   r   appendcopy)r=   r>   r?   r@   Zexecution_timeZreached_recording_timeoutZshould_be_recordingeventloopr    
start_timer!   r"   callback>   s   z#Microphone.record.<locals>.callback)rL   r   Z
samplerater   r   )Zaxis)r   )r=   r%   r>   r   r?   r   r@   r   )asyncioget_event_loopEventr   rA   rB   rE   ZInputStreamr   r.   r   waitlenr,   Zconcatenatearrayr2   )r    r3   rL   streamZconcatenated_chunksr!   rH   r"   r7   8   s,   
)r   r   r   r   r   r   r   r   )r$   r%   r&   r   )r3   r4   r&   r%   )r3   r9   r&   r   ).)r3   r:   r&   r   )F)r3   r;   r&   r<   )	__name__
__module____qualname__r,   Zint16r#   r2   r	   r7   r!   r!   r!   r"   r      s    

r   )
__future__r   r(   rA   r*   rM   typingr   r   r   r   r   r   r	   Ztyping_extensionsr
   r   _typesr   r   _extrasr   r,   r   rE   Znumpy.typingZnptr.   Zgenericr   r   r!   r!   r!   r"   <module>   s   $