File size: 1,629 Bytes
f6b56a2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from batch_processing import Batch
import argparse
import sys
from pathlib import Path
from gyraudio.audio_separation.properties import PATHS, BUFFERS, NAME, SAMPLING_RATE
from gyraudio.io.audio import load_audio_tensor


def parse_command_line_generic_audio_load() -> argparse.ArgumentParser:
    parser = argparse.ArgumentParser(description='Batch audio loading',
                                     formatter_class=argparse.ArgumentDefaultsHelpFormatter)

    parser.add_argument("-preload", "--preload", action="store_true", help="Preload audio files")
    return parser


def load_buffers_custom(signal: dict, device="cpu") -> None:
    generic_signal, sampling_rate = load_audio_tensor(signal[PATHS], device=device)
    signal[BUFFERS] = generic_signal
    signal[SAMPLING_RATE] = sampling_rate


def audio_loading(input: Path, preload: bool) -> dict:
    name = input.parent.name + "/" + input.stem
    signal = {
        NAME: name,
        PATHS: input,
    }
    if preload:
        load_buffers_custom(signal)
    return signal


def generic_audio_loading_batch(input: Path, args: argparse.Namespace) -> dict:
    """Wrapper to load audio files from a directory using batch_processing
    """
    return audio_loading(input, preload=args.preload)


def main(argv):
    batch = Batch(argv)
    batch.set_io_description(
        input_help='input audio files',
        output_help=argparse.SUPPRESS
    )
    parser = parse_command_line_generic_audio_load()
    batch.parse_args(parser)
    all_signals = batch.run(generic_audio_loading_batch)
    return all_signals


if __name__ == "__main__":
    main(sys.argv[1:])