-
Notifications
You must be signed in to change notification settings - Fork 35
/
pyworld_features.py
120 lines (101 loc) · 4.27 KB
/
pyworld_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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
'''
AAA lllllll lllllll iiii
A:::A l:::::l l:::::l i::::i
A:::::A l:::::l l:::::l iiii
A:::::::A l:::::l l:::::l
A:::::::::A l::::l l::::l iiiiiii eeeeeeeeeeee
A:::::A:::::A l::::l l::::l i:::::i ee::::::::::::ee
A:::::A A:::::A l::::l l::::l i::::i e::::::eeeee:::::ee
A:::::A A:::::A l::::l l::::l i::::i e::::::e e:::::e
A:::::A A:::::A l::::l l::::l i::::i e:::::::eeeee::::::e
A:::::AAAAAAAAA:::::A l::::l l::::l i::::i e:::::::::::::::::e
A:::::::::::::::::::::A l::::l l::::l i::::i e::::::eeeeeeeeeee
A:::::AAAAAAAAAAAAA:::::A l::::l l::::l i::::i e:::::::e
A:::::A A:::::A l::::::ll::::::li::::::ie::::::::e
A:::::A A:::::A l::::::ll::::::li::::::i e::::::::eeeeeeee
A:::::A A:::::A l::::::ll::::::li::::::i ee:::::::::::::e
AAAAAAA AAAAAAAlllllllllllllllliiiiiiii eeeeeeeeeeeeee
| ___| | | / _ \ | ___ \_ _| _
| |_ ___ __ _| |_ _ _ _ __ ___ ___ / /_\ \| |_/ / | | (_)
| _/ _ \/ _` | __| | | | '__/ _ \/ __| | _ || __/ | |
| || __/ (_| | |_| |_| | | | __/\__ \ | | | || | _| |_ _
\_| \___|\__,_|\__|\__,_|_| \___||___/ \_| |_/\_| \___/ (_)
___ _ _
/ _ \ | (_)
/ /_\ \_ _ __| |_ ___
| _ | | | |/ _` | |/ _ \
| | | | |_| | (_| | | (_) |
\_| |_/\__,_|\__,_|_|\___/
This will featurize folders of audio files if the default_audio_features = ['pyworld_features']
Extracts a range of pitch-based features.
For more information, check out hte documentation: https://github.com/JeremyCCHsu/Python-Wrapper-for-World-Vocoder
'''
import os, sys
import scipy.io.wavfile as wav
import numpy as np
# adding this in because some installations may not have pyworld features installed
try:
import pyworld as pw
except:
os.system('pip3 install pyworld==0.2.10')
import pyworld as pw
import numpy as np
# get statistical features in numpy
def stats(matrix, label):
labels=list()
# extract features
if label in ['smoothed_spectrogram', 'aperiodicity']:
mean=np.mean(matrix, axis=0)
for i in range(len(mean)):
labels.append(label+'_mean_'+str(i))
std=np.std(matrix, axis=0)
for i in range(len(std)):
labels.append(label+'_std_'+str(i))
maxv=np.amax(matrix, axis=0)
for i in range(len(maxv)):
labels.append(label+'_max_'+str(i))
minv=np.amin(matrix, axis=0)
for i in range(len(minv)):
labels.append(label+'_min_'+str(i))
median=np.median(matrix, axis=0)
for i in range(len(median)):
labels.append(label+'_median_'+str(i))
else:
mean=np.mean(matrix)
std=np.std(matrix)
maxv=np.amax(matrix)
minv=np.amin(matrix)
median=np.median(matrix)
labels=[label+'_mean',label+'_std',label+'_max'+label+'_min',label, '_median']
features=np.append(mean, std)
features=np.append(features, maxv)
features=np.append(features, minv)
features=np.append(features, median)
return features, labels
def pyworld_featurize(audiofile):
fs, x = wav.read(audiofile)
print(x)
print(fs)
# corrects for 2 channel audio
try:
x= x[:,0]
except:
pass
x=np.array(np.ascontiguousarray(x), dtype=np.double)
print(fs)
print(x)
_f0, t = pw.dio(x, fs) # raw pitch extractor
f0 = pw.stonemask(x, _f0, t, fs) # pitch refinement
sp = pw.cheaptrick(x, f0, t, fs) # extract smoothed spectrogram
ap = pw.d4c(x, f0, t, fs) # extract aperiodicity)
features_0, labels_0 = stats(_f0, 'pitch')
features_1, labels_1 = stats(_f0, 'pitch_refinement')
features_2, labels_2 = stats(sp, 'smoothed_spectrogram')
features_3, labels_3 = stats(ap, 'aperiodicity')
features_0=list(features_0)
features_1=list(features_1)
features_2=list(features_2)
features_3=list(features_3)
features=features_0+features_1+features_2+features_3
labels=labels_0+labels_1+labels_2+labels_3
return features, labels