diff 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
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/experiment-reverb/code/apply_reverb.py	Wed Dec 14 13:15:48 2016 +0000
@@ -0,0 +1,108 @@
+# -*- coding: utf-8 -*-
+"""
+Created on Wed Mar 23 21:46:05 2016
+
+@author: Emmanouil Theofanis Chourdakis
+"""
+from mapping import *
+from essentia import Pool
+from essentia.standard import YamlInput, YamlOutput
+from scikits.audiolab import Format, Sndfile
+#from essentia import *
+from essentia.standard import *
+from sys import argv
+from ui import zafar
+from numpy import *
+from scipy.signal import fftconvolve
+
+def estimate_T60(d1, g1, gc, G, SR):
+    ga = 1/sqrt(2)
+    return d1/SR/log(g1)*log(10**-3/ga/(1-gc)/G)     
+    
+if __name__=="__main__":
+    if len(argv) != 3:
+        print("Incorrect number of arguments:")
+        print("Usage: ")
+        print("%s <parameter_file> <audio_file>")
+        print("")
+        
+        sys.exit(-1)    
+        
+    
+    songfname = argv[2]
+    outfname = songfname.replace(".wav","_reverb_wav")
+    paramfname = argv[1]
+    
+    parameters_pool = YamlInput(filename = paramfname)()
+    
+    d1t = parameters_pool['parameters.d1']*d1_max + d1_min
+    dat = parameters_pool['parameters.da']*da_max + da_min
+    g1 = parameters_pool['parameters.g1']*g1_max + g1_min
+    gc = parameters_pool['parameters.gc']*gc_max + gc_min
+    G = parameters_pool['parameters.G']*G_max + G_min       
+    
+    print "[II] Applying reverb to %s" % songfname
+    print "[II] Loading file..."    
+    
+    alo = AudioLoader(filename=songfname)()
+    
+    audio = alo[0]
+    SR = alo[1]
+    numChannels = alo[2]
+    
+    
+    lx = audio[:,0]
+    rx = audio[:,1]    
+    
+    
+    print "[II] Applying reverb"
+            
+    T = 1.0/SR    
+    
+    d1 = int(d1t*SR)
+    da = int(dat*SR)
+    
+    print "[II] Parameters: "
+    print "[II]  d1: %f" % (d1/SR)
+    print "[II]  g1: %f" % g1
+    print "[II]  da: %f" % (da/SR)
+    print "[II]  gc:  %f" % gc
+    print "[II]  G: %f" % G
+    
+    mt = 0.002
+    m = int(mt*SR)
+    
+    T60 = estimate_T60(d1,g1,gc,G,SR)
+    
+    
+    delta = zeros((int(4*T60*SR),))
+    delta[0] = 1
+    
+    
+    (ly, ry) = zafar(delta,delta,d1,g1,da,G,gc,m)    
+    
+    lim = max(len(ly), len(ry))
+    t = arange(0, lim)*T
+    
+    padded_y = zeros(shape(t))
+    padded_y[0:len(ly)] = ly
+    padded_y[0:len(ry)] = ry    
+    
+    print "[II] Convovling left channel"
+    l_out = 0.3*fftconvolve(ly, lx)
+    
+    print "[II] Convolving right channel"
+    r_out = 0.3*fftconvolve(ry, rx)
+    
+    lim = min(len(l_out), len(r_out))
+    if numChannels == 1:
+        audio_out = l_out[0:lim]
+    else:
+        audio_out = concatenate((matrix(l_out[0:lim]).T,
+                                 matrix(r_out[0:lim]).T),
+                                 axis=1)
+    
+    audio_out = audio_out/max(audio_out)
+    audio_file = Sndfile(outfname, 'w', Format('wav'), numChannels, SR)
+    audio_file.write_frames(audio_out)
+    audio_file.close()    
\ No newline at end of file