
    i3;                    z    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e      Zd Zd Zy)    )annotationsNc                  P    e Zd ZdZd Zed
d       ZddZddZddZ	ddZ
dd	Zy)	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                    |dkD  sJ d       |dz  dk(  rd|cxk  rdk  sJ d        J d       || _         || _        t        |      | _        y )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   s       V/var/www/auto_recruiter/arenv/lib/python3.12/site-packages/speech_recognition/audio.py__init__zAudioData.__init__   sl    QH HH1!a<&<1&<	<;	<<&<	<;	<<$&-    c                    ddl }|j                         }|j                  |      5 }|j                  |      cddd       S # 1 sw Y   yxY w)z8Creates a new ``AudioData`` instance from an audio file.r   N)speech_recognition
Recognizer	AudioFilerecord)cls	file_pathsrrsources        r   	from_filezAudioData.from_file$   sA     	(MMO\\)$ 	$88F#	$ 	$ 	$s   AA
Nc                x   ||dk\  sJ d       |||dn|k\  sJ d       |d}n(t        || j                  z  | j                  z  dz        }|t        | j                        }n(t        || j                  z  | j                  z  dz        }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.
        r   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_bytes        r   get_segmentzAudioData.get_segment-   s     A	87	8-~!Ax"
 	WV	W 
 JD,,,t/@/@@TIJ >4??+H$***T->->>4GH OOJx0
 	
r   c           
     >   ||dkD  sJ d       |"|dz  dk(  rd|cxk  rdk  sJ d        J d       | j                   | j                  dk(  rt        j                  dd      |@| j                  |k7  r1t        j
                  | j                  d| j                  |d      \  }|| j                  |k7  r|dk(  r[t        j                  | j                  d      	 t        j                  d	dd       t        j                  | j                  |      n!t        j                  | j                  |      |dk(  rt        j                  dd      S # t        j                  $ r1 d	j                  fd
t        dt              d      D              Y aw xY w)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   r   c              3  4   K   | ]  }|d z   |dz      yw)r   r   N ).0iraw_datas     r   	<genexpr>z)AudioData.get_raw_data.<locals>.<genexpr>~   s(      ( !QA.(      )r	   r   audioopbiasr
   ratecvlin2linerrorjoinranger   )r   convert_rateconvert_width_r)   s       @r   get_raw_datazAudioData.get_raw_dataK   s     L1$4	BA	B4$A"qM'>Q'>	JI	J 
'>	JI	J 
 ?? !||!TH
 #(8(8L(H!..!!  KHa $):):m)K""??d//LLQ  ' $"3"3] H #??d//
 A||!SH + MM  #xx (!&q#h-!;(  Hs    E AFFc                   | j                  ||      }|| j                  n|}|| j                  n|}t        j                         5 }t        j                  |d      }	 |j                  |       |j                  |       |j                  d       |j                  |       |j                         }|j                          	 ddd       |S # |j                          w xY w# 1 sw Y   S xY w)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   )r7   r
   r   ioBytesIOwaveopensetframeratesetsampwidthsetnchannelswriteframesgetvalueclose)	r   r4   r5   r)   r
   r   wav_file
wav_writerwav_datas	            r   get_wav_datazAudioData.get_wav_data   s     $$\=A , 4D, 	 "/!6DM 	
 ZZ\ 		#X8T2J#''4''5''*&&x0#,,.  "		#    "		# s%   C#AC3C#C  C##C-c           
        | j                  ||      || j                  n|}|| j                  n|t        t        d      rt	        j
                        n>dz
  dd   dj                  fdt        dz
  t                    D              z   t        j                         5 }t        j                  |d      }	 |j                  |       |j                         |j                  d       |j!                         |j#                         }|j%                          	 ddd       |S # |j%                          w xY w# 1 sw Y   S xY w)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  4   K   | ]  }|z   |d      yw)rJ   Nr&   )r'   r(   r)   r   s     r   r*   z*AudioData.get_aiff_data.<locals>.<genexpr>   s,      B \)1b01Br+   r9   )r7   r
   r   hasattrr-   rI   r2   r3   r   r:   r;   aifcr=   r>   r?   r@   rA   rB   rC   )	r   r4   r5   r
   	aiff_fileaiff_writer	aiff_datar)   r   s	          @@r   get_aiff_datazAudioData.get_aiff_data   sR    $$\=A , 4D, 	 "/!6DM 	
 Z
 '',?Hq 0 52 56 B|a/XMB : H ZZ\ 		$Y))It4K$((5((6((+''1%..0	!!#		$  !!#		$ s%   .E
AD5E
5EE

Ec                   |"|dz  dk(  rd|cxk  rdk  sJ d        J d       | j                   dkD  r|d}| j                  ||      }t               }t        j                  dk(  rMt        j                         }|xj                  t
        j                  z  c_        t
        j                  |_
        nd}t        j                  |ddd	d
gt
        j                  t
        j                  |      }|j                  |      \  }}|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   rG   get_flac_converterosname
subprocessSTARTUPINFOdwFlagsSTARTF_USESHOWWINDOWSW_HIDEwShowWindowPopenPIPEcommunicate)	r   r4   r5   rF   flac_converterstartup_infoprocess	flac_datastderrs	            r   get_flac_datazAudioData.get_flac_data   s%    $A"qM'>Q'>	JI	J 
'>	JI	J 

 !m&;M $$\=A+-GGtO%113L  //  "" $  L""" //??$
 $//9	6r   )r   strreturnr   )NN)__name__
__module____qualname____doc__r   classmethodr   r"   r7   rG   rQ   ri   r&   r   r   r   r      s<    
. $ $
<FP>(T1r   r   c                    t        d      } | t        j                  j                  t        j                  j	                  t
                    }t        j                         t        j                         }}|dk(  r%|dv r!t        j                  j                  |d      } n|dk(  r%|dv r!t        j                  j                  |d      } n_|dk(  r%|d	v r!t        j                  j                  |d
      } n5|dk(  r%|dv r!t        j                  j                  |d      } nt        d      	 t        j                  | t        j                        st        j                  |       }t        j                  | |j                  t        j                   z         dt        j                         v r<t"        j$                  dk\  rt        j&                         nt        j                  d       | S # t        $ r Y | S w xY w)zdReturns the absolute path of a FLAC converter executable, or raises an OSError if none can be found.flacWindows>   x86i686i786AMD64x86_64zflac-win32.exeDarwin>   rt   ru   rv   rw   arm64rx   zflac-macLinux>   rt   ru   rv   zflac-linux-x86>   rw   rx   zflac-linux-x86_64zFLAC 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_whichrY   pathdirnameabspath__file__platformsystemmachiner2   OSErroraccessX_OKstatchmodst_modeS_IEXECsysversion_infor|   )rd   	base_pathr   r   	stat_infos        r   rX   rX     s   !&)NGGOOGGOOH%
	 #//+X-=-=-?Y7 /
 $
  WW\\)5EFNxG 0
 %
  WW\\)Z@Nw7.E#EWW\\)5EFNw7.A#AWW\\)5HIN x 
 yy1/IHH^Y%6%6%EF(//++ --7	RYYv=N
   s   +B<G) )	G65G6c                Z   t        j                  d      }|j                  t         j                  j                        D ]j  }t         j                  j                  ||       }t         j                  j                  |      sCt        j                  |t         j                        sh|c S  y)zDPython 2 compatibility: backport of ``shutil.which()`` from Python 3PATHN)	rY   getenvsplitr~   pathsepr2   existsr   r   )pgmr~   ps      r   r}   r}   C  sj    99VDZZ( GGLLC 77>>!1bgg!6Hr   )
__future__r   rM   r-   r:   rY   r   r   r[   r   r<   objectr   rX   r}   r&   r   r   <module>r      s=    "   	 	    
  D0fr   