-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdriver.py
76 lines (55 loc) · 2.51 KB
/
driver.py
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
69
70
71
72
73
74
75
76
import asyncio
import os
import random
import utils
from filter import Filter
from utils import is_file_in_dir, ensure_folder, merge_audio
import tqdm
class Remixer:
def __init__(self, args):
# get how many samples we need to generate
self.to_generate = int(args['<num_generate>'])
print("Generating:", self.to_generate)
# get the input directory
self.in_dir = args['<input_dir>']
print("Input:", self.in_dir)
# get the output directory
self.out_dir = args['<output_dir>']
print("Output:", self.out_dir)
# samples per sample
self.samples_per_sample = int(args['<samples_per_sample>'])
print("Samples to take:", self.samples_per_sample)
if self.samples_per_sample > 12:
print("WARNING: possibly wrong input format! more than 12 samples/sample specified")
print("Clamping to 12")
self.samples_per_sample = 12
self.filter_driver = Filter(args['--filter'] if '--filter' in args else None,
args['--outfmt'] if '--outfmt' in args else None)
print(self.filter_driver)
# get all files in input dir
self.file_collection = self.iterate_directory(self.in_dir)
def select_some(self, N):
# copy the input directory collection
# select some
return random.sample(self.file_collection, N)
# gets all files in a directory and checks it against the filter
def iterate_directory(self, path):
return [(x, path) for x in tqdm.tqdm(os.listdir(path), desc=f"Scanning {path}")
if is_file_in_dir(x, path)
and self.filter_driver.good(x)]
async def run(self):
ensure_folder(self.out_dir)
@utils.background
def parallel():
[x, *rest] = self.select_some(self.samples_per_sample)
# based on the filter and the outfmt
# we need to create the output filename
# here
out_name = self.filter_driver.pattern_replace([y[0] for y in [x] + rest])
merge_audio(os.path.join(*x[::-1]), *[os.path.join(*p[::-1]) for p in rest],
out=os.path.join(self.out_dir, out_name))
# Generate Jobs
futures = [parallel() for _ in tqdm.trange(self.to_generate, desc="Dispatching jobs")]
# Await Jobs
for future in tqdm.tqdm(futures, desc="Awaiting futures"):
await future