o
    h/                     @   s  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dZ			dde
d	ed
edee deee  f
d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dZd de
dee
e
f defddZde
fddZdd ZdS )!    N)OptionalTupleUnionbpayloadsampling_ratereturnc                 C   s   | }d}d}dddd|d|d|d	d
ddg}z!t j|t jt jd}|| }W d   n1 s1w   Y  W n tyH } ztd|d}~ww |d }	t|	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audio r(   v/var/www/html/construction_image-detection-poc/venv/lib/python3.10/site-packages/transformers/pipelines/audio_utils.pyffmpeg_read
   sB   
r*   r	   chunk_length_sr!   ffmpeg_input_deviceffmpeg_additional_argsc                 c   s    |  }d}|dkrd}n|dkrd}nt d| dt }|dkr+d	}	|p)d
}
n|dkr6d}	|p4d}
n|dkrAd}	|p@t }
|du rGg n|}dd|	d|
d|d|d|ddddddg}|| tt| | | }t||}|D ]}|V  qrd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 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]).

    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itemr(   r(   r)   ffmpeg_microphone1   sV   "




rH   stream_chunk_sstride_length_sc                 c   sp   |dur|}n|}t | ||||du rg n|d}|dkr#tj}	d}
n|dkr-tj}	d}
ntd| d|du r=|d	 }tt| | |
 }t|ttfrR||g}tt| |d
  |
 }tt| |d  |
 }t	j	
 }t	j|d}t||||fddD ]6}tj|d |	d|d< |d d
 |
 |d d |
 f|d< | |d< ||7 }t	j	
 |d|  krq|V  q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-   r.   r/   r	   r0   r1   r2      r      )secondsT)stridestreamraw)dtyperN   r   
   )rH   r   int16r   r   r?   r@   
isinstancefloatdatetimenow	timedeltachunk_bytes_iterr   )r   r+   rI   rJ   r!   r,   r-   chunk_s
microphonerQ   rB   rE   stride_leftstride_right
audio_timedeltarG   r(   r(   r)   ffmpeg_microphone_live   sL   0
r`   FrE   rN   rO   c           
      c   s
   d}|\}}|| |krt d| d| d| d}| D ]L}||7 }|r<t||k r<|df}|d| |ddV  qt||krj||f}|d| |d	}	|rUd
|	d< |	V  |}||| | d }t||ksBqt||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.
        z5Stride needs to be strictly smaller than chunk_len: (z, z) vs r   NT)rP   rN   partial)rP   rN   Frb   )r   len)
rF   rE   rN   rO   accr\   r]   _stride_leftrP   rG   r(   r(   r)   rY      s:   

rY   buflenc              
   c   s    d}z.t j| t j|d}	 |j|}|dkrn|V  qW d   W dS 1 s*w   Y  W dS  tyB } ztd|d}~ww )zJ
    Internal function to create the generator of data through ffmpeg
    i   )r   bufsizeTra   NzHffmpeg was not found but is required to stream audio files from filename)r   r   r   r   readr   r   )r"   rf   rg   r#   rP   r%   r(   r(   r)   rA     s    &
rA   c                  C   s   g d} z/t j| dt jdd}dd |j D }|r1|d dd	 }td
|  d| W S W dS  tyA   td Y dS w )z3
    Retrieve the microphone name in Windows .
    )r
   z-list_devicestruer   r9   r    Tzutf-8)textstderrencodingc                 S   s   g | ]}d |v r|qS )z(audio)r(   ).0liner(   r(   r)   
<listcomp>   s    z(_get_microphone_name.<locals>.<listcomp>r   "rL   zUsing microphone: zaudio=zOffmpeg was not found. Please install it or make sure it is in your system PATH.r5   )r   runr   rl   
splitlinessplitprintr   )commandffmpeg_devicesmicrophone_linesmicrophone_namer(   r(   r)   r=     s   
r=   )r	   NN)NNr	   NN)F)rV   r;   r   typingr   r   r   numpyr   bytesr?   arrayr*   rU   strlistrH   r`   boolrY   rA   r=   r(   r(   r(   r)   <module>   sV   *

X

 _#