2 # Copyright (c) 2012 The Chromium Authors. All rights reserved.
3 # Use of this source code is governed by a BSD-style license that can be
4 # found in the LICENSE file.
6 """Audio basic playback test. Verifies basic audio output.
8 The tests plays an audio file and records its output while playing. It uses
9 SOX tool to eliminate silence from begining and end of audio recorded. Then it
10 uses PESQ tool to verify the recorded audio against expected file.
12 The output of PESQ is a pair of numbers between 0 and 5 describing how close the
13 recorded audio is to its reference file.
15 The output of this test are the PESQ values that are graphed, example:
16 RESULT audio_pesq: ref= 4.498 score
17 RESULT audio_pesq: actual= 4.547 score
31 _TEST_HTML_PATH = pyauto.PyUITest.GetFileURLForDataPath('media', 'html',
33 _MEDIA_PATH = os.path.abspath(os.path.join(pyauto.PyUITest.DataDir(),
34 'pyauto_private', 'media'))
36 # The media file to play.
37 _TEST_AUDIO = pyauto.PyUITest.GetFileURLForPath(_MEDIA_PATH, 'dance2_10sec.wav')
38 # The recording duration should be at least as the media duration. We use 5
39 # extra seconds of record in this test.
42 # Due to different noise introduced by audio recording tools on windows and
43 # linux, we require an expected audio file per platform.
44 if 'win32' in sys.platform:
45 _TEST_EXPECTED_AUDIO = os.path.join(_MEDIA_PATH, 'audio_record_win.wav')
47 _TEST_EXPECTED_AUDIO = os.path.join(_MEDIA_PATH, 'audio_record_lin.wav')
50 def GetTempFilename():
51 """Returns an absolute path to an empty temp file."""
52 f, path = tempfile.mkstemp(suffix='_actual.wav')
57 class AudioRecordPerfTest(pyauto.PyUITest):
58 """PyAuto test container. See file doc string for more information."""
60 def testBasicAudioPlaybackRecord(self):
61 """Plays an audio file and verifies its output against expected."""
62 # The 2 temp files that will be potentially used in the test.
64 file_no_silence = None
65 # Rebase test expectation file is REBASE=1 env variable is set.
66 rebase = 'REBASE' in os.environ
69 temp_file = GetTempFilename()
70 record_thread = audio_tools.AudioRecorderThread(_RECORD_DURATION,
73 self.NavigateToURL(_TEST_HTML_PATH)
74 self.assertTrue(self.ExecuteJavascript("startTest('%s');" % _TEST_AUDIO))
77 if record_thread.error:
78 self.fail(record_thread.error)
79 file_no_silence = _TEST_EXPECTED_AUDIO if rebase else GetTempFilename()
80 audio_tools.RemoveSilence(temp_file, file_no_silence)
82 # Exit if we just want to rebase expected output.
86 pesq_values = audio_tools.RunPESQ(_TEST_EXPECTED_AUDIO, file_no_silence)
88 self.fail('Test failed to get pesq results.')
89 pyauto_utils.PrintPerfResult('audio_pesq', 'ref', pesq_values[0], 'score')
90 pyauto_utils.PrintPerfResult('audio_pesq', 'actual', pesq_values[1],
93 logging.error('Test failed: %s', self.GetDOMValue('error'))
95 # Delete the temporary files used by the test.
98 if not rebase and file_no_silence:
99 os.remove(file_no_silence)
102 if __name__ == '__main__':