
    1h3;                    z    S SK Jr  S SKrS SKrS SKrS SKrS SKrS SKrS SKrS SK	r	S SK
r
 " S S\5      rS rS rg)    )annotationsNc                  d    \ rS rSrSrS r\SS j5       rSS jrSS jr	SS jr
SS	 jrSS
 jrSrg)	AudioData   a  
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                    US:  d   S5       eUS-  S:X  a  SUs=::  a  S::  d    S5       e   S5       eXl         X l        [        U5      U l        g )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       P/var/www/auris/envauris/lib/python3.13/site-packages/speech_recognition/audio.py__init__AudioData.__init__   sh    QH HH1!a<&<1&<	<;	<<&<	<;	<<$&-    c                    SSK nUR                  5       nUR                  U5       nUR                  U5      sSSS5        $ ! , (       d  f       g= f)z8Creates a new ``AudioData`` instance from an audio file.r   N)speech_recognition
Recognizer	AudioFilerecord)cls	file_pathsrrsources        r   	from_fileAudioData.from_file$   s8     	(MMO\\)$88F# %$$s   A
ANc                   Ub  US:  d   S5       eUb  UUc  SOU:  d   S5       eUc  SnO'[        XR                  -  U R                  -  S-  5      nUc  [        U R                  5      nO'[        X R                  -  U R                  -  S-  5      n[        U R                  X4 U R                  U R                  5      $ )aO  
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_segmentAudioData.get_segment-   s     A	87	8-~!Ax"
 	WV	W 
 J,,,t/@/@@TIJ >4??+H***T->->>4GH OOJ0
 	
r   c           
     l  ^ Ub  US:  d   S5       eUb&  US-  S:X  a  SUs=::  a  S::  d    S5       e   S5       eU R                   mU R                  S:X  a  [        R                  " TSS5      mUbB  U R                  U:w  a2  [        R
                  " TU R                  SU R                  US5      u  mnUb  U R                  U:w  a  US:X  a^  [        R                  " TU R                  S5      m [        R                  " S	SS5        [        R                  " TU R                  U5      mO"[        R                  " TU R                  U5      mUS:X  a  [        R                  " TSS5      mT$ ! [        R                   a2    S	R                  U4S
 j[        S[        T5      S5       5       5      m Nef = f)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  :   >#    U  H  nTUS -   US-    v   M     g7f)r   r	   N ).0iraw_datas     r   	<genexpr>)AudioData.get_raw_data.<locals>.<genexpr>~   s'      (!;A !QA.!;      )r
   r   audioopbiasr   ratecvlin2linerrorjoinranger    )r   convert_rateconvert_width_r-   s       @r   get_raw_data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   0E- -AF32F3c                   U R                  X5      nUc  U R                  OUnUc  U R                  OUn[        R                  " 5        n[
        R                  " US5      n UR                  U5        UR                  U5        UR                  S5        UR                  U5        UR                  5       nUR                  5          SSS5        U$ ! UR                  5         f = f! , (       d  f       W$ = f)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   )r<   r   r   ioBytesIOwaveopensetframeratesetsampwidthsetnchannelswriteframesgetvalueclose)	r   r9   r:   r-   r   r   wav_file
wav_writerwav_datas	            r   get_wav_dataAudioData.get_wav_data   s     $$\A , 4D, 	 "/!6DM 	
 ZZ\X8T2J#''4''5''*&&x0#,,.  "     " \ s%   	C&"AC6C&C##C&&
C5c           
       ^^ U R                  X5      mUc  U R                  OUnUc  U R                  OUm[        [        S5      (       a  [        R
                  " TT5      mO?TTS-
  SS2   SR                  UU4S j[        TS-
  [        T5      T5       5       5      -   m[        R                  " 5        n[        R                  " US5      n UR                  U5        UR                  T5        UR                  S5        UR!                  T5        UR#                  5       nUR%                  5          SSS5        U$ ! UR%                  5         f = f! , (       d  f       W$ = f)a  
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  :   >#    U  H  nTUT-   US 2   v   M     g7f)rQ   Nr*   )r+   r,   r-   r   s     r   r.   *AudioData.get_aiff_data.<locals>.<genexpr>   s*      BMA \)1b01Mr0   r?   )r<   r   r   hasattrr2   rP   r7   r8   r    r@   rA   aifcrC   rD   rE   rF   rG   rH   rI   )	r   r9   r:   r   	aiff_fileaiff_writer	aiff_datar-   r   s	          @@r   get_aiff_data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%   7EAD?$E?EE
E#c                   Ub&  US-  S:X  a  SUs=::  a  S::  d    S5       e   S5       eU R                   S:  a  Uc  SnU R                  X5      n[        5       n[        R                  S:X  aN  [
        R                  " 5       nU=R                  [
        R                  -  sl        [
        R                  Ul
        OSn[
        R                  " USSS	S
/[
        R                  [
        R                  US9nUR                  U5      u  pxU$ )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   rM   get_flac_converterosname
subprocessSTARTUPINFOdwFlagsSTARTF_USESHOWWINDOWSW_HIDEwShowWindowPopenPIPEcommunicate)	r   r9   r:   rL   flac_converterstartup_infoprocess	flac_datastderrs	            r   get_flac_dataAudioData.get_flac_data   s!    $A"qM'>Q'>	JI	J 
'>	JI	J 

 !m&;M $$\A+-GGtO%113L  //  "" $  L""" //??$
 $//9	r   )r
   r   r   )r   strreturnr   )NN)__name__
__module____qualname____firstlineno____doc__r   classmethodr   r%   r<   rM   rY   rr   __static_attributes__r*   r   r   r   r      s<    
. $ $
<FP>(T1r   r   c                 *   [        S5      n U Gc(  [        R                  R                  [        R                  R	                  [
        5      5      n[        R                  " 5       [        R                  " 5       p2US:X  a'  US;   a!  [        R                  R                  US5      n OUS:X  a'  US;   a!  [        R                  R                  US5      n OeUS:X  a'  US	;   a!  [        R                  R                  US
5      n O8US:X  a'  US;   a!  [        R                  R                  US5      n O[        S5      e [        R                  " U [        R                  5      (       d  [        R                  " U 5      n[        R                  " XR                  [        R                   -  5        S[        R                  " 5       ;   a?  ["        R$                  S:  a  [        R&                  " 5       O[        R                  " S5        U $ ! [         a     U $ f = f)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>   r   r   r   r   arm64r   zflac-macLinux>   r   r   r   zflac-linux-x86>   r   r   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_whichrb   pathdirnameabspath__file__platformsystemmachiner7   OSErroraccessX_OKstatchmodst_modeS_IEXECsysversion_infor   )rm   	base_pathr   r   	stat_infos        r   ra   ra     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1/IHH^%6%6%EF(//++ --7	RYYv=N
   s   9C	H 
HHc                v   [         R                  " S5      nUR                  [         R                  R                  5       Hv  n[         R                  R                  X 5      n[         R                  R                  U5      (       d  MH  [         R                  " U[         R                  5      (       d  Mt  Us  $    g)zDPython 2 compatibility: backport of ``shutil.which()`` from Python 3PATHN)	rb   getenvsplitr   pathsepr7   existsr   r   )pgmr   ps      r   r   r   C  si    99VDZZ(GGLL 77>>!1bgg!6!6H )r   )
__future__r   rU   r2   r@   rb   r   r   rd   r   rB   objectr   ra   r   r*   r   r   <module>r      s=    "   	 	    
  D0fr   