o
    Õ­j3;  ã                   @  sx   d dl mZ d dlZd dlZd dlZd dlZd dlZd dlZd dlZd dl	Z	d dl
Z
G dd„ deƒZdd„ Zdd„ ZdS )	é    )ÚannotationsNc                   @  sX   e Zd ZdZdd„ Zeddd„ƒZdd
d„Zddd„Zddd„Z	ddd„Z
ddd„Zd	S )Ú	AudioDataaÝ  
    Creates a new ``AudioData`` instance, which represents mono audio data.

    The raw audio data is specified by ``frame_data``, which is a sequence of bytes representing audio samples. This is the frame data structure used by the PCM WAV format.

    The width of each sample, in bytes, is specified by ``sample_width``. Each group of ``sample_width`` bytes represents a single audio sample.

    The audio data is assumed to have a sample rate of ``sample_rate`` samples per second (Hertz).

    Usually, instances of this class are obtained from ``recognizer_instance.record`` or ``recognizer_instance.listen``, or in the callback for ``recognizer_instance.listen_in_background``, rather than instantiating them directly.
    c                 C  sZ   |dksJ dƒ‚|d dkrd|  krdks J dƒ‚ J dƒ‚|| _ || _t|ƒ| _d S )Nr   z&Sample rate must be a positive integeré   é   z.Sample width must be between 1 and 4 inclusive)Ú
frame_dataÚsample_rateÚintÚsample_width)Úselfr   r   r	   © r   úU/var/www/html/CropPilot/venv/lib/python3.10/site-packages/speech_recognition/audio.pyÚ__init__   s    ÿÿzAudioData.__init__Ú	file_pathÚstrÚreturnc                 C  sH   ddl }| ¡ }| |¡�}| |¡W  d  ƒ S 1 sw   Y  dS )z8Creates a new ``AudioData`` instance from an audio file.r   N)Úspeech_recognitionÚ
RecognizerÚ	AudioFileÚrecord)Úclsr   ÚsrÚrÚsourcer   r   r   Ú	from_file$   s
   $ÿzAudioData.from_fileNc                 C  s¨   |du s|dksJ dƒ‚|du s||du rdn|ksJ dƒ‚|du r%d}nt || j | j d ƒ}|du r;t| jƒ}nt || j | j d ƒ}t| j||… | j| jƒS )ag  
        Returns a new ``AudioData`` instance, trimmed to a given time interval. In other words, an ``AudioData`` instance with the same audio data except starting at ``start_ms`` milliseconds in and ending ``end_ms`` milliseconds in.

        If not specified, ``start_ms`` defaults to the beginning of the audio, and ``end_ms`` defaults to the end.
        Nr   z*``start_ms`` must be a non-negative numberzI``end_ms`` must be a non-negative number greater or equal to ``start_ms``iè  )r   r   r	   Úlenr   r   )r
   Ústart_msÚend_msÚ
start_byteÚend_byter   r   r   Úget_segment-   s.   ÿ
ÿþÿÿýzAudioData.get_segmentc              
     sN  |du s|dksJ dƒ‚|du s(|d dkr$d|  kr#dks(J dƒ‚ J dƒ‚| j ‰ | jdkr7t ˆ dd¡‰ |durN| j|krNt ˆ | jd| j|d¡\‰ }|durš| j|krš|dkr’t ˆ | jd¡‰ z	t d	dd¡ W n tjyˆ   d	 ‡ fd
d„t	dt
ˆ ƒdƒD ƒ¡‰ Y nw t ˆ | j|¡‰ nt ˆ | j|¡‰ |dkr¥t ˆ dd¡‰ ˆ S )a)  
        Returns a byte string representing the raw frame data for the audio represented by the ``AudioData`` instance.

        If ``convert_rate`` is specified and the audio sample rate is not ``convert_rate`` Hz, the resulting audio is resampled to match.

        If ``convert_width`` is specified and the audio samples are not ``convert_width`` bytes each, the resulting audio is converted to match.

        Writing these bytes directly to a file results in a valid `RAW/PCM audio file <https://en.wikipedia.org/wiki/Raw_audio_format>`__.
        Nr   z4Sample rate to convert to must be a positive integerr   r   z<Sample width to convert to must be between 1 and 4 inclusivei€ÿÿÿé   ó    c                 3  s$   � | ]}ˆ |d  |d … V  qdS )r   r   Nr   ©Ú.0Úi©Úraw_datar   r   Ú	<genexpr>~   s
   € ÿ
ÿz)AudioData.get_raw_data.<locals>.<genexpr>é€   )r   r	   ÚaudioopÚbiasr   ÚratecvÚlin2linÚerrorÚjoinÚranger   )r
   Úconvert_rateÚconvert_widthÚ_r   r%   r   Úget_raw_dataK   sd   ÿ ÿÿ
ÿú
ÿÿÿþýÿÿÿzAudioData.get_raw_datac           	   	   C  s²   |   ||¡}|du r| jn|}|du r| jn|}t ¡ �3}t |d¡}z| |¡ | |¡ | 	d¡ | 
|¡ | ¡ }W | ¡  n| ¡  w W d  ƒ |S 1 sRw   Y  |S )a!  
        Returns a byte string representing the contents of a WAV file containing the audio represented by the ``AudioData`` instance.

        If ``convert_width`` is specified and the audio samples are not ``convert_width`` bytes each, the resulting audio is converted to match.

        If ``convert_rate`` is specified and the audio sample rate is not ``convert_rate`` Hz, the resulting audio is resampled to match.

        Writing these bytes directly to a file results in a valid `WAV file <https://en.wikipedia.org/wiki/WAV>`__.
        NÚwbr   )r3   r   r	   ÚioÚBytesIOÚwaveÚopenÚsetframerateÚsetsampwidthÚsetnchannelsÚwriteframesÚgetvalueÚclose)	r
   r0   r1   r&   r   r	   Úwav_fileÚ
wav_writerÚwav_datar   r   r   Úget_wav_data“   s$   
ÿÿ






÷
ö
zAudioData.get_wav_datac              	     s  |   ||¡‰ |du r| jn|}|du r| jn|‰ttdƒr$t ˆ ˆ¡‰ nˆ ˆd dd… d ‡ ‡fdd„tˆd tˆ ƒˆƒD ƒ¡ ‰ t	 
¡ �3}t |d¡}z| |¡ | ˆ¡ | d¡ | ˆ ¡ | ¡ }W | ¡  n| ¡  w W d  ƒ |S 1 s|w   Y  |S )	aB  
        Returns a byte string representing the contents of an AIFF-C file containing the audio represented by the ``AudioData`` instance.

        If ``convert_width`` is specified and the audio samples are not ``convert_width`` bytes each, the resulting audio is converted to match.

        If ``convert_rate`` is specified and the audio sample rate is not ``convert_rate`` Hz, the resulting audio is resampled to match.

        Writing these bytes directly to a file results in a valid `AIFF-C file <https://en.wikipedia.org/wiki/Audio_Interchange_File_Format>`__.
        NÚbyteswapr   éÿÿÿÿr!   c                 3  s"   � | ]}ˆ |ˆ |d … V  qdS )rD   Nr   r"   ©r&   r	   r   r   r'   Ê   s
   € ÿ
ÿz*AudioData.get_aiff_data.<locals>.<genexpr>r4   )r3   r   r	   Úhasattrr)   rC   r.   r/   r   r5   r6   Úaifcr8   r9   r:   r;   r<   r=   r>   )r
   r0   r1   r   Ú	aiff_fileÚaiff_writerÚ	aiff_datar   rE   r   Úget_aiff_data²   s2   
ÿÿÿ 
þ






÷
ö
zAudioData.get_aiff_datac           	      C  sÂ   |du s|d dkrd|  krdksJ dƒ‚ J dƒ‚| j dkr'|du r'd}|  ||¡}tƒ }tjdkrFt ¡ }| jtjO  _tj	|_
nd}tj|ddd	d
gtjtj|d�}| |¡\}}|S )aÈ  
        Returns a byte string representing the contents of a FLAC file containing the audio represented by the ``AudioData`` instance.

        Note that 32-bit FLAC is not supported. If the audio data is 32-bit and ``convert_width`` is not specified, then the resulting FLAC will be a 24-bit FLAC.

        If ``convert_rate`` is specified and the audio sample rate is not ``convert_rate`` Hz, the resulting audio is resampled to match.

        If ``convert_width`` is specified and the audio samples are not ``convert_width`` bytes each, the resulting audio is converted to match.

        Writing these bytes directly to a file results in a valid `FLAC file <https://en.wikipedia.org/wiki/FLAC>`__.
        Nr   r   r    z<Sample width to convert to must be between 1 and 3 inclusiveÚntz--stdoutz--totally-silentz--bestú-)ÚstdinÚstdoutÚstartupinfo)r	   rB   Úget_flac_converterÚosÚnameÚ
subprocessÚSTARTUPINFOÚdwFlagsÚSTARTF_USESHOWWINDOWÚSW_HIDEÚwShowWindowÚPopenÚPIPEÚcommunicate)	r
   r0   r1   rA   Úflac_converterÚstartup_infoÚprocessÚ	flac_dataÚstderrr   r   r   Úget_flac_dataÜ   s>    ÿÿ
ÿÿûözAudioData.get_flac_data)r   r   r   r   )NN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   Úclassmethodr   r   r3   rB   rK   rb   r   r   r   r   r      s    	


H
*r   c                  C  s:  t dƒ} | du r_tj tj t¡¡}t ¡ t ¡ }}|dkr+|dv r+tj 	|d¡} n4|dkr;|dv r;tj 	|d¡} n$|d	krK|d
v rKtj 	|d¡} n|d	kr[|dv r[tj 	|d¡} nt
dƒ‚z3t | tj¡s�t | ¡}t | |jtjB ¡ d	t ¡ v r�tjdkr…t ¡ nt d¡ W | S W | S W | S  t
yœ   Y | S w )zdReturns the absolute path of a FLAC converter executable, or raises an OSError if none can be found.ÚflacNÚWindows>   Úx86Úi686Úi786ÚAMD64Úx86_64zflac-win32.exeÚDarwin>   rj   rk   rl   rm   Úarm64rn   zflac-macÚLinux>   rj   rk   rl   zflac-linux-x86>   rm   rn   zflac-linux-x86_64z¥FLAC conversion utility not available - consider installing the FLAC command line application by running `apt-get install flac` or your operating system's equivalent)r    r    Úsync)Úshutil_whichrR   ÚpathÚdirnameÚabspathÚ__file__ÚplatformÚsystemÚmachiner.   ÚOSErrorÚaccessÚX_OKÚstatÚchmodÚst_modeÚS_IEXECÚsysÚversion_inforr   )r]   Ú	base_pathry   rz   Ú	stat_infor   r   r   rQ     sB   
ÿÿ
€÷	úýýrQ   c                 C  sR   t  d¡}| t jj¡D ]}t j || ¡}t j |¡r&t  |t j¡r&|  S qdS )zDPython 2 compatibility: backport of ``shutil.which()`` from Python 3ÚPATHN)	rR   ÚgetenvÚsplitrt   Úpathsepr.   Úexistsr|   r}   )Úpgmrt   Úpr   r   r   rs   C  s   
€ýrs   )Ú
__future__r   rG   r)   r5   rR   rx   r~   rT   r‚   r7   Úobjectr   rQ   rs   r   r   r   r   Ú<module>   s      3