This repository has been archived by the owner on Jul 20, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
essentia_analyzer.py
151 lines (131 loc) · 4.81 KB
/
essentia_analyzer.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
import settings
from subprocess import Popen, PIPE, STDOUT
import os
import json
import math
import experiment
class EssentiaAnalyzer(object):
"""
Using this analyzer is not recommended, because by default it analyzes a lot of features, and
one cannot easily disable features in the executable. This makes it computationally expensive.
"""
AVAILABLE_FEATURES = {
'barkbands_crest',
'barkbands_flatness_db',
'barkbands_kurtosis',
'barkbands_skewness',
'barkbands_spread',
'dissonance',
'erbbands_crest',
'erbbands_flatness_db',
'erbbands_kurtosis',
'erbbands_skewness',
'erbbands_spread',
'melbands_crest',
'melbands_flatness_db',
'melbands_kurtosis',
'melbands_skewness',
'melbands_spread',
'pitch_salience',
'silence_rate_20dB',
'silence_rate_30dB',
'silence_rate_60dB',
'spectral_centroid',
'spectral_complexity',
'spectral_decrease',
'spectral_energy',
'spectral_energyband_high',
'spectral_energyband_low',
'spectral_energyband_middle_high',
'spectral_energyband_middle_low',
'spectral_entropy',
'spectral_flux',
'spectral_kurtosis',
'spectral_rms',
'spectral_rolloff',
'spectral_skewness',
'spectral_spread',
'spectral_strongpeak',
'zerocrossingrate',
# TODO: proper parsing. These are lists within lists
# 'barkbands'
# 'erbbands'
# 'gfcc'
# 'melbands'
# 'mfcc'
# 'spectral_contrast_coeffs',
# 'spectral_contrast_valleys'
# TODO: proper parsing and handling. These are single numbers.
# 'average_loudness',
# 'dynamic_complexity'
}
POST_PROCESSING = {
'spectral_centroid': math.log
}
def __init__(self, features):
self.features = features
def analyze_multiple(self, sound_files):
if len(sound_files) == 0:
return
commands = [self.get_command(sound) for sound in sound_files]
for i in range(0, len(commands), settings.NUM_SIMULTANEOUS_PROCESSES):
commands_batch = commands[i:i + settings.NUM_SIMULTANEOUS_PROCESSES]
# run commands batch in parallel
processes = [
Popen(
command,
stdin=PIPE,
stdout=PIPE,
stderr=STDOUT
)
for command in commands_batch
]
for j in range(len(processes)):
processes[j].wait()
stdout = processes[j].communicate()[0]
if settings.VERBOSE:
print(stdout)
if 'completely silent file' in stdout:
if settings.VERBOSE:
print('Discarding completely silent file')
sound_files[i + j].is_silent = True
continue
self.parse_output(sound_files[i + j])
self.post_process(sound_files[i + j])
self.clean_up(sound_files[i + j])
@staticmethod
def get_output_analysis_file_path(that_sound_file):
return os.path.join(
settings.TEMP_DIRECTORY,
experiment.Experiment.folder_name,
that_sound_file.filename + '.essentia.json'
)
@staticmethod
def get_command(that_sound_file):
return [
'streaming_extractor_music',
os.path.abspath(that_sound_file.file_path),
os.path.abspath(EssentiaAnalyzer.get_output_analysis_file_path(that_sound_file)),
os.path.abspath(os.path.join('.', 'essentia_profile.yaml'))
]
def parse_output(self, that_sound_file):
for feature in self.features:
that_sound_file.analysis['series'][feature] = []
analysis_file_path = self.get_output_analysis_file_path(that_sound_file) + '_frames'
with open(analysis_file_path, 'r') as analysis_file:
data = json.load(analysis_file)
for feature in self.features:
that_sound_file.analysis['series'][feature] = data['lowlevel'][feature]
def post_process(self, that_sound_file):
for feature in self.features:
if feature in self.POST_PROCESSING and self.POST_PROCESSING[feature]:
that_sound_file.analysis['series'][feature] = map(
self.POST_PROCESSING[feature],
that_sound_file.analysis['series'][feature]
)
def clean_up(self, that_sound_file):
analysis_file_path = self.get_output_analysis_file_path(that_sound_file)
os.remove(analysis_file_path)
os.remove(analysis_file_path + '_frames')
def final_clean_up(self):
pass