o
    rri
                     @   sT   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 dZ
dd	d
ZdS )z@Utility for on the fly pitch/tempo change for data augmentation.    N   )
save_audioc                   @   s8   e Zd ZdZdddddgdfdd	Zd
d Zdd ZdS )RepitchedWrapperzA
    Wrap a dataset to apply online change of pitch / tempo.
    g?            Tc                 C   s.   || _ || _|| _|| _|| _|| _|| _d S N)datasetproba	max_pitch	max_tempo	tempo_stdsamevocals)selfr
   r   r   r   r   r   r    r   B/home/ubuntu/.local/lib/python3.10/site-packages/demucs/repitch.py__init__   s   
zRepitchedWrapper.__init__c                 C   s
   t | jS r	   )lenr
   )r   r   r   r   __len__    s   
zRepitchedWrapper.__len__c           
      C   s   | j | }|jd }tdd| j  | }t | jk rhg }t|D ]>\}}|dks-| jsHt| j	 | j	}t
d| j}	tt| j |	| j}	t|||	|| jv d}||d d d |f  q"t|}|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_tempor   r   r   __getitem__#   s*   


zRepitchedWrapper.__getitem__N)__name__
__module____qualname____doc__r   r   r.   r   r   r   r   r      s    

r   FD  c              
   C   s   t jdd}t jdd}t| |j|dd d|j|jd| d|dg}|r,|d	g7 }|r3|d
g7 }z
tj|ddd W n tjyU }	 ztd|	j	d d}	~	ww t
|j\} }
|
|ksdJ | S )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srr   r   r   r!   ;   s,   


r!   )FFr3   )r2   r   
subprocessr=   r:   r#   
torchaudiorC   audior   r   r!   r   r   r   r   <module>   s   )