U
    ~fh                     @  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rd 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                   @  s   e Zd Zdejddfddddddd	Zd
ddddZedd
dddZedddddZeddddddZddddddZdS )
Microphone   NintzType[DType]zUnion[Callable[[], bool], None]zUnion[float, None])channelsdtypeshould_recordtimeoutc                 C  s,   || _ || _|| _g | _|| _t|| _d S N)r   r   r   buffer_chunksr   callableZhas_record_function)selfr   r   r   r    r   =/tmp/pip-unpacked-wheel-rjiviquo/openai/helpers/microphone.py__init__   s    zMicrophone.__init__npt.NDArray[DType]r   )
audio_datareturnc              	   C  sl   t  }t|d>}|| j |t| jj	 |
t ||  W 5 Q R X |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_wavzLiteral[True])return_ndarrayr#   c                   s   d S r   r   r   r0   r   r   r   record/   s    zMicrophone.recordzLiteral[False]c                   s   d S r   r   r1   r   r   r   r2   2   s    .Nonec                   s   d S r   r   r1   r   r   r   r2   5   s    FzUnion[bool, None]z$Union[npt.NDArray[DType], FileTypes]c              	     s   t  t   g _t ddddd fdd}tj|jt	j
d}|   I d H  W 5 Q R X tjdkrtjjdd	ntjg jd
}|r|S |S d S )Nr!   r   r   )indata_frame_count
_time_info_statusc                   sz   t   }jd k	r |jknd}|r: j tjtjrL nd}|sf j tjj	
|   d S )NFT)timeperf_counterr   Zcall_soon_threadsafesetsdZCallbackStopr   r   r   appendcopy)r4   r5   r6   r7   Zexecution_timeZreached_recording_timeoutZshould_be_recordingeventZloopr   
start_timer   r   callback>   s    z#Microphone.record.<locals>.callback)rA   r   Z
samplerater   r   )Zaxis)r   )asyncioZget_event_loopEventr   r8   r9   r;   ZInputStreamr   r+   r   waitlenr)   Zconcatenatearrayr/   )r   r0   rA   streamZconcatenated_chunksr   r>   r   r2   8   s(    ).)F)	__name__
__module____qualname__r)   Zint16r    r/   r	   r2   r   r   r   r   r      s   
r   )
__future__r   r%   r8   r'   rB   typingr   r   r   r   r   r   r	   Ztyping_extensionsr
   r   _typesr   r   Z_extrasr   r)   r   r;   Znumpy.typingZnptr+   Zgenericr   r   r   r   r   r   <module>   s   $