o
    si                  	   @   sT   d dl mZ d dlZd dlZd dlZG dd dejjjZ	e
ddddddd	d
ZdS )    )PathNc                   @   s`   e Zd ZdZdZ								dd	d
Zdd ZdddZdd Zdd Z	dd Z
dd ZdS )DAMPVSEPSinglesDataseta-  DAMP-VSEP vocal separation dataset

    This dataset utilises one of the two preprocessed versions of DAMP-VSEP
    from https://github.com/groadabike/DAMP-VSEP-Singles aimed for
    SINGLE SINGER separation.

    The DAMP-VSEP dataset is hosted on Zenodo.
    https://zenodo.org/record/3553059

    Args:
        root_path (str): Root path to DAMP-VSEP dataset.
        task (str): one of ``'enh_vocal'``,``'separation'``.

            * ``'enh_vocal'`` for vocal enhanced.
            * ``'separation'`` for vocal and background separation.

        split (str):  one of ``'train_english'``, ``'train_singles'``,
            ``'valid'`` and ``'test'``.
            Default to ``'train_singles'``.
        ex_per_track (int, optional):
            Number of samples yielded from each track, can be used to increase
            dataset size, defaults to ``1``.
        random_segments (boolean, optional): Enables random offset for track segments.
        sample_rate (int, optional): Sample rate of files in dataset.
            Default 16000 Hz
        segment (float, optional): Duration of segments in seconds,
            Defaults to ``None`` which loads the full-length audio tracks.
        norm (Str, optional): Type of normalisation to use. Default to ``None``

            * ``'song_level'`` use mixture mean and std.
            * ```None``` no normalisation

        source_augmentations (Callable, optional): Augmentations applied to the sources (only).
            Default to ``None``.
        mixture (str, optional): Whether to use the original mixture with non-linear effects
            or remix sources. Default to original.

            * ``'remix'`` for use addition to remix the sources.
            * ``'original'`` for use the original mixture.

    .. note:: There are 2 train set available:

        * train_english: Uses all English spoken song. Duets are converted into 2 singles.
          Totalling 9243 performances and 77Hrs.
        * train_singles: Uses all singles performances, discarding all duets.
          Totalling 20660 performances and 149 hrs.

    z	DAMP-VSEPtrain_singles   F>  Noriginalc                 C   s   || _ t| | _|dv sJ d|
dv sJ d|| _|dkr&dg| _n	|dkr/ddg| _|| _|  | _g | j| _	|| _
|| _|| _ || _|| _|	| _|
| _| jd	kr`| jd
krbtdd S d S )N)	enh_vocal
separationz.Task should be one of 'enh_vocal','separation')remixr   z,Mixture should be one of 'remix', 'original'r   vocalr	   
backgroundr   train_englishz:The 'train_english' train can only accept 'remix' mixture.)sample_rater   
expanduser	root_pathtasktargetsplit
get_trackstracksperf_keyex_per_trackrandom_segmentssegmentnormsource_augmentationsmixture	Exception)selfr   r   r   r   r   r   r   r   r   r    r   R/home/ubuntu/.local/lib/python3.10/site-packages/asteroid/data/dampvsep_dataset.py__init__<   s,   


zDAMPVSEPSinglesDataset.__init__c                 C   s   t | j| j S N)lenr   r   r   r   r   r    __len__e   s   zDAMPVSEPSinglesDataset.__len__              ?c              	   C   s\   dd l }dd l}|jdtd |j|| jd||ddd\}	}
|r$|	|9 }	|	|8 }	|	| }	|	S )Nr   ignore)categoryTfloat32	polyphase)srmonooffsetdurationdtyperes_type)librosawarningsfilterwarningsUserWarningloadr   )r   pathstartr/   scalermeanstdr2   r3   x_r   r   r    _load_audioh   s"   
	z"DAMPVSEPSinglesDataset._load_audioc              	      s  dd l }i }| j } j| }| _d}|t j| d d d } jr=t	dt j| d  j
 }t j
}d}d}	 jdkru jdkr^t j| d }t j| d	 }	n jd
krut j| d }t j| d }	dD ]D}
d }|
dkrt j| d } j j j| |
  |t j| |
 d  ||||	d} jr | j}t|j}|||
< qwtj fdd| D dd} jd
kr|d}||fS  j j j| d  |t j| d  |||	d}||fS )Nr   r&   r/   d   r'   
song_levelr   original_mix_meanoriginal_mix_stdr
   r:   r;   )r   r   r   r9   _start)r8   r/   r9   r:   r;   c                    s   g | ]\}}| j v r|qS r   )r   ).0srcwavr$   r   r    
<listcomp>   s    z6DAMPVSEPSinglesDataset.__getitem__.<locals>.<listcomp>)dimoriginal_mixbackground_start)r8   r/   r:   r;   )mathr   r   mixture_pathfloorfloatr   r   randomuniformr   r   r   r>   r   r   r   torch
from_numpyTstackitemssum)r   indexrK   audio_sourcestrack_idperfr8   r/   mix_meanmix_stdsourcer9   r<   	audio_mixr   r$   r    __getitem__   sb   

  







z"DAMPVSEPSinglesDataset.__getitem__c                 C   s   || j  }| j| S r"   )r   r   )r   idxrY   r   r   r    get_track_name   s   

z%DAMPVSEPSinglesDataset.get_track_namec                 C   sH   t d| j d| j d}| rtt|d}|S td| j d)zYLoads metadata with tracks info.
        Raises error if metadata doesn't exist.
        z	metadata/_srz.jsonrzMetadata file for z
 not found)r   r   r   existsjsonr6   openRuntimeError)r   metadata_pathr   r   r   r    r      s
   z!DAMPVSEPSinglesDataset.get_tracksc                 C   s(   t  }| j|d< | j|d< tg|d< |S )zGet dataset infos (for publishing models).

        Returns:
            dict, dataset infos with keys `dataset`, `task` and `licences`.
        datasetr   licenses)dictdataset_namer   dampvsep_license)r   infosr   r   r    	get_infos   s
   


z DAMPVSEPSinglesDataset.get_infos)r   r   Fr   NNNr   )r&   NNr&   r'   )__name__
__module____qualname____doc__rl   r!   r%   r>   r_   ra   r   ro   r   r   r   r    r      s$    1
)
?r   z[DAMP-VSEP: Smule Digital Archive of Mobile Performances - Vocal Separation (Version 1.0.1) z!https://zenodo.org/record/3553059z
Smule, Incz'Smule's Research Data License AgreementT)title
title_linkauthorauthor_linklicenselicense_linknon_commercial)pathlibr   torch.utils.datarQ   rO   re   utilsdataDatasetr   rk   rm   r   r   r   r    <module>   s     V
