1159b3361Sopenharmony_ci#!/usr/bin/env python 2159b3361Sopenharmony_cifrom string import * 3159b3361Sopenharmony_ci 4159b3361Sopenharmony_ciimport commands 5159b3361Sopenharmony_ciimport getopt 6159b3361Sopenharmony_ciimport os 7159b3361Sopenharmony_ciimport sys 8159b3361Sopenharmony_ci 9159b3361Sopenharmony_ci 10159b3361Sopenharmony_cidef Usage(mesg): 11159b3361Sopenharmony_ci print mesg + os.linesep 12159b3361Sopenharmony_ci 13159b3361Sopenharmony_ci print "Usage: " + os.linesep 14159b3361Sopenharmony_ci 15159b3361Sopenharmony_ci print "Mode 1. Compare output of 'lame1' and 'lame2':" 16159b3361Sopenharmony_ci print "./lametest.py [options] options_file input.wav lame1 lame2" + os.linesep 17159b3361Sopenharmony_ci 18159b3361Sopenharmony_ci print "Mode 2. Compare output of lame1 with reference solutions:" 19159b3361Sopenharmony_ci print "./lametest.py [options] options_file input.wav lame1" + os.linesep 20159b3361Sopenharmony_ci 21159b3361Sopenharmony_ci print "Mode 3. Generate reference solutions using lame1:" 22159b3361Sopenharmony_ci print "./lametest.py -m options_file input.wav lame1" + os.linesep 23159b3361Sopenharmony_ci 24159b3361Sopenharmony_ci print "options: " 25159b3361Sopenharmony_ci print " -w convert mp3's to wav's before comparison" 26159b3361Sopenharmony_ci 27159b3361Sopenharmony_ci sys.exit(0) 28159b3361Sopenharmony_ci 29159b3361Sopenharmony_ci 30159b3361Sopenharmony_ci# 31159b3361Sopenharmony_ci# compare two files, return # bytes which differ and the 32159b3361Sopenharmony_ci# number of bytes in larger file 33159b3361Sopenharmony_ci# 34159b3361Sopenharmony_cidef fdiff(name1, name2): 35159b3361Sopenharmony_ci cmd = "cmp -l " + name1 + " " + name2 + " | wc -l" 36159b3361Sopenharmony_ci # XXX either 37159b3361Sopenharmony_ci # use a combination of os.popen + read 38159b3361Sopenharmony_ci # or 39159b3361Sopenharmony_ci # write owen cmp+wc function 40159b3361Sopenharmony_ci # to replace commands.getoutput 41159b3361Sopenharmony_ci out = commands.getoutput(cmd) 42159b3361Sopenharmony_ci out = split(out, "\n") 43159b3361Sopenharmony_ci out = out[-1] 44159b3361Sopenharmony_ci if find(out, "No") == -1: 45159b3361Sopenharmony_ci diff = atof(out) 46159b3361Sopenharmony_ci 47159b3361Sopenharmony_ci status = os.access(name1, os.R_OK) 48159b3361Sopenharmony_ci if 1 != status: 49159b3361Sopenharmony_ci size1 = 0 50159b3361Sopenharmony_ci else: 51159b3361Sopenharmony_ci size1 = os.path.getsize(name1) 52159b3361Sopenharmony_ci size2 = os.path.getsize(name2) 53159b3361Sopenharmony_ci diff = diff + abs(size1 - size2) 54159b3361Sopenharmony_ci size = max(size1, size2) 55159b3361Sopenharmony_ci else: 56159b3361Sopenharmony_ci diff = -1 57159b3361Sopenharmony_ci size = 0 58159b3361Sopenharmony_ci return (diff, size) 59159b3361Sopenharmony_ci 60159b3361Sopenharmony_ci# 61159b3361Sopenharmony_ci# compare two files, return # bytes which differ and the 62159b3361Sopenharmony_ci# number of bytes in larger file 63159b3361Sopenharmony_ci# 64159b3361Sopenharmony_ci 65159b3361Sopenharmony_ci 66159b3361Sopenharmony_cidef compare(name1, name2, decode): 67159b3361Sopenharmony_ci if decode: 68159b3361Sopenharmony_ci print "converting mp3 to wav for comparison..." 69159b3361Sopenharmony_ci # XXX shouldn't we use lame1 instead of a hardcoded lame? 70159b3361Sopenharmony_ci os.system("lame --quiet --mp3input --decode " + name1) 71159b3361Sopenharmony_ci os.system("lame --quiet --mp3input --decode " + name2) 72159b3361Sopenharmony_ci name1 = name1 + ".wav" 73159b3361Sopenharmony_ci name2 = name2 + ".wav" 74159b3361Sopenharmony_ci 75159b3361Sopenharmony_ci rcode = 0 76159b3361Sopenharmony_ci diff, size = fdiff(name1, name2) 77159b3361Sopenharmony_ci if diff == 0: 78159b3361Sopenharmony_ci print "output identical: diff=%i total=%i" % (diff, size) 79159b3361Sopenharmony_ci rcode = 1 80159b3361Sopenharmony_ci elif diff > 0: 81159b3361Sopenharmony_ci print "output different: diff=%i total=%i %2.0f%%" % \ 82159b3361Sopenharmony_ci (diff, size, 100 * float(diff) / size) 83159b3361Sopenharmony_ci else: 84159b3361Sopenharmony_ci print "Error comparing files:" 85159b3361Sopenharmony_ci print "File 1: " + name1 86159b3361Sopenharmony_ci print "File 2: " + name2 87159b3361Sopenharmony_ci 88159b3361Sopenharmony_ci return rcode 89159b3361Sopenharmony_ci 90159b3361Sopenharmony_ci 91159b3361Sopenharmony_ci# 92159b3361Sopenharmony_ci# main program 93159b3361Sopenharmony_ci# 94159b3361Sopenharmony_citry: 95159b3361Sopenharmony_ci optlist, args = getopt.getopt(sys.argv[1:], 'wm') 96159b3361Sopenharmony_ciexcept getopt.error, val: 97159b3361Sopenharmony_ci Usage('ERROR: ' + val) 98159b3361Sopenharmony_ci 99159b3361Sopenharmony_cidecode = 0 100159b3361Sopenharmony_cilame2 = "none" 101159b3361Sopenharmony_cifor opt in optlist: 102159b3361Sopenharmony_ci if opt[0] == '-w': 103159b3361Sopenharmony_ci decode = 1 104159b3361Sopenharmony_ci elif opt[0] == '-m': 105159b3361Sopenharmony_ci lame2 = "makeref" 106159b3361Sopenharmony_ci print os.linesep + "Generating reference output" 107159b3361Sopenharmony_ci 108159b3361Sopenharmony_ci 109159b3361Sopenharmony_ciif len(args) < 3: 110159b3361Sopenharmony_ci Usage("Not enough arguments.") 111159b3361Sopenharmony_ciif len(args) > 4: 112159b3361Sopenharmony_ci Usage("Too many arguments.") 113159b3361Sopenharmony_ci 114159b3361Sopenharmony_ciif lame2 == "makeref": 115159b3361Sopenharmony_ci if len(args) != 3: 116159b3361Sopenharmony_ci Usage("Too many arguments for -r/-m mode.") 117159b3361Sopenharmony_cielse: 118159b3361Sopenharmony_ci if len(args) == 3: 119159b3361Sopenharmony_ci lame2 = "ref" 120159b3361Sopenharmony_ci 121159b3361Sopenharmony_ci# populate variables from args and normalize & expand path 122159b3361Sopenharmony_ciif len(args) >= 3: 123159b3361Sopenharmony_ci options_file = os.path.normpath(os.path.expanduser(args[0])) 124159b3361Sopenharmony_ci input_file = os.path.normpath(os.path.expanduser(args[1])) 125159b3361Sopenharmony_ci lame1 = os.path.normpath(os.path.expanduser(args[2])) 126159b3361Sopenharmony_ciif len(args) >= 4: 127159b3361Sopenharmony_ci lame2 = os.path.normpath(os.path.expanduser(args[3])) 128159b3361Sopenharmony_ci 129159b3361Sopenharmony_ci# check readability of options_file 130159b3361Sopenharmony_cistatus = os.access(options_file, os.R_OK) 131159b3361Sopenharmony_ciif status != 1: 132159b3361Sopenharmony_ci Usage(options_file + " not readable") 133159b3361Sopenharmony_ci 134159b3361Sopenharmony_ci# check readability of input_file 135159b3361Sopenharmony_cistatus = os.access(input_file, os.R_OK) 136159b3361Sopenharmony_ciif status != 1: 137159b3361Sopenharmony_ci Usage(input_file + " not readable") 138159b3361Sopenharmony_ci 139159b3361Sopenharmony_ci 140159b3361Sopenharmony_ci# generate searchlist of directories 141159b3361Sopenharmony_cipath = split(os.environ['PATH'], os.pathsep) 142159b3361Sopenharmony_cipath.append(os.curdir) 143159b3361Sopenharmony_ci 144159b3361Sopenharmony_ci# init indicator vars 145159b3361Sopenharmony_cilame1_ok = 0 146159b3361Sopenharmony_cilame2_ok = 0 147159b3361Sopenharmony_ci 148159b3361Sopenharmony_ci# check for executable lame1 149159b3361Sopenharmony_cifor x in path: 150159b3361Sopenharmony_ci status = os.access(os.path.join(x, lame1), os.X_OK) 151159b3361Sopenharmony_ci if status == 1: 152159b3361Sopenharmony_ci lame1_ok = 1 153159b3361Sopenharmony_ci break 154159b3361Sopenharmony_ciif lame1_ok != 1: 155159b3361Sopenharmony_ci Usage(lame1 + " is not executable") 156159b3361Sopenharmony_ci 157159b3361Sopenharmony_ciif not (lame2 == "ref" or lame2 == "makeref"): 158159b3361Sopenharmony_ci # check for executable lame2 159159b3361Sopenharmony_ci for x in path: 160159b3361Sopenharmony_ci status = os.access(os.path.join(x, lame2), os.X_OK) 161159b3361Sopenharmony_ci if status == 1: 162159b3361Sopenharmony_ci lame2_ok = 1 163159b3361Sopenharmony_ci break 164159b3361Sopenharmony_ci if lame2_ok != 1: 165159b3361Sopenharmony_ci Usage(lame2 + " is not executable") 166159b3361Sopenharmony_ci 167159b3361Sopenharmony_ci 168159b3361Sopenharmony_citmp = split(options_file, os.sep) 169159b3361Sopenharmony_citmp = tmp[-1] 170159b3361Sopenharmony_cibasename = replace(input_file, ".wav", "") 171159b3361Sopenharmony_cibasename = basename + "." + tmp 172159b3361Sopenharmony_ci 173159b3361Sopenharmony_ci 174159b3361Sopenharmony_cinum_ok = 0 175159b3361Sopenharmony_cin = 0 176159b3361Sopenharmony_cifoptions = open(options_file) 177159b3361Sopenharmony_ciline = rstrip(foptions.readline()) 178159b3361Sopenharmony_ciwhile line: 179159b3361Sopenharmony_ci n = n + 1 180159b3361Sopenharmony_ci name1 = basename + "." + str(n) + ".mp3" 181159b3361Sopenharmony_ci name2 = basename + "." + str(n) + "ref.mp3" 182159b3361Sopenharmony_ci 183159b3361Sopenharmony_ci print # empty line 184159b3361Sopenharmony_ci 185159b3361Sopenharmony_ci if lame2 == 'ref': 186159b3361Sopenharmony_ci cmd = "rm -f " + name1 187159b3361Sopenharmony_ci os.system(cmd) 188159b3361Sopenharmony_ci cmd = lame1 + " --quiet " + line + " " + input_file + " " + name1 189159b3361Sopenharmony_ci print "executable: ", lame1 190159b3361Sopenharmony_ci print "options: ", line 191159b3361Sopenharmony_ci print "input: ", input_file 192159b3361Sopenharmony_ci print "reference output:", name2 193159b3361Sopenharmony_ci print cmd 194159b3361Sopenharmony_ci os.system(cmd) 195159b3361Sopenharmony_ci num_ok = num_ok + compare(name1, name2, decode) 196159b3361Sopenharmony_ci elif lame2 == 'makeref': 197159b3361Sopenharmony_ci cmd = "rm -f " + name2 198159b3361Sopenharmony_ci os.system(cmd) 199159b3361Sopenharmony_ci print "executable: ", lame1 200159b3361Sopenharmony_ci print "options: ", line 201159b3361Sopenharmony_ci print "input: ", input_file 202159b3361Sopenharmony_ci print "output: ", name2 203159b3361Sopenharmony_ci cmd = lame1 + " --quiet " + line + " " + input_file + " " + name2 204159b3361Sopenharmony_ci os.system(cmd) 205159b3361Sopenharmony_ci else: 206159b3361Sopenharmony_ci cmd = "rm -f " + name1 207159b3361Sopenharmony_ci os.system(cmd) 208159b3361Sopenharmony_ci cmd = "rm -f " + name2 209159b3361Sopenharmony_ci os.system(cmd) 210159b3361Sopenharmony_ci print "executable: ", lame1 211159b3361Sopenharmony_ci print "executable2: ", lame2 212159b3361Sopenharmony_ci print "options: ", line 213159b3361Sopenharmony_ci print "input: ", input_file 214159b3361Sopenharmony_ci cmd1 = lame1 + " --quiet " + line + " " + input_file + " " + name1 215159b3361Sopenharmony_ci cmd2 = lame2 + " --quiet " + line + " " + input_file + " " + name2 216159b3361Sopenharmony_ci os.system(cmd1) 217159b3361Sopenharmony_ci os.system(cmd2) 218159b3361Sopenharmony_ci num_ok = num_ok + compare(name1, name2, decode) 219159b3361Sopenharmony_ci 220159b3361Sopenharmony_ci line = rstrip(foptions.readline()) 221159b3361Sopenharmony_ci 222159b3361Sopenharmony_cifoptions.close() 223159b3361Sopenharmony_ci 224159b3361Sopenharmony_ciif lame2 != 'makeref': 225159b3361Sopenharmony_ci print os.linesep + "Number of tests which passed: ", num_ok 226159b3361Sopenharmony_ci print "Number of tests which failed: ", n - num_ok 227