-
Notifications
You must be signed in to change notification settings - Fork 4
/
main.py
190 lines (146 loc) · 6.52 KB
/
main.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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
import matplotlib.pyplot as plt
import pandas as pd
import os
import sys
from scipy import stats
from sklearn import preprocessing
from sklearn.decomposition import PCA
from sklearn.cluster import KMeans
import argparse
import random
import time
from pythonosc import udp_client
import essentia
import essentia.standard as es
def isMatch(name, patterns):
if not patterns:
return False
for pattern in patterns:
if fnmatch.fnmatch(name, pattern):
return True
return False
def normalize_zscore(featureData):
mu = np.mean(featureData,axis=1)
std = np.std(featureData,axis=1)
normFeatureData = ((featureData.transpose() - mu) / std).transpose()
return normFeatureData
def add_to_dict(dict, keys, value):
for key in keys[:-1]:
dict = dict.setdefault(key, {})
dict[keys[-1]] = value
def pool_to_array(pool, include_descs=None, ignore_descs=None):
# a workaround to convert Pool to np.array
# check pool descriptor names
descs = pool.descriptorNames()
if include_descs:
descs = [d for d in descs if isMatch(d, include_descs)]
if ignore_descs:
descs = [d for d in descs if not isMatch(d, ignore_descs)]
# let's start with 10 features
result = []
i = 0
# append everything to dict result
for d in descs:
value = pool[d]
result.append(value)
i+=1
#add_to_dict(result, keys, value)
return result
def compute_features(complete_path):
result = []
meta_result = []
file_count = 0
# for loop over files
for file in os.listdir(complete_path):
if file.endswith(".wav"):
file_count+=1
# print(file +' : ' + str(file_count))
# load our audio into an array
audio = es.MonoLoader(filename=complete_path + file, sampleRate=44100)()
# create the pool and the necessary algorithms
pool = essentia.Pool()
window = es.Windowing()
energy = es.Energy()
spectrum = es.Spectrum()
centroid = es.Centroid(range=22050)
rolloff = es.RollOff()
crest = es.Crest()
speak = es.StrongPeak()
rmse = es.RMS()
mfcc = es.MFCC()
flux = es.Flux()
barkbands = es.BarkBands( sampleRate = 44100)
zerocrossingrate = es.ZeroCrossingRate()
meta = es.MetadataReader(filename=complete_path + file, failOnError=True)()
pool_meta, duration, bitrate, samplerate, channels = meta[7:]
# centralmoments = es.SpectralCentralMoments()
# distributionshape = es.DistributionShape()
# compute the centroid for all frames in our audio and add it to the pool
for frame in es.FrameGenerator(audio, frameSize = 1024, hopSize = 512):
frame_windowed = window(frame)
frame_spectrum = spectrum(frame_windowed)
c = centroid(frame_spectrum)
pool.add('spectral.centroid', c)
cr = crest(frame_spectrum)
pool.add('spectral crest', cr)
r = rolloff(frame_spectrum)
pool.add('spectral rolloff', r)
sp = speak(frame_spectrum)
pool.add('strong peak', sp)
rms = rmse(frame_spectrum)
pool.add('RMS', rms)
pool.add('spectral_energy', energy(frame_spectrum))
# (frame_melbands, frame_mfcc) = mfcc(frame_spectrum)
# pool.add('frame_MFCC', frame_mfcc)
fl = flux(frame_spectrum)
pool.add('spectral flux', fl)
# bbands = barkbands(frame_spectrum)
# pool.add('bark bands', bbands)
zcr = zerocrossingrate(frame_spectrum)
pool.add('zero crossing rate', zcr)
# frame_centralmoments = centralmoments(power_spectrum)
# (frame_spread, frame_skewness, frame_kurtosis) = distributionshape(frame_centralmoments)
# pool.add('spectral_kurtosis', frame_kurtosis)
# pool.add('spectral_spread', frame_spread)
# pool.add('spectral_skewness', frame_skewness)
# aggregate the results (find mean if needed)
aggrpool = es.PoolAggregator(defaultStats = ['mean'])(pool) #,'stdev' ])(pool)
pool_meta.set("duration", duration)
pool_meta.set("filename", os.path.relpath(file))
# write pools to lists
pool_arr = pool_to_array(aggrpool)
result.append(pool_arr)
meta_arr = pool_to_array(pool_meta)
meta_result.append(meta_arr)
features_df = pd.DataFrame.from_records(result)
features_df.columns = ['centroid', 'crest','roll off','strong peak','rms','energy','flux','zcr']
meta_df = pd.DataFrame.from_records(meta_result)
meta_df.columns = ['duration','filename','metadata.tags.comment']
del meta_df['metadata.tags.comment']
return features_df,meta_df
# See all feature names in the pool in a sorted order
# print(sorted(features.descriptorNames()))
if __name__ == '__main__':
features,metadata = compute_features('./samples/')
# normalized_features = preprocessing.normalize(features,axis =1)
features = preprocessing.StandardScaler().fit_transform(features)
standardized_features = pd.DataFrame(features, columns = ['centroid', 'crest','roll off','strong peak','rms','energy','flux','zcr'])
pca = PCA(n_components=2)
principalComponents = pca.fit_transform(standardized_features)
principalComponents = preprocessing.MinMaxScaler().fit_transform(principalComponents)
principal_df = pd.DataFrame(data = principalComponents
,columns = ['pc_1', 'pc_2'])
print(principal_df.head())
kmeans = KMeans(n_clusters=4).fit(principal_df)
centroids = kmeans.cluster_centers_
plt.scatter(principal_df['pc_1'], principal_df['pc_2'], c= kmeans.labels_.astype(float), s=50, alpha=0.5)
plt.scatter(centroids[:, 0], centroids[:, 1], c='red', s=50)
df = pd.concat([principal_df,metadata],axis = 1)
df = df.sort_values('filename')
client = udp_client.SimpleUDPClient('127.0.0.1', 7400)
count = 1
for index,row in df.iterrows():
print( count, "pca." + str(count), row['filename'], row['pc_1'], row['pc_2'])
client.send_message("/pca", [ count, "pca."+str(count), row['pc_1']*500, row['pc_2']*500,row['filename'] ] )
count+=1
plt.show()