-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathfeatures.py
executable file
·69 lines (51 loc) · 1.72 KB
/
features.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
import argparse
import librosa
import numpy as np
from tqdm import tqdm
from os.path import join, isfile
from joblib import Parallel, delayed
from python_speech_features import get_filterbanks, sigproc
samplerate = 16000
nfft = 512
winlen = 0.025 * samplerate
winstep = 0.01 * samplerate
banks = get_filterbanks(40, nfft, samplerate).transpose()
def job(input_name, output_name):
audio, _ = librosa.load(input_name, mono=True, sr=samplerate)
if len(audio) == 0:
return False
signal = sigproc.preemphasis(audio, 0.97)
x = sigproc.framesig(signal, winlen, winstep, np.hanning)
if len(x) == 0:
return False
x = sigproc.powspec(x, nfft)
x = np.dot(x, banks)
x = np.where(x == 0, np.finfo(float).eps, x)
x = np.log(x).astype(dtype=np.float32)
if np.isnan(np.sum(x)):
return False
np.save(output_name, x)
return True
parser = argparse.ArgumentParser(description='Compute features')
parser.add_argument('--manifest', type=str)
parser.add_argument('--jobs', type=int, default=8)
args = parser.parse_args()
prefix = args.manifest.replace('.csv', '')
print(prefix)
files = dict()
with open(args.manifest) as f:
for line in tqdm(f.readlines()):
path = line.split(',')[0]
audio_path = join(prefix, path)
if not isfile(audio_path):
continue
numpy_path = join(prefix, path.replace('.wav', '.npy'))
if isfile(numpy_path):
continue
files[audio_path] = numpy_path
tasks = []
for audio_path, numpy_path in files.items():
tasks.append(delayed(job)(audio_path, numpy_path))
print('Tasks:', len(tasks))
results = Parallel(n_jobs=args.jobs, backend='multiprocessing', verbose=1)(tasks)
print('Success:', sum(results))