File size: 1,495 Bytes
5019931
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import argparse
import time

import librosa
import soundfile

from bytesep.inference import SeparatorWrapper

sample_rate = 44100  # Must be 44100 when using the downloaded checkpoints.


def separate(args):

    audio_path = args.audio_path
    source_type = args.source_type
    device = "cuda"  # "cuda" | "cpu"

    # Load audio.
    audio, fs = librosa.load(audio_path, sr=sample_rate, mono=False)

    if audio.ndim == 1:
        audio = audio[None, :]
        # (2, segment_samples)

    # separator
    separator = SeparatorWrapper(
        source_type=source_type,
        model=None,
        checkpoint_path=None,
        device=device,
    )

    t1 = time.time()

    # Separate.
    sep_wav = separator.separate(audio)

    sep_time = time.time() - t1

    # Write out audio
    sep_audio_path = 'sep_{}.wav'.format(source_type)

    soundfile.write(file=sep_audio_path, data=sep_wav.T, samplerate=sample_rate)

    print("Write out to {}".format(sep_audio_path))
    print("Time: {:.3f}".format(sep_time))


if __name__ == '__main__':
    parser = argparse.ArgumentParser()
    parser.add_argument(
        '--audio_path',
        type=str,
        default="resources/vocals_accompaniment_10s.mp3",
        help="Audio path",
    )
    parser.add_argument(
        '--source_type',
        type=str,
        choices=['vocals', 'accompaniment'],
        default="accompaniment",
        help="Source type to be separated.",
    )

    args = parser.parse_args()

    separate(args)