<reponame>TychoLin/asteroid
from asteroid.data import MUSDB18Dataset
import torch
from pathlib import Path

validation_tracks = [
    "Actions - One Minute Smile",
    "<NAME> - Waltz For My Victims",
    "<NAME> - Promises & Lies",
    "<NAME> - A Reason To Leave",
    "Triviul - Angelsaint",
    "<NAME> - Goodbye Bolero",
    "Fergessen - Nos Palpitants",
    "Leaf - Summerghost",
    "Skelpolu - Human Mistakes",
    "<NAME> - Pennies",
    "ANiMAL - Rockshow",
    "<NAME> - On The Line",
    "Meaxic - Take A Step",
    "Traffic Experiment - Sirens",
]


def load_datasets(parser, args):
    """Loads the specified dataset from commandline arguments

    Returns:
        train_dataset, validation_dataset
    """

    args = parser.parse_args()

    dataset_kwargs = {
        "root": Path(args.train_dir),
    }

    source_augmentations = Compose(
        [globals()["_augment_" + aug] for aug in args.source_augmentations]
    )

    train_dataset = MUSDB18Dataset(
        split="train",
        sources=args.sources,
        targets=args.sources,
        source_augmentations=source_augmentations,
        random_track_mix=True,
        segment=args.seq_dur,
        random_segments=True,
        sample_rate=args.sample_rate,
        samples_per_track=args.samples_per_track,
        **dataset_kwargs,
    )
    train_dataset = filtering_out_valid(train_dataset)

    valid_dataset = MUSDB18Dataset(
        split="train",
        subset=validation_tracks,
        sources=args.sources,
        targets=args.sources,
        segment=None,
        **dataset_kwargs,
    )

    return train_dataset, valid_dataset


def filtering_out_valid(input_dataset):
    """Filtering out validation tracks from input dataset.

    Return:
        input_dataset (w/o validation tracks)
    """
    for i, tmp in enumerate(input_dataset.tracks):
        if str(tmp["path"]).split("/")[-1] in validation_tracks:
            del input_dataset.tracks[i]

    return input_dataset


class Compose(object):
    """Composes several augmentation transforms.
    Args:
        augmentations: list of augmentations to compose.
    """

    def __init__(self, transforms):
        self.transforms = transforms

    def __call__(self, audio):
        for transform in self.transforms:
            audio = transform(audio)
        return audio


def _augment_gain(audio, low=0.25, high=1.25):
    """Applies a random gain to each source between `low` and `high`"""
    gain = low + torch.rand(1) * (high - low)
    return audio * gain


def _augment_channelswap(audio):
    """Randomly swap channels of stereo sources"""
    if audio.shape[0] == 2 and torch.FloatTensor(1).uniform_() < 0.5:
        return torch.flip(audio, [0])

    return audio

from asteroid.data import MUSDB18Dataset
import torch
from pathlib import Path

validation_tracks = [
    "Actions - One Minute Smile",
    "<NAME> - Waltz For My Victims",
    "<NAME> - Promises & Lies",
    "<NAME> - A Reason To Leave",
    "Triviul - Angelsaint",
    "<NAME> - Goodbye Bolero",
    "Fergessen - Nos Palpitants",
    "Leaf - Summerghost",
    "Skelpolu - Human Mistakes",
    "<NAME> - Pennies",
    "ANiMAL - Rockshow",
    "<NAME> - On The Line",
    "Meaxic - Take A Step",
    "Traffic Experiment - Sirens",
]


def load_datasets(parser, args):
    """Loads the specified dataset from commandline arguments

    Returns:
        train_dataset, validation_dataset
    """

    args = parser.parse_args()

    dataset_kwargs = {
        "root": Path(args.train_dir),
    }

    source_augmentations = Compose(
        [globals()["_augment_" + aug] for aug in args.source_augmentations]
    )

    train_dataset = MUSDB18Dataset(
        split="train",
        sources=args.sources,
        targets=args.sources,
        source_augmentations=source_augmentations,
        random_track_mix=True,
        segment=args.seq_dur,
        random_segments=True,
        sample_rate=args.sample_rate,
        samples_per_track=args.samples_per_track,
        **dataset_kwargs,
    )
    train_dataset = filtering_out_valid(train_dataset)

    valid_dataset = MUSDB18Dataset(
        split="train",
        subset=validation_tracks,
        sources=args.sources,
        targets=args.sources,
        segment=None,
        **dataset_kwargs,
    )

    return train_dataset, valid_dataset


def filtering_out_valid(input_dataset):
    """Filtering out validation tracks from input dataset.

    Return:
        input_dataset (w/o validation tracks)
    """
    for i, tmp in enumerate(input_dataset.tracks):
        if str(tmp["path"]).split("/")[-1] in validation_tracks:
            del input_dataset.tracks[i]

    return input_dataset


class Compose(object):
    """Composes several augmentation transforms.
    Args:
        augmentations: list of augmentations to compose.
    """

    def __init__(self, transforms):
        self.transforms = transforms

    def __call__(self, audio):
        for transform in self.transforms:
            audio = transform(audio)
        return audio


def _augment_gain(audio, low=0.25, high=1.25):
    """Applies a random gain to each source between `low` and `high`"""
    gain = low + torch.rand(1) * (high - low)
    return audio * gain


def _augment_channelswap(audio):
    """Randomly swap channels of stereo sources"""
    if audio.shape[0] == 2 and torch.FloatTensor(1).uniform_() < 0.5:
        return torch.flip(audio, [0])

    return audio
Loads the specified dataset from commandline arguments      Returns:         train_dataset, validation_dataset Filtering out validation tracks from input dataset.      Return:         input_dataset (w/o validation tracks) Composes several augmentation transforms.     Args:         augmentations: list of augmentations to compose. Applies a random gain to each source between `low` and `high` Randomly swap channels of stereo sources