o
    ?Hh                     @  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   callablehas_record_function)selfr   r   r   r    r"   Y/home/air/sanwanet/gpt-api/venv/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opensetnchannelsr   setsampwidthnpr   itemsizesetframerateSAMPLE_RATEwriteframestobytesseek)r!   r%   bufferwav_filer"   r"   r#   _ndarray_to_wav%   s   


zMicrophone._ndarray_to_wavreturn_ndarrayLiteral[True]c                      d S r   r"   r!   r9   r"   r"   r#   record/      zMicrophone.recordLiteral[False]c                   r;   r   r"   r<   r"   r"   r#   r=   2   r>   .Nonec                   r;   r   r"   r<   r"   r"   r#   r=   5   r>   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sdCallbackStopr   r   r   appendcopy)rC   rD   rE   rF   execution_timereached_recording_timeoutshould_be_recordingeventloopr!   
start_timer"   r#   callback>   s   z#Microphone.record.<locals>.callback)rV   r   
samplerater   r   )axis)r   )rC   r&   rD   r   rE   r   rF   r   )asyncioget_event_loopEventr   rG   rH   rK   InputStreamr   r2   r   waitlenr/   concatenatearrayr8   )r!   r9   rV   streamconcatenated_chunksr"   rR   r#   r=   8   s,   
)r   r   r   r   r   r   r   r   )r%   r&   r'   r   )r9   r:   r'   r&   )r9   r?   r'   r   ).)r9   r@   r'   r   )F)r9   rA   r'   rB   )	__name__
__module____qualname__r/   int16r$   r8   r	   r=   r"   r"   r"   r#   r      s    

r   )
__future__r   r)   rG   r+   rY   typingr   r   r   r   r   r   r	   typing_extensionsr
   r   _typesr   r   _extrasr   r/   r   rK   numpy.typingnptr2   genericr   r   r"   r"   r"   r#   <module>   s   $