
    Vh%                     H   d dl Z d dl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mZ d Z G d d          Zd!d	Zd
 Zd Zd Zd Zd"dZd#dZ	 	 	 	 	 d$de	j        dej        eef         dededej        d         dej        d         dedej        d         fd ZdS )%    N)Path   )temp_filenamesc           
          t          j        dddt          |           ddddg          }t          j        |                    d                    S )	Nffprobe	-loglevelpanicz-print_formatjsonz-show_formatz-show_streamszutf-8)spcheck_outputstrr
   loadsdecode)pathstdout_datas     R/var/www/html/movieo_spanner_bot/venv/lib/python3.11/site-packages/demucs/audio.py
_read_infor      sP    /;D		?FNO#  K :k((11222    c                       e Zd ZdZdefdZd Zed             Zed             Z	ed             Z
d Zdd
ZddZdd ed          ddfdZdS )	AudioFilez
    Allows to read audio from any format supported by ffmpeg, as well as resampling or
    converting to mono on the fly. See :method:`read` for more details.
    r   c                 <    t          |          | _        d | _        d S N)r   r   _info)selfr   s     r   __init__zAudioFile.__init__!   s    JJ	


r   c                 L   d| j         fg}|                    d|                                 f           |                    d|                                 f           |                    dt	          |           f           d                    d |D                       }d| dS )	Nr   
sampleratechannelsstreamsz, c              3   *   K   | ]\  }}| d | V  dS )=N ).0namevalues      r   	<genexpr>z%AudioFile.__repr__.<locals>.<genexpr>*   s4       O O{tUD!2!25!2!2 O O O O O Or   z
AudioFile())r   appendr   r   lenjoin)r   featuresfeatures_strs      r   __repr__zAudioFile.__repr__%   s    TY'(t'8'89:::T]]__5666CII.///yy O Oh O O OOO+L++++r   c                 P    | j         t          | j                  | _         | j         S r   )r   r   r   r   s    r   infozAudioFile.info-   s#    :#DI..DJzr   c                 B    t          | j        d         d                   S )Nformatduration)floatr0   r/   s    r   r3   zAudioFile.duration3   s    TYx(4555r   c                 J    d t          | j        d                   D             S )Nc                 0    g | ]\  }}|d          dk    |S )
codec_typeaudior"   )r#   indexstreams      r   
<listcomp>z,AudioFile._audio_streams.<locals>.<listcomp>9   s6     
 
 
#eVl#w.. ...r   r   )	enumerater0   r/   s    r   _audio_streamszAudioFile._audio_streams7   s2    
 
'091E'F'F
 
 
 	
r   c                 *    t          | j                  S r   )r)   r=   r/   s    r   __len__zAudioFile.__len__>   s    4&'''r   r   c                 d    t          | j        d         | j        |                  d                   S )Nr   r   intr0   r=   r   r:   s     r   r   zAudioFile.channelsA   s)    49Y'(;F(CDZPQQQr   c                 d    t          | j        d         | j        |                  d                   S )Nr   sample_raterA   rC   s     r   r   zAudioFile.samplerateD   s)    49Y'(;F(CD]STTTr   Nc                    t          j        t          t          |                               |         }t	          |t           j                   }|r|g}|d}d}nOt          |p|                                 |z            }t          |dz   |p|                                 z            }t          t          |                    5 }	ddg}
|
ddgz  }
|r|
dt          |          gz  }
|
dt          | j                  gz  }
t          ||	          D ]Z\  }}|
d	d
| j        |          gz  }
||
dt          |          gz  }
|
ddgz  }
|
ddgz  }
||
dt          |          gz  }
|
|gz  }
[t          j        |
d           g }|	D ]}t          j        |t           j                  }t%          j        |          }|                    d|                                                                           }|t/          ||          }||dd|f         }|                    |           	 ddd           n# 1 swxY w Y   t%          j        |d          }|r|d         }|S )a,  
        Slightly more efficient implementation than stempeg,
        in particular, this will extract all stems at once
        rather than having to loop over one file multiple times
        for each stream.

        Args:
            seek_time (float):  seek time in seconds or None if no seeking is needed.
            duration (float): duration in seconds to extract or None to extract until the end.
            streams (slice, int or list): streams to extract, can be a single int, a list or
                a slice. If it is a slice or list, the output will be of size [S, C, T]
                with S the number of streams, C the number of channels and T the number of samples.
                If it is an int, the output will be [C, T].
            samplerate (int): if provided, will resample on the fly. If None, no resampling will
                be done. Original sampling rate can be obtained with :method:`samplerate`.
            channels (int): if 1, will convert to mono. We do not rely on ffmpeg for that
                as ffmpeg automatically scale by +3dB to conserve volume when playing on speakers.
                See https://sound.stackexchange.com/a/42710.
                Our definition of mono is simply the average of the two channels. Any other
                value will be ignored.
        Nr   ffmpegz-yr   r	   z-ssz-iz-mapz0:z-tz-threads1z-ff32lez-arT)check)dtype.r   )dim)nparrayranger)   
isinstancendarrayrB   r   r4   r   r   r   zipr=   r   runfromfilefloat32torch
from_numpyviewr   tconvert_audio_channelsr(   stack)r   	seek_timer3   r   r   r   singletarget_sizequery_duration	filenamescommandr:   filenamewavswavs                  r   readzAudioFile.readG   s   6 (5T++,,W5444 	 iGK!NNz>T__->->(JKKK"K!O
8WdooFWFW#XYYNCLL)) 	!Y&GW--G 3E3y>>22c$)nn--G$'$;$; & & F$F)<V)D$F$FGG!-c.&9&9::GJ,,D'?*)s:77GH:%F7$''''D% ! !k("*===&s++hhr4==??335577'0h??C*c<K</0CC    !%	! 	! 	! 	! 	! 	! 	! 	! 	! 	! 	! 	! 	! 	! 	!6 k$A&&& 	a&C
s   E&H::H>H>)r   )__name__
__module____qualname____doc__r   r   r-   propertyr0   r3   r=   r?   r   r   slicerf   r"   r   r   r   r      s        T    , , ,   X
 6 6 X6 
 
 X
( ( (R R R RU U U U U4[[E E E E E Er   r      c                     | j         ^ }}}||k    rn[|dk    r|                     dd          } n=|dk    r | j        g |||R  } n%||k    r| dd|ddf         } nt          d          | S )z.Convert audio to the given number of channels.r   T)rM   keepdim.Nz@The audio file has less channels than requested but is not mono.)shapemeanexpand
ValueError)re   r   rq   src_channelslengths        r   r[   r[      s    #&9 UL&x	Q hh2th,,			 cj2%226222		!	! #yy!!!#$ [\\\Jr   c                 N    t          | |          } t          j        | ||          S )zTConvert audio from a given samplerate to a target one and target number of channels.)r[   juliusresample_frac)re   from_samplerateto_samplerater   s       r   convert_audior|      s&    
 h
/
/C_mDDDr   c                 t    | j         j        r+|                     dd          dz                                  S | S )z,Convert audio to 16 bits integer PCM format.rL   r     )rK   is_floating_pointclamp_shortre   s    r   i16_pcmr      s9    
y" 

2q!!Y/66888
r   c                 L    | j         j        r| S |                                 dz  S )z*Convert audio to float 32 bits PCM format.r~   )rK   r   r4   r   s    r   f32_pcmr      s'    
y" )
yy{{i((r   c                 V    | j         j        rt          |           S t          |           S )zHConvert audio to either f32 pcm or i16 pcm depending on the given dtype.)rK   r   r   r   )re   rK   s     r   as_dtype_pcmr      s'    
y" s||s||r   D  @  Fc                    | j         \  }}t          |           } t          j                    }|                    |           |                    |           |                    |           |                    |           |s|                                 | j	        
                                } |                     dd                                          } |                    |                                           }	|	|                                z  }	t!          |d          5 }
|
                    |	           ddd           dS # 1 swxY w Y   dS )z6Save given audio as mp3. This should work on all OSes.r   r   wbN)rq   r   lameencEncoderset_bit_rateset_in_sample_rateset_channelsset_qualitysilencedatacpu	transposenumpyencodetobytesflushopenwrite)re   r   r   bitratequalityverboseCTencodermp3_datafs              r   
encode_mp3r      si   9DAq
#,,CoG!!!z***    
(,,..C
--1


#
#
%
%C~~ckkmm,,HH	dD		 Q	                 s   &E		EErescalec                 \   ||dk    r| S | j         j        s
J d            |dk    r;| t          d|                                                                 z  d          z  } nJ|dk    r|                     dd	          } n-|d
k    rt          j        |           } nt          d|           | S )z9
    different strategies for avoiding raw clipping.
    Nnoneztoo late for clippingr   g)\(?r   clampgGzgGz?tanhzInvalid mode )rK   r   maxabsr   rW   r   rt   )re   modes     r   prevent_clipr      s     |tv~~
9&??(???&yCswwyy}}.222	iit$$	joo///000Jr      re   r   r   r   clip)r   r   r   r   bits_per_sample)r          as_floatpreset)rm                  c                    t          | |          } t          |          }|j                                        }|dk    rt	          | ||||d           dS |dk    r1|rd}d}	nd}	t          j        t          |          | ||	|	           dS |d
k    r't          j        t          |          | ||           dS t          d|           )a  Save audio file, automatically preventing clipping if necessary
    based on the given `clip` strategy. If the path ends in `.mp3`, this
    will save as mp3 with the given `bitrate`. Use `preset` to set mp3 quality:
    2 for highest quality, 7 for fastest speed
    )r   z.mp3T)r   z.wavr   PCM_FPCM_S)rE   encodingr   z.flac)rE   r   zInvalid suffix for path: N)	r   r   suffixlowerr   tasaver   rt   )
re   r   r   r   r   r   r   r   r   r   s
             r   
save_audior      s    s
&
&
&C::D[  F3j'64HHHHHH	6		 	 OHHH
D		3J!?	D 	D 	D 	D 	D 	D	7		
D		3JXXXXXX=V==>>>r   )rm   )r   r   rm   F)r   )r   r   r   Frm   ) r
   
subprocessr   pathlibr   r   rx   r   rN   rW   
torchaudior   typingtputilsr   r   r   r[   r|   r   r   r   r   r   TensorUnionr   rB   Literalboolr   r"   r   r   <module>r      s                             ! ! ! ! ! !3 3 3p p p p p p p pf   4E E E  ) ) )     &   * "FO9; %67? ?EL ?Xc4i(?? ? Z BC	?
 !#
: 6? ? z"23? ? ? ? ? ?r   