-
Notifications
You must be signed in to change notification settings - Fork 48
Expand file tree
/
Copy pathpreprocess_multispeaker.py
More file actions
56 lines (49 loc) · 1.69 KB
/
preprocess_multispeaker.py
File metadata and controls
56 lines (49 loc) · 1.69 KB
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
import sys
import glob
import pickle
import os
import multiprocessing as mp
from utils.dsp import *
SEG_PATH = sys.argv[1]
DATA_PATH = sys.argv[2]
def get_files(path):
next_speaker_id = 0
speaker_ids = {}
filenames = []
for filename in glob.iglob(f'{path}/**/*.wav', recursive=True):
speaker_name = filename.split('/')[-2]
if speaker_name not in speaker_ids:
speaker_ids[speaker_name] = next_speaker_id
next_speaker_id += 1
filenames.append([])
filenames[speaker_ids[speaker_name]].append(filename)
return filenames
files = get_files(SEG_PATH)
def process_file(i, path):
dir = f'{DATA_PATH}/{i}'
name = path.split('/')[-1][:-4] # Drop .wav
filename = f'{dir}/{name}.npy'
if os.path.exists(filename):
print(f'{filename} already exists, skipping')
return
floats = load_wav(path, encode=False)
trimmed, _ = librosa.effects.trim(floats, top_db=25)
quant = (trimmed * (2**15 - 0.5) - 0.5).astype(np.int16)
if max(abs(quant)) < 2048:
print(f'audio fragment too quiet ({max(abs(quant))}), skipping: {path}')
return
if len(quant) < 10000:
print(f'audio fragment too short ({len(quant)} samples), skipping: {path}')
return
os.makedirs(dir, exist_ok=True)
np.save(filename, quant)
return name
index = []
with mp.Pool(8) as pool:
for i, speaker in enumerate(files):
res = pool.starmap_async(process_file, [(i, path) for path in speaker]).get()
index.append([x for x in res if x])
print(f'Done processing speaker {i}')
os.makedirs(DATA_PATH, exist_ok=True)
with open(f'{DATA_PATH}/index.pkl', 'wb') as f:
pickle.dump(index, f)