comparison experiment-reverb/code/apply_reverb.py @ 0:246d5546657c

initial commit, needs cleanup
author Emmanouil Theofanis Chourdakis <e.t.chourdakis@qmul.ac.uk>
date Wed, 14 Dec 2016 13:15:48 +0000
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:246d5546657c
1 # -*- coding: utf-8 -*-
2 """
3 Created on Wed Mar 23 21:46:05 2016
4
5 @author: Emmanouil Theofanis Chourdakis
6 """
7 from mapping import *
8 from essentia import Pool
9 from essentia.standard import YamlInput, YamlOutput
10 from scikits.audiolab import Format, Sndfile
11 #from essentia import *
12 from essentia.standard import *
13 from sys import argv
14 from ui import zafar
15 from numpy import *
16 from scipy.signal import fftconvolve
17
18 def estimate_T60(d1, g1, gc, G, SR):
19 ga = 1/sqrt(2)
20 return d1/SR/log(g1)*log(10**-3/ga/(1-gc)/G)
21
22 if __name__=="__main__":
23 if len(argv) != 3:
24 print("Incorrect number of arguments:")
25 print("Usage: ")
26 print("%s <parameter_file> <audio_file>")
27 print("")
28
29 sys.exit(-1)
30
31
32 songfname = argv[2]
33 outfname = songfname.replace(".wav","_reverb_wav")
34 paramfname = argv[1]
35
36 parameters_pool = YamlInput(filename = paramfname)()
37
38 d1t = parameters_pool['parameters.d1']*d1_max + d1_min
39 dat = parameters_pool['parameters.da']*da_max + da_min
40 g1 = parameters_pool['parameters.g1']*g1_max + g1_min
41 gc = parameters_pool['parameters.gc']*gc_max + gc_min
42 G = parameters_pool['parameters.G']*G_max + G_min
43
44 print "[II] Applying reverb to %s" % songfname
45 print "[II] Loading file..."
46
47 alo = AudioLoader(filename=songfname)()
48
49 audio = alo[0]
50 SR = alo[1]
51 numChannels = alo[2]
52
53
54 lx = audio[:,0]
55 rx = audio[:,1]
56
57
58 print "[II] Applying reverb"
59
60 T = 1.0/SR
61
62 d1 = int(d1t*SR)
63 da = int(dat*SR)
64
65 print "[II] Parameters: "
66 print "[II] d1: %f" % (d1/SR)
67 print "[II] g1: %f" % g1
68 print "[II] da: %f" % (da/SR)
69 print "[II] gc: %f" % gc
70 print "[II] G: %f" % G
71
72 mt = 0.002
73 m = int(mt*SR)
74
75 T60 = estimate_T60(d1,g1,gc,G,SR)
76
77
78 delta = zeros((int(4*T60*SR),))
79 delta[0] = 1
80
81
82 (ly, ry) = zafar(delta,delta,d1,g1,da,G,gc,m)
83
84 lim = max(len(ly), len(ry))
85 t = arange(0, lim)*T
86
87 padded_y = zeros(shape(t))
88 padded_y[0:len(ly)] = ly
89 padded_y[0:len(ry)] = ry
90
91 print "[II] Convovling left channel"
92 l_out = 0.3*fftconvolve(ly, lx)
93
94 print "[II] Convolving right channel"
95 r_out = 0.3*fftconvolve(ry, rx)
96
97 lim = min(len(l_out), len(r_out))
98 if numChannels == 1:
99 audio_out = l_out[0:lim]
100 else:
101 audio_out = concatenate((matrix(l_out[0:lim]).T,
102 matrix(r_out[0:lim]).T),
103 axis=1)
104
105 audio_out = audio_out/max(audio_out)
106 audio_file = Sndfile(outfname, 'w', Format('wav'), numChannels, SR)
107 audio_file.write_frames(audio_out)
108 audio_file.close()