
    ]i%                     d   S SK r S SKr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Jr  S r " S S5      rSS jrS	 rS
 rS rS rSS jrSS jr     SS\	R2                  S\R4                  \\4   S\S\S\R:                  S   S\R:                  S   S\S\R:                  S   4S jjrg)    N)Path   )temp_filenamesc           
          [         R                  " SSS[        U 5      SSSS/5      n[        R                  " UR                  S5      5      $ )	Nffprobe	-loglevelpanicz-print_formatjsonz-show_formatz-show_streamszutf-8)spcheck_outputstrr
   loadsdecode)pathstdout_datas     I/mnt/rpi/tmp/demucs-venv-sys/lib/python3.13/site-packages/demucs/audio.py
_read_infor      sG    //;D	?FNO# K ::k((122    c                       \ rS rSrSrS\4S jrS r\S 5       r	\S 5       r
\S 5       rS	 rSS
 jrSS jrSS\" S5      SS4S jrSrg)	AudioFile   z
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                 2    [        U5      U l        S U l        g N)r   r   _info)selfr   s     r   __init__AudioFile.__init__!   s    J	
r   c                    SU R                   4/nUR                  SU R                  5       45        UR                  SU R                  5       45        UR                  S[	        U 5      45        SR                  S U 5       5      nSU S3$ )	Nr   
sampleratechannelsstreamsz, c              3   4   #    U  H  u  pU S U 3v   M     g7f)=N ).0namevalues      r   	<genexpr>%AudioFile.__repr__.<locals>.<genexpr>*   s      Oh{tD65'!2hs   z
AudioFile())r   appendr   r    lenjoin)r   featuresfeatures_strs      r   __repr__AudioFile.__repr__%   sz    TYY'(t'89:T]]_56CI./yy Oh OOL>++r   c                 h    U R                   c  [        U R                  5      U l         U R                   $ r   )r   r   r   r   s    r   infoAudioFile.info-   s&    ::#DII.DJzzr   c                 8    [        U R                  S   S   5      $ )Nformatduration)floatr4   r3   s    r   r8   AudioFile.duration3   s    TYYx(455r   c                 ~    [        U R                  S   5       VVs/ s H  u  pUS   S:X  d  M  UPM     snn$ s  snnf )Nr!   
codec_typeaudio)	enumerater4   )r   indexstreams      r   _audio_streamsAudioFile._audio_streams7   sF     (191E'F
'Fmel#w. 'F
 	
 
s   99c                 ,    [        U R                  5      $ r   )r,   rA   r3   s    r   __len__AudioFile.__len__>   s    4&&''r   c                 X    [        U R                  S   U R                  U      S   5      $ )Nr!   r    intr4   rA   r   r@   s     r   r    AudioFile.channelsA   s+    499Y'(;(;F(CDZPQQr   c                 X    [        U R                  S   U R                  U      S   5      $ )Nr!   sample_raterG   rI   s     r   r   AudioFile.samplerateD   s+    499Y'(;(;F(CD]STTr   Nc                    [         R                  " [        [        U 5      5      5      U   n[	        U[         R
                  5      (       + nU(       a  U/nUc  SnSnOM[        U=(       d    U R                  5       U-  5      n[        US-   U=(       d    U R                  5       -  5      n[        [        U5      5       n	SS/n
U
SS/-  n
U(       a  U
S[        U5      /-  n
U
S[        U R                  5      /-  n
[        X95       HU  u  pU
S	S
U R                  U    3/-  n
Ub  U
S[        U5      /-  n
U
SS/-  n
U
SS/-  n
Ub  U
S[        U5      /-  n
X/-  n
MW     [        R                  " U
SS9  / nU	 H  n[         R                   " U[         R"                  S9n[$        R&                  " U5      nUR)                  SU R+                  5       5      R-                  5       nUb  [/        X5      nUb	  USSU24   nUR1                  U5        M     SSS5        [$        R2                  " WSS9nU(       a  US   nU$ ! , (       d  f       N1= f)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ndarrayrH   r   r9   r   r   r   ziprA   r   runfromfilefloat32torch
from_numpyviewr    tconvert_audio_channelsr+   stack)r   	seek_timer8   r!   r   r    singletarget_sizequery_duration	filenamescommandr@   filenamewavswavs                  r   readAudioFile.readG   s1   6 ((5T+,W544iGK!Nz>T__->(JKK"K!O
8WdooFW#XYNCL)Y&GW--GE3y>22c$))n--G$'$; Fb)<)<V)D(E$FGG!-c.&9::GJ,,D'?*)s:77G:% %< FF7$'D%kk("**=&&s+hhr4==?3557'0?C*c<K</0CC  &% *6 kk$A&a&C
= *)s   >EH<<
I
)r   r   )r   )__name__
__module____qualname____firstlineno____doc__r   r   r0   propertyr4   r8   rA   rD   r    r   slicern   __static_attributes__r$   r   r   r   r      s    T ,  
 6 6 
 
(RU 4[Er   r   c                     U R                   Gt p#nX1:X  a   U $ US:X  a  U R                  SSS9n U $ US:X  a  U R                  " / UQUPUP76 n U $ X1:  a  U SSU2SS24   n U $ [        S5      e)z.Convert audio to the given number of channels.r   T)rU   keepdim.Nz@The audio file has less channels than requested but is not mono.)shapemeanexpand
ValueError)rm   r    r{   src_channelslengths        r   rc   rc      s    #&99 U&& J% 
Q hh2th, J 
	 jj2%2262 J 
	! #yy!#$ J [\\r   c                 F    [        X5      n [        R                  " XU5      $ )zTConvert audio from a given samplerate to a target one and target number of channels.)rc   juliusresample_frac)rm   from_samplerateto_samplerater    s       r   convert_audior      s    
 
/CmDDr   c                     U R                   R                  (       a#  U R                  SS5      S-  R                  5       $ U $ )z,Convert audio to 16 bits integer PCM format.rT   r     )rS   is_floating_pointclamp_shortrm   s    r   i16_pcmr      s3    
yy""

2q!Y/6688
r   c                 b    U R                   R                  (       a  U $ U R                  5       S-  $ )z*Convert audio to float 32 bits PCM format.r   )rS   r   r9   r   s    r   f32_pcmr      s%    
yy""
yy{i((r   c                 d    U R                   R                  (       a  [        U 5      $ [        U 5      $ )zHConvert audio to either f32 pcm or i16 pcm depending on the given dtype.)rS   r   r   r   )rm   rS   s     r   as_dtype_pcmr      s#    
yy""s|s|r   c                 Z   U R                   u  pg[        U 5      n [        R                  " 5       nUR	                  U5        UR                  U5        UR                  U5        UR                  U5        U(       d  UR                  5         U R                  R                  5       n U R                  SS5      R                  5       n UR                  U R                  5       5      n	XR                  5       -  n	[!        US5       n
U
R#                  U	5        SSS5        g! , (       d  f       g= f)z6Save given audio as mp3. This should work on all OSes.r   r   wbN)r{   r   lameencEncoderset_bit_rateset_in_sample_rateset_channelsset_qualitysilencedatacpu	transposenumpyencodetobytesflushopenwrite)rm   r   r   bitratequalityverboseCTencodermp3_datafs              r   
encode_mp3r      s    99DA
#,CooG!z* 
((,,.C
--1

#
#
%C~~ckkm,HH	dD	Q	 
		s   D
D*c                 T   Ub  US:X  a  U $ U R                   R                  (       d   S5       eUS:X  a0  U [        SU R                  5       R                  5       -  S5      -  n U $ US:X  a  U R	                  SS5      n U $ US	:X  a  [
        R                  " U 5      n U $ [        S
U 35      e)z1
different strategies for avoiding raw clipping.
noneztoo late for clippingrescaleg)\(?r   clampgGzgGz?tanhzInvalid mode )rS   r   maxabsr   r_   r   r~   )rm   modes     r   prevent_clipr      s     |tv~
99&&?(??&yCswwy}}.22 J 
iit$
 J	 
jjo J =/00r   rm   r   r   r   clip)r   r   r   r   bits_per_sample)          as_floatpreset)                  c           	      T   [        XS9n [        U5      nUR                  R                  5       nUS:X  a  [	        XX#USS9  gUS:X  a.  U(       a  SnSn	OSn	[
        R                  " [        U5      XXS	9  gUS
:X  a   [
        R                  " [        U5      XUS9  g[        SU 35      e)zSave 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)rL   encodingr   z.flac)rL   r   zInvalid suffix for path: N)	r   r   suffixlowerr   tasaver   r~   )
rm   r   r   r   r   r   r   r   r   r   s
             r   
save_audior      s     s
&C:D[[ F3j64H	6	 OHH
D	3!	D	7	
D	3X4VH=>>r   )r   )iD  @  r   F)r   )r   r   r   Fr   ) r
   
subprocessr   pathlibr   r   r   r   rV   r_   
torchaudior   typingtputilsr   r   r   rc   r   r   r   r   r   r   TensorUnionr   rH   Literalboolr   r$   r   r   <module>r      s             !3p pf4E)&* "FO9; %67?ELL ?XXc4i(?? ? ZZ BC	?
 !#

: 6? ? zz"23?r   