-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathextractfeatures.py
73 lines (64 loc) · 2.21 KB
/
extractfeatures.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
from python_speech_features import mfcc
from python_speech_features import delta
# import scipy.io.wavfile as wav
import subprocess as sp
import numpy as np
FFMPEG_PATH = "C:\Program Files\FFMPEG\\bin\\ffmpeg.exe"
class FeatureExtractor(object):
def __init__(self):
self.raw_audio = None
self.mfcc = None
self.d_mfcc = None
self.audio_features = None
def read_audio(self, file):
# print("1")
self.raw_audio = None
self.mfcc = None
self.d_mfcc = None
self.audio_features = None
command = [FFMPEG_PATH,
'-i', file,
'-f', 's16le',
'-acodec', 'pcm_s16le',
'-ar', '16000',
'-ac', '1',
'-']
# print("2")
pipe = sp.Popen(command, stdin=sp.PIPE, stderr=sp.PIPE, stdout=sp.PIPE, bufsize=10**8)
# print("3")
self.raw_audio = pipe.stdout.read()
pipe.terminate()
print("Read %s - Length of sample array: %d" % (file, len(self.raw_audio)))
# (x, wavfile) = wav.read(file)
self.raw_audio = np.fromstring(self.raw_audio, dtype="int16")
print(self.raw_audio)
# print(wavfile)
def get_mfcc_features(self):
# if(self.mfcc==None):
self.mfcc = mfcc(self.raw_audio, 16000)
# print(len(self.mfcc[50]))
# print(self.mfcc)
# return self.mfcc
# Work in progress :P
# def get_delta_mfcc_features(self):
# if(self.d_mfcc==None):
# if(self.mfcc==None):
# self.get_mfcc_features()
# self.d_mfcc = delta(self.mfcc, 1)
# print(self.d_mfcc)
# return self.d_mfcc
def build_features(self):
if(self.mfcc==None):
self.get_mfcc_features()
self.audio_features = np.zeros(13, dtype="int16")
l = len(self.mfcc)
for x in self.mfcc:
for i in range(0,13):
self.audio_features[i] += x[i]
# print(x[i])
for i in range(0,13):
self.audio_features[i] /= l
def get_features(self):
if(self.audio_features==None):
self.build_features()
return self.audio_features