lametest.py 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. #!/usr/bin/env python
  2. from string import *
  3. import commands
  4. import getopt
  5. import os
  6. import sys
  7. def Usage(mesg):
  8. print mesg + os.linesep
  9. print "Usage: " + os.linesep
  10. print "Mode 1. Compare output of 'lame1' and 'lame2':"
  11. print "./lametest.py [options] options_file input.wav lame1 lame2" + os.linesep
  12. print "Mode 2. Compare output of lame1 with reference solutions:"
  13. print "./lametest.py [options] options_file input.wav lame1" + os.linesep
  14. print "Mode 3. Generate reference solutions using lame1:"
  15. print "./lametest.py -m options_file input.wav lame1" + os.linesep
  16. print "options: "
  17. print " -w convert mp3's to wav's before comparison"
  18. sys.exit(0)
  19. #
  20. # compare two files, return # bytes which differ and the
  21. # number of bytes in larger file
  22. #
  23. def fdiff(name1, name2):
  24. cmd = "cmp -l " + name1 + " " + name2 + " | wc -l"
  25. # XXX either
  26. # use a combination of os.popen + read
  27. # or
  28. # write owen cmp+wc function
  29. # to replace commands.getoutput
  30. out = commands.getoutput(cmd)
  31. out = split(out, "\n")
  32. out = out[-1]
  33. if find(out, "No") == -1:
  34. diff = atof(out)
  35. status = os.access(name1, os.R_OK)
  36. if 1 != status:
  37. size1 = 0
  38. else:
  39. size1 = os.path.getsize(name1)
  40. size2 = os.path.getsize(name2)
  41. diff = diff + abs(size1 - size2)
  42. size = max(size1, size2)
  43. else:
  44. diff = -1
  45. size = 0
  46. return (diff, size)
  47. #
  48. # compare two files, return # bytes which differ and the
  49. # number of bytes in larger file
  50. #
  51. def compare(name1, name2, decode):
  52. if decode:
  53. print "converting mp3 to wav for comparison..."
  54. # XXX shouldn't we use lame1 instead of a hardcoded lame?
  55. os.system("lame --quiet --mp3input --decode " + name1)
  56. os.system("lame --quiet --mp3input --decode " + name2)
  57. name1 = name1 + ".wav"
  58. name2 = name2 + ".wav"
  59. rcode = 0
  60. diff, size = fdiff(name1, name2)
  61. if diff == 0:
  62. print "output identical: diff=%i total=%i" % (diff, size)
  63. rcode = 1
  64. elif diff > 0:
  65. print "output different: diff=%i total=%i %2.0f%%" % \
  66. (diff, size, 100 * float(diff) / size)
  67. else:
  68. print "Error comparing files:"
  69. print "File 1: " + name1
  70. print "File 2: " + name2
  71. return rcode
  72. #
  73. # main program
  74. #
  75. try:
  76. optlist, args = getopt.getopt(sys.argv[1:], 'wm')
  77. except getopt.error, val:
  78. Usage('ERROR: ' + val)
  79. decode = 0
  80. lame2 = "none"
  81. for opt in optlist:
  82. if opt[0] == '-w':
  83. decode = 1
  84. elif opt[0] == '-m':
  85. lame2 = "makeref"
  86. print os.linesep + "Generating reference output"
  87. if len(args) < 3:
  88. Usage("Not enough arguments.")
  89. if len(args) > 4:
  90. Usage("Too many arguments.")
  91. if lame2 == "makeref":
  92. if len(args) != 3:
  93. Usage("Too many arguments for -r/-m mode.")
  94. else:
  95. if len(args) == 3:
  96. lame2 = "ref"
  97. # populate variables from args and normalize & expand path
  98. if len(args) >= 3:
  99. options_file = os.path.normpath(os.path.expanduser(args[0]))
  100. input_file = os.path.normpath(os.path.expanduser(args[1]))
  101. lame1 = os.path.normpath(os.path.expanduser(args[2]))
  102. if len(args) >= 4:
  103. lame2 = os.path.normpath(os.path.expanduser(args[3]))
  104. # check readability of options_file
  105. status = os.access(options_file, os.R_OK)
  106. if status != 1:
  107. Usage(options_file + " not readable")
  108. # check readability of input_file
  109. status = os.access(input_file, os.R_OK)
  110. if status != 1:
  111. Usage(input_file + " not readable")
  112. # generate searchlist of directories
  113. path = split(os.environ['PATH'], os.pathsep)
  114. path.append(os.curdir)
  115. # init indicator vars
  116. lame1_ok = 0
  117. lame2_ok = 0
  118. # check for executable lame1
  119. for x in path:
  120. status = os.access(os.path.join(x, lame1), os.X_OK)
  121. if status == 1:
  122. lame1_ok = 1
  123. break
  124. if lame1_ok != 1:
  125. Usage(lame1 + " is not executable")
  126. if not (lame2 == "ref" or lame2 == "makeref"):
  127. # check for executable lame2
  128. for x in path:
  129. status = os.access(os.path.join(x, lame2), os.X_OK)
  130. if status == 1:
  131. lame2_ok = 1
  132. break
  133. if lame2_ok != 1:
  134. Usage(lame2 + " is not executable")
  135. tmp = split(options_file, os.sep)
  136. tmp = tmp[-1]
  137. basename = replace(input_file, ".wav", "")
  138. basename = basename + "." + tmp
  139. num_ok = 0
  140. n = 0
  141. foptions = open(options_file)
  142. line = rstrip(foptions.readline())
  143. while line:
  144. n = n + 1
  145. name1 = basename + "." + str(n) + ".mp3"
  146. name2 = basename + "." + str(n) + "ref.mp3"
  147. print # empty line
  148. if lame2 == 'ref':
  149. cmd = "rm -f " + name1
  150. os.system(cmd)
  151. cmd = lame1 + " --quiet " + line + " " + input_file + " " + name1
  152. print "executable: ", lame1
  153. print "options: ", line
  154. print "input: ", input_file
  155. print "reference output:", name2
  156. print cmd
  157. os.system(cmd)
  158. num_ok = num_ok + compare(name1, name2, decode)
  159. elif lame2 == 'makeref':
  160. cmd = "rm -f " + name2
  161. os.system(cmd)
  162. print "executable: ", lame1
  163. print "options: ", line
  164. print "input: ", input_file
  165. print "output: ", name2
  166. cmd = lame1 + " --quiet " + line + " " + input_file + " " + name2
  167. os.system(cmd)
  168. else:
  169. cmd = "rm -f " + name1
  170. os.system(cmd)
  171. cmd = "rm -f " + name2
  172. os.system(cmd)
  173. print "executable: ", lame1
  174. print "executable2: ", lame2
  175. print "options: ", line
  176. print "input: ", input_file
  177. cmd1 = lame1 + " --quiet " + line + " " + input_file + " " + name1
  178. cmd2 = lame2 + " --quiet " + line + " " + input_file + " " + name2
  179. os.system(cmd1)
  180. os.system(cmd2)
  181. num_ok = num_ok + compare(name1, name2, decode)
  182. line = rstrip(foptions.readline())
  183. foptions.close()
  184. if lame2 != 'makeref':
  185. print os.linesep + "Number of tests which passed: ", num_ok
  186. print "Number of tests which failed: ", n - num_ok