-
Notifications
You must be signed in to change notification settings - Fork 25
/
pySpeechRev.py
67 lines (48 loc) · 1.67 KB
/
pySpeechRev.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
# pySpeechRev
# Mirco Ravanelli - Fondazione Bruno Kessler
# Nov 2017
# Example:
# python pySpeechRev.py clean_examples/ rev_examples/ list.txt
import numpy as np
from scipy import signal
import sys
import os
import soundfile as sf
# Load support lib
from supplib import ReadList
from supplib import copy_folder
from supplib import load_IR
from supplib import shift
# Reading input arguments
in_folder = sys.argv[1] # input folder
out_folder = sys.argv[2] # output folder
list_file = sys.argv[3] # list file ("wav_file IR_file")
# Read List file
[list_sig, list_ir] = ReadList(list_file)
# Replicate input folder structure to output folder
copy_folder(in_folder, out_folder)
# Speech Data Reverberation Loop
for i in range(len(list_sig)):
# Open clean wav file
# [fs, signal_clean] = wavfile.read(list_sig[i])
[signal_clean, fs] = sf.read(list_sig[i])
signal_clean = signal_clean.astype(np.float64)
# Signal normalization
signal_clean = signal_clean / np.max(np.abs(signal_clean))
# Open Impulse Response (IR)
IR = load_IR(list_ir[i])
# IR normalization
IR = IR / np.abs(np.max(IR))
p_max = np.argmax(np.abs(IR))
signal_rev = signal.fftconvolve(signal_clean, IR, mode="full")
# Normalization
signal_rev = signal_rev / np.max(np.abs(signal_rev))
# IR delay compensation
signal_rev = shift(signal_rev, -p_max)
# Cut reverberated signal (same length as clean sig)
signal_rev = signal_rev[0 : signal_clean.shape[0]]
# Save Reverberated Speech
file_out = list_sig[i].replace(in_folder, out_folder)
# wavfile.write(file_out,fs,signal_rev)
sf.write(file_out, signal_rev, fs)
print("Done %s" % (file_out))