
    mi
                     X    d 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	  G d d      Z
ddZy)	z@Utility for on the fly pitch/tempo change for data augmentation.    N   )
save_audioc                   2    e Zd ZdZdddddgdfdZd	 Zd
 Zy)RepitchedWrapperzA
    Wrap a dataset to apply online change of pitch / tempo.
    g?            Tc                 f    || _         || _        || _        || _        || _        || _        || _        y N)datasetproba	max_pitch	max_tempo	tempo_stdsamevocals)selfr   r   r   r   r   r   r   s           H/var/www/stems/demucs_env/lib/python3.12/site-packages/demucs/repitch.py__init__zRepitchedWrapper.__init__   s3    
"""	    c                 ,    t        | j                        S r   )lenr   )r   s    r   __len__zRepitchedWrapper.__len__    s    4<<  r   c                    | j                   |   }|j                  d   }t        dd| j                  z  z
  |z        }t	        j                         | j
                  k  rg }t        |      D ]  \  }}|dk(  s| j                  svt	        j                  | j                   | j                        }t	        j                  d| j                        }	t        t        | j                   |	      | j                        }	t        |	|| j                  v       }|j!                  |d d d |f           t#        j$                  |      }|S |dd |f   }|S )Nr   g{Gz?r   )voice.)r   shapeintr   randomr   	enumerater   randintr   gaussr   minmaxrepitchr   appendtorchstack)
r   indexstreams	in_length
out_lengthoutsidxstreamdelta_pitchdelta_tempos
             r   __getitem__zRepitchedWrapper.__getitem__#   s-   ,,u%MM"%	!dT^^33y@A
==?TZZ'D(1 
4V!8499"(..$..$.."QK"(,,q$.."AK"%c4>>/;&G"XK ,	.
 F1kzk>23
4 kk$'G  c;J;./Gr   N)__name__
__module____qualname____doc__r   r   r3    r   r   r   r      s(     '*Q"aSt!r   r   c                    t        j                  d      }t        j                  d      }t        | |j                  |d       d|j                  |j                  d| d|dg}|r|d	gz  }|r|d
gz  }	 t	        j
                  |dd       t        j                  |j                        \  } }
|
|k(  sJ | S # t        j                  $ r,}	t        d|	j                  j                  d             d}	~	ww xY w)z
    tempo is a relative delta in percentage, so tempo=10 means tempo at 110%!
    pitch is in semi tones.
    Requires `soundstretch` to be installed, see
    https://www.surina.net/soundtouch/soundstretch.html
    z.wav)suffixclamp)clipsoundstretchz-pitch=z-tempo=z.6fz-quickz-speechT)capture_outputcheckzCould not change bpm because zutf-8N)tempfileNamedTemporaryFiler   namesprunCalledProcessErrorRuntimeErrorstderrdecodetaload)wavpitchtempor   quick
samplerateinfileoutfilecommanderrorsrs              r   r&   r&   ;   s    ((7F))8GsFKK':
%
%G H:I;[
wt48 gggll#GCJ	    [:5<<;N;Nw;W:XYZZ[s   9B< <C;'C66C;)FFiD  )r7   r    
subprocessrC   r@   r(   
torchaudiorI   audior   r   r&   r8   r   r   <module>rX      s-    G      & &Rr   