Modify audio quality test.

For speaker test, change to use wav file instead of looping.
Use the xtalk stage for THDN test.

BUG=None
TEST=None

Change-Id: I19ccdf62f188c9f2d49d102c2110e2a80449f949
Reviewed-on: https://gerrit.chromium.org/gerrit/30689
Reviewed-by: Hsinyu Chao <hychao@chromium.org>
Tested-by: Hsinyu Chao <hychao@chromium.org>
diff --git a/client/site_tests/factory_AudioQuality/factory_AudioQuality.py b/client/site_tests/factory_AudioQuality/factory_AudioQuality.py
index d05baec..aa5eefb 100644
--- a/client/site_tests/factory_AudioQuality/factory_AudioQuality.py
+++ b/client/site_tests/factory_AudioQuality/factory_AudioQuality.py
@@ -18,8 +18,8 @@
 import tempfile
 import threading
 
-from autotest_lib.client.bin import test, utils
-from autotest_lib.client.common_lib import error
+from autotest_lib.client.bin import test
+from autotest_lib.client.common_lib import error, utils
 from autotest_lib.client.cros import factory_setup_modules
 from autotest_lib.client.cros.audio import audio_helper
 from cros.factory.test import factory
@@ -77,6 +77,7 @@
 
 class factory_AudioQuality(test.test):
     version = 2
+    preserve_srcdir = True
 
     def handle_connection(self, conn, *args):
         '''
@@ -115,14 +116,30 @@
         '''
         Plays a single tone.
         '''
-        cmdargs = [self._ah.sox_path, '-n', '-d', 'synth', '20.0', 'sine',
+        cmdargs = [self._ah.sox_path, '-n', '-d', 'synth', '10.0', 'sine',
                 '1000.0']
-        self._play_tone_process = subprocess.Popen(cmdargs)
+
+        factory.console.info('running %s\n' % cmdargs)
+        self._tone_job = utils.BgJob(' '.join(cmdargs))
 
     def restore_configuration(self):
         '''
         Stops all the running process and restore the mute settings.
         '''
+        if hasattr(self, '_wav_job'):
+            job = self._wav_job
+            if job:
+                utils.nuke_subprocess(job.sp)
+                utils.join_bg_jobs([job], timeout=1)
+                self._wav_job = None
+
+        if hasattr(self, '_tone_job'):
+            job = self._tone_job
+            if job:
+                utils.nuke_subprocess(job.sp)
+                utils.join_bg_jobs([job], timeout=1)
+                self._tone_job = None
+
         if hasattr(self, '_play_tone_process') and self._play_tone_process:
             self._play_tone_process.kill()
             self._play_tone_process = None
@@ -187,6 +204,11 @@
         self.ui.CallJSFunction('setMessage', _LABEL_AUDIOLOOP)
         self.start_loop()
 
+    def handle_loop_jack(self, *args):
+        self.restore_configuration()
+        self.ui.CallJSFunction('setMessage', _LABEL_AUDIOLOOP)
+        self.play_wav()
+
     def handle_loop_from_dmic(self, *args):
         self.handle_loop()
         self.ui.CallJSFunction('setMessage', _LABEL_AUDIOLOOP +
@@ -194,21 +216,33 @@
         self._ah.set_mixer_controls(self._dmic_switch_mixer_settings)
 
     def handle_loop_speaker_unmute(self, *args):
-        self.handle_loop()
+        self.restore_configuration()
+        self.ui.CallJSFunction('setMessage', _LABEL_AUDIOLOOP)
+
+        self.play_wav()
+
         self.ui.CallJSFunction('setMessage', _LABEL_AUDIOLOOP +
                 _LABEL_SPEAKER_MUTE_OFF)
         self.unmute_speaker()
 
+    def play_wav(self):
+        wav_path = os.path.join(self.srcdir, '10SEC.wav')
+        cmdargs = ['aplay', wav_path]
+        factory.console.info('running %s\n' % cmdargs)
+        self._wav_job = utils.BgJob(' '.join(cmdargs))
+
     def handle_xtalk_left(self, *args):
         self.restore_configuration()
         self.ui.CallJSFunction('setMessage', _LABEL_PLAYTONE_LEFT)
-        self.headphone_playback_switch(False, True)
+        self.playback_switch(False, True)
+        self.unmute_speaker()
         self.play_tone()
 
     def handle_xtalk_right(self, *args):
         self.restore_configuration()
         self.ui.CallJSFunction('setMessage', _LABEL_PLAYTONE_RIGHT)
-        self.headphone_playback_switch(True, False)
+        self.playback_switch(True, False)
+        self.unmute_speaker()
         self.play_tone()
 
     def listen_forever(self, sock):
@@ -236,9 +270,9 @@
         self.ui.CallJSFunction('setMessage',
                 'Ready for connection | 準備完成,等待連結')
 
-    def headphone_playback_switch(self, left=False, right=False):
+    def playback_switch(self, left=False, right=False):
         '''
-        Sets headphone playback switch values.
+        Sets playback switch values.
 
         Args:
             left: true to set left channel on.
@@ -246,7 +280,7 @@
         '''
         left_switch = 'on' if left else 'off'
         right_switch = 'on' if right else 'off'
-        mixer_settings = [{'name': "'Headphone Playback Switch'",
+        mixer_settings = [{'name': "'Speaker Playback Switch'",
                            'value': ('%s,%s' % (left_switch, right_switch))}]
         self._ah.set_mixer_controls(mixer_settings)
 
@@ -328,7 +362,7 @@
         self._handlers[_LOOP_0_RE] = self.handle_loop_none
         self._handlers[_LOOP_1_RE] = self.handle_loop_from_dmic
         self._handlers[_LOOP_2_RE] = self.handle_loop_speaker_unmute
-        self._handlers[_LOOP_3_RE] = self.handle_loop
+        self._handlers[_LOOP_3_RE] = self.handle_loop_jack
         self._handlers[_XTALK_L_RE] = self.handle_xtalk_left
         self._handlers[_XTALK_R_RE] = self.handle_xtalk_right