-
Notifications
You must be signed in to change notification settings - Fork 36
/
prepare-impulse-responses.py
executable file
·147 lines (124 loc) · 3.52 KB
/
prepare-impulse-responses.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
#!/usr/bin/python
#Copyright (c) 2015 Idiap Research Institute, http://www.idiap.ch/
#Written by Marc Ferras <marc.ferras@idiap.ch>,
#
#This file is part of AcSim.
#
#AcSim is free software: you can redistribute it and/or modify
#it under the terms of the GNU General Public License version 3 as
#published by the Free Software Foundation.
#
#Foobar is distributed in the hope that it will be useful,
#but WITHOUT ANY WARRANTY; without even the implied warranty of
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
#GNU General Public License for more details.
#
#You should have received a copy of the GNU General Public License
#along with AcSim. If not, see <http://www.gnu.org/licenses/>.
import os,sys,subprocess
import numpy as np
import scikits.audiolab as al # audiolab, install through pip, requires libsndfile
import scikits.samplerate as src # secret rabbit code resampled, install through pip, requires SRC
import matplotlib.pyplot as plt
import struct
import re
def loadSignal(fileName):
try:
x, Fs, encFmt = al.wavread(fileName)
except IOError:
print('Could not import file "%s"' % sigPath)
return None
return (x, Fs)
def find1(L, S):
return [x for x in (L) if S in x]
irOriginal='impulse-responses-original'
irOutput='impulse-responses'
irDeviceList='ir-device-file-list.txt'
irSpaceList='ir-space-file-list.txt'
kwDevice = ['devices']
kwSpace = ['spaces']
kwBlockSpace = []
# read wav IR
swav=subprocess.check_output('find -L ' + irOriginal + ' -type f -name \"*.wav\"', shell=True)
swav=swav.split('\n')
swav=swav[:-1]
nDevice = 0
nSpace = 0
fdevice=open(irDeviceList,'wt')
fspace=open(irSpaceList,'wt')
for ln in swav:
(fileName, fileExtension) = os.path.splitext(ln)
# read wav file
(x,fs)=loadSignal(ln)
sx=x.shape
if len(sx)==2:
ir = x[:,1]
elif len(sx)==1:
ir = x
else:
print 'too many channels for IR ' + ln + ': skipping'
continue
ir8k = src.resample(ir, 8000/float(fs), 'sinc_medium')
ir8kabs = ir8k * ir8k
sum8k=ir8kabs.sum()
ir8k = ir8k / np.sqrt(sum8k)
dname=os.path.dirname(ln)
lin=dname.split('/')[1:]
lout=[]
for elem in lin:
e = ''.join(e for e in elem if e.isalnum())
lout.append(e)
outdir=irOutput + '/' + '/'.join(lout)
basename = os.path.basename(ln)
basename = re.sub('.wav','',basename)
basename = ''.join(e for e in basename if e.isalnum())
basename8k = basename + '-8000.ir'
outfile8k = outdir + '/' + basename8k
ir16k = src.resample(ir, 16000/float(fs), 'sinc_medium')
ir16kabs = ir16k * ir16k
sum16k=ir16kabs.sum()
ir16k = ir16k / np.sqrt(sum16k)
basename16k = basename + '-16000.ir'
outfile16k = outdir + '/' + basename16k
foundSpace = False
foundBlockSpace = False
for kw in kwSpace:
if kw in dname:
foundSpace = True
if foundSpace:
for kw2 in kwBlockSpace:
if kw2 in dname:
foundBlockSpace = True
if foundSpace and not foundBlockSpace:
break
elif foundSpace and foundBlockSpace:
foundSpace = False
foundDevice = False
for kw in kwDevice:
if kw in dname:
foundDevice = True
break
if foundDevice:
try:
os.makedirs(outdir)
except:
pass
print ln
np.savetxt(outfile8k, ir8k, newline=' ')
np.savetxt(outfile16k, ir16k, newline=' ')
fdevice.write(outfile16k +'\n')
nDevice = nDevice + 1
if foundSpace:
try:
os.makedirs(outdir)
except:
pass
print ln
np.savetxt(outfile8k, ir8k, newline=' ')
np.savetxt(outfile16k, ir16k, newline=' ')
fspace.write(outfile16k +'\n')
nSpace = nSpace + 1
fdevice.close()
fspace.close()
print str(nDevice) + ' device IRs'
print str(nSpace) + ' space IRs'