| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
import time |
| 13 |
import os |
| 14 |
import sys |
| 15 |
import re |
| 16 |
import subprocess |
| 17 |
import wave |
| 18 |
import shutil |
| 19 |
import inc_const as const |
| 20 |
import inc_util as util |
| 21 |
import numpy as np |
| 22 |
|
| 23 |
from inc_cfg import * |
| 24 |
from pesq import pesq |
| 25 |
|
| 26 |
|
| 27 |
cfg_file = util.load_module_from_file("cfg_file", ARGS[1]) |
| 28 |
|
| 29 |
|
| 30 |
PESQ = "tools/pesq" |
| 31 |
VISQOL = "tools/visqol" |
| 32 |
PESQ_DEFAULT_THRESHOLD = 3.4 |
| 33 |
USE_PYTHON_PESQ = 1 |
| 34 |
ENABLE_VISQOL = 1 |
| 35 |
VISQOL_DEFAULT_THRESHOLD = 4 |
| 36 |
VISQOL_MODEL_FILE = "tools/model/mod.tflite" |
| 37 |
|
| 38 |
|
| 39 |
pesq_sample_rate_opt = "" |
| 40 |
input_filename = "" |
| 41 |
output_filename = "" |
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
def test_func(t): |
| 46 |
global pesq_sample_rate_opt |
| 47 |
global input_filename |
| 48 |
global output_filename |
| 49 |
global ENABLE_VISQOL |
| 50 |
|
| 51 |
ua1 = t.process[0] |
| 52 |
ua2 = t.process[1] |
| 53 |
|
| 54 |
|
| 55 |
input_filename = re.compile(const.MEDIA_PLAY_FILE).search(ua1.inst_param.arg).group(1) |
| 56 |
|
| 57 |
|
| 58 |
output_filename = re.compile(const.MEDIA_REC_FILE).search(ua2.inst_param.arg).group(1) |
| 59 |
|
| 60 |
|
| 61 |
fin = wave.open(input_filename, "r") |
| 62 |
if fin == None: |
| 63 |
raise TestError("Failed opening input WAV file") |
| 64 |
inwavlen = fin.getnframes() * 1.0 / fin.getframerate() |
| 65 |
inwavlen += 0.2 |
| 66 |
fin.close() |
| 67 |
print("WAV input len = " + str(inwavlen) + "s") |
| 68 |
|
| 69 |
|
| 70 |
mo_clock_rate = re.compile("\.(\d+)\.wav").search(output_filename) |
| 71 |
if (mo_clock_rate is None): |
| 72 |
raise TestError("Cannot compare input & output, incorrect output filename format") |
| 73 |
clock_rate = mo_clock_rate.group(1) |
| 74 |
|
| 75 |
channel_count = 1 |
| 76 |
if re.search("--stereo", ua2.inst_param.arg) is not None: |
| 77 |
channel_count = 2 |
| 78 |
|
| 79 |
|
| 80 |
if channel_count == 2: |
| 81 |
if re.search("\.\d+\.\d+\.wav", input_filename) != None: |
| 82 |
input_filename = re.sub("\.\d+\.\d+\.wav", "." + str(channel_count) + "."+clock_rate+".wav", input_filename) |
| 83 |
else: |
| 84 |
input_filename = re.sub("\.\d+\.wav", "." + str(channel_count) + "."+clock_rate+".wav", input_filename) |
| 85 |
|
| 86 |
if (clock_rate != "8") and (clock_rate != "16"): |
| 87 |
raise TestError("PESQ only works on clock rate 8kHz or 16kHz, clock rate used = "+clock_rate+ "kHz") |
| 88 |
|
| 89 |
if (ENABLE_VISQOL == 1) and (clock_rate != "16"): |
| 90 |
print("VISQOL only works on clock rate 16kHz, clock rate used = "+ clock_rate +"kHz, disabling VISQOL") |
| 91 |
ENABLE_VISQOL = 0 |
| 92 |
|
| 93 |
|
| 94 |
pesq_sample_rate_opt = "+" + clock_rate + "000" |
| 95 |
|
| 96 |
|
| 97 |
if ua1.use_telnet: |
| 98 |
ua1.send("call new " + t.inst_params[1].uri) |
| 99 |
else: |
| 100 |
ua1.send("m") |
| 101 |
ua1.send(t.inst_params[1].uri) |
| 102 |
ua1.expect(const.STATE_CALLING) |
| 103 |
|
| 104 |
|
| 105 |
ua2.expect(const.STATE_CONFIRMED) |
| 106 |
|
| 107 |
ua1.sync_stdout() |
| 108 |
ua2.sync_stdout() |
| 109 |
time.sleep(2) |
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
ua2.send("cd 0 1") |
| 115 |
ua2.send("cd 4 0") |
| 116 |
ua2.send("cc 4 1") |
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
ua1.send("cd 0 4") |
| 122 |
ua1.send("cd 4 0") |
| 123 |
ua1.send("cc 1 4") |
| 124 |
|
| 125 |
time.sleep(inwavlen) |
| 126 |
|
| 127 |
|
| 128 |
ua2.send("cd 4 1") |
| 129 |
ua2.expect(const.MEDIA_DISCONN_PORT_SUCCESS) |
| 130 |
ua1.send("cd 1 4") |
| 131 |
ua1.expect(const.MEDIA_DISCONN_PORT_SUCCESS) |
| 132 |
|
| 133 |
def run_visqol(input_filename, output_filename): |
| 134 |
visqol_bin = os.path.abspath(VISQOL) |
| 135 |
model_file = os.path.abspath(VISQOL_MODEL_FILE) |
| 136 |
|
| 137 |
cmd = [ |
| 138 |
visqol_bin, |
| 139 |
"--similarity_to_quality_model", model_file, |
| 140 |
"--use_speech_mode", |
| 141 |
"--reference_file", input_filename, |
| 142 |
"--degraded_file", output_filename |
| 143 |
] |
| 144 |
|
| 145 |
try: |
| 146 |
process = subprocess.Popen( |
| 147 |
cmd, |
| 148 |
stdout=subprocess.PIPE, |
| 149 |
stderr=subprocess.PIPE, |
| 150 |
universal_newlines=True |
| 151 |
) |
| 152 |
stdout, stderr = process.communicate() |
| 153 |
|
| 154 |
if process.returncode != 0: |
| 155 |
raise Exception(f"ViSQOL failed with exit code {process.returncode}: {stderr}") |
| 156 |
|
| 157 |
match = re.search(r"MOS-LQO:\s+([\d\.]+)", stdout) |
| 158 |
|
| 159 |
if not match: |
| 160 |
print(f"DEBUG OUTPUT: {stdout}") |
| 161 |
raise Exception("Failed to parse MOS-LQO from ViSQOL output") |
| 162 |
|
| 163 |
return float(match.group(1)) |
| 164 |
|
| 165 |
except Exception as e: |
| 166 |
print("Visqol Error: " + str(e)) |
| 167 |
raise |
| 168 |
|
| 169 |
def read_wav_to_array(filename): |
| 170 |
with wave.open(filename, 'rb') as wf: |
| 171 |
rate = wf.getframerate() |
| 172 |
if rate != 8000 and rate != 16000: |
| 173 |
raise TestError("PESQ only works on clock rate 8kHz or 16kHz, clock rate used = "+str(rate)+ "kHz") |
| 174 |
|
| 175 |
|
| 176 |
frames = wf.readframes(wf.getnframes()) |
| 177 |
audio_array = np.frombuffer(frames, dtype=np.int16) |
| 178 |
|
| 179 |
return rate, audio_array |
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
def post_func(t): |
| 184 |
global pesq_sample_rate_opt |
| 185 |
global input_filename |
| 186 |
global output_filename |
| 187 |
pesq_test_res = 0 |
| 188 |
visqol_test_res = 0 |
| 189 |
|
| 190 |
endpt = t.process[0] |
| 191 |
|
| 192 |
|
| 193 |
if (cfg_file.pesq_threshold is not None and cfg_file.pesq_threshold > -0.5 ): |
| 194 |
pesq_threshold = cfg_file.pesq_threshold |
| 195 |
else: |
| 196 |
pesq_threshold = PESQ_DEFAULT_THRESHOLD |
| 197 |
|
| 198 |
if (USE_PYTHON_PESQ == 1): |
| 199 |
|
| 200 |
in_rate, in_data = read_wav_to_array(input_filename) |
| 201 |
out_rate, out_data = read_wav_to_array(output_filename) |
| 202 |
if (in_rate != out_rate): |
| 203 |
raise TestError("Different rate from input file = "+ in_rate + " and output file = " + out_rate) |
| 204 |
|
| 205 |
if in_rate == 8000: |
| 206 |
mode='nb' |
| 207 |
else: |
| 208 |
mode='wb' |
| 209 |
|
| 210 |
pesq_res = pesq(in_rate, in_data, out_data, mode) |
| 211 |
else: |
| 212 |
|
| 213 |
fullcmd = os.path.normpath(PESQ) + " " + pesq_sample_rate_opt + " " + input_filename + " " + output_filename |
| 214 |
endpt.trace("Popen " + fullcmd) |
| 215 |
pesq_proc = subprocess.Popen(fullcmd, shell=True, stdout=subprocess.PIPE, universal_newlines=True) |
| 216 |
pesq_out = pesq_proc.communicate() |
| 217 |
|
| 218 |
|
| 219 |
mo_pesq_out = re.compile("Prediction[^=]+=\s+([\-\d\.]+)\s*").search(pesq_out[0]) |
| 220 |
if (mo_pesq_out is None): |
| 221 |
raise TestError("Failed to fetch PESQ result") |
| 222 |
|
| 223 |
pesq_res = mo_pesq_out.group(1) |
| 224 |
|
| 225 |
if (float(pesq_res) >= pesq_threshold): |
| 226 |
endpt.trace("Success, PESQ result = " + str(pesq_res) + " (target=" + str(pesq_threshold) + ").") |
| 227 |
pesq_test_res = 1 |
| 228 |
else: |
| 229 |
endpt.trace("Failed, PESQ result = " + str(pesq_res) + " (target=" + str(pesq_threshold) + ").") |
| 230 |
|
| 231 |
if (ENABLE_VISQOL == 1): |
| 232 |
visqol_threshold = getattr(cfg_file, 'visqol_threshold', None) |
| 233 |
if (visqol_threshold is None) or (visqol_threshold <= 0 ): |
| 234 |
visqol_threshold = VISQOL_DEFAULT_THRESHOLD |
| 235 |
|
| 236 |
visqol_res = run_visqol(input_filename, output_filename) |
| 237 |
|
| 238 |
if (float(visqol_res) >= visqol_threshold): |
| 239 |
endpt.trace("Success, VISQOL result = " + str(visqol_res) + " (target=" + str(visqol_threshold) + ").") |
| 240 |
visqol_test_res = 1 |
| 241 |
else: |
| 242 |
endpt.trace("Failed, VISQOL result = " + str(visqol_res) + " (target=" + str(visqol_threshold) + ").") |
| 243 |
|
| 244 |
if (pesq_test_res == 0 and visqol_test_res == 0): |
| 245 |
|
| 246 |
wavoutname = ARGS[1] |
| 247 |
wavoutname = re.sub("[\\\/]", "_", wavoutname) |
| 248 |
wavoutname = re.sub("\.py$", ".wav", wavoutname) |
| 249 |
wavoutname = "logs/" + wavoutname |
| 250 |
try: |
| 251 |
shutil.copyfile(output_filename, wavoutname) |
| 252 |
print("Output WAV is copied to " + wavoutname) |
| 253 |
except: |
| 254 |
print("Couldn't copy output WAV, please check if 'logs' directory exists.") |
| 255 |
|
| 256 |
err_str = "WAV seems to be degraded badly, PESQ = "+ str(pesq_res) + " (target=" + str(pesq_threshold) + ")." |
| 257 |
if (ENABLE_VISQOL == 1): |
| 258 |
err_str += ", VISQOL = " + str(visqol_res) + " (target=" + str(visqol_threshold) + ")." |
| 259 |
|
| 260 |
raise TestError(err_str) |
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
test = cfg_file.test_param |
| 265 |
test.test_func = test_func |
| 266 |
test.post_func = post_func |