1""" 2/* Copyright (c) 2023 Amazon 3 Written by Jan Buethe */ 4/* 5 Redistribution and use in source and binary forms, with or without 6 modification, are permitted provided that the following conditions 7 are met: 8 9 - Redistributions of source code must retain the above copyright 10 notice, this list of conditions and the following disclaimer. 11 12 - Redistributions in binary form must reproduce the above copyright 13 notice, this list of conditions and the following disclaimer in the 14 documentation and/or other materials provided with the distribution. 15 16 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 17 ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 18 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 19 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER 20 OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 21 EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 22 PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 23 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 24 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 25 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 26 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27*/ 28""" 29 30import os 31import argparse 32 33 34from scipy.io import wavfile 35from pesq import pesq 36import numpy as np 37from moc import compare 38from moc2 import compare as compare2 39#from warpq import compute_WAPRQ as warpq 40from lace_loss_metric import compare as laceloss_compare 41 42 43parser = argparse.ArgumentParser() 44parser.add_argument('folder', type=str, help='folder with processed items') 45parser.add_argument('metric', type=str, choices=['pesq', 'moc', 'moc2', 'laceloss'], help='metric to be used for evaluation') 46 47 48def get_bitrates(folder): 49 with open(os.path.join(folder, 'bitrates.txt')) as f: 50 x = f.read() 51 52 bitrates = [int(y) for y in x.rstrip('\n').split()] 53 54 return bitrates 55 56def get_itemlist(folder): 57 with open(os.path.join(folder, 'items.txt')) as f: 58 lines = f.readlines() 59 60 items = [x.split()[0] for x in lines] 61 62 return items 63 64 65def process_item(folder, item, bitrate, metric): 66 fs, x_clean = wavfile.read(os.path.join(folder, 'clean', f"{item}_{bitrate}_clean.wav")) 67 fs, x_opus = wavfile.read(os.path.join(folder, 'opus', f"{item}_{bitrate}_opus.wav")) 68 fs, x_lace = wavfile.read(os.path.join(folder, 'lace', f"{item}_{bitrate}_lace.wav")) 69 fs, x_nolace = wavfile.read(os.path.join(folder, 'nolace', f"{item}_{bitrate}_nolace.wav")) 70 71 x_clean = x_clean.astype(np.float32) / 2**15 72 x_opus = x_opus.astype(np.float32) / 2**15 73 x_lace = x_lace.astype(np.float32) / 2**15 74 x_nolace = x_nolace.astype(np.float32) / 2**15 75 76 if metric == 'pesq': 77 result = [pesq(fs, x_clean, x_opus), pesq(fs, x_clean, x_lace), pesq(fs, x_clean, x_nolace)] 78 elif metric =='moc': 79 result = [compare(x_clean, x_opus), compare(x_clean, x_lace), compare(x_clean, x_nolace)] 80 elif metric =='moc2': 81 result = [compare2(x_clean, x_opus), compare2(x_clean, x_lace), compare2(x_clean, x_nolace)] 82 # elif metric == 'warpq': 83 # result = [warpq(x_clean, x_opus), warpq(x_clean, x_lace), warpq(x_clean, x_nolace)] 84 elif metric == 'laceloss': 85 result = [laceloss_compare(x_clean, x_opus), laceloss_compare(x_clean, x_lace), laceloss_compare(x_clean, x_nolace)] 86 else: 87 raise ValueError(f'unknown metric {metric}') 88 89 return result 90 91def process_bitrate(folder, items, bitrate, metric): 92 results = np.zeros((len(items), 3)) 93 94 for i, item in enumerate(items): 95 results[i, :] = np.array(process_item(folder, item, bitrate, metric)) 96 97 return results 98 99 100if __name__ == "__main__": 101 args = parser.parse_args() 102 103 items = get_itemlist(args.folder) 104 bitrates = get_bitrates(args.folder) 105 106 results = dict() 107 for br in bitrates: 108 print(f"processing bitrate {br}...") 109 results[br] = process_bitrate(args.folder, items, br, args.metric) 110 111 np.save(os.path.join(args.folder, f'results_{args.metric}.npy'), results) 112 113 print("Done.") 114