mod_media_playrec.py 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. # PLAYFILE -> RECFILE:
  2. # Input file is played and is recorded to output, then compare them.
  3. # Useful to tes clock rates compatibility and resample quality
  4. # null-audio
  5. # port 1: wav file input xxxxxx.clock_rate.wav, e.g: test1.8.wav
  6. # port 2: wav file ouput xxxxxx.clock_rate.wav, e.g: res1.8.wav
  7. # wav input must be more than 3 seconds long
  8. import time
  9. import sys
  10. import re
  11. import subprocess
  12. import inc_const as const
  13. import inc_util as util
  14. from inc_cfg import *
  15. # Load configuration
  16. cfg_file = util.load_module_from_file("cfg_file", ARGS[1])
  17. # WAV similarity calculator
  18. COMPARE_WAV_EXE = ""
  19. if sys.platform.find("win32")!=-1:
  20. COMPARE_WAV_EXE = "tools/cmp_wav.exe"
  21. G_INUNIX = False
  22. else:
  23. COMPARE_WAV_EXE = "tools/cmp_wav"
  24. G_INUNIX = True
  25. # Threshold to declare degradation is too high when result is lower than this value
  26. COMPARE_THRESHOLD = 2
  27. # COMPARE params
  28. input_filename = "" # Input filename
  29. output_filename = "" # Output filename
  30. # Test body function
  31. def test_func(t):
  32. global input_filename
  33. global output_filename
  34. endpt = t.process[0]
  35. # Get input file name
  36. input_filename = re.compile(const.MEDIA_PLAY_FILE).search(endpt.inst_param.arg).group(1)
  37. endpt.trace("Input file = " + input_filename)
  38. # Get output file name
  39. output_filename = re.compile(const.MEDIA_REC_FILE).search(endpt.inst_param.arg).group(1)
  40. endpt.trace("Output file = " + output_filename)
  41. # Find appropriate clock rate for the input file
  42. clock_rate = re.compile(".+(\.\d+\.wav)$").match(output_filename).group(1)
  43. if (clock_rate==None):
  44. endpt.trace("Cannot compare input & output, incorrect output filename format")
  45. return
  46. input_filename = re.sub("\.\d+\.wav$", clock_rate, input_filename)
  47. endpt.trace("WAV file to be compared with output = " + input_filename)
  48. # Connect input-output file
  49. endpt.sync_stdout()
  50. endpt.send("cc 1 2")
  51. endpt.expect(const.MEDIA_CONN_PORT_SUCCESS)
  52. # Wait
  53. time.sleep(3)
  54. endpt.sync_stdout()
  55. # Disconnect input-output file
  56. endpt.send("cd 1 2")
  57. endpt.expect(const.MEDIA_DISCONN_PORT_SUCCESS)
  58. # Post body function
  59. def post_func(t):
  60. global input_filename
  61. global output_filename
  62. endpt = t.process[0]
  63. # Check WAV similarity
  64. fullcmd = COMPARE_WAV_EXE + " " + input_filename + " " + output_filename + " " + "3000"
  65. endpt.trace("Popen " + fullcmd)
  66. cmp_proc = subprocess.Popen(fullcmd, shell=G_INUNIX, stdout=subprocess.PIPE, universal_newlines=True)
  67. # Parse similarity ouput
  68. line = cmp_proc.stdout.readline()
  69. mo_sim_val = re.match(".+=\s+(\d+)", line)
  70. if (mo_sim_val == None):
  71. raise TestError("Error comparing WAV files")
  72. return
  73. # Evaluate the similarity value
  74. sim_val = mo_sim_val.group(1)
  75. if (int(sim_val) >= COMPARE_THRESHOLD):
  76. endpt.trace("WAV similarity = " + sim_val)
  77. else:
  78. raise TestError("WAV degraded heavily, similarity = " + sim_val)
  79. # Here where it all comes together
  80. test = cfg_file.test_param
  81. test.test_func = test_func
  82. test.post_func = post_func