
    g/                     d   d dl Z d dlZd dlZd dlmZmZmZ d dlZde	de
dej        fdZ	 	 	 dde
ded	ed
ee         deee                  f
dZ	 	 	 	 	 dde
dedee
         deeeeef         ef                  d	ed
ee         deee                  fdZdde
dee
e
f         defdZde
fdZd ZdS )    N)OptionalTupleUnionbpayloadsampling_ratereturnc                    | }d}d}dddd|d|d|d	d
ddg}	 t          j        |t           j        t           j                  5 }|                    |           }ddd           n# 1 swxY w Y   n"# t          $ r}t          d          |d}~ww xY w|d         }	t          j        |	t          j                  }
|
j	        d         dk    rt          d          |
S )z?
    Helper function to read an audio file through ffmpeg.
    1f32leffmpeg-izpipe:0-ac-ar-f-hide_banner	-loglevelquietpipe:1)stdinstdoutNzFffmpeg was not found but is required to load audio files from filenamer   a  Soundfile is either not in the correct format or is malformed. Ensure that the soundfile has a valid audio file extension (e.g. wav, flac or mp3) and is not corrupted. If reading from a remote URL, ensure that the URL is the full address to **download** the audio file.)

subprocessPopenPIPEcommunicateFileNotFoundError
ValueErrornp
frombufferfloat32shape)r   r   aracformat_for_conversionffmpeg_commandffmpeg_processoutput_streamerror	out_bytesaudios              ^/var/www/html/ai-engine/env/lib/python3.11/site-packages/transformers/pipelines/audio_utils.pyffmpeg_readr+   
   s`    	B	B#

N nnJOJO\\\ 	A`n*66x@@M	A 	A 	A 	A 	A 	A 	A 	A 	A 	A 	A 	A 	A 	A 	A n n nabbhmmna IM)RZ00E{1~[
 
 	

 Ls;   +A1 A%A1 %A))A1 ,A)-A1 1
B;BBr   chunk_length_sr#   ffmpeg_input_deviceffmpeg_additional_argsc              #     K   |  }d}|dk    rd}n|dk    rd}nt          d| d          t          j                    }|dk    rd	}	|pd
}
n%|dk    rd}	|pd}
n|dk    rd}	|pt                      }
|g n|}dd|	d|
d|d|d|ddddddg}|                    |           t          t          | |z                      |z  }t          ||          }|D ]}|V  dS )a  
    Helper function to read audio from a microphone using ffmpeg. The default input device will be used unless another
    input device is specified using the `ffmpeg_input_device` argument. Uses 'alsa' on Linux, 'avfoundation' on MacOS and
    'dshow' on Windows.

    Arguments:
        sampling_rate (`int`):
            The sampling_rate to use when reading the data from the microphone. Try using the model's sampling_rate to
            avoid resampling later.
        chunk_length_s (`float` or `int`):
            The length of the maximum chunk of audio to be sent returned.
        format_for_conversion (`str`, defaults to `f32le`):
            The name of the format of the audio samples to be returned by ffmpeg. The standard is `f32le`, `s16le`
            could also be used.
        ffmpeg_input_device (`str`, *optional*):
            The indentifier of the input device to be used by ffmpeg (i.e. ffmpeg's '-i' argument). If unset,
            the default input device will be used. See `https://www.ffmpeg.org/ffmpeg-devices.html#Input-Devices`
            for how to specify and list input devices.
        ffmpeg_additional_args (`list[str]`, *optional*):
            Additional arguments to pass to ffmpeg, can include arguments like -nostdin for running as a background
            process. For example, to pass -nostdin to the ffmpeg process, pass in ["-nostdin"]. If passing in flags
            with multiple arguments, use the following convention (eg ["flag", "arg1", "arg2]).

    Returns:
        A generator yielding audio chunks of `chunk_length_s` seconds as `bytes` objects of length
        `int(round(sampling_rate * chunk_length_s)) * size_of_sample`.
    r
   s16le   r      Unhandled format ` `. Please use `s16le` or `f32le`LinuxalsadefaultDarwinavfoundationz:defaultWindowsdshowNr   r   r   r   r   z-fflagsnobufferr   r   r   r   )r   platformsystem_get_microphone_nameextendintround_ffmpeg_stream)r   r,   r#   r-   r.   r!   r"   size_of_sampler>   format_input_r$   	chunk_leniteratoritems                  r*   ffmpeg_microphonerJ   1   sp     D 	B	B''	'	)	)e.Ceeefff_F$1		8		 $2
	9		$>(<(>(>#9#ARRG] 	

#N( 0111E-.899::^KIni88H  



     stream_chunk_sstride_length_sc              #     K   ||}n|}t          | ||||g n|          }|dk    rt          j        }	d}
n(|dk    rt          j        }	d}
nt	          d| d          ||d	z  }t          t          | |z                      |
z  }t          |t
          t          f          r||g}t          t          | |d
         z                      |
z  }t          t          | |d         z                      |
z  }t          j	        
                                }t          j        |          }t          ||||fd          D ]}}t          j        |d         |	          |d<   |d         d
         |
z  |d         d         |
z  f|d<   | |d<   ||z  }t          j	        
                                |d|z  z   k    ry|V  ~dS )a
  
    Helper function to read audio from a microphone using ffmpeg. This will output `partial` overlapping chunks starting
    from `stream_chunk_s` (if it is defined) until `chunk_length_s` is reached. It will make use of striding to avoid
    errors on the "sides" of the various chunks. The default input device will be used unless another input device is
    specified using the `ffmpeg_input_device` argument. Uses 'alsa' on Linux, 'avfoundation' on MacOS and 'dshow' on Windows.

    Arguments:
        sampling_rate (`int`):
            The sampling_rate to use when reading the data from the microphone. Try using the model's sampling_rate to
            avoid resampling later.
        chunk_length_s (`float` or `int`):
            The length of the maximum chunk of audio to be sent returned. This includes the eventual striding.
        stream_chunk_s (`float` or `int`):
            The length of the minimal temporary audio to be returned.
        stride_length_s (`float` or `int` or `(float, float)`, *optional*):
            The length of the striding to be used. Stride is used to provide context to a model on the (left, right) of
            an audio sample but without using that part to actually make the prediction. Setting this does not change
            the length of the chunk.
        format_for_conversion (`str`, *optional*, defaults to `f32le`):
            The name of the format of the audio samples to be returned by ffmpeg. The standard is `f32le`, `s16le`
            could also be used.
        ffmpeg_input_device (`str`, *optional*):
            The identifier of the input device to be used by ffmpeg (i.e. ffmpeg's '-i' argument). If unset,
            the default input device will be used. See `https://www.ffmpeg.org/ffmpeg-devices.html#Input-Devices`
            for how to specify and list input devices.
        ffmpeg_additional_args (`list[str]`, *optional*):
            Additional arguments to pass to ffmpeg, can include arguments like -nostdin for running as a background
            process. For example, to pass -nostdin to the ffmpeg process, pass in ["-nostdin"]. If passing in flags
            with multiple arguments, use the following convention (eg ["flag", "arg1", "arg2]).

    Return:
        A generator yielding dictionaries of the following form

        `{"sampling_rate": int, "raw": np.array(), "partial" bool}` With optionally a `"stride" (int, int)` key if
        `stride_length_s` is defined.

        `stride` and `raw` are all expressed in `samples`, and `partial` is a boolean saying if the current yield item
        is a whole chunk, or a partial temporary result to be later replaced by another larger chunk.
    N)r#   r-   r.   r0   r1   r   r2   r3   r4      r      )secondsT)stridestreamraw)dtyperR   r   
   )rJ   r   int16r   r   rA   rB   
isinstancefloatdatetimenow	timedeltachunk_bytes_iterr   )r   r,   rL   rM   r#   r-   r.   chunk_s
microphonerU   rD   rG   stride_leftstride_right
audio_timedeltarI   s                    r*   ffmpeg_microphone_liverd      s     ` !  "3/%;%CrrI_  J ''	'	)	)
e.Ceeefff(1,E-.899::^KI/C<00 =*O<eMOA,>>??@@>QKu]_Q-??@@AANRL"&&((Jw///E Y\?Zcghhh  mDKu===UN1/N1/
X !._e
  ""Z"u*%<<<



 rK   FrG   rR   rS   c              #     K   d}|\  }}||z   |k    rt          d| d| d|           d}| D ]}||z  }|r(t          |          |k     r|df}|d|         |ddV  1t          |          |k    rA||f}|d|         |d	}	|rd
|	d<   |	V  |}|||z
  |z
  d         }t          |          |k    At          |          |k    r||dfd	}	|rd
|	d<   |	V  dS dS )z
    Reads raw bytes from an iterator and does chunks of length `chunk_len`. Optionally adds `stride` to each chunks to
    get overlaps. `stream` is used to return partial results even if a full `chunk_len` is not yet available.
    rK   z5Stride needs to be strictly smaller than chunk_len: (z, z) vs r   NT)rT   rR   partial)rT   rR   Frf   )r   len)
rH   rG   rR   rS   accr`   ra   _stride_leftrT   rI   s
             r*   r]   r]      s     
 C &K\!Y..qKqqS_qqfoqq
 
 	
 L D Ds
 	Dc#hh**"A&Fjyj/VMMMMMMc((i''&5":I:&AA ,&+DO


*)k1L@BBC c((i'' 3xx+|Q&788 	$#DO




	 rK   buflenc              #     K   d}	 t          j        | t           j        |          5 }	 |j                            |          }|dk    rn|V  &	 ddd           dS # 1 swxY w Y   dS # t
          $ r}t          d          |d}~ww xY w)zJ
    Internal function to create the generator of data through ffmpeg
    i   )r   bufsizeTrK   NzHffmpeg was not found but is required to stream audio files from filename)r   r   r   r   readr   r   )r$   rj   rl   r%   rT   r'   s         r*   rC   rC     s       GpnZ_gVVV 	Zh$+0088#::				 		 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	  p p pcddjoops:   !A* (AA* A!!A* $A!%A* *
B	4BB	c                  N   g d} 	 t          j        | dt           j        d          }d |j                                        D             }|r8|d                             d          d         }t          d	|            d
| S n# t          $ r t          d           Y nw xY wdS )z3
    Retrieve the microphone name in Windows .
    )r   z-list_devicestruer   r;   r    Tzutf-8)textstderrencodingc                     g | ]}d |v |	S )z(audio) ).0lines     r*   
<listcomp>z(_get_microphone_name.<locals>.<listcomp>   s$    eeeTS\`dSdSdDSdSdSdrK   r   "rP   zUsing microphone: zaudio=zOffmpeg was not found. Please install it or make sure it is in your system PATH.r7   )r   runr   rr   
splitlinessplitprintr   )commandffmpeg_devicesmicrophone_linesmicrophone_names       r*   r?   r?     s     KJJG	a#d:?]deeeee^-B-M-M-O-Oeee 	..q177<<Q?O888999-O---	.  a a a_`````a 9s   A>B B"!B")r   NN)NNr   NN)F)rZ   r=   r   typingr   r   r   numpyr   bytesrA   arrayr+   rY   strlistrJ   rd   boolr]   rC   r?   ru   rK   r*   <module>r      s         ) ) ) ) ) ) ) ) ) )    $% $ $ $ $ $ $T "))-26R RRR R "#	R
 %T#Y/R R R Rp %)CG!()-26\ \\\ SM\ eE%,$7$>?@	\
 \ "#\ %T#Y/\ \ \ \~   #  uS#X  PT        Fp3 p p p p     rK   