-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwaveFiles.py~
84 lines (71 loc) · 2.43 KB
/
waveFiles.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
from scipy.io import wavfile
import numpy as np
from scikits.talkbox.features import mfcc
import subprocess
#import pysox
NOISERED_CONSTANT = 0.5
DEL_WAVE_TIME_MARGIN = 0.0019
MFCC_WINDOW = 256
def getClicks(timeStamps, sampleRate, wave):
d = DEL_WAVE_TIME_MARGIN
if d*sampleRate < MFCC_WINDOW:
d = float(MFCC_WINDOW)/sampleRate
print timeStamps, sampleRate
clicksList = []
for t in timeStamps:
if t != 0:
clicksList.append(wave[int(sampleRate*(t-d)):int(sampleRate*(t+d))])
else:
clicksList.append(wave[:int(sampleRate*(t+d))])
return clicksList
def deNoise():
trimTime = 0
log = open('log.txt')
for line in log:
trimTime = float(line.split()[1])
if trimTime != 0:
break
log.close()
print trimTime
p1 = subprocess.Popen('sox output.wav -n trim 0 %f noiseprof noise-profile' % trimTime)
p1.wait()
subprocess.Popen('sox output.wav deNoised.wav noisered noise-profile %f' % NOISERED_CONSTANT)
#inFile = pysox.CSoxStream('output.wav')
#noiseFile = pysox.CNullFile()
#chain = pysox.CEffectsChain(inFile, noiseFile)
#trim = pysox.CEffect('trim', [b'0', str(trimTime)])
#noiseProfile = pysox.CEffect('noiseprof', [b'noise-profile'])
#chain.add_effect(trim)
#chain.add_effect(noiseProfile)
#chain.flow_effects()
#inFile.close()
#
#inFile = pysox.CSoxStream('output.wav')
#print inFile.get_signal()
#outFile = pysox.CSoxStream('deNoised.wav', 'waveData', inFile.get_signal())
#chain = pysox.CEffectsChain(inFile, outFile)
#noiseRed = pysox.CEffect('noisered', [b'noise-profile', str(NOISERED_CONSTANT)])
#chain.add_effect(noiseRed)
#chain.flow_effects()
#inFile.close()
#outFile.close()
def getMFCC():
sampleRate, waveData = wavfile.read("deNoised.wav")
waveData = np.float64(waveData/32768.)
state = []
inputFile = open('log.txt')
for line in inputFile:
state.append(float(line.split()[1]))
state.append(float(line.split()[2]))
clicks = getClicks(state, sampleRate, waveData)
cepsList = []
mspecList = []
for click in clicks:
ceps = mfcc(click, fs=sampleRate)
cepsList.append(ceps[0])
mspecList.append(ceps[1])
return cepsList, mspecList
#wavfile.write("wavfile.wav",sampleRate ,np.int16(wNew*32768))
if __name__ == "__main__":
deNoise()
getMFCC()