L i/&ddlZddlZddlZddlmZmZddlZdede dejfdZ dde de de d ee d eee f d Z dde de d ee d eeee e fe fde d ee d eee fdZdde dee e fdefdZde fdZdZy)N)OptionalUnionbpayload sampling_ratereturnc |}d}d}dddd|d|d|d d d d g } tj|tjtj 5}|j|}dddd} t j| t j} | jddk(r t d| S#1swYQxYw#t$r}t d|d}~wwxYw)z? Helper function to read an audio file through ffmpeg. 1f32leffmpeg-izpipe:0-ac-ar-f -hide_banner -loglevelquietpipe:1)stdinstdoutNzFffmpeg was not found but is required to load audio files from filenamera 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.) subprocessPopenPIPE communicateFileNotFoundError ValueErrornp frombufferfloat32shape) rraracformat_for_conversionffmpeg_commandffmpeg_process output_streamerror out_bytesaudios h/mnt/ssd/data/python-lab/Trading/venv/lib/python3.12/site-packages/transformers/pipelines/audio_utils.py ffmpeg_readr* s ?B B#      N n   nJOOJOO \ A`n*66x@M Aa I MM)RZZ 0E {{1~ [  L A A nabhmmns/4B: B.B:.B73B:: C CCchunk_length_sr"ffmpeg_input_deviceffmpeg_additional_argsc#K|}d}|dk(rd}n|dk(rd}ntd|dtj}|dk(r d } |xsd } n#|d k(r d } |xsd } n|dk(rd} |xs t} |gn|}dd d d|d|d|ddddddg} | j |t t ||z|z} t| | } | D]}|yw)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 s16ler Unhandled format ` `. Please use `s16le` or `f32le`LinuxalsadefaultDarwin avfoundationz:defaultWindowsdshowNr rr r rz-fflagsnobufferrrrr)rplatformsystem_get_microphone_nameextendintround_ffmpeg_stream)rr+r"r,r-r r!size_of_sampler=format_input_r# chunk_leniteratoritems r)ffmpeg_microphonerI1s0D ?B B' ' )-.C-DDdeff __ F $1  8  $2  9 $>(<(>#9#ARG]        #N(01E-.89:^KIni8H sCCstream_chunk_sstride_length_sc#K||}n|}t|||||gn|}|dk(rtj} d} n'|dk(rtj} d} nt d|d||d z }t t ||z| z} t|t tfr||g}t t ||d z| z} t t ||d z| z} tjj}tj| }t|| | | fd D]n}tj|d| |d<|dd | z|dd | zf|d<||d<||z }tjj|d|zzkDrk|pyw)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.ndarray, "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/r0r r1r2r3r)secondsT)stridestreamraw)dtyperPr )rIrint16rrr@rA isinstancefloatdatetimenow timedeltachunk_bytes_iterr)rr+rJrKr"r,r-chunk_s microphonerSrCrF stride_left stride_right audio_timedeltarHs r)ffmpeg_microphone_liverbs`!  "3/%;%CrI_ J' ' ) -.C-DDdeff(1,E-.89:^KI/C<0*O<eMOA,>>?@>QKu]_Q-??@ANRL""&&(J   w /E Y \?Zcgh mmDKu=U N1  / N1  / X!._e    "Z"u*%< <   sFF rFrPrQc#tKd}|\}}||z|k\rtd|d|d|d}|D]i}||z }|rt||kr|df}|d||dd(t||k\s7||f}|d||d } |rd | d <| |}|||z |z d}t||k\r3kt||kDr||dfd } |rd | d <| yyw) 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 rNT)rRrPpartial)rRrPFre)rlen) rGrFrPrQaccr^r_ _stride_leftrRrHs r)r[r[s- C &K\!Y.CK=PRS_R``efoep q  LD s  c#h*"A&Fjy/VM Mc(i'& 5":I&A&+DO * )k1L@BCc(i' D  3x+|Q&78 #DO sAB8!2B8$B8buflenc#Kd} tj|tj|5} |jj |}|dk(rn|& dddy#1swYyxYw#t $r}t d|d}~wwxYww)zJ Internal function to create the generator of data through ffmpeg i)rbufsizerdNzHffmpeg was not found but is required to stream audio files from filename)rrrrreadrr)r#rirkr$rRr&s r)rBrBsGp   nZ__g V Zh$++008#:      pcdjoopsKB&A*(AA*BA'#A*&B'A** B3 A??BBcLgd} tj|dtjd}|jj Dcgc] }d|vs| }}|r*|dj dd}t d |d |S y cc}w#t$rt d Yy wxYw) z3 Retrieve the microphone name in Windows . )r z -list_devicestruerr:r Tzutf-8)textstderrencodingz(audio)r"rNzUsing microphone: zaudio=zOffmpeg was not found. Please install it or make sure it is in your system PATH.r6)rrunrrq splitlinessplitprintr)commandffmpeg_deviceslinemicrophone_linesmicrophone_names r)r>r>sKG a#d:??]de-;-B-B-M-M-OeTS\`dSdDee .q177rdr)rsF"$%$$ $T"))-26 RRRR"# R %T#Y/ Rp%)CG!()-26\\\SM\eE%,$7$>?@ \  \ "# \%T#Y/\~ # uS#X PT F p3 p rd